1 /*
2    utils tests
3    Copyright (C) 2001-2004, Joe Orton <joe@manyfish.co.uk>
4 
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9 
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 
19 */
20 
21 #include "config.h"
22 
23 #ifdef HAVE_STDLIB_H
24 #include <stdlib.h>
25 #endif
26 #ifdef HAVE_STRING_H
27 #include <string.h>
28 #endif
29 
30 #include "ne_utils.h"
31 #include "ne_md5.h"
32 #include "ne_alloc.h"
33 #include "ne_dates.h"
34 #include "ne_string.h"
35 
36 #include "tests.h"
37 
38 static const struct {
39     const char *status;
40     int major, minor, code;
41     const char *rp;
42 } accept_sl[] = {
43     /* These are really valid. */
44     { "HTTP/1.1 200 OK", 1, 1, 200, "OK" },
45     { "HTTP/1.1000 200 OK", 1, 1000, 200, "OK" },
46     { "HTTP/1000.1000 200 OK", 1000, 1000, 200, "OK" },
47     { "HTTP/00001.1 200 OK", 1, 1, 200, "OK" },
48     { "HTTP/1.00001 200 OK", 1, 1, 200, "OK" },
49     { "HTTP/99.99 999 99999", 99, 99, 999, "99999" },
50     { "HTTP/1.1 100 ", 1, 1, 100, "" },
51 
52     /* these aren't really valid but we should be able to parse them. */
53     { "HTTP/1.1 100", 1, 1, 100, "" },
54     { "HTTP/1.1   200   OK", 1, 1, 200, "OK" },
55     { "HTTP/1.1   200 \t  OK", 1, 1, 200, "OK" },
56     { "   HTTP/1.1 200 OK", 1, 1, 200, "OK" },
57     { "Norman is a dog HTTP/1.1 200 OK", 1, 1, 200, "OK" },
58     { NULL }
59 };
60 
61 static const char *bad_sl[] = {
62     "",
63     "HTTP/1.1 1000 OK",
64     "HTTP/1.1 1000",
65     "HTTP/-1.1 100 OK",
66     "HTTP/1.1 -100 OK",
67     "HTTP/ 200 OK",
68     "HTTP/",
69     "HTTP/1.1A 100 OK",
70     "HTTP/1.",
71     "HTTP/1.1 1",
72     "Fish/1.1 100 OK",
73     "HTTP/1.1 10",
74     "HTTP",
75     "H\0TP/1.1 100 OK",
76     NULL
77 };
78 
status_lines(void)79 static int status_lines(void)
80 {
81     ne_status s;
82     int n;
83 
84     for (n = 0; accept_sl[n].status != NULL; n++) {
85 	ONV(ne_parse_statusline(accept_sl[n].status, &s),
86 	    ("valid #%d: parse", n));
87 	ONV(accept_sl[n].major != s.major_version, ("valid #%d: major", n));
88 	ONV(accept_sl[n].minor != s.minor_version, ("valid #%d: minor", n));
89 	ONV(accept_sl[n].code != s.code, ("valid #%d: code", n));
90 	ONV(strcmp(accept_sl[n].rp, s.reason_phrase),
91 	    ("valid #%d: reason phrase", n));
92         ne_free(s.reason_phrase);
93     }
94 
95     for (n = 0; bad_sl[n] != NULL; n++) {
96 	ONV(ne_parse_statusline(bad_sl[n], &s) == 0,
97 	    ("invalid #%d", n));
98     }
99 
100     return OK;
101 }
102 
103 /* Write MD5 of 'len' bytes of 'str' to 'digest' */
digest_md5(const char * data,size_t len,unsigned char digest[16])104 static unsigned char *digest_md5(const char *data, size_t len, unsigned char digest[16])
105 {
106     struct ne_md5_ctx ctx;
107 
108 #define CHUNK 100
109     ne_md5_init_ctx(&ctx);
110     /* exercise the buffering interface */
111     while (len > CHUNK) {
112         ne_md5_process_bytes(data, CHUNK, &ctx);
113         len -= CHUNK;
114         data += CHUNK;
115     }
116     ne_md5_process_bytes(data, len, &ctx);
117     ne_md5_finish_ctx(&ctx, digest);
118 
119     return digest;
120 }
121 
md5(void)122 static int md5(void)
123 {
124     unsigned char buf[17] = {0}, buf2[17] = {0};
125     char ascii[33] = {0};
126     char zzzs[500];
127 
128     ne_md5_to_ascii(digest_md5("", 0, buf), ascii);
129     ONN("MD5(null)", strcmp(ascii, "d41d8cd98f00b204e9800998ecf8427e"));
130 
131     ne_md5_to_ascii(digest_md5("foobar", 7, buf), ascii);
132     ONN("MD5(foobar)", strcmp(ascii, "b4258860eea29e875e2ee4019763b2bb"));
133 
134     /* $ perl -e 'printf "z"x500' | md5sum
135      * 8b9323bd72250ea7f1b2b3fb5046391a  - */
136     memset(zzzs, 'z', sizeof zzzs);
137     ne_md5_to_ascii(digest_md5(zzzs, sizeof zzzs, buf), ascii);
138     ONN("MD5(\"z\"x512)", strcmp(ascii, "8b9323bd72250ea7f1b2b3fb5046391a"));
139 
140     ne_ascii_to_md5(ascii, buf2);
141     ON(memcmp(buf, buf2, 16));
142 
143     return OK;
144 }
145 
md5_alignment(void)146 static int md5_alignment(void)
147 {
148     char *bb = ne_malloc(66);
149     struct ne_md5_ctx ctx;
150 
151     /* regression test for a bug in md5.c in <0.15.0 on SPARC, where
152      * the process_bytes function would SIGBUS if the buffer argument
153      * isn't 32-bit aligned. Won't trigger on x86 though. */
154     ne_md5_init_ctx(&ctx);
155     ne_md5_process_bytes(bb + 1, 65, &ctx);
156     ne_free(bb);
157 
158     return OK;
159 }
160 
161 static const struct {
162     const char *str;
163     time_t time;
164     enum { d_rfc1123, d_iso8601, d_rfc1036 } type;
165 } good_dates[] = {
166     { "Fri, 08 Jun 2001 22:59:46 GMT", 992041186, d_rfc1123 },
167     { "Friday, 08-Jun-01 22:59:46 GMT", 992041186, d_rfc1036 },
168     { "Wednesday, 06-Jun-01 22:59:46 GMT", 991868386, d_rfc1036 },
169     /* some different types of ISO8601 dates. */
170     { "2001-06-08T22:59:46Z", 992041186, d_iso8601 },
171     { "2001-06-08T22:59:46.9Z", 992041186, d_iso8601 },
172     { "2001-06-08T26:00:46+03:01", 992041186, d_iso8601 },
173     { "2001-06-08T20:58:46-02:01", 992041186, d_iso8601 },
174     { NULL }
175 };
176 
parse_dates(void)177 static int parse_dates(void)
178 {
179     int n;
180 
181     for (n = 0; good_dates[n].str != NULL; n++) {
182 	time_t res;
183 	const char *str = good_dates[n].str;
184 
185 	switch (good_dates[n].type) {
186 	case d_rfc1036: res = ne_rfc1036_parse(str); break;
187 	case d_iso8601: res = ne_iso8601_parse(str); break;
188 	case d_rfc1123: res = ne_rfc1123_parse(str); break;
189 	default: res = -1; break;
190 	}
191 
192 	ONV(res == -1, ("date %d parse", n));
193 
194 #define FT "%" NE_FMT_TIME_T
195 	ONV(res != good_dates[n].time, (
196 	    "date %d incorrect (" FT " not " FT ")", n,
197 	    res, good_dates[n].time));
198     }
199 
200     return OK;
201 }
202 
203 /* trigger segfaults in ne_rfc1036_parse() in <=0.24.5. */
regress_dates(void)204 static int regress_dates(void)
205 {
206     static const char *dates[] = {
207         "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
208     };
209     size_t n;
210 
211     for (n = 0; n < sizeof(dates)/sizeof(dates[0]); n++) {
212         ne_rfc1036_parse(dates[n]);
213         ne_iso8601_parse(dates[n]);
214         ne_rfc1123_parse(dates[n]);
215     }
216 
217     return OK;
218 }
219 
versioning(void)220 static int versioning(void)
221 {
222 #define GOOD(n,m,msg) ONV(ne_version_match(n,m), \
223 ("match of " msg " failed (%d.%d)", n, m))
224 #define BAD(n,m,msg) ONV(ne_version_match(n,m) == 0, \
225 ("match of " msg " succeeded (%d.%d)", n, m))
226     GOOD(NEON_VERSION_MAJOR, NEON_VERSION_MINOR, "current version");
227     BAD(NEON_VERSION_MAJOR, NEON_VERSION_MINOR + 1, "later minor");
228     BAD(NEON_VERSION_MAJOR + 1, 0, "later major");
229 #if NEON_VERSION_MINOR > 0
230     GOOD(NEON_VERSION_MAJOR, NEON_VERSION_MINOR - 1, "earlier minor");
231 #endif
232 #if NEON_VERSION_MAJOR > 0
233     BAD(NEON_VERSION_MAJOR - 1, 0, "earlier major");
234 #endif
235 #undef GOOD
236 #undef BAD
237     return OK;
238 }
239 
240 /* basic ne_version_string() sanity tests */
version_string(void)241 static int version_string(void)
242 {
243     char buf[1024];
244 
245     ne_snprintf(buf, sizeof buf, "%s", ne_version_string());
246 
247     NE_DEBUG(NE_DBG_HTTP, "Version string: %s\n", buf);
248 
249     ONN("version string too long", strlen(buf) > 200);
250     ONN("version string contained newline", strchr(buf, '\n') != NULL);
251 
252     return OK;
253 }
254 
support(void)255 static int support(void)
256 {
257 #ifdef NEON_SSL
258     ONN("SSL support not advertised", !ne_supports_ssl());
259 #else
260     ONN("SSL support advertised", ne_supports_ssl());
261 #endif
262     return OK;
263 }
264 
265 ne_test tests[] = {
266     T(status_lines),
267     T(md5),
268     T(md5_alignment),
269     T(parse_dates),
270     T(regress_dates),
271     T(versioning),
272     T(version_string),
273     T(support),
274     T(NULL)
275 };
276