tests.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. #include <stdlib.h>
  2. #include <stdbool.h>
  3. #include <esp_log.h>
  4. #include <esp_system.h>
  5. #include <http_server.h>
  6. #include "tests.h"
  7. static const char *TAG="TESTS";
  8. int pre_start_mem, post_stop_mem, post_stop_min_mem;
  9. bool basic_sanity = true;
  10. /********************* Basic Handlers Start *******************/
  11. esp_err_t hello_get_handler(httpd_req_t *req)
  12. {
  13. #define STR "Hello World!"
  14. ESP_LOGI(TAG, "Free Stack for server task: %d", uxTaskGetStackHighWaterMark(NULL));
  15. httpd_resp_send(req, STR, strlen(STR));
  16. return ESP_OK;
  17. #undef STR
  18. }
  19. esp_err_t hello_type_get_handler(httpd_req_t *req)
  20. {
  21. #define STR "Hello World!"
  22. httpd_resp_set_type(req, HTTPD_TYPE_TEXT);
  23. httpd_resp_send(req, STR, strlen(STR));
  24. return ESP_OK;
  25. #undef STR
  26. }
  27. esp_err_t hello_status_get_handler(httpd_req_t *req)
  28. {
  29. #define STR "Hello World!"
  30. httpd_resp_set_status(req, HTTPD_500);
  31. httpd_resp_send(req, STR, strlen(STR));
  32. return ESP_OK;
  33. #undef STR
  34. }
  35. esp_err_t echo_post_handler(httpd_req_t *req)
  36. {
  37. ESP_LOGI(TAG, "/echo handler read content length %d", req->content_len);
  38. char* buf = malloc(req->content_len + 1);
  39. size_t off = 0;
  40. int ret;
  41. if (!buf) {
  42. return ESP_FAIL;
  43. }
  44. while (off < req->content_len) {
  45. /* Read data received in the request */
  46. ret = httpd_req_recv(req, buf + off, req->content_len - off);
  47. if (ret < 0) {
  48. free (buf);
  49. return ESP_FAIL;
  50. }
  51. off += ret;
  52. ESP_LOGI(TAG, "/echo handler recv length %d", ret);
  53. }
  54. buf[off] = '\0';
  55. if (req->content_len < 128) {
  56. ESP_LOGI(TAG, "/echo handler read %s", buf);
  57. }
  58. /* Search for Custom header field */
  59. char* req_hdr = 0;
  60. size_t hdr_len = httpd_req_get_hdr_value_len(req, "Custom");
  61. if (hdr_len) {
  62. /* Read Custom header value */
  63. req_hdr = malloc(hdr_len + 1);
  64. if (req_hdr) {
  65. httpd_req_get_hdr_value_str(req, "Custom", req_hdr, hdr_len + 1);
  66. /* Set as additional header for response packet */
  67. httpd_resp_set_hdr(req, "Custom", req_hdr);
  68. }
  69. }
  70. httpd_resp_send(req, buf, req->content_len);
  71. free (req_hdr);
  72. free (buf);
  73. return ESP_OK;
  74. }
  75. void adder_free_func(void *ctx)
  76. {
  77. ESP_LOGI(TAG, "Custom Free Context function called");
  78. free(ctx);
  79. }
  80. /* Create a context, keep incrementing value in the context, by whatever was
  81. * received. Return the result
  82. */
  83. esp_err_t adder_post_handler(httpd_req_t *req)
  84. {
  85. char buf[10];
  86. char outbuf[50];
  87. int ret;
  88. /* Read data received in the request */
  89. ret = httpd_req_recv(req, buf, sizeof(buf));
  90. if (ret < 0) {
  91. return ESP_FAIL;
  92. }
  93. buf[ret] = '\0';
  94. int val = atoi(buf);
  95. ESP_LOGI(TAG, "/adder handler read %d", val);
  96. if (! req->sess_ctx) {
  97. ESP_LOGI(TAG, "/adder allocating new session");
  98. req->sess_ctx = malloc(sizeof(int));
  99. req->free_ctx = adder_free_func;
  100. *(int *)req->sess_ctx = 0;
  101. }
  102. int *adder = (int *)req->sess_ctx;
  103. *adder += val;
  104. snprintf(outbuf, sizeof(outbuf),"%d", *adder);
  105. httpd_resp_send(req, outbuf, strlen(outbuf));
  106. return ESP_OK;
  107. }
  108. esp_err_t leftover_data_post_handler(httpd_req_t *req)
  109. {
  110. /* Only echo the first 10 bytes of the request, leaving the rest of the
  111. * request data as is.
  112. */
  113. char buf[11];
  114. int ret;
  115. /* Read data received in the request */
  116. ret = httpd_req_recv(req, buf, sizeof(buf) - 1);
  117. if (ret < 0) {
  118. return ESP_FAIL;
  119. }
  120. buf[ret] = '\0';
  121. ESP_LOGI(TAG, "leftover data handler read %s", buf);
  122. httpd_resp_send(req, buf, strlen(buf));
  123. return ESP_OK;
  124. }
  125. int httpd_default_send(int sockfd, const char *buf, unsigned buf_len, int flags);
  126. void generate_async_resp(void *arg)
  127. {
  128. char buf[250];
  129. int fd = (int )arg;
  130. #define HTTPD_HDR_STR "HTTP/1.1 200 OK\r\n" \
  131. "Content-Type: text/html\r\n" \
  132. "Content-Length: %d\r\n"
  133. #define STR "Hello Double World!"
  134. ESP_LOGI(TAG, "Executing queued work fd : %d", fd);
  135. snprintf(buf, sizeof(buf), HTTPD_HDR_STR,
  136. strlen(STR));
  137. httpd_default_send(fd, buf, strlen(buf), 0);
  138. /* Space for sending additional headers based on set_header */
  139. httpd_default_send(fd, "\r\n", strlen("\r\n"), 0);
  140. httpd_default_send(fd, STR, strlen(STR), 0);
  141. #undef STR
  142. }
  143. esp_err_t async_get_handler(httpd_req_t *req)
  144. {
  145. #define STR "Hello World!"
  146. httpd_resp_send(req, STR, strlen(STR));
  147. /* Also register a HTTPD Work which sends the same data on the same
  148. * socket again
  149. */
  150. int fd = httpd_req_to_sockfd(req);
  151. if (fd < 0) {
  152. return ESP_FAIL;
  153. }
  154. ESP_LOGI(TAG, "Queuing work fd : %d", fd);
  155. httpd_queue_work(req->handle, generate_async_resp, (void *)fd);
  156. return ESP_OK;
  157. #undef STR
  158. }
  159. httpd_uri_t basic_handlers[] = {
  160. { .uri = "/hello/type_html",
  161. .method = HTTP_GET,
  162. .handler = hello_type_get_handler,
  163. .user_ctx = NULL,
  164. },
  165. { .uri = "/hello",
  166. .method = HTTP_GET,
  167. .handler = hello_get_handler,
  168. .user_ctx = NULL,
  169. },
  170. { .uri = "/hello/status_500",
  171. .method = HTTP_GET,
  172. .handler = hello_status_get_handler,
  173. .user_ctx = NULL,
  174. },
  175. { .uri = "/echo",
  176. .method = HTTP_POST,
  177. .handler = echo_post_handler,
  178. .user_ctx = NULL,
  179. },
  180. { .uri = "/echo",
  181. .method = HTTP_PUT,
  182. .handler = echo_post_handler,
  183. .user_ctx = NULL,
  184. },
  185. { .uri = "/leftover_data",
  186. .method = HTTP_POST,
  187. .handler = leftover_data_post_handler,
  188. .user_ctx = NULL,
  189. },
  190. { .uri = "/adder",
  191. .method = HTTP_POST,
  192. .handler = adder_post_handler,
  193. .user_ctx = NULL,
  194. },
  195. { .uri = "/async_data",
  196. .method = HTTP_GET,
  197. .handler = async_get_handler,
  198. .user_ctx = NULL,
  199. }
  200. };
  201. int basic_handlers_no = sizeof(basic_handlers)/sizeof(httpd_uri_t);
  202. void register_basic_handlers(httpd_handle_t hd)
  203. {
  204. int i;
  205. ESP_LOGI(TAG, "Registering basic handlers");
  206. ESP_LOGI(TAG, "No of handlers = %d", basic_handlers_no);
  207. for (i = 0; i < basic_handlers_no; i++) {
  208. if (httpd_register_uri_handler(hd, &basic_handlers[i]) != ESP_OK) {
  209. ESP_LOGW(TAG, "register uri failed for %d", i);
  210. return;
  211. }
  212. }
  213. ESP_LOGI(TAG, "Success");
  214. }
  215. /********************* Basic Handlers End *******************/
  216. esp_err_t my_hello_post_handler(httpd_req_t *req)
  217. {
  218. char buf[10];
  219. char outbuf[50];
  220. int ret;
  221. ret = httpd_req_recv(req, buf, sizeof(buf));
  222. if (ret < 0) {
  223. return ESP_FAIL;
  224. }
  225. httpd_resp_set_status(req, HTTPD_404);
  226. httpd_resp_set_type(req, HTTPD_TYPE_TEXT);
  227. ESP_LOGI(TAG, "Read %d bytes as:%s:", ret, buf);
  228. buf[ret] = '\0';
  229. #define STR "my_hello_handler"
  230. snprintf(outbuf, sizeof(outbuf), STR" %s", buf);
  231. httpd_resp_send(req, outbuf, strlen(outbuf));
  232. return ESP_OK;
  233. #undef STR
  234. }
  235. /********************* Test Handler Limit Start *******************/
  236. esp_err_t null_func(httpd_req_t *req)
  237. {
  238. return ESP_OK;
  239. }
  240. httpd_uri_t handler_limit_uri (char* path)
  241. {
  242. httpd_uri_t uri = {
  243. .uri = path,
  244. .method = HTTP_GET,
  245. .handler = null_func,
  246. .user_ctx = NULL,
  247. };
  248. return uri;
  249. };
  250. static inline unsigned num_digits(unsigned x)
  251. {
  252. unsigned digits = 1;
  253. while ((x = x/10) != 0) {
  254. digits++;
  255. }
  256. return digits;
  257. }
  258. #define HTTPD_TEST_MAX_URI_HANDLERS 8
  259. void test_handler_limit(httpd_handle_t hd)
  260. {
  261. int i, ret;
  262. char x[HTTPD_TEST_MAX_URI_HANDLERS+1][num_digits(HTTPD_TEST_MAX_URI_HANDLERS)+1];
  263. httpd_uri_t uris[HTTPD_TEST_MAX_URI_HANDLERS+1];
  264. for (i = 0; i < HTTPD_TEST_MAX_URI_HANDLERS + 1; i++) {
  265. sprintf(x[i],"%d",i);
  266. uris[i] = handler_limit_uri(x[i]);
  267. }
  268. /* Register multiple instances of the same handler for MAX URI Handlers */
  269. ESP_LOGI(TAG, "Test: Register Max URI handlers: %d...", HTTPD_TEST_MAX_URI_HANDLERS);
  270. for (i = 0; i < HTTPD_TEST_MAX_URI_HANDLERS; i++) {
  271. ret = httpd_register_uri_handler(hd, &uris[i]);
  272. if (ret != ESP_OK) {
  273. ESP_LOGI(TAG, "Fail");
  274. goto error_ret;
  275. }
  276. }
  277. ESP_LOGI(TAG, "Success");
  278. /* Register the MAX URI + 1 Handlers should fail */
  279. ESP_LOGI(TAG, "Test: Register Max URI + 1 handlers: %d th...", HTTPD_TEST_MAX_URI_HANDLERS +1 );
  280. ret = httpd_register_uri_handler(hd, &uris[HTTPD_TEST_MAX_URI_HANDLERS]);
  281. if (ret == ESP_OK) {
  282. ESP_LOGI(TAG, "Fail");
  283. goto error_ret;
  284. }
  285. ESP_LOGI(TAG, "Success");
  286. /* Unregister the one of the Handler should pass */
  287. ESP_LOGI(TAG, "Test: Unregister 0th handler...");
  288. ret = httpd_unregister_uri_handler(hd, uris[0].uri, uris[0].method);
  289. if (ret != ESP_OK) {
  290. ESP_LOGI(TAG, "Fail");
  291. goto error_ret;
  292. }
  293. ESP_LOGI(TAG, "Success");
  294. /* Unregister non added Handler should fail */
  295. ESP_LOGI(TAG, "Test: Again unregister 0th handler not registered...");
  296. ret = httpd_unregister_uri_handler(hd, uris[0].uri, uris[0].method);
  297. if (ret == ESP_OK) {
  298. ESP_LOGI(TAG, "Fail");
  299. goto error_ret;
  300. }
  301. ESP_LOGI(TAG, "Success");
  302. /* Register the MAX URI Handler should pass */
  303. ESP_LOGI(TAG, "Test: Register back 0th handler...");
  304. ret = httpd_register_uri_handler(hd, &uris[0]);
  305. if (ret != ESP_OK) {
  306. ESP_LOGI(TAG, "Fail");
  307. goto error_ret;
  308. }
  309. ESP_LOGI(TAG, "Success");
  310. /* Reregister same instance of handler should fail */
  311. ESP_LOGI(TAG, "Test: Register 0th handler again after registering...");
  312. ret = httpd_register_uri_handler(hd, &uris[0]);
  313. if (ret == ESP_OK) {
  314. ESP_LOGI(TAG, "Fail");
  315. goto error_ret;
  316. }
  317. ESP_LOGI(TAG, "Success");
  318. /* Register the MAX URI + 1 Handlers should fail */
  319. ESP_LOGI(TAG, "Test: Register 1 more handler...");
  320. ret = httpd_register_uri_handler(hd, &uris[HTTPD_TEST_MAX_URI_HANDLERS]);
  321. if (ret == ESP_OK) {
  322. ESP_LOGI(TAG, "Fail");
  323. goto error_ret;
  324. }
  325. ESP_LOGI(TAG, "Success");
  326. /* Unregister the same handler for MAX URI Handlers */
  327. ESP_LOGI(TAG, "Test: Unregister all handlers:");
  328. for (i = 0; i < HTTPD_TEST_MAX_URI_HANDLERS; i++) {
  329. ret = httpd_unregister_uri_handler(hd, uris[i].uri, uris[i].method);
  330. if (ret != 0) {
  331. ESP_LOGI(TAG, "Fail");
  332. goto error_ret;
  333. }
  334. }
  335. ESP_LOGI(TAG, "Success");
  336. error_ret:
  337. for (; i < HTTPD_TEST_MAX_URI_HANDLERS; i++) {
  338. httpd_unregister_uri_handler(hd, uris[i].uri, uris[i].method);
  339. }
  340. basic_sanity = false;
  341. }
  342. /********************* Test Handler Limit End *******************/
  343. httpd_handle_t test_httpd_start()
  344. {
  345. pre_start_mem = esp_get_free_heap_size();
  346. httpd_handle_t hd;
  347. httpd_config_t config = HTTPD_DEFAULT_CONFIG();
  348. if (httpd_start(&hd, &config) == ESP_OK) {
  349. ESP_LOGI(TAG, "Started HTTP server on port: %d", config.server_port);
  350. ESP_LOGI(TAG, "Max URI handlers: %d", config.max_uri_handlers);
  351. ESP_LOGI(TAG, "Max Open Sessions: %d", config.max_open_sockets);
  352. ESP_LOGI(TAG, "Max Header Length: %d", HTTPD_MAX_REQ_HDR_LEN);
  353. ESP_LOGI(TAG, "Max URI Length: %d", HTTPD_MAX_URI_LEN);
  354. ESP_LOGI(TAG, "Max Stack Size: %d", config.stack_size);
  355. return hd;
  356. }
  357. return NULL;
  358. }
  359. httpd_handle_t test_httpd_start_dummy(uint16_t id)
  360. {
  361. pre_start_mem = esp_get_free_heap_size();
  362. ESP_LOGI(TAG, "HTTPD Start: Current free memory: %d", pre_start_mem);
  363. httpd_handle_t hd;
  364. httpd_config_t config = HTTPD_DEFAULT_CONFIG();
  365. config.max_uri_handlers = HTTPD_TEST_MAX_URI_HANDLERS;
  366. config.server_port += id;
  367. config.ctrl_port += id;
  368. if (httpd_start(&hd, &config) == ESP_OK) {
  369. return hd;
  370. }
  371. return NULL;
  372. }
  373. void test_httpd_stop(httpd_handle_t hd)
  374. {
  375. httpd_stop(hd);
  376. post_stop_mem = esp_get_free_heap_size();
  377. ESP_LOGI(TAG, "HTTPD Stop: Current free memory: %d", post_stop_mem);
  378. // Below function is not available but would be desirable to have
  379. // post_stop_min_mem = os_get_minimum_free_mem();
  380. // ESP_LOGI(TAG, "HTTPD Stop: Minimum free memory: %d", post_stop_min_mem);
  381. }
  382. #define SERVER_INSTANCES 10
  383. /* Currently this only tests for the number of tasks.
  384. * Heap leakage is not tested as LWIP allocates memory
  385. * which may not be freed immedietly causing erroneous
  386. * evaluation. Another test to implement would be the
  387. * monitoring of open sockets, but LWIP presently provides
  388. * no such API for getting the number of open sockets.
  389. */
  390. bool leak_test(void)
  391. {
  392. httpd_handle_t hd[SERVER_INSTANCES];
  393. bool success = true;
  394. unsigned task_count = uxTaskGetNumberOfTasks();
  395. ESP_LOGI(TAG, "Leak Test Started...");
  396. ESP_LOGI(TAG, "Current free heap : %d", xPortGetFreeHeapSize());
  397. ESP_LOGI(TAG, "Total tasks running : %d", task_count);
  398. for (int i = 0; i < SERVER_INSTANCES; i++) {
  399. ESP_LOGI(TAG, "Starting Server Instance [%d]", i);
  400. hd[i] = test_httpd_start_dummy(i);
  401. if (hd[i]) {
  402. register_basic_handlers(hd[i]);
  403. task_count++;
  404. }
  405. ESP_LOGI(TAG, "Current free heap : %d", xPortGetFreeHeapSize());
  406. ESP_LOGI(TAG, "Total tasks running : %d", uxTaskGetNumberOfTasks());
  407. if (uxTaskGetNumberOfTasks() != task_count) {
  408. ESP_LOGE(TAG, "Task count mismatch");
  409. success = false;
  410. break;
  411. }
  412. }
  413. for (int i = 0; i < SERVER_INSTANCES; i++) {
  414. ESP_LOGI(TAG, "Stopping Server Instance [%d]", i);
  415. if (hd[i]) {
  416. httpd_stop(hd[i]);
  417. task_count--;
  418. }
  419. ESP_LOGI(TAG, "Current free heap : %d", xPortGetFreeHeapSize());
  420. ESP_LOGI(TAG, "Total tasks running : %d", uxTaskGetNumberOfTasks());
  421. if (uxTaskGetNumberOfTasks() != task_count) {
  422. ESP_LOGE(TAG, "Task count mismatch");
  423. success = false;
  424. }
  425. }
  426. if (success) {
  427. ESP_LOGI(TAG, "Leak Test Passed");
  428. }
  429. else {
  430. ESP_LOGI(TAG, "Leak Test Failed");
  431. }
  432. return success;
  433. }
  434. httpd_handle_t start_tests()
  435. {
  436. leak_test();
  437. httpd_handle_t hd = test_httpd_start();
  438. if (hd) {
  439. test_handler_limit(hd);
  440. register_basic_handlers(hd);
  441. }
  442. return hd;
  443. }
  444. void stop_tests(httpd_handle_t hd)
  445. {
  446. ESP_LOGI(TAG, "Stopping httpd");
  447. test_httpd_stop(hd);
  448. }