113901Salanb/*- 213901Salanb * Copyright (c) 2003-2007 Tim Kientzle 313901Salanb * All rights reserved. 413901Salanb * 513901Salanb * Redistribution and use in source and binary forms, with or without 613901Salanb * modification, are permitted provided that the following conditions 713901Salanb * are met: 813901Salanb * 1. Redistributions of source code must retain the above copyright 913901Salanb * notice, this list of conditions and the following disclaimer. 1013901Salanb * 2. Redistributions in binary form must reproduce the above copyright 1113901Salanb * notice, this list of conditions and the following disclaimer in the 1213901Salanb * documentation and/or other materials provided with the distribution. 1313901Salanb * 1413901Salanb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 1513901Salanb * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1613901Salanb * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 1713901Salanb * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 1813901Salanb * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 1913901Salanb * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2013901Salanb * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2113901Salanb * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2213901Salanb * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2313901Salanb * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2413901Salanb */ 2513901Salanb#include "test.h" 2613901Salanb 2713901Salanb/* 2813901Salanb * Test that "--help", "-h", and "-W help" options all work and 2913901Salanb * generate reasonable output. 3013901Salanb */ 3113901Salanb 3216481Schegarstatic int 3313901Salanbin_first_line(const char *p, const char *substring) 3413901Salanb{ 3513901Salanb size_t l = strlen(substring); 3613901Salanb 3713901Salanb while (*p != '\0' && *p != '\n') { 3813901Salanb if (memcmp(p, substring, l) == 0) 3913901Salanb return (1); 4013901Salanb ++p; 4113901Salanb } 4213901Salanb return (0); 4313901Salanb} 4413901Salanb 4513901SalanbDEFINE_TEST(test_option_help) 4613901Salanb{ 4713901Salanb int r; 4813901Salanb char *p; 4913901Salanb size_t plen; 5013901Salanb 5113901Salanb /* Exercise --help option. */ 5213901Salanb r = systemf("%s --help >help.stdout 2>help.stderr", testprog); 5313901Salanb assertEqualInt(r, 0); 5413901Salanb failure("--help should generate nothing to stderr."); 5513901Salanb assertEmptyFile("help.stderr"); 5613901Salanb /* Help message should start with name of program. */ 5713901Salanb p = slurpfile(&plen, "help.stdout"); 5813901Salanb failure("Help output should be long enough."); 5913901Salanb assert(plen >= 7); 6013901Salanb failure("First line of help output should contain string 'bsdcpio'"); 6113901Salanb assert(in_first_line(p, "bsdcpio")); 6213901Salanb /* 6313901Salanb * TODO: Extend this check to further verify that --help output 6413901Salanb * looks approximately right. 6513901Salanb */ 6613901Salanb free(p); 6713901Salanb 6813901Salanb /* -h option should generate the same output. */ 6913901Salanb r = systemf("%s -h >h.stdout 2>h.stderr", testprog); 7013901Salanb assertEqualInt(r, 0); 7113901Salanb failure("-h should generate nothing to stderr."); 7213901Salanb assertEmptyFile("h.stderr"); 7313901Salanb failure("stdout should be same for -h and --help"); 7413901Salanb assertEqualFile("h.stdout", "help.stdout"); 7513901Salanb 7613901Salanb /* -W help should be another synonym. */ 7713901Salanb r = systemf("%s -W help >Whelp.stdout 2>Whelp.stderr", testprog); 7813901Salanb assertEqualInt(r, 0); 7913901Salanb failure("-W help should generate nothing to stderr."); 8013901Salanb assertEmptyFile("Whelp.stderr"); 8113901Salanb failure("stdout should be same for -W help and --help"); 8213901Salanb assertEqualFile("Whelp.stdout", "help.stdout"); 8313901Salanb} 8413901Salanb