torture_su.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. /*
  2. * This file is part of the Sofia-SIP package
  3. *
  4. * Copyright (C) 2005 Nokia Corporation.
  5. *
  6. * Contact: Pekka Pessi <pekka.pessi@nokia.com>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public License
  10. * as published by the Free Software Foundation; either version 2.1 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  21. * 02110-1301 USA
  22. *
  23. */
  24. /**@internal
  25. * @file torture_su.c
  26. *
  27. * Testing functions for su socket functions.
  28. *
  29. * @author Pekka Pessi <Pekka.Pessi@nokia.com>
  30. *
  31. * @date Created: Thu May 2 18:17:46 2002 ppessi
  32. */
  33. #include "config.h"
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <sofia-sip/su.h>
  38. #include <sofia-sip/su_localinfo.h>
  39. int tstflags;
  40. #define TSTFLAGS tstflags
  41. #include <sofia-sip/tstdef.h>
  42. char const *name = "torture_su";
  43. /** */
  44. static int test_sockaddr(void)
  45. {
  46. su_localinfo_t hints[1] = {{ LI_CANONNAME }};
  47. su_localinfo_t *li, *res = NULL;
  48. int s;
  49. su_sockaddr_t su[1], a[1], b[1];
  50. BEGIN();
  51. hints->li_family = AF_INET;
  52. TEST(su_getlocalinfo(hints, &res), 0);
  53. for (li = res; li; li = li->li_next) {
  54. if (li->li_addrlen != res->li_addrlen ||
  55. memcmp(li->li_addr, res->li_addr, li->li_addrlen) != 0)
  56. TEST_1(su_cmp_sockaddr(li->li_addr, res->li_addr) != 0);
  57. else
  58. TEST_1(su_cmp_sockaddr(li->li_addr, res->li_addr) == 0);
  59. }
  60. memset(su, 0, sizeof su);
  61. TEST(su_getlocalip(su), 0);
  62. if (res->li_family == AF_INET)
  63. TEST(su_cmp_sockaddr(res->li_addr, su), 0);
  64. TEST_1(su_gli_strerror(ELI_NOERROR));
  65. TEST_1(su_gli_strerror(ELI_NOADDRESS));
  66. TEST_1(su_gli_strerror(ELI_FAMILY));
  67. TEST_1(su_gli_strerror(ELI_MEMORY));
  68. TEST_1(su_gli_strerror(ELI_RESOLVER));
  69. TEST_1(su_gli_strerror(ELI_SYSTEM));
  70. TEST_1(su_gli_strerror(-100));
  71. li = su_copylocalinfo(res); TEST_1(li);
  72. su_freelocalinfo(li);
  73. s = su_socket(res->li_family, SOCK_DGRAM, 0); TEST_1(s != -1);
  74. TEST(su_setblocking(s, 0), 0);
  75. TEST(su_setblocking(s, 1), 0);
  76. TEST(su_close(s), 0);
  77. su_freelocalinfo(res), res = NULL;
  78. #if SU_HAVE_IN6
  79. hints->li_family = AF_INET6;
  80. hints->li_flags &= ~LI_CANONNAME;
  81. hints->li_flags |= LI_V4MAPPED;
  82. TEST(su_getlocalinfo(hints, &res), 0);
  83. for (li = res; li; li = li->li_next)
  84. TEST(li->li_family, AF_INET6);
  85. su_freelocalinfo(res), res = NULL;
  86. #endif
  87. hints->li_flags |= LI_NUMERIC;
  88. TEST(su_getlocalinfo(hints, &res), 0);
  89. su_freelocalinfo(res), res = NULL;
  90. res = NULL;
  91. hints->li_flags |= LI_NAMEREQD;
  92. su_getlocalinfo(hints, &res);
  93. su_freelocalinfo(res), res = NULL;
  94. memset(a, 0, sizeof *a);
  95. memset(b, 0, sizeof *b);
  96. TEST_1(su_match_sockaddr(a, b));
  97. b->su_family = AF_INET;
  98. TEST_1(su_match_sockaddr(a, b));
  99. a->su_port = htons(12);
  100. TEST_1(!su_match_sockaddr(a, b));
  101. b->su_port = htons(12);
  102. TEST_1(su_match_sockaddr(a, b));
  103. a->su_sin.sin_addr.s_addr = htonl(0x7f000001);
  104. TEST_1(su_match_sockaddr(a, b));
  105. a->su_family = AF_INET;
  106. TEST_1(!su_match_sockaddr(a, b));
  107. b->su_sin.sin_addr.s_addr = htonl(0x7f000001);
  108. TEST_1(su_match_sockaddr(a, b));
  109. a->su_sin.sin_addr.s_addr = 0;
  110. TEST_1(su_match_sockaddr(a, b));
  111. #if SU_HAVE_IN6
  112. a->su_family = AF_INET6;
  113. TEST_1(!su_match_sockaddr(a, b));
  114. b->su_family = AF_INET6;
  115. TEST_1(su_match_sockaddr(a, b));
  116. b->su_sin6.sin6_addr.s6_addr[15] = 1;
  117. TEST_1(su_match_sockaddr(a, b));
  118. TEST_1(!su_match_sockaddr(b, a));
  119. a->su_sin6.sin6_addr.s6_addr[15] = 2;
  120. TEST_1(!su_match_sockaddr(a, b));
  121. a->su_family = 0;
  122. TEST_1(su_match_sockaddr(a, b));
  123. TEST_1(!su_match_sockaddr(b, a));
  124. #endif
  125. END();
  126. }
  127. #include <sofia-sip/su_wait.h>
  128. int test_sendrecv(void)
  129. {
  130. su_socket_t s, l, a;
  131. ssize_t n;
  132. su_sockaddr_t su, csu;
  133. socklen_t sulen = sizeof su.su_sin, csulen = sizeof csu.su_sin;
  134. char b1[8], b2[8], b3[8];
  135. su_iovec_t sv[3], rv[3];
  136. sv[0].siv_base = "one!one!", sv[0].siv_len = 8;
  137. sv[1].siv_base = "two!two!", sv[1].siv_len = 8;
  138. sv[2].siv_base = "third!", sv[2].siv_len = 6;
  139. rv[0].siv_base = b1, rv[0].siv_len = 8;
  140. rv[1].siv_base = b2, rv[1].siv_len = 8;
  141. rv[2].siv_base = b3, rv[2].siv_len = 8;
  142. BEGIN();
  143. s = su_socket(AF_INET, SOCK_DGRAM, 0); TEST_1(s != -1);
  144. su_setblocking(s, 1);
  145. memset(&su, 0, sulen);
  146. su.su_len = sulen;
  147. su.su_family = AF_INET;
  148. TEST(su_inet_pton(AF_INET, "127.0.0.1", &su.su_sin.sin_addr), 1);
  149. TEST(bind(s, &su.su_sa, sulen), 0);
  150. TEST(getsockname(s, &su.su_sa, &sulen), 0);
  151. n = su_vsend(s, sv, 3, 0, &su, sulen); TEST(n, 8 + 8 + 6);
  152. n = su_vrecv(s, rv, 3, 0, &su, &sulen); TEST(n, 8 + 8 + 6);
  153. TEST_M(rv[0].siv_base, sv[0].siv_base, sv[0].siv_len);
  154. TEST_M(rv[1].siv_base, sv[1].siv_base, sv[1].siv_len);
  155. TEST_M(rv[2].siv_base, sv[2].siv_base, sv[2].siv_len);
  156. su_close(s);
  157. l = su_socket(AF_INET, SOCK_STREAM, 0); TEST_1(l != -1);
  158. s = su_socket(AF_INET, SOCK_STREAM, 0); TEST_1(s != -1);
  159. memset(&su, 0, sulen);
  160. su.su_len = sulen;
  161. su.su_family = AF_INET;
  162. TEST(su_inet_pton(AF_INET, "127.0.0.1", &su.su_sin.sin_addr), 1);
  163. TEST(bind(l, &su.su_sa, sulen), 0);
  164. TEST(bind(s, &su.su_sa, sulen), 0);
  165. TEST(getsockname(l, &su.su_sa, &sulen), 0);
  166. TEST(listen(l, 5), 0);
  167. TEST(su_setblocking(s, 1), 0);
  168. TEST(connect(s, &su.su_sa, sulen), 0);
  169. a = accept(l, &csu.su_sa, &csulen); TEST_1(a != -1);
  170. TEST(su_setblocking(a, 1), 0);
  171. n = su_vsend(s, sv, 3, 0, NULL, 0); TEST(n, 8 + 8 + 6);
  172. n = su_vrecv(a, rv, 3, 0, NULL, NULL); TEST(n, 8 + 8 + 6);
  173. TEST_M(rv[0].siv_base, sv[0].siv_base, sv[0].siv_len);
  174. TEST_M(rv[1].siv_base, sv[1].siv_base, sv[1].siv_len);
  175. TEST_M(rv[2].siv_base, sv[2].siv_base, sv[2].siv_len);
  176. n = send(a, "", 0, 0); TEST(n, 0);
  177. n = su_vsend(a, sv, 3, 0, NULL, 0); TEST(n, 8 + 8 + 6);
  178. {
  179. su_wait_t w[1] = { SU_WAIT_INIT };
  180. TEST(su_wait_create(w, s, SU_WAIT_IN | SU_WAIT_HUP), 0);
  181. TEST(su_wait(w, 0, 500), SU_WAIT_TIMEOUT);
  182. TEST(su_wait(w, 1, 500), 0);
  183. TEST(su_wait_events(w, s), SU_WAIT_IN);
  184. TEST_SIZE(su_getmsgsize(s), 8 + 8 + 6);
  185. n = su_vrecv(s, rv, 3, 0, NULL, NULL); TEST(n, 8 + 8 + 6);
  186. TEST(su_wait(w, 1, 100), SU_WAIT_TIMEOUT);
  187. shutdown(a, 2);
  188. TEST(su_wait(w, 1, 100), 0);
  189. #if SU_HAVE_WINSOCK
  190. TEST_1(su_wait_events(w, s) & SU_WAIT_HUP);
  191. #else
  192. TEST_1(su_wait_events(w, s));
  193. n = su_vrecv(s, rv, 3, 0, NULL, NULL); TEST(n, 0);
  194. #endif
  195. su_wait_destroy(w);
  196. }
  197. su_close(a);
  198. su_close(l);
  199. su_close(s);
  200. END();
  201. }
  202. #if HAVE_SELECT
  203. #if HAVE_WIN32
  204. int test_select(void)
  205. {
  206. return 0;
  207. }
  208. #else
  209. #if HAVE_SYS_SELECT_H
  210. #include <sys/select.h>
  211. #elif HAVE_SYS_TIME_H
  212. #include <sys/time.h>
  213. #endif
  214. #include <sys/types.h>
  215. #include <unistd.h>
  216. #ifndef __NFDBITS
  217. #define __NFDBITS (8 * sizeof (long int))
  218. #endif
  219. #undef howmany
  220. /* Size of fd set in bytes. Sorry, octets. */
  221. #define howmany(n) (((n) + __NFDBITS - 1) / __NFDBITS * (__NFDBITS / 8))
  222. #define FD_ZERO_TO(maxfd, set) \
  223. memset((set), 0, howmany(maxfd))
  224. /* Test assumptions in su_select_port implementation */
  225. int test_select(void)
  226. {
  227. su_socket_t s;
  228. su_sockaddr_t su;
  229. socklen_t sulen = sizeof su.su_sin;
  230. size_t bytes;
  231. fd_set *rset, *wset;
  232. struct timeval tv;
  233. BEGIN();
  234. s = su_socket(AF_INET, SOCK_DGRAM, 0); TEST_1(s != -1);
  235. memset(&su, 0, sulen);
  236. su.su_len = sulen;
  237. su.su_family = AF_INET;
  238. TEST(su_inet_pton(AF_INET, "127.0.0.1", &su.su_sin.sin_addr), 1);
  239. TEST(bind(s, &su.su_sa, sulen), 0);
  240. TEST(getsockname(s, &su.su_sa, &sulen), 0);
  241. tv.tv_sec = 0; tv.tv_usec = 1000;
  242. TEST(select(0, NULL, NULL, NULL, &tv), 0);
  243. bytes = howmany(s);
  244. TEST_1(rset = malloc(bytes));
  245. TEST_1(wset = malloc(bytes));
  246. FD_ZERO_TO(s, rset); FD_ZERO_TO(s, wset); FD_SET(s, wset);
  247. tv.tv_sec = 0, tv.tv_usec = 1000;
  248. TEST(select(s + 1, NULL, wset, NULL, &tv), 1);
  249. TEST_1(FD_ISSET(s, wset));
  250. FD_ZERO_TO(s, rset); FD_ZERO_TO(s, wset);
  251. FD_SET(s, rset); FD_SET(s, wset);
  252. tv.tv_sec = 0, tv.tv_usec = 1000;
  253. TEST(select(s + 1, rset, wset, NULL, &tv), 1);
  254. TEST_1(!FD_ISSET(s, rset));
  255. TEST_1(FD_ISSET(s, wset));
  256. FD_ZERO_TO(s, rset); FD_ZERO_TO(s, wset);
  257. FD_SET(s, rset); FD_SET(s, wset);
  258. tv.tv_sec = 0, tv.tv_usec = 1000;
  259. TEST(select(s + 1, rset, NULL, NULL, &tv), 0);
  260. TEST_1(!FD_ISSET(s, rset));
  261. FD_ZERO_TO(s, rset); FD_ZERO_TO(s, wset);
  262. FD_SET(s, rset); FD_CLR(s, wset);
  263. tv.tv_sec = 0, tv.tv_usec = 1000;
  264. TEST(select(s + 1, rset, wset, NULL, &tv), 0);
  265. TEST_1(!FD_ISSET(s, rset));
  266. TEST_1(!FD_ISSET(s, wset));
  267. TEST(su_sendto(s, "foo", 3, 0, &su, sulen), 3);
  268. FD_ZERO_TO(s, rset); FD_ZERO_TO(s, wset);
  269. FD_SET(s, rset); FD_CLR(s, wset);
  270. tv.tv_sec = 0, tv.tv_usec = 1000;
  271. TEST(select(s + 1, rset, wset, NULL, &tv), 1);
  272. TEST_1(FD_ISSET(s, rset));
  273. TEST_1(!FD_ISSET(s, wset));
  274. FD_ZERO_TO(s, rset); FD_ZERO_TO(s, wset);
  275. FD_SET(s, rset); FD_SET(s, wset);
  276. tv.tv_sec = 0, tv.tv_usec = 1000;
  277. TEST(select(s + 1, rset, wset, NULL, &tv), 2);
  278. TEST_1(FD_ISSET(s, rset));
  279. TEST_1(FD_ISSET(s, wset));
  280. su_close(s);
  281. free(wset);
  282. free(rset);
  283. END();
  284. }
  285. #endif
  286. #else
  287. int test_select(void)
  288. {
  289. return 0;
  290. }
  291. #endif
  292. #include <sofia-sip/su_md5.h>
  293. int test_md5(void)
  294. {
  295. BEGIN();
  296. su_md5_t md5[1], md5i[1];
  297. uint8_t digest[SU_MD5_DIGEST_SIZE];
  298. char hexdigest[2 * SU_MD5_DIGEST_SIZE + 1];
  299. struct { char *input; uint8_t digest[SU_MD5_DIGEST_SIZE]; } suite[] = {
  300. { (""),
  301. { 0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04,
  302. 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e } },
  303. { ("a"), { 0x0c, 0xc1, 0x75, 0xb9, 0xc0, 0xf1, 0xb6, 0xa8,
  304. 0x31, 0xc3, 0x99, 0xe2, 0x69, 0x77, 0x26, 0x61 } },
  305. { ("abc"), { 0x90, 0x01, 0x50, 0x98, 0x3c, 0xd2, 0x4f, 0xb0,
  306. 0xd6, 0x96, 0x3f, 0x7d, 0x28, 0xe1, 0x7f, 0x72 } },
  307. { ("message digest"),
  308. { 0xf9, 0x6b, 0x69, 0x7d, 0x7c, 0xb7, 0x93, 0x8d,
  309. 0x52, 0x5a, 0x2f, 0x31, 0xaa, 0xf1, 0x61, 0xd0 } },
  310. { ("abcdefghijklmnopqrstuvwxyz"),
  311. { 0xc3, 0xfc, 0xd3, 0xd7, 0x61, 0x92, 0xe4, 0x00,
  312. 0x7d, 0xfb, 0x49, 0x6c, 0xca, 0x67, 0xe1, 0x3b } },
  313. { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
  314. { 0xd1, 0x74, 0xab, 0x98, 0xd2, 0x77, 0xd9, 0xf5,
  315. 0xa5, 0x61, 0x1c, 0x2c, 0x9f, 0x41, 0x9d, 0x9f } },
  316. { "1234567890123456789012345678901234567890"
  317. "1234567890123456789012345678901234567890",
  318. { 0x57, 0xed, 0xf4, 0xa2, 0x2b, 0xe3, 0xc9, 0x55,
  319. 0xac, 0x49, 0xda, 0x2e, 0x21, 0x07, 0xb6, 0x7a } }};
  320. su_md5_init(md5);
  321. su_md5_update(md5, suite[0].input, 0);
  322. su_md5_digest(md5, digest);
  323. TEST_M(digest, suite[0].digest, SU_MD5_DIGEST_SIZE);
  324. su_md5_deinit(md5);
  325. su_md5_init(md5);
  326. su_md5_strupdate(md5, suite[1].input);
  327. su_md5_digest(md5, digest);
  328. TEST_M(digest, suite[1].digest, SU_MD5_DIGEST_SIZE);
  329. su_md5_deinit(md5);
  330. su_md5_init(md5);
  331. su_md5_iupdate(md5, suite[2].input, 3);
  332. su_md5_digest(md5, digest);
  333. TEST_M(digest, suite[2].digest, SU_MD5_DIGEST_SIZE);
  334. su_md5_deinit(md5);
  335. su_md5_init(md5);
  336. su_md5_striupdate(md5, suite[3].input);
  337. su_md5_digest(md5, digest);
  338. TEST_M(digest, suite[3].digest, SU_MD5_DIGEST_SIZE);
  339. su_md5_deinit(md5);
  340. su_md5_init(md5);
  341. su_md5_iupdate(md5, suite[4].input, 13);
  342. su_md5_striupdate(md5, suite[4].input + 13);
  343. su_md5_digest(md5, digest);
  344. TEST_M(digest, suite[4].digest, SU_MD5_DIGEST_SIZE);
  345. su_md5_deinit(md5);
  346. su_md5_init(md5);
  347. su_md5_update(md5, suite[5].input, 13);
  348. su_md5_strupdate(md5, suite[5].input + 13);
  349. su_md5_digest(md5, digest);
  350. TEST_M(digest, suite[5].digest, SU_MD5_DIGEST_SIZE);
  351. su_md5_deinit(md5);
  352. su_md5_init(md5);
  353. su_md5_update(md5, suite[6].input, 13);
  354. su_md5_strupdate(md5, suite[6].input + 13);
  355. su_md5_digest(md5, digest);
  356. TEST_M(digest, suite[6].digest, SU_MD5_DIGEST_SIZE);
  357. su_md5_deinit(md5);
  358. su_md5_init(md5);
  359. su_md5_str0update(md5, NULL);
  360. su_md5_hexdigest(md5, hexdigest);
  361. TEST_S(hexdigest, "93b885adfe0da089cdf634904fd59f71");
  362. su_md5_deinit(md5);
  363. su_md5_init(md5);
  364. su_md5_stri0update(md5, NULL);
  365. su_md5_stri0update(md5, "ABBADABBADOO");
  366. su_md5_hexdigest(md5, hexdigest);
  367. TEST_S(hexdigest, "101e6dd7cfabdb5c74f44b4c545c05cc");
  368. su_md5_init(md5);
  369. su_md5_update(md5, "\0abbadabbadoo\0", 14);
  370. su_md5_hexdigest(md5, hexdigest);
  371. TEST_S(hexdigest, "101e6dd7cfabdb5c74f44b4c545c05cc");
  372. su_md5_deinit(md5);
  373. /* Calculate md5 sum of 512 MB of zero */
  374. if (getenv("EXPENSIVE_CHECKS")) {
  375. char zerokilo[1024] = { '\0' };
  376. int i;
  377. su_md5_init(md5);
  378. su_md5_iupdate(md5, zerokilo, 19);
  379. for (i = 1; i < 512 * 1024; i++)
  380. su_md5_update(md5, zerokilo, 1024);
  381. *md5i = *md5;
  382. su_md5_update(md5, zerokilo, 1024 - 19);
  383. su_md5_hexdigest(md5, hexdigest);
  384. TEST_S(hexdigest, "aa559b4e3523a6c931f08f4df52d58f2");
  385. su_md5_deinit(md5);
  386. su_md5_iupdate(md5i, zerokilo, 1024 - 19);
  387. su_md5_hexdigest(md5i, hexdigest);
  388. TEST_S(hexdigest, "aa559b4e3523a6c931f08f4df52d58f2");
  389. }
  390. END();
  391. }
  392. void usage(int exitcode)
  393. {
  394. fprintf(stderr, "usage: %s [-v] [-a]\n", name);
  395. exit(exitcode);
  396. }
  397. int main(int argc, char *argv[])
  398. {
  399. int retval = 0;
  400. int i;
  401. su_init();
  402. for (i = 1; argv[i]; i++) {
  403. if (strcmp(argv[i], "-v") == 0)
  404. tstflags |= tst_verbatim;
  405. else if (strcmp(argv[i], "-a") == 0)
  406. tstflags |= tst_abort;
  407. else
  408. usage(1);
  409. }
  410. retval |= test_sockaddr();
  411. retval |= test_sendrecv();
  412. retval |= test_select();
  413. retval |= test_md5(); fflush(stdout);
  414. su_deinit();
  415. return retval;
  416. }