Added function to sort the provided certificate chain prior to verification.
[gnutls.git] / tests / utils.c
blob490c1e2f3aca58763f65c69d6b575b6ce716e453
1 /*
2 * Copyright (C) 2004-2012 Free Software Foundation, Inc.
4 * Author: Simon Josefsson
6 * This file is part of GnuTLS.
8 * GnuTLS is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * GnuTLS is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with GnuTLS; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <stdarg.h>
31 #include "utils.h"
33 int debug = 0;
34 int error_count = 0;
35 int break_on_error = 0;
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";
44 void
45 fail (const char *format, ...)
47 char str[1024];
48 va_list arg_ptr;
50 va_start (arg_ptr, format);
51 vsnprintf ( str, sizeof(str), format, arg_ptr);
52 va_end (arg_ptr);
53 fputs(str, stderr);
54 error_count++;
55 if (break_on_error)
56 exit (1);
59 void
60 success (const char *format, ...)
62 char str[1024];
63 va_list arg_ptr;
65 va_start (arg_ptr, format);
66 vsnprintf ( str, sizeof(str), format, arg_ptr);
67 va_end (arg_ptr);
68 fputs(str, stderr);
71 void
72 escapeprint (const char *str, size_t len)
74 size_t i;
76 printf (" (length %d bytes):\n\t", (int) len);
77 for (i = 0; i < len; i++)
79 if (((str[i] & 0xFF) >= 'A' && (str[i] & 0xFF) <= 'Z') ||
80 ((str[i] & 0xFF) >= 'a' && (str[i] & 0xFF) <= 'z') ||
81 ((str[i] & 0xFF) >= '0' && (str[i] & 0xFF) <= '9')
82 || (str[i] & 0xFF) == ' ' || (str[i] & 0xFF) == '.')
83 printf ("%c", (str[i] & 0xFF));
84 else
85 printf ("\\x%02X", (str[i] & 0xFF));
86 if ((i + 1) % 16 == 0 && (i + 1) < len)
87 printf ("'\n\t'");
89 printf ("\n");
92 void
93 hexprint (const void *_str, size_t len)
95 size_t i;
96 const char* str = _str;
98 printf ("\t;; ");
99 for (i = 0; i < len; i++)
101 printf ("%02x ", (str[i] & 0xFF));
102 if ((i + 1) % 8 == 0)
103 printf (" ");
104 if ((i + 1) % 16 == 0 && i + 1 < len)
105 printf ("\n\t;; ");
107 printf ("\n");
110 void
111 binprint (const void *_str, size_t len)
113 size_t i;
114 const char* str = _str;
116 printf ("\t;; ");
117 for (i = 0; i < len; i++)
119 printf ("%d%d%d%d%d%d%d%d ",
120 (str[i] & 0xFF) & 0x80 ? 1 : 0,
121 (str[i] & 0xFF) & 0x40 ? 1 : 0,
122 (str[i] & 0xFF) & 0x20 ? 1 : 0,
123 (str[i] & 0xFF) & 0x10 ? 1 : 0,
124 (str[i] & 0xFF) & 0x08 ? 1 : 0,
125 (str[i] & 0xFF) & 0x04 ? 1 : 0,
126 (str[i] & 0xFF) & 0x02 ? 1 : 0, (str[i] & 0xFF) & 0x01 ? 1 : 0);
127 if ((i + 1) % 3 == 0)
128 printf (" ");
129 if ((i + 1) % 6 == 0 && i + 1 < len)
130 printf ("\n\t;; ");
132 printf ("\n");
136 main (int argc, char *argv[])
139 if (strcmp (argv[argc - 1], "-v") == 0 ||
140 strcmp (argv[argc - 1], "--verbose") == 0)
141 debug = 1;
142 else if (strcmp (argv[argc - 1], "-b") == 0 ||
143 strcmp (argv[argc - 1], "--break-on-error") == 0)
144 break_on_error = 1;
145 else if (strcmp (argv[argc - 1], "-h") == 0 ||
146 strcmp (argv[argc - 1], "-?") == 0 ||
147 strcmp (argv[argc - 1], "--help") == 0)
149 printf ("Usage: %s [-vbh?] [--verbose] [--break-on-error] [--help]\n",
150 argv[0]);
151 return 1;
153 while (argc-- > 1);
155 doit ();
157 if (debug || error_count > 0)
158 printf ("Self test `%s' finished with %d errors\n", argv[0], error_count);
160 return error_count ? 1 : 0;