Add.
[gsasl.git] / tests / utils.c
blobf469db10a2c8e95e92113209dae99ea213432426
1 /* utils.c --- Self test utilities.
2 * Copyright (C) 2002, 2003, 2004, 2005, 2007 Simon Josefsson
4 * This file is part of GNU SASL.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
25 #include <stdio.h>
26 #include <stdlib.h>
28 #include "utils.h"
30 int debug = 1;
31 int error_count = 0;
32 int break_on_error = 0;
34 void
35 fail (const char *format, ...)
37 va_list arg_ptr;
39 va_start (arg_ptr, format);
40 vfprintf (stderr, format, arg_ptr);
41 va_end (arg_ptr);
42 error_count++;
43 if (break_on_error)
44 exit (1);
47 void
48 success (const char *format, ...)
50 va_list arg_ptr;
52 va_start (arg_ptr, format);
53 if (debug)
54 vfprintf (stdout, format, arg_ptr);
55 va_end (arg_ptr);
58 void
59 escapeprint (const char *str, size_t len)
61 size_t i;
63 printf (" (length %d bytes):\n\t", len);
64 for (i = 0; i < len; i++)
66 if (((str[i] & 0xFF) >= 'A' && (str[i] & 0xFF) <= 'Z') ||
67 ((str[i] & 0xFF) >= 'a' && (str[i] & 0xFF) <= 'z') ||
68 ((str[i] & 0xFF) >= '0' && (str[i] & 0xFF) <= '9')
69 || (str[i] & 0xFF) == ' ' || (str[i] & 0xFF) == '.')
70 printf ("%c", (str[i] & 0xFF));
71 else
72 printf ("\\x%02X", (str[i] & 0xFF));
73 if ((i + 1) % 16 == 0 && (i + 1) < len)
74 printf ("'\n\t'");
76 printf ("\n");
79 void
80 hexprint (const char *str, size_t len)
82 size_t i;
84 printf ("\t;; ");
85 for (i = 0; i < len; i++)
87 printf ("%02x ", (str[i] & 0xFF));
88 if ((i + 1) % 8 == 0)
89 printf (" ");
90 if ((i + 1) % 16 == 0 && i + 1 < len)
91 printf ("\n\t;; ");
93 printf ("\n");
96 void
97 binprint (const char *str, size_t len)
99 size_t i;
101 printf ("\t;; ");
102 for (i = 0; i < len; i++)
104 printf ("%d%d%d%d%d%d%d%d ",
105 (str[i] & 0xFF) & 0x80 ? 1 : 0,
106 (str[i] & 0xFF) & 0x40 ? 1 : 0,
107 (str[i] & 0xFF) & 0x20 ? 1 : 0,
108 (str[i] & 0xFF) & 0x10 ? 1 : 0,
109 (str[i] & 0xFF) & 0x08 ? 1 : 0,
110 (str[i] & 0xFF) & 0x04 ? 1 : 0,
111 (str[i] & 0xFF) & 0x02 ? 1 : 0, (str[i] & 0xFF) & 0x01 ? 1 : 0);
112 if ((i + 1) % 3 == 0)
113 printf (" ");
114 if ((i + 1) % 6 == 0 && i + 1 < len)
115 printf ("\n\t;; ");
117 printf ("\n");
121 main (int argc, char *argv[])
124 if (strcmp (argv[argc - 1], "-v") == 0 ||
125 strcmp (argv[argc - 1], "--verbose") == 0)
126 debug = 1;
127 else if (strcmp (argv[argc - 1], "-b") == 0 ||
128 strcmp (argv[argc - 1], "--break-on-error") == 0)
129 break_on_error = 1;
130 else if (strcmp (argv[argc - 1], "-h") == 0 ||
131 strcmp (argv[argc - 1], "-?") == 0 ||
132 strcmp (argv[argc - 1], "--help") == 0)
134 printf ("Usage: %s [-vbh?] [--verbose] [--break-on-error] [--help]\n",
135 argv[0]);
136 return 1;
138 while (argc-- > 1);
140 doit ();
142 if (debug)
143 printf ("Self test `%s' finished with %d errors\n", argv[0], error_count);
145 return error_count ? 1 : 0;