diff options
Diffstat (limited to 'src/daemon/daemontest.c')
-rw-r--r-- | src/daemon/daemontest.c | 148 |
1 files changed, 145 insertions, 3 deletions
diff --git a/src/daemon/daemontest.c b/src/daemon/daemontest.c index 1b36e64f..5ebe1e24 100644 --- a/src/daemon/daemontest.c +++ b/src/daemon/daemontest.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of libmicrohttpd | 2 | This file is part of libmicrohttpd |
3 | (C) 2007 YOUR NAME HERE | 3 | (C) 2007 Christian Grothoff |
4 | 4 | ||
5 | libmicrohttpd is free software; you can redistribute it and/or modify | 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 | 6 | it under the terms of the GNU General Public License as published |
@@ -21,14 +21,156 @@ | |||
21 | /** | 21 | /** |
22 | * @file daemontest.c | 22 | * @file daemontest.c |
23 | * @brief Testcase for libmicrohttpd | 23 | * @brief Testcase for libmicrohttpd |
24 | * @author FIXME | 24 | * @author Christian Grothoff |
25 | */ | 25 | */ |
26 | 26 | ||
27 | #include "config.h" | 27 | #include "config.h" |
28 | #include "curl/curl.h" | ||
28 | #include "microhttpd.h" | 29 | #include "microhttpd.h" |
30 | #include <stdlib.h> | ||
31 | #include <unistd.h> | ||
32 | #include <string.h> | ||
29 | 33 | ||
34 | static int testStartError() { | ||
35 | return NULL != MHD_start_daemon(0, 0, NULL, NULL, NULL, NULL); | ||
36 | } | ||
37 | |||
38 | static int apc_nothing(void * cls, | ||
39 | const struct sockaddr * addr, | ||
40 | socklen_t addrlen) { | ||
41 | return MHD_NO; | ||
42 | } | ||
43 | |||
44 | static int apc_all(void * cls, | ||
45 | const struct sockaddr * addr, | ||
46 | socklen_t addrlen) { | ||
47 | return MHD_YES; | ||
48 | } | ||
49 | |||
50 | static int ahc_nothing(void * cls, | ||
51 | struct MHD_Session * session, | ||
52 | const char * url, | ||
53 | const char * method) { | ||
54 | return MHD_NO; | ||
55 | } | ||
56 | |||
57 | struct CBC { | ||
58 | char * buf; | ||
59 | size_t pos; | ||
60 | size_t size; | ||
61 | }; | ||
62 | |||
63 | static size_t copyBuffer(void * ptr, | ||
64 | size_t size, | ||
65 | size_t nmemb, | ||
66 | void * ctx) { | ||
67 | struct CBC * cbc = ctx; | ||
68 | |||
69 | if (cbc->pos + size * nmemb > cbc->size) | ||
70 | return 0; /* overflow */ | ||
71 | memcpy(&cbc->buf[cbc->pos], | ||
72 | ptr, | ||
73 | size * nmemb); | ||
74 | cbc->pos += size * nmemb; | ||
75 | return size * nmemb; | ||
76 | } | ||
77 | |||
78 | static int ahc_echo(void * cls, | ||
79 | struct MHD_Session * session, | ||
80 | const char * url, | ||
81 | const char * method) { | ||
82 | const char * me = cls; | ||
83 | struct MHD_Response * response; | ||
84 | int ret; | ||
85 | |||
86 | if (0 != strcmp(me, method)) | ||
87 | return MHD_NO; /* unexpected method */ | ||
88 | response = MHD_create_response_from_data(strlen(url), | ||
89 | (void*) url, | ||
90 | MHD_NO, | ||
91 | MHD_YES); | ||
92 | ret = MHD_queue_response(session, | ||
93 | MHD_HTTP_OK, | ||
94 | response); | ||
95 | MHD_destroy_response(response); | ||
96 | return ret; | ||
97 | } | ||
98 | |||
99 | static int testStartStop() { | ||
100 | struct MHD_Daemon * d; | ||
101 | |||
102 | d = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY | MHD_USE_IPv4, | ||
103 | 1080, | ||
104 | &apc_nothing, | ||
105 | NULL, | ||
106 | &ahc_nothing, | ||
107 | NULL); | ||
108 | if (d == NULL) | ||
109 | return 1; | ||
110 | MHD_stop_daemon(d); | ||
111 | return 0; | ||
112 | } | ||
113 | |||
114 | static int testGet() { | ||
115 | struct MHD_Daemon * d; | ||
116 | CURL * c; | ||
117 | int ret; | ||
118 | char buf[2048]; | ||
119 | struct CBC cbc; | ||
120 | |||
121 | cbc.buf = buf; | ||
122 | cbc.size = 2048; | ||
123 | cbc.pos = 0; | ||
124 | ret = 0; | ||
125 | d = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY | MHD_USE_IPv4, | ||
126 | 1080, | ||
127 | &apc_all, | ||
128 | NULL, | ||
129 | &ahc_echo, | ||
130 | "GET"); | ||
131 | if (d == NULL) | ||
132 | return 1; | ||
133 | c = curl_easy_init(); | ||
134 | curl_easy_setopt(c, | ||
135 | CURLOPT_URL, | ||
136 | "http://localhost:1080/hello_world"); | ||
137 | curl_easy_setopt(c, | ||
138 | CURLOPT_WRITEFUNCTION, | ||
139 | ©Buffer); | ||
140 | curl_easy_setopt(c, | ||
141 | CURLOPT_WRITEDATA, | ||
142 | &cbc); | ||
143 | curl_easy_setopt(c, | ||
144 | CURLOPT_FAILONERROR, | ||
145 | 1); | ||
146 | curl_easy_setopt(c, | ||
147 | CURLOPT_CONNECTTIMEOUT, | ||
148 | 15L); | ||
149 | /* NOTE: use of CONNECTTIMEOUT without also | ||
150 | setting NOSIGNAL results in really weird | ||
151 | crashes on my system! */ | ||
152 | curl_easy_setopt(c, | ||
153 | CURLOPT_NOSIGNAL, | ||
154 | 1); | ||
155 | if (CURLE_OK != curl_easy_perform(c)) | ||
156 | ret = 1; | ||
157 | curl_easy_cleanup(c); | ||
158 | if (cbc.pos != strlen("hello_world")) | ||
159 | ret = 2; | ||
160 | if (0 != strncmp("hello_world", | ||
161 | cbc.buf, | ||
162 | strlen("hello_world"))) | ||
163 | ret = 3; | ||
164 | MHD_stop_daemon(d); | ||
165 | return ret; | ||
166 | } | ||
30 | 167 | ||
31 | int main(int argc, | 168 | int main(int argc, |
32 | char * const * argv) { | 169 | char * const * argv) { |
33 | return 0; /* 0 == pass */ | 170 | unsigned int errorCount = 0; |
171 | |||
172 | errorCount += testStartError(); | ||
173 | errorCount += testStartStop(); | ||
174 | errorCount += testGet(); | ||
175 | return errorCount != 0; /* 0 == pass */ | ||
34 | } | 176 | } |