test-parser.c

Go to the documentation of this file.
00001 /* test-parser.c --- Self tests of DIGEST-MD5 parser & printer.
00002  * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009  Simon Josefsson
00003  *
00004  * This file is part of GNU SASL Library.
00005  *
00006  * GNU SASL Library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public License
00008  * as published by the Free Software Foundation; either version 2.1 of
00009  * the License, or (at your option) any later version.
00010  *
00011  * GNU SASL Library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with GNU SASL Library; if not, write to the Free
00018  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019  * Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include <string.h>
00026 
00027 #include "parser.h"
00028 #include "printer.h"
00029 #include "digesthmac.h"
00030 
00031 #include "gc.h"
00032 
00033 int
00034 main (int argc, char *argv[])
00035 {
00036   digest_md5_challenge c;
00037   digest_md5_response r;
00038   digest_md5_finish f;
00039   char buf32[33];
00040   char buf16[16];
00041   int rc;
00042   char *tmp;
00043 
00044   {
00045     const char *token = "nonce=4711, foo=bar, algorithm=md5-sess";
00046 
00047     printf ("challenge `%s': ", token);
00048     rc = digest_md5_parse_challenge (token, 0, &c);
00049     if (rc != 0)
00050       abort ();
00051     printf ("nonce `%s': %s", c.nonce,
00052             strcmp ("4711", c.nonce) == 0 ? "PASS" : "FAILURE");
00053     printf ("\n");
00054     tmp = digest_md5_print_challenge (&c);
00055     if (!tmp)
00056       abort ();
00057     printf ("printed `%s' PASS\n", tmp);
00058     free (tmp);
00059   }
00060 
00061   {
00062     const char *token = "qop=\"auth, auth-conf\", nonce=42, algorithm=md5-sess";
00063 
00064     printf ("challenge `%s': ", token);
00065     rc = digest_md5_parse_challenge (token, 0, &c);
00066     if (rc == 0)
00067       abort ();
00068     printf ("PASS\n");
00069   }
00070 
00071   {
00072     const char *token = "cipher=\"des\", nonce=42, algorithm=md5-sess";
00073 
00074     printf ("challenge `%s': ", token);
00075     rc = digest_md5_parse_challenge (token, 0, &c);
00076     if (rc == 0)
00077       abort ();
00078     printf ("PASS\n");
00079   }
00080 
00081   {
00082     const char *token = "qop=\"auth, auth-conf\", nonce=42, "
00083       "algorithm=md5-sess, cipher=\"des\"";
00084 
00085     printf ("challenge `%s': ", token);
00086     rc = digest_md5_parse_challenge (token, 0, &c);
00087     if (rc != 0)
00088       abort ();
00089     printf ("qop %02x ciphers %02x: %s\n", c.qops, c.ciphers,
00090             (c.qops == 5 && c.ciphers == 1) ? "PASS" : "FAILURE");
00091     tmp = digest_md5_print_challenge (&c);
00092     if (!tmp)
00093       abort ();
00094     printf ("printed `%s' PASS\n", tmp);
00095     free (tmp);
00096   }
00097 
00098   {
00099     const char *token = "bar=foo, foo=bar";
00100 
00101     printf ("challenge `%s': ", token);
00102     rc = digest_md5_parse_challenge (token, 0, &c);
00103     if (rc == 0)
00104       abort ();
00105     printf ("PASS\n");
00106   }
00107 
00108   {
00109     const char *token = "realm=foo, realm=bar, nonce=42, algorithm=md5-sess";
00110 
00111     printf ("challenge `%s': ", token);
00112     rc = digest_md5_parse_challenge (token, 0, &c);
00113     if (rc != 0)
00114       abort ();
00115     if (c.nrealms != 2)
00116       abort ();
00117     printf ("realms `%s', `%s': PASS\n", c.realms[0], c.realms[1]);
00118     tmp = digest_md5_print_challenge (&c);
00119     if (!tmp)
00120       abort ();
00121     printf ("printed `%s' PASS\n", tmp);
00122     free (tmp);
00123   }
00124 
00125   /* Response */
00126 
00127   {
00128     const char *token = "bar=foo, foo=bar";
00129 
00130     printf ("response `%s': ", token);
00131     rc = digest_md5_parse_response (token, 0, &r);
00132     if (rc == 0)
00133       abort ();
00134     printf ("PASS\n");
00135   }
00136 
00137   {
00138     const char *token = "username=jas, nonce=42, cnonce=4711, nc=00000001, "
00139       "digest-uri=foo, response=01234567890123456789012345678901";
00140 
00141     printf ("response `%s': ", token);
00142     rc = digest_md5_parse_response (token, 0, &r);
00143     if (rc != 0)
00144       abort ();
00145     printf ("username `%s', nonce `%s', cnonce `%s',"
00146             " nc %08lx, digest-uri `%s', response `%s': PASS\n",
00147             r.username, r.nonce, r.cnonce, r.nc, r.digesturi, r.response);
00148     tmp = digest_md5_print_response (&r);
00149     if (!tmp)
00150       abort ();
00151     printf ("printed `%s' PASS\n", tmp);
00152     free (tmp);
00153   }
00154 
00155   /* Auth-response, finish. */
00156 
00157   {
00158     const char *token = "rspauth=\"6a204da26b9888ee40bb3052ff056a67\"";
00159 
00160     printf ("finish `%s': ", token);
00161     rc = digest_md5_parse_finish (token, 0, &f);
00162     if (rc != 0)
00163       abort ();
00164     printf ("`%s'? %s\n", f.rspauth,
00165             strcmp ("6a204da26b9888ee40bb3052ff056a67", f.rspauth) == 0
00166             ? "ok" : "FAILURE");
00167   }
00168 
00169   {
00170     const char *token = "bar=foo, foo=bar";
00171 
00172     printf ("finish `%s': ", token);
00173     rc = digest_md5_parse_finish (token, 0, &f);
00174     if (rc == 0)
00175       abort ();
00176     printf ("invalid? PASS\n");
00177   }
00178 
00179   rc = gc_init ();
00180   if (rc != 0)
00181     abort ();
00182 
00183   memset (buf16, 'Q', 16);
00184 
00185   rc = digest_md5_hmac (buf32, buf16, "nonce", 1, "cnonce",
00186                         DIGEST_MD5_QOP_AUTH, "authzid", "digesturi",
00187                         1, 0, NULL, NULL, NULL, NULL);
00188   if (rc != 0)
00189     abort ();
00190   buf32[32] = '\0';
00191   if (strcmp (buf32, "6a204da26b9888ee40bb3052ff056a67") != 0)
00192     abort ();
00193   printf ("digest: `%s': PASS\n", buf32);
00194 
00195   rc = digest_md5_hmac (buf32, buf16, "nonce", 1, "cnonce",
00196                         DIGEST_MD5_QOP_AUTH, "authzid", "digesturi", 0, 0,
00197                         NULL, NULL, NULL, NULL);
00198   if (rc != 0)
00199     abort ();
00200   buf32[32] = '\0';
00201   if (strcmp (buf32, "6c1f58bfa46e9c225b93745c84204efd") != 0)
00202     abort ();
00203   printf ("digest: `%s': PASS\n", buf32);
00204 
00205   return 0;
00206 }