test_http_server.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright 2018 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <stdlib.h>
  15. #include <stdbool.h>
  16. #include <esp_system.h>
  17. #include <http_server.h>
  18. #include "unity.h"
  19. int pre_start_mem, post_stop_mem, post_stop_min_mem;
  20. bool basic_sanity = true;
  21. esp_err_t null_func(httpd_req_t *req)
  22. {
  23. return ESP_OK;
  24. }
  25. httpd_uri_t handler_limit_uri (char* path)
  26. {
  27. httpd_uri_t uri = {
  28. .uri = path,
  29. .method = HTTP_GET,
  30. .handler = null_func,
  31. .user_ctx = NULL,
  32. };
  33. return uri;
  34. };
  35. static inline unsigned num_digits(unsigned x)
  36. {
  37. unsigned digits = 1;
  38. while ((x = x/10) != 0) {
  39. digits++;
  40. }
  41. return digits;
  42. }
  43. #define HTTPD_TEST_MAX_URI_HANDLERS 8
  44. void test_handler_limit(httpd_handle_t hd)
  45. {
  46. int i;
  47. char x[HTTPD_TEST_MAX_URI_HANDLERS+1][num_digits(HTTPD_TEST_MAX_URI_HANDLERS)+1];
  48. httpd_uri_t uris[HTTPD_TEST_MAX_URI_HANDLERS+1];
  49. for (i = 0; i < HTTPD_TEST_MAX_URI_HANDLERS + 1; i++) {
  50. sprintf(x[i],"%d",i);
  51. uris[i] = handler_limit_uri(x[i]);
  52. }
  53. /* Register multiple instances of the same handler for MAX URI Handlers */
  54. for (i = 0; i < HTTPD_TEST_MAX_URI_HANDLERS; i++) {
  55. TEST_ASSERT(httpd_register_uri_handler(hd, &uris[i]) == ESP_OK);
  56. }
  57. /* Register the MAX URI + 1 Handlers should fail */
  58. TEST_ASSERT(httpd_register_uri_handler(hd, &uris[HTTPD_TEST_MAX_URI_HANDLERS]) != ESP_OK);
  59. /* Unregister the one of the Handler should pass */
  60. TEST_ASSERT(httpd_unregister_uri_handler(hd, uris[0].uri, uris[0].method) == ESP_OK);
  61. /* Unregister non added Handler should fail */
  62. TEST_ASSERT(httpd_unregister_uri_handler(hd, uris[0].uri, uris[0].method) != ESP_OK);
  63. /* Register the MAX URI Handler should pass */
  64. TEST_ASSERT(httpd_register_uri_handler(hd, &uris[0]) == ESP_OK);
  65. /* Reregister same instance of handler should fail */
  66. TEST_ASSERT(httpd_register_uri_handler(hd, &uris[0]) != ESP_OK);
  67. /* Register the MAX URI + 1 Handlers should fail */
  68. TEST_ASSERT(httpd_register_uri_handler(hd, &uris[HTTPD_TEST_MAX_URI_HANDLERS]) != ESP_OK);
  69. /* Unregister the same handler for MAX URI Handlers */
  70. for (i = 0; i < HTTPD_TEST_MAX_URI_HANDLERS; i++) {
  71. TEST_ASSERT(httpd_unregister_uri_handler(hd, uris[i].uri, uris[i].method) == ESP_OK);
  72. }
  73. basic_sanity = false;
  74. }
  75. /********************* Test Handler Limit End *******************/
  76. httpd_handle_t test_httpd_start(uint16_t id)
  77. {
  78. httpd_handle_t hd;
  79. httpd_config_t config = HTTPD_DEFAULT_CONFIG();
  80. config.max_uri_handlers = HTTPD_TEST_MAX_URI_HANDLERS;
  81. config.server_port += id;
  82. config.ctrl_port += id;
  83. TEST_ASSERT(httpd_start(&hd, &config) == ESP_OK)
  84. return hd;
  85. }
  86. #define SERVER_INSTANCES 2
  87. /* Currently this only tests for the number of tasks.
  88. * Heap leakage is not tested as LWIP allocates memory
  89. * which may not be freed immedietly causing erroneous
  90. * evaluation. Another test to implement would be the
  91. * monitoring of open sockets, but LWIP presently provides
  92. * no such API for getting the number of open sockets.
  93. */
  94. TEST_CASE("Leak Test", "[HTTP SERVER]")
  95. {
  96. httpd_handle_t hd[SERVER_INSTANCES];
  97. unsigned task_count = uxTaskGetNumberOfTasks();
  98. pre_start_mem = esp_get_free_heap_size();
  99. bool res = true;
  100. for (int i = 0; i < SERVER_INSTANCES; i++) {
  101. hd[i] = test_httpd_start(i);
  102. vTaskDelay(10);
  103. if (uxTaskGetNumberOfTasks() != ++task_count) {
  104. res = false;
  105. }
  106. }
  107. for (int i = 0; i < SERVER_INSTANCES; i++) {
  108. if (httpd_stop(hd[i]) != ESP_OK) {
  109. res = false;
  110. }
  111. vTaskDelay(10);
  112. if (uxTaskGetNumberOfTasks() != --task_count) {
  113. res = false;
  114. }
  115. }
  116. post_stop_mem = esp_get_free_heap_size();
  117. TEST_ASSERT(res == true);
  118. }
  119. TEST_CASE("Basic Functionality Tests", "[HTTP SERVER]")
  120. {
  121. httpd_handle_t hd;
  122. httpd_config_t config = HTTPD_DEFAULT_CONFIG();
  123. TEST_ASSERT(httpd_start(&hd, &config) == ESP_OK);
  124. test_handler_limit(hd);
  125. TEST_ASSERT(httpd_stop(hd) == ESP_OK);
  126. }