security: add 2 test for Digest authentication
[siplcs.git] / src / core / sip-sec-digest-tests.c
blobac4eea415bfc6694e41c8ebb854793e95163790c
1 /**
2 * @file sip-sec-digest-test.c
4 * pidgin-sipe
6 * Copyright (C) 2013 SIPE Project <http://sipe.sourceforge.net/>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <stdarg.h>
28 #include <glib.h>
30 #include "sipe-common.h"
31 #include "sipe-crypt.h"
32 #include "uuid.h"
34 #define SIP_SEC_DIGEST_COMPILING_TEST
35 static const gchar *cnonce_fixed;
36 #include "sip-sec-digest.c"
39 * Stubs
41 gboolean sipe_backend_debug_enabled(void)
43 return(TRUE);
46 void sipe_backend_debug_literal(sipe_debug_level level,
47 const gchar *msg)
49 printf("DEBUG(%d): %s\n", level, msg);
52 void sipe_backend_debug(sipe_debug_level level,
53 const gchar *format,
54 ...)
56 va_list ap;
57 gchar *newformat = g_strdup_printf("DEBUG(%d): %s\n", level, format);
59 va_start(ap, format);
60 vprintf(newformat, ap);
61 va_end(ap);
63 g_free(newformat);
66 const gchar *sipe_backend_network_ip_address(SIPE_UNUSED_PARAMETER struct sipe_core_public *sipe_public)
68 return(NULL);
71 char *generateUUIDfromEPID(SIPE_UNUSED_PARAMETER const gchar *epid)
73 return(NULL);
76 char *sipe_get_epid(SIPE_UNUSED_PARAMETER const char *self_sip_uri,
77 SIPE_UNUSED_PARAMETER const char *hostname,
78 SIPE_UNUSED_PARAMETER const char *ip_address)
80 return(NULL);
84 * Tester code
86 #define PARSED_USERNAME 0
87 #define PARSED_REALM 1
88 #define PARSED_NONCE 2
89 #define PARSED_URI 3
90 #define PARSED_QOP 4
91 #define PARSED_NC 5
92 #define PARSED_CNONCE 6
93 #define PARSED_RESPONSE 7
94 #define PARSED_OPAQUE 8
95 #define PARSED_MAX 9
96 static void parse(const gchar *string,
97 gchar *parsed[PARSED_MAX])
99 const gchar *header;
100 const gchar *param;
101 guint i;
103 for (i = 0; i < PARSED_MAX; i++)
104 parsed[i] = NULL;
106 if (strstr(string, "Digest ") == NULL)
107 return;
108 header = string + 7;
110 /* skip white space */
111 while (*header == ' ')
112 header++;
114 /* start of next parameter value */
115 while ((param = strchr(header, '=')) != NULL) {
116 const gchar *end;
118 /* parameter value type */
119 param++;
120 if (*param == '"') {
121 /* string: xyz="..."(,) */
122 end = strchr(++param, '"');
123 if (!end) {
124 SIPE_DEBUG_ERROR("parse: corrupted string parameter near '%s'", header);
125 break;
127 } else {
128 /* number: xyz=12345(,) */
129 end = strchr(param, ',');
130 if (!end) {
131 /* last parameter */
132 end = param + strlen(param);
136 #define COMPARE(string, index) \
137 if (g_str_has_prefix(header, #string "=")) { \
138 g_free(parsed[ PARSED_ ## index]); \
139 parsed[ PARSED_ ## index] = g_strndup(param, end - param); \
140 } else
142 COMPARE(username, USERNAME)
143 COMPARE(realm, REALM)
144 COMPARE(nonce, NONCE)
145 COMPARE(uri, URI)
146 COMPARE(qop, QOP)
147 COMPARE(nc, NC)
148 COMPARE(cnonce, CNONCE)
149 COMPARE(response, RESPONSE)
150 COMPARE(opaque, OPAQUE)
151 { /* ignore */ }
153 /* skip to next parameter */
154 while ((*end == '"') || (*end == ',') || (*end == ' '))
155 end++;
156 header = end;
160 static guint expected(const gchar *reference,
161 const gchar *testvalue)
163 gchar *reference_parsed[PARSED_MAX];
164 gchar *testvalue_parsed[PARSED_MAX];
165 guint i;
166 guint failed = 0;
168 parse(reference, reference_parsed);
169 parse(testvalue, testvalue_parsed);
170 for (i = 0; i < PARSED_MAX; i++) {
171 gchar *ref = reference_parsed[i];
172 gchar *test = testvalue_parsed[i];
173 if (!sipe_strequal(ref, test) && (ref || test)) {
174 SIPE_DEBUG_ERROR("FAILED(%d): expected '%s' got '%s'",
175 i, ref, test);
176 failed = 1;
178 g_free(test);
179 g_free(ref);
181 SIPE_DEBUG_INFO("Response: %s", testvalue);
183 return(failed);
186 int main(SIPE_UNUSED_PARAMETER int argc, SIPE_UNUSED_PARAMETER char *argv[])
188 guint failed = 0;
190 /* Initialization for crypto backend (test mode) */
191 sipe_crypto_init(FALSE);
193 #define RUNTEST(_user, _password, _cnonce, _header, _method, _uri, _reference) \
195 struct sipe_core_private sipe_private; \
196 gchar *response; \
197 printf("\n"); \
198 sipe_private.authuser = _user ; \
199 sipe_private.password = _password ; \
200 cnonce_fixed = _cnonce; \
201 response = sip_sec_digest_authorization(&sipe_private, _header, _method, _uri); \
202 failed += expected(_reference, response); \
203 g_free(response); \
207 * RFC-2617 Section 3.5
209 RUNTEST("Mufasa", "Circle Of Life", "0a4f113b",
210 "realm=\"testrealm@host.com\", qop=\"auth,auth-int\", nonce=\"dcd98b7102dd2f0e8b11d0f600bfb0c093\", opaque=\"5ccc069c403ebaf9f0171e9517f40e41\"",
211 "GET",
212 "/dir/index.html",
213 "Digest username=\"Mufasa\", realm=\"testrealm@host.com\", nonce=\"dcd98b7102dd2f0e8b11d0f600bfb0c093\", uri=\"/dir/index.html\", qop=auth, nc=00000001, cnonce=\"0a4f113b\", response=\"6629fae49393a05397450978507c4ef1\", opaque=\"5ccc069c403ebaf9f0171e9517f40e41\"");
216 * http://www.ntu.edu.sg/home/ehchua/programming/webprogramming/HTTP_Authentication.html
218 RUNTEST("bob", "bob", "1672b410efa182c061c2f0a58acaa17d",
220 * The Server challenge shown does not correspond to the
221 * Client response. Use realm/nonce from the Client response.
223 * "realm=\"Members only\", nonce=\"LHOKe1l2BAA=5c373ae0d933a0bb6321125a56a2fcdb6fd7c93b\", algorithm=MD5, qop=\"auth\"",
225 "realm=\"members only\", nonce=\"5UImQA==3d76b2ab859e1770ec60ed285ec68a3e63028461\", algorithm=MD5, qop=\"auth\"",
226 "GET",
227 "/digest_auth/test.html",
228 "Digest username=\"bob\", realm=\"members only\", qop=\"auth\", algorithm=\"MD5\", uri=\"/digest_auth/test.html\", nonce=\"5UImQA==3d76b2ab859e1770ec60ed285ec68a3e63028461\", nc=00000001, cnonce=\"1672b410efa182c061c2f0a58acaa17d\", response=\"3d9ebe6b9534a7135a3fde59a5a72668\"");
230 return(failed);
234 Local Variables:
235 mode: c
236 c-file-style: "bsd"
237 indent-tabs-mode: t
238 tab-width: 8
239 End: