1228753Smm/*-
2228753Smm * Copyright (c) 2003-2007 Tim Kientzle
3228753Smm * All rights reserved.
4228753Smm *
5228753Smm * Redistribution and use in source and binary forms, with or without
6228753Smm * modification, are permitted provided that the following conditions
7228753Smm * are met:
8228753Smm * 1. Redistributions of source code must retain the above copyright
9228753Smm *    notice, this list of conditions and the following disclaimer.
10228753Smm * 2. Redistributions in binary form must reproduce the above copyright
11228753Smm *    notice, this list of conditions and the following disclaimer in the
12228753Smm *    documentation and/or other materials provided with the distribution.
13228753Smm *
14228753Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15228753Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16228753Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17228753Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18228753Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19228753Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20228753Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21228753Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22228753Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23228753Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24228753Smm */
25228753Smm#include "test.h"
26228753Smm__FBSDID("$FreeBSD$");
27228753Smm
28232153Smm#ifdef HAVE_LOCALE_H
29232153Smm#include <locale.h>
30232153Smm#endif
31228753Smm
32228753SmmDEFINE_TEST(test_option_t)
33228753Smm{
34228753Smm	char *p;
35228753Smm	int r;
36232153Smm	time_t mtime;
37232153Smm	char date[32];
38232153Smm	char date2[32];
39228753Smm
40228753Smm	/* List reference archive, make sure the TOC is correct. */
41228753Smm	extract_reference_file("test_option_t.cpio");
42228753Smm	r = systemf("%s -it < test_option_t.cpio >it.out 2>it.err", testprog);
43228753Smm	assertEqualInt(r, 0);
44228753Smm	assertTextFileContents("1 block\n", "it.err");
45228753Smm	extract_reference_file("test_option_t.stdout");
46228753Smm	p = slurpfile(NULL, "test_option_t.stdout");
47228753Smm	assertTextFileContents(p, "it.out");
48228753Smm	free(p);
49228753Smm
50228753Smm	/* We accept plain "-t" as a synonym for "-it" */
51228753Smm	r = systemf("%s -t < test_option_t.cpio >t.out 2>t.err", testprog);
52228753Smm	assertEqualInt(r, 0);
53228753Smm	assertTextFileContents("1 block\n", "t.err");
54228753Smm	extract_reference_file("test_option_t.stdout");
55228753Smm	p = slurpfile(NULL, "test_option_t.stdout");
56228753Smm	assertTextFileContents(p, "t.out");
57228753Smm	free(p);
58228753Smm
59228753Smm	/* But "-ot" is an error. */
60228753Smm	assert(0 != systemf("%s -ot < test_option_t.cpio >ot.out 2>ot.err",
61228753Smm			    testprog));
62228753Smm	assertEmptyFile("ot.out");
63228753Smm
64228753Smm	/* List reference archive verbosely, make sure the TOC is correct. */
65228753Smm	r = systemf("%s -itv < test_option_t.cpio >tv.out 2>tv.err", testprog);
66228753Smm	assertEqualInt(r, 0);
67228753Smm	assertTextFileContents("1 block\n", "tv.err");
68228753Smm	extract_reference_file("test_option_tv.stdout");
69228753Smm
70228753Smm	/* This doesn't work because the usernames on different systems
71228753Smm	 * are different and cpio now looks up numeric UIDs on
72228753Smm	 * the local system. */
73228753Smm	/* assertEqualFile("tv.out", "test_option_tv.stdout"); */
74228753Smm
75228753Smm	/* List reference archive with numeric IDs, verify TOC is correct. */
76228753Smm	r = systemf("%s -itnv < test_option_t.cpio >itnv.out 2>itnv.err",
77228753Smm		    testprog);
78228753Smm	assertEqualInt(r, 0);
79228753Smm	assertTextFileContents("1 block\n", "itnv.err");
80228753Smm	p = slurpfile(NULL, "itnv.out");
81228753Smm	/* Since -n uses numeric UID/GID, this part should be the
82228753Smm	 * same on every system. */
83228753Smm	assertEqualMem(p, "-rw-r--r--   1 1000     1000            0 ",42);
84232153Smm
85232153Smm	/* Date varies depending on local timezone and locale. */
86232153Smm	mtime = 1;
87232153Smm#ifdef HAVE_LOCALE_H
88232153Smm	setlocale(LC_ALL, "");
89232153Smm#endif
90232153Smm#if defined(_WIN32) && !defined(__CYGWIN__)
91232153Smm	strftime(date2, sizeof(date), "%b %d  %Y", localtime(&mtime));
92232153Smm	_snprintf(date, sizeof(date)-1, "%12s file", date2);
93232153Smm#else
94232153Smm	strftime(date2, sizeof(date), "%b %e  %Y", localtime(&mtime));
95232153Smm	snprintf(date, sizeof(date)-1, "%12s file", date2);
96232153Smm#endif
97232153Smm	assertEqualMem(p + 42, date, strlen(date));
98228753Smm	free(p);
99228753Smm
100228753Smm	/* But "-n" without "-t" is an error. */
101228753Smm	assert(0 != systemf("%s -in < test_option_t.cpio >in.out 2>in.err",
102228753Smm			    testprog));
103228753Smm	assertEmptyFile("in.out");
104228753Smm}
105