1 /* $Id: wire-t.c 6084 2002-12-27 07:24:55Z rra $ */
9 #include "inn/messages.h"
14 /* Read in a file and return the contents in newly allocated memory. Fills in
15 the provided stat buffer. */
17 read_file(const char *name, struct stat *st)
23 if (stat(name, st) < 0)
24 sysdie("cannot stat %s", name);
25 article = xmalloc(st->st_size);
26 fd = open(name, O_RDONLY);
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);
37 /* Test article for wire_findbody. */
38 const char ta[] = "Path: \r\nFrom: \r\n\r\n";
49 end = ta + sizeof(ta) - 1;
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);
62 if (access("articles/strange", F_OK) < 0)
63 if (access("lib/articles/strange", F_OK) == 0)
65 article = read_file("articles/strange", &st);
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);
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");
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");
101 article = read_file("articles/no-body", &st);
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);
112 article = read_file("articles/truncated", &st);
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);