bio_dgram_test.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  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(int idx)
  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 (idx == 0) {
  423. if (!TEST_int_eq(BIO_new_bio_dgram_pair(&bio1, 0, &bio2, 0), 1))
  424. goto err;
  425. } else {
  426. if (!TEST_ptr(bio1 = bio2 = BIO_new(BIO_s_dgram_mem())))
  427. goto err;
  428. }
  429. mtu1 = BIO_dgram_get_mtu(bio1);
  430. if (!TEST_int_ge(mtu1, 1280))
  431. goto err;
  432. mtu2 = BIO_dgram_get_mtu(bio2);
  433. if (!TEST_int_ge(mtu2, 1280))
  434. goto err;
  435. if (!TEST_int_eq(mtu1, mtu2))
  436. goto err;
  437. if (!TEST_int_le(mtu1, sizeof(scratch) - 4))
  438. goto err;
  439. for (i = 0; idx == 0 || i < 9; ++i) {
  440. if (!TEST_int_eq(random_data(key, scratch, sizeof(scratch), i), 1))
  441. goto err;
  442. blen = ((*(uint32_t*)scratch) % mtu1) + 1;
  443. r = BIO_write(bio1, scratch + 4, blen);
  444. if (r == -1)
  445. break;
  446. if (!TEST_int_eq(r, blen))
  447. goto err;
  448. total += blen;
  449. if (!TEST_size_t_lt(total, 1 * 1024 * 1024))
  450. goto err;
  451. }
  452. /*
  453. * Should be able to fit at least 9 datagrams in default write buffer size
  454. * in worst case
  455. */
  456. if (!TEST_int_ge(i, 9))
  457. goto err;
  458. /* Check we read back the same data */
  459. num_dgram = i;
  460. for (i = 0; i < num_dgram; ++i) {
  461. if (!TEST_int_eq(random_data(key, scratch, sizeof(scratch), i), 1))
  462. goto err;
  463. blen = ((*(uint32_t*)scratch) % mtu1) + 1;
  464. r = BIO_read(bio2, scratch2, sizeof(scratch2));
  465. if (!TEST_int_eq(r, blen))
  466. goto err;
  467. if (!TEST_mem_eq(scratch + 4, blen, scratch2, blen))
  468. goto err;
  469. }
  470. /* Should now be out of data */
  471. if (!TEST_int_eq(BIO_read(bio2, scratch2, sizeof(scratch2)), -1))
  472. goto err;
  473. /* sendmmsg/recvmmsg */
  474. if (!TEST_int_eq(random_data(key, scratch, sizeof(scratch), 0), 1))
  475. goto err;
  476. msgs[0].data = scratch;
  477. msgs[0].data_len = 19;
  478. msgs[1].data = scratch + 19;
  479. msgs[1].data_len = 46;
  480. if (!TEST_true(BIO_sendmmsg(bio1, msgs, sizeof(BIO_MSG), OSSL_NELEM(msgs), 0,
  481. &num_processed))
  482. || !TEST_size_t_eq(num_processed, 2))
  483. goto err;
  484. rmsgs[0].data = scratch2;
  485. rmsgs[0].data_len = 64;
  486. rmsgs[1].data = scratch2 + 64;
  487. rmsgs[1].data_len = 64;
  488. if (!TEST_true(BIO_recvmmsg(bio2, rmsgs, sizeof(BIO_MSG), OSSL_NELEM(rmsgs), 0,
  489. &num_processed))
  490. || !TEST_size_t_eq(num_processed, 2))
  491. goto err;
  492. if (!TEST_mem_eq(rmsgs[0].data, rmsgs[0].data_len, scratch, 19))
  493. goto err;
  494. if (!TEST_mem_eq(rmsgs[1].data, rmsgs[1].data_len, scratch + 19, 46))
  495. goto err;
  496. /* sendmmsg/recvmmsg with peer */
  497. addr1 = BIO_ADDR_new();
  498. if (!TEST_ptr(addr1))
  499. goto err;
  500. if (!TEST_int_eq(BIO_ADDR_rawmake(addr1, AF_INET, &in_local,
  501. sizeof(in_local), 1234), 1))
  502. goto err;
  503. addr2 = BIO_ADDR_new();
  504. if (!TEST_ptr(addr2))
  505. goto err;
  506. if (!TEST_int_eq(BIO_ADDR_rawmake(addr2, AF_INET, &in_local,
  507. sizeof(in_local), 2345), 1))
  508. goto err;
  509. addr3 = BIO_ADDR_new();
  510. if (!TEST_ptr(addr3))
  511. goto err;
  512. addr4 = BIO_ADDR_new();
  513. if (!TEST_ptr(addr4))
  514. goto err;
  515. msgs[0].peer = addr1;
  516. /* fails due to lack of caps on peer */
  517. if (!TEST_false(BIO_sendmmsg(bio1, msgs, sizeof(BIO_MSG),
  518. OSSL_NELEM(msgs), 0, &num_processed))
  519. || !TEST_size_t_eq(num_processed, 0))
  520. goto err;
  521. if (!TEST_int_eq(BIO_dgram_set_caps(bio2, ref_caps), 1))
  522. goto err;
  523. if (!TEST_int_eq(BIO_dgram_get_caps(bio2), ref_caps))
  524. goto err;
  525. if (!TEST_int_eq(BIO_dgram_get_effective_caps(bio1), ref_caps))
  526. goto err;
  527. if (idx == 0 && !TEST_int_eq(BIO_dgram_get_effective_caps(bio2), 0))
  528. goto err;
  529. if (!TEST_int_eq(BIO_dgram_set_caps(bio1, ref_caps), 1))
  530. goto err;
  531. /* succeeds with cap now available */
  532. if (!TEST_true(BIO_sendmmsg(bio1, msgs, sizeof(BIO_MSG), 1, 0, &num_processed))
  533. || !TEST_size_t_eq(num_processed, 1))
  534. goto err;
  535. /* enable local addr support */
  536. if (!TEST_int_eq(BIO_dgram_set_local_addr_enable(bio2, 1), 1))
  537. goto err;
  538. rmsgs[0].data = scratch2;
  539. rmsgs[0].data_len = 64;
  540. rmsgs[0].peer = addr3;
  541. rmsgs[0].local = addr4;
  542. if (!TEST_true(BIO_recvmmsg(bio2, rmsgs, sizeof(BIO_MSG), OSSL_NELEM(rmsgs), 0,
  543. &num_processed))
  544. || !TEST_size_t_eq(num_processed, 1))
  545. goto err;
  546. if (!TEST_mem_eq(rmsgs[0].data, rmsgs[0].data_len, msgs[0].data, 19))
  547. goto err;
  548. /* We didn't set the source address so this should be zero */
  549. if (!TEST_int_eq(BIO_ADDR_family(addr3), 0))
  550. goto err;
  551. if (!TEST_int_eq(BIO_ADDR_family(addr4), AF_INET))
  552. goto err;
  553. if (!TEST_int_eq(BIO_ADDR_rawport(addr4), 1234))
  554. goto err;
  555. /* test source address */
  556. msgs[0].local = addr2;
  557. if (!TEST_int_eq(BIO_dgram_set_local_addr_enable(bio1, 1), 1))
  558. goto err;
  559. if (!TEST_true(BIO_sendmmsg(bio1, msgs, sizeof(BIO_MSG), 1, 0, &num_processed))
  560. || !TEST_size_t_eq(num_processed, 1))
  561. goto err;
  562. rmsgs[0].data = scratch2;
  563. rmsgs[0].data_len = 64;
  564. if (!TEST_true(BIO_recvmmsg(bio2, rmsgs, sizeof(BIO_MSG), OSSL_NELEM(rmsgs), 0, &num_processed))
  565. || !TEST_size_t_eq(num_processed, 1))
  566. goto err;
  567. if (!TEST_mem_eq(rmsgs[0].data, rmsgs[0].data_len,
  568. msgs[0].data, msgs[0].data_len))
  569. goto err;
  570. if (!TEST_int_eq(BIO_ADDR_family(addr3), AF_INET))
  571. goto err;
  572. if (!TEST_int_eq(BIO_ADDR_rawport(addr3), 2345))
  573. goto err;
  574. if (!TEST_int_eq(BIO_ADDR_family(addr4), AF_INET))
  575. goto err;
  576. if (!TEST_int_eq(BIO_ADDR_rawport(addr4), 1234))
  577. goto err;
  578. /* test truncation, pending */
  579. r = BIO_write(bio1, scratch, 64);
  580. if (!TEST_int_eq(r, 64))
  581. goto err;
  582. memset(scratch2, 0, 64);
  583. if (!TEST_int_eq(BIO_dgram_set_no_trunc(bio2, 1), 1))
  584. goto err;
  585. if (!TEST_int_eq(BIO_read(bio2, scratch2, 32), -1))
  586. goto err;
  587. if (!TEST_int_eq(BIO_pending(bio2), 64))
  588. goto err;
  589. if (!TEST_int_eq(BIO_dgram_set_no_trunc(bio2, 0), 1))
  590. goto err;
  591. if (!TEST_int_eq(BIO_read(bio2, scratch2, 32), 32))
  592. goto err;
  593. if (!TEST_mem_eq(scratch, 32, scratch2, 32))
  594. goto err;
  595. testresult = 1;
  596. err:
  597. if (idx == 0)
  598. BIO_free(bio1);
  599. BIO_free(bio2);
  600. BIO_ADDR_free(addr1);
  601. BIO_ADDR_free(addr2);
  602. BIO_ADDR_free(addr3);
  603. BIO_ADDR_free(addr4);
  604. return testresult;
  605. }
  606. # endif /* !defined(OPENSSL_NO_CHACHA) */
  607. #endif /* !defined(OPENSSL_NO_DGRAM) && !defined(OPENSSL_NO_SOCK) */
  608. int setup_tests(void)
  609. {
  610. if (!test_skip_common_options()) {
  611. TEST_error("Error parsing test options\n");
  612. return 0;
  613. }
  614. #if !defined(OPENSSL_NO_DGRAM) && !defined(OPENSSL_NO_SOCK)
  615. ADD_ALL_TESTS(test_bio_dgram, OSSL_NELEM(bio_dgram_cases));
  616. # if !defined(OPENSSL_NO_CHACHA)
  617. ADD_ALL_TESTS(test_bio_dgram_pair, 2);
  618. # endif
  619. #endif
  620. return 1;
  621. }