util-tests.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. ONN("bogus return value", rv != u.buf);
  161. ne_md5_to_ascii(u.buf, hex);
  162. ONV(strcmp(INIT_MD5, hex) != 0,
  163. ("read context was %s not %s", hex, INIT_MD5));
  164. ne_md5_destroy_ctx(ctx);
  165. return OK;
  166. }
  167. static const struct {
  168. const char *str;
  169. time_t time;
  170. enum { d_rfc1123, d_iso8601, d_rfc1036, d_asctime } type;
  171. } good_dates[] = {
  172. { "Fri, 08 Jun 2001 22:59:46 GMT", 992041186, d_rfc1123 },
  173. { "Friday, 08-Jun-01 22:59:46 GMT", 992041186, d_rfc1036 },
  174. { "Wednesday, 06-Jun-01 22:59:46 GMT", 991868386, d_rfc1036 },
  175. { "Wed Jun 06 22:59:46 2001", 991868386, d_asctime },
  176. /* some different types of ISO8601 dates. */
  177. { "2001-06-08T22:59:46Z", 992041186, d_iso8601 },
  178. { "2001-06-08T22:59:46.9Z", 992041186, d_iso8601 },
  179. { "2001-06-08T26:00:46+03:01", 992041186, d_iso8601 },
  180. { "2001-06-08T20:58:46-02:01", 992041186, d_iso8601 },
  181. { NULL }
  182. };
  183. static int parse_dates(void)
  184. {
  185. int n;
  186. for (n = 0; good_dates[n].str != NULL; n++) {
  187. time_t res;
  188. const char *str = good_dates[n].str;
  189. switch (good_dates[n].type) {
  190. case d_rfc1036: res = ne_rfc1036_parse(str); break;
  191. case d_iso8601: res = ne_iso8601_parse(str); break;
  192. case d_rfc1123: res = ne_rfc1123_parse(str); break;
  193. case d_asctime: res = ne_asctime_parse(str); break;
  194. default: res = -1; break;
  195. }
  196. ONV(res == -1, ("date %d parse", n));
  197. #define FT "%" NE_FMT_TIME_T
  198. ONV(res != good_dates[n].time, (
  199. "date %d incorrect (" FT " not " FT ")", n,
  200. res, good_dates[n].time));
  201. }
  202. return OK;
  203. }
  204. #define BAD_DATE(format, result) \
  205. ONN(format " date parse must fail", result != -1)
  206. /* Test for bad dates; trigger segfaults in ne_rfc1036_parse() in
  207. * <=0.24.5. */
  208. static int bad_dates(void)
  209. {
  210. static const char *dates[] = {
  211. "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  212. "Friday, 08-Jun-01",
  213. };
  214. size_t n;
  215. for (n = 0; n < sizeof(dates)/sizeof(dates[0]); n++) {
  216. BAD_DATE("rfc1036", ne_rfc1036_parse(dates[n]));
  217. BAD_DATE("iso8601", ne_iso8601_parse(dates[n]));
  218. BAD_DATE("rfc1123", ne_rfc1123_parse(dates[n]));
  219. BAD_DATE("asctime", ne_asctime_parse(dates[n]));
  220. }
  221. #if SIZEOF_TIME_T == 8 && defined(INT64_MAX)
  222. {
  223. char *rv = ne_rfc1123_date(INT64_MAX);
  224. ONV(rv != NULL,
  225. ("RFC1123 date conversion surprisingly worked for INT64_MAX: %s",
  226. rv));
  227. }
  228. #endif
  229. return OK;
  230. }
  231. #define GOOD(n,m,msg) ONV(ne_version_match(n,m), \
  232. ("match of " msg " failed (%d.%d)", n, m))
  233. #define BAD(n,m,msg) ONV(ne_version_match(n,m) == 0, \
  234. ("match of " msg " succeeded (%d.%d)", n, m))
  235. static int versioning(void)
  236. {
  237. GOOD(NE_VERSION_MAJOR, NE_VERSION_MINOR, "current version");
  238. BAD(NE_VERSION_MAJOR + 1, 0, "later major");
  239. BAD(NE_VERSION_MAJOR, NE_VERSION_MINOR + 1, "later minor");
  240. #if NE_VERSION_MAJOR > 1
  241. BAD(NE_VERSION_MAJOR - 1, 0, "earlier major");
  242. #if NE_VERSION_MINOR > 0
  243. GOOD(NE_VERSION_MAJOR, NE_VERSION_MINOR - 1, "earlier minor");
  244. #endif /* NE_VERSION_MINOR > 0 */
  245. #else /* where NE_VERSION_MAJOR < 2; note that 0.28 thru 1.0 maintain
  246. * backwards compatibility to 0.27 */
  247. BAD(0, 26, "minor version before 0.27");
  248. GOOD(0, 27, "current version back-compat to 0.27");
  249. GOOD(0, 28, "current version back-compat to 0.28");
  250. GOOD(0, 29, "current version back-compat to 0.29");
  251. GOOD(0, 30, "current version back-compat to 0.30");
  252. #endif
  253. return OK;
  254. }
  255. #undef GOOD
  256. #undef BAD
  257. /* basic ne_version_string() sanity tests */
  258. static int version_string(void)
  259. {
  260. char buf[1024];
  261. ne_snprintf(buf, sizeof buf, "%s", ne_version_string());
  262. NE_DEBUG(NE_DBG_HTTP, "Version string: %s\n", buf);
  263. ONN("version string too long", strlen(buf) > 200);
  264. ONN("version string contained newline", strchr(buf, '\n') != NULL);
  265. return OK;
  266. }
  267. static int support(void)
  268. {
  269. #ifdef NE_HAVE_SSL
  270. ONN("SSL support not advertised", !ne_has_support(NE_FEATURE_SSL));
  271. #else
  272. ONN("SSL support advertised", ne_has_support(NE_FEATURE_SSL));
  273. #endif
  274. #ifdef NE_HAVE_ZLIB
  275. ONN("zlib support not advertised", !ne_has_support(NE_FEATURE_ZLIB));
  276. #else
  277. ONN("zlib support advertised", ne_has_support(NE_FEATURE_ZLIB));
  278. #endif
  279. #ifdef NE_HAVE_IPV6
  280. ONN("IPv6 support not advertised", !ne_has_support(NE_FEATURE_IPV6));
  281. #else
  282. ONN("IPv6 support advertised", ne_has_support(NE_FEATURE_IPV6));
  283. #endif
  284. #ifdef NE_HAVE_LFS
  285. ONN("LFS support not advertised", !ne_has_support(NE_FEATURE_LFS));
  286. #else
  287. ONN("LFS support advertised", ne_has_support(NE_FEATURE_LFS));
  288. #endif
  289. #ifdef NE_HAVE_TS_SSL
  290. ONN("Thread-safe SSL support not advertised",
  291. !ne_has_support(NE_FEATURE_TS_SSL));
  292. #else
  293. ONN("Thread-safe SSL support advertised",
  294. ne_has_support(NE_FEATURE_TS_SSL));
  295. #endif
  296. #ifdef NE_HAVE_I18N
  297. ONN("i18n support not advertised",
  298. !ne_has_support(NE_FEATURE_I18N));
  299. #else
  300. ONN("i18n SSL support advertised",
  301. ne_has_support(NE_FEATURE_I18N));
  302. #endif
  303. #ifdef NE_HAVE_GSSAPI
  304. ONN("GSSAPI support not advertised",
  305. !ne_has_support(NE_FEATURE_GSSAPI));
  306. #else
  307. ONN("GSSAPI support advertised",
  308. ne_has_support(NE_FEATURE_GSSAPI));
  309. #endif
  310. #ifdef NE_HAVE_LIBPXY
  311. ONN("libproxy support not advertised",
  312. !ne_has_support(NE_FEATURE_LIBPXY));
  313. #else
  314. ONN("libproxy support advertised",
  315. ne_has_support(NE_FEATURE_LIBPXY));
  316. #endif
  317. return OK;
  318. }
  319. ne_test tests[] = {
  320. T(status_lines),
  321. T(md5),
  322. T(md5_alignment),
  323. T(md5_read),
  324. T(parse_dates),
  325. T(bad_dates),
  326. T(versioning),
  327. T(version_string),
  328. T(support),
  329. T(NULL)
  330. };