rfc822: new module.
[ccan] / ccan / rfc822 / test / run-no-body.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 const char no_body[] = 
18         "Date: Tue, 22 Feb 2011 00:15:59 +1100\n"
19         "From: Mister From <from@example.com>\n"
20         "To: Mizz To <to@example.org>\n"
21         "Subject: Some subject\n"
22         "Message-ID: <20110221131559.GA28327@example>\n";
23
24 const char truncated[] = 
25         "Date: Tue, 22 Feb 2011 00:15:59 +1100\n"
26         "From: Mister From <from@example.com>\n"
27         "To: Mizz To <to@";
28
29 static int test_no_body(const char *buf, size_t len)
30 {
31         struct rfc822_msg *msg;
32         struct bytestring body;
33         int ok = 1;
34
35         msg = rfc822_start(NULL, buf, len);
36         allocation_failure_check();
37
38         body = rfc822_body(msg);
39         allocation_failure_check();
40         if (body.ptr)
41                 ok = 0;
42
43         rfc822_free(msg);
44         allocation_failure_check();
45         return ok;
46 }
47
48 static int test_truncated(const char *buf, size_t len)
49 {
50         struct rfc822_msg *msg;
51         struct rfc822_header *h = NULL;
52         struct bytestring body;
53         int ok = 1;
54
55         msg = rfc822_start(NULL, buf, len);
56         allocation_failure_check();
57
58         do {
59                 h = rfc822_next_header(msg, h);
60                 allocation_failure_check();
61         } while (h);
62
63         body = rfc822_body(msg);
64         allocation_failure_check();
65         if (body.ptr)
66                 ok = 0;
67
68         rfc822_free(msg);
69         allocation_failure_check();
70         return ok;
71 }
72
73 int main(int argc, char *argv[])
74 {
75         failtest_setup(argc, argv);
76
77         /* This is how many tests you plan to run */
78         plan_tests(3);
79
80         ok1(test_no_body(no_body, sizeof(no_body)));
81         ok1(test_no_body(truncated, sizeof(truncated)));
82         ok1(test_truncated(truncated, sizeof(truncated)));
83
84         /* This exits depending on whether all tests passed */
85         failtest_exit(exit_status());
86 }