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$");
27 
28 /*
29  * Test that --version option works and generates reasonable output.
30  */
31 
32 static void
33 verify(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, "-- ", 3);
63 	q += 3; s -= 3;
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 	/* All terminated by end-of-line: \r, \r\n, or \n */
78 	assert(s >= 1);
79 	failure("Version: %s", p);
80 	if (*q == '\x0d') {
81 		if (q[1] != '\0')
82 			assertEqualMem(q, "\x0d\x0a", 2);
83 	} else
84 		assertEqualMem(q, "\x0a", 1);
85 }
86 
87 
88 DEFINE_TEST(test_option_version)
89 {
90 	int r;
91 	char *p;
92 	size_t s;
93 
94 	r = systemf("%s --version >version.stdout 2>version.stderr", testprog);
95 	if (r != 0)
96 		r = systemf("%s -W version >version.stdout 2>version.stderr",
97 		    testprog);
98 	failure("Unable to run either %s --version or %s -W version",
99 	    testprog, testprog);
100 	if (!assert(r == 0))
101 		return;
102 
103 	/* --version should generate nothing to stderr. */
104 	assertEmptyFile("version.stderr");
105 	/* Verify format of version message. */
106 	p = slurpfile(&s, "version.stdout");
107 	verify(p, s);
108 	free(p);
109 }
110