1248590Smm/*-
2248590Smm * Copyright (c) 2010 Tim Kientzle
3248590Smm * Copyright (c) 2012 Michihiro NAKAJIMA
4248590Smm * All rights reserved.
5248590Smm *
6248590Smm * Redistribution and use in source and binary forms, with or without
7248590Smm * modification, are permitted provided that the following conditions
8248590Smm * are met:
9248590Smm * 1. Redistributions of source code must retain the above copyright
10248590Smm *    notice, this list of conditions and the following disclaimer.
11248590Smm * 2. Redistributions in binary form must reproduce the above copyright
12248590Smm *    notice, this list of conditions and the following disclaimer in the
13248590Smm *    documentation and/or other materials provided with the distribution.
14248590Smm *
15248590Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16248590Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17248590Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18248590Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19248590Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20248590Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21248590Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22248590Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23248590Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24248590Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25248590Smm */
26248590Smm#include "test.h"
27248590Smm__FBSDID("$FreeBSD$");
28248590Smm
29248590SmmDEFINE_TEST(test_option_older_than)
30248590Smm{
31248590Smm	struct stat st;
32248590Smm
33248590Smm	/*
34248590Smm	 * Basic test of --older-than.
35248590Smm	 * First, create three files with different mtimes.
36248590Smm	 * Create test1.tar with --older-than, test2.tar without.
37248590Smm	 */
38248590Smm	assertMakeDir("test1in", 0755);
39248590Smm	assertChdir("test1in");
40248590Smm	assertMakeDir("a", 0755);
41248590Smm	assertMakeDir("a/b", 0755);
42248590Smm	assertMakeFile("old.txt", 0644, "old.txt");
43248590Smm	assertMakeFile("a/b/old.txt", 0644, "old file in old directory");
44248590Smm	assertEqualInt(0, stat("old.txt", &st));
45248590Smm	sleepUntilAfter(st.st_mtime);
46248590Smm	assertMakeFile("middle.txt", 0644, "middle.txt");
47248590Smm	assertEqualInt(0, stat("middle.txt", &st));
48248590Smm	sleepUntilAfter(st.st_mtime);
49248590Smm	assertMakeFile("new.txt", 0644, "new");
50248590Smm	assertMakeFile("a/b/new.txt", 0644, "new file in old directory");
51248590Smm
52248590Smm	/* Test --older-than on create */
53248590Smm	assertEqualInt(0,
54248590Smm		systemf("%s --format pax -cf ../test1.tar "
55248590Smm			"--older-than middle.txt *.txt a",
56248590Smm			testprog));
57248590Smm	assertEqualInt(0,
58248590Smm		systemf("%s --format pax -cf ../test2.tar *.txt a",
59248590Smm			testprog));
60248590Smm	assertChdir("..");
61248590Smm
62248590Smm	/* Extract test1.tar to a clean dir and verify what got archived. */
63248590Smm	assertMakeDir("test1out", 0755);
64248590Smm	assertChdir("test1out");
65248590Smm	assertEqualInt(0, systemf("%s xf ../test1.tar", testprog));
66248590Smm	assertFileNotExists("new.txt");
67248590Smm	assertFileNotExists("a/b/new.txt");
68248590Smm	assertFileNotExists("middle.txt");
69248590Smm	assertFileExists("old.txt");
70248590Smm	assertFileExists("a/b/old.txt");
71248590Smm	assertChdir("..");
72248590Smm
73248590Smm	/* Extract test2.tar to a clean dir with --older-than and verify. */
74248590Smm	assertMakeDir("test2out", 0755);
75248590Smm	assertChdir("test2out");
76248590Smm	assertEqualInt(0,
77248590Smm		systemf("%s xf ../test2.tar --older-than ../test1in/middle.txt",
78248590Smm			testprog));
79248590Smm	assertFileNotExists("new.txt");
80248590Smm	assertFileNotExists("a/b/new.txt");
81248590Smm	assertFileNotExists("middle.txt");
82248590Smm	assertFileExists("old.txt");
83248590Smm	assertFileExists("a/b/old.txt");
84248590Smm	assertChdir("..");
85248590Smm}
86