test_https.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include <nlohmann_json/json.hpp>
  2. #include "kbf.h"
  3. #include "kbf/wifi_legacy.h"
  4. #include "kbf/http/server.h"
  5. #include "kbf/http/client.h"
  6. #include "kbf/http/exception.h"
  7. #include "kbf/rtos.h"
  8. #include <unity.h>
  9. using namespace kbf;
  10. using namespace std;
  11. using nlohmann::json;
  12. extern const unsigned char cert_pem_start[] asm("_binary_cert_pem_start");
  13. extern const unsigned char cert_pem_end[] asm("_binary_cert_pem_end");
  14. extern const unsigned char cert_wrong_pem_start[] asm("_binary_wrong_cert_pem_start");
  15. extern const unsigned char cert_wrong_pem_end[] asm("_binary_wrong_cert_pem_end");
  16. extern const unsigned char key_pem_start[] asm("_binary_key_pem_start");
  17. extern const unsigned char key_pem_end[] asm("_binary_key_pem_end");
  18. static void assert_fail(http::Client &client, const string &message) {
  19. bool caught = false;
  20. try {
  21. auto response = client.get("https://localhost/get-me");
  22. ESP_LOGI("assert", "gonna fail, got %d", response->status);
  23. } catch (http::exception::ConnectionError &e) {
  24. caught = true;
  25. }
  26. TEST_ASSERT_TRUE_MESSAGE(caught, message.c_str());
  27. }
  28. static void assert_ok(http::Client &client, const string &message) {
  29. auto response = client.get("https://localhost/get-me");
  30. TEST_ASSERT_EQUAL_MESSAGE(200, response->status, message.c_str());
  31. TEST_ASSERT_EQUAL_STRING("OK", response->body.c_str());
  32. }
  33. TEST_CASE("HTTPS GET", "[kbf_http]") {
  34. wifi_legacy::start();
  35. auto server = http::Server();
  36. TEST_ASSERT_FALSE(server.isRunning())
  37. http::Response (*handleGet)(const http::Request &, void *) = {[](const http::Request &request, void *) {
  38. TEST_ASSERT_EQUAL(http::GET, request.method);
  39. return http::Response("OK");
  40. }};
  41. server.route({http::GET, "/get-me", handleGet, nullptr});
  42. server.startSSL(cert_pem_start, cert_pem_end, key_pem_start, key_pem_end);
  43. TEST_ASSERT_TRUE(server.isRunning())
  44. auto client = http::Client();
  45. assert_fail(client, "no certs");
  46. http::Client::addCert(cert_pem_start, cert_pem_end);
  47. assert_ok(client, "right cert");
  48. client.disconnect();
  49. http::Client::clearCerts();
  50. assert_fail(client, "certs cleared");
  51. http::Client::addCert(cert_wrong_pem_start, cert_wrong_pem_end);
  52. assert_fail(client, "wrong cert");
  53. http::Client::addCert(cert_pem_start, cert_pem_end);
  54. assert_ok(client, "wrong + right cert");
  55. client.disconnect();
  56. http::Client::clearCerts();
  57. http::Client::addCert(cert_pem_start, cert_pem_end);
  58. http::Client::addCert(cert_wrong_pem_start, cert_wrong_pem_end);
  59. assert_ok(client, "right + wrong cert");
  60. server.stop();
  61. wifi_legacy::stop();
  62. }
  63. // TODO KBF-22 fix async test
  64. static rtos::EventGroup eventGroup;
  65. TEST_CASE("HTTPS async", "[broken]") {
  66. wifi_legacy::start();
  67. http::Response (*handleRequest)(const http::Request &, void *) = {[](const http::Request &request, void *) {
  68. auto response = http::Response("OK");
  69. kbf::sleep(100);
  70. return response;
  71. }};
  72. auto server = http::Server()
  73. .route({http::GET, "/", handleRequest, nullptr})
  74. .startSSL(cert_pem_start, cert_pem_end, key_pem_start, key_pem_end);
  75. auto client = http::Client(true);
  76. client.onSuccess = {[](http::Client &client, const http::Response &response) {
  77. TEST_ASSERT_EQUAL_STRING("OK", response.body.data());
  78. eventGroup.setBit(0);
  79. }};
  80. auto response = client.get("https://localhost/");
  81. TEST_ASSERT_NULL(response)
  82. TEST_ASSERT_EQUAL(0, eventGroup.getBit(0));
  83. kbf::sleep(200);
  84. TEST_ASSERT_EQUAL(1, eventGroup.getBit(0));
  85. server.stop();
  86. wifi_legacy::stop();
  87. }