test_option_version.c revision 302001
1241823Smarcel/*- 2241823Smarcel * Copyright (c) 2003-2007 Tim Kientzle 3241823Smarcel * All rights reserved. 4241823Smarcel * 5241823Smarcel * Redistribution and use in source and binary forms, with or without 6241823Smarcel * modification, are permitted provided that the following conditions 7241823Smarcel * are met: 8241823Smarcel * 1. Redistributions of source code must retain the above copyright 9241823Smarcel * notice, this list of conditions and the following disclaimer. 10241823Smarcel * 2. Redistributions in binary form must reproduce the above copyright 11241823Smarcel * notice, this list of conditions and the following disclaimer in the 12241823Smarcel * documentation and/or other materials provided with the distribution. 13241823Smarcel * 14241823Smarcel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 15241823Smarcel * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16241823Smarcel * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17241823Smarcel * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 18241823Smarcel * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19241823Smarcel * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20241823Smarcel * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21241823Smarcel * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22241823Smarcel * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23241823Smarcel * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24241823Smarcel */ 25241823Smarcel#include "test.h" 26241823Smarcel__FBSDID("$FreeBSD: stable/10/contrib/libarchive/cpio/test/test_option_version.c 302001 2016-06-17 22:40:10Z mm $"); 27241823Smarcel 28241823Smarcel/* 29241823Smarcel * Test that --version option works and generates reasonable output. 30260024Sjmmv */ 31241823Smarcel 32241823Smarcelstatic void 33241823Smarcelverify(const char *p, size_t s) 34241823Smarcel{ 35241823Smarcel const char *q = p; 36241823Smarcel 37241823Smarcel /* Version message should start with name of program, then space. */ 38241823Smarcel failure("version message too short:", p); 39241823Smarcel if (!assert(s > 6)) 40241823Smarcel return; 41241823Smarcel failure("Version message should begin with 'bsdcpio': %s", p); 42270905Sngie if (!assertEqualMem(q, "bsdcpio ", 8)) 43241823Smarcel /* If we're not testing bsdcpio, don't keep going. */ 44241823Smarcel return; 45241823Smarcel q += 8; s -= 8; 46241823Smarcel /* Version number is a series of digits and periods. */ 47241823Smarcel while (s > 0 && (*q == '.' || (*q >= '0' && *q <= '9'))) { 48241823Smarcel ++q; 49260024Sjmmv --s; 50260024Sjmmv } 51260024Sjmmv /* Version number terminated by space. */ 52260024Sjmmv failure("Version: %s", p); 53262849Sjmmv assert(s > 1); 54241823Smarcel /* 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 /* Skip arbitrary third-party version numbers. */ 78 while (s > 0 && (*q == ' ' || *q == '/' || *q == '.' || isalnum(*q))) { 79 ++q; 80 --s; 81 } 82 /* All terminated by end-of-line: \r, \r\n, or \n */ 83 assert(s >= 1); 84 failure("Version: %s", p); 85 if (*q == '\x0d') { 86 if (q[1] != '\0') 87 assertEqualMem(q, "\x0d\x0a", 2); 88 } else 89 assertEqualMem(q, "\x0a", 1); 90} 91 92 93DEFINE_TEST(test_option_version) 94{ 95 int r; 96 char *p; 97 size_t s; 98 99 r = systemf("%s --version >version.stdout 2>version.stderr", testprog); 100 if (r != 0) 101 r = systemf("%s -W version >version.stdout 2>version.stderr", 102 testprog); 103 failure("Unable to run either %s --version or %s -W version", 104 testprog, testprog); 105 if (!assert(r == 0)) 106 return; 107 108 /* --version should generate nothing to stderr. */ 109 assertEmptyFile("version.stderr"); 110 /* Verify format of version message. */ 111 p = slurpfile(&s, "version.stdout"); 112 verify(p, s); 113 free(p); 114} 115