summaryrefslogtreecommitdiff
path: root/test/util-tests.c
blob: d6309a9a838c85930d5dad9f1327af115d010836 (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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/* 
   utils tests
   Copyright (C) 2001-2006, Joe Orton <joe@manyfish.co.uk>

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
  
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
  
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#include "config.h"

#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif

#include "ne_utils.h"
#include "ne_md5.h"
#include "ne_alloc.h"
#include "ne_dates.h"
#include "ne_string.h"

#include "tests.h"

static const struct {
    const char *status;
    int major, minor, code;
    const char *rp;
} accept_sl[] = {
    /* These are really valid. */
    { "HTTP/1.1 200 OK", 1, 1, 200, "OK" },
    { "HTTP/1.1000 200 OK", 1, 1000, 200, "OK" },
    { "HTTP/1000.1000 200 OK", 1000, 1000, 200, "OK" },
    { "HTTP/00001.1 200 OK", 1, 1, 200, "OK" },
    { "HTTP/1.00001 200 OK", 1, 1, 200, "OK" },
    { "HTTP/99.99 999 99999", 99, 99, 999, "99999" },
    { "HTTP/1.1 100 ", 1, 1, 100, "" },

    /* these aren't really valid but we should be able to parse them. */
    { "HTTP/1.1 100", 1, 1, 100, "" },
    { "HTTP/1.1   200   OK", 1, 1, 200, "OK" },
    { "HTTP/1.1   200 \t  OK", 1, 1, 200, "OK" },
    { "   HTTP/1.1 200 OK", 1, 1, 200, "OK" },
    { "Norman is a dog HTTP/1.1 200 OK", 1, 1, 200, "OK" },
    { NULL }
};

static const char *bad_sl[] = {
    "",
    "HTTP/1.1 1000 OK",
    "HTTP/1.1 1000",
    "HTTP/-1.1 100 OK",
    "HTTP/1.1 -100 OK",
    "HTTP/ 200 OK",
    "HTTP/",
    "HTTP/1.1A 100 OK",
    "HTTP/1.",
    "HTTP/1.1 1",
    "Fish/1.1 100 OK",
    "HTTP/1.1 10",
    "HTTP",
    "H\0TP/1.1 100 OK",
    NULL
};  

static int status_lines(void)
{
    ne_status s;
    int n;

    for (n = 0; accept_sl[n].status != NULL; n++) {
	ONV(ne_parse_statusline(accept_sl[n].status, &s),
	    ("valid #%d: parse", n));
	ONV(accept_sl[n].major != s.major_version, ("valid #%d: major", n));
	ONV(accept_sl[n].minor != s.minor_version, ("valid #%d: minor", n));
	ONV(accept_sl[n].code != s.code, ("valid #%d: code", n));
	ONV(strcmp(accept_sl[n].rp, s.reason_phrase), 
	    ("valid #%d: reason phrase", n));
        ne_free(s.reason_phrase);
    }
    
    for (n = 0; bad_sl[n] != NULL; n++) {
	ONV(ne_parse_statusline(bad_sl[n], &s) == 0, 
	    ("invalid #%d", n));
    }

    return OK;
}

/* Write MD5 of 'len' bytes of 'str' to 'digest' */
static const unsigned char *digest_md5(const char *data, size_t len,
                                 unsigned int digest[4])
{
    struct ne_md5_ctx *ctx;

#define CHUNK 100
    ctx = ne_md5_create_ctx();
    if (!ctx) {
        return (unsigned char *)"NO-MD5-SUPPORT";
    }        
    /* exercise the buffering interface */
    while (len > CHUNK) {
        ne_md5_process_bytes(data, CHUNK, ctx);
        len -= CHUNK;
        data += CHUNK;
    }
    ne_md5_process_bytes(data, len, ctx);
    ne_md5_finish_ctx(ctx, digest);
    ne_md5_destroy_ctx(ctx);

    return (unsigned char *)digest;
}

static int md5(void)
{
    unsigned int buf[4], buf2[4] = {0};
    char ascii[33] = {0};
    char zzzs[500];

    ne_md5_to_ascii(digest_md5("", 0, buf), ascii);
    ONN("MD5(null)", strcmp(ascii, "d41d8cd98f00b204e9800998ecf8427e"));
    
    ne_md5_to_ascii(digest_md5("foobar", 7, buf), ascii);
    ONN("MD5(foobar)", strcmp(ascii, "b4258860eea29e875e2ee4019763b2bb"));

    /* $ perl -e 'printf "z"x500' | md5sum
     * 8b9323bd72250ea7f1b2b3fb5046391a  - */
    memset(zzzs, 'z', sizeof zzzs);
    ne_md5_to_ascii(digest_md5(zzzs, sizeof zzzs, buf), ascii);
    ONN("MD5(\"z\"x512)", strcmp(ascii, "8b9323bd72250ea7f1b2b3fb5046391a"));

    ne_ascii_to_md5(ascii, (unsigned char *)buf2);
    ON(memcmp(buf, buf2, 16));
    
    return OK;
}

static int md5_alignment(void)
{
    char *bb = ne_malloc(66);
    struct ne_md5_ctx *ctx;

    /* regression test for a bug in md5.c in <0.15.0 on SPARC, where
     * the process_bytes function would SIGBUS if the buffer argument
     * isn't 32-bit aligned. Won't trigger on x86 though. */
    ctx = ne_md5_create_ctx();
    ONN("could not create MD5 context", ctx == NULL);
    ne_md5_process_bytes(bb + 1, 65, ctx);
    ne_md5_destroy_ctx(ctx);
    ne_free(bb);

    return OK;
}

static const struct {
    const char *str;
    time_t time;
    enum { d_rfc1123, d_iso8601, d_rfc1036 } type;
} good_dates[] = {
    { "Fri, 08 Jun 2001 22:59:46 GMT", 992041186, d_rfc1123 },
    { "Friday, 08-Jun-01 22:59:46 GMT", 992041186, d_rfc1036 },
    { "Wednesday, 06-Jun-01 22:59:46 GMT", 991868386, d_rfc1036 },
    /* some different types of ISO8601 dates. */
    { "2001-06-08T22:59:46Z", 992041186, d_iso8601 },
    { "2001-06-08T22:59:46.9Z", 992041186, d_iso8601 },
    { "2001-06-08T26:00:46+03:01", 992041186, d_iso8601 },
    { "2001-06-08T20:58:46-02:01", 992041186, d_iso8601 },
    { NULL }
};

static int parse_dates(void)
{
    int n;

    for (n = 0; good_dates[n].str != NULL; n++) {
	time_t res;
	const char *str = good_dates[n].str;

	switch (good_dates[n].type) {
	case d_rfc1036: res = ne_rfc1036_parse(str); break;
	case d_iso8601: res = ne_iso8601_parse(str); break;
	case d_rfc1123: res = ne_rfc1123_parse(str); break;
	default: res = -1; break;
	}
	
	ONV(res == -1, ("date %d parse", n));
	
#define FT "%" NE_FMT_TIME_T
	ONV(res != good_dates[n].time, (
	    "date %d incorrect (" FT " not " FT ")", n,
	    res, good_dates[n].time));
    }

    return OK;
}

#define BAD_DATE(format, result) \
    ONN(format " date parse must fail", result != -1)

/* Test for bad dates; trigger segfaults in ne_rfc1036_parse() in
 * <=0.24.5. */
static int bad_dates(void)
{
    static const char *dates[] = {
        "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
        "Friday, 08-Jun-01",
    };
    size_t n;
    
    for (n = 0; n < sizeof(dates)/sizeof(dates[0]); n++) {
        BAD_DATE("rfc1036", ne_rfc1036_parse(dates[n]));
        BAD_DATE("iso8601", ne_iso8601_parse(dates[n]));
        BAD_DATE("rfc1123", ne_rfc1123_parse(dates[n]));
        BAD_DATE("asctime", ne_asctime_parse(dates[n]));
    }

    return OK;
}

#define GOOD(n,m,msg) ONV(ne_version_match(n,m), \
("match of " msg " failed (%d.%d)", n, m))
#define BAD(n,m,msg) ONV(ne_version_match(n,m) == 0, \
("match of " msg " succeeded (%d.%d)", n, m))

static int versioning(void)
{
    GOOD(NE_VERSION_MAJOR, NE_VERSION_MINOR, "current version");
    BAD(NE_VERSION_MAJOR + 1, 0, "later major");
    BAD(NE_VERSION_MAJOR, NE_VERSION_MINOR + 1, "later minor");
#if NE_VERSION_MAJOR > 0
    BAD(NE_VERSION_MAJOR - 1, 0, "earlier major");
#if NE_VERSION_MINOR > 0
    GOOD(NE_VERSION_MAJOR, NE_VERSION_MINOR - 1, "earlier minor");
#endif /* NE_VERSION_MINOR > 0 */
#else /* where NE_VERSION_MAJOR == 0 */
    BAD(0, NE_VERSION_MINOR - 1, "earlier minor for 0.x");
#endif
    return OK;
}

#undef GOOD
#undef BAD

/* basic ne_version_string() sanity tests */
static int version_string(void)
{
    char buf[1024];
    
    ne_snprintf(buf, sizeof buf, "%s", ne_version_string());
    
    NE_DEBUG(NE_DBG_HTTP, "Version string: %s\n", buf);

    ONN("version string too long", strlen(buf) > 200);
    ONN("version string contained newline", strchr(buf, '\n') != NULL);

    return OK;    
}

static int support(void)
{
#ifdef NE_HAVE_SSL
    ONN("SSL support not advertised", !ne_has_support(NE_FEATURE_SSL));
#else
    ONN("SSL support advertised", ne_has_support(NE_FEATURE_SSL));
#endif
#ifdef NE_HAVE_ZLIB
    ONN("zlib support not advertised", !ne_has_support(NE_FEATURE_ZLIB));
#else
    ONN("zlib support advertised", ne_has_support(NE_FEATURE_ZLIB));
#endif
#ifdef NE_HAVE_IPV6
    ONN("IPv6 support not advertised", !ne_has_support(NE_FEATURE_IPV6));
#else
    ONN("IPv6 support advertised", ne_has_support(NE_FEATURE_IPV6));
#endif
#ifdef NE_HAVE_LFS
    ONN("LFS support not advertised", !ne_has_support(NE_FEATURE_LFS));
#else
    ONN("LFS support advertised", ne_has_support(NE_FEATURE_LFS));
#endif
#ifdef NE_HAVE_TS_SSL
    ONN("Thread-safe SSL support not advertised", 
        !ne_has_support(NE_FEATURE_TS_SSL));
#else
    ONN("Thread-safe SSL support advertised", 
        ne_has_support(NE_FEATURE_TS_SSL));
#endif
#ifdef NE_HAVE_I18N
    ONN("i18n support not advertised", 
        !ne_has_support(NE_FEATURE_I18N));
#else
    ONN("i18n SSL support advertised", 
        ne_has_support(NE_FEATURE_I18N));
#endif
    return OK;
}

ne_test tests[] = {
    T(status_lines),
    T(md5),
    T(md5_alignment),
    T(parse_dates),
    T(bad_dates),
    T(versioning),
    T(version_string),
    T(support),
    T(NULL)
};