157580Smjacob/*-
257580Smjacob * Copyright (c) 2010 Tim Kientzle
357580Smjacob * Copyright (c) 2012 Michihiro NAKAJIMA
457580Smjacob * All rights reserved.
557580Smjacob *
657580Smjacob * Redistribution and use in source and binary forms, with or without
757580Smjacob * modification, are permitted provided that the following conditions
857580Smjacob * are met:
957580Smjacob * 1. Redistributions of source code must retain the above copyright
1057580Smjacob *    notice, this list of conditions and the following disclaimer.
1157580Smjacob * 2. Redistributions in binary form must reproduce the above copyright
1257580Smjacob *    notice, this list of conditions and the following disclaimer in the
1357580Smjacob *    documentation and/or other materials provided with the distribution.
1457580Smjacob *
1557580Smjacob * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
1657580Smjacob * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1757580Smjacob * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1857580Smjacob * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
1957580Smjacob * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2057580Smjacob * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2157580Smjacob * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2257580Smjacob * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2357580Smjacob * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2457580Smjacob * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2557580Smjacob */
2657580Smjacob#include "test.h"
2757580Smjacob__FBSDID("$FreeBSD$");
2857580Smjacob
2957580SmjacobDEFINE_TEST(test_option_older_than)
3057580Smjacob{
3157580Smjacob	struct stat st;
3257580Smjacob
3357580Smjacob	/*
3457580Smjacob	 * Basic test of --older-than.
3557580Smjacob	 * First, create three files with different mtimes.
36235911Smav	 * Create test1.tar with --older-than, test2.tar without.
37235911Smav	 */
3857580Smjacob	assertMakeDir("test1in", 0755);
3957580Smjacob	assertChdir("test1in");
4057580Smjacob	assertMakeDir("a", 0755);
4157580Smjacob	assertMakeDir("a/b", 0755);
42235911Smav	assertMakeFile("old.txt", 0644, "old.txt");
43235911Smav	assertMakeFile("a/b/old.txt", 0644, "old file in old directory");
4457580Smjacob	assertEqualInt(0, stat("old.txt", &st));
4557580Smjacob	sleepUntilAfter(st.st_mtime);
46198934Sdelphij	assertMakeFile("middle.txt", 0644, "middle.txt");
4757580Smjacob	assertEqualInt(0, stat("middle.txt", &st));
4857580Smjacob	sleepUntilAfter(st.st_mtime);
4957580Smjacob	assertMakeFile("new.txt", 0644, "new");
50235911Smav	assertMakeFile("a/b/new.txt", 0644, "new file in old directory");
5157580Smjacob
5257580Smjacob	/* Test --older-than on create */
5357580Smjacob	assertEqualInt(0,
5457580Smjacob		systemf("%s --format pax -cf ../test1.tar "
5557580Smjacob			"--older-than middle.txt *.txt a",
5657580Smjacob			testprog));
5757580Smjacob	assertEqualInt(0,
5857580Smjacob		systemf("%s --format pax -cf ../test2.tar *.txt a",
5957580Smjacob			testprog));
6057580Smjacob	assertChdir("..");
6157580Smjacob
6257580Smjacob	/* Extract test1.tar to a clean dir and verify what got archived. */
63235911Smav	assertMakeDir("test1out", 0755);
64235911Smav	assertChdir("test1out");
65235911Smav	assertEqualInt(0, systemf("%s xf ../test1.tar", testprog));
6657580Smjacob	assertFileNotExists("new.txt");
6757580Smjacob	assertFileNotExists("a/b/new.txt");
6857580Smjacob	assertFileNotExists("middle.txt");
6957580Smjacob	assertFileExists("old.txt");
70	assertFileExists("a/b/old.txt");
71	assertChdir("..");
72
73	/* Extract test2.tar to a clean dir with --older-than and verify. */
74	assertMakeDir("test2out", 0755);
75	assertChdir("test2out");
76	assertEqualInt(0,
77		systemf("%s xf ../test2.tar --older-than ../test1in/middle.txt",
78			testprog));
79	assertFileNotExists("new.txt");
80	assertFileNotExists("a/b/new.txt");
81	assertFileNotExists("middle.txt");
82	assertFileExists("old.txt");
83	assertFileExists("a/b/old.txt");
84	assertChdir("..");
85}
86