]> git.ozlabs.org Git - ccan/commitdiff
rfc822: Allow test infrastructure to handle headers with minor errors
authorDavid Gibson <david@gibson.dropbear.id.au>
Thu, 4 Oct 2012 14:35:11 +0000 (00:35 +1000)
committerDavid Gibson <david@gibson.dropbear.id.au>
Fri, 5 Oct 2012 03:22:01 +0000 (13:22 +1000)
Currently the test infrastructure for constructing example messages then
parsing them assumes that constructed headers never have errors of any
kind.  That's true so far, but it limits the versatility of this test
apparatus.  This patch extends the infrastructure to allow minor errors
(that is things other than a missing colon) to be handled.  The existing
test data is also extended to include cases which use this.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
ccan/rfc822/test/helper.c
ccan/rfc822/test/helper.h
ccan/rfc822/test/run-hdr-and-body.c
ccan/rfc822/test/testdata.h

index f16803e23a8336ec1c275d33def4d901f3e33bd4..42894ed0288cbee530e38725133b4aa83be760fb 100644 (file)
@@ -57,17 +57,20 @@ void failtest_setup(int argc, char *argv[])
        talloc_set_allocator(malloc_wrapper, free_wrapper, realloc_wrapper);
 }
 
        talloc_set_allocator(malloc_wrapper, free_wrapper, realloc_wrapper);
 }
 
-void check_header(struct rfc822_msg *msg, struct rfc822_header *h,
+void check_header(struct rfc822_msg *msg,
+                 struct rfc822_header *h,
                  const char *name, const char *val,
                  const char *name, const char *val,
-                 int crlf)
+                 enum rfc822_header_errors experr, int crlf)
 {
 {
+       enum rfc822_header_errors errs;
        struct bytestring hname, hvalue, hfull;
        size_t namelen = strlen(name);
        size_t valuelen = strlen(val);
        size_t nln = crlf ? 2 : 1;
        size_t fulllen = namelen + valuelen + 1 + nln;
 
        struct bytestring hname, hvalue, hfull;
        size_t namelen = strlen(name);
        size_t valuelen = strlen(val);
        size_t nln = crlf ? 2 : 1;
        size_t fulllen = namelen + valuelen + 1 + nln;
 
-       ok(rfc822_header_errors(msg, h) == 0, "Header valid");
+       errs = rfc822_header_errors(msg, h);
+       ok(errs == experr, "Header errors 0x%x != 0x%x", errs, experr);
        allocation_failure_check();
 
        hname = rfc822_header_raw_name(msg, h);
        allocation_failure_check();
 
        hname = rfc822_header_raw_name(msg, h);
index 2bdb9c43cb05aa6f7ebcb4275436c8438528cb00..f1bd9279d5012ce45bd11e23813f644a9ed7de8d 100644 (file)
@@ -6,4 +6,4 @@ void allocation_failure_check(void);
 #define CHECK_HEADER_NUMTESTS  4
 void check_header(struct rfc822_msg *msg, struct rfc822_header *h,
                  const char *name, const char *val,
 #define CHECK_HEADER_NUMTESTS  4
 void check_header(struct rfc822_msg *msg, struct rfc822_header *h,
                  const char *name, const char *val,
-                 int crlf);
+                 enum rfc822_header_errors experr, int crlf);
index 956a6bd6e17392e264029556558fd9d5e6166dbf..25e66cc2fd1330b2f31ccc3e51cdfc4778cd0f63 100644 (file)
@@ -23,7 +23,8 @@
                        if (!(_h)) \
                                break; \
                        check_header((_msg), (_h), (_e)->hdrs[_i].name, \
                        if (!(_h)) \
                                break; \
                        check_header((_msg), (_h), (_e)->hdrs[_i].name, \
-                                    (_e)->hdrs[_i].val, crlf); \
+                                    (_e)->hdrs[_i].val,                \
+                                    (_e)->hdrs[_i].errors, crlf);      \
                } \
        } while (0)
 
                } \
        } while (0)
 
index 4ef07095474cef88fa940a466f2798e4f7ec79cd..421b74875a1c37378c95a003dbf44e73a55826d8 100644 (file)
@@ -7,6 +7,7 @@
 
 struct testhdr {
        const char *name, *val;
 
 struct testhdr {
        const char *name, *val;
+       enum rfc822_header_errors errors;
 };
 
 struct aexample {
 };
 
 struct aexample {
@@ -57,11 +58,23 @@ AEXAMPLE(test_msg_nlnl_mixed);
 const char test_msg_space_body_body[] = " Message with LWS at start of body\n";
 AEXAMPLE(test_msg_space_body);
 
 const char test_msg_space_body_body[] = " Message with LWS at start of body\n";
 AEXAMPLE(test_msg_space_body);
 
+struct testhdr bad_hdrs_hdrs[] = {
+       {"From", "Mister From <from@example.com>"},
+       {"To", "Mizz To <to@example.org>"},
+       {"X-Bad-\bName", "This header field has bad characters in the name",
+                .errors = RFC822_HDR_BAD_NAME_CHARS},
+       {"Subject", "Some subject"},
+       {"Message-ID", "<20110221131559.GA28327@example>"},
+};
+#define bad_hdrs_body test_msg_1_body
+AEXAMPLE(bad_hdrs)
+
 #define for_each_aexample(_e)                               \
        foreach_ptr((_e), &test_msg_1, &test_msg_empty_body, \
                    &test_msg_nlnl_lf, &test_msg_nlnl_crlf, \
                    &test_msg_nlnl_mixed, \
 #define for_each_aexample(_e)                               \
        foreach_ptr((_e), &test_msg_1, &test_msg_empty_body, \
                    &test_msg_nlnl_lf, &test_msg_nlnl_crlf, \
                    &test_msg_nlnl_mixed, \
-                   &test_msg_space_body)
+                   &test_msg_space_body, \
+                   &bad_hdrs)
 
 #define for_each_aexample_buf(_e, _buf, _len)  \
        for_each_aexample((_e))                 \
 
 #define for_each_aexample_buf(_e, _buf, _len)  \
        for_each_aexample((_e))                 \