1/*-
2 * Copyright (c) 2003-2007 Tim Kientzle
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25#include "test.h"
26
27#ifdef HAVE_LOCALE_H
28#include <locale.h>
29#endif
30
31DEFINE_TEST(test_option_t)
32{
33	char *p;
34	int r;
35	time_t mtime;
36	char date[32];
37	char date2[32];
38	struct tm *tmptr;
39#if defined(HAVE_LOCALTIME_R) || defined(HAVE_LOCALTIME_S)
40	struct tm tmbuf;
41#endif
42
43	/* List reference archive, make sure the TOC is correct. */
44	extract_reference_file("test_option_t.cpio");
45	r = systemf("%s -it < test_option_t.cpio >it.out 2>it.err", testprog);
46	assertEqualInt(r, 0);
47	assertTextFileContents("1 block\n", "it.err");
48	extract_reference_file("test_option_t.stdout");
49	p = slurpfile(NULL, "test_option_t.stdout");
50	assertTextFileContents(p, "it.out");
51	free(p);
52
53	/* We accept plain "-t" as a synonym for "-it" */
54	r = systemf("%s -t < test_option_t.cpio >t.out 2>t.err", testprog);
55	assertEqualInt(r, 0);
56	assertTextFileContents("1 block\n", "t.err");
57	extract_reference_file("test_option_t.stdout");
58	p = slurpfile(NULL, "test_option_t.stdout");
59	assertTextFileContents(p, "t.out");
60	free(p);
61
62	/* But "-ot" is an error. */
63	assert(0 != systemf("%s -ot < test_option_t.cpio >ot.out 2>ot.err",
64			    testprog));
65	assertEmptyFile("ot.out");
66
67	/* List reference archive verbosely, make sure the TOC is correct. */
68	r = systemf("%s -itv < test_option_t.cpio >tv.out 2>tv.err", testprog);
69	assertEqualInt(r, 0);
70	assertTextFileContents("1 block\n", "tv.err");
71	extract_reference_file("test_option_tv.stdout");
72
73	/* This doesn't work because the usernames on different systems
74	 * are different and cpio now looks up numeric UIDs on
75	 * the local system. */
76	/* assertEqualFile("tv.out", "test_option_tv.stdout"); */
77
78	/* List reference archive with numeric IDs, verify TOC is correct. */
79	r = systemf("%s -itnv < test_option_t.cpio >itnv.out 2>itnv.err",
80		    testprog);
81	assertEqualInt(r, 0);
82	assertTextFileContents("1 block\n", "itnv.err");
83	p = slurpfile(NULL, "itnv.out");
84	/* Since -n uses numeric UID/GID, this part should be the
85	 * same on every system. */
86	assertEqualMem(p, "-rw-r--r--   1 1000     1000            0 ",42);
87
88	/* Date varies depending on local timezone and locale. */
89	mtime = 1;
90#ifdef HAVE_LOCALE_H
91	setlocale(LC_ALL, "");
92#endif
93#if defined(HAVE_LOCALTIME_S)
94        tmptr = localtime_s(&tmbuf, &mtime) ? NULL : &tmbuf;
95#elif defined(HAVE_LOCALTIME_R)
96        tmptr = localtime_r(&mtime, &tmbuf);
97#else
98        tmptr = localtime(&mtime);
99#endif
100#if defined(_WIN32) && !defined(__CYGWIN__)
101	strftime(date2, sizeof(date2)-1, "%b %d  %Y", tmptr);
102	_snprintf(date, sizeof(date)-1, "%12.12s file", date2);
103#else
104	strftime(date2, sizeof(date2)-1, "%b %e  %Y", tmptr);
105	snprintf(date, sizeof(date)-1, "%12.12s file", date2);
106#endif
107	assertEqualMem(p + 42, date, strlen(date));
108	free(p);
109
110	/* But "-n" without "-t" is an error. */
111	assert(0 != systemf("%s -in < test_option_t.cpio >in.out 2>in.err",
112			    testprog));
113	assertEmptyFile("in.out");
114}
115