test_option_version.c revision 305188
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__FBSDID("$FreeBSD: stable/11/contrib/libarchive/cpio/test/test_option_version.c 305188 2016-09-01 07:53:59Z mm $");
27
28/*
29 * Test that --version option works and generates reasonable output.
30 */
31
32static void
33verify(const char *p, size_t s)
34{
35	const char *q = p;
36
37	/* Version message should start with name of program, then space. */
38	failure("version message too short:", p);
39	if (!assert(s > 6))
40		return;
41	failure("Version message should begin with 'bsdcpio': %s", p);
42	if (!assertEqualMem(q, "bsdcpio ", 8))
43		/* If we're not testing bsdcpio, don't keep going. */
44		return;
45	q += 8; s -= 8;
46	/* Version number is a series of digits and periods. */
47	while (s > 0 && (*q == '.' || (*q >= '0' && *q <= '9'))) {
48		++q;
49		--s;
50	}
51	/* Version number terminated by space. */
52	failure("Version: %s", p);
53	assert(s > 1);
54	/* Skip a single trailing a,b,c, or d. */
55	if (*q == 'a' || *q == 'b' || *q == 'c' || *q == 'd')
56		++q;
57	failure("Version: %s", p);
58	assert(*q == ' ');
59	++q; --s;
60	/* Separator. */
61	failure("Version: %s", p);
62	assertEqualMem(q, "- ", 2);
63	q += 2; s -= 2;
64	/* libarchive name and version number */
65	assert(s > 11);
66	failure("Version: %s", p);
67	assertEqualMem(q, "libarchive ", 11);
68	q += 11; s -= 11;
69	/* Version number is a series of digits and periods. */
70	while (s > 0 && (*q == '.' || (*q >= '0' && *q <= '9'))) {
71		++q;
72		--s;
73	}
74	/* Skip a single trailing a,b,c, or d. */
75	if (*q == 'a' || *q == 'b' || *q == 'c' || *q == 'd')
76		++q;
77	/* Skip arbitrary third-party version numbers. */
78	while (s > 0 && (*q == ' ' || *q == '-' || *q == '/' || *q == '.' || isalnum(*q))) {
79		++q;
80		--s;
81	}
82	/* All terminated by end-of-line: \r, \r\n, or \n */
83	assert(s >= 1);
84	failure("Version: %s", p);
85	if (*q == '\x0d') {
86		if (q[1] != '\0')
87			assertEqualMem(q, "\x0d\x0a", 2);
88	} else
89		assertEqualMem(q, "\x0a", 1);
90}
91
92
93DEFINE_TEST(test_option_version)
94{
95	int r;
96	char *p;
97	size_t s;
98
99	r = systemf("%s --version >version.stdout 2>version.stderr", testprog);
100	if (r != 0)
101		r = systemf("%s -W version >version.stdout 2>version.stderr",
102		    testprog);
103	failure("Unable to run either %s --version or %s -W version",
104	    testprog, testprog);
105	if (!assert(r == 0))
106		return;
107
108	/* --version should generate nothing to stderr. */
109	assertEmptyFile("version.stderr");
110	/* Verify format of version message. */
111	p = slurpfile(&s, "version.stdout");
112	verify(p, s);
113	free(p);
114}
115