11556Srgrimes/*-
21556Srgrimes * Copyright (c) 2003-2007 Tim Kientzle
31556Srgrimes * All rights reserved.
41556Srgrimes *
51556Srgrimes * Redistribution and use in source and binary forms, with or without
61556Srgrimes * modification, are permitted provided that the following conditions
71556Srgrimes * are met:
81556Srgrimes * 1. Redistributions of source code must retain the above copyright
91556Srgrimes *    notice, this list of conditions and the following disclaimer.
101556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111556Srgrimes *    notice, this list of conditions and the following disclaimer in the
121556Srgrimes *    documentation and/or other materials provided with the distribution.
131556Srgrimes *
141556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
151556Srgrimes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
161556Srgrimes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
171556Srgrimes * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
181556Srgrimes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
191556Srgrimes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
201556Srgrimes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
211556Srgrimes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
221556Srgrimes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
231556Srgrimes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
241556Srgrimes */
251556Srgrimes#include "test.h"
261556Srgrimes__FBSDID("$FreeBSD$");
271556Srgrimes
281556Srgrimes/*
291556Srgrimes * Test that --version option works and generates reasonable output.
301556Srgrimes */
311556Srgrimes
321556Srgrimesstatic void
331556Srgrimesverify(const char *p, size_t s)
341556Srgrimes{
353044Sdg	const char *q = p;
3618754Ssteve
371556Srgrimes	/* Version message should start with name of program, then space. */
381556Srgrimes	failure("version message too short:", p);
391556Srgrimes	if (!assert(s > 6))
4017987Speter		return;
411556Srgrimes	failure("Version message should begin with 'bsdcpio': %s", p);
421556Srgrimes	if (!assertEqualMem(q, "bsdcpio ", 8))
4317987Speter		/* If we're not testing bsdcpio, don't keep going. */
4417987Speter		return;
4517987Speter	q += 8; s -= 8;
461556Srgrimes	/* Version number is a series of digits and periods. */
471556Srgrimes	while (s > 0 && (*q == '.' || (*q >= '0' && *q <= '9'))) {
481556Srgrimes		++q;
491556Srgrimes		--s;
501556Srgrimes	}
511556Srgrimes	/* Version number terminated by space. */
521556Srgrimes	failure("Version: %s", p);
531556Srgrimes	assert(s > 1);
541556Srgrimes	/* Skip a single trailing a,b,c, or d. */
551556Srgrimes	if (*q == 'a' || *q == 'b' || *q == 'c' || *q == 'd')
561556Srgrimes		++q;
571556Srgrimes	failure("Version: %s", p);
581556Srgrimes	assert(*q == ' ');
591556Srgrimes	++q; --s;
601556Srgrimes	/* Separator. */
611556Srgrimes	failure("Version: %s", p);
621556Srgrimes	assertEqualMem(q, "-- ", 3);
631556Srgrimes	q += 3; s -= 3;
641556Srgrimes	/* libarchive name and version number */
651556Srgrimes	assert(s > 11);
661556Srgrimes	failure("Version: %s", p);
6717987Speter	assertEqualMem(q, "libarchive ", 11);
681556Srgrimes	q += 11; s -= 11;
6917987Speter	/* Version number is a series of digits and periods. */
701556Srgrimes	while (s > 0 && (*q == '.' || (*q >= '0' && *q <= '9'))) {
7117987Speter		++q;
721556Srgrimes		--s;
731556Srgrimes	}
741556Srgrimes	/* Skip a single trailing a,b,c, or d. */
751556Srgrimes	if (*q == 'a' || *q == 'b' || *q == 'c' || *q == 'd')
761556Srgrimes		++q;
771556Srgrimes	/* All terminated by end-of-line: \r, \r\n, or \n */
781556Srgrimes	assert(s >= 1);
791556Srgrimes	failure("Version: %s", p);
801556Srgrimes	if (*q == '\x0d') {
811556Srgrimes		if (q[1] != '\0')
821556Srgrimes			assertEqualMem(q, "\x0d\x0a", 2);
831556Srgrimes	} else
841556Srgrimes		assertEqualMem(q, "\x0a", 1);
851556Srgrimes}
861556Srgrimes
871556Srgrimes
881556SrgrimesDEFINE_TEST(test_option_version)
891556Srgrimes{
901556Srgrimes	int r;
911556Srgrimes	char *p;
921556Srgrimes	size_t s;
931556Srgrimes
9417987Speter	r = systemf("%s --version >version.stdout 2>version.stderr", testprog);
951556Srgrimes	if (r != 0)
961556Srgrimes		r = systemf("%s -W version >version.stdout 2>version.stderr",
9717987Speter		    testprog);
9817987Speter	failure("Unable to run either %s --version or %s -W version",
9917987Speter	    testprog, testprog);
10017987Speter	if (!assert(r == 0))
10117987Speter		return;
10217987Speter
10317987Speter	/* --version should generate nothing to stderr. */
10417987Speter	assertEmptyFile("version.stderr");
1051556Srgrimes	/* Verify format of version message. */
1061556Srgrimes	p = slurpfile(&s, "version.stdout");
1071556Srgrimes	verify(p, s);
1081556Srgrimes	free(p);
1091556Srgrimes}
1101556Srgrimes