Update gnulib.
[gsasl.git] / lib / digest-md5 / test-parser.c
blobc4e04de176b9097e3956d1b38f2877614de86e1c
1 /* test-parser.c --- Self tests of DIGEST-MD5 parser & printer.
2 * Copyright (C) 2004, 2006 Simon Josefsson
4 * This file is part of GNU SASL Library.
6 * GNU SASL Library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public License
8 * as published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
11 * GNU SASL Library 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with GNU SASL Library; if not, write to the Free
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
27 #include "parser.h"
28 #include "printer.h"
29 #include "digesthmac.h"
31 int
32 main (int argc, char *argv[])
34 digest_md5_challenge c;
35 digest_md5_response r;
36 digest_md5_finish f;
37 char buf32[33];
38 char buf16[16];
39 int rc;
40 char *tmp;
43 char *token = "nonce=4711, foo=bar, algorithm=md5-sess";
45 printf ("challenge `%s': ", token);
46 rc = digest_md5_parse_challenge (token, 0, &c);
47 if (rc != 0)
48 abort ();
49 printf ("nonce `%s': %s", c.nonce,
50 strcmp ("4711", c.nonce) == 0 ? "PASS" : "FAILURE");
51 printf ("\n");
52 tmp = digest_md5_print_challenge (&c);
53 if (!tmp)
54 abort ();
55 printf ("printed `%s' PASS\n", tmp);
56 free (tmp);
60 char *token = "qop=\"auth, auth-conf\", nonce=42, algorithm=md5-sess";
62 printf ("challenge `%s': ", token);
63 rc = digest_md5_parse_challenge (token, 0, &c);
64 if (rc == 0)
65 abort ();
66 printf ("PASS\n");
70 char *token = "cipher=\"des\", nonce=42, algorithm=md5-sess";
72 printf ("challenge `%s': ", token);
73 rc = digest_md5_parse_challenge (token, 0, &c);
74 if (rc == 0)
75 abort ();
76 printf ("PASS\n");
80 char *token = "qop=\"auth, auth-conf\", nonce=42, algorithm=md5-sess, "
81 "cipher=\"des\"";
83 printf ("challenge `%s': ", token);
84 rc = digest_md5_parse_challenge (token, 0, &c);
85 if (rc != 0)
86 abort ();
87 printf ("qop %02x ciphers %02x: %s\n", c.qops, c.ciphers,
88 (c.qops == 5 && c.ciphers == 1) ? "PASS" : "FAILURE");
89 tmp = digest_md5_print_challenge (&c);
90 if (!tmp)
91 abort ();
92 printf ("printed `%s' PASS\n", tmp);
93 free (tmp);
97 char *token = "bar=foo, foo=bar";
99 printf ("challenge `%s': ", token);
100 rc = digest_md5_parse_challenge (token, 0, &c);
101 if (rc == 0)
102 abort ();
103 printf ("PASS\n");
107 char *token = "realm=foo, realm=bar, nonce=42, algorithm=md5-sess";
109 printf ("challenge `%s': ", token);
110 rc = digest_md5_parse_challenge (token, 0, &c);
111 if (rc != 0)
112 abort ();
113 if (c.nrealms != 2)
114 abort ();
115 printf ("realms `%s', `%s': PASS\n", c.realms[0], c.realms[1]);
116 tmp = digest_md5_print_challenge (&c);
117 if (!tmp)
118 abort ();
119 printf ("printed `%s' PASS\n", tmp);
120 free (tmp);
123 /* Response */
126 char *token = "bar=foo, foo=bar";
128 printf ("response `%s': ", token);
129 rc = digest_md5_parse_response (token, 0, &r);
130 if (rc == 0)
131 abort ();
132 printf ("PASS\n");
136 char *token = "username=jas, nonce=42, cnonce=4711, nc=00000001, "
137 "digest-uri=foo, response=01234567890123456789012345678901";
139 printf ("response `%s': ", token);
140 rc = digest_md5_parse_response (token, 0, &r);
141 if (rc != 0)
142 abort ();
143 printf ("username `%s', nonce `%s', cnonce `%s',"
144 " nc %08lx, digest-uri `%s', response `%s': PASS\n",
145 r.username, r.nonce, r.cnonce, r.nc, r.digesturi, r.response);
146 tmp = digest_md5_print_response (&r);
147 if (!tmp)
148 abort ();
149 printf ("printed `%s' PASS\n", tmp);
150 free (tmp);
153 /* Auth-response, finish. */
156 char *token = "rspauth=\"6a204da26b9888ee40bb3052ff056a67\"";
158 printf ("finish `%s': ", token);
159 rc = digest_md5_parse_finish (token, 0, &f);
160 if (rc != 0)
161 abort ();
162 printf ("`%s'? %s\n", f.rspauth,
163 strcmp ("6a204da26b9888ee40bb3052ff056a67", f.rspauth) == 0
164 ? "ok" : "FAILURE");
168 char *token = "bar=foo, foo=bar";
170 printf ("finish `%s': ", token);
171 rc = digest_md5_parse_finish (token, 0, &f);
172 if (rc == 0)
173 abort ();
174 printf ("invalid? PASS\n", token);
177 memset (buf16, 'Q', 16);
179 rc = digest_md5_hmac (buf32, buf16, "nonce", 1, "cnonce",
180 DIGEST_MD5_QOP_AUTH, "authzid", "digesturi",
181 1, 0, NULL, NULL, NULL, NULL);
182 if (rc != 0)
183 abort ();
184 buf32[32] = '\0';
185 if (strcmp (buf32, "6a204da26b9888ee40bb3052ff056a67") != 0)
186 abort ();
187 printf ("digest: `%s': PASS\n", buf32);
189 rc = digest_md5_hmac (buf32, buf16, "nonce", 1, "cnonce",
190 DIGEST_MD5_QOP_AUTH, "authzid", "digesturi", 0, 0,
191 NULL, NULL, NULL, NULL);
192 if (rc != 0)
193 abort ();
194 buf32[32] = '\0';
195 if (strcmp (buf32, "6c1f58bfa46e9c225b93745c84204efd") != 0)
196 abort ();
197 printf ("digest: `%s': PASS\n", buf32);
199 return 0;