socket.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707
  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, echo_server, NULL));
  1010. ECHO("hello, session ID test case!\n");
  1011. #ifdef SOCKET_SSL
  1012. len1 = 0;
  1013. ONN("retrieve session id length",
  1014. ne_sock_sessid(sock, NULL, &len1));
  1015. if (len1 == 0) {
  1016. /* recent versions of OpenSSL seem to do this, not sure
  1017. * why or whether it's bad. */
  1018. finish(sock, 1);
  1019. t_context("zero-length session ID, cannot test further");
  1020. return SKIP;
  1021. }
  1022. if (len1 < sizeof buf) {
  1023. buf[len1] = 'Z';
  1024. }
  1025. {
  1026. size_t len2;
  1027. len2 = sizeof buf;
  1028. ONN("could not retrieve session id",
  1029. ne_sock_sessid(sock, buf, &len2));
  1030. ONN("buffer size changed!?", len1 != len2);
  1031. }
  1032. ONN("buffer written past expected end",
  1033. len1 < sizeof buf && buf[len1] != 'Z');
  1034. /* Attempt retrieval into too-short buffer: */
  1035. len1 = 0;
  1036. ONN("success for buffer overflow case",
  1037. ne_sock_sessid(sock, buf, &len1) == 0);
  1038. #else
  1039. len1 = sizeof buf;
  1040. ONN("retrieved session id for non-SSL socket!?",
  1041. ne_sock_sessid(sock, buf, &len1) == 0);
  1042. #endif
  1043. ne_sock_close(sock);
  1044. return await_server();
  1045. }
  1046. static int serve_ppeer(ne_socket *sock, void *ud)
  1047. {
  1048. unsigned int port = 99999;
  1049. ne_inet_addr *ia = ne_sock_peer(sock, &port);
  1050. char buf[128], line[256];
  1051. if (ia == NULL)
  1052. ne_snprintf(line, sizeof line, "error: %s", ne_sock_error(sock));
  1053. else
  1054. ne_snprintf(line, sizeof line,
  1055. "%s@%u\n", ne_iaddr_print(ia, buf, sizeof buf),
  1056. port);
  1057. CALL(full_write(sock, line, strlen(line)));
  1058. ne_iaddr_free(ia);
  1059. return OK;
  1060. }
  1061. static int try_prebind(int addr, int port)
  1062. {
  1063. ne_socket *sock = ne_sock_create();
  1064. ne_inet_addr *ia;
  1065. char buf[128], line[256];
  1066. unsigned int srvport;
  1067. ia = get_lh_inet_addr();
  1068. ONN("ne_iaddr_make returned NULL", ia == NULL);
  1069. CALL(new_spawn_server(1, serve_ppeer, NULL, &srvport));
  1070. ne_sock_prebind(sock, addr ? ia : NULL, port ? 7778 : 0);
  1071. ONN("could not connect", ne_sock_connect(sock, ia, srvport));
  1072. ne_snprintf(line, sizeof line,
  1073. "%s@%d\n", ne_iaddr_print(ia, buf, sizeof buf),
  1074. 7778);
  1075. if (!port) {
  1076. /* Don't know what port will be chosen, so... */
  1077. ssize_t ret = ne_sock_readline(sock, buffer, BUFSIZ);
  1078. ONV(ret < 0, ("socket error `%s'", ne_sock_error(sock)));
  1079. ONV(strncmp(line, buffer, strchr(line, '@') - line) != 0,
  1080. ("bad address: '%s', expecting '%s'",
  1081. buffer, line));
  1082. }
  1083. else {
  1084. LINE(line);
  1085. }
  1086. ne_sock_close(sock);
  1087. CALL(await_server());
  1088. ne_iaddr_free(ia);
  1089. return OK;
  1090. }
  1091. static int prebind(void)
  1092. {
  1093. CALL(try_prebind(1, 0));
  1094. CALL(try_prebind(0, 1));
  1095. CALL(try_prebind(1, 1));
  1096. return OK;
  1097. }
  1098. static int serve_cipher(ne_socket *sock, void *ud)
  1099. {
  1100. char *ciph = ne_sock_cipher(sock);
  1101. char *s = ciph && strlen(ciph) ? ciph : "NULL";
  1102. CALL(full_write(sock, s, strlen(s)));
  1103. if (ciph) ne_free(ciph);
  1104. return OK;
  1105. }
  1106. static int cipher(void)
  1107. {
  1108. ne_socket *sock;
  1109. #ifdef SOCKET_SSL
  1110. char *ciph;
  1111. CALL(begin(&sock, serve_cipher, NULL));
  1112. ciph = ne_sock_cipher(sock);
  1113. ONN("NULL/empty cipher", ciph == NULL || strlen(ciph) == 0);
  1114. FULLREAD(ciph);
  1115. ne_free(ciph);
  1116. #else
  1117. CALL(begin(&sock, serve_cipher, NULL));
  1118. ONN("non-NULL cipher for non-SSL socket",
  1119. ne_sock_cipher(sock) != NULL);
  1120. FULLREAD("NULL");
  1121. #endif
  1122. return finish(sock, 1);
  1123. }
  1124. static int protocols(void)
  1125. {
  1126. ne_socket *sock;
  1127. enum ne_ssl_protocol proto;
  1128. CALL(begin(&sock, serve_close, NULL));
  1129. proto = ne_sock_getproto(sock);
  1130. #ifdef SOCKET_SSL
  1131. ONN("protocol should be set", proto == NE_SSL_PROTO_UNSPEC);
  1132. #else
  1133. ONN("protocol should be unset", proto != NE_SSL_PROTO_UNSPEC);
  1134. #endif
  1135. return finish(sock, 1);
  1136. }
  1137. #ifdef SOCKET_SSL
  1138. static int serve_protocol(ne_socket *sock, void *ud)
  1139. {
  1140. enum ne_ssl_protocol proto = ne_sock_getproto(sock);
  1141. const char *name = ne_ssl_proto_name(proto);
  1142. WRITEL("protocol\n");
  1143. WRITEL(name);
  1144. WRITEL("\n");
  1145. return 0;
  1146. }
  1147. static int proto_tls12(void)
  1148. {
  1149. ne_socket *sock;
  1150. enum ne_ssl_protocol proto;
  1151. CALL(beginc(&sock, client_ctx_tls12, serve_protocol, NULL));
  1152. proto = ne_sock_getproto(sock);
  1153. NE_DEBUG(NE_DBG_SOCKET, "protocol: %d\n", proto);
  1154. FULLREAD("protocol\n");
  1155. FULLREAD("TLSv1.2\n");
  1156. return finish(sock, 1);
  1157. }
  1158. #endif
  1159. static int error(void)
  1160. {
  1161. ne_socket *sock = ne_sock_create();
  1162. ne_sock_set_error(sock, "%s:%s", "fish", "42");
  1163. ONCMP("fish:42", ne_sock_error(sock), "socket error", "set");
  1164. ne_sock_close(sock);
  1165. return OK;
  1166. }
  1167. static int begin_socks(ne_socket **sock, struct socks_server *srv,
  1168. server_fn server, void *userdata)
  1169. {
  1170. unsigned int port;
  1171. srv->server = server;
  1172. srv->userdata = userdata;
  1173. srv->say_hello = 1;
  1174. CALL(new_spawn_server(1, socks_server, srv, &port));
  1175. return do_connect(sock, localhost, port);
  1176. }
  1177. static int socks_proxy(void)
  1178. {
  1179. static const struct {
  1180. enum ne_sock_sversion version;
  1181. int addr;
  1182. const char *fqdn;
  1183. unsigned int port;
  1184. const char *username, *password;
  1185. } ts[] = {
  1186. { NE_SOCK_SOCKSV4, 4, NULL, 55555, NULL, NULL },
  1187. { NE_SOCK_SOCKSV4, 4, NULL, 55555, "foobar", NULL },
  1188. { NE_SOCK_SOCKSV4A, 0, "www.example.com", 55555, NULL, NULL },
  1189. { NE_SOCK_SOCKSV5, 0, "www.example.com", 55555, NULL, NULL },
  1190. { NE_SOCK_SOCKSV5, 4, NULL, 55555, NULL, NULL },
  1191. #ifdef TEST_IPV6
  1192. { NE_SOCK_SOCKSV5, 6, NULL, 55555, NULL, NULL },
  1193. #endif
  1194. { NE_SOCK_SOCKSV5, 0, "www.example.com", 55555, "norman", "foobar" }
  1195. };
  1196. unsigned n;
  1197. for (n = 0; n < sizeof(ts)/sizeof(ts[n]); n++) {
  1198. ne_socket *sock;
  1199. struct socks_server arg = {0};
  1200. int ret;
  1201. arg.version = ts[n].version;
  1202. arg.expect_port = ts[n].port;
  1203. if (ts[n].addr == 4)
  1204. arg.expect_addr = ne_iaddr_make(ne_iaddr_ipv4, raw_127);
  1205. #ifdef TEST_IPV6
  1206. else if (ts[n].addr == 6)
  1207. arg.expect_addr = ne_iaddr_make(ne_iaddr_ipv4, raw6_cafe);
  1208. #endif
  1209. else
  1210. arg.expect_fqdn = ts[n].fqdn;
  1211. arg.username = ts[n].username;
  1212. arg.password = ts[n].password;
  1213. CALL(begin_socks(&sock, &arg, echo_server, NULL));
  1214. ret = ne_sock_proxy(sock, ts[n].version, arg.expect_addr,
  1215. ts[n].fqdn, ts[n].port,
  1216. ts[n].username, ts[n].password);
  1217. ONV(ret, ("proxy connect #%u gave %d", n, ret));
  1218. FULLREAD("ok!\n");
  1219. ECHO("hello,\n");
  1220. ECHO("\n");
  1221. ECHO("world\n");
  1222. if (ts[n].addr)
  1223. ne_iaddr_free(arg.expect_addr);
  1224. CALL(finish(sock, 0));
  1225. }
  1226. return OK;
  1227. }
  1228. static int fail_socks(void)
  1229. {
  1230. static const struct {
  1231. enum ne_sock_sversion version;
  1232. enum socks_failure failure;
  1233. const char *expect;
  1234. const char *username, *password;
  1235. } ts[] = {
  1236. { NE_SOCK_SOCKSV5, fail_init_vers,
  1237. "Invalid version in proxy response", NULL, NULL },
  1238. { NE_SOCK_SOCKSV5, fail_init_trunc,
  1239. "Could not read initial response from proxy: Connection closed",
  1240. NULL, NULL },
  1241. { NE_SOCK_SOCKSV5, fail_init_close,
  1242. "Could not read initial response from proxy: Connection closed",
  1243. NULL, NULL },
  1244. { NE_SOCK_SOCKSV5, fail_no_auth,
  1245. "No acceptable authentication method",
  1246. NULL, NULL },
  1247. { NE_SOCK_SOCKSV5, fail_bogus_auth,
  1248. "Unexpected authentication method chosen",
  1249. NULL, NULL },
  1250. { NE_SOCK_SOCKSV5, fail_auth_close,
  1251. "Could not read login reply: Connection closed",
  1252. "foo", "bar" },
  1253. { NE_SOCK_SOCKSV5, fail_auth_denied,
  1254. "Authentication failed", "foo", "bar" }
  1255. };
  1256. unsigned n;
  1257. for (n = 0; n < sizeof(ts)/sizeof(ts[n]); n++) {
  1258. ne_socket *sock;
  1259. struct socks_server arg = {0};
  1260. int ret;
  1261. arg.version = ts[n].version;
  1262. arg.failure = ts[n].failure;
  1263. arg.expect_port = 5555;
  1264. arg.expect_addr = ne_iaddr_make(ne_iaddr_ipv4, raw_127);
  1265. arg.username = ts[n].username;
  1266. arg.password = ts[n].password;
  1267. CALL(begin_socks(&sock, &arg, echo_server, NULL));
  1268. ret = ne_sock_proxy(sock, ts[n].version, arg.expect_addr,
  1269. NULL, arg.expect_port,
  1270. ts[n].username, ts[n].password);
  1271. ONV(ret == 0,
  1272. ("proxy connect #%u succeeded, expected failure '%s'", n,
  1273. ts[n].expect));
  1274. if (ret != 0 && strstr(ne_sock_error(sock), ts[n].expect) == NULL) {
  1275. t_warning("proxy connect #%u got unexpected failure '%s', wanted '%s'",
  1276. n, ne_sock_error(sock), ts[n].expect);
  1277. }
  1278. ne_iaddr_free(arg.expect_addr);
  1279. CALL(finish(sock, 0));
  1280. }
  1281. return OK;
  1282. }
  1283. static int scopes(void)
  1284. {
  1285. ne_inet_addr *ia;
  1286. #ifdef TEST_IPV6
  1287. ia = ne_iaddr_parse("fe80::cafe", ne_iaddr_ipv6);
  1288. #else
  1289. ia = ne_iaddr_parse("127.0.0.1", ne_iaddr_ipv4);
  1290. #endif
  1291. (void) ne_iaddr_set_scope(ia, "foobar");
  1292. (void) ne_iaddr_get_scope(ia);
  1293. ne_iaddr_free(ia);
  1294. return OK;
  1295. }
  1296. ne_test tests[] = {
  1297. T(multi_init),
  1298. T_LEAKY(resolve),
  1299. T(resolve_numeric),
  1300. #ifdef SOCKET_SSL
  1301. T_LEAKY(init_ssl),
  1302. #endif
  1303. T(addr_make_v4),
  1304. T(parse_v4),
  1305. T(addr_make_v6),
  1306. T(addr_compare),
  1307. T(addr_reverse),
  1308. T(just_connect),
  1309. T(addr_connect),
  1310. T(addr_peer),
  1311. T(addr_canonical),
  1312. T(read_close),
  1313. T(peek_close),
  1314. T(open_close),
  1315. T(single_read),
  1316. T(single_peek),
  1317. T(small_reads),
  1318. T(read_and_peek),
  1319. T(larger_read),
  1320. T(ssl_session_id),
  1321. T(cipher),
  1322. T(protocols),
  1323. #ifdef SOCKET_SSL
  1324. T(proto_tls12),
  1325. #endif
  1326. T(line_simple),
  1327. T(line_closure),
  1328. T(line_empty),
  1329. T(line_toolong),
  1330. T(line_overflow),
  1331. T(line_mingle),
  1332. T(line_chunked),
  1333. T(line_long_chunked),
  1334. T(small_writes),
  1335. T(large_writes),
  1336. T(large_writev),
  1337. T(echo_lines),
  1338. T(blocking),
  1339. T(prebind),
  1340. T(error),
  1341. #ifdef SOCKET_SSL
  1342. T(ssl_shutdown),
  1343. T(ssl_closure),
  1344. T(ssl_truncate),
  1345. #else
  1346. T(write_reset),
  1347. T(read_reset),
  1348. T(bidi),
  1349. #endif
  1350. #if TEST_CONNECT_TIMEOUT
  1351. T(connect_timeout),
  1352. #endif
  1353. T(read_timeout),
  1354. T(peek_timeout),
  1355. T(readline_timeout),
  1356. T(fullread_timeout),
  1357. T(block_timeout),
  1358. T(socks_proxy),
  1359. T(fail_socks),
  1360. T(scopes),
  1361. T(NULL)
  1362. };