From: David Gibson Date: Thu, 4 Oct 2012 14:17:10 +0000 (+1000) Subject: rfc822: Rename RFC822_HDR_BAD_NAME constant X-Git-Url: http://git.ozlabs.org/?a=commitdiff_plain;ds=sidebyside;h=4be05500e605ccc0a68f1d3704c12ffd03cf2dfb;p=ccan rfc822: Rename RFC822_HDR_BAD_NAME constant This error constant is actually more specific than the name suggests - it indicates that a header field name contains characters which are not permitted in a header field name. Rename the constant to better reflect this, likewise rename the testcase based around it. Signed-off-by: David Gibson --- diff --git a/ccan/rfc822/rfc822.c b/ccan/rfc822/rfc822.c index 4a5e4ccd..6a0f95fc 100644 --- a/ccan/rfc822/rfc822.c +++ b/ccan/rfc822/rfc822.c @@ -257,7 +257,7 @@ enum rfc822_header_errors rfc822_header_errors(struct rfc822_msg *msg, assert(c != ':'); if ((c < 33) || (c > 126)) { - err |= RFC822_HDR_BAD_NAME; + err |= RFC822_HDR_BAD_NAME_CHARS; break; } } diff --git a/ccan/rfc822/rfc822.h b/ccan/rfc822/rfc822.h index 2beae410..1170c113 100644 --- a/ccan/rfc822/rfc822.h +++ b/ccan/rfc822/rfc822.h @@ -117,7 +117,7 @@ struct bytestring rfc822_body(struct rfc822_msg *msg); enum rfc822_header_errors { RFC822_HDR_NO_COLON = 1, - RFC822_HDR_BAD_NAME = 2, + RFC822_HDR_BAD_NAME_CHARS = 2, }; enum rfc822_header_errors rfc822_header_errors(struct rfc822_msg *msg, diff --git a/ccan/rfc822/test/run-bad-header-name-chars.c b/ccan/rfc822/test/run-bad-header-name-chars.c new file mode 100644 index 00000000..532f3078 --- /dev/null +++ b/ccan/rfc822/test/run-bad-header-name-chars.c @@ -0,0 +1,95 @@ +#include +#include +#include +#include +#include +#include + +#define CCAN_RFC822_DEBUG + +#include + +#include + +#include "testdata.h" +#include "helper.h" + +#define NAME_TEMPLATE "X-Bad-Header" +#define NAME_TEMPLATE_LEN (strlen(NAME_TEMPLATE)) + +const char bad_name_template[] = + NAME_TEMPLATE ": This is a good header that will become bad\n"; + +static bool bad_name(const char *buf, char c) +{ + struct rfc822_msg *msg; + struct rfc822_header *hdr; + enum rfc822_header_errors errs; + struct bytestring hname; + + msg = rfc822_start(NULL, buf, strlen(buf)); + + allocation_failure_check(); + + hdr = rfc822_first_header(msg); + ok1(hdr); + + allocation_failure_check(); + + errs = rfc822_header_errors(msg, hdr); + + allocation_failure_check(); + + ok1(!(errs & ~RFC822_HDR_BAD_NAME_CHARS)); + + /* Check raw_name still works properly */ + hname = rfc822_header_raw_name(msg, hdr); + + allocation_failure_check(); + + ok1(hname.ptr && hname.len == NAME_TEMPLATE_LEN); + ok1((hname.ptr[0] == c) && + (memcmp(hname.ptr + 1, NAME_TEMPLATE + 1, NAME_TEMPLATE_LEN - 1) == 0)); + + rfc822_free(msg); + + allocation_failure_check(); + + return !!(errs & RFC822_HDR_BAD_NAME_CHARS); +} + +int main(int argc, char *argv[]) +{ + char c; + + failtest_setup(argc, argv); + + plan_tests(5 * (1 + 3 + 4)); + + ok1(!bad_name(bad_name_template, bad_name_template[0])); + + /* Good characters */ + foreach_int(c, 'a', 'Z', '3') { + char *tmp = strdup(bad_name_template); + + tmp[0] = c; + + ok1(!bad_name(tmp, c)); + + free(tmp); + } + + /* Bad characters */ + foreach_int(c, ' ', '\t', '\b', '\x80') { + char *tmp = strdup(bad_name_template); + + tmp[0] = c; + + ok1(bad_name(tmp, c)); + + free(tmp); + } + + /* This exits depending on whether all tests passed */ + failtest_exit(exit_status()); +} diff --git a/ccan/rfc822/test/run-bad-header-name.c b/ccan/rfc822/test/run-bad-header-name.c deleted file mode 100644 index 7b4eac19..00000000 --- a/ccan/rfc822/test/run-bad-header-name.c +++ /dev/null @@ -1,95 +0,0 @@ -#include -#include -#include -#include -#include -#include - -#define CCAN_RFC822_DEBUG - -#include - -#include - -#include "testdata.h" -#include "helper.h" - -#define NAME_TEMPLATE "X-Bad-Header" -#define NAME_TEMPLATE_LEN (strlen(NAME_TEMPLATE)) - -const char bad_name_template[] = - NAME_TEMPLATE ": This is a good header that will become bad\n"; - -static bool bad_name(const char *buf, char c) -{ - struct rfc822_msg *msg; - struct rfc822_header *hdr; - enum rfc822_header_errors errs; - struct bytestring hname; - - msg = rfc822_start(NULL, buf, strlen(buf)); - - allocation_failure_check(); - - hdr = rfc822_first_header(msg); - ok1(hdr); - - allocation_failure_check(); - - errs = rfc822_header_errors(msg, hdr); - - allocation_failure_check(); - - ok1(!(errs & ~RFC822_HDR_BAD_NAME)); - - /* Check raw_name still works properly */ - hname = rfc822_header_raw_name(msg, hdr); - - allocation_failure_check(); - - ok1(hname.ptr && hname.len == NAME_TEMPLATE_LEN); - ok1((hname.ptr[0] == c) && - (memcmp(hname.ptr + 1, NAME_TEMPLATE + 1, NAME_TEMPLATE_LEN - 1) == 0)); - - rfc822_free(msg); - - allocation_failure_check(); - - return !!(errs & RFC822_HDR_BAD_NAME); -} - -int main(int argc, char *argv[]) -{ - char c; - - failtest_setup(argc, argv); - - plan_tests(5 * (1 + 3 + 4)); - - ok1(!bad_name(bad_name_template, bad_name_template[0])); - - /* Good characters */ - foreach_int(c, 'a', 'Z', '3') { - char *tmp = strdup(bad_name_template); - - tmp[0] = c; - - ok1(!bad_name(tmp, c)); - - free(tmp); - } - - /* Bad characters */ - foreach_int(c, ' ', '\t', '\b', '\x80') { - char *tmp = strdup(bad_name_template); - - tmp[0] = c; - - ok1(bad_name(tmp, c)); - - free(tmp); - } - - /* This exits depending on whether all tests passed */ - failtest_exit(exit_status()); -}