socket.c 42 KB

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