]> git.ozlabs.org Git - ccan/blob - ccan/rfc822/test/run-hdr-of-name.c
Merge branch 'io'
[ccan] / ccan / rfc822 / test / run-hdr-of-name.c
1 #include <ccan/foreach/foreach.h>
2 #include <ccan/failtest/failtest_override.h>
3 #include <ccan/failtest/failtest.h>
4 #include <stdlib.h>
5 #include <string.h>
6
7 #define CCAN_RFC822_DEBUG
8
9 #include <ccan/rfc822/rfc822.h>
10
11 #include <ccan/rfc822/rfc822.c>
12
13 #include "testdata.h"
14 #include "helper.h"
15
16 static void test_hdrbyname(const struct aexample *e, const char *buf, size_t len,
17                            const char *exname, int crlf)
18 {
19         struct rfc822_msg *msg;
20         struct rfc822_header *h, *hx;
21         int i, j;
22
23         msg = rfc822_start(NULL, buf, len);
24         allocation_failure_check();
25         ok(msg, "opened %s", exname);
26
27         for (i = 0; i < e->nhdrs; i++) {
28                 struct testhdr *eh = &e->hdrs[i];
29
30                 h = rfc822_first_header_of_name(msg, eh->name);
31                 hx = rfc822_next_header_of_name(msg, NULL, eh->name);
32                 ok1(h == hx);
33
34                 for (j = 0; h && j < eh->index; j++)
35                         h = rfc822_next_header_of_name(msg, h, eh->name);
36                 ok(h, "header \"%s\" (#%d) exists", eh->name, eh->index);
37                 if (!h)
38                         break;
39                 check_header(msg, h, eh->name, eh->val, eh->errors, crlf);
40
41                 h = rfc822_next_header_of_name(msg, h, eh->name);
42                 ok1((eh->index != eh->last) ^ !h);
43         }
44
45         h = rfc822_first_header_of_name(msg, NULL);
46         ok(!h, "Never match NULL name");
47
48         rfc822_free(msg);
49         allocation_failure_check();
50 }
51
52 int main(int argc, char *argv[])
53 {
54         struct aexample *e;
55
56         /* This is how many tests you plan to run */
57         plan_tests(6*num_aexamples() + 14*num_aexample_hdrs());
58
59         failtest_setup(argc, argv);
60
61         for_each_aexample(e) {
62                 int crlf;
63
64                 foreach_int(crlf, 0, 1) {
65                         const char *buf;
66                         size_t len;
67                         char exname[256];
68
69                         sprintf(exname, "%s[%s]", e->name, NLT(crlf));
70
71                         buf = assemble_msg(e, &len, crlf);
72                         ok((buf), "assembled %s", exname);
73                         if (!buf)
74                                 continue;
75
76                         test_hdrbyname(e, buf, len, exname, crlf);
77
78                         tal_free(buf);
79                 }
80         }
81
82         /* This exits depending on whether all tests passed */
83         failtest_exit(exit_status());
84 }