1231200Smm/*-
2231200Smm * Copyright (c) 2010 Tim Kientzle
3231200Smm * All rights reserved.
4231200Smm *
5231200Smm * Redistribution and use in source and binary forms, with or without
6231200Smm * modification, are permitted provided that the following conditions
7231200Smm * are met:
8231200Smm * 1. Redistributions of source code must retain the above copyright
9231200Smm *    notice, this list of conditions and the following disclaimer.
10231200Smm * 2. Redistributions in binary form must reproduce the above copyright
11231200Smm *    notice, this list of conditions and the following disclaimer in the
12231200Smm *    documentation and/or other materials provided with the distribution.
13231200Smm *
14231200Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15231200Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16231200Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17231200Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18231200Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19231200Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20231200Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21231200Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22231200Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23231200Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24231200Smm */
25231200Smm#include "test.h"
26231200Smm__FBSDID("$FreeBSD$");
27231200Smm
28231200SmmDEFINE_TEST(test_option_newer_than)
29231200Smm{
30231200Smm  struct stat st;
31231200Smm
32231200Smm  /*
33231200Smm   * Basic test of --newer-than.
34231200Smm   * First, create three files with different mtimes.
35231200Smm   * Create test1.tar with --newer-than, test2.tar without.
36231200Smm   */
37231200Smm  assertMakeDir("test1in", 0755);
38231200Smm  assertChdir("test1in");
39231200Smm  assertMakeDir("a", 0755);
40231200Smm  assertMakeDir("a/b", 0755);
41231200Smm  assertMakeFile("old.txt", 0644, "old.txt");
42231200Smm  assertEqualInt(0, stat("old.txt", &st));
43231200Smm  sleepUntilAfter(st.st_mtime);
44231200Smm  assertMakeFile("middle.txt", 0644, "middle.txt");
45231200Smm  assertEqualInt(0, stat("middle.txt", &st));
46231200Smm  sleepUntilAfter(st.st_mtime);
47231200Smm  assertMakeFile("new.txt", 0644, "new");
48231200Smm  assertMakeFile("a/b/new.txt", 0644, "new file in old directory");
49231200Smm
50231200Smm  /* Test --newer-than on create */
51248616Smm  assertEqualInt(0,
52248616Smm	systemf("%s --format pax -cf ../test1.tar "
53248616Smm		"--newer-than middle.txt *.txt a", testprog));
54248616Smm  assertEqualInt(0,
55248616Smm	systemf("%s --format pax -cf ../test2.tar *.txt a", testprog));
56231200Smm  assertChdir("..");
57231200Smm
58231200Smm  /* Extract test1.tar to a clean dir and verify what got archived. */
59231200Smm  assertMakeDir("test1out", 0755);
60231200Smm  assertChdir("test1out");
61231200Smm  assertEqualInt(0, systemf("%s xf ../test1.tar", testprog));
62231200Smm  assertFileExists("new.txt");
63231200Smm  assertFileExists("a/b/new.txt");
64231200Smm  assertFileNotExists("middle.txt");
65231200Smm  assertFileNotExists("old.txt");
66231200Smm  assertChdir("..");
67231200Smm
68231200Smm  /* Extract test2.tar to a clean dir with --newer-than and verify. */
69231200Smm  assertMakeDir("test2out", 0755);
70231200Smm  assertChdir("test2out");
71231200Smm  assertEqualInt(0, systemf("%s xf ../test2.tar --newer-than ../test1in/middle.txt", testprog));
72231200Smm  assertFileExists("new.txt");
73231200Smm  assertFileExists("a/b/new.txt");
74231200Smm  assertFileNotExists("middle.txt");
75231200Smm  assertFileNotExists("old.txt");
76231200Smm  assertChdir("..");
77231200Smm
78231200Smm}
79