test_openssl.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* Copyright (c) 2014, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #define ESP_OPENSSL_SUPPRESS_LEGACY_WARNING
  15. #include "openssl/ssl.h"
  16. #include "unity.h"
  17. /**
  18. * @brief This simple test suite is taken from OpenSSL err_test.cc and bio_test.cc, the relevant test
  19. * cases were adopted to the supported fraction of OpenSSL port in esp-idf
  20. */
  21. //
  22. // Basic error stack support and test
  23. //
  24. #define ERR_NUM_ERRORS 4
  25. TEST_CASE("ErrTest, Overflow", "[openssl]")
  26. {
  27. for (unsigned i = 0; i < ERR_NUM_ERRORS*2; i++) {
  28. ERR_put_error(1, 0 /* unused */, i+1, "test", 1);
  29. }
  30. for (unsigned i = 0; i < ERR_NUM_ERRORS - 1; i++) {
  31. uint32_t err = ERR_get_error();
  32. /* Errors are returned in order they were pushed, with the least recent ones
  33. * removed, up to |ERR_NUM_ERRORS - 1| errors. So the errors returned are
  34. * |ERR_NUM_ERRORS + 2| through |ERR_NUM_ERRORS * 2|, inclusive. */
  35. TEST_ASSERT_NOT_EQUAL(0u, err);
  36. TEST_ASSERT_EQUAL(i + ERR_NUM_ERRORS + 2, ERR_GET_REASON(err));
  37. }
  38. TEST_ASSERT_EQUAL(0u, ERR_get_error());
  39. }
  40. TEST_CASE("ErrTest, PutError", "[openssl]")
  41. {
  42. TEST_ASSERT_EQUAL(0u, ERR_get_error()); // ERR_get_error returned value before an error was added.
  43. ERR_put_error(1, 0 /* unused */, 2, "test", 4);
  44. int peeked_line, line, peeked_flags, flags;
  45. const char *peeked_file, *file, *peeked_data, *data;
  46. uint32_t peeked_packed_error =
  47. ERR_peek_error_line_data(&peeked_file, &peeked_line, &peeked_data,
  48. &peeked_flags);
  49. uint32_t packed_error = ERR_get_error_line_data(&file, &line, &data, &flags);
  50. TEST_ASSERT_EQUAL(peeked_packed_error, packed_error);
  51. TEST_ASSERT_EQUAL(peeked_file, file);
  52. TEST_ASSERT_EQUAL_STRING("test", file);
  53. TEST_ASSERT_EQUAL(4, line);
  54. TEST_ASSERT_EQUAL(1, ERR_GET_LIB(packed_error));
  55. TEST_ASSERT_EQUAL(2, ERR_GET_REASON(packed_error));
  56. }
  57. TEST_CASE("ErrTest, ClearError", "[openssl]")
  58. {
  59. TEST_ASSERT_EQUAL(0u, ERR_get_error()); // ERR_get_error returned value before an error was added.
  60. ERR_put_error(1, 0 /* unused */, 2, "test", 4);
  61. ERR_clear_error();
  62. // The error queue should be cleared.
  63. TEST_ASSERT_EQUAL(0u, ERR_get_error());
  64. }
  65. //
  66. // Simplified BIO support and check
  67. //
  68. TEST_CASE("BioTest, TestPair", "[openssl]")
  69. {
  70. BIO *bio1, *bio2;
  71. TEST_ASSERT_NOT_EQUAL(0, BIO_new_bio_pair(&bio1, 10, &bio2, 10));
  72. TEST_ASSERT_EQUAL(BIO_ctrl_get_write_guarantee(bio1), 10);
  73. // Data written in one end may be read out the other.
  74. char buf[20];
  75. TEST_ASSERT_EQUAL(5, BIO_write(bio1, "12345", 5));
  76. TEST_ASSERT_EQUAL(5, BIO_ctrl_get_write_guarantee(bio1));
  77. TEST_ASSERT_EQUAL(5, BIO_read(bio2, buf, sizeof(buf)));
  78. TEST_ASSERT_EQUAL_UINT8_ARRAY("12345", buf, 5);
  79. TEST_ASSERT_EQUAL(10, BIO_ctrl_get_write_guarantee(bio1));
  80. // Attempting to write more than 10 bytes will write partially.
  81. TEST_ASSERT_EQUAL(10, BIO_write(bio1, "1234567890___", 13));
  82. TEST_ASSERT_EQUAL(0, BIO_ctrl_get_write_guarantee(bio1));
  83. TEST_ASSERT_EQUAL(-1, BIO_write(bio1, "z", 1));
  84. TEST_ASSERT_TRUE(BIO_should_write(bio1));
  85. TEST_ASSERT_EQUAL(10, BIO_read(bio2, buf, sizeof(buf)));
  86. TEST_ASSERT_EQUAL_UINT8_ARRAY("1234567890", buf, 10);
  87. TEST_ASSERT_EQUAL(10, BIO_ctrl_get_write_guarantee(bio1));
  88. // Unsuccessful reads update the read request.
  89. TEST_ASSERT_EQUAL(-1, BIO_read(bio2, buf, 5));
  90. TEST_ASSERT_TRUE(BIO_should_read(bio2));
  91. // The read request is clamped to the size of the buffer.
  92. TEST_ASSERT_EQUAL(-1, BIO_read(bio2, buf, 20));
  93. TEST_ASSERT_TRUE(BIO_should_read(bio2));
  94. // Data may be written and read in chunks.
  95. TEST_ASSERT_EQUAL(BIO_write(bio1, "12345", 5), 5);
  96. TEST_ASSERT_EQUAL(5, BIO_ctrl_get_write_guarantee(bio1));
  97. TEST_ASSERT_EQUAL(5, BIO_write(bio1, "67890___", 8));
  98. TEST_ASSERT_EQUAL(0, BIO_ctrl_get_write_guarantee(bio1));
  99. TEST_ASSERT_EQUAL(3, BIO_read(bio2, buf, 3));
  100. TEST_ASSERT_EQUAL_UINT8_ARRAY("123", buf, 3);
  101. TEST_ASSERT_EQUAL(3, BIO_ctrl_get_write_guarantee(bio1));
  102. TEST_ASSERT_EQUAL(7, BIO_read(bio2, buf, sizeof(buf)));
  103. TEST_ASSERT_EQUAL_UINT8_ARRAY("4567890", buf, 7);
  104. TEST_ASSERT_EQUAL(10, BIO_ctrl_get_write_guarantee(bio1));
  105. // Test writes and reads starting in the middle of the ring buffer and
  106. // wrapping to front.
  107. TEST_ASSERT_EQUAL(8, BIO_write(bio1, "abcdefgh", 8));
  108. TEST_ASSERT_EQUAL(2, BIO_ctrl_get_write_guarantee(bio1));
  109. TEST_ASSERT_EQUAL(3, BIO_read(bio2, buf, 3));
  110. TEST_ASSERT_EQUAL_UINT8_ARRAY("abc", buf, 3);
  111. TEST_ASSERT_EQUAL(5, BIO_ctrl_get_write_guarantee(bio1));
  112. TEST_ASSERT_EQUAL(5, BIO_write(bio1, "ijklm___", 8));
  113. TEST_ASSERT_EQUAL(0, BIO_ctrl_get_write_guarantee(bio1));
  114. TEST_ASSERT_EQUAL(10, BIO_read(bio2, buf, sizeof(buf)));
  115. TEST_ASSERT_EQUAL_UINT8_ARRAY("defghijklm", buf, 10);
  116. TEST_ASSERT_EQUAL(10, BIO_ctrl_get_write_guarantee(bio1));
  117. // Data may flow from both ends in parallel.
  118. TEST_ASSERT_EQUAL(5, BIO_write(bio1, "12345", 5));
  119. TEST_ASSERT_EQUAL(5, BIO_write(bio2, "67890", 5));
  120. TEST_ASSERT_EQUAL(5, BIO_read(bio2, buf, sizeof(buf)));
  121. TEST_ASSERT_EQUAL_UINT8_ARRAY("12345", buf, 5);
  122. TEST_ASSERT_EQUAL(5, BIO_read(bio1, buf, sizeof(buf)));
  123. TEST_ASSERT_EQUAL_UINT8_ARRAY("67890", buf, 5);
  124. // Other tests below not imported since BIO_shutdown_wr() not supported
  125. // - Closing the write end causes an EOF on the read half, after draining.
  126. // - A closed write end may not be written to.
  127. // - The other end is still functional.
  128. }