chiark / gitweb /
rename recentact to lowvol
[inn-innduct.git] / tests / lib / wire-t.c
1 /* $Id: wire-t.c 6084 2002-12-27 07:24:55Z rra $ */
2 /* wire test suite. */
3
4 #include "config.h"
5 #include "clibrary.h"
6 #include <fcntl.h>
7 #include <sys/stat.h>
8
9 #include "inn/messages.h"
10 #include "inn/wire.h"
11 #include "libinn.h"
12 #include "libtest.h"
13
14 /* Read in a file and return the contents in newly allocated memory.  Fills in
15    the provided stat buffer. */
16 static char *
17 read_file(const char *name, struct stat *st)
18 {
19     int fd;
20     char *article;
21     ssize_t count;
22
23     if (stat(name, st) < 0)
24         sysdie("cannot stat %s", name);
25     article = xmalloc(st->st_size);
26     fd = open(name, O_RDONLY);
27     if (fd < 0)
28         sysdie("cannot open %s", name);
29     count = read(fd, article, st->st_size);
30     if (count < st->st_size)
31         die("unable to read all of %s", name);
32     close(fd);
33     return article;
34 }
35
36
37 /* Test article for wire_findbody. */
38 const char ta[] = "Path: \r\nFrom: \r\n\r\n";
39
40 int
41 main(void)
42 {
43     const char *p, *end;
44     char *article;
45     struct stat st;
46
47     puts("34");
48
49     end = ta + sizeof(ta) - 1;
50     p = end - 4;
51     ok(1, wire_findbody(ta, sizeof(ta) - 1) == end);
52     ok(2, wire_findbody(ta, sizeof(ta) - 2) == NULL);
53     ok(3, wire_findbody(ta, sizeof(ta) - 3) == NULL);
54     ok(4, wire_findbody(ta, sizeof(ta) - 4) == NULL);
55     ok(5, wire_findbody(ta, sizeof(ta) - 5) == NULL);
56     ok(6, wire_findbody(p, 4) == end);
57     ok(7, wire_findbody(p, 3) == NULL);
58     ok(8, wire_findbody(p, 2) == NULL);
59     ok(9, wire_findbody(p, 1) == NULL);
60     ok(10, wire_findbody(p, 0) == NULL);
61
62     if (access("articles/strange", F_OK) < 0)
63         if (access("lib/articles/strange", F_OK) == 0)
64             chdir("lib");
65     article = read_file("articles/strange", &st);
66
67     p = wire_findbody(article, st.st_size);
68     ok(11, strncmp(p, "Path: This is", strlen("Path: This is")) == 0);
69     p = wire_nextline(p, article + st.st_size - 1);
70     ok(12, strncmp(p, "Second: Not", strlen("Second: Not")) == 0);
71     p = wire_nextline(p, article + st.st_size - 1);
72     ok(13, p == NULL);
73     p = wire_findheader(article, st.st_size, "Path");
74     ok(14, p == article + 6);
75     p = wire_findheader(article, st.st_size, "From");
76     ok(15, strncmp(p, "This is the real", strlen("This is the real")) == 0);
77     p = wire_findheader(article, st.st_size, "SUMMARY");
78     ok(16, strncmp(p, "First text", strlen("First text")) == 0);
79     p = wire_findheader(article, st.st_size, "Header");
80     ok(17, strncmp(p, "This one is real", strlen("This one is real")) == 0);
81     p = wire_findheader(article, st.st_size, "message-id");
82     ok(18, strncmp(p, "<foo@example.com>", strlen("<foo@example.com>")) == 0);
83     p = wire_findheader(article, st.st_size, "Second");
84     ok(19, p == NULL);
85     p = wire_findheader(article, st.st_size, "suBJect");
86     ok(20, strncmp(p, "This is\rnot", strlen("This is\rnot")) == 0);
87     end = wire_endheader(p, article + st.st_size - 1);
88     ok(21, strncmp(end, "\nFrom: This is", strlen("\nFrom: This is")) == 0);
89     p = wire_findheader(article, st.st_size, "keywordS");
90     ok(22, strncmp(p, "this is --", strlen("this is --")) == 0);
91     end = wire_endheader(p, article + st.st_size - 1);
92     ok(23, strncmp(end, "\nSummary: ", strlen("\nSummary: ")) == 0);
93     p = wire_findheader(article, st.st_size, "strange");
94     ok(24, strncmp(p, "This is\n\nnot", strlen("This is\n\nnot")) == 0);
95     end = wire_endheader(p, article + st.st_size - 1);
96     ok(25, strncmp(end, "\nMessage-ID: ", strlen("\nMessage-ID: ")) == 0);
97     p = wire_findheader(article, st.st_size, "Message");
98     ok(26, p == NULL);
99
100     free(article);
101     article = read_file("articles/no-body", &st);
102
103     ok(27, wire_findbody(article, st.st_size) == NULL);
104     p = wire_findheader(article, st.st_size, "message-id");
105     ok(28, strncmp(p, "<id1@example.com>\r\n",
106                    strlen("<id1@example.com>\r\n")) == 0);
107     end = wire_endheader(p, article + st.st_size - 1);
108     ok(29, end == article + st.st_size - 1);
109     ok(30, wire_nextline(p, article + st.st_size - 1) == NULL);
110
111     free(article);
112     article = read_file("articles/truncated", &st);
113
114     ok(31, wire_findbody(article, st.st_size) == NULL);
115     p = wire_findheader(article, st.st_size, "date");
116     ok(32, strncmp(p, "Mon, 23 Dec", strlen("Mon, 23 Dec")) == 0);
117     ok(33, wire_endheader(p, article + st.st_size - 1) == NULL);
118     ok(34, wire_nextline(p, article + st.st_size - 1) == NULL);
119
120     free(article);
121
122     return 0;
123 }