alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved. |
subrata_modak | 3311a25 | 2009-10-13 13:59:29 +0000 | [diff] [blame] | 3 | * Copyright (c) 2009 Cyril Hrubis chrubis@suse.cz |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 4 | * |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of version 2 of the GNU General Public License as |
| 7 | * published by the Free Software Foundation. |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 8 | * |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 9 | * This program is distributed in the hope that it would be useful, but |
| 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 12 | * |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 13 | * Further, this software is distributed without any warranty that it is |
| 14 | * free of the rightful claim of any third person regarding infringement |
| 15 | * or the like. Any license provided herein, whether implied or |
| 16 | * otherwise, applies only to this software file. Patent licenses, if |
| 17 | * any, provided herein do not apply to combinations of this program with |
| 18 | * other software, or any other product whatsoever. |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 19 | * |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 20 | * You should have received a copy of the GNU General Public License along |
| 21 | * with this program; if not, write the Free Software Foundation, Inc., 59 |
| 22 | * Temple Place - Suite 330, Boston MA 02111-1307, USA. |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 23 | * |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 24 | * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, |
| 25 | * Mountain View, CA 94043, or: |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 26 | * |
| 27 | * http://www.sgi.com |
| 28 | * |
| 29 | * For further information regarding this notice, see: |
| 30 | * |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 31 | * http://oss.sgi.com/projects/GenInfo/NoticeExplan/ |
| 32 | */ |
| 33 | |
| 34 | |
yaberauneya | 7113a0c | 2009-12-01 08:57:20 +0000 | [diff] [blame] | 35 | /* $Id: tst_res.c,v 1.14 2009/12/01 08:57:20 yaberauneya Exp $ */ |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 36 | |
| 37 | /********************************************************** |
| 38 | * |
| 39 | * OS Testing - Silicon Graphics, Inc. |
| 40 | * |
| 41 | * FUNCTION NAME : |
| 42 | * tst_res() - Print result message (include file contents) |
| 43 | * tst_resm() - Print result message |
| 44 | * tst_brk() - Print result message (include file contents) |
| 45 | * and break remaining test cases |
| 46 | * tst_brkm() - Print result message and break remaining test |
| 47 | * cases |
Garrett Cooper | 5b87544 | 2010-12-13 20:49:15 -0800 | [diff] [blame] | 48 | * tst_flush() - Print any messages pending in the output stream |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 49 | * tst_exit() - Exit test with a meaningful exit value. |
| 50 | * tst_environ() - Keep results coming to original stdout |
| 51 | * |
| 52 | * FUNCTION TITLE : Standard automated test result reporting mechanism |
| 53 | * |
| 54 | * SYNOPSIS: |
| 55 | * #include "test.h" |
| 56 | * |
| 57 | * void tst_res(ttype, fname, tmesg [,arg]...) |
| 58 | * int ttype; |
| 59 | * char *fname; |
| 60 | * char *tmesg; |
| 61 | * |
| 62 | * void tst_resm(ttype, tmesg [,arg]...) |
| 63 | * int ttype; |
| 64 | * char *tmesg; |
| 65 | * |
| 66 | * void tst_brk(ttype, fname, cleanup, tmesg, [,argv]...) |
| 67 | * int ttype; |
| 68 | * char *fname; |
| 69 | * void (*cleanup)(); |
| 70 | * char *tmesg; |
| 71 | * |
| 72 | * void tst_brkm(ttype, cleanup, tmesg [,arg]...) |
| 73 | * int ttype; |
| 74 | * void (*cleanup)(); |
| 75 | * char *tmesg; |
| 76 | * |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 77 | * void tst_flush() |
| 78 | * |
| 79 | * void tst_exit() |
| 80 | * |
| 81 | * int tst_environ() |
| 82 | * |
| 83 | * AUTHOR : Kent Rogers (from Dave Fenner's original) |
| 84 | * |
| 85 | * CO-PILOT : Rich Logan |
| 86 | * |
| 87 | * DATE STARTED : 05/01/90 (rewritten 1/96) |
| 88 | * |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 89 | * MAJOR CLEANUPS BY : Cyril Hrubis |
| 90 | * |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 91 | * DESCRIPTION |
| 92 | * See the man page(s). |
| 93 | * |
| 94 | *#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#**/ |
Garrett Cooper | d3e6e8f | 2010-12-13 02:55:35 -0800 | [diff] [blame] | 95 | |
| 96 | #ifdef GARRETT_IS_A_PEDANTIC_BASTARD |
| 97 | #include <assert.h> |
| 98 | #endif |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 99 | #include <errno.h> |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 100 | #include <stdio.h> |
| 101 | #include <stdlib.h> |
| 102 | #include <stdarg.h> |
Garrett Cooper | d3e6e8f | 2010-12-13 02:55:35 -0800 | [diff] [blame] | 103 | #include <string.h> |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 104 | #include <unistd.h> |
| 105 | #include "test.h" |
vapier | 9799ea1 | 2009-07-20 02:42:32 +0000 | [diff] [blame] | 106 | #include "usctest.h" |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 107 | |
Garrett Cooper | d3e6e8f | 2010-12-13 02:55:35 -0800 | [diff] [blame] | 108 | /* Break bad habits. */ |
| 109 | #ifdef GARRETT_IS_A_PEDANTIC_BASTARD |
| 110 | pid_t spawned_program_pid; |
| 111 | #endif |
| 112 | |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 113 | #define VERBOSE 1 /* flag values for the T_mode variable */ |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 114 | #define NOPASS 3 |
| 115 | #define DISCARD 4 |
| 116 | |
| 117 | #define MAXMESG 80 /* max length of internal messages */ |
| 118 | #define USERMESG 2048 /* max length of user message */ |
| 119 | #define TRUE 1 |
| 120 | #define FALSE 0 |
| 121 | |
| 122 | /* |
| 123 | * EXPAND_VAR_ARGS - Expand the variable portion (arg_fmt) of a result |
| 124 | * message into the specified string. |
| 125 | */ |
subrata_modak | 3311a25 | 2009-10-13 13:59:29 +0000 | [diff] [blame] | 126 | #define EXPAND_VAR_ARGS(buf, arg_fmt, buf_len) { \ |
| 127 | va_list ap; \ |
| 128 | \ |
| 129 | if (arg_fmt != NULL) { \ |
| 130 | if (Expand_varargs) { \ |
| 131 | va_start(ap, arg_fmt); \ |
| 132 | vsnprintf(buf, buf_len, arg_fmt, ap); \ |
| 133 | va_end(ap); \ |
| 134 | } else \ |
| 135 | strncpy(buf, arg_fmt, buf_len); \ |
| 136 | } else \ |
| 137 | buf[0] = '\0'; \ |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 138 | } |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 139 | |
| 140 | /* |
| 141 | * Define local function prototypes. |
| 142 | */ |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 143 | static void check_env(void); |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 144 | static void tst_condense(int tnum, int ttype, char *tmesg); |
Garrett Cooper | 5b87544 | 2010-12-13 20:49:15 -0800 | [diff] [blame] | 145 | static void tst_print(char *tcid, int tnum, int ttype, char *tmesg); |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 146 | static void cat_file(char *filename); |
| 147 | |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 148 | /* |
| 149 | * Define some static/global variables. |
| 150 | */ |
| 151 | static FILE *T_out = NULL; /* tst_res() output file descriptor */ |
| 152 | static char *File; /* file whose contents is part of result */ |
| 153 | static int T_exitval = 0; /* exit value used by tst_exit() */ |
| 154 | static int T_mode = VERBOSE; /* flag indicating print mode: VERBOSE, */ |
Garrett Cooper | 5b87544 | 2010-12-13 20:49:15 -0800 | [diff] [blame] | 155 | /* NOPASS, DISCARD */ |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 156 | |
| 157 | static int Expand_varargs = TRUE; /* if TRUE, expand varargs stuff */ |
| 158 | static char Warn_mesg[MAXMESG]; /* holds warning messages */ |
| 159 | |
| 160 | /* |
| 161 | * These are used for condensing output when NOT in verbose mode. |
| 162 | */ |
| 163 | static int Buffered = FALSE; /* TRUE if condensed output is currently */ |
| 164 | /* buffered (i.e. not yet printed) */ |
| 165 | static char *Last_tcid; /* previous test case id */ |
| 166 | static int Last_num; /* previous test case number */ |
| 167 | static int Last_type; /* previous test result type */ |
| 168 | static char *Last_mesg; /* previous test result message */ |
| 169 | |
| 170 | |
| 171 | /* |
| 172 | * These globals may be externed by the test. |
| 173 | */ |
| 174 | int Tst_count = 0; /* current count of test cases executed; NOTE: */ |
| 175 | /* Tst_count may be externed by other programs */ |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 176 | |
| 177 | /* |
| 178 | * These globals must be defined in the test. |
| 179 | */ |
| 180 | extern char *TCID; /* Test case identifier from the test source */ |
| 181 | extern int TST_TOTAL; /* Total number of test cases from the test */ |
| 182 | /* source */ |
| 183 | |
vapier | 9799ea1 | 2009-07-20 02:42:32 +0000 | [diff] [blame] | 184 | struct pair { |
| 185 | const char *name; |
| 186 | int val; |
| 187 | }; |
| 188 | #define PAIR(def) [def] = { .name = #def, .val = def, }, |
| 189 | const char *pair_lookup(struct pair *pair, int pair_size, int idx) |
| 190 | { |
| 191 | if (idx < 0 || idx >= pair_size || pair[idx].name == NULL) |
| 192 | return "???"; |
| 193 | return pair[idx].name; |
| 194 | } |
| 195 | #define pair_lookup(pair, idx) pair_lookup(pair, ARRAY_SIZE(pair), idx) |
| 196 | |
| 197 | /* |
| 198 | * strttype() - convert a type result to the human readable string |
| 199 | */ |
| 200 | const char *strttype(int ttype) |
| 201 | { |
| 202 | struct pair ttype_pairs[] = { |
| 203 | PAIR(TPASS) |
| 204 | PAIR(TFAIL) |
| 205 | PAIR(TBROK) |
| 206 | PAIR(TRETR) |
| 207 | PAIR(TCONF) |
| 208 | PAIR(TWARN) |
| 209 | PAIR(TINFO) |
| 210 | }; |
| 211 | return pair_lookup(ttype_pairs, TTYPE_RESULT(ttype)); |
| 212 | } |
| 213 | |
| 214 | /* |
| 215 | * strerrnodef() - convert an errno value to its C define |
| 216 | */ |
| 217 | static const char *strerrnodef(int err) |
| 218 | { |
| 219 | struct pair errno_pairs[] = { |
| 220 | PAIR(EPERM) |
| 221 | PAIR(ENOENT) |
| 222 | PAIR(ESRCH) |
| 223 | PAIR(EINTR) |
| 224 | PAIR(EIO) |
| 225 | PAIR(ENXIO) |
| 226 | PAIR(E2BIG) |
| 227 | PAIR(ENOEXEC) |
| 228 | PAIR(EBADF) |
| 229 | PAIR(ECHILD) |
| 230 | PAIR(EAGAIN) |
| 231 | PAIR(ENOMEM) |
| 232 | PAIR(EACCES) |
| 233 | PAIR(EFAULT) |
| 234 | PAIR(ENOTBLK) |
| 235 | PAIR(EBUSY) |
| 236 | PAIR(EEXIST) |
| 237 | PAIR(EXDEV) |
| 238 | PAIR(ENODEV) |
| 239 | PAIR(ENOTDIR) |
| 240 | PAIR(EISDIR) |
| 241 | PAIR(EINVAL) |
| 242 | PAIR(ENFILE) |
| 243 | PAIR(EMFILE) |
| 244 | PAIR(ENOTTY) |
| 245 | PAIR(ETXTBSY) |
| 246 | PAIR(EFBIG) |
| 247 | PAIR(ENOSPC) |
| 248 | PAIR(ESPIPE) |
| 249 | PAIR(EROFS) |
| 250 | PAIR(EMLINK) |
| 251 | PAIR(EPIPE) |
| 252 | PAIR(EDOM) |
| 253 | PAIR(ERANGE) |
vapier | 995d5a3 | 2009-08-28 11:05:36 +0000 | [diff] [blame] | 254 | PAIR(ENAMETOOLONG) |
vapier | 9799ea1 | 2009-07-20 02:42:32 +0000 | [diff] [blame] | 255 | }; |
| 256 | return pair_lookup(errno_pairs, err); |
| 257 | } |
| 258 | |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 259 | /* |
| 260 | * tst_res() - Main result reporting function. Handle test information |
| 261 | * appropriately depending on output display mode. Call |
| 262 | * tst_condense() or tst_print() to actually print results. |
| 263 | * All result functions (tst_resm(), tst_brk(), etc.) |
| 264 | * eventually get here to print the results. |
| 265 | */ |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 266 | void tst_res(int ttype, char *fname, char *arg_fmt, ...) |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 267 | { |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 268 | char tmesg[USERMESG]; |
vapier | 9799ea1 | 2009-07-20 02:42:32 +0000 | [diff] [blame] | 269 | int ttype_result = TTYPE_RESULT(ttype); |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 270 | |
| 271 | #if DEBUG |
Garrett Cooper | 8798ebf | 2010-12-13 20:27:37 -0800 | [diff] [blame] | 272 | printf("IN tst_res; Tst_count = %d\n", Tst_count); |
| 273 | fflush(stdout); |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 274 | #endif |
| 275 | |
subrata_modak | 3311a25 | 2009-10-13 13:59:29 +0000 | [diff] [blame] | 276 | EXPAND_VAR_ARGS(tmesg, arg_fmt, USERMESG); |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 277 | |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 278 | /* |
| 279 | * Save the test result type by ORing ttype into the current exit |
| 280 | * value (used by tst_exit()). |
| 281 | */ |
vapier | 9799ea1 | 2009-07-20 02:42:32 +0000 | [diff] [blame] | 282 | T_exitval |= ttype_result; |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 283 | |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 284 | /* |
| 285 | * Unless T_out has already been set by tst_environ(), make tst_res() |
| 286 | * output go to standard output. |
| 287 | */ |
| 288 | if (T_out == NULL) |
| 289 | T_out = stdout; |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 290 | |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 291 | /* |
| 292 | * Check TOUTPUT environment variable (if first time) and set T_mode |
| 293 | * flag. |
| 294 | */ |
| 295 | check_env(); |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 296 | |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 297 | if (fname != NULL && access(fname, F_OK) == 0) |
| 298 | File = fname; |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 299 | |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 300 | /* |
| 301 | * Set the test case number and print the results, depending on the |
| 302 | * display type. |
| 303 | */ |
vapier | 9799ea1 | 2009-07-20 02:42:32 +0000 | [diff] [blame] | 304 | if (ttype_result == TWARN || ttype_result == TINFO) { |
Garrett Cooper | 5b87544 | 2010-12-13 20:49:15 -0800 | [diff] [blame] | 305 | tst_print(TCID, 0, ttype, tmesg); |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 306 | } else { |
| 307 | if (Tst_count < 0) |
Garrett Cooper | 5b87544 | 2010-12-13 20:49:15 -0800 | [diff] [blame] | 308 | tst_print(TCID, 0, TWARN, |
| 309 | "tst_res(): Tst_count < 0 is not valid"); |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 310 | |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 311 | /* |
| 312 | * Process each display type. |
| 313 | */ |
| 314 | switch (T_mode) { |
Garrett Cooper | 8798ebf | 2010-12-13 20:27:37 -0800 | [diff] [blame] | 315 | case DISCARD: |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 316 | break; |
Garrett Cooper | 8798ebf | 2010-12-13 20:27:37 -0800 | [diff] [blame] | 317 | case NOPASS: /* filtered by tst_print() */ |
Garrett Cooper | 5b87544 | 2010-12-13 20:49:15 -0800 | [diff] [blame] | 318 | tst_condense(Tst_count+1, ttype, tmesg); |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 319 | break; |
Garrett Cooper | 8798ebf | 2010-12-13 20:27:37 -0800 | [diff] [blame] | 320 | default: /* VERBOSE */ |
Garrett Cooper | 5b87544 | 2010-12-13 20:49:15 -0800 | [diff] [blame] | 321 | tst_print(TCID, Tst_count+1, ttype, tmesg); |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 322 | break; |
| 323 | } |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 324 | |
Garrett Cooper | 8798ebf | 2010-12-13 20:27:37 -0800 | [diff] [blame] | 325 | Tst_count++; |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 326 | } |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 327 | |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 328 | Expand_varargs = TRUE; |
| 329 | } |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 330 | |
| 331 | |
| 332 | /* |
Garrett Cooper | 5b87544 | 2010-12-13 20:49:15 -0800 | [diff] [blame] | 333 | * tst_condense() - Handle test cases in NOPASS mode (i.e. |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 334 | * buffer the current result and print the last result |
| 335 | * if different than the current). If a file was |
| 336 | * specified, print the current result and do not |
| 337 | * buffer it. |
| 338 | */ |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 339 | static void tst_condense(int tnum, int ttype, char *tmesg) |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 340 | { |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 341 | char *file; |
vapier | 9799ea1 | 2009-07-20 02:42:32 +0000 | [diff] [blame] | 342 | int ttype_result = TTYPE_RESULT(ttype); |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 343 | |
| 344 | #if DEBUG |
Garrett Cooper | 6bfb01e | 2010-11-22 10:16:28 -0800 | [diff] [blame] | 345 | printf( "IN tst_condense: tcid = %s, tnum = %d, ttype = %d, " |
| 346 | "tmesg = %s\n", |
| 347 | TCID, tnum, ttype, tmesg); |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 348 | fflush(stdout); |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 349 | #endif |
| 350 | |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 351 | /* |
| 352 | * If this result is the same as the previous result, return. |
| 353 | */ |
| 354 | if (Buffered == TRUE) { |
vapier | 9799ea1 | 2009-07-20 02:42:32 +0000 | [diff] [blame] | 355 | if (strcmp(Last_tcid, TCID) == 0 && Last_type == ttype_result && |
Garrett Cooper | 5b87544 | 2010-12-13 20:49:15 -0800 | [diff] [blame] | 356 | strcmp(Last_mesg, tmesg) == 0 && File == NULL) |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 357 | return; |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 358 | |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 359 | /* |
| 360 | * This result is different from the previous result. First, |
| 361 | * print the previous result. |
| 362 | */ |
| 363 | file = File; |
| 364 | File = NULL; |
Garrett Cooper | 5b87544 | 2010-12-13 20:49:15 -0800 | [diff] [blame] | 365 | tst_print(Last_tcid, Last_num, Last_type, Last_mesg); |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 366 | free(Last_tcid); |
| 367 | free(Last_mesg); |
| 368 | File = file; |
| 369 | } |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 370 | |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 371 | /* |
| 372 | * If a file was specified, print the current result since we have no |
| 373 | * way of retaining the file contents for comparing with future |
| 374 | * results. Otherwise, buffer the current result info for next time. |
| 375 | */ |
| 376 | if (File != NULL) { |
Garrett Cooper | 5b87544 | 2010-12-13 20:49:15 -0800 | [diff] [blame] | 377 | tst_print(TCID, tnum, ttype, tmesg); |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 378 | Buffered = FALSE; |
| 379 | } else { |
| 380 | Last_tcid = (char *)malloc(strlen(TCID) + 1); |
| 381 | strcpy(Last_tcid, TCID); |
| 382 | Last_num = tnum; |
vapier | 9799ea1 | 2009-07-20 02:42:32 +0000 | [diff] [blame] | 383 | Last_type = ttype_result; |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 384 | Last_mesg = (char *)malloc(strlen(tmesg) + 1); |
| 385 | strcpy(Last_mesg, tmesg); |
| 386 | Buffered = TRUE; |
| 387 | } |
| 388 | } |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 389 | |
| 390 | |
| 391 | /* |
Garrett Cooper | 5b87544 | 2010-12-13 20:49:15 -0800 | [diff] [blame] | 392 | * tst_flush() - Print any messages pending because due to tst_condense, |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 393 | * and flush T_out. |
| 394 | */ |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 395 | void tst_flush(void) |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 396 | { |
| 397 | #if DEBUG |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 398 | printf("IN tst_flush\n"); |
| 399 | fflush(stdout); |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 400 | #endif |
| 401 | |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 402 | /* |
Garrett Cooper | 5b87544 | 2010-12-13 20:49:15 -0800 | [diff] [blame] | 403 | * Print out last line if in NOPASS mode. |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 404 | */ |
Garrett Cooper | 5b87544 | 2010-12-13 20:49:15 -0800 | [diff] [blame] | 405 | if (Buffered == TRUE && T_mode == NOPASS) { |
| 406 | tst_print(Last_tcid, Last_num, Last_type, Last_mesg); |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 407 | Buffered = FALSE; |
| 408 | } |
| 409 | |
| 410 | fflush(T_out); |
| 411 | } |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 412 | |
| 413 | |
| 414 | /* |
Garrett Cooper | 5b87544 | 2010-12-13 20:49:15 -0800 | [diff] [blame] | 415 | * tst_print() - Print a line to the output stream. |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 416 | */ |
Garrett Cooper | 5b87544 | 2010-12-13 20:49:15 -0800 | [diff] [blame] | 417 | static void tst_print(char *tcid, int tnum, int ttype, char *tmesg) |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 418 | { |
yaberauneya | 7113a0c | 2009-12-01 08:57:20 +0000 | [diff] [blame] | 419 | /* |
| 420 | * avoid unintended side effects from failures with fprintf when |
| 421 | * calling write(2), et all. |
| 422 | */ |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame^] | 423 | int err = errno; |
vapier | 9799ea1 | 2009-07-20 02:42:32 +0000 | [diff] [blame] | 424 | const char *type; |
| 425 | int ttype_result = TTYPE_RESULT(ttype); |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 426 | |
Garrett Cooper | d3e6e8f | 2010-12-13 02:55:35 -0800 | [diff] [blame] | 427 | #ifdef GARRETT_IS_A_PEDANTIC_BASTARD |
| 428 | /* Don't execute these APIs unless you have the same pid as main! */ |
Garrett Cooper | 60cd167 | 2010-11-23 21:06:05 -0800 | [diff] [blame] | 429 | if (spawned_program_pid != 0) { |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame^] | 430 | /* |
Garrett Cooper | d3e6e8f | 2010-12-13 02:55:35 -0800 | [diff] [blame] | 431 | * Die quickly and noisily so people get the cluebat that the |
| 432 | * test needs to be fixed. These APIs should _not_ be called |
| 433 | * from forked processes because of the fact that it can confuse |
| 434 | * end-users with printouts, cleanup will potentially blow away |
| 435 | * directories and/or files still in use introducing |
| 436 | * non-determinism, etc. |
| 437 | * |
| 438 | * assert will not return (by design in accordance with POSIX |
| 439 | * 1003.1) if the assertion fails. Read abort(3) for more |
| 440 | * details. So don't worry about saving / restoring the signal |
| 441 | * handler, unless you have a buggy OS that you've hacked 15 |
| 442 | * different ways to Sunday. |
| 443 | */ |
Garrett Cooper | 60cd167 | 2010-11-23 21:06:05 -0800 | [diff] [blame] | 444 | assert(spawned_program_pid == getpid()); |
Garrett Cooper | d3e6e8f | 2010-12-13 02:55:35 -0800 | [diff] [blame] | 445 | } |
| 446 | #endif |
| 447 | |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 448 | #if DEBUG |
Garrett Cooper | 5b87544 | 2010-12-13 20:49:15 -0800 | [diff] [blame] | 449 | printf("IN tst_print: tnum = %d, ttype = %d, tmesg = %s\n", |
| 450 | tnum, ttype, tmesg); |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 451 | fflush(stdout); |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 452 | #endif |
| 453 | |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 454 | /* |
| 455 | * Save the test result type by ORing ttype into the current exit value |
| 456 | * (used by tst_exit()). This is already done in tst_res(), but is |
| 457 | * also done here to catch internal warnings. For internal warnings, |
| 458 | * tst_print() is called directly with a case of TWARN. |
| 459 | */ |
vapier | 9799ea1 | 2009-07-20 02:42:32 +0000 | [diff] [blame] | 460 | T_exitval |= ttype_result; |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 461 | |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 462 | /* |
| 463 | * If output mode is DISCARD, or if the output mode is NOPASS and this |
| 464 | * result is not one of FAIL, BROK, or WARN, just return. This check |
| 465 | * is necessary even though we check for DISCARD mode inside of |
| 466 | * tst_res(), since occasionally we get to this point without going |
| 467 | * through tst_res() (e.g. internal TWARN messages). |
| 468 | */ |
vapier | 9799ea1 | 2009-07-20 02:42:32 +0000 | [diff] [blame] | 469 | if (T_mode == DISCARD || (T_mode == NOPASS && ttype_result != TFAIL && |
| 470 | ttype_result != TBROK && ttype_result != TWARN)) |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 471 | return; |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 472 | |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 473 | /* |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 474 | * Build the result line and print it. |
| 475 | */ |
vapier | 9799ea1 | 2009-07-20 02:42:32 +0000 | [diff] [blame] | 476 | type = strttype(ttype); |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 477 | if (T_mode == VERBOSE) { |
vapier | 9799ea1 | 2009-07-20 02:42:32 +0000 | [diff] [blame] | 478 | fprintf(T_out, "%-8s %4d %s : %s", tcid, tnum, type, tmesg); |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 479 | } else { |
Garrett Cooper | 5b87544 | 2010-12-13 20:49:15 -0800 | [diff] [blame] | 480 | fprintf(T_out, "%-8s %4d %s : %s", |
| 481 | tcid, tnum, type, tmesg); |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 482 | } |
vapier | 9799ea1 | 2009-07-20 02:42:32 +0000 | [diff] [blame] | 483 | if (ttype & TERRNO) { |
vapier | 9799ea1 | 2009-07-20 02:42:32 +0000 | [diff] [blame] | 484 | fprintf(T_out, ": errno=%s(%i): %s", strerrnodef(err), |
| 485 | err, strerror(err)); |
| 486 | } |
yaberauneya | 7113a0c | 2009-12-01 08:57:20 +0000 | [diff] [blame] | 487 | if (ttype & TTERRNO) { |
| 488 | fprintf(T_out, ": TEST_ERRNO=%s(%i): %s", |
| 489 | strerrnodef(TEST_ERRNO), (int)TEST_ERRNO, |
| 490 | strerror(TEST_ERRNO)); |
| 491 | } |
vapier | 9799ea1 | 2009-07-20 02:42:32 +0000 | [diff] [blame] | 492 | fprintf(T_out, "\n"); |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 493 | |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 494 | /* |
| 495 | * If tst_res() was called with a file, append file contents to the |
| 496 | * end of last printed result. |
| 497 | */ |
| 498 | if (File != NULL) |
| 499 | cat_file(File); |
| 500 | |
| 501 | File = NULL; |
| 502 | } |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 503 | |
| 504 | |
| 505 | /* |
| 506 | * check_env() - Check the value of the environment variable TOUTPUT and |
| 507 | * set the global variable T_mode. The TOUTPUT environment |
Garrett Cooper | 5b87544 | 2010-12-13 20:49:15 -0800 | [diff] [blame] | 508 | * variable should be set to "VERBOSE", "NOPASS", or "DISCARD". |
| 509 | * If TOUTPUT does not exist or is not set to a valid value, the |
| 510 | * default is "VERBOSE". |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 511 | */ |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 512 | static void check_env(void) |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 513 | { |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 514 | static int first_time = 1; |
| 515 | char *value; |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 516 | |
| 517 | #if DEBUG |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 518 | printf("IN check_env\n"); |
| 519 | fflush(stdout); |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 520 | #endif |
| 521 | |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 522 | if (!first_time) |
| 523 | return; |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 524 | |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 525 | first_time = 0; |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 526 | |
Garrett Cooper | 60cd167 | 2010-11-23 21:06:05 -0800 | [diff] [blame] | 527 | /* BTOUTPUT not defined, use default */ |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 528 | if ((value = getenv(TOUTPUT)) == NULL) { |
| 529 | T_mode = VERBOSE; |
| 530 | return; |
| 531 | } |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 532 | |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 533 | if (strcmp(value, TOUT_NOPASS_S) == 0) { |
| 534 | T_mode = NOPASS; |
| 535 | return; |
| 536 | } |
| 537 | |
| 538 | if (strcmp(value, TOUT_DISCARD_S) == 0) { |
| 539 | T_mode = DISCARD; |
| 540 | return; |
| 541 | } |
| 542 | |
| 543 | /* default */ |
| 544 | T_mode = VERBOSE; |
| 545 | return; |
| 546 | } |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 547 | |
| 548 | |
| 549 | /* |
| 550 | * tst_exit() - Call exit() with the value T_exitval, set up by |
| 551 | * tst_res(). T_exitval has a bit set for most of the |
| 552 | * result types that were seen (including TPASS, TFAIL, |
| 553 | * TBROK, TWARN, TCONF). Also, print the last result (if |
| 554 | * necessary) before exiting. |
| 555 | */ |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 556 | void tst_exit(void) |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 557 | { |
| 558 | #if DEBUG |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 559 | printf("IN tst_exit\n"); fflush(stdout); |
| 560 | fflush(stdout); |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 561 | #endif |
| 562 | |
Garrett Cooper | 5b87544 | 2010-12-13 20:49:15 -0800 | [diff] [blame] | 563 | /* Call tst_flush() flush any output in the buffer. */ |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 564 | tst_flush(); |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 565 | |
Garrett Cooper | 5b87544 | 2010-12-13 20:49:15 -0800 | [diff] [blame] | 566 | /* Mask out TRETR, TINFO, and TCONF results from the exit status. */ |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 567 | exit(T_exitval & ~(TRETR | TINFO | TCONF)); |
| 568 | } |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 569 | |
| 570 | |
| 571 | /* |
| 572 | * tst_environ() - Preserve the tst_res() output location, despite any |
| 573 | * changes to stdout. |
| 574 | */ |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 575 | int tst_environ(void) |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 576 | { |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 577 | if ((T_out = fdopen(dup(fileno(stdout)), "w")) == NULL) |
| 578 | return -1; |
| 579 | else |
| 580 | return 0; |
| 581 | } |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 582 | |
| 583 | |
| 584 | /* |
| 585 | * tst_brk() - Fail or break current test case, and break the remaining |
| 586 | * tests cases. |
| 587 | */ |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 588 | void tst_brk(int ttype, char *fname, void (*func)(void), char *arg_fmt, ...) |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 589 | { |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 590 | char tmesg[USERMESG]; |
vapier | 9799ea1 | 2009-07-20 02:42:32 +0000 | [diff] [blame] | 591 | int ttype_result = TTYPE_RESULT(ttype); |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 592 | |
| 593 | #if DEBUG |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 594 | printf("IN tst_brk\n"); fflush(stdout); |
| 595 | fflush(stdout); |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 596 | #endif |
| 597 | |
subrata_modak | 3311a25 | 2009-10-13 13:59:29 +0000 | [diff] [blame] | 598 | EXPAND_VAR_ARGS(tmesg, arg_fmt, USERMESG); |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 599 | |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 600 | /* |
| 601 | * Only FAIL, BROK, CONF, and RETR are supported by tst_brk(). |
| 602 | */ |
vapier | 9799ea1 | 2009-07-20 02:42:32 +0000 | [diff] [blame] | 603 | if (ttype_result != TFAIL && ttype_result != TBROK && |
| 604 | ttype_result != TCONF && ttype_result != TRETR) { |
Garrett Cooper | eacf2a3 | 2010-12-16 16:04:04 -0800 | [diff] [blame] | 605 | sprintf(Warn_mesg, "%s: Invalid Type: %d. Using TBROK", |
| 606 | __func__, ttype_result); |
Garrett Cooper | 5b87544 | 2010-12-13 20:49:15 -0800 | [diff] [blame] | 607 | tst_print(TCID, 0, TWARN, Warn_mesg); |
Garrett Cooper | eacf2a3 | 2010-12-16 16:04:04 -0800 | [diff] [blame] | 608 | /* Keep TERRNO, TTERRNO, etc. */ |
| 609 | ttype = (ttype & ~ttype_result) | TBROK; |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 610 | } |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 611 | |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 612 | /* Print the first result, if necessary. */ |
| 613 | if (Tst_count < TST_TOTAL) |
| 614 | tst_res(ttype, fname, "%s", tmesg); |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 615 | |
Garrett Cooper | eacf2a3 | 2010-12-16 16:04:04 -0800 | [diff] [blame] | 616 | if (ttype_result == TCONF) |
Garrett Cooper | 5b87544 | 2010-12-13 20:49:15 -0800 | [diff] [blame] | 617 | tst_res(ttype, NULL, |
| 618 | "Remaining cases not appropriate for configuration"); |
Garrett Cooper | eacf2a3 | 2010-12-16 16:04:04 -0800 | [diff] [blame] | 619 | else if (ttype_result == TRETR) |
| 620 | tst_res(ttype, NULL, "Remaining cases retired"); |
| 621 | else if (ttype_result == TBROK) |
| 622 | tst_res(TBROK, NULL, "Remaining cases broken"); |
Garrett Cooper | 5b87544 | 2010-12-13 20:49:15 -0800 | [diff] [blame] | 623 | Expand_varargs = TRUE; |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 624 | |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 625 | /* |
| 626 | * If no cleanup function was specified, just return to the caller. |
Garrett Cooper | 9f2555e | 2010-12-13 23:55:22 -0800 | [diff] [blame] | 627 | * Otherwise call the specified function. |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 628 | */ |
Garrett Cooper | 6bfb01e | 2010-11-22 10:16:28 -0800 | [diff] [blame] | 629 | if (func != NULL) { |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 630 | (*func)(); |
Garrett Cooper | 6bfb01e | 2010-11-22 10:16:28 -0800 | [diff] [blame] | 631 | } |
| 632 | tst_exit(); |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 633 | } |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 634 | |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 635 | /* |
| 636 | * tst_resm() - Interface to tst_res(), with no filename. |
| 637 | */ |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 638 | void tst_resm(int ttype, char *arg_fmt, ...) |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 639 | { |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 640 | char tmesg[USERMESG]; |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 641 | |
| 642 | #if DEBUG |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 643 | printf("IN tst_resm\n"); fflush(stdout); |
| 644 | fflush(stdout); |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 645 | #endif |
| 646 | |
subrata_modak | 3311a25 | 2009-10-13 13:59:29 +0000 | [diff] [blame] | 647 | EXPAND_VAR_ARGS(tmesg, arg_fmt, USERMESG); |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 648 | |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 649 | tst_res(ttype, NULL, "%s", tmesg); |
| 650 | } |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 651 | |
| 652 | |
| 653 | /* |
| 654 | * tst_brkm() - Interface to tst_brk(), with no filename. |
| 655 | */ |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 656 | void tst_brkm(int ttype, void (*func)(void), char *arg_fmt, ...) |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 657 | { |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 658 | char tmesg[USERMESG]; |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 659 | |
| 660 | #if DEBUG |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 661 | printf("IN tst_brkm\n"); fflush(stdout); |
| 662 | fflush(stdout); |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 663 | #endif |
| 664 | |
subrata_modak | 3311a25 | 2009-10-13 13:59:29 +0000 | [diff] [blame] | 665 | EXPAND_VAR_ARGS(tmesg, arg_fmt, USERMESG); |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 666 | |
vapier | f88c09e | 2009-08-28 10:00:39 +0000 | [diff] [blame] | 667 | tst_brk(ttype, NULL, func, "%s", tmesg); |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 668 | } |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 669 | |
| 670 | |
| 671 | /* |
vapier | 640adf4 | 2008-05-06 15:47:54 +0000 | [diff] [blame] | 672 | * tst_require_root() - Test for root permissions and abort if not. |
| 673 | */ |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 674 | void tst_require_root(void (*func)(void)) |
vapier | 640adf4 | 2008-05-06 15:47:54 +0000 | [diff] [blame] | 675 | { |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 676 | if (geteuid() != 0) |
| 677 | tst_brkm(TCONF, func, "Test needs to be run as root"); |
vapier | 640adf4 | 2008-05-06 15:47:54 +0000 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | |
| 681 | /* |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 682 | * cat_file() - Print the contents of a file to standard out. |
| 683 | */ |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 684 | static void cat_file(char *filename) |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 685 | { |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 686 | FILE *fp; |
| 687 | int b_read, b_written; |
| 688 | char buffer[BUFSIZ]; |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 689 | |
| 690 | #if DEBUG |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 691 | printf("IN cat_file\n"); fflush(stdout); |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 692 | #endif |
| 693 | |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 694 | if ((fp = fopen(filename, "r")) == NULL) { |
| 695 | sprintf(Warn_mesg, |
| 696 | "tst_res(): fopen(%s, \"r\") failed; errno = %d: %s", |
| 697 | filename, errno, strerror(errno)); |
Garrett Cooper | 5b87544 | 2010-12-13 20:49:15 -0800 | [diff] [blame] | 698 | tst_print(TCID, 0, TWARN, Warn_mesg); |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 699 | return; |
| 700 | } |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 701 | |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 702 | errno = 0; |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 703 | |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 704 | while ((b_read = fread(buffer, 1, BUFSIZ, fp)) != 0) { |
| 705 | if ((b_written = fwrite(buffer, 1, b_read, T_out)) != b_read) { |
| 706 | sprintf(Warn_mesg, |
Garrett Cooper | 6bfb01e | 2010-11-22 10:16:28 -0800 | [diff] [blame] | 707 | "tst_res(): While trying to cat \"%s\", " |
| 708 | "fwrite() wrote only %d of %d bytes", |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 709 | filename, b_written, b_read); |
Garrett Cooper | 5b87544 | 2010-12-13 20:49:15 -0800 | [diff] [blame] | 710 | tst_print(TCID, 0, TWARN, Warn_mesg); |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 711 | break; |
| 712 | } |
| 713 | } |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 714 | |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 715 | if (!feof(fp)) { |
| 716 | sprintf(Warn_mesg, |
Garrett Cooper | 6bfb01e | 2010-11-22 10:16:28 -0800 | [diff] [blame] | 717 | "tst_res(): While trying to cat \"%s\", fread() " |
| 718 | "failed, errno = %d: %s", |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 719 | filename, errno, strerror(errno)); |
Garrett Cooper | 5b87544 | 2010-12-13 20:49:15 -0800 | [diff] [blame] | 720 | tst_print(TCID, 0, TWARN, Warn_mesg); |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 721 | } |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 722 | |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 723 | if (fclose(fp) != 0) { |
| 724 | sprintf(Warn_mesg, |
Garrett Cooper | 6bfb01e | 2010-11-22 10:16:28 -0800 | [diff] [blame] | 725 | "tst_res(): While trying to cat \"%s\", fclose() " |
| 726 | "failed, errno = %d: %s", |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 727 | filename, errno, strerror(errno)); |
Garrett Cooper | 5b87544 | 2010-12-13 20:49:15 -0800 | [diff] [blame] | 728 | tst_print(TCID, 0, TWARN, Warn_mesg); |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 729 | } |
| 730 | } |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 731 | |
| 732 | |
| 733 | #ifdef UNIT_TEST |
| 734 | /**************************************************************************** |
| 735 | * Unit test code: Takes input from stdin and can make the following |
| 736 | * calls: tst_res(), tst_resm(), tst_brk(), tst_brkm(), |
| 737 | * tst_flush_buf(), tst_exit(). |
| 738 | ****************************************************************************/ |
| 739 | int TST_TOTAL = 10; |
| 740 | char *TCID = "TESTTCID"; |
| 741 | |
| 742 | #define RES "tst_res.c UNIT TEST message; ttype = %d; contents of \"%s\":" |
| 743 | #define RESM "tst_res.c UNIT TEST message; ttype = %d" |
| 744 | |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 745 | int main(void) |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 746 | { |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 747 | int ttype; |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 748 | char chr; |
| 749 | char fname[MAXMESG]; |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 750 | |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 751 | printf("UNIT TEST of tst_res.c. Options for ttype:\n\ |
| 752 | -1 : call tst_exit()\n\ |
| 753 | -2 : call tst_flush()\n\ |
| 754 | -3 : call tst_brk()\n\ |
Garrett Cooper | 5b87544 | 2010-12-13 20:49:15 -0800 | [diff] [blame] | 755 | -4 : call tst_res()\n\ |
vapier | 9799ea1 | 2009-07-20 02:42:32 +0000 | [diff] [blame] | 756 | %2i : call tst_res(TPASS, ...)\n\ |
| 757 | %2i : call tst_res(TFAIL, ...)\n\ |
| 758 | %2i : call tst_res(TBROK, ...)\n\ |
| 759 | %2i : call tst_res(TWARN, ...)\n\ |
| 760 | %2i : call tst_res(TRETR, ...)\n\ |
| 761 | %2i : call tst_res(TINFO, ...)\n\ |
| 762 | %2i : call tst_res(TCONF, ...)\n\n", |
| 763 | TPASS, TFAIL, TBROK, TWARN, TRETR, TINFO, TCONF); |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 764 | |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 765 | while (1) { |
vapier | 9799ea1 | 2009-07-20 02:42:32 +0000 | [diff] [blame] | 766 | printf("Enter ttype (-5,-4,-3,-2,-1,%i,%i,%i,%i,%i,%i,%i): ", |
| 767 | TPASS, TFAIL, TBROK, TWARN, TRETR, TINFO, TCONF); |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 768 | scanf("%d%c", &ttype, &chr); |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 769 | |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 770 | switch (ttype) { |
Garrett Cooper | 6bfb01e | 2010-11-22 10:16:28 -0800 | [diff] [blame] | 771 | case -1: |
| 772 | tst_exit(); |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 773 | break; |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 774 | |
Garrett Cooper | 6bfb01e | 2010-11-22 10:16:28 -0800 | [diff] [blame] | 775 | case -2: |
| 776 | tst_flush(); |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 777 | break; |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 778 | |
Garrett Cooper | 6bfb01e | 2010-11-22 10:16:28 -0800 | [diff] [blame] | 779 | case -3: |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 780 | printf("Enter the current type (%i=FAIL, %i=BROK, %i=RETR, %i=CONF): ", |
Garrett Cooper | 6bfb01e | 2010-11-22 10:16:28 -0800 | [diff] [blame] | 781 | TFAIL, TBROK, TRETR, TCONF); |
| 782 | scanf("%d%c", &ttype, &chr); |
| 783 | printf("Enter file name (<cr> for none): "); |
| 784 | gets(fname); |
| 785 | if (strcmp(fname, "") == 0) |
| 786 | tst_brkm(ttype, tst_exit, RESM, ttype); |
| 787 | else |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 788 | tst_brk(ttype, fname, tst_exit, RES, ttype, fname); |
| 789 | break; |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 790 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 791 | case -4: |
| 792 | printf("Enter the current type (%i,%i,%i,%i,%i,%i,%i): ", |
| 793 | TPASS, TFAIL, TBROK, TWARN, TRETR, TINFO, TCONF); |
Garrett Cooper | 6bfb01e | 2010-11-22 10:16:28 -0800 | [diff] [blame] | 794 | scanf("%d%c", &ttype, &chr); |
| 795 | default: |
| 796 | printf("Enter file name (<cr> for none): "); |
| 797 | gets(fname); |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 798 | |
Garrett Cooper | 6bfb01e | 2010-11-22 10:16:28 -0800 | [diff] [blame] | 799 | if (strcmp(fname, "") == 0) |
| 800 | tst_resm(ttype, RESM, ttype); |
| 801 | else |
| 802 | tst_res(ttype, fname, RES, ttype, fname); |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 803 | break; |
| 804 | } |
| 805 | |
| 806 | } |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 807 | } |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 808 | |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame^] | 809 | #endif /* UNIT_TEST */ |