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
|
#include <ccan/foreach/foreach.h>
#include <ccan/failtest/failtest_override.h>
#include <ccan/failtest/failtest.h>
#include <ccan/tap/tap.h>
#include <stdlib.h>
#include <string.h>
#define CCAN_RFC822_DEBUG
#include <ccan/rfc822/rfc822.h>
#include <ccan/rfc822/rfc822.c>
#include "testdata.h"
#include "helper.h"
#define CHECK_HEADERS(_e, _msg, _h, _n, _crlf) \
do { \
int _i; \
for (_i = 0; _i < (_e)->nhdrs; _i++) { \
(_h) = rfc822_next_header((_msg), (_h)); \
ok((_h), "header %d exists %s", _i, (_n)); \
if (!(_h)) \
break; \
check_header((_msg), (_h), (_e)->hdrs[_i].name, \
(_e)->hdrs[_i].val, \
(_e)->hdrs[_i].errors, crlf); \
} \
} while (0)
static void test_bodyhdr(const struct aexample *e, const char *buf, size_t len,
const char *exname, int crlf)
{
struct rfc822_msg *msg;
struct rfc822_header *h = NULL;
struct bytestring body;
msg = rfc822_start(NULL, buf, len);
allocation_failure_check();
ok(msg, "opened %s", exname);
body = rfc822_body(msg);
allocation_failure_check();
ok(bytestring_eq(body, bytestring_from_string(e->body)),
"body content %s", exname);
CHECK_HEADERS(e, msg, h, exname, crlf);
h = rfc822_next_header(msg, h);
allocation_failure_check();
ok(!h, "Too many headers for %s", exname);
rfc822_free(msg);
allocation_failure_check();
}
static void test_hdrbody(const struct aexample *e, const char *buf, size_t len,
const char *exname, int crlf)
{
struct rfc822_msg *msg;
struct rfc822_header *h = NULL;
struct bytestring body;
msg = rfc822_start(NULL, buf, len);
allocation_failure_check();
ok(msg, "opened %s", exname);
CHECK_HEADERS(e, msg, h, exname, crlf);
h = rfc822_next_header(msg, h);
allocation_failure_check();
ok(!h, "Too many headers for %s", exname);
body = rfc822_body(msg);
allocation_failure_check();
ok(bytestring_eq(body, bytestring_from_string(e->body)),
"body content %s", exname);
rfc822_free(msg);
allocation_failure_check();
}
static void test_hdrhdr(const struct aexample *e, const char *buf, size_t len,
const char *exname, int crlf)
{
struct rfc822_msg *msg;
struct rfc822_header *h;
msg = rfc822_start(NULL, buf, len);
allocation_failure_check();
ok(msg, "opened %s", exname);
h = NULL;
CHECK_HEADERS(e, msg, h, exname, crlf);
h = rfc822_next_header(msg, h);
allocation_failure_check();
ok(!h, "Too many headers for %s", exname);
/* And again, this time it should be cached */
h = NULL;
CHECK_HEADERS(e, msg, h, exname, crlf);
h = rfc822_next_header(msg, h);
allocation_failure_check();
ok(!h, "Too many headers for %s", exname);
rfc822_free(msg);
allocation_failure_check();
}
int main(int argc, char *argv[])
{
struct aexample *e;
/* This is how many tests you plan to run */
plan_tests(20*num_aexamples()
+ (36 + CHECK_HEADER_NUMTESTS)*num_aexample_hdrs());
failtest_setup(argc, argv);
for_each_aexample(e) {
int crlf;
foreach_int(crlf, 0, 1) {
const char *buf;
size_t len;
char exname[256];
sprintf(exname, "%s[%s]", e->name, NLT(crlf));
buf = assemble_msg(e, &len, crlf);
ok((buf), "assembled %s", exname);
if (!buf)
continue;
test_bodyhdr(e, buf, len, exname, crlf);
test_hdrbody(e, buf, len, exname, crlf);
test_hdrhdr(e, buf, len, exname, crlf);
tal_free(buf);
}
}
/* This exits depending on whether all tests passed */
failtest_exit(exit_status());
}
|