socket.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624
  1. /*
  2. Socket handling tests
  3. Copyright (C) 2002-2011, 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. /* This module can be compiled with -DSOCKET_SSL enabled, to run all
  17. * the tests over an SSL connection. */
  18. #include "config.h"
  19. #include <sys/types.h>
  20. #include <sys/socket.h> /* for AF_INET6 */
  21. #ifdef HAVE_STDLIB_H
  22. #include <stdlib.h>
  23. #endif
  24. #ifdef HAVE_STRING_H
  25. #include <string.h>
  26. #endif
  27. #ifdef HAVE_UNISTD_H
  28. #include <unistd.h> /* for gethostname() */
  29. #endif
  30. #include <time.h> /* for time() */
  31. #include "ne_socket.h"
  32. #include "ne_utils.h"
  33. #include "ne_alloc.h"
  34. #include "child.h"
  35. #include "tests.h"
  36. #include "utils.h"
  37. #ifdef SOCKET_SSL
  38. #include "ne_ssl.h"
  39. ne_ssl_context *server_ctx, *client_ctx;
  40. #endif
  41. static ne_sock_addr *localhost;
  42. static char buffer[BUFSIZ];
  43. #if defined(AF_INET6) && defined(USE_GETADDRINFO)
  44. #define TEST_IPV6
  45. #endif
  46. /* tests for doing init/finish multiple times. */
  47. static int multi_init(void)
  48. {
  49. int res1 = ne_sock_init(), res2 = ne_sock_init();
  50. ONV(res1 != res2, ("cached init result changed from %d to %d",
  51. res1, res2));
  52. ne_sock_exit();
  53. ne_sock_exit();
  54. ne_sock_exit();
  55. res1 = ne_sock_init();
  56. ONV(res1 != res2, ("re-init after exit gave %d not %d",
  57. res1, res2));
  58. ne_sock_exit();
  59. res2 = ne_sock_init();
  60. ONV(res1 != res2, ("second time, cached init result changed from %d to %d",
  61. res1, res2));
  62. return OK;
  63. }
  64. /* Create and connect *sock to address addr on given port. */
  65. static int do_connect(ne_socket **sock, ne_sock_addr *addr, unsigned int port)
  66. {
  67. const ne_inet_addr *ia;
  68. *sock = ne_sock_create();
  69. ONN("could not create socket", *sock == NULL);
  70. for (ia = ne_addr_first(addr); ia; ia = ne_addr_next(addr)) {
  71. if (ne_sock_connect(*sock, ia, port) == 0)
  72. return OK;
  73. }
  74. t_context("could not connect to server: %s", ne_sock_error(*sock));
  75. ne_sock_close(*sock);
  76. return FAIL;
  77. }
  78. static int close_and_wait(ne_socket *sock)
  79. {
  80. int ret = ne_sock_close(sock);
  81. ONV(ret, ("failed closing socket: %d", ret));
  82. return await_server();
  83. }
  84. #ifdef SOCKET_SSL
  85. static int init_ssl(void)
  86. {
  87. char *server_key;
  88. ne_ssl_certificate *cert;
  89. /* take srcdir as argv[1]. */
  90. if (test_argc > 1) {
  91. server_key = ne_concat(test_argv[1], "/server.key", NULL);
  92. } else {
  93. server_key = ne_strdup("server.key");
  94. }
  95. ONN("sock_init failed", ne_sock_init());
  96. server_ctx = ne_ssl_context_create(1);
  97. ONN("SSL_CTX_new failed", server_ctx == NULL);
  98. ne_ssl_context_keypair(server_ctx, "server.cert", server_key);
  99. client_ctx = ne_ssl_context_create(0);
  100. ONN("SSL_CTX_new failed for client", client_ctx == NULL);
  101. cert = ne_ssl_cert_read("ca/cert.pem");
  102. ONN("could not load ca/cert.pem", cert == NULL);
  103. ne_ssl_context_trustcert(client_ctx, cert);
  104. ne_free(server_key);
  105. return OK;
  106. }
  107. #endif
  108. static int resolve(void)
  109. {
  110. char buf[256];
  111. localhost = ne_addr_resolve("localhost", 0);
  112. ONV(ne_addr_result(localhost),
  113. ("could not resolve `localhost': %s",
  114. ne_addr_error(localhost, buf, sizeof buf)));
  115. /* and again for child.c */
  116. return lookup_localhost();
  117. }
  118. static int serve_close(ne_socket *sock, void *ud)
  119. {
  120. return 0;
  121. }
  122. #ifdef SOCKET_SSL
  123. struct serve_pair {
  124. server_fn fn;
  125. void *userdata;
  126. };
  127. static int wrap_serve(ne_socket *sock, void *ud)
  128. {
  129. struct serve_pair *pair = ud;
  130. if (ne_sock_accept_ssl(sock, server_ctx)) {
  131. NE_DEBUG(NE_DBG_SOCKET, "SSL_accept failed: %s\n", ne_sock_error(sock));
  132. return 1;
  133. }
  134. NE_DEBUG(NE_DBG_SOCKET, "SSL accept okay.\n");
  135. return pair->fn(sock, pair->userdata);
  136. }
  137. static int begin(ne_socket **sock, server_fn fn, void *ud)
  138. {
  139. struct serve_pair pair;
  140. unsigned int port;
  141. pair.fn = fn;
  142. pair.userdata = ud;
  143. CALL(new_spawn_server(1, wrap_serve, &pair, &port));
  144. CALL(do_connect(sock, localhost, port));
  145. ONV(ne_sock_connect_ssl(*sock, client_ctx, NULL),
  146. ("SSL negotiation failed: %s", ne_sock_error(*sock)));
  147. return OK;
  148. }
  149. #else
  150. /* non-SSL begin() function. */
  151. static int begin(ne_socket **sock, server_fn fn, void *ud)
  152. {
  153. unsigned int port;
  154. CALL(new_spawn_server(1, fn, ud, &port));
  155. return do_connect(sock, localhost, port);
  156. }
  157. #endif
  158. static int resolve_numeric(void)
  159. {
  160. ne_sock_addr *addr = ne_addr_resolve("127.0.0.1", 0);
  161. ONV(ne_addr_result(addr),
  162. ("failed to resolve 127.0.0.1: %s",
  163. ne_addr_error(addr, buffer, sizeof buffer)));
  164. ONN("ne_addr_first returned NULL", ne_addr_first(addr) == NULL);
  165. ONN("ne_iaddr_print didn't return buffer",
  166. ne_iaddr_print(ne_addr_first(addr), buffer, sizeof buffer) != buffer);
  167. ONV(strcmp(buffer, "127.0.0.1"), ("ntop gave `%s' not 127.0.0.1", buffer));
  168. ne_addr_destroy(addr);
  169. return OK;
  170. }
  171. #if 0
  172. static int resolve_ipv6(void)
  173. {
  174. char err[256];
  175. ne_sock_addr *addr = ne_addr_resolve("[::1]", 0);
  176. ONV(ne_addr_result(addr),
  177. ("could not resolve `[::1]': %s",
  178. ne_addr_error(addr, err, sizeof err)));
  179. ne_addr_destroy(addr);
  180. return OK;
  181. }
  182. #endif
  183. static const unsigned char raw_127[4] = "\x7f\0\0\01", /* 127.0.0.1 */
  184. raw6_nuls[16] = /* :: */ "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
  185. #ifdef TEST_IPV6
  186. static const unsigned char
  187. raw6_cafe[16] = /* feed::cafe */ "\xfe\xed\0\0\0\0\0\0\0\0\0\0\0\0\xca\xfe",
  188. raw6_babe[16] = /* cafe:babe:: */ "\xca\xfe\xba\xbe\0\0\0\0\0\0\0\0\0\0\0\0";
  189. #endif
  190. /* Check the given inet addr is 127.0.0.1. */
  191. static int check_is_raw127(const ne_inet_addr *ia)
  192. {
  193. unsigned char raw[5];
  194. raw[4] = 'Z';
  195. ONN("bogus ne_iaddr_typeof return", ne_iaddr_typeof(ia) != ne_iaddr_ipv4);
  196. ONN("ne_iaddr_raw gave bad retval", ne_iaddr_raw(ia, raw) != raw);
  197. ONN("raw address mismatch", memcmp(raw, raw_127, 4) != 0);
  198. ONN("ne_iaddr_raw buffer overflow", raw[4] != 'Z');
  199. return OK;
  200. }
  201. static int addr_make_v4(void)
  202. {
  203. ne_inet_addr *ia;
  204. char pr[50];
  205. ia = ne_iaddr_make(ne_iaddr_ipv4, raw_127);
  206. ONN("ne_iaddr_make returned NULL", ia == NULL);
  207. ne_iaddr_print(ia, pr, sizeof pr);
  208. ONV(strcmp(pr, "127.0.0.1"), ("address was %s not 127.0.0.1", pr));
  209. CALL(check_is_raw127(ia));
  210. ne_iaddr_free(ia);
  211. return OK;
  212. }
  213. static int parse_v4(void)
  214. {
  215. ne_inet_addr *ia;
  216. ia = ne_iaddr_parse("127.0.0.1", ne_iaddr_ipv4);
  217. ONN("parse failed", ia == NULL);
  218. CALL(check_is_raw127(ia));
  219. ne_iaddr_free(ia);
  220. return OK;
  221. }
  222. static int addr_make_v6(void)
  223. {
  224. #ifdef TEST_IPV6
  225. struct {
  226. const unsigned char *addr;
  227. const char *rep;
  228. } as[] = {
  229. { raw6_cafe, "feed::cafe" },
  230. { raw6_babe, "cafe:babe::" },
  231. { raw6_nuls, "::" },
  232. { NULL, NULL }
  233. };
  234. int n;
  235. for (n = 0; as[n].rep != NULL; n++) {
  236. ne_inet_addr *ia = ne_iaddr_make(ne_iaddr_ipv6, as[n].addr);
  237. char pr[128];
  238. unsigned char raw[17];
  239. ONV(ia == NULL, ("could not make address for '%s'",
  240. as[n].rep));
  241. ne_iaddr_print(ia, pr, sizeof pr);
  242. ONV(strcmp(pr, as[n].rep),
  243. ("address %d was '%s' not '%s'", n, pr, as[n].rep));
  244. ONN("bogus ne_iaddr_typeof return", ne_iaddr_typeof(ia) != ne_iaddr_ipv6);
  245. raw[16] = 'Z';
  246. ONN("ne_iaddr_raw gave bad retval", ne_iaddr_raw(ia, raw) != raw);
  247. ONN("raw address mismatch", memcmp(raw, as[n].addr, 4) != 0);
  248. ONN("ne_iaddr_raw buffer overflow", raw[16] != 'Z');
  249. ne_iaddr_free(ia);
  250. ia = ne_iaddr_parse(as[n].rep, ne_iaddr_ipv6);
  251. ONV(ia == NULL, ("ne_iaddr_parse failed for %s", as[n].rep));
  252. ONN("bogus ne_iaddr_typeof return", ne_iaddr_typeof(ia) != ne_iaddr_ipv6);
  253. ONN("ne_iaddr_raw gave bad retval", ne_iaddr_raw(ia, raw) != raw);
  254. ONN("raw address mismatch", memcmp(raw, as[n].addr, 4) != 0);
  255. ONN("ne_iaddr_raw buffer overflow", raw[16] != 'Z');
  256. ne_iaddr_free(ia);
  257. }
  258. return OK;
  259. #else
  260. /* should fail when lacking IPv6 support. */
  261. ne_inet_addr *ia = ne_iaddr_make(ne_iaddr_ipv6, raw6_nuls);
  262. ONN("ne_iaddr_make did not return NULL", ia != NULL);
  263. ONN("ne_iaddr_parse did not return NULL", ne_iaddr_parse("127.0.0.1", ne_iaddr_ipv6));
  264. #endif
  265. return OK;
  266. }
  267. static const unsigned char raw_1234[] = "\x01\x02\x03\x04";
  268. static int addr_compare(void)
  269. {
  270. ne_inet_addr *ia1, *ia2;
  271. int ret;
  272. ia1 = ne_iaddr_make(ne_iaddr_ipv4, raw_127);
  273. ia2 = ne_iaddr_make(ne_iaddr_ipv4, raw_127);
  274. ONN("addr_make returned NULL", !ia1 || !ia2);
  275. ret = ne_iaddr_cmp(ia1, ia2);
  276. ONV(ret != 0, ("comparison of equal IPv4 addresses was %d", ret));
  277. ne_iaddr_free(ia2);
  278. ia2 = ne_iaddr_make(ne_iaddr_ipv4, raw_1234);
  279. ret = ne_iaddr_cmp(ia1, ia2);
  280. ONN("comparison of unequal IPv4 addresses was zero", ret == 0);
  281. #ifdef TEST_IPV6
  282. ne_iaddr_free(ia2);
  283. ia2 = ne_iaddr_make(ne_iaddr_ipv6, raw6_cafe);
  284. ONN("could not make IPv6 address", !ia2);
  285. ret = ne_iaddr_cmp(ia1, ia2);
  286. ONN("comparison of IPv4 and IPv6 addresses was zero", ret == 0);
  287. ne_iaddr_free(ia1);
  288. ia1 = ne_iaddr_make(ne_iaddr_ipv6, raw6_cafe);
  289. ret = ne_iaddr_cmp(ia1, ia2);
  290. ONN("comparison of equal IPv6 addresses was not zero", ret != 0);
  291. ne_iaddr_free(ia1);
  292. ia1 = ne_iaddr_make(ne_iaddr_ipv6, raw6_babe);
  293. ret = ne_iaddr_cmp(ia1, ia2);
  294. ONN("comparison of unequal IPv6 address was zero", ret == 0);
  295. #endif
  296. ne_iaddr_free(ia1);
  297. ne_iaddr_free(ia2);
  298. return OK;
  299. }
  300. static int addr_reverse(void)
  301. {
  302. ne_inet_addr *ia = ne_iaddr_make(ne_iaddr_ipv4, raw_127);
  303. char buf[128], *syshost = NULL;
  304. int match;
  305. #ifdef HAVE_GETHOSTNAME
  306. char host[128];
  307. if (gethostname(host, sizeof host) == 0) {
  308. syshost = host;
  309. }
  310. #endif
  311. ONN("ne_iaddr_make returned NULL", ia == NULL);
  312. ONN("reverse lookup for 127.0.0.1 failed",
  313. ne_iaddr_reverse(ia, buf, sizeof buf) != 0);
  314. NE_DEBUG(NE_DBG_SOCKET, "Reverse lookup for 127.0.0.1 => %s\n", buf);
  315. match = strcmp(buf, "localhost.localdomain") == 0
  316. || strcmp(buf, "localhost") == 0;
  317. if (!match && syshost)
  318. /* If the returned name has the system hostname as a prefix, that's
  319. * good enough. */
  320. match = strncmp(buf, syshost, strlen(syshost)) == 0;
  321. if (!match)
  322. t_warning("reverse lookup for 127.0.0.1 got '%s'", buf);
  323. ONN("reverse lookup for 127.0.0.1 got empty string", strlen(buf) == 0);
  324. ne_iaddr_free(ia);
  325. return OK;
  326. }
  327. static int addr_canonical(void)
  328. {
  329. ne_sock_addr *sa;
  330. const char *h;
  331. sa = ne_addr_resolve("localhost", NE_ADDR_CANON);
  332. ONN("could not resolve localhost", sa == NULL);
  333. h = ne_addr_canonical(sa);
  334. ONN("no canonical name for localhost", h == NULL);
  335. NE_DEBUG(NE_DBG_SOCKET, "canonical name: %s\n", h);
  336. ne_addr_destroy(sa);
  337. return OK;
  338. }
  339. static int just_connect(void)
  340. {
  341. ne_socket *sock;
  342. CALL(begin(&sock, serve_close, NULL));
  343. return close_and_wait(sock);
  344. }
  345. /* Connect to an address crafted using ne_iaddr_make rather than from
  346. * the resolver. */
  347. static int addr_connect(void)
  348. {
  349. ne_socket *sock = ne_sock_create();
  350. ne_inet_addr *ia;
  351. unsigned int port;
  352. ia = get_lh_inet_addr();
  353. ONN("ne_iaddr_make returned NULL", ia == NULL);
  354. CALL(new_spawn_server(1, serve_close, NULL, &port));
  355. ONN("could not connect", ne_sock_connect(sock, ia, port));
  356. ne_iaddr_free(ia);
  357. return close_and_wait(sock);
  358. }
  359. static int addr_peer(void)
  360. {
  361. ne_socket *sock = ne_sock_create();
  362. ne_inet_addr *ia, *ia2;
  363. unsigned int port = 9999, realport;
  364. int ret;
  365. ia = get_lh_inet_addr();
  366. ONN("ne_iaddr_make returned NULL", ia == NULL);
  367. CALL(new_spawn_server(1, serve_close, NULL, &realport));
  368. ONN("could not connect", ne_sock_connect(sock, ia, realport));
  369. ia2 = ne_sock_peer(sock, &port);
  370. ret = ne_iaddr_cmp(ia, ia2);
  371. ONV(ret != 0,
  372. ("comparison of peer with server address was %d", ret));
  373. ONV(port != realport, ("got peer port %u, expected %u", port, realport));
  374. ne_sock_close(sock);
  375. CALL(await_server());
  376. ne_iaddr_free(ia);
  377. ne_iaddr_free(ia2);
  378. return OK;
  379. }
  380. /* Exect a read() to return EOF */
  381. static int expect_close(ne_socket *sock)
  382. {
  383. ssize_t n = ne_sock_read(sock, buffer, 1);
  384. ONV(n > 0, ("read got %" NE_FMT_SSIZE_T " bytes not closure", n));
  385. ONV(n < 0 && n != NE_SOCK_CLOSED,
  386. ("read got error not closure: `%s'", ne_sock_error(sock)));
  387. return OK;
  388. }
  389. static int good_close(ne_socket *sock)
  390. {
  391. NE_DEBUG(NE_DBG_SOCKET, "Socket error was %s\n", ne_sock_error(sock));
  392. ONN("close failed", ne_sock_close(sock));
  393. return OK;
  394. }
  395. /* Finish a test, closing socket and rejoining child. If eof is non-zero,
  396. * expects to read EOF from the socket before closing. */
  397. static int finish(ne_socket *sock, int eof)
  398. {
  399. if (eof)
  400. CALL(expect_close(sock));
  401. else
  402. ne_sock_shutdown(sock, NE_SOCK_SEND);
  403. CALL(good_close(sock));
  404. return await_server();
  405. }
  406. /* Exect a ne_sock_peek() to return EOF */
  407. static int expect_peek_close(ne_socket *sock)
  408. {
  409. ssize_t n = ne_sock_read(sock, buffer, 1);
  410. ONV(n != NE_SOCK_CLOSED, ("peek gave %" NE_FMT_SSIZE_T " not closure", n));
  411. return OK;
  412. }
  413. /* Test that just does a connect then a close. */
  414. static int read_close(void)
  415. {
  416. ne_socket *sock;
  417. CALL(begin(&sock, serve_close, NULL));
  418. CALL(expect_close(sock));
  419. ONN("close failed", ne_sock_close(sock));
  420. return await_server();
  421. }
  422. /* Test that just does an open then a close. */
  423. static int open_close(void)
  424. {
  425. ONN("close of newly opened socket failed", ne_sock_close(ne_sock_create()));
  426. return OK;
  427. }
  428. /* Test that just does a connect then a close (but gets the close via
  429. * ne_sock_peek). */
  430. static int peek_close(void)
  431. {
  432. ne_socket *sock;
  433. CALL(begin(&sock, serve_close, NULL));
  434. CALL(expect_peek_close(sock));
  435. ONN("close failed", ne_sock_close(sock));
  436. return await_server();
  437. }
  438. /* Don't change this string. */
  439. #define STR "Hello, World."
  440. /* do a sock_peek() on sock for 'len' bytes, and expect 'str'. */
  441. static int peek_expect(ne_socket *sock, const char *str, size_t len)
  442. {
  443. ssize_t ret;
  444. memset(buffer, '@', sizeof buffer);
  445. ret = ne_sock_peek(sock, buffer, len);
  446. ONV((ssize_t)len != ret,
  447. ("peek got %" NE_FMT_SSIZE_T " bytes not %" NE_FMT_SIZE_T, ret, len));
  448. ONV(memcmp(str, buffer, len),
  449. ("peek mismatch: `%.*s' not `%.*s'",
  450. (int)len, buffer, (int)len, str));
  451. ONV(buffer[len] != '@',
  452. ("buffer overrun: %" NE_FMT_SSIZE_T "nth byte was '%c' not '@'",
  453. len, buffer[len]));
  454. return OK;
  455. }
  456. /* do a sock_read() on sock for 'len' bytes, and expect 'str'. */
  457. static int read_expect(ne_socket *sock, const char *str, size_t len)
  458. {
  459. ssize_t ret = ne_sock_read(sock, buffer, len);
  460. ONV((ssize_t)len != ret,
  461. ("read got %" NE_FMT_SSIZE_T " bytes (%s) not %" NE_FMT_SIZE_T,
  462. ret, ne_sock_error(sock), len));
  463. ONV(memcmp(str, buffer, len),
  464. ("read mismatch: `%.*s' not `%.*s'",
  465. (int)len, buffer, (int)len, str));
  466. return OK;
  467. }
  468. /* do a sock_read() on sock for 'len' bytes, and expect 'str'. */
  469. static int fullread_expect(ne_socket *sock, const char *str, size_t len)
  470. {
  471. ssize_t ret = ne_sock_fullread(sock, buffer, len);
  472. ONV(ret, ("fullread failed (%" NE_FMT_SSIZE_T "): %s",
  473. ret, ne_sock_error(sock)));
  474. ONV(memcmp(str, buffer, len),
  475. ("fullread mismatch: `%.*s' not `%.*s'",
  476. (int)len, buffer, (int)len, str));
  477. return OK;
  478. }
  479. #define FULLREAD(str) CALL(fullread_expect(sock, str, strlen(str)))
  480. /* Declare a struct string */
  481. #define DECL(var,str) struct string var = { str, 0 }; var.len = strlen(str)
  482. #define DECL_LONG(var,ch,n) struct string var; var.data = memset(ne_malloc(n), ch, n); var.len = n;
  483. /* Test a simple read. */
  484. static int single_read(void)
  485. {
  486. ne_socket *sock;
  487. DECL(hello, STR);
  488. CALL(begin(&sock, serve_sstring, &hello));
  489. CALL(read_expect(sock, STR, strlen(STR)));
  490. CALL(expect_close(sock));
  491. CALL(good_close(sock));
  492. return await_server();
  493. }
  494. /* Test a simple peek. */
  495. static int single_peek(void)
  496. {
  497. ne_socket *sock;
  498. DECL(hello, STR);
  499. CALL(begin(&sock, serve_sstring, &hello));
  500. CALL(peek_expect(sock, STR, strlen(STR)));
  501. return finish(sock, 0);
  502. }
  503. /* Test lots of 1-byte reads. */
  504. static int small_reads(void)
  505. {
  506. ne_socket *sock;
  507. char *pnt;
  508. DECL(hello, STR);
  509. CALL(begin(&sock, serve_sstring, &hello));
  510. /* read the string byte-by-byte. */
  511. for (pnt = hello.data; *pnt; pnt++) {
  512. CALL(read_expect(sock, pnt, 1));
  513. }
  514. return finish(sock, 1);
  515. }
  516. /* peek or read, expecting to get given string. */
  517. #define READ(str) CALL(read_expect(sock, str, strlen(str)))
  518. #define PEEK(str) CALL(peek_expect(sock, str, strlen(str)))
  519. /* Stress out the read buffer handling a little. */
  520. static int read_and_peek(void)
  521. {
  522. ne_socket *sock;
  523. DECL(hello, STR);
  524. CALL(begin(&sock, serve_sstring, &hello));
  525. PEEK("Hello");
  526. PEEK("Hell");
  527. PEEK(STR);
  528. READ("He");
  529. PEEK("llo, ");
  530. READ("l");
  531. PEEK("lo, World.");
  532. READ("lo, Worl");
  533. PEEK("d."); PEEK("d");
  534. READ("d.");
  535. return finish(sock, 1);
  536. }
  537. /* Read more bytes than were written. */
  538. static int larger_read(void)
  539. {
  540. ne_socket *sock;
  541. ssize_t nb;
  542. DECL(hello, STR);
  543. CALL(begin(&sock, serve_sstring, &hello));
  544. nb = ne_sock_read(sock, buffer, hello.len + 10);
  545. ONV(nb != (ssize_t)hello.len,
  546. ("read gave too many bytes (%" NE_FMT_SSIZE_T ")", nb));
  547. ONN("read gave wrong data", memcmp(buffer, hello.data, hello.len));
  548. return finish(sock, 1);
  549. }
  550. static int line_expect(ne_socket *sock, const char *line)
  551. {
  552. ssize_t ret = ne_sock_readline(sock, buffer, BUFSIZ);
  553. size_t len = strlen(line);
  554. NE_DEBUG(NE_DBG_SOCKET, " -> expected=%s -> actual=%s", line, buffer);
  555. ONV(ret == NE_SOCK_CLOSED, ("socket closed, expecting `%s'", line));
  556. ONV(ret < 0, ("socket error `%s', expecting `%s'",
  557. ne_sock_error(sock), line));
  558. ONV((size_t)ret != len || strcmp(line, buffer),
  559. ("readline mismatch: `%s' not `%s'", buffer, line));
  560. return OK;
  561. }
  562. #define LINE(x) CALL(line_expect(sock, x))
  563. #define STR2 "Goodbye, cruel world."
  564. static int line_simple(void)
  565. {
  566. ne_socket *sock;
  567. DECL(oneline, STR "\n" STR2 "\n");
  568. CALL(begin(&sock, serve_sstring, &oneline));
  569. LINE(STR "\n");
  570. LINE(STR2 "\n");
  571. return finish(sock, 1);
  572. }
  573. static int line_closure(void)
  574. {
  575. ne_socket *sock;
  576. ssize_t ret;
  577. DECL(oneline, STR "\n" "foobar");
  578. CALL(begin(&sock, serve_sstring, &oneline));
  579. LINE(STR "\n");
  580. ret = ne_sock_readline(sock, buffer, BUFSIZ);
  581. ONV(ret != NE_SOCK_CLOSED,
  582. ("readline got %" NE_FMT_SSIZE_T " not EOF: %s", ret,
  583. ne_sock_error(sock)));
  584. return finish(sock, 0);
  585. }
  586. /* check that empty lines are handled correctly. */
  587. static int line_empty(void)
  588. {
  589. ne_socket *sock;
  590. DECL(oneline, "\n\na\n\n");
  591. CALL(begin(&sock, serve_sstring, &oneline));
  592. LINE("\n"); LINE("\n");
  593. LINE("a\n"); LINE("\n");
  594. return finish(sock, 1);
  595. }
  596. static int line_toolong(void)
  597. {
  598. ne_socket *sock;
  599. ssize_t ret;
  600. DECL(oneline, "AAAAAA\n");
  601. CALL(begin(&sock, serve_sstring, &oneline));
  602. ret = ne_sock_readline(sock, buffer, 5);
  603. ONV(ret != NE_SOCK_ERROR,
  604. ("readline should fail on long line: %" NE_FMT_SSIZE_T, ret));
  605. reap_server();
  606. ne_sock_close(sock);
  607. return OK;
  608. }
  609. #define OVERLEN (9000)
  610. static int line_overflow(void)
  611. {
  612. ne_socket *sock;
  613. ssize_t ret;
  614. DECL_LONG(line, 'A', OVERLEN);
  615. CALL(begin(&sock, serve_sstring, &line));
  616. PEEK("A"); /* fill the read buffer */
  617. ret = ne_sock_readline(sock, buffer, OVERLEN);
  618. ONV(ret != NE_SOCK_ERROR,
  619. ("readline should fail on overlong line: %" NE_FMT_SSIZE_T, ret));
  620. ne_free(line.data);
  621. return finish(sock, 0);
  622. }
  623. /* readline()s mingled with other operations: buffering tests. */
  624. static int line_mingle(void)
  625. {
  626. ne_socket *sock;
  627. DECL(oneline, "alpha\nbeta\ndelta\ngamma\n");
  628. CALL(begin(&sock, serve_sstring, &oneline));
  629. READ("a"); LINE("lpha\n");
  630. READ("beta"); LINE("\n");
  631. PEEK("d"); PEEK("delt");
  632. LINE("delta\n");
  633. READ("gam"); LINE("ma\n");
  634. return finish(sock, 1);
  635. }
  636. /* readline which needs multiple read() calls. */
  637. static int line_chunked(void)
  638. {
  639. ne_socket *sock;
  640. DECL(oneline, "this is a line\n");
  641. CALL(begin(&sock, serve_sstring_slowly, &oneline));
  642. LINE("this is a line\n");
  643. return finish(sock, 1);
  644. }
  645. static int line_long_chunked(void)
  646. {
  647. ne_socket *sock;
  648. ssize_t ret;
  649. DECL_LONG(line, 'Z', OVERLEN);
  650. CALL(begin(&sock, serve_sstring_slowly, &line));
  651. FULLREAD("ZZZZZZZZ"); /* fill the buffer */
  652. ret = ne_sock_readline(sock, buffer, sizeof buffer);
  653. ONV(ret != NE_SOCK_ERROR,
  654. ("readline gave %" NE_FMT_SSIZE_T " not failure", ret));
  655. reap_server();
  656. ne_sock_close(sock);
  657. ne_free(line.data);
  658. return OK;
  659. }
  660. static time_t to_start, to_finish;
  661. static int to_begin(ne_socket **sock)
  662. {
  663. CALL(begin(sock, sleepy_server, NULL));
  664. ne_sock_read_timeout(*sock, 1);
  665. to_start = time(NULL);
  666. return OK;
  667. }
  668. static int to_end(ne_socket *sock)
  669. {
  670. to_finish = time(NULL);
  671. reap_server(); /* hopefully it's hung. */
  672. ONN("timeout ignored, or very slow machine", to_finish - to_start > 3);
  673. ONN("close failed", ne_sock_close(sock));
  674. return OK;
  675. }
  676. #define TO_BEGIN ne_socket *sock; CALL(to_begin(&sock))
  677. #define TO_OP(x) do { int to_ret = (x); \
  678. ONV(to_ret != NE_SOCK_TIMEOUT, ("operation did not timeout: got %d (%s)", to_ret, ne_sock_error(sock))); \
  679. } while (0)
  680. #define TO_FINISH return to_end(sock)
  681. #ifndef TEST_CONNECT_TIMEOUT
  682. #define TEST_CONNECT_TIMEOUT 0
  683. #endif
  684. #if TEST_CONNECT_TIMEOUT
  685. /* No obvious way to reliably test a connect() timeout. But
  686. * www.example.com seems to drop packets on ports other than 80 so
  687. * that actually works pretty well. Disabled by default. */
  688. static int connect_timeout(void)
  689. {
  690. static const unsigned char example_dot_com[] = "\xC0\x00\x22\xA6";
  691. ne_socket *sock = ne_sock_create();
  692. ne_inet_addr *ia = ne_iaddr_make(ne_iaddr_ipv4, example_dot_com);
  693. ne_sock_connect_timeout(sock, 1);
  694. TO_OP(ne_sock_connect(sock, ia, 8080));
  695. ne_iaddr_free(ia);
  696. ne_sock_close(sock);
  697. return OK;
  698. }
  699. #endif
  700. static int peek_timeout(void)
  701. {
  702. TO_BEGIN;
  703. TO_OP(ne_sock_peek(sock, buffer, 1));
  704. TO_FINISH;
  705. }
  706. static int read_timeout(void)
  707. {
  708. TO_BEGIN;
  709. TO_OP(ne_sock_read(sock, buffer, 1));
  710. TO_FINISH;
  711. }
  712. static int readline_timeout(void)
  713. {
  714. TO_BEGIN;
  715. TO_OP(ne_sock_readline(sock, buffer, 1));
  716. TO_FINISH;
  717. }
  718. static int fullread_timeout(void)
  719. {
  720. TO_BEGIN;
  721. TO_OP(ne_sock_fullread(sock, buffer, 1));
  722. TO_FINISH;
  723. }
  724. static int serve_expect(ne_socket *sock, void *ud)
  725. {
  726. struct string *str = ud;
  727. ssize_t ret;
  728. while (str->len &&
  729. (ret = ne_sock_read(sock, buffer, sizeof(buffer))) > 0) {
  730. NE_DEBUG(NE_DBG_SOCKET, "Got %" NE_FMT_SSIZE_T " bytes.\n", ret);
  731. ONV(memcmp(str->data, buffer, ret),
  732. ("unexpected data: [%.*s] not [%.*s]",
  733. (int)ret, buffer, (int)ret, str->data));
  734. str->data += ret;
  735. str->len -= ret;
  736. NE_DEBUG(NE_DBG_SOCKET, "%" NE_FMT_SIZE_T " bytes left.\n", str->len);
  737. }
  738. NE_DEBUG(NE_DBG_SOCKET, "All data read.\n");
  739. return OK;
  740. }
  741. #define WRITEL(str) CALL(full_write(sock, str, strlen(str))); \
  742. minisleep()
  743. static int small_writes(void)
  744. {
  745. ne_socket *sock;
  746. DECL(str, "This\nIs\nSome\nText.\n");
  747. CALL(begin(&sock, serve_expect, &str));
  748. WRITEL("This\n"); WRITEL("Is\n"); WRITEL("Some\n"); WRITEL("Text.\n");
  749. return finish(sock, 1);
  750. }
  751. static int large_writes(void)
  752. {
  753. #define LARGE_SIZE (123456)
  754. struct string str;
  755. ne_socket *sock;
  756. ssize_t n;
  757. str.data = ne_malloc(LARGE_SIZE);
  758. str.len = LARGE_SIZE;
  759. for (n = 0; n < LARGE_SIZE; n++)
  760. str.data[n] = 41 + n % 130;
  761. CALL(begin(&sock, serve_expect, &str));
  762. CALL(full_write(sock, str.data, str.len));
  763. ne_free(str.data);
  764. return finish(sock, 1);
  765. }
  766. static int full_writev(ne_socket *sock, struct ne_iovec *vec, int count)
  767. {
  768. int ret = ne_sock_fullwritev(sock, vec, count);
  769. NE_DEBUG(NE_DBG_SOCKET, "wrote vector (%d)\n", count);
  770. ONV(ret, ("writev failed (%d): %s", ret, ne_sock_error(sock)));
  771. return OK;
  772. }
  773. #undef LARGE_SIZE
  774. #define LARGE_SIZE (123456 * 4)
  775. static int large_writev(void)
  776. {
  777. struct string str;
  778. ne_socket *sock;
  779. ssize_t n;
  780. struct ne_iovec vec[4];
  781. str.data = ne_malloc(LARGE_SIZE);
  782. str.len = LARGE_SIZE;
  783. for (n = 0; n < LARGE_SIZE; n++)
  784. str.data[n] = 41 + n % 130;
  785. for (n = 0; n < 4; n++) {
  786. vec[n].base = str.data + n * LARGE_SIZE / 4;
  787. vec[n].len = LARGE_SIZE / 4;
  788. }
  789. CALL(begin(&sock, serve_expect, &str));
  790. CALL(full_writev(sock, vec, 4));
  791. ne_free(str.data);
  792. return finish(sock, 1);
  793. }
  794. /* echoes back lines. */
  795. static int echo_server(ne_socket *sock, void *ud)
  796. {
  797. ssize_t ret;
  798. while ((ret = ne_sock_readline(sock, buffer, sizeof(buffer))) > 0) {
  799. NE_DEBUG(NE_DBG_SOCKET, "Line: %s", buffer);
  800. ONN("write failed", ne_sock_fullwrite(sock, buffer, ret));
  801. NE_DEBUG(NE_DBG_SOCKET, "Wrote line.\n");
  802. }
  803. ONV(ret != NE_SOCK_CLOSED, ("unexpected readline failure: %s",
  804. ne_sock_error(sock)));
  805. NE_DEBUG(NE_DBG_SOCKET, "ssl: Readline got closure\n");
  806. return 0;
  807. }
  808. static int echo_expect(ne_socket *sock, const char *line)
  809. {
  810. CALL(full_write(sock, line, strlen(line)));
  811. return line_expect(sock, line);
  812. }
  813. #define ECHO(line) CALL(echo_expect(sock, line))
  814. static int echo_lines(void)
  815. {
  816. ne_socket *sock;
  817. CALL(begin(&sock, echo_server, NULL));
  818. ECHO("hello,\n");
  819. ECHO("\n");
  820. ECHO("world\n");
  821. return finish(sock, 0);
  822. }
  823. #ifdef SOCKET_SSL
  824. static int serve_wait_close(ne_socket *sock, void *ud)
  825. {
  826. ONV(ne_sock_read(sock, buffer, 1) != NE_SOCK_CLOSED,
  827. ("failed waiting for TLS closure: %s", ne_sock_error(sock)));
  828. return 0;
  829. }
  830. static int ssl_shutdown(void)
  831. {
  832. ne_socket *sock;
  833. int ret;
  834. CALL(begin(&sock, serve_wait_close, NULL));
  835. ONV(ne_sock_shutdown(sock, NE_SOCK_RECV) != NE_SOCK_RETRY,
  836. ("TLS socket closed too early"));
  837. ret = ne_sock_shutdown(sock, NE_SOCK_SEND);
  838. if (ret == NE_SOCK_RETRY) {
  839. /* Wait for closure. */
  840. ret = ne_sock_read(sock, buffer, 0);
  841. ONV(ret != NE_SOCK_CLOSED,
  842. ("read for closure didn't get closure: %d/%s",
  843. ret, ne_sock_error(sock)));
  844. }
  845. else {
  846. ONV(ret, ("socket shutdown unexpected state: %d/%s",
  847. ret, ne_sock_error(sock)));
  848. }
  849. CALL(await_server());
  850. ne_sock_close(sock);
  851. return OK;
  852. }
  853. static int ssl_closure(void)
  854. {
  855. ne_socket *sock;
  856. ssize_t ret;
  857. CALL(begin(&sock, serve_close, NULL));
  858. CALL(full_write(sock, "a", 1));
  859. ne_sock_shutdown(sock, NE_SOCK_SEND);
  860. CALL(await_server());
  861. do {
  862. ret = ne_sock_fullwrite(sock, "a", 1);
  863. } while (ret == 0);
  864. ONV(ret != NE_SOCK_RESET && ret != NE_SOCK_CLOSED,
  865. ("write got %" NE_FMT_SSIZE_T " not reset or closure: %s", ret,
  866. ne_sock_error(sock)));
  867. ne_sock_close(sock);
  868. return OK;
  869. }
  870. static int serve_truncate(ne_socket *sock, void *userdata)
  871. {
  872. if (ne_sock_read(sock, buffer, 1) != 1)
  873. NE_DEBUG(NE_DBG_SOCKET, "serve_truncate failed to read a byte.\n");
  874. NE_DEBUG(NE_DBG_SOCKET, "read a byte, exiting...\n");
  875. exit(0);
  876. }
  877. /* when an EOF is received without a clean shutdown (close_notify
  878. message). */
  879. static int ssl_truncate(void)
  880. {
  881. ne_socket *sock; int ret;
  882. CALL(begin(&sock, serve_truncate, NULL));
  883. CALL(full_write(sock, "a", 1));
  884. ret = ne_sock_read(sock, buffer, 1);
  885. ONV(ret != NE_SOCK_TRUNC,
  886. ("socket got error %d not truncation: `%s'", ret,
  887. ne_sock_error(sock)));
  888. ne_sock_close(sock);
  889. CALL(await_server());
  890. return OK;
  891. }
  892. #else
  893. /* use W Richard Stevens' SO_LINGER trick to elicit a TCP RST */
  894. static int serve_reset(ne_socket *sock, void *ud)
  895. {
  896. ONV(ne_sock_read(sock, buffer, 1) != 1,
  897. ("socket read error `%s'", ne_sock_error(sock)));
  898. ONV(buffer[0] != 'R',
  899. ("got unexpected byte %c from client", buffer[0]));
  900. reset_socket(sock);
  901. exit(0);
  902. return 0;
  903. }
  904. static int write_reset(void)
  905. {
  906. ne_socket *sock;
  907. int ret;
  908. CALL(begin(&sock, serve_reset, NULL));
  909. CALL(full_write(sock, "R", 1));
  910. CALL(await_server());
  911. ret = ne_sock_fullwrite(sock, "a", 1);
  912. if (ret == 0) {
  913. ne_sock_close(sock);
  914. return SKIP;
  915. }
  916. if (ret == NE_SOCK_CLOSED) {
  917. t_warning("got EOF, failed to elicit TCP RST");
  918. } else {
  919. ONV(ret != NE_SOCK_RESET,
  920. ("write got %d not reset: %s", ret, ne_sock_error(sock)));
  921. }
  922. return good_close(sock);
  923. }
  924. static int read_reset(void)
  925. {
  926. ne_socket *sock;
  927. ssize_t ret;
  928. CALL(begin(&sock, serve_reset, NULL));
  929. CALL(full_write(sock, "R", 1));
  930. CALL(await_server());
  931. ret = ne_sock_read(sock, buffer, 1);
  932. if (ret == NE_SOCK_CLOSED) {
  933. ne_sock_close(sock);
  934. return SKIP;
  935. }
  936. ONV(ret != NE_SOCK_RESET,
  937. ("read got %" NE_FMT_SSIZE_T " not reset: %s", ret,
  938. ne_sock_error(sock)));
  939. return good_close(sock);
  940. }
  941. #endif
  942. static int expect_block_timeout(ne_socket *sock, int timeout, const char *msg)
  943. {
  944. int ret;
  945. NE_DEBUG(NE_DBG_SOCKET, "blocking for %d\n", timeout);
  946. ret = ne_sock_block(sock, timeout);
  947. ONV(ret != NE_SOCK_TIMEOUT, (
  948. "ne_sock_block got %d not timeout: %s", ret, msg));
  949. return OK;
  950. }
  951. static int blocking(void)
  952. {
  953. ne_socket *sock;
  954. int ret;
  955. CALL(begin(&sock, echo_server, NULL));
  956. CALL(expect_block_timeout(sock, 1, "with non-zero timeout"));
  957. WRITEL("Hello, world.\n");
  958. /* poll for data */
  959. do {
  960. ret = ne_sock_block(sock, 1);
  961. } while (ret == NE_SOCK_TIMEOUT);
  962. ONV(ret != 0, ("ne_sock_block never got data: %d", ret));
  963. PEEK("Hello,");
  964. ret = ne_sock_block(sock, 1);
  965. ONV(ret != 0, ("ne_sock_block failed after peek: %d", ret));
  966. LINE("Hello, world.\n");
  967. return finish(sock, 0);
  968. }
  969. static int block_timeout(void)
  970. {
  971. TO_BEGIN;
  972. TO_OP(ne_sock_block(sock, 1));
  973. TO_FINISH;
  974. }
  975. #ifndef SOCKET_SSL
  976. /* Waits for EOF from read-side and then sends "abcd". */
  977. static int serve_shutdown(ne_socket *sock, void *userdata)
  978. {
  979. ONV(ne_sock_read(sock, buffer, 1) != NE_SOCK_CLOSED,
  980. ("expected to get closure"));
  981. CALL(full_write(sock, "abcd", 4));
  982. return 0;
  983. }
  984. static int bidi(void)
  985. {
  986. ne_socket *sock;
  987. CALL(begin(&sock, serve_shutdown, NULL));
  988. CALL(expect_block_timeout(sock, 1, "read should timeout before closure"));
  989. ONV(ne_sock_shutdown(sock, NE_SOCK_SEND) != 0,
  990. ("shutdown failed: `%s'", ne_sock_error(sock)));
  991. FULLREAD("abcd");
  992. return finish(sock, 1);
  993. }
  994. #endif
  995. static int ssl_session_id(void)
  996. {
  997. ne_socket *sock;
  998. unsigned char buf[128];
  999. size_t len1;
  1000. CALL(begin(&sock, serve_close, NULL));
  1001. #ifdef SOCKET_SSL
  1002. len1 = 0;
  1003. ONN("retrieve session id length",
  1004. ne_sock_sessid(sock, NULL, &len1));
  1005. if (len1 == 0) {
  1006. /* recent versions of OpenSSL seem to do this, not sure
  1007. * why or whether it's bad. */
  1008. finish(sock, 1);
  1009. t_context("zero-length session ID, cannot test further");
  1010. return SKIP;
  1011. }
  1012. if (len1 < sizeof buf) {
  1013. buf[len1] = 'Z';
  1014. }
  1015. {
  1016. size_t len2;
  1017. len2 = sizeof buf;
  1018. ONN("could not retrieve session id",
  1019. ne_sock_sessid(sock, buf, &len2));
  1020. ONN("buffer size changed!?", len1 != len2);
  1021. }
  1022. ONN("buffer written past expected end",
  1023. len1 < sizeof buf && buf[len1] != 'Z');
  1024. /* Attempt retrieval into too-short buffer: */
  1025. len1 = 0;
  1026. ONN("success for buffer overflow case",
  1027. ne_sock_sessid(sock, buf, &len1) == 0);
  1028. #else
  1029. len1 = sizeof buf;
  1030. ONN("retrieved session id for non-SSL socket!?",
  1031. ne_sock_sessid(sock, buf, &len1) == 0);
  1032. #endif
  1033. ne_sock_close(sock);
  1034. return await_server();
  1035. }
  1036. static int serve_ppeer(ne_socket *sock, void *ud)
  1037. {
  1038. unsigned int port = 99999;
  1039. ne_inet_addr *ia = ne_sock_peer(sock, &port);
  1040. char buf[128], line[256];
  1041. if (ia == NULL)
  1042. ne_snprintf(line, sizeof line, "error: %s", ne_sock_error(sock));
  1043. else
  1044. ne_snprintf(line, sizeof line,
  1045. "%s@%u\n", ne_iaddr_print(ia, buf, sizeof buf),
  1046. port);
  1047. CALL(full_write(sock, line, strlen(line)));
  1048. ne_iaddr_free(ia);
  1049. return OK;
  1050. }
  1051. static int try_prebind(int addr, int port)
  1052. {
  1053. ne_socket *sock = ne_sock_create();
  1054. ne_inet_addr *ia;
  1055. char buf[128], line[256];
  1056. unsigned int srvport;
  1057. ia = get_lh_inet_addr();
  1058. ONN("ne_iaddr_make returned NULL", ia == NULL);
  1059. CALL(new_spawn_server(1, serve_ppeer, NULL, &srvport));
  1060. ne_sock_prebind(sock, addr ? ia : NULL, port ? 7778 : 0);
  1061. ONN("could not connect", ne_sock_connect(sock, ia, srvport));
  1062. ne_snprintf(line, sizeof line,
  1063. "%s@%d\n", ne_iaddr_print(ia, buf, sizeof buf),
  1064. 7778);
  1065. if (!port) {
  1066. /* Don't know what port will be chosen, so... */
  1067. ssize_t ret = ne_sock_readline(sock, buffer, BUFSIZ);
  1068. ONV(ret < 0, ("socket error `%s'", ne_sock_error(sock)));
  1069. ONV(strncmp(line, buffer, strchr(line, '@') - line) != 0,
  1070. ("bad address: '%s', expecting '%s'",
  1071. buffer, line));
  1072. }
  1073. else {
  1074. LINE(line);
  1075. }
  1076. ne_sock_close(sock);
  1077. CALL(await_server());
  1078. ne_iaddr_free(ia);
  1079. return OK;
  1080. }
  1081. static int prebind(void)
  1082. {
  1083. CALL(try_prebind(1, 0));
  1084. CALL(try_prebind(0, 1));
  1085. CALL(try_prebind(1, 1));
  1086. return OK;
  1087. }
  1088. static int serve_cipher(ne_socket *sock, void *ud)
  1089. {
  1090. char *ciph = ne_sock_cipher(sock);
  1091. char *s = ciph && strlen(ciph) ? ciph : "NULL";
  1092. CALL(full_write(sock, s, strlen(s)));
  1093. if (ciph) ne_free(ciph);
  1094. return OK;
  1095. }
  1096. static int cipher(void)
  1097. {
  1098. ne_socket *sock;
  1099. #ifdef SOCKET_SSL
  1100. char *ciph;
  1101. CALL(begin(&sock, serve_cipher, NULL));
  1102. ciph = ne_sock_cipher(sock);
  1103. ONN("NULL/empty cipher", ciph == NULL || strlen(ciph) == 0);
  1104. FULLREAD(ciph);
  1105. ne_free(ciph);
  1106. #else
  1107. CALL(begin(&sock, serve_cipher, NULL));
  1108. ONN("non-NULL cipher for non-SSL socket",
  1109. ne_sock_cipher(sock) != NULL);
  1110. FULLREAD("NULL");
  1111. #endif
  1112. return finish(sock, 1);
  1113. }
  1114. static int error(void)
  1115. {
  1116. ne_socket *sock = ne_sock_create();
  1117. ne_sock_set_error(sock, "%s:%s", "fish", "42");
  1118. ONCMP("fish:42", ne_sock_error(sock), "socket error", "set");
  1119. ne_sock_close(sock);
  1120. return OK;
  1121. }
  1122. static int begin_socks(ne_socket **sock, struct socks_server *srv,
  1123. server_fn server, void *userdata)
  1124. {
  1125. unsigned int port;
  1126. srv->server = server;
  1127. srv->userdata = userdata;
  1128. srv->say_hello = 1;
  1129. CALL(new_spawn_server(1, socks_server, srv, &port));
  1130. return do_connect(sock, localhost, port);
  1131. }
  1132. static int socks_proxy(void)
  1133. {
  1134. static const struct {
  1135. enum ne_sock_sversion version;
  1136. int addr;
  1137. const char *fqdn;
  1138. unsigned int port;
  1139. const char *username, *password;
  1140. } ts[] = {
  1141. { NE_SOCK_SOCKSV4, 4, NULL, 55555, NULL, NULL },
  1142. { NE_SOCK_SOCKSV4, 4, NULL, 55555, "foobar", NULL },
  1143. { NE_SOCK_SOCKSV4A, 0, "www.example.com", 55555, NULL, NULL },
  1144. { NE_SOCK_SOCKSV5, 0, "www.example.com", 55555, NULL, NULL },
  1145. { NE_SOCK_SOCKSV5, 4, NULL, 55555, NULL, NULL },
  1146. #ifdef TEST_IPV6
  1147. { NE_SOCK_SOCKSV5, 6, NULL, 55555, NULL, NULL },
  1148. #endif
  1149. { NE_SOCK_SOCKSV5, 0, "www.example.com", 55555, "norman", "foobar" }
  1150. };
  1151. unsigned n;
  1152. for (n = 0; n < sizeof(ts)/sizeof(ts[n]); n++) {
  1153. ne_socket *sock;
  1154. struct socks_server arg = {0};
  1155. int ret;
  1156. arg.version = ts[n].version;
  1157. arg.expect_port = ts[n].port;
  1158. if (ts[n].addr == 4)
  1159. arg.expect_addr = ne_iaddr_make(ne_iaddr_ipv4, raw_127);
  1160. #ifdef TEST_IPV6
  1161. else if (ts[n].addr == 6)
  1162. arg.expect_addr = ne_iaddr_make(ne_iaddr_ipv4, raw6_cafe);
  1163. #endif
  1164. else
  1165. arg.expect_fqdn = ts[n].fqdn;
  1166. arg.username = ts[n].username;
  1167. arg.password = ts[n].password;
  1168. CALL(begin_socks(&sock, &arg, echo_server, NULL));
  1169. ret = ne_sock_proxy(sock, ts[n].version, arg.expect_addr,
  1170. ts[n].fqdn, ts[n].port,
  1171. ts[n].username, ts[n].password);
  1172. ONV(ret, ("proxy connect #%u gave %d", n, ret));
  1173. FULLREAD("ok!\n");
  1174. ECHO("hello,\n");
  1175. ECHO("\n");
  1176. ECHO("world\n");
  1177. if (ts[n].addr)
  1178. ne_iaddr_free(arg.expect_addr);
  1179. CALL(finish(sock, 0));
  1180. }
  1181. return OK;
  1182. }
  1183. static int fail_socks(void)
  1184. {
  1185. static const struct {
  1186. enum ne_sock_sversion version;
  1187. enum socks_failure failure;
  1188. const char *expect;
  1189. const char *username, *password;
  1190. } ts[] = {
  1191. { NE_SOCK_SOCKSV5, fail_init_vers,
  1192. "Invalid version in proxy response", NULL, NULL },
  1193. { NE_SOCK_SOCKSV5, fail_init_trunc,
  1194. "Could not read initial response from proxy: Connection closed",
  1195. NULL, NULL },
  1196. { NE_SOCK_SOCKSV5, fail_init_close,
  1197. "Could not read initial response from proxy: Connection closed",
  1198. NULL, NULL },
  1199. { NE_SOCK_SOCKSV5, fail_no_auth,
  1200. "No acceptable authentication method",
  1201. NULL, NULL },
  1202. { NE_SOCK_SOCKSV5, fail_bogus_auth,
  1203. "Unexpected authentication method chosen",
  1204. NULL, NULL },
  1205. { NE_SOCK_SOCKSV5, fail_auth_close,
  1206. "Could not read login reply: Connection closed",
  1207. "foo", "bar" },
  1208. { NE_SOCK_SOCKSV5, fail_auth_denied,
  1209. "Authentication failed", "foo", "bar" }
  1210. };
  1211. unsigned n;
  1212. for (n = 0; n < sizeof(ts)/sizeof(ts[n]); n++) {
  1213. ne_socket *sock;
  1214. struct socks_server arg = {0};
  1215. int ret;
  1216. arg.version = ts[n].version;
  1217. arg.failure = ts[n].failure;
  1218. arg.expect_port = 5555;
  1219. arg.expect_addr = ne_iaddr_make(ne_iaddr_ipv4, raw_127);
  1220. arg.username = ts[n].username;
  1221. arg.password = ts[n].password;
  1222. CALL(begin_socks(&sock, &arg, echo_server, NULL));
  1223. ret = ne_sock_proxy(sock, ts[n].version, arg.expect_addr,
  1224. NULL, arg.expect_port,
  1225. ts[n].username, ts[n].password);
  1226. ONV(ret == 0,
  1227. ("proxy connect #%u succeeded, expected failure '%s'", n,
  1228. ts[n].expect));
  1229. if (ret != 0 && strstr(ne_sock_error(sock), ts[n].expect) == NULL) {
  1230. t_warning("proxy connect #%u got unexpected failure '%s', wanted '%s'",
  1231. n, ne_sock_error(sock), ts[n].expect);
  1232. }
  1233. ne_iaddr_free(arg.expect_addr);
  1234. CALL(finish(sock, 0));
  1235. }
  1236. return OK;
  1237. }
  1238. ne_test tests[] = {
  1239. T(multi_init),
  1240. T_LEAKY(resolve),
  1241. T(resolve_numeric),
  1242. #ifdef SOCKET_SSL
  1243. T_LEAKY(init_ssl),
  1244. #endif
  1245. T(addr_make_v4),
  1246. T(parse_v4),
  1247. T(addr_make_v6),
  1248. T(addr_compare),
  1249. T(addr_reverse),
  1250. T(just_connect),
  1251. T(addr_connect),
  1252. T(addr_peer),
  1253. T(addr_canonical),
  1254. T(read_close),
  1255. T(peek_close),
  1256. T(open_close),
  1257. T(single_read),
  1258. T(single_peek),
  1259. T(small_reads),
  1260. T(read_and_peek),
  1261. T(larger_read),
  1262. T(ssl_session_id),
  1263. T(cipher),
  1264. T(line_simple),
  1265. T(line_closure),
  1266. T(line_empty),
  1267. T(line_toolong),
  1268. T(line_overflow),
  1269. T(line_mingle),
  1270. T(line_chunked),
  1271. T(line_long_chunked),
  1272. T(small_writes),
  1273. T(large_writes),
  1274. T(large_writev),
  1275. T(echo_lines),
  1276. T(blocking),
  1277. T(prebind),
  1278. T(error),
  1279. #ifdef SOCKET_SSL
  1280. T(ssl_shutdown),
  1281. T(ssl_closure),
  1282. T(ssl_truncate),
  1283. #else
  1284. T(write_reset),
  1285. T(read_reset),
  1286. T(bidi),
  1287. #endif
  1288. #if TEST_CONNECT_TIMEOUT
  1289. T(connect_timeout),
  1290. #endif
  1291. T(read_timeout),
  1292. T(peek_timeout),
  1293. T(readline_timeout),
  1294. T(fullread_timeout),
  1295. T(block_timeout),
  1296. T(socks_proxy),
  1297. T(fail_socks),
  1298. T(NULL)
  1299. };