1139823Simp/*-
263670Snsayer * Copyright (c) 2003-2007 Tim Kientzle
363670Snsayer * All rights reserved.
463670Snsayer *
563670Snsayer * Redistribution and use in source and binary forms, with or without
663670Snsayer * modification, are permitted provided that the following conditions
763670Snsayer * are met:
863670Snsayer * 1. Redistributions of source code must retain the above copyright
963670Snsayer *    notice, this list of conditions and the following disclaimer.
1063670Snsayer * 2. Redistributions in binary form must reproduce the above copyright
1163670Snsayer *    notice, this list of conditions and the following disclaimer in the
1263670Snsayer *    documentation and/or other materials provided with the distribution.
1363670Snsayer *
1463670Snsayer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
1563670Snsayer * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1663670Snsayer * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1763670Snsayer * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
1863670Snsayer * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1963670Snsayer * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2063670Snsayer * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2163670Snsayer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2263670Snsayer * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2363670Snsayer * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2463670Snsayer */
2563670Snsayer#include "test.h"
2663670Snsayer__FBSDID("$FreeBSD: stable/10/contrib/libarchive/cpio/test/test_option_t.c 344674 2019-02-28 22:57:09Z mm $");
2763670Snsayer
2863670Snsayer#ifdef HAVE_LOCALE_H
2963670Snsayer#include <locale.h>
3063670Snsayer#endif
3163670Snsayer
3263670SnsayerDEFINE_TEST(test_option_t)
3363670Snsayer{
3463670Snsayer	char *p;
3563803Snsayer	int r;
3663670Snsayer	time_t mtime;
3763670Snsayer	char date[32];
38162711Sru	char date2[32];
3963670Snsayer
4063670Snsayer	/* List reference archive, make sure the TOC is correct. */
4163670Snsayer	extract_reference_file("test_option_t.cpio");
4263670Snsayer	r = systemf("%s -it < test_option_t.cpio >it.out 2>it.err", testprog);
43139207Sphk	assertEqualInt(r, 0);
4463670Snsayer	assertTextFileContents("1 block\n", "it.err");
4563670Snsayer	extract_reference_file("test_option_t.stdout");
4663670Snsayer	p = slurpfile(NULL, "test_option_t.stdout");
4763670Snsayer	assertTextFileContents(p, "it.out");
48129880Sphk	free(p);
4963670Snsayer
50164033Srwatson	/* We accept plain "-t" as a synonym for "-it" */
5163670Snsayer	r = systemf("%s -t < test_option_t.cpio >t.out 2>t.err", testprog);
52139207Sphk	assertEqualInt(r, 0);
5363670Snsayer	assertTextFileContents("1 block\n", "t.err");
5463670Snsayer	extract_reference_file("test_option_t.stdout");
5563670Snsayer	p = slurpfile(NULL, "test_option_t.stdout");
5663670Snsayer	assertTextFileContents(p, "t.out");
5763670Snsayer	free(p);
5863670Snsayer
5963670Snsayer	/* But "-ot" is an error. */
6083043Sbrooks	assert(0 != systemf("%s -ot < test_option_t.cpio >ot.out 2>ot.err",
6163670Snsayer			    testprog));
6263670Snsayer	assertEmptyFile("ot.out");
6363670Snsayer
6463670Snsayer	/* List reference archive verbosely, make sure the TOC is correct. */
65166497Sbms	r = systemf("%s -itv < test_option_t.cpio >tv.out 2>tv.err", testprog);
66152315Sru	assertEqualInt(r, 0);
6763670Snsayer	assertTextFileContents("1 block\n", "tv.err");
68147256Sbrooks	extract_reference_file("test_option_tv.stdout");
6963670Snsayer
7063670Snsayer	/* This doesn't work because the usernames on different systems
7163670Snsayer	 * are different and cpio now looks up numeric UIDs on
7263670Snsayer	 * the local system. */
7363670Snsayer	/* assertEqualFile("tv.out", "test_option_tv.stdout"); */
7463670Snsayer
7563670Snsayer	/* List reference archive with numeric IDs, verify TOC is correct. */
7663670Snsayer	r = systemf("%s -itnv < test_option_t.cpio >itnv.out 2>itnv.err",
7763670Snsayer		    testprog);
7863670Snsayer	assertEqualInt(r, 0);
7963670Snsayer	assertTextFileContents("1 block\n", "itnv.err");
8063670Snsayer	p = slurpfile(NULL, "itnv.out");
8183043Sbrooks	/* Since -n uses numeric UID/GID, this part should be the
82126077Sphk	 * same on every system. */
8363670Snsayer	assertEqualMem(p, "-rw-r--r--   1 1000     1000            0 ",42);
8463670Snsayer
85111742Sdes	/* Date varies depending on local timezone and locale. */
8663670Snsayer	mtime = 1;
8763670Snsayer#ifdef HAVE_LOCALE_H
88148868Srwatson	setlocale(LC_ALL, "");
89148868Srwatson#endif
90130585Sphk#if defined(_WIN32) && !defined(__CYGWIN__)
9163670Snsayer	strftime(date2, sizeof(date2)-1, "%b %d  %Y", localtime(&mtime));
9263670Snsayer	_snprintf(date, sizeof(date)-1, "%12.12s file", date2);
9393084Sbde#else
9493084Sbde	strftime(date2, sizeof(date2)-1, "%b %e  %Y", localtime(&mtime));
9593084Sbde	snprintf(date, sizeof(date)-1, "%12.12s file", date2);
9663670Snsayer#endif
97166497Sbms	assertEqualMem(p + 42, date, strlen(date));
98166497Sbms	free(p);
99166497Sbms
100166497Sbms	/* But "-n" without "-t" is an error. */
101166497Sbms	assert(0 != systemf("%s -in < test_option_t.cpio >in.out 2>in.err",
102166497Sbms			    testprog));
103166497Sbms	assertEmptyFile("in.out");
104166497Sbms}
10563670Snsayer