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, 2006  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 int
00032 main (int argc, char *argv[])
00033 {
00034   digest_md5_challenge c;
00035   digest_md5_response r;
00036   digest_md5_finish f;
00037   char buf32[33];
00038   char buf16[16];
00039   int rc;
00040   char *tmp;
00041 
00042   {
00043     char *token = "nonce=4711, foo=bar, algorithm=md5-sess";
00044 
00045     printf ("challenge `%s': ", token);
00046     rc = digest_md5_parse_challenge (token, 0, &c);
00047     if (rc != 0)
00048       abort ();
00049     printf ("nonce `%s': %s", c.nonce,
00050             strcmp ("4711", c.nonce) == 0 ? "PASS" : "FAILURE");
00051     printf ("\n");
00052     tmp = digest_md5_print_challenge (&c);
00053     if (!tmp)
00054       abort ();
00055     printf ("printed `%s' PASS\n", tmp);
00056     free (tmp);
00057   }
00058 
00059   {
00060     char *token = "qop=\"auth, auth-conf\", nonce=42, algorithm=md5-sess";
00061 
00062     printf ("challenge `%s': ", token);
00063     rc = digest_md5_parse_challenge (token, 0, &c);
00064     if (rc == 0)
00065       abort ();
00066     printf ("PASS\n");
00067   }
00068 
00069   {
00070     char *token = "cipher=\"des\", nonce=42, algorithm=md5-sess";
00071 
00072     printf ("challenge `%s': ", token);
00073     rc = digest_md5_parse_challenge (token, 0, &c);
00074     if (rc == 0)
00075       abort ();
00076     printf ("PASS\n");
00077   }
00078 
00079   {
00080     char *token = "qop=\"auth, auth-conf\", nonce=42, algorithm=md5-sess, "
00081       "cipher=\"des\"";
00082 
00083     printf ("challenge `%s': ", token);
00084     rc = digest_md5_parse_challenge (token, 0, &c);
00085     if (rc != 0)
00086       abort ();
00087     printf ("qop %02x ciphers %02x: %s\n", c.qops, c.ciphers,
00088             (c.qops == 5 && c.ciphers == 1) ? "PASS" : "FAILURE");
00089     tmp = digest_md5_print_challenge (&c);
00090     if (!tmp)
00091       abort ();
00092     printf ("printed `%s' PASS\n", tmp);
00093     free (tmp);
00094   }
00095 
00096   {
00097     char *token = "bar=foo, foo=bar";
00098 
00099     printf ("challenge `%s': ", token);
00100     rc = digest_md5_parse_challenge (token, 0, &c);
00101     if (rc == 0)
00102       abort ();
00103     printf ("PASS\n");
00104   }
00105 
00106   {
00107     char *token = "realm=foo, realm=bar, nonce=42, algorithm=md5-sess";
00108 
00109     printf ("challenge `%s': ", token);
00110     rc = digest_md5_parse_challenge (token, 0, &c);
00111     if (rc != 0)
00112       abort ();
00113     if (c.nrealms != 2)
00114       abort ();
00115     printf ("realms `%s', `%s': PASS\n", c.realms[0], c.realms[1]);
00116     tmp = digest_md5_print_challenge (&c);
00117     if (!tmp)
00118       abort ();
00119     printf ("printed `%s' PASS\n", tmp);
00120     free (tmp);
00121   }
00122 
00123   /* Response */
00124 
00125   {
00126     char *token = "bar=foo, foo=bar";
00127 
00128     printf ("response `%s': ", token);
00129     rc = digest_md5_parse_response (token, 0, &r);
00130     if (rc == 0)
00131       abort ();
00132     printf ("PASS\n");
00133   }
00134 
00135   {
00136     char *token = "username=jas, nonce=42, cnonce=4711, nc=00000001, "
00137       "digest-uri=foo, response=01234567890123456789012345678901";
00138 
00139     printf ("response `%s': ", token);
00140     rc = digest_md5_parse_response (token, 0, &r);
00141     if (rc != 0)
00142       abort ();
00143     printf ("username `%s', nonce `%s', cnonce `%s',"
00144             " nc %08lx, digest-uri `%s', response `%s': PASS\n",
00145             r.username, r.nonce, r.cnonce, r.nc, r.digesturi, r.response);
00146     tmp = digest_md5_print_response (&r);
00147     if (!tmp)
00148       abort ();
00149     printf ("printed `%s' PASS\n", tmp);
00150     free (tmp);
00151   }
00152 
00153   /* Auth-response, finish. */
00154 
00155   {
00156     char *token = "rspauth=\"6a204da26b9888ee40bb3052ff056a67\"";
00157 
00158     printf ("finish `%s': ", token);
00159     rc = digest_md5_parse_finish (token, 0, &f);
00160     if (rc != 0)
00161       abort ();
00162     printf ("`%s'? %s\n", f.rspauth,
00163             strcmp ("6a204da26b9888ee40bb3052ff056a67", f.rspauth) == 0
00164             ? "ok" : "FAILURE");
00165   }
00166 
00167   {
00168     char *token = "bar=foo, foo=bar";
00169 
00170     printf ("finish `%s': ", token);
00171     rc = digest_md5_parse_finish (token, 0, &f);
00172     if (rc == 0)
00173       abort ();
00174     printf ("invalid? PASS\n", token);
00175   }
00176 
00177   memset (buf16, 'Q', 16);
00178 
00179   rc = digest_md5_hmac (buf32, buf16, "nonce", 1, "cnonce",
00180                         DIGEST_MD5_QOP_AUTH, "authzid", "digesturi",
00181                         1, 0, NULL, NULL, NULL, NULL);
00182   if (rc != 0)
00183     abort ();
00184   buf32[32] = '\0';
00185   if (strcmp (buf32, "6a204da26b9888ee40bb3052ff056a67") != 0)
00186     abort ();
00187   printf ("digest: `%s': PASS\n", buf32);
00188 
00189   rc = digest_md5_hmac (buf32, buf16, "nonce", 1, "cnonce",
00190                         DIGEST_MD5_QOP_AUTH, "authzid", "digesturi", 0, 0,
00191                         NULL, NULL, NULL, NULL);
00192   if (rc != 0)
00193     abort ();
00194   buf32[32] = '\0';
00195   if (strcmp (buf32, "6c1f58bfa46e9c225b93745c84204efd") != 0)
00196     abort ();
00197   printf ("digest: `%s': PASS\n", buf32);
00198 
00199   return 0;
00200 }

Generated on Tue Aug 22 12:06:07 2006 for gsasl by  doxygen 1.4.7