testdata.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #ifndef RFC822_TESTDATA_H
  2. #define RFC822_TESTDATA_H
  3. #include <ccan/talloc/talloc.h>
  4. #include <ccan/array_size/array_size.h>
  5. #include <ccan/foreach/foreach.h>
  6. struct testhdr {
  7. const char *name, *val;
  8. };
  9. struct aexample {
  10. const char *name;
  11. struct testhdr *hdrs;
  12. size_t nhdrs;
  13. const char *body;
  14. };
  15. #define AEXAMPLE(s) \
  16. struct aexample s = { \
  17. .name = #s, \
  18. .hdrs = s##_hdrs, \
  19. .nhdrs = ARRAY_SIZE(s##_hdrs), \
  20. .body = s##_body, \
  21. };
  22. struct testhdr test_msg_1_hdrs[] = {
  23. {"Date", "Tue, 22 Feb 2011 00:15:59 +1100"},
  24. {"From", "Mister From <from@example.com>"},
  25. {"To", "Mizz To <to@example.org>"},
  26. {"Subject", "Some subject"},
  27. {"Message-ID", "<20110221131559.GA28327@example>"},
  28. {"MIME-Version", "1.0"},
  29. {"Content-Type", "text/plain; charset=us-ascii"},
  30. {"Content-Disposition", "inline"},
  31. };
  32. const char test_msg_1_body[] = "Test message\n";
  33. AEXAMPLE(test_msg_1);
  34. #define test_msg_empty_body_hdrs test_msg_1_hdrs
  35. const char test_msg_empty_body_body[] = "";
  36. AEXAMPLE(test_msg_empty_body);
  37. #define test_msg_nlnl_lf_hdrs test_msg_1_hdrs
  38. const char test_msg_nlnl_lf_body[] = "Message containing \n\n inside body\n";
  39. AEXAMPLE(test_msg_nlnl_lf);
  40. #define test_msg_nlnl_crlf_hdrs test_msg_1_hdrs
  41. const char test_msg_nlnl_crlf_body[] = "Message containing \r\n\r\n inside body\r\n";
  42. AEXAMPLE(test_msg_nlnl_crlf);
  43. #define test_msg_nlnl_mixed_hdrs test_msg_1_hdrs
  44. const char test_msg_nlnl_mixed_body[] = "Message containing both \n\n and \r\n\r\n inside body\n\r\n";
  45. AEXAMPLE(test_msg_nlnl_mixed);
  46. #define test_msg_space_body_hdrs test_msg_1_hdrs
  47. const char test_msg_space_body_body[] = " Message with LWS at start of body\n";
  48. AEXAMPLE(test_msg_space_body);
  49. #define for_each_aexample(_e) \
  50. foreach_ptr((_e), &test_msg_1, &test_msg_empty_body, \
  51. &test_msg_nlnl_lf, &test_msg_nlnl_crlf, \
  52. &test_msg_nlnl_mixed, \
  53. &test_msg_space_body)
  54. #define for_each_aexample_buf(_e, _buf, _len) \
  55. for_each_aexample((_e)) \
  56. if (((_buf) = assemble_msg((_e), &(_len))) != NULL)
  57. static inline int num_aexamples(void)
  58. {
  59. const struct aexample *e;
  60. int n = 0;
  61. for_each_aexample(e)
  62. n++;
  63. return n;
  64. }
  65. static inline int num_aexample_hdrs(void)
  66. {
  67. const struct aexample *e;
  68. int n = 0;
  69. for_each_aexample(e)
  70. n += e->nhdrs;
  71. return n;
  72. }
  73. static inline const char *assemble_msg(const struct aexample *e,
  74. size_t *len, int crlf)
  75. {
  76. const char *nl = crlf ? "\r\n" : "\n";
  77. int nln = crlf ? 2 : 1;
  78. char *msg, *amsg;
  79. size_t n = 0;
  80. int i;
  81. msg = talloc_strdup(NULL, "");
  82. if (!msg)
  83. return NULL;
  84. for (i = 0; i < e->nhdrs; i++) {
  85. amsg = talloc_asprintf_append(msg, "%s:%s%s", e->hdrs[i].name,
  86. e->hdrs[i].val, nl);
  87. if (!amsg) {
  88. talloc_free(msg);
  89. return NULL;
  90. }
  91. msg = amsg;
  92. n += strlen(e->hdrs[i].name) + strlen(e->hdrs[i].val) + 1 + nln;
  93. }
  94. amsg = talloc_asprintf_append(msg, "%s%s", nl, e->body);
  95. if (!amsg) {
  96. talloc_free(msg);
  97. return NULL;
  98. }
  99. msg = amsg;
  100. n += strlen(e->body) + nln;
  101. *len = n;
  102. return msg;
  103. }
  104. #define NLT(crlf) ((crlf) ? "CRLF" : "LF")
  105. #endif /* RFC822_TESTDATA_H */