Tizen 2.0 Release
[external/libgnutls26.git] / tests / utils.c
1 /*
2  * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2010 Free Software
3  * Foundation, Inc.
4  *
5  * Author: Simon Josefsson
6  *
7  * This file is part of GnuTLS.
8  *
9  * GnuTLS is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * GnuTLS is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with GnuTLS; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include <stdio.h>
29 #include <stdlib.h>
30
31 #include "utils.h"
32
33 int debug = 0;
34 int error_count = 0;
35 int break_on_error = 0;
36
37 const char *pkcs3 =
38   "-----BEGIN DH PARAMETERS-----\n"
39   "MIGGAoGAtkxw2jlsVCsrfLqxrN+IrF/3W8vVFvDzYbLmxi2GQv9s/PQGWP1d9i22\n"
40   "P2DprfcJknWt7KhCI1SaYseOQIIIAYP78CfyIpGScW/vS8khrw0rlQiyeCvQgF3O\n"
41   "GeGOEywcw+oQT4SmFOD7H0smJe2CNyjYpexBXQ/A0mbTF9QKm1cCAQU=\n"
42   "-----END DH PARAMETERS-----\n";
43
44 void
45 fail (const char *format, ...)
46 {
47   va_list arg_ptr;
48
49   va_start (arg_ptr, format);
50   vfprintf (stderr, format, arg_ptr);
51   va_end (arg_ptr);
52   error_count++;
53   if (break_on_error)
54     exit (1);
55 }
56
57 void
58 success (const char *format, ...)
59 {
60   va_list arg_ptr;
61
62   va_start (arg_ptr, format);
63   vfprintf (stdout, format, arg_ptr);
64   va_end (arg_ptr);
65 }
66
67 void
68 escapeprint (const char *str, size_t len)
69 {
70   size_t i;
71
72   printf (" (length %d bytes):\n\t", (int) len);
73   for (i = 0; i < len; i++)
74     {
75       if (((str[i] & 0xFF) >= 'A' && (str[i] & 0xFF) <= 'Z') ||
76           ((str[i] & 0xFF) >= 'a' && (str[i] & 0xFF) <= 'z') ||
77           ((str[i] & 0xFF) >= '0' && (str[i] & 0xFF) <= '9')
78           || (str[i] & 0xFF) == ' ' || (str[i] & 0xFF) == '.')
79         printf ("%c", (str[i] & 0xFF));
80       else
81         printf ("\\x%02X", (str[i] & 0xFF));
82       if ((i + 1) % 16 == 0 && (i + 1) < len)
83         printf ("'\n\t'");
84     }
85   printf ("\n");
86 }
87
88 void
89 hexprint (const char *str, size_t len)
90 {
91   size_t i;
92
93   printf ("\t;; ");
94   for (i = 0; i < len; i++)
95     {
96       printf ("%02x ", (str[i] & 0xFF));
97       if ((i + 1) % 8 == 0)
98         printf (" ");
99       if ((i + 1) % 16 == 0 && i + 1 < len)
100         printf ("\n\t;; ");
101     }
102   printf ("\n");
103 }
104
105 void
106 binprint (const char *str, size_t len)
107 {
108   size_t i;
109
110   printf ("\t;; ");
111   for (i = 0; i < len; i++)
112     {
113       printf ("%d%d%d%d%d%d%d%d ",
114               (str[i] & 0xFF) & 0x80 ? 1 : 0,
115               (str[i] & 0xFF) & 0x40 ? 1 : 0,
116               (str[i] & 0xFF) & 0x20 ? 1 : 0,
117               (str[i] & 0xFF) & 0x10 ? 1 : 0,
118               (str[i] & 0xFF) & 0x08 ? 1 : 0,
119               (str[i] & 0xFF) & 0x04 ? 1 : 0,
120               (str[i] & 0xFF) & 0x02 ? 1 : 0, (str[i] & 0xFF) & 0x01 ? 1 : 0);
121       if ((i + 1) % 3 == 0)
122         printf (" ");
123       if ((i + 1) % 6 == 0 && i + 1 < len)
124         printf ("\n\t;; ");
125     }
126   printf ("\n");
127 }
128
129 int
130 main (int argc, char *argv[])
131 {
132   do
133     if (strcmp (argv[argc - 1], "-v") == 0 ||
134         strcmp (argv[argc - 1], "--verbose") == 0)
135       debug = 1;
136     else if (strcmp (argv[argc - 1], "-b") == 0 ||
137              strcmp (argv[argc - 1], "--break-on-error") == 0)
138       break_on_error = 1;
139     else if (strcmp (argv[argc - 1], "-h") == 0 ||
140              strcmp (argv[argc - 1], "-?") == 0 ||
141              strcmp (argv[argc - 1], "--help") == 0)
142       {
143         printf ("Usage: %s [-vbh?] [--verbose] [--break-on-error] [--help]\n",
144                 argv[0]);
145         return 1;
146       }
147   while (argc-- > 1);
148
149   doit ();
150
151   printf ("Self test `%s' finished with %d errors\n", argv[0], error_count);
152
153   return error_count ? 1 : 0;
154 }