test_http_server.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 <esp_http_server.h>
  18. #include "unity.h"
  19. #include "test_utils.h"
  20. int pre_start_mem, post_stop_mem, post_stop_min_mem;
  21. bool basic_sanity = true;
  22. esp_err_t null_func(httpd_req_t *req)
  23. {
  24. return ESP_OK;
  25. }
  26. httpd_uri_t handler_limit_uri (char* path)
  27. {
  28. httpd_uri_t uri = {
  29. .uri = path,
  30. .method = HTTP_GET,
  31. .handler = null_func,
  32. .user_ctx = NULL,
  33. };
  34. return uri;
  35. };
  36. static inline unsigned num_digits(unsigned x)
  37. {
  38. unsigned digits = 1;
  39. while ((x = x/10) != 0) {
  40. digits++;
  41. }
  42. return digits;
  43. }
  44. #define HTTPD_TEST_MAX_URI_HANDLERS 8
  45. void test_handler_limit(httpd_handle_t hd)
  46. {
  47. int i;
  48. char x[HTTPD_TEST_MAX_URI_HANDLERS+1][num_digits(HTTPD_TEST_MAX_URI_HANDLERS)+1];
  49. httpd_uri_t uris[HTTPD_TEST_MAX_URI_HANDLERS+1];
  50. for (i = 0; i < HTTPD_TEST_MAX_URI_HANDLERS + 1; i++) {
  51. sprintf(x[i],"%d",i);
  52. uris[i] = handler_limit_uri(x[i]);
  53. }
  54. /* Register multiple instances of the same handler for MAX URI Handlers */
  55. for (i = 0; i < HTTPD_TEST_MAX_URI_HANDLERS; i++) {
  56. TEST_ASSERT(httpd_register_uri_handler(hd, &uris[i]) == ESP_OK);
  57. }
  58. /* Register the MAX URI + 1 Handlers should fail */
  59. TEST_ASSERT(httpd_register_uri_handler(hd, &uris[HTTPD_TEST_MAX_URI_HANDLERS]) != ESP_OK);
  60. /* Unregister the one of the Handler should pass */
  61. TEST_ASSERT(httpd_unregister_uri_handler(hd, uris[0].uri, uris[0].method) == ESP_OK);
  62. /* Unregister non added Handler should fail */
  63. TEST_ASSERT(httpd_unregister_uri_handler(hd, uris[0].uri, uris[0].method) != ESP_OK);
  64. /* Register the MAX URI Handler should pass */
  65. TEST_ASSERT(httpd_register_uri_handler(hd, &uris[0]) == ESP_OK);
  66. /* Reregister same instance of handler should fail */
  67. TEST_ASSERT(httpd_register_uri_handler(hd, &uris[0]) != ESP_OK);
  68. /* Register the MAX URI + 1 Handlers should fail */
  69. TEST_ASSERT(httpd_register_uri_handler(hd, &uris[HTTPD_TEST_MAX_URI_HANDLERS]) != ESP_OK);
  70. /* Unregister the same handler for MAX URI Handlers */
  71. for (i = 0; i < HTTPD_TEST_MAX_URI_HANDLERS; i++) {
  72. TEST_ASSERT(httpd_unregister_uri_handler(hd, uris[i].uri, uris[i].method) == ESP_OK);
  73. }
  74. basic_sanity = false;
  75. }
  76. /********************* Test Handler Limit End *******************/
  77. httpd_handle_t test_httpd_start(uint16_t id)
  78. {
  79. httpd_handle_t hd;
  80. httpd_config_t config = HTTPD_DEFAULT_CONFIG();
  81. config.max_uri_handlers = HTTPD_TEST_MAX_URI_HANDLERS;
  82. config.server_port += id;
  83. config.ctrl_port += id;
  84. TEST_ASSERT(httpd_start(&hd, &config) == ESP_OK)
  85. return hd;
  86. }
  87. #define SERVER_INSTANCES 2
  88. /* Currently this only tests for the number of tasks.
  89. * Heap leakage is not tested as LWIP allocates memory
  90. * which may not be freed immedietly causing erroneous
  91. * evaluation. Another test to implement would be the
  92. * monitoring of open sockets, but LWIP presently provides
  93. * no such API for getting the number of open sockets.
  94. */
  95. TEST_CASE("Leak Test", "[HTTP SERVER]")
  96. {
  97. httpd_handle_t hd[SERVER_INSTANCES];
  98. unsigned task_count;
  99. bool res = true;
  100. test_case_uses_tcpip();
  101. task_count = uxTaskGetNumberOfTasks();
  102. printf("Initial task count: %d\n", task_count);
  103. pre_start_mem = esp_get_free_heap_size();
  104. for (int i = 0; i < SERVER_INSTANCES; i++) {
  105. hd[i] = test_httpd_start(i);
  106. vTaskDelay(10);
  107. unsigned num_tasks = uxTaskGetNumberOfTasks();
  108. task_count++;
  109. if (num_tasks != task_count) {
  110. printf("Incorrect task count (starting): %d expected %d\n",
  111. num_tasks, task_count);
  112. res = false;
  113. }
  114. }
  115. for (int i = 0; i < SERVER_INSTANCES; i++) {
  116. if (httpd_stop(hd[i]) != ESP_OK) {
  117. printf("Failed to stop httpd task %d\n", i);
  118. res = false;
  119. }
  120. vTaskDelay(10);
  121. unsigned num_tasks = uxTaskGetNumberOfTasks();
  122. task_count--;
  123. if (num_tasks != task_count) {
  124. printf("Incorrect task count (stopping): %d expected %d\n",
  125. num_tasks, task_count);
  126. res = false;
  127. }
  128. }
  129. post_stop_mem = esp_get_free_heap_size();
  130. TEST_ASSERT(res == true);
  131. }
  132. TEST_CASE("Basic Functionality Tests", "[HTTP SERVER]")
  133. {
  134. httpd_handle_t hd;
  135. httpd_config_t config = HTTPD_DEFAULT_CONFIG();
  136. test_case_uses_tcpip();
  137. TEST_ASSERT(httpd_start(&hd, &config) == ESP_OK);
  138. test_handler_limit(hd);
  139. TEST_ASSERT(httpd_stop(hd) == ESP_OK);
  140. }
  141. TEST_CASE("URI Wildcard Matcher Tests", "[HTTP SERVER]")
  142. {
  143. struct uritest {
  144. const char *template;
  145. const char *uri;
  146. bool matches;
  147. };
  148. struct uritest uris[] = {
  149. {"/", "/", true},
  150. {"", "", true},
  151. {"/", "", false},
  152. {"/wrong", "/", false},
  153. {"/", "/wrong", false},
  154. {"/asdfghjkl/qwertrtyyuiuioo", "/asdfghjkl/qwertrtyyuiuioo", true},
  155. {"/path", "/path", true},
  156. {"/path", "/path/", false},
  157. {"/path/", "/path", false},
  158. {"?", "", false}, // this is not valid, but should not crash
  159. {"?", "sfsdf", false},
  160. {"/path/?", "/pa", false},
  161. {"/path/?", "/path", true},
  162. {"/path/?", "/path/", true},
  163. {"/path/?", "/path/alalal", false},
  164. {"/path/*", "/path", false},
  165. {"/path/*", "/", false},
  166. {"/path/*", "/path/", true},
  167. {"/path/*", "/path/blabla", true},
  168. {"*", "", true},
  169. {"*", "/", true},
  170. {"*", "/aaa", true},
  171. {"/path/?*", "/pat", false},
  172. {"/path/?*", "/pathb", false},
  173. {"/path/?*", "/pathxx", false},
  174. {"/path/?*", "/pathblabla", false},
  175. {"/path/?*", "/path", true},
  176. {"/path/?*", "/path/", true},
  177. {"/path/?*", "/path/blabla", true},
  178. {"/path/*?", "/pat", false},
  179. {"/path/*?", "/pathb", false},
  180. {"/path/*?", "/pathxx", false},
  181. {"/path/*?", "/path", true},
  182. {"/path/*?", "/path/", true},
  183. {"/path/*?", "/path/blabla", true},
  184. {"/path/*/xxx", "/path/", false},
  185. {"/path/*/xxx", "/path/*/xxx", true},
  186. {}
  187. };
  188. struct uritest *ut = &uris[0];
  189. while(ut->template != 0) {
  190. bool match = httpd_uri_match_wildcard(ut->template, ut->uri, strlen(ut->uri));
  191. TEST_ASSERT(match == ut->matches);
  192. ut++;
  193. }
  194. }
  195. TEST_CASE("Max Allowed Sockets Test", "[HTTP SERVER]")
  196. {
  197. test_case_uses_tcpip();
  198. httpd_handle_t hd;
  199. httpd_config_t config = HTTPD_DEFAULT_CONFIG();
  200. /* Starting server with default config options should pass */
  201. TEST_ASSERT(httpd_start(&hd, &config) == ESP_OK);
  202. TEST_ASSERT(httpd_stop(hd) == ESP_OK);
  203. /* Default value of max_open_sockets is already set as per
  204. * maximum limit imposed by LWIP. Increasing this beyond the
  205. * maximum allowed value, without increasing LWIP limit,
  206. * should fail */
  207. config.max_open_sockets += 1;
  208. TEST_ASSERT(httpd_start(&hd, &config) != ESP_OK);
  209. }