diff options
author | Christian Grothoff <christian@grothoff.org> | 2012-01-19 16:50:06 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2012-01-19 16:50:06 +0000 |
commit | 8c2c92ce0a5f579e8116b344eda06dc9c168bbe9 (patch) | |
tree | b32daacddf27087323f39c3821bd59e413dad4a9 /src/testcurl/daemontest_digestauth_with_arguments.c | |
parent | 19c33f86184fb2cc8fdcc18fd5693bccade52e64 (diff) | |
download | libmicrohttpd-8c2c92ce0a5f579e8116b344eda06dc9c168bbe9.tar.gz libmicrohttpd-8c2c92ce0a5f579e8116b344eda06dc9c168bbe9.zip |
forgot to add this file earlier
Diffstat (limited to 'src/testcurl/daemontest_digestauth_with_arguments.c')
-rw-r--r-- | src/testcurl/daemontest_digestauth_with_arguments.c | 211 |
1 files changed, 211 insertions, 0 deletions
diff --git a/src/testcurl/daemontest_digestauth_with_arguments.c b/src/testcurl/daemontest_digestauth_with_arguments.c new file mode 100644 index 00000000..70830a26 --- /dev/null +++ b/src/testcurl/daemontest_digestauth_with_arguments.c | |||
@@ -0,0 +1,211 @@ | |||
1 | /* | ||
2 | This file is part of libmicrohttpd | ||
3 | (C) 2010, 2012 Christian Grothoff | ||
4 | |||
5 | libmicrohttpd is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 2, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | libmicrohttpd is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with libmicrohttpd; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file daemontest_digestauth_with_arguments.c | ||
23 | * @brief Testcase for libmicrohttpd Digest Auth with arguments | ||
24 | * @author Amr Ali | ||
25 | */ | ||
26 | |||
27 | #include "MHD_config.h" | ||
28 | #include "platform.h" | ||
29 | #include <curl/curl.h> | ||
30 | #include <microhttpd.h> | ||
31 | #include <stdlib.h> | ||
32 | #include <string.h> | ||
33 | #include <time.h> | ||
34 | |||
35 | #ifndef WINDOWS | ||
36 | #include <sys/socket.h> | ||
37 | #include <unistd.h> | ||
38 | #endif | ||
39 | |||
40 | #define PAGE "<html><head><title>libmicrohttpd demo</title></head><body>Access granted</body></html>" | ||
41 | |||
42 | #define DENIED "<html><head><title>libmicrohttpd demo</title></head><body>Access denied</body></html>" | ||
43 | |||
44 | #define OPAQUE "11733b200778ce33060f31c9af70a870ba96ddd4" | ||
45 | |||
46 | struct CBC | ||
47 | { | ||
48 | char *buf; | ||
49 | size_t pos; | ||
50 | size_t size; | ||
51 | }; | ||
52 | |||
53 | static size_t | ||
54 | copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx) | ||
55 | { | ||
56 | struct CBC *cbc = ctx; | ||
57 | |||
58 | if (cbc->pos + size * nmemb > cbc->size) | ||
59 | return 0; /* overflow */ | ||
60 | memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb); | ||
61 | cbc->pos += size * nmemb; | ||
62 | return size * nmemb; | ||
63 | } | ||
64 | |||
65 | static int | ||
66 | ahc_echo (void *cls, | ||
67 | struct MHD_Connection *connection, | ||
68 | const char *url, | ||
69 | const char *method, | ||
70 | const char *version, | ||
71 | const char *upload_data, size_t *upload_data_size, | ||
72 | void **unused) | ||
73 | { | ||
74 | struct MHD_Response *response; | ||
75 | char *username; | ||
76 | const char *password = "testpass"; | ||
77 | const char *realm = "test@example.com"; | ||
78 | int ret; | ||
79 | |||
80 | username = MHD_digest_auth_get_username(connection); | ||
81 | if ( (username == NULL) || | ||
82 | (0 != strcmp (username, "testuser")) ) | ||
83 | { | ||
84 | response = MHD_create_response_from_buffer(strlen (DENIED), | ||
85 | DENIED, | ||
86 | MHD_RESPMEM_PERSISTENT); | ||
87 | ret = MHD_queue_auth_fail_response(connection, realm, | ||
88 | OPAQUE, | ||
89 | response, | ||
90 | MHD_NO); | ||
91 | MHD_destroy_response(response); | ||
92 | return ret; | ||
93 | } | ||
94 | ret = MHD_digest_auth_check(connection, realm, | ||
95 | username, | ||
96 | password, | ||
97 | 300); | ||
98 | free(username); | ||
99 | if ( (ret == MHD_INVALID_NONCE) || | ||
100 | (ret == MHD_NO) ) | ||
101 | { | ||
102 | response = MHD_create_response_from_buffer(strlen (DENIED), | ||
103 | DENIED, | ||
104 | MHD_RESPMEM_PERSISTENT); | ||
105 | if (NULL == response) | ||
106 | return MHD_NO; | ||
107 | ret = MHD_queue_auth_fail_response(connection, realm, | ||
108 | OPAQUE, | ||
109 | response, | ||
110 | (ret == MHD_INVALID_NONCE) ? MHD_YES : MHD_NO); | ||
111 | MHD_destroy_response(response); | ||
112 | return ret; | ||
113 | } | ||
114 | response = MHD_create_response_from_buffer(strlen(PAGE), PAGE, | ||
115 | MHD_RESPMEM_PERSISTENT); | ||
116 | ret = MHD_queue_response(connection, MHD_HTTP_OK, response); | ||
117 | MHD_destroy_response(response); | ||
118 | return ret; | ||
119 | } | ||
120 | |||
121 | |||
122 | static int | ||
123 | testDigestAuth () | ||
124 | { | ||
125 | int fd; | ||
126 | CURL *c; | ||
127 | CURLcode errornum; | ||
128 | struct MHD_Daemon *d; | ||
129 | struct CBC cbc; | ||
130 | size_t len; | ||
131 | size_t off = 0; | ||
132 | char buf[2048]; | ||
133 | char rnd[8]; | ||
134 | |||
135 | cbc.buf = buf; | ||
136 | cbc.size = 2048; | ||
137 | cbc.pos = 0; | ||
138 | fd = open("/dev/urandom", O_RDONLY); | ||
139 | if (-1 == fd) | ||
140 | { | ||
141 | fprintf(stderr, "Failed to open `%s': %s\n", | ||
142 | "/dev/urandom", | ||
143 | strerror(errno)); | ||
144 | return 1; | ||
145 | } | ||
146 | while (off < 8) | ||
147 | { | ||
148 | len = read(fd, rnd, 8); | ||
149 | if (len == -1) | ||
150 | { | ||
151 | fprintf(stderr, "Failed to read `%s': %s\n", | ||
152 | "/dev/urandom", | ||
153 | strerror(errno)); | ||
154 | (void) close(fd); | ||
155 | return 1; | ||
156 | } | ||
157 | off += len; | ||
158 | } | ||
159 | (void) close(fd); | ||
160 | d = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG, | ||
161 | 1337, NULL, NULL, &ahc_echo, PAGE, | ||
162 | MHD_OPTION_DIGEST_AUTH_RANDOM, sizeof (rnd), rnd, | ||
163 | MHD_OPTION_NONCE_NC_SIZE, 300, | ||
164 | MHD_OPTION_END); | ||
165 | if (d == NULL) | ||
166 | return 1; | ||
167 | c = curl_easy_init (); | ||
168 | curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1:1337/foo?key=value"); | ||
169 | curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, ©Buffer); | ||
170 | curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); | ||
171 | curl_easy_setopt (c, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST); | ||
172 | curl_easy_setopt (c, CURLOPT_USERPWD, "testuser:testpass"); | ||
173 | curl_easy_setopt (c, CURLOPT_FAILONERROR, 1); | ||
174 | curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L); | ||
175 | curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 15L); | ||
176 | curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); | ||
177 | /* NOTE: use of CONNECTTIMEOUT without also | ||
178 | setting NOSIGNAL results in really weird | ||
179 | crashes on my system!*/ | ||
180 | curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1); | ||
181 | if (CURLE_OK != (errornum = curl_easy_perform (c))) | ||
182 | { | ||
183 | fprintf (stderr, | ||
184 | "curl_easy_perform failed: `%s'\n", | ||
185 | curl_easy_strerror (errornum)); | ||
186 | curl_easy_cleanup (c); | ||
187 | MHD_stop_daemon (d); | ||
188 | return 2; | ||
189 | } | ||
190 | curl_easy_cleanup (c); | ||
191 | MHD_stop_daemon (d); | ||
192 | if (cbc.pos != strlen (PAGE)) | ||
193 | return 4; | ||
194 | if (0 != strncmp (PAGE, cbc.buf, strlen (PAGE))) | ||
195 | return 8; | ||
196 | return 0; | ||
197 | } | ||
198 | |||
199 | int | ||
200 | main (int argc, char *const *argv) | ||
201 | { | ||
202 | unsigned int errorCount = 0; | ||
203 | |||
204 | if (0 != curl_global_init (CURL_GLOBAL_WIN32)) | ||
205 | return 2; | ||
206 | errorCount += testDigestAuth (); | ||
207 | if (errorCount != 0) | ||
208 | fprintf (stderr, "Error (code: %u)\n", errorCount); | ||
209 | curl_global_cleanup (); | ||
210 | return errorCount != 0; /* 0 == pass */ | ||
211 | } | ||