bio_dgram_test.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. /*
  2. * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <string.h>
  10. #include <openssl/bio.h>
  11. #include <openssl/rand.h>
  12. #include "testutil.h"
  13. #include "internal/sockets.h"
  14. #if !defined(OPENSSL_NO_DGRAM) && !defined(OPENSSL_NO_SOCK)
  15. static int compare_addr(const BIO_ADDR *a, const BIO_ADDR *b)
  16. {
  17. struct in_addr xa, xb;
  18. #if OPENSSL_USE_IPV6
  19. struct in6_addr xa6, xb6;
  20. #endif
  21. void *pa, *pb;
  22. size_t slen, tmplen;
  23. if (BIO_ADDR_family(a) != BIO_ADDR_family(b))
  24. return 0;
  25. if (BIO_ADDR_family(a) == AF_INET) {
  26. pa = &xa;
  27. pb = &xb;
  28. slen = sizeof(xa);
  29. }
  30. #if OPENSSL_USE_IPV6
  31. else if (BIO_ADDR_family(a) == AF_INET6) {
  32. pa = &xa6;
  33. pb = &xb6;
  34. slen = sizeof(xa6);
  35. }
  36. #endif
  37. else {
  38. return 0;
  39. }
  40. tmplen = slen;
  41. if (!TEST_int_eq(BIO_ADDR_rawaddress(a, pa, &tmplen), 1))
  42. return 0;
  43. tmplen = slen;
  44. if (!TEST_int_eq(BIO_ADDR_rawaddress(b, pb, &tmplen), 1))
  45. return 0;
  46. if (!TEST_mem_eq(pa, slen, pb, slen))
  47. return 0;
  48. if (!TEST_int_eq(BIO_ADDR_rawport(a), BIO_ADDR_rawport(b)))
  49. return 0;
  50. return 1;
  51. }
  52. static int do_sendmmsg(BIO *b, BIO_MSG *msg,
  53. size_t num_msg, uint64_t flags,
  54. size_t *num_processed)
  55. {
  56. size_t done;
  57. for (done = 0; done < num_msg; ) {
  58. if (!BIO_sendmmsg(b, msg + done, sizeof(BIO_MSG),
  59. num_msg - done, flags, num_processed))
  60. return 0;
  61. done += *num_processed;
  62. }
  63. *num_processed = done;
  64. return 1;
  65. }
  66. static int do_recvmmsg(BIO *b, BIO_MSG *msg,
  67. size_t num_msg, uint64_t flags,
  68. size_t *num_processed)
  69. {
  70. size_t done;
  71. for (done = 0; done < num_msg; ) {
  72. if (!BIO_recvmmsg(b, msg + done, sizeof(BIO_MSG),
  73. num_msg - done, flags, num_processed))
  74. return 0;
  75. done += *num_processed;
  76. }
  77. *num_processed = done;
  78. return 1;
  79. }
  80. static int test_bio_dgram_impl(int af, int use_local)
  81. {
  82. int testresult = 0;
  83. BIO *b1 = NULL, *b2 = NULL;
  84. int fd1 = -1, fd2 = -1;
  85. BIO_ADDR *addr1 = NULL, *addr2 = NULL, *addr3 = NULL, *addr4 = NULL,
  86. *addr5 = NULL, *addr6 = NULL;
  87. struct in_addr ina;
  88. #if OPENSSL_USE_IPV6
  89. struct in6_addr ina6;
  90. #endif
  91. void *pina;
  92. size_t inal, i;
  93. union BIO_sock_info_u info1 = {0}, info2 = {0};
  94. char rx_buf[128], rx_buf2[128];
  95. BIO_MSG tx_msg[128], rx_msg[128];
  96. char tx_buf[128];
  97. size_t num_processed = 0;
  98. if (af == AF_INET) {
  99. TEST_info("# Testing with AF_INET, local=%d\n", use_local);
  100. pina = &ina;
  101. inal = sizeof(ina);
  102. }
  103. #if OPENSSL_USE_IPV6
  104. else if (af == AF_INET6) {
  105. TEST_info("# Testing with AF_INET6, local=%d\n", use_local);
  106. pina = &ina6;
  107. inal = sizeof(ina6);
  108. }
  109. #endif
  110. else {
  111. goto err;
  112. }
  113. memset(pina, 0, inal);
  114. ina.s_addr = htonl(0x7f000001UL);
  115. #if OPENSSL_USE_IPV6
  116. ina6.s6_addr[15] = 1;
  117. #endif
  118. addr1 = BIO_ADDR_new();
  119. if (!TEST_ptr(addr1))
  120. goto err;
  121. addr2 = BIO_ADDR_new();
  122. if (!TEST_ptr(addr2))
  123. goto err;
  124. addr3 = BIO_ADDR_new();
  125. if (!TEST_ptr(addr3))
  126. goto err;
  127. addr4 = BIO_ADDR_new();
  128. if (!TEST_ptr(addr4))
  129. goto err;
  130. addr5 = BIO_ADDR_new();
  131. if (!TEST_ptr(addr5))
  132. goto err;
  133. addr6 = BIO_ADDR_new();
  134. if (!TEST_ptr(addr6))
  135. goto err;
  136. if (!TEST_int_eq(BIO_ADDR_rawmake(addr1, af, pina, inal, 0), 1))
  137. goto err;
  138. if (!TEST_int_eq(BIO_ADDR_rawmake(addr2, af, pina, inal, 0), 1))
  139. goto err;
  140. fd1 = BIO_socket(af, SOCK_DGRAM, IPPROTO_UDP, 0);
  141. if (!TEST_int_ge(fd1, 0))
  142. goto err;
  143. fd2 = BIO_socket(af, SOCK_DGRAM, IPPROTO_UDP, 0);
  144. if (!TEST_int_ge(fd2, 0))
  145. goto err;
  146. if (!TEST_int_gt(BIO_bind(fd1, addr1, 0), 0))
  147. goto err;
  148. if (!TEST_int_gt(BIO_bind(fd2, addr2, 0), 0))
  149. goto err;
  150. info1.addr = addr1;
  151. if (!TEST_int_gt(BIO_sock_info(fd1, BIO_SOCK_INFO_ADDRESS, &info1), 0))
  152. goto err;
  153. info2.addr = addr2;
  154. if (!TEST_int_gt(BIO_sock_info(fd2, BIO_SOCK_INFO_ADDRESS, &info2), 0))
  155. goto err;
  156. if (!TEST_int_gt(BIO_ADDR_rawport(addr1), 0))
  157. goto err;
  158. if (!TEST_int_gt(BIO_ADDR_rawport(addr2), 0))
  159. goto err;
  160. b1 = BIO_new_dgram(fd1, 0);
  161. if (!TEST_ptr(b1))
  162. goto err;
  163. b2 = BIO_new_dgram(fd2, 0);
  164. if (!TEST_ptr(b2))
  165. goto err;
  166. if (!TEST_int_gt(BIO_dgram_set_peer(b1, addr2), 0))
  167. goto err;
  168. if (!TEST_int_gt(BIO_write(b1, "hello", 5), 0))
  169. goto err;
  170. /* Receiving automatically sets peer as source addr */
  171. if (!TEST_int_eq(BIO_read(b2, rx_buf, sizeof(rx_buf)), 5))
  172. goto err;
  173. if (!TEST_mem_eq(rx_buf, 5, "hello", 5))
  174. goto err;
  175. if (!TEST_int_gt(BIO_dgram_get_peer(b2, addr3), 0))
  176. goto err;
  177. if (!TEST_int_eq(compare_addr(addr3, addr1), 1))
  178. goto err;
  179. /* Clear peer */
  180. if (!TEST_int_gt(BIO_ADDR_rawmake(addr3, af, pina, inal, 0), 0))
  181. goto err;
  182. if (!TEST_int_gt(BIO_dgram_set_peer(b1, addr3), 0))
  183. goto err;
  184. if (!TEST_int_gt(BIO_dgram_set_peer(b2, addr3), 0))
  185. goto err;
  186. /* Now test using sendmmsg/recvmmsg with no peer set */
  187. tx_msg[0].data = "apple";
  188. tx_msg[0].data_len = 5;
  189. tx_msg[0].peer = NULL;
  190. tx_msg[0].local = NULL;
  191. tx_msg[0].flags = 0;
  192. tx_msg[1].data = "orange";
  193. tx_msg[1].data_len = 6;
  194. tx_msg[1].peer = NULL;
  195. tx_msg[1].local = NULL;
  196. tx_msg[1].flags = 0;
  197. /* First effort should fail due to missing destination address */
  198. if (!TEST_false(do_sendmmsg(b1, tx_msg, 2, 0, &num_processed))
  199. || !TEST_size_t_eq(num_processed, 0))
  200. goto err;
  201. /*
  202. * Second effort should fail due to local being requested
  203. * when not enabled
  204. */
  205. tx_msg[0].peer = addr2;
  206. tx_msg[0].local = addr1;
  207. tx_msg[1].peer = addr2;
  208. tx_msg[1].local = addr1;
  209. if (!TEST_false(do_sendmmsg(b1, tx_msg, 2, 0, &num_processed)
  210. || !TEST_size_t_eq(num_processed, 0)))
  211. goto err;
  212. /* Enable local if we are using it */
  213. if (BIO_dgram_get_local_addr_cap(b1) > 0 && use_local) {
  214. if (!TEST_int_eq(BIO_dgram_set_local_addr_enable(b1, 1), 1))
  215. goto err;
  216. } else {
  217. tx_msg[0].local = NULL;
  218. tx_msg[1].local = NULL;
  219. use_local = 0;
  220. }
  221. /* Third effort should succeed */
  222. if (!TEST_true(do_sendmmsg(b1, tx_msg, 2, 0, &num_processed))
  223. || !TEST_size_t_eq(num_processed, 2))
  224. goto err;
  225. /* Now try receiving */
  226. rx_msg[0].data = rx_buf;
  227. rx_msg[0].data_len = sizeof(rx_buf);
  228. rx_msg[0].peer = addr3;
  229. rx_msg[0].local = addr4;
  230. rx_msg[0].flags = (1UL<<31); /* undefined flag, should be erased */
  231. memset(rx_buf, 0, sizeof(rx_buf));
  232. rx_msg[1].data = rx_buf2;
  233. rx_msg[1].data_len = sizeof(rx_buf2);
  234. rx_msg[1].peer = addr5;
  235. rx_msg[1].local = addr6;
  236. rx_msg[1].flags = (1UL<<31); /* undefined flag, should be erased */
  237. memset(rx_buf2, 0, sizeof(rx_buf2));
  238. /*
  239. * Should fail at first due to local being requested when not
  240. * enabled
  241. */
  242. if (!TEST_false(do_recvmmsg(b2, rx_msg, 2, 0, &num_processed))
  243. || !TEST_size_t_eq(num_processed, 0))
  244. goto err;
  245. /* Fields have not been modified */
  246. if (!TEST_int_eq((int)rx_msg[0].data_len, sizeof(rx_buf)))
  247. goto err;
  248. if (!TEST_int_eq((int)rx_msg[1].data_len, sizeof(rx_buf2)))
  249. goto err;
  250. if (!TEST_ulong_eq((unsigned long)rx_msg[0].flags, 1UL<<31))
  251. goto err;
  252. if (!TEST_ulong_eq((unsigned long)rx_msg[1].flags, 1UL<<31))
  253. goto err;
  254. /* Enable local if we are using it */
  255. if (BIO_dgram_get_local_addr_cap(b2) > 0 && use_local) {
  256. if (!TEST_int_eq(BIO_dgram_set_local_addr_enable(b2, 1), 1))
  257. goto err;
  258. } else {
  259. rx_msg[0].local = NULL;
  260. rx_msg[1].local = NULL;
  261. use_local = 0;
  262. }
  263. /* Do the receive. */
  264. if (!TEST_true(do_recvmmsg(b2, rx_msg, 2, 0, &num_processed))
  265. || !TEST_size_t_eq(num_processed, 2))
  266. goto err;
  267. /* data_len should have been updated correctly */
  268. if (!TEST_int_eq((int)rx_msg[0].data_len, 5))
  269. goto err;
  270. if (!TEST_int_eq((int)rx_msg[1].data_len, 6))
  271. goto err;
  272. /* flags should have been zeroed */
  273. if (!TEST_int_eq((int)rx_msg[0].flags, 0))
  274. goto err;
  275. if (!TEST_int_eq((int)rx_msg[1].flags, 0))
  276. goto err;
  277. /* peer address should match expected */
  278. if (!TEST_int_eq(compare_addr(addr3, addr1), 1))
  279. goto err;
  280. if (!TEST_int_eq(compare_addr(addr5, addr1), 1))
  281. goto err;
  282. /*
  283. * Do not test local address yet as some platforms do not reliably return
  284. * local addresses for messages queued for RX before local address support
  285. * was enabled. Instead, send some new messages and test they're received
  286. * with the correct local addresses.
  287. */
  288. if (!TEST_true(do_sendmmsg(b1, tx_msg, 2, 0, &num_processed))
  289. || !TEST_size_t_eq(num_processed, 2))
  290. goto err;
  291. /* Receive the messages. */
  292. rx_msg[0].data_len = sizeof(rx_buf);
  293. rx_msg[1].data_len = sizeof(rx_buf2);
  294. if (!TEST_true(do_recvmmsg(b2, rx_msg, 2, 0, &num_processed))
  295. || !TEST_size_t_eq(num_processed, 2))
  296. goto err;
  297. if (rx_msg[0].local != NULL) {
  298. /* If we are using local, it should match expected */
  299. if (!TEST_int_eq(compare_addr(addr4, addr2), 1))
  300. goto err;
  301. if (!TEST_int_eq(compare_addr(addr6, addr2), 1))
  302. goto err;
  303. }
  304. /*
  305. * Try sending more than can be handled in one sendmmsg call (when using the
  306. * sendmmsg implementation)
  307. */
  308. for (i = 0; i < OSSL_NELEM(tx_msg); ++i) {
  309. tx_buf[i] = (char)i;
  310. tx_msg[i].data = tx_buf + i;
  311. tx_msg[i].data_len = 1;
  312. tx_msg[i].peer = addr2;
  313. tx_msg[i].local = use_local ? addr1 : NULL;
  314. tx_msg[i].flags = 0;
  315. }
  316. if (!TEST_true(do_sendmmsg(b1, tx_msg, OSSL_NELEM(tx_msg), 0, &num_processed))
  317. || !TEST_size_t_eq(num_processed, OSSL_NELEM(tx_msg)))
  318. goto err;
  319. /*
  320. * Try receiving more than can be handled in one recvmmsg call (when using
  321. * the recvmmsg implementation)
  322. */
  323. for (i = 0; i < OSSL_NELEM(rx_msg); ++i) {
  324. rx_buf[i] = '\0';
  325. rx_msg[i].data = rx_buf + i;
  326. rx_msg[i].data_len = 1;
  327. rx_msg[i].peer = NULL;
  328. rx_msg[i].local = NULL;
  329. rx_msg[i].flags = 0;
  330. }
  331. if (!TEST_true(do_recvmmsg(b2, rx_msg, OSSL_NELEM(rx_msg), 0, &num_processed))
  332. || !TEST_size_t_eq(num_processed, OSSL_NELEM(rx_msg)))
  333. goto err;
  334. if (!TEST_mem_eq(tx_buf, OSSL_NELEM(tx_msg), rx_buf, OSSL_NELEM(tx_msg)))
  335. goto err;
  336. testresult = 1;
  337. err:
  338. BIO_free(b1);
  339. BIO_free(b2);
  340. if (fd1 >= 0)
  341. BIO_closesocket(fd1);
  342. if (fd2 >= 0)
  343. BIO_closesocket(fd2);
  344. BIO_ADDR_free(addr1);
  345. BIO_ADDR_free(addr2);
  346. BIO_ADDR_free(addr3);
  347. BIO_ADDR_free(addr4);
  348. BIO_ADDR_free(addr5);
  349. BIO_ADDR_free(addr6);
  350. return testresult;
  351. }
  352. struct bio_dgram_case {
  353. int af, local;
  354. };
  355. static const struct bio_dgram_case bio_dgram_cases[] = {
  356. /* Test without local */
  357. { AF_INET, 0 },
  358. #if OPENSSL_USE_IPV6
  359. { AF_INET6, 0 },
  360. #endif
  361. /* Test with local */
  362. { AF_INET, 1 },
  363. #if OPENSSL_USE_IPV6
  364. { AF_INET6, 1 }
  365. #endif
  366. };
  367. static int test_bio_dgram(int idx)
  368. {
  369. return test_bio_dgram_impl(bio_dgram_cases[idx].af,
  370. bio_dgram_cases[idx].local);
  371. }
  372. # if !defined(OPENSSL_NO_CHACHA)
  373. static int random_data(const uint32_t *key, uint8_t *data, size_t data_len, size_t offset)
  374. {
  375. int ret = 0, outl;
  376. EVP_CIPHER_CTX *ctx = NULL;
  377. EVP_CIPHER *cipher = NULL;
  378. static const uint8_t zeroes[2048];
  379. uint32_t counter[4] = {0};
  380. counter[0] = (uint32_t)offset;
  381. ctx = EVP_CIPHER_CTX_new();
  382. if (ctx == NULL)
  383. goto err;
  384. cipher = EVP_CIPHER_fetch(NULL, "ChaCha20", NULL);
  385. if (cipher == NULL)
  386. goto err;
  387. if (EVP_EncryptInit_ex2(ctx, cipher, (uint8_t *)key, (uint8_t *)counter, NULL) == 0)
  388. goto err;
  389. while (data_len > 0) {
  390. outl = data_len > sizeof(zeroes) ? (int)sizeof(zeroes) : (int)data_len;
  391. if (EVP_EncryptUpdate(ctx, data, &outl, zeroes, outl) != 1)
  392. goto err;
  393. data += outl;
  394. data_len -= outl;
  395. }
  396. ret = 1;
  397. err:
  398. EVP_CIPHER_CTX_free(ctx);
  399. EVP_CIPHER_free(cipher);
  400. return ret;
  401. }
  402. static int test_bio_dgram_pair(void)
  403. {
  404. int testresult = 0, blen, mtu1, mtu2, r;
  405. BIO *bio1 = NULL, *bio2 = NULL;
  406. uint8_t scratch[2048 + 4], scratch2[2048];
  407. uint32_t key[8];
  408. size_t i, num_dgram, num_processed = 0;
  409. BIO_MSG msgs[2], rmsgs[2];
  410. BIO_ADDR *addr1 = NULL, *addr2 = NULL, *addr3 = NULL, *addr4 = NULL;
  411. struct in_addr in_local;
  412. size_t total = 0;
  413. const uint32_t ref_caps = BIO_DGRAM_CAP_HANDLES_SRC_ADDR
  414. | BIO_DGRAM_CAP_HANDLES_DST_ADDR
  415. | BIO_DGRAM_CAP_PROVIDES_SRC_ADDR
  416. | BIO_DGRAM_CAP_PROVIDES_DST_ADDR;
  417. memset(msgs, 0, sizeof(msgs));
  418. memset(rmsgs, 0, sizeof(rmsgs));
  419. in_local.s_addr = ntohl(0x7f000001);
  420. for (i = 0; i < OSSL_NELEM(key); ++i)
  421. key[i] = test_random();
  422. if (!TEST_int_eq(BIO_new_bio_dgram_pair(&bio1, 0, &bio2, 0), 1))
  423. goto err;
  424. mtu1 = BIO_dgram_get_mtu(bio1);
  425. if (!TEST_int_ge(mtu1, 1280))
  426. goto err;
  427. mtu2 = BIO_dgram_get_mtu(bio2);
  428. if (!TEST_int_ge(mtu2, 1280))
  429. goto err;
  430. if (!TEST_int_eq(mtu1, mtu2))
  431. goto err;
  432. if (!TEST_int_le(mtu1, sizeof(scratch) - 4))
  433. goto err;
  434. for (i = 0;; ++i) {
  435. if (!TEST_int_eq(random_data(key, scratch, sizeof(scratch), i), 1))
  436. goto err;
  437. blen = ((*(uint32_t*)scratch) % mtu1) + 1;
  438. r = BIO_write(bio1, scratch + 4, blen);
  439. if (r == -1)
  440. break;
  441. if (!TEST_int_eq(r, blen))
  442. goto err;
  443. total += blen;
  444. if (!TEST_size_t_lt(total, 1 * 1024 * 1024))
  445. goto err;
  446. }
  447. /*
  448. * Should be able to fit at least 9 datagrams in default write buffer size
  449. * in worst case
  450. */
  451. if (!TEST_int_ge(i, 9))
  452. goto err;
  453. /* Check we read back the same data */
  454. num_dgram = i;
  455. for (i = 0; i < num_dgram; ++i) {
  456. if (!TEST_int_eq(random_data(key, scratch, sizeof(scratch), i), 1))
  457. goto err;
  458. blen = ((*(uint32_t*)scratch) % mtu1) + 1;
  459. r = BIO_read(bio2, scratch2, sizeof(scratch2));
  460. if (!TEST_int_eq(r, blen))
  461. goto err;
  462. if (!TEST_mem_eq(scratch + 4, blen, scratch2, blen))
  463. goto err;
  464. }
  465. /* Should now be out of data */
  466. if (!TEST_int_eq(BIO_read(bio2, scratch2, sizeof(scratch2)), -1))
  467. goto err;
  468. /* sendmmsg/recvmmsg */
  469. if (!TEST_int_eq(random_data(key, scratch, sizeof(scratch), 0), 1))
  470. goto err;
  471. msgs[0].data = scratch;
  472. msgs[0].data_len = 19;
  473. msgs[1].data = scratch + 19;
  474. msgs[1].data_len = 46;
  475. if (!TEST_true(BIO_sendmmsg(bio1, msgs, sizeof(BIO_MSG), OSSL_NELEM(msgs), 0,
  476. &num_processed))
  477. || !TEST_size_t_eq(num_processed, 2))
  478. goto err;
  479. rmsgs[0].data = scratch2;
  480. rmsgs[0].data_len = 64;
  481. rmsgs[1].data = scratch2 + 64;
  482. rmsgs[1].data_len = 64;
  483. if (!TEST_true(BIO_recvmmsg(bio2, rmsgs, sizeof(BIO_MSG), OSSL_NELEM(rmsgs), 0,
  484. &num_processed))
  485. || !TEST_size_t_eq(num_processed, 2))
  486. goto err;
  487. if (!TEST_mem_eq(rmsgs[0].data, rmsgs[0].data_len, scratch, 19))
  488. goto err;
  489. if (!TEST_mem_eq(rmsgs[1].data, rmsgs[1].data_len, scratch + 19, 46))
  490. goto err;
  491. /* sendmmsg/recvmmsg with peer */
  492. addr1 = BIO_ADDR_new();
  493. if (!TEST_ptr(addr1))
  494. goto err;
  495. if (!TEST_int_eq(BIO_ADDR_rawmake(addr1, AF_INET, &in_local,
  496. sizeof(in_local), 1234), 1))
  497. goto err;
  498. addr2 = BIO_ADDR_new();
  499. if (!TEST_ptr(addr2))
  500. goto err;
  501. if (!TEST_int_eq(BIO_ADDR_rawmake(addr2, AF_INET, &in_local,
  502. sizeof(in_local), 2345), 1))
  503. goto err;
  504. addr3 = BIO_ADDR_new();
  505. if (!TEST_ptr(addr3))
  506. goto err;
  507. addr4 = BIO_ADDR_new();
  508. if (!TEST_ptr(addr4))
  509. goto err;
  510. msgs[0].peer = addr1;
  511. /* fails due to lack of caps on peer */
  512. if (!TEST_false(BIO_sendmmsg(bio1, msgs, sizeof(BIO_MSG), OSSL_NELEM(msgs),
  513. 0, &num_processed))
  514. || !TEST_size_t_eq(num_processed, 0))
  515. goto err;
  516. if (!TEST_int_eq(BIO_dgram_set_caps(bio2, ref_caps), 1))
  517. goto err;
  518. if (!TEST_int_eq(BIO_dgram_get_caps(bio2), ref_caps))
  519. goto err;
  520. if (!TEST_int_eq(BIO_dgram_get_effective_caps(bio1), ref_caps))
  521. goto err;
  522. if (!TEST_int_eq(BIO_dgram_get_effective_caps(bio2), 0))
  523. goto err;
  524. if (!TEST_int_eq(BIO_dgram_set_caps(bio1, ref_caps), 1))
  525. goto err;
  526. /* succeeds with cap now available */
  527. if (!TEST_true(BIO_sendmmsg(bio1, msgs, sizeof(BIO_MSG), 1, 0, &num_processed))
  528. || !TEST_size_t_eq(num_processed, 1))
  529. goto err;
  530. /* enable local addr support */
  531. if (!TEST_int_eq(BIO_dgram_set_local_addr_enable(bio2, 1), 1))
  532. goto err;
  533. rmsgs[0].data = scratch2;
  534. rmsgs[0].data_len = 64;
  535. rmsgs[0].peer = addr3;
  536. rmsgs[0].local = addr4;
  537. if (!TEST_true(BIO_recvmmsg(bio2, rmsgs, sizeof(BIO_MSG), OSSL_NELEM(rmsgs), 0,
  538. &num_processed))
  539. || !TEST_size_t_eq(num_processed, 1))
  540. goto err;
  541. if (!TEST_mem_eq(rmsgs[0].data, rmsgs[0].data_len, msgs[0].data, 19))
  542. goto err;
  543. /* We didn't set the source address so this should be zero */
  544. if (!TEST_int_eq(BIO_ADDR_family(addr3), 0))
  545. goto err;
  546. if (!TEST_int_eq(BIO_ADDR_family(addr4), AF_INET))
  547. goto err;
  548. if (!TEST_int_eq(BIO_ADDR_rawport(addr4), 1234))
  549. goto err;
  550. /* test source address */
  551. msgs[0].local = addr2;
  552. if (!TEST_int_eq(BIO_dgram_set_local_addr_enable(bio1, 1), 1))
  553. goto err;
  554. if (!TEST_true(BIO_sendmmsg(bio1, msgs, sizeof(BIO_MSG), 1, 0, &num_processed))
  555. || !TEST_size_t_eq(num_processed, 1))
  556. goto err;
  557. rmsgs[0].data = scratch2;
  558. rmsgs[0].data_len = 64;
  559. if (!TEST_true(BIO_recvmmsg(bio2, rmsgs, sizeof(BIO_MSG), OSSL_NELEM(rmsgs), 0, &num_processed))
  560. || !TEST_size_t_eq(num_processed, 1))
  561. goto err;
  562. if (!TEST_mem_eq(rmsgs[0].data, rmsgs[0].data_len,
  563. msgs[0].data, msgs[0].data_len))
  564. goto err;
  565. if (!TEST_int_eq(BIO_ADDR_family(addr3), AF_INET))
  566. goto err;
  567. if (!TEST_int_eq(BIO_ADDR_rawport(addr3), 2345))
  568. goto err;
  569. if (!TEST_int_eq(BIO_ADDR_family(addr4), AF_INET))
  570. goto err;
  571. if (!TEST_int_eq(BIO_ADDR_rawport(addr4), 1234))
  572. goto err;
  573. /* test truncation, pending */
  574. r = BIO_write(bio1, scratch, 64);
  575. if (!TEST_int_eq(r, 64))
  576. goto err;
  577. memset(scratch2, 0, 64);
  578. if (!TEST_int_eq(BIO_dgram_set_no_trunc(bio2, 1), 1))
  579. goto err;
  580. if (!TEST_int_eq(BIO_read(bio2, scratch2, 32), -1))
  581. goto err;
  582. if (!TEST_int_eq(BIO_pending(bio2), 64))
  583. goto err;
  584. if (!TEST_int_eq(BIO_dgram_set_no_trunc(bio2, 0), 1))
  585. goto err;
  586. if (!TEST_int_eq(BIO_read(bio2, scratch2, 32), 32))
  587. goto err;
  588. if (!TEST_mem_eq(scratch, 32, scratch2, 32))
  589. goto err;
  590. testresult = 1;
  591. err:
  592. BIO_free(bio1);
  593. BIO_free(bio2);
  594. BIO_ADDR_free(addr1);
  595. BIO_ADDR_free(addr2);
  596. BIO_ADDR_free(addr3);
  597. BIO_ADDR_free(addr4);
  598. return testresult;
  599. }
  600. # endif /* !defined(OPENSSL_NO_CHACHA) */
  601. #endif /* !defined(OPENSSL_NO_DGRAM) && !defined(OPENSSL_NO_SOCK) */
  602. int setup_tests(void)
  603. {
  604. if (!test_skip_common_options()) {
  605. TEST_error("Error parsing test options\n");
  606. return 0;
  607. }
  608. #if !defined(OPENSSL_NO_DGRAM) && !defined(OPENSSL_NO_SOCK)
  609. ADD_ALL_TESTS(test_bio_dgram, OSSL_NELEM(bio_dgram_cases));
  610. # if !defined(OPENSSL_NO_CHACHA)
  611. ADD_TEST(test_bio_dgram_pair);
  612. # endif
  613. #endif
  614. return 1;
  615. }