Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / libarchive / dist / cpio / test / test_option_version.c
blob95258eb7e8276e8fca1ddf4691c217014c391942
1 /*-
2 * Copyright (c) 2003-2007 Tim Kientzle
3 * All rights reserved.
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.
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.
25 #include "test.h"
26 __FBSDID("$FreeBSD$");
29 * Test that --version option works and generates reasonable output.
32 static void
33 verify(const char *p, size_t s)
35 const char *q = p;
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;
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;
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 a newline. */
78 assert(s >= 1);
79 failure("Version: %s", p);
80 assertEqualMem(q, "\n", 1);
84 DEFINE_TEST(test_option_version)
86 int r;
87 char *p;
88 size_t s;
90 r = systemf("%s --version >version.stdout 2>version.stderr", testprog);
91 if (r != 0)
92 r = systemf("%s -W version >version.stdout 2>version.stderr",
93 testprog);
94 failure("Unable to run either %s --version or %s -W version",
95 testprog, testprog);
96 if (!assert(r == 0))
97 return;
99 /* --version should generate nothing to stderr. */
100 assertEmptyFile("version.stderr");
101 /* Verify format of version message. */
102 p = slurpfile(&s, "version.stdout");
103 verify(p, s);
104 free(p);