/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. * Internal unit tests called by DejaGNU.
  28. */
  29. #if HAVE_CONFIG_H
  30. #include "config.h"
  31. #endif
  32. #include <sys/types.h>
  33. #include <sys/stat.h>
  34. #include <fcntl.h>
  35. #include <stdio.h>
  36. #include <string.h>
  37. #include <errno.h>
  38. #include <stdlib.h>
  39. #include "src/common/err.h"
  40. #include "src/common/xmalloc.h"
  41. #include "src/common/xstring.h"
  42. #include "src/common/pipecmd.h"
  43. #include "src/common/fd.h"
  44. #include "dsh.h"
  45. typedef enum { FAIL, PASS } testresult_t;
  46. typedef testresult_t((*testfun_t) (void));
  47. typedef struct {
  48. char *desc;
  49. testfun_t fun;
  50. } testcase_t;
  51. static testresult_t _test_xstrerrorcat(void);
  52. static testresult_t _test_pipecmd(void);
  53. static testcase_t testcases[] = {
  54. /* 0 */ {"xstrerrorcat", &_test_xstrerrorcat},
  55. /* 1 */ {"pipecmd", &_test_pipecmd},
  56. };
  57. static void _testmsg(int testnum, testresult_t result)
  58. {
  59. out("%P: Test %d: %s: %s\n", testnum, testcases[testnum].desc,
  60. result == PASS ? "PASS" : "FAIL");
  61. }
  62. static testresult_t _test_xstrerrorcat(void)
  63. {
  64. int e;
  65. testresult_t result = PASS;
  66. for (e = 1; e < 100; e++) {
  67. char *s1 = NULL;
  68. char *s2 = strerror(e);
  69. errno = e;
  70. xstrerrorcat(&s1);
  71. if (strcmp(s1, s2) != 0) {
  72. err ("xsterrorcat (errno=%d) = \"%s\" (should be \"%s\")\n", e, s1, s2);
  73. result = FAIL;
  74. }
  75. Free((void **) &s1);
  76. }
  77. return result;
  78. }
  79. static testresult_t _test_pipecmd(void)
  80. {
  81. const char expected[] = "host=foo0 user=foouser n=0";
  82. const char *args[] = { "host=%h", "user=%u", "n=%n", NULL };
  83. int n;
  84. char buf [1024];
  85. pipecmd_t p;
  86. if (!(p = pipecmd ("/bin/echo", args, "foo0", "foouser", 0)))
  87. return FAIL;
  88. if ((n = fd_read_n (pipecmd_stdoutfd (p), buf, sizeof (buf))) < 0)
  89. return FAIL;
  90. buf [n-1] = '\0';
  91. if (strcmp (expected, buf)) {
  92. err ("testcase: pipecmd: expected \"%s\" got \"%s\"\n", expected, buf);
  93. return FAIL;
  94. }
  95. pipecmd_wait (p, NULL);
  96. pipecmd_destroy (p);
  97. return PASS;
  98. }
  99. void testcase(int testnum)
  100. {
  101. testresult_t result;
  102. if (testnum < 0 || testnum >= (sizeof(testcases) / sizeof(testcase_t)))
  103. errx("%P: Test %d unknown\n", testnum);
  104. result = testcases[testnum].fun();
  105. _testmsg(testnum, result);
  106. exit(0);
  107. }
  108. /*
  109. * vi:tabstop=4 shiftwidth=4 expandtab
  110. */