PageRenderTime 57ms CodeModel.GetById 49ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/src/pdsh/testcase.c

https://code.google.com/
C | 132 lines | 79 code | 22 blank | 31 comment | 9 complexity | d079282ccb3150c3eda8fd19ef0aeffd MD5 | raw file
  1/*****************************************************************************\
  2 *  $Id$
  3 *****************************************************************************
  4 *  Copyright (C) 2001-2002 The Regents of the University of California.
  5 *  Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
  6 *  Written by Jim Garlick <garlick@llnl.gov>.
  7 *  UCRL-CODE-2003-005.
  8 *  
  9 *  This file is part of Pdsh, a parallel remote shell program.
 10 *  For details, see <http://www.llnl.gov/linux/pdsh/>.
 11 *  
 12 *  Pdsh is free software; you can redistribute it and/or modify it under
 13 *  the terms of the GNU General Public License as published by the Free
 14 *  Software Foundation; either version 2 of the License, or (at your option)
 15 *  any later version.
 16 *  
 17 *  Pdsh is distributed in the hope that it will be useful, but WITHOUT ANY
 18 *  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 19 *  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 20 *  details.
 21 *  
 22 *  You should have received a copy of the GNU General Public License along
 23 *  with Pdsh; if not, write to the Free Software Foundation, Inc.,
 24 *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 25\*****************************************************************************/
 26
 27/*
 28 * Internal unit tests called by DejaGNU.
 29 */
 30
 31#if	HAVE_CONFIG_H
 32#include "config.h"
 33#endif
 34
 35#include <sys/types.h>
 36#include <sys/stat.h>
 37#include <fcntl.h>
 38#include <stdio.h>
 39#include <string.h>
 40#include <errno.h>
 41#include <stdlib.h>
 42
 43#include "src/common/err.h"
 44#include "src/common/xmalloc.h"
 45#include "src/common/xstring.h"
 46#include "src/common/pipecmd.h"
 47#include "src/common/fd.h"
 48#include "dsh.h"
 49
 50typedef enum { FAIL, PASS } testresult_t;
 51typedef testresult_t((*testfun_t) (void));
 52typedef struct {
 53    char *desc;
 54    testfun_t fun;
 55} testcase_t;
 56
 57static testresult_t _test_xstrerrorcat(void);
 58static testresult_t _test_pipecmd(void);
 59
 60static testcase_t testcases[] = {
 61    /* 0 */ {"xstrerrorcat", &_test_xstrerrorcat},
 62    /* 1 */ {"pipecmd",      &_test_pipecmd},
 63};
 64
 65static void _testmsg(int testnum, testresult_t result)
 66{
 67    out("%P: Test %d: %s: %s\n", testnum, testcases[testnum].desc,
 68        result == PASS ? "PASS" : "FAIL");
 69}
 70
 71static testresult_t _test_xstrerrorcat(void)
 72{
 73    int e;
 74    testresult_t result = PASS;
 75
 76    for (e = 1; e < 100; e++) {
 77        char *s1 = NULL;
 78        char *s2 = strerror(e);
 79        errno = e;
 80        xstrerrorcat(&s1);
 81        if (strcmp(s1, s2) != 0) {
 82            err ("xsterrorcat (errno=%d) = \"%s\" (should be \"%s\")\n", e, s1, s2);
 83
 84            result = FAIL;
 85        }
 86        Free((void **) &s1);
 87    }
 88    return result;
 89}
 90
 91static testresult_t _test_pipecmd(void)
 92{
 93    const char expected[] = "host=foo0 user=foouser n=0";
 94    const char *args[] = { "host=%h", "user=%u", "n=%n", NULL };
 95
 96    int n;
 97    char buf [1024];
 98    pipecmd_t p;
 99
100    if (!(p = pipecmd ("/bin/echo", args, "foo0", "foouser", 0)))
101        return FAIL;
102
103    if ((n = fd_read_n (pipecmd_stdoutfd (p), buf, sizeof (buf))) < 0)
104        return FAIL;
105
106    buf [n-1] = '\0';
107
108    if (strcmp (expected, buf)) {
109        err ("testcase: pipecmd: expected \"%s\" got \"%s\"\n", expected, buf);
110        return FAIL;
111    }
112
113    pipecmd_wait (p, NULL);
114    pipecmd_destroy (p);
115
116    return PASS;
117}
118
119void testcase(int testnum)
120{
121    testresult_t result;
122
123    if (testnum < 0 || testnum >= (sizeof(testcases) / sizeof(testcase_t)))
124        errx("%P: Test %d unknown\n", testnum);
125    result = testcases[testnum].fun();
126    _testmsg(testnum, result);
127    exit(0);
128}
129
130/*
131 * vi:tabstop=4 shiftwidth=4 expandtab
132 */