util-tests.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. utils tests
  3. Copyright (C) 2001-2006, Joe Orton <[email protected]>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #include "config.h"
  17. #ifdef HAVE_STDLIB_H
  18. #include <stdlib.h>
  19. #endif
  20. #ifdef HAVE_STRING_H
  21. #include <string.h>
  22. #endif
  23. #ifdef HAVE_STDINT_H
  24. #include <stdint.h>
  25. #endif
  26. #include "ne_utils.h"
  27. #include "ne_md5.h"
  28. #include "ne_alloc.h"
  29. #include "ne_dates.h"
  30. #include "ne_string.h"
  31. #include "tests.h"
  32. static const struct {
  33. const char *status;
  34. int major, minor, code;
  35. const char *rp;
  36. } accept_sl[] = {
  37. /* These are really valid. */
  38. { "HTTP/1.1 200 OK", 1, 1, 200, "OK" },
  39. { "HTTP/9.9 599 OK", 9, 9, 599, "OK" },
  40. { "HTTP/1.0 123 OK-is-OK1234", 1, 0, 123, "OK-is-OK1234" },
  41. { "HTTP/1.1 100 Alpha\tBeta", 1, 1, 100, "Alpha Beta" }, /* should be cleaned. */
  42. { "HTTP/1.1 100 Alpha Beta", 1, 1, 100, "Alpha Beta" },
  43. { "HTTP/1.1 100 fØØbÆr", 1, 1, 100, "f b r" }, /* UTF-8 should be cleaned */
  44. /* these aren't really valid but we should be able to parse them. */
  45. { NULL }
  46. };
  47. static const char *const bad_sl[] = {
  48. "",
  49. "HTTP/1.1 1000 OK",
  50. "HTTP/1.1 1000",
  51. "HTTP/-1.1 100 OK",
  52. "HTTP/1.1 -100 OK",
  53. "HTTP/ 200 OK",
  54. "HTTP/",
  55. "HTTP/1.1A 100 OK",
  56. "HTTP/1.",
  57. "HTTP/1.1 1",
  58. "Fish/1.1 100 OK",
  59. "HTTP/1.1 10",
  60. "HTTP",
  61. "H\0TP/1.1 100 OK",
  62. /* Previously allowed, now disallowed. */
  63. "HTTP/1.1 200 OK",
  64. "HTTP/1.1 200 \t OK",
  65. " HTTP/1.1 200 OK",
  66. "Norman is a dog HTTP/1.1 200 OK",
  67. "HTTP/1.1000 100 OK",
  68. "HTTP/1000.1000 100 OK",
  69. "HTTP/00001.1 100 OK",
  70. "HTTP/1.00001 100 OK",
  71. "HTTP/99.99 100 OK",
  72. "HTTP/1.1 600 OK",
  73. NULL
  74. };
  75. static int status_lines(void)
  76. {
  77. ne_status s;
  78. int n;
  79. for (n = 0; accept_sl[n].status != NULL; n++) {
  80. ONV(ne_parse_statusline(accept_sl[n].status, &s),
  81. ("valid #%d: parsing '%s' failed", n, accept_sl[n].status));
  82. ONV(accept_sl[n].major != s.major_version, ("valid #%d: major", n));
  83. ONV(accept_sl[n].minor != s.minor_version, ("valid #%d: minor", n));
  84. ONV(accept_sl[n].code != s.code, ("valid #%d: code %d not %d", n, s.code, accept_sl[n].code));
  85. ONV(strcmp(accept_sl[n].rp, s.reason_phrase),
  86. ("valid #%d: reason phrase [%s] not [%s]", n, s.reason_phrase, accept_sl[n].rp));
  87. ne_free(s.reason_phrase);
  88. memset(&s, 0, sizeof s);
  89. }
  90. for (n = 0; bad_sl[n] != NULL; n++) {
  91. ONV(ne_parse_statusline(bad_sl[n], &s) == 0,
  92. ("invalid #%d parsed OK - [%s]", n, bad_sl[n]));
  93. }
  94. return OK;
  95. }
  96. /* Write MD5 of 'len' bytes of 'str' to 'digest' */
  97. static const unsigned char *digest_md5(const char *data, size_t len,
  98. unsigned int digest[4])
  99. {
  100. struct ne_md5_ctx *ctx;
  101. #define CHUNK 100
  102. ctx = ne_md5_create_ctx();
  103. if (!ctx) {
  104. return (unsigned char *)"NO-MD5-SUPPORT";
  105. }
  106. /* exercise the buffering interface */
  107. while (len > CHUNK) {
  108. ne_md5_process_bytes(data, CHUNK, ctx);
  109. len -= CHUNK;
  110. data += CHUNK;
  111. }
  112. ne_md5_process_bytes(data, len, ctx);
  113. ne_md5_finish_ctx(ctx, digest);
  114. ne_md5_destroy_ctx(ctx);
  115. return (unsigned char *)digest;
  116. }
  117. static int md5(void)
  118. {
  119. unsigned int buf[4], buf2[4] = {0};
  120. char ascii[33] = {0};
  121. char zzzs[500];
  122. ne_md5_to_ascii(digest_md5("", 0, buf), ascii);
  123. ONN("MD5(null)", strcmp(ascii, "d41d8cd98f00b204e9800998ecf8427e"));
  124. ne_md5_to_ascii(digest_md5("foobar", 7, buf), ascii);
  125. ONN("MD5(foobar)", strcmp(ascii, "b4258860eea29e875e2ee4019763b2bb"));
  126. /* $ perl -e 'printf "z"x500' | md5sum
  127. * 8b9323bd72250ea7f1b2b3fb5046391a - */
  128. memset(zzzs, 'z', sizeof zzzs);
  129. ne_md5_to_ascii(digest_md5(zzzs, sizeof zzzs, buf), ascii);
  130. ONN("MD5(\"z\"x512)", strcmp(ascii, "8b9323bd72250ea7f1b2b3fb5046391a"));
  131. ne_ascii_to_md5(ascii, (unsigned char *)buf2);
  132. ON(memcmp(buf, buf2, 16));
  133. return OK;
  134. }
  135. static int md5_alignment(void)
  136. {
  137. char *bb = ne_malloc(66);
  138. struct ne_md5_ctx *ctx;
  139. /* regression test for a bug in md5.c in <0.15.0 on SPARC, where
  140. * the process_bytes function would SIGBUS if the buffer argument
  141. * isn't 32-bit aligned. Won't trigger on x86 though. */
  142. ctx = ne_md5_create_ctx();
  143. ONN("could not create MD5 context", ctx == NULL);
  144. ne_md5_process_bytes(bb + 1, 65, ctx);
  145. ne_md5_destroy_ctx(ctx);
  146. ne_free(bb);
  147. return OK;
  148. }
  149. #define INIT_MD5 "0123456789abcdeffedcba9876543210"
  150. static int md5_read(void)
  151. {
  152. union {
  153. unsigned int int32[4];
  154. unsigned char buf[16];
  155. } u;
  156. struct ne_md5_ctx *ctx = ne_md5_create_ctx();
  157. void *rv;
  158. char hex[33];
  159. rv = ne_md5_read_ctx(ctx, u.buf);
  160. if (rv == NULL) {
  161. ne_md5_destroy_ctx(ctx);
  162. return SKIP;
  163. }
  164. ONN("bogus return value", rv != u.buf);
  165. ne_md5_to_ascii(u.buf, hex);
  166. ONV(strcmp(INIT_MD5, hex) != 0,
  167. ("read context was %s not %s", hex, INIT_MD5));
  168. ne_md5_destroy_ctx(ctx);
  169. return OK;
  170. }
  171. static const struct {
  172. const char *str;
  173. time_t time;
  174. enum { d_rfc1123, d_iso8601, d_rfc1036, d_asctime } type;
  175. } good_dates[] = {
  176. { "Fri, 08 Jun 2001 22:59:46 GMT", 992041186, d_rfc1123 },
  177. { "Friday, 08-Jun-01 22:59:46 GMT", 992041186, d_rfc1036 },
  178. { "Wednesday, 06-Jun-01 22:59:46 GMT", 991868386, d_rfc1036 },
  179. { "Wed Jun 06 22:59:46 2001", 991868386, d_asctime },
  180. /* some different types of ISO8601 dates. */
  181. { "2001-06-08T22:59:46Z", 992041186, d_iso8601 },
  182. { "2001-06-08T22:59:46.9Z", 992041186, d_iso8601 },
  183. { "2001-06-08T26:00:46+03:01", 992041186, d_iso8601 },
  184. { "2001-06-08T20:58:46-02:01", 992041186, d_iso8601 },
  185. { NULL }
  186. };
  187. static int parse_dates(void)
  188. {
  189. int n;
  190. for (n = 0; good_dates[n].str != NULL; n++) {
  191. time_t res;
  192. const char *str = good_dates[n].str;
  193. switch (good_dates[n].type) {
  194. case d_rfc1036: res = ne_rfc1036_parse(str); break;
  195. case d_iso8601: res = ne_iso8601_parse(str); break;
  196. case d_rfc1123: res = ne_rfc1123_parse(str); break;
  197. case d_asctime: res = ne_asctime_parse(str); break;
  198. default: res = -1; break;
  199. }
  200. ONV(res == -1, ("date %d parse", n));
  201. #define FT "%" NE_FMT_TIME_T
  202. ONV(res != good_dates[n].time, (
  203. "date %d incorrect (" FT " not " FT ")", n,
  204. res, good_dates[n].time));
  205. }
  206. return OK;
  207. }
  208. #define BAD_DATE(format, result) \
  209. ONN(format " date parse must fail", result != -1)
  210. /* Test for bad dates; trigger segfaults in ne_rfc1036_parse() in
  211. * <=0.24.5. */
  212. static int bad_dates(void)
  213. {
  214. static const char *dates[] = {
  215. "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  216. "Friday, 08-Jun-01",
  217. };
  218. size_t n;
  219. for (n = 0; n < sizeof(dates)/sizeof(dates[0]); n++) {
  220. BAD_DATE("rfc1036", ne_rfc1036_parse(dates[n]));
  221. BAD_DATE("iso8601", ne_iso8601_parse(dates[n]));
  222. BAD_DATE("rfc1123", ne_rfc1123_parse(dates[n]));
  223. BAD_DATE("asctime", ne_asctime_parse(dates[n]));
  224. }
  225. #if SIZEOF_TIME_T == 8 && defined(INT64_MAX)
  226. {
  227. char *rv = ne_rfc1123_date(INT64_MAX);
  228. ONV(rv != NULL,
  229. ("RFC1123 date conversion surprisingly worked for INT64_MAX: %s",
  230. rv));
  231. }
  232. #endif
  233. return OK;
  234. }
  235. #define GOOD(n,m,msg) ONV(ne_version_match(n,m), \
  236. ("match of " msg " failed (%d.%d)", n, m))
  237. #define BAD(n,m,msg) ONV(ne_version_match(n,m) == 0, \
  238. ("match of " msg " succeeded (%d.%d)", n, m))
  239. static int versioning(void)
  240. {
  241. GOOD(NE_VERSION_MAJOR, NE_VERSION_MINOR, "current version");
  242. BAD(NE_VERSION_MAJOR + 1, 0, "later major");
  243. BAD(NE_VERSION_MAJOR, NE_VERSION_MINOR + 1, "later minor");
  244. #if NE_VERSION_MAJOR > 1
  245. BAD(NE_VERSION_MAJOR - 1, 0, "earlier major");
  246. #if NE_VERSION_MINOR > 0
  247. GOOD(NE_VERSION_MAJOR, NE_VERSION_MINOR - 1, "earlier minor");
  248. #endif /* NE_VERSION_MINOR > 0 */
  249. #else /* where NE_VERSION_MAJOR < 2; note that 0.28 thru 1.0 maintain
  250. * backwards compatibility to 0.27 */
  251. BAD(0, 26, "minor version before 0.27");
  252. GOOD(0, 27, "current version back-compat to 0.27");
  253. GOOD(0, 28, "current version back-compat to 0.28");
  254. GOOD(0, 29, "current version back-compat to 0.29");
  255. GOOD(0, 30, "current version back-compat to 0.30");
  256. #endif
  257. return OK;
  258. }
  259. #undef GOOD
  260. #undef BAD
  261. /* basic ne_version_string() sanity tests */
  262. static int version_string(void)
  263. {
  264. char buf[1024];
  265. ne_snprintf(buf, sizeof buf, "%s", ne_version_string());
  266. NE_DEBUG(NE_DBG_HTTP, "Version string: %s\n", buf);
  267. ONN("version string too long", strlen(buf) > 200);
  268. ONN("version string contained newline", strchr(buf, '\n') != NULL);
  269. return OK;
  270. }
  271. static int support(void)
  272. {
  273. #ifdef NE_HAVE_SSL
  274. ONN("SSL support not advertised", !ne_has_support(NE_FEATURE_SSL));
  275. #else
  276. ONN("SSL support advertised", ne_has_support(NE_FEATURE_SSL));
  277. #endif
  278. #ifdef NE_HAVE_ZLIB
  279. ONN("zlib support not advertised", !ne_has_support(NE_FEATURE_ZLIB));
  280. #else
  281. ONN("zlib support advertised", ne_has_support(NE_FEATURE_ZLIB));
  282. #endif
  283. #ifdef NE_HAVE_IPV6
  284. ONN("IPv6 support not advertised", !ne_has_support(NE_FEATURE_IPV6));
  285. #else
  286. ONN("IPv6 support advertised", ne_has_support(NE_FEATURE_IPV6));
  287. #endif
  288. #ifdef NE_HAVE_LFS
  289. ONN("LFS support not advertised", !ne_has_support(NE_FEATURE_LFS));
  290. #else
  291. ONN("LFS support advertised", ne_has_support(NE_FEATURE_LFS));
  292. #endif
  293. #ifdef NE_HAVE_TS_SSL
  294. ONN("Thread-safe SSL support not advertised",
  295. !ne_has_support(NE_FEATURE_TS_SSL));
  296. #else
  297. ONN("Thread-safe SSL support advertised",
  298. ne_has_support(NE_FEATURE_TS_SSL));
  299. #endif
  300. #ifdef NE_HAVE_I18N
  301. ONN("i18n support not advertised",
  302. !ne_has_support(NE_FEATURE_I18N));
  303. #else
  304. ONN("i18n SSL support advertised",
  305. ne_has_support(NE_FEATURE_I18N));
  306. #endif
  307. #ifdef NE_HAVE_GSSAPI
  308. ONN("GSSAPI support not advertised",
  309. !ne_has_support(NE_FEATURE_GSSAPI));
  310. #else
  311. ONN("GSSAPI support advertised",
  312. ne_has_support(NE_FEATURE_GSSAPI));
  313. #endif
  314. #ifdef NE_HAVE_LIBPXY
  315. ONN("libproxy support not advertised",
  316. !ne_has_support(NE_FEATURE_LIBPXY));
  317. #else
  318. ONN("libproxy support advertised",
  319. ne_has_support(NE_FEATURE_LIBPXY));
  320. #endif
  321. return OK;
  322. }
  323. ne_test tests[] = {
  324. T(status_lines),
  325. T(md5),
  326. T(md5_alignment),
  327. T(md5_read),
  328. T(parse_dates),
  329. T(bad_dates),
  330. T(versioning),
  331. T(version_string),
  332. T(support),
  333. T(NULL)
  334. };