summaryrefslogtreecommitdiffstats
path: root/src/lib-http/test-http-auth.c
blob: 49ac7eef854f0b9df35bdf246adbadd23241fbc4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/* Copyright (c) 2013-2018 Dovecot authors, see the included COPYING file */

#include "test-lib.h"
#include "test-common.h"
#include "array.h"
#include "str-sanitize.h"
#include "http-auth.h"

struct http_auth_challenge_test {
	const char *scheme;
	const char *data;
	struct http_auth_param *params;
};

struct http_auth_challenges_test {
	const char *challenges_in;

	struct http_auth_challenge_test *challenges;
};

/* Valid auth challenges tests */
static const struct http_auth_challenges_test
valid_auth_challenges_tests[] = {
	{ 
		.challenges_in = "Basic realm=\"WallyWorld\"",
		.challenges = (struct http_auth_challenge_test []) {
			{ .scheme = "Basic",
				.data = NULL,
				.params = (struct http_auth_param []) { 
					{ "realm", "WallyWorld" }, { NULL, NULL }
				}
			},{
				.scheme = NULL
			}
		}
	},{
		.challenges_in = "Digest "
                 "realm=\"testrealm@host.com\", "
                 "qop=\"auth,auth-int\", "
                 "nonce=\"dcd98b7102dd2f0e8b11d0f600bfb0c093\", "
                 "opaque=\"5ccc069c403ebaf9f0171e9517f40e41\"",
		.challenges = (struct http_auth_challenge_test []) {
			{ .scheme = "Digest",
				.data = NULL,
				.params = (struct http_auth_param []) { 
					{ "realm", "testrealm@host.com" },
					{ "qop", "auth,auth-int" },
					{ "nonce", "dcd98b7102dd2f0e8b11d0f600bfb0c093" },
					{ "opaque", "5ccc069c403ebaf9f0171e9517f40e41" },
					{ NULL, NULL }
				}
			},{
				.scheme = NULL
			}
		}
	},{
		.challenges_in = "Newauth realm=\"apps\", type=1, "
                     "title=\"Login to \\\"apps\\\"\", Basic realm=\"simple\"",
		.challenges = (struct http_auth_challenge_test []) {
			{ .scheme = "Newauth",
				.data = NULL,
				.params = (struct http_auth_param []) { 
					{ "realm", "apps" },
					{ "type", "1" },
					{ "title", "Login to \"apps\"" },
					{ NULL, NULL }
				}
			},{
				.scheme = "Basic",
				.data = NULL,
				.params = (struct http_auth_param []) { 
					{ "realm", "simple" },
					{ NULL, NULL }
				}
			},{
				.scheme = NULL
			}
		}
	}
};

static const unsigned int valid_auth_challenges_test_count =
	N_ELEMENTS(valid_auth_challenges_tests);

static void test_http_auth_challenges_valid(void)
{
	unsigned int i;

	for (i = 0; i < valid_auth_challenges_test_count; i++) T_BEGIN {
		const char *challenges_in;
		ARRAY_TYPE(http_auth_challenge) out;
		const struct http_auth_challenges_test *test;
		bool result;

		test = &valid_auth_challenges_tests[i];
		challenges_in = test->challenges_in;

		test_begin(t_strdup_printf("http auth challenges valid [%d]", i));

		i_zero(&out);
		result = (http_auth_parse_challenges
			((const unsigned char *)challenges_in, strlen(challenges_in), 
				&out) > 0);
		test_out(t_strdup_printf("parse `%s'", challenges_in), result);
		if (result) {
			const struct http_auth_challenge *chalo;
			const struct http_auth_challenge_test *chalt;
			unsigned int index;

			index = 0;
			chalt = test->challenges;
			array_foreach(&out, chalo) {
				const struct http_auth_param *paramo, *paramt;
				unsigned int pindex;

				if (chalt != NULL && chalt->scheme != NULL) {
					i_assert(chalo->scheme != NULL);
					test_out(t_strdup_printf("[%d]->scheme = %s",
							index, str_sanitize(chalo->scheme, 80)),
							strcmp(chalo->scheme, chalt->scheme) == 0);
					if (chalo->data == NULL || chalt->data == NULL) {
						test_out(t_strdup_printf("[%d]->data = %s",
							index, str_sanitize(chalo->data, 80)),
							chalo->data == chalt->data);
					} else {
						test_out(t_strdup_printf("[%d]->data = %s",
							index, str_sanitize(chalo->data, 80)),
							strcmp(chalo->data, chalt->data) == 0);
					}
					paramt = chalt->params;
					pindex = 0;
					array_foreach(&chalo->params, paramo) {
						if (paramt->name == NULL) {
							test_out(t_strdup_printf("[%d]->params[%d]: %s = %s",
								index, pindex, str_sanitize(paramo->name, 80),
								str_sanitize(paramo->value, 80)), FALSE);
							break;
						} else {
							test_out(t_strdup_printf("[%d]->params[%d]: %s = %s",
								index, pindex, str_sanitize(paramo->name, 80),
								str_sanitize(paramo->value, 80)),
								strcmp(paramo->name, paramt->name) == 0 &&
								strcmp(paramo->value, paramt->value) == 0);
							paramt++;
						}
						pindex++;
					}
					chalt++;
				}
				index++;
			}
		}

		test_end();
	} T_END;
}

struct http_auth_credentials_test {
	const char *credentials_in;

	const char *scheme;
	const char *data;
	struct http_auth_param *params;
};

/* Valid auth credentials tests */
static const struct http_auth_credentials_test
valid_auth_credentials_tests[] = {
	{ 
		.credentials_in = "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==",
		.scheme = "Basic",
		.data = "QWxhZGRpbjpvcGVuIHNlc2FtZQ==",
		.params = NULL
	},{
		.credentials_in = "Digest username=\"Mufasa\", "
                 "realm=\"testrealm@host.com\", "
                 "nonce=\"dcd98b7102dd2f0e8b11d0f600bfb0c093\", "
                 "uri=\"/dir/index.html\", "
                 "qop=auth, "
                 "nc=00000001, "
                 "cnonce=\"0a4f113b\", "
                 "response=\"6629fae49393a05397450978507c4ef1\", "
                 "opaque=\"5ccc069c403ebaf9f0171e9517f40e41\"",
		.scheme = "Digest",
		.data = NULL,
		.params = (struct http_auth_param []) {
			{ "username", "Mufasa" },
			{ "realm", "testrealm@host.com" },
			{ "nonce", "dcd98b7102dd2f0e8b11d0f600bfb0c093" },
			{ "uri", "/dir/index.html" },
			{ "qop", "auth" },
			{ "nc", "00000001" },
			{ "cnonce", "0a4f113b" },
			{ "response", "6629fae49393a05397450978507c4ef1" },
			{ "opaque", "5ccc069c403ebaf9f0171e9517f40e41" },
			{ NULL, NULL }
		}
	}
};

static const unsigned int valid_auth_credentials_test_count =
	N_ELEMENTS(valid_auth_credentials_tests);

static void test_http_auth_credentials_valid(void)
{
	unsigned int i;

	for (i = 0; i < valid_auth_credentials_test_count; i++) T_BEGIN {
		const char *credentials_in;
		struct http_auth_credentials out;
		const struct http_auth_credentials_test *test;
		bool result;

		test = &valid_auth_credentials_tests[i];
		credentials_in = test->credentials_in;

		test_begin(t_strdup_printf("http auth credentials valid [%d]", i));

		result = (http_auth_parse_credentials
			((const unsigned char *)credentials_in, strlen(credentials_in), 
				&out) > 0);
		test_out(t_strdup_printf("parse `%s'", credentials_in), result);
		if (result) {
			const struct http_auth_param *paramo, *paramt;
			unsigned int index;

			i_assert(out.scheme != NULL);
			test_out(t_strdup_printf("->scheme = %s",
					str_sanitize(out.scheme, 80)),
					strcmp(out.scheme, test->scheme) == 0);
			if (out.data == NULL || test->data == NULL) {
				test_out(t_strdup_printf("->data = %s",
					str_sanitize(out.data, 80)),
					out.data == test->data);
			} else {
				test_out(t_strdup_printf("->data = %s",
					str_sanitize(out.data, 80)),
					strcmp(out.data, test->data) == 0);
			}
			paramt = test->params;
			index = 0;
			if (array_is_created(&out.params)) {
				array_foreach(&out.params, paramo) {
					if (paramt == NULL || paramt->name == NULL) {
						test_out(t_strdup_printf("->params[%d]: %s = %s",
							index++, str_sanitize(paramo->name, 80),
							str_sanitize(paramo->value, 80)), FALSE);
						break;
					} else {
						test_out(t_strdup_printf("->params[%d]: %s = %s",
							index++, str_sanitize(paramo->name, 80),
							str_sanitize(paramo->value, 80)),
							strcmp(paramo->name, paramt->name) == 0 &&
							strcmp(paramo->value, paramt->value) == 0);
						paramt++;
					}
				}
			}
		}

		test_end();
	} T_END;
}


int main(void)
{
	static void (*const test_functions[])(void) = {
		test_http_auth_challenges_valid,
		test_http_auth_credentials_valid,
		NULL
	};
	return test_run(test_functions);
}