]> git.ozlabs.org Git - ccan/blob - ccan/rfc822/test/run-bad-header.c
rfc822: new module.
[ccan] / ccan / rfc822 / test / run-bad-header.c
1 #include <ccan/foreach/foreach.h>
2 #include <ccan/failtest/failtest_override.h>
3 #include <ccan/failtest/failtest.h>
4 #include <ccan/tap/tap.h>
5 #include <stdlib.h>
6 #include <string.h>
7
8 #define CCAN_RFC822_DEBUG
9
10 #include <ccan/rfc822/rfc822.h>
11
12 #include <ccan/rfc822/rfc822.c>
13
14 #include "testdata.h"
15 #include "helper.h"
16
17 #define BAD_HEADER_STR "This is a bad header\n"
18 const char bad_header[] = 
19         "Date: Tue, 22 Feb 2011 00:15:59 +1100\n"
20         BAD_HEADER_STR
21         "From: Mister From <from@example.com>\n"
22         "To: Mizz To <to@example.org>\n"
23         "Subject: Some subject\n"
24         "Message-ID: <20110221131559.GA28327@example>\n";
25
26 static void test_bad_header(const char *buf, size_t len)
27 {
28         struct rfc822_msg *msg;
29         struct rfc822_header *hdr;
30         struct bytestring hfull;
31
32         plan_tests(3);
33
34         msg = rfc822_start(NULL, buf, len);
35
36         allocation_failure_check();
37
38         hdr = rfc822_first_header(msg);
39         allocation_failure_check();
40
41         ok(hdr && (rfc822_header_errors(msg, hdr) == 0), "First header valid");
42         allocation_failure_check();
43
44         hdr = rfc822_next_header(msg, hdr);
45         allocation_failure_check();
46
47         ok(hdr && (rfc822_header_errors(msg, hdr) == RFC822_HDR_NO_COLON),
48            "Second header invalid");
49
50         hfull = rfc822_header_raw_content(msg, hdr);
51         allocation_failure_check();
52
53         ok(bytestring_eq(hfull, BYTESTRING(BAD_HEADER_STR)),
54                 "Invalid header content");
55
56         rfc822_free(msg);
57         allocation_failure_check();
58 }
59
60 int main(int argc, char *argv[])
61 {
62         failtest_setup(argc, argv);
63
64         test_bad_header(bad_header, sizeof(bad_header));
65
66         /* This exits depending on whether all tests passed */
67         failtest_exit(exit_status());
68 }