test.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <unistd.h>
  9. #include <signal.h>
  10. #include <string.h>
  11. #include "esp32_mock.h"
  12. #include "mdns.h"
  13. #include "mdns_private.h"
  14. //
  15. // Global stuctures containing packet payload, search
  16. mdns_rx_packet_t g_packet;
  17. struct pbuf mypbuf;
  18. mdns_search_once_t *search = NULL;
  19. //
  20. // Dependency injected test functions
  21. void mdns_test_execute_action(void *action);
  22. mdns_srv_item_t *mdns_test_mdns_get_service_item(const char *service, const char *proto);
  23. mdns_search_once_t *mdns_test_search_init(const char *name, const char *service, const char *proto, uint16_t type, uint32_t timeout, uint8_t max_results);
  24. esp_err_t mdns_test_send_search_action(mdns_action_type_t type, mdns_search_once_t *search);
  25. void mdns_test_search_free(mdns_search_once_t *search);
  26. void mdns_test_init_di(void);
  27. extern mdns_server_t *_mdns_server;
  28. //
  29. // mdns function wrappers for mdns setup in test mode
  30. static int mdns_test_hostname_set(const char *mdns_hostname)
  31. {
  32. for (int i = 0; i < MDNS_MAX_INTERFACES; i++) {
  33. _mdns_server->interfaces[i].pcbs[MDNS_IP_PROTOCOL_V4].state = PCB_RUNNING; // mark the PCB running to exercise mdns in fully operational mode
  34. _mdns_server->interfaces[i].pcbs[MDNS_IP_PROTOCOL_V6].state = PCB_RUNNING;
  35. }
  36. int ret = mdns_hostname_set(mdns_hostname);
  37. mdns_action_t *a = NULL;
  38. GetLastItem(&a);
  39. mdns_test_execute_action(a);
  40. return ret;
  41. }
  42. static int mdns_test_add_delegated_host(const char *mdns_hostname)
  43. {
  44. mdns_ip_addr_t addr = { .addr = { .u_addr = ESP_IPADDR_TYPE_V4 } };
  45. addr.addr.u_addr.ip4.addr = 0x11111111;
  46. int ret = mdns_delegate_hostname_add(mdns_hostname, &addr);
  47. mdns_action_t *a = NULL;
  48. GetLastItem(&a);
  49. mdns_test_execute_action(a);
  50. return ret;
  51. }
  52. static int mdns_test_service_instance_name_set(const char *service, const char *proto, const char *instance)
  53. {
  54. int ret = mdns_service_instance_name_set(service, proto, instance);
  55. mdns_action_t *a = NULL;
  56. GetLastItem(&a);
  57. mdns_test_execute_action(a);
  58. return ret;
  59. }
  60. static int mdns_test_service_txt_set(const char *service, const char *proto, uint8_t num_items, mdns_txt_item_t txt[])
  61. {
  62. int ret = mdns_service_txt_set(service, proto, txt, num_items);
  63. mdns_action_t *a = NULL;
  64. GetLastItem(&a);
  65. mdns_test_execute_action(a);
  66. return ret;
  67. }
  68. static int mdns_test_sub_service_add(const char *sub_name, const char *service_name, const char *proto, uint32_t port)
  69. {
  70. if (mdns_service_add(NULL, service_name, proto, port, NULL, 0)) {
  71. // This is expected failure as the service thread is not running
  72. }
  73. mdns_action_t *a = NULL;
  74. GetLastItem(&a);
  75. mdns_test_execute_action(a);
  76. if (mdns_test_mdns_get_service_item(service_name, proto) == NULL) {
  77. return ESP_FAIL;
  78. }
  79. int ret = mdns_service_subtype_add_for_host(NULL, service_name, proto, NULL, sub_name);
  80. a = NULL;
  81. GetLastItem(&a);
  82. mdns_test_execute_action(a);
  83. return ret;
  84. }
  85. static int mdns_test_service_add(const char *service_name, const char *proto, uint32_t port)
  86. {
  87. if (mdns_service_add(NULL, service_name, proto, port, NULL, 0)) {
  88. // This is expected failure as the service thread is not running
  89. }
  90. mdns_action_t *a = NULL;
  91. GetLastItem(&a);
  92. mdns_test_execute_action(a);
  93. if (mdns_test_mdns_get_service_item(service_name, proto) == NULL) {
  94. return ESP_FAIL;
  95. }
  96. return ESP_OK;
  97. }
  98. static mdns_result_t *mdns_test_query(const char *name, const char *service, const char *proto, uint16_t type)
  99. {
  100. search = mdns_test_search_init(name, service, proto, type, 3000, 20);
  101. if (!search) {
  102. abort();
  103. }
  104. if (mdns_test_send_search_action(ACTION_SEARCH_ADD, search)) {
  105. mdns_test_search_free(search);
  106. abort();
  107. }
  108. mdns_action_t *a = NULL;
  109. GetLastItem(&a);
  110. mdns_test_execute_action(a);
  111. return NULL;
  112. }
  113. static void mdns_test_query_free(void)
  114. {
  115. mdns_test_search_free(search);
  116. }
  117. //
  118. // function "under test" where afl-mangled packets passed
  119. //
  120. void mdns_parse_packet(mdns_rx_packet_t *packet);
  121. //
  122. // Test starts here
  123. //
  124. int main(int argc, char **argv)
  125. {
  126. int i;
  127. const char *mdns_hostname = "minifritz";
  128. const char *mdns_instance = "Hristo's Time Capsule";
  129. mdns_txt_item_t arduTxtData[4] = {
  130. {"board", "esp32"},
  131. {"tcp_check", "no"},
  132. {"ssh_upload", "no"},
  133. {"auth_upload", "no"}
  134. };
  135. const uint8_t mac[6] = {0xDE, 0xAD, 0xBE, 0xEF, 0x00, 0x32};
  136. uint8_t buf[1460];
  137. char winstance[21 + strlen(mdns_hostname)];
  138. sprintf(winstance, "%s [%02x:%02x:%02x:%02x:%02x:%02x]", mdns_hostname, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  139. // Init depencency injected methods
  140. mdns_test_init_di();
  141. if (mdns_init()) {
  142. abort();
  143. }
  144. if (mdns_test_hostname_set(mdns_hostname)) {
  145. abort();
  146. }
  147. if (mdns_test_add_delegated_host(mdns_hostname) || mdns_test_add_delegated_host("megafritz")) {
  148. abort();
  149. }
  150. #ifndef MDNS_NO_SERVICES
  151. if (mdns_test_sub_service_add("_server", "_fritz", "_tcp", 22)) {
  152. abort();
  153. }
  154. if (mdns_test_service_add("_telnet", "_tcp", 22)) {
  155. abort();
  156. }
  157. if (mdns_test_service_add("_workstation", "_tcp", 9)) {
  158. abort();
  159. }
  160. if (mdns_test_service_instance_name_set("_workstation", "_tcp", winstance)) {
  161. abort();
  162. }
  163. if (mdns_test_service_add("_arduino", "_tcp", 3232)) {
  164. abort();
  165. }
  166. if (mdns_test_service_txt_set("_arduino", "_tcp", 4, arduTxtData)) {
  167. abort();
  168. }
  169. if (mdns_test_service_add("_http", "_tcp", 80)) {
  170. abort();
  171. }
  172. if (mdns_test_service_instance_name_set("_http", "_tcp", "ESP WebServer")) {
  173. abort();
  174. }
  175. if (
  176. mdns_test_service_add("_afpovertcp", "_tcp", 548)
  177. || mdns_test_service_add("_rfb", "_tcp", 885)
  178. || mdns_test_service_add("_smb", "_tcp", 885)
  179. || mdns_test_service_add("_adisk", "_tcp", 885)
  180. || mdns_test_service_add("_airport", "_tcp", 885)
  181. || mdns_test_service_add("_printer", "_tcp", 885)
  182. || mdns_test_service_add("_airplay", "_tcp", 885)
  183. || mdns_test_service_add("_raop", "_tcp", 885)
  184. || mdns_test_service_add("_uscan", "_tcp", 885)
  185. || mdns_test_service_add("_uscans", "_tcp", 885)
  186. || mdns_test_service_add("_ippusb", "_tcp", 885)
  187. || mdns_test_service_add("_scanner", "_tcp", 885)
  188. || mdns_test_service_add("_ipp", "_tcp", 885)
  189. || mdns_test_service_add("_ipps", "_tcp", 885)
  190. || mdns_test_service_add("_pdl-datastream", "_tcp", 885)
  191. || mdns_test_service_add("_ptp", "_tcp", 885)
  192. || mdns_test_service_add("_sleep-proxy", "_udp", 885)) {
  193. abort();
  194. }
  195. #endif
  196. mdns_result_t *results = NULL;
  197. FILE *file;
  198. size_t nread;
  199. #ifdef INSTR_IS_OFF
  200. size_t len = 1460;
  201. memset(buf, 0, 1460);
  202. if (argc != 2) {
  203. printf("Non-instrumentation mode: please supply a file name created by AFL to reproduce crash\n");
  204. return 1;
  205. } else {
  206. //
  207. // Note: parameter1 is a file (mangled packet) which caused the crash
  208. file = fopen(argv[1], "r");
  209. assert(file >= 0 );
  210. len = fread(buf, 1, 1460, file);
  211. fclose(file);
  212. }
  213. for (i = 0; i < 1; i++) {
  214. #else
  215. while (__AFL_LOOP(1000)) {
  216. memset(buf, 0, 1460);
  217. size_t len = read(0, buf, 1460);
  218. #endif
  219. mypbuf.payload = malloc(len);
  220. memcpy(mypbuf.payload, buf, len);
  221. mypbuf.len = len;
  222. g_packet.pb = &mypbuf;
  223. mdns_test_query("minifritz", "_fritz", "_tcp", MDNS_TYPE_ANY);
  224. mdns_test_query(NULL, "_fritz", "_tcp", MDNS_TYPE_PTR);
  225. mdns_test_query(NULL, "_afpovertcp", "_tcp", MDNS_TYPE_PTR);
  226. mdns_parse_packet(&g_packet);
  227. free(mypbuf.payload);
  228. }
  229. #ifndef MDNS_NO_SERVICES
  230. mdns_service_remove_all();
  231. mdns_action_t *a = NULL;
  232. GetLastItem(&a);
  233. mdns_test_execute_action(a);
  234. #endif
  235. ForceTaskDelete();
  236. mdns_free();
  237. return 0;
  238. }