membio_test.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 <openssl/bio.h>
  10. #include "testutil.h"
  11. #ifndef OPENSSL_NO_DGRAM
  12. static int test_dgram(void)
  13. {
  14. BIO *bio = BIO_new(BIO_s_dgram_mem()), *rbio = NULL;
  15. int testresult = 0;
  16. const char msg1[] = "12345656";
  17. const char msg2[] = "abcdefghijklmno";
  18. const char msg3[] = "ABCDEF";
  19. const char msg4[] = "FEDCBA";
  20. char buf[80];
  21. if (!TEST_ptr(bio))
  22. goto err;
  23. rbio = BIO_new_mem_buf(msg1, sizeof(msg1));
  24. if (!TEST_ptr(rbio))
  25. goto err;
  26. /* Setting the EOF return value on a non datagram mem BIO should be fine */
  27. if (!TEST_int_gt(BIO_set_mem_eof_return(rbio, 0), 0))
  28. goto err;
  29. /* Setting the EOF return value on a datagram mem BIO should fail */
  30. if (!TEST_int_le(BIO_set_mem_eof_return(bio, 0), 0))
  31. goto err;
  32. /* Write 4 dgrams */
  33. if (!TEST_int_eq(BIO_write(bio, msg1, sizeof(msg1)), sizeof(msg1)))
  34. goto err;
  35. if (!TEST_int_eq(BIO_write(bio, msg2, sizeof(msg2)), sizeof(msg2)))
  36. goto err;
  37. if (!TEST_int_eq(BIO_write(bio, msg3, sizeof(msg3)), sizeof(msg3)))
  38. goto err;
  39. if (!TEST_int_eq(BIO_write(bio, msg4, sizeof(msg4)), sizeof(msg4)))
  40. goto err;
  41. /* Reading all 4 dgrams out again should all be the correct size */
  42. if (!TEST_int_eq(BIO_read(bio, buf, sizeof(buf)), sizeof(msg1))
  43. || !TEST_mem_eq(buf, sizeof(msg1), msg1, sizeof(msg1))
  44. || !TEST_int_eq(BIO_read(bio, buf, sizeof(buf)), sizeof(msg2))
  45. || !TEST_mem_eq(buf, sizeof(msg2), msg2, sizeof(msg2))
  46. || !TEST_int_eq(BIO_read(bio, buf, sizeof(buf)), sizeof(msg3))
  47. || !TEST_mem_eq(buf, sizeof(msg3), msg3, sizeof(msg3))
  48. || !TEST_int_eq(BIO_read(bio, buf, sizeof(buf)), sizeof(msg4))
  49. || !TEST_mem_eq(buf, sizeof(msg4), msg4, sizeof(msg4)))
  50. goto err;
  51. /* Interleaving writes and reads should be fine */
  52. if (!TEST_int_eq(BIO_write(bio, msg1, sizeof(msg1)), sizeof(msg1)))
  53. goto err;
  54. if (!TEST_int_eq(BIO_write(bio, msg2, sizeof(msg2)), sizeof(msg2)))
  55. goto err;
  56. if (!TEST_int_eq(BIO_read(bio, buf, sizeof(buf)), sizeof(msg1))
  57. || !TEST_mem_eq(buf, sizeof(msg1), msg1, sizeof(msg1)))
  58. goto err;
  59. if (!TEST_int_eq(BIO_write(bio, msg3, sizeof(msg3)), sizeof(msg3)))
  60. goto err;
  61. if (!TEST_int_eq(BIO_read(bio, buf, sizeof(buf)), sizeof(msg2))
  62. || !TEST_mem_eq(buf, sizeof(msg2), msg2, sizeof(msg2))
  63. || !TEST_int_eq(BIO_read(bio, buf, sizeof(buf)), sizeof(msg3))
  64. || !TEST_mem_eq(buf, sizeof(msg3), msg3, sizeof(msg3)))
  65. goto err;
  66. /*
  67. * Requesting less than the available data in a dgram should not impact the
  68. * next packet.
  69. */
  70. if (!TEST_int_eq(BIO_write(bio, msg1, sizeof(msg1)), sizeof(msg1)))
  71. goto err;
  72. if (!TEST_int_eq(BIO_write(bio, msg2, sizeof(msg2)), sizeof(msg2)))
  73. goto err;
  74. if (!TEST_int_eq(BIO_read(bio, buf, /* Short buffer */ 2), 2)
  75. || !TEST_mem_eq(buf, 2, msg1, 2))
  76. goto err;
  77. if (!TEST_int_eq(BIO_read(bio, buf, sizeof(buf)), sizeof(msg2))
  78. || !TEST_mem_eq(buf, sizeof(msg2), msg2, sizeof(msg2)))
  79. goto err;
  80. /*
  81. * Writing a zero length datagram will return zero, but no datagrams will
  82. * be written. Attempting to read when there are no datagrams to read should
  83. * return a negative result, but not eof. Retry flags will be set.
  84. */
  85. if (!TEST_int_eq(BIO_write(bio, NULL, 0), 0)
  86. || !TEST_int_lt(BIO_read(bio, buf, sizeof(buf)), 0)
  87. || !TEST_false(BIO_eof(bio))
  88. || !TEST_true(BIO_should_retry(bio)))
  89. goto err;
  90. if (!TEST_int_eq(BIO_dgram_set_mtu(bio, 123456), 1)
  91. || !TEST_int_eq(BIO_dgram_get_mtu(bio), 123456))
  92. goto err;
  93. testresult = 1;
  94. err:
  95. BIO_free(rbio);
  96. BIO_free(bio);
  97. return testresult;
  98. }
  99. #endif
  100. int setup_tests(void)
  101. {
  102. if (!test_skip_common_options()) {
  103. TEST_error("Error parsing test options\n");
  104. return 0;
  105. }
  106. #ifndef OPENSSL_NO_DGRAM
  107. ADD_TEST(test_dgram);
  108. #endif
  109. return 1;
  110. }