xref: /freebsd/contrib/libarchive/cat/test/test_version.c (revision 907b59d76938e654f0d040a888e8dfca3de1e222)
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 /*
28  * Test that --version option works and generates reasonable output.
29  */
30 
31 DEFINE_TEST(test_version)
32 {
33 	int r;
34 	char *p, *q;
35 	size_t s;
36 
37 
38 	r = systemf("%s --version >version.stdout 2>version.stderr", testprog);
39 	failure("Unable to run %s --version", testprog);
40 	if (!assert(r == 0))
41 		return;
42 
43 	/* --version should generate nothing to stdout. */
44 	assertEmptyFile("version.stderr");
45 	/* Verify format of version message. */
46 	q = p = slurpfile(&s, "version.stdout");
47 	/* Version message should start with name of program, then space. */
48 	assert(s > 6);
49 	failure("Version must start with 'bsdcat': ``%s''", p);
50 	if (!assertEqualMem(q, "bsdcat ", 7))
51 		return;
52 	q += 7; s -= 7;
53 	/* Version number is a series of digits and periods. */
54 	while (s > 0 && (*q == '.' || (*q >= '0' && *q <= '9'))) {
55 		++q;
56 		--s;
57 	}
58 	/* Version number terminated by space. */
59 	failure("No space after bsdcat version: ``%s''", p);
60 	assert(s > 1);
61 	/* Skip a single trailing a,b,c, or d. */
62 	if (*q == 'a' || *q == 'b' || *q == 'c' || *q == 'd')
63 		++q;
64 	failure("No space after bsdcat version: ``%s''", p);
65 	assert(*q == ' ');
66 	++q; --s;
67 	/* Separator. */
68 	failure("No `-' between bsdcat and libarchive versions: ``%s''", p);
69 	assertEqualMem(q, "- ", 2);
70 	q += 2; s -= 2;
71 	/* libarchive name and version number */
72 	failure("Not long enough for libarchive version: ``%s''", p);
73 	assert(s > 11);
74 	failure("Libarchive version must start with `libarchive': ``%s''", p);
75 	assertEqualMem(q, "libarchive ", 11);
76 	q += 11; s -= 11;
77 	/* Version number is a series of digits and periods. */
78 	while (s > 0 && (*q == '.' || (*q >= '0' && *q <= '9'))) {
79 		++q;
80 		--s;
81 	}
82 	/* Skip a single trailing a,b,c, or d. */
83 	if (*q == 'a' || *q == 'b' || *q == 'c' || *q == 'd')
84 		++q;
85 	/* Skip arbitrary third-party version numbers. */
86 	while (s > 0 && (*q == ' ' || *q == '/' || *q == '.' || isalnum(*q))) {
87 		++q;
88 		--s;
89 	}
90 	/* All terminated by end-of-line. */
91 	assert(s >= 1);
92 	/* Skip an optional CR character (e.g., Windows) */
93 	failure("Version output must end with \\n or \\r\\n");
94 	if (*q == '\r') { ++q; --s; }
95 	assertEqualMem(q, "\n", 1);
96 	free(p);
97 }
98