Add.
[gsasl.git] / tests / utils.c
blob901464babe886097e2a055ab98cc667245780b88
1 /* utils.c --- Self test utilities.
2 * Copyright (C) 2002, 2003, 2004, 2005 Simon Josefsson
4 * This file is part of GNU SASL.
6 * GNU SASL is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * GNU SASL is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GNU SASL; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #ifdef HAVE_CONFIG_H
23 # include "config.h"
24 #endif
26 #include <stdio.h>
27 #include <stdlib.h>
29 #include "utils.h"
31 int debug = 1;
32 int error_count = 0;
33 int break_on_error = 0;
35 void
36 fail (const char *format, ...)
38 va_list arg_ptr;
40 va_start (arg_ptr, format);
41 vfprintf (stderr, format, arg_ptr);
42 va_end (arg_ptr);
43 error_count++;
44 if (break_on_error)
45 exit (1);
48 void
49 success (const char *format, ...)
51 va_list arg_ptr;
53 va_start (arg_ptr, format);
54 if (debug)
55 vfprintf (stdout, format, arg_ptr);
56 va_end (arg_ptr);
59 void
60 escapeprint (const char *str, size_t len)
62 size_t i;
64 printf (" (length %d bytes):\n\t", len);
65 for (i = 0; i < len; i++)
67 if (((str[i] & 0xFF) >= 'A' && (str[i] & 0xFF) <= 'Z') ||
68 ((str[i] & 0xFF) >= 'a' && (str[i] & 0xFF) <= 'z') ||
69 ((str[i] & 0xFF) >= '0' && (str[i] & 0xFF) <= '9')
70 || (str[i] & 0xFF) == ' ' || (str[i] & 0xFF) == '.')
71 printf ("%c", (str[i] & 0xFF));
72 else
73 printf ("\\x%02X", (str[i] & 0xFF));
74 if ((i + 1) % 16 == 0 && (i + 1) < len)
75 printf ("'\n\t'");
77 printf ("\n");
80 void
81 hexprint (const char *str, size_t len)
83 size_t i;
85 printf ("\t;; ");
86 for (i = 0; i < len; i++)
88 printf ("%02x ", (str[i] & 0xFF));
89 if ((i + 1) % 8 == 0)
90 printf (" ");
91 if ((i + 1) % 16 == 0 && i + 1 < len)
92 printf ("\n\t;; ");
94 printf ("\n");
97 void
98 binprint (const char *str, size_t len)
100 size_t i;
102 printf ("\t;; ");
103 for (i = 0; i < len; i++)
105 printf ("%d%d%d%d%d%d%d%d ",
106 (str[i] & 0xFF) & 0x80 ? 1 : 0,
107 (str[i] & 0xFF) & 0x40 ? 1 : 0,
108 (str[i] & 0xFF) & 0x20 ? 1 : 0,
109 (str[i] & 0xFF) & 0x10 ? 1 : 0,
110 (str[i] & 0xFF) & 0x08 ? 1 : 0,
111 (str[i] & 0xFF) & 0x04 ? 1 : 0,
112 (str[i] & 0xFF) & 0x02 ? 1 : 0, (str[i] & 0xFF) & 0x01 ? 1 : 0);
113 if ((i + 1) % 3 == 0)
114 printf (" ");
115 if ((i + 1) % 6 == 0 && i + 1 < len)
116 printf ("\n\t;; ");
118 printf ("\n");
122 main (int argc, char *argv[])
125 if (strcmp (argv[argc - 1], "-v") == 0 ||
126 strcmp (argv[argc - 1], "--verbose") == 0)
127 debug = 1;
128 else if (strcmp (argv[argc - 1], "-b") == 0 ||
129 strcmp (argv[argc - 1], "--break-on-error") == 0)
130 break_on_error = 1;
131 else if (strcmp (argv[argc - 1], "-h") == 0 ||
132 strcmp (argv[argc - 1], "-?") == 0 ||
133 strcmp (argv[argc - 1], "--help") == 0)
135 printf ("Usage: %s [-vbh?] [--verbose] [--break-on-error] [--help]\n",
136 argv[0]);
137 return 1;
139 while (argc-- > 1);
141 doit ();
143 if (debug)
144 printf ("Self test `%s' finished with %d errors\n", argv[0], error_count);
146 return error_count ? 1 : 0;