membio_test.c 4.1 KB

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