bio_dgram_test.c 21 KB

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