1228753Smm/*-
2228753Smm * Copyright (c) 2003-2007 Tim Kientzle
3228753Smm * All rights reserved.
4228753Smm *
5228753Smm * Redistribution and use in source and binary forms, with or without
6228753Smm * modification, are permitted provided that the following conditions
7228753Smm * are met:
8228753Smm * 1. Redistributions of source code must retain the above copyright
9228753Smm *    notice, this list of conditions and the following disclaimer.
10228753Smm * 2. Redistributions in binary form must reproduce the above copyright
11228753Smm *    notice, this list of conditions and the following disclaimer in the
12228753Smm *    documentation and/or other materials provided with the distribution.
13228753Smm *
14228753Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15228753Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16228753Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17228753Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18228753Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19228753Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20228753Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21228753Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22228753Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23228753Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24228753Smm */
25228753Smm#include "test.h"
26228763Smm__FBSDID("$FreeBSD: releng/11.0/contrib/libarchive/tar/test/test_version.c 305313 2016-09-03 00:27:41Z mm $");
27228753Smm
28228753Smm/*
29228753Smm * Test that --version option works and generates reasonable output.
30228753Smm */
31228753Smm
32228753SmmDEFINE_TEST(test_version)
33228753Smm{
34228753Smm	int r;
35228753Smm	char *p, *q;
36228753Smm	size_t s;
37228753Smm
38228753Smm
39228753Smm	r = systemf("%s --version >version.stdout 2>version.stderr", testprog);
40228753Smm	if (r != 0)
41228753Smm		r = systemf("%s -W version >version.stdout 2>version.stderr",
42228753Smm		    testprog);
43228753Smm	failure("Unable to run either %s --version or %s -W version",
44228753Smm	    testprog, testprog);
45228753Smm	if (!assert(r == 0))
46228753Smm		return;
47228753Smm
48228753Smm	/* --version should generate nothing to stdout. */
49228753Smm	assertEmptyFile("version.stderr");
50228753Smm	/* Verify format of version message. */
51228753Smm	q = p = slurpfile(&s, "version.stdout");
52228753Smm	/* Version message should start with name of program, then space. */
53228753Smm	assert(s > 6);
54228753Smm	failure("Version must start with 'bsdtar': ``%s''", p);
55228753Smm	if (!assertEqualMem(q, "bsdtar ", 7))
56228753Smm		return;
57228753Smm	q += 7; s -= 7;
58228753Smm	/* Version number is a series of digits and periods. */
59228753Smm	while (s > 0 && (*q == '.' || (*q >= '0' && *q <= '9'))) {
60228753Smm		++q;
61228753Smm		--s;
62228753Smm	}
63228753Smm	/* Version number terminated by space. */
64228753Smm	failure("No space after bsdtar version: ``%s''", p);
65228753Smm	assert(s > 1);
66228753Smm	/* Skip a single trailing a,b,c, or d. */
67228753Smm	if (*q == 'a' || *q == 'b' || *q == 'c' || *q == 'd')
68228753Smm		++q;
69228753Smm	failure("No space after bsdtar version: ``%s''", p);
70228753Smm	assert(*q == ' ');
71228753Smm	++q; --s;
72228753Smm	/* Separator. */
73228753Smm	failure("No `-' between bsdtar and libarchive versions: ``%s''", p);
74228753Smm	assertEqualMem(q, "- ", 2);
75228753Smm	q += 2; s -= 2;
76228753Smm	/* libarchive name and version number */
77228753Smm	failure("Not long enough for libarchive version: ``%s''", p);
78228753Smm	assert(s > 11);
79228753Smm	failure("Libarchive version must start with `libarchive': ``%s''", p);
80228753Smm	assertEqualMem(q, "libarchive ", 11);
81228753Smm	q += 11; s -= 11;
82228753Smm	/* Version number is a series of digits and periods. */
83228753Smm	while (s > 0 && (*q == '.' || (*q >= '0' && *q <= '9'))) {
84228753Smm		++q;
85228753Smm		--s;
86228753Smm	}
87228753Smm	/* Skip a single trailing a,b,c, or d. */
88228753Smm	if (*q == 'a' || *q == 'b' || *q == 'c' || *q == 'd')
89228753Smm		++q;
90299529Smm	/* Skip arbitrary third-party version numbers. */
91305313Smm	while (s > 0 && (*q == ' ' || *q == '-' || *q == '/' || *q == '.' || isalnum(*q))) {
92299529Smm		++q;
93299529Smm		--s;
94299529Smm	}
95228753Smm	/* All terminated by end-of-line. */
96228753Smm	assert(s >= 1);
97228753Smm	/* Skip an optional CR character (e.g., Windows) */
98228753Smm	failure("Version output must end with \\n or \\r\\n");
99228753Smm	if (*q == '\r') { ++q; --s; }
100228753Smm	assertEqualMem(q, "\n", 1);
101228753Smm	free(p);
102228753Smm}
103