test.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // Copyright 2015-2016 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. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <unistd.h>
  16. #include <signal.h>
  17. #include <string.h>
  18. #include "mdns.h"
  19. #include "mdns_private.h"
  20. //
  21. // Global stuctures containing packet payload, search
  22. mdns_rx_packet_t g_packet;
  23. struct pbuf mypbuf;
  24. mdns_search_once_t * search = NULL;
  25. //
  26. // Dependency injected test functions
  27. void mdns_test_execute_action(void * action);
  28. mdns_srv_item_t * mdns_test_mdns_get_service_item(const char * service, const char * proto);
  29. 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);
  30. esp_err_t mdns_test_send_search_action(mdns_action_type_t type, mdns_search_once_t * search);
  31. void mdns_test_search_free(mdns_search_once_t * search);
  32. void mdns_test_init_di(void);
  33. //
  34. // mdns function wrappers for mdns setup in test mode
  35. static int mdns_test_hostname_set(const char * mdns_hostname)
  36. {
  37. int ret = mdns_hostname_set(mdns_hostname);
  38. mdns_action_t * a = NULL;
  39. GetLastItem(&a);
  40. mdns_test_execute_action(a);
  41. return ret;
  42. }
  43. static int mdns_test_service_instance_name_set(const char * service, const char * proto, const char * instance)
  44. {
  45. int ret = mdns_service_instance_name_set(service, proto, instance);
  46. mdns_action_t * a = NULL;
  47. GetLastItem(&a);
  48. mdns_test_execute_action(a);
  49. return ret;
  50. }
  51. static int mdns_test_service_txt_set(const char * service, const char * proto, uint8_t num_items, mdns_txt_item_t txt[])
  52. {
  53. int ret = mdns_service_txt_set(service, proto, txt, num_items);
  54. mdns_action_t * a = NULL;
  55. GetLastItem(&a);
  56. mdns_test_execute_action(a);
  57. return ret;
  58. }
  59. static int mdns_test_service_add(const char * service_name, const char * proto, uint32_t port)
  60. {
  61. if (mdns_service_add(NULL, service_name, proto, port, NULL, 0)) {
  62. // This is expected failure as the service thread is not running
  63. }
  64. mdns_action_t * a = NULL;
  65. GetLastItem(&a);
  66. mdns_test_execute_action(a);
  67. if (mdns_test_mdns_get_service_item(service_name, proto)==NULL) {
  68. return ESP_FAIL;
  69. }
  70. return ESP_OK;
  71. }
  72. static mdns_result_t* mdns_test_query(const char * service_name, const char * proto)
  73. {
  74. search = mdns_test_search_init(NULL, service_name, proto, MDNS_TYPE_PTR, 3000, 20);
  75. if (!search) {
  76. abort();
  77. }
  78. if (mdns_test_send_search_action(ACTION_SEARCH_ADD, search)) {
  79. mdns_test_search_free(search);
  80. abort();
  81. }
  82. mdns_action_t * a = NULL;
  83. GetLastItem(&a);
  84. mdns_test_execute_action(a);
  85. return NULL;
  86. }
  87. static void mdns_test_query_free(void)
  88. {
  89. mdns_test_search_free(search);
  90. }
  91. //
  92. // function "under test" where afl-mangled packets passed
  93. //
  94. void mdns_parse_packet(mdns_rx_packet_t * packet);
  95. //
  96. // Test starts here
  97. //
  98. int main(int argc, char** argv)
  99. {
  100. int i;
  101. const char * mdns_hostname = "minifritz";
  102. const char * mdns_instance = "Hristo's Time Capsule";
  103. mdns_txt_item_t arduTxtData[4] = {
  104. {"board","esp32"},
  105. {"tcp_check","no"},
  106. {"ssh_upload","no"},
  107. {"auth_upload","no"}
  108. };
  109. const uint8_t mac[6] = {0xDE, 0xAD, 0xBE, 0xEF, 0x00, 0x32};
  110. uint8_t buf[1460];
  111. char winstance[21+strlen(mdns_hostname)];
  112. sprintf(winstance, "%s [%02x:%02x:%02x:%02x:%02x:%02x]", mdns_hostname, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  113. // Init depencency injected methods
  114. mdns_test_init_di();
  115. if (mdns_init()) {
  116. abort();
  117. }
  118. if (mdns_test_hostname_set(mdns_hostname)) {
  119. abort();
  120. }
  121. if (mdns_test_service_add("_workstation", "_tcp", 9)) {
  122. abort();
  123. }
  124. if (mdns_test_service_instance_name_set("_workstation", "_tcp", winstance)) {
  125. abort();
  126. }
  127. if (mdns_test_service_add("_arduino", "_tcp", 3232)) {
  128. abort();
  129. }
  130. if (mdns_test_service_txt_set("_arduino", "_tcp", 4, arduTxtData)) {
  131. abort();
  132. }
  133. if (mdns_test_service_add("_http", "_tcp", 80)) {
  134. abort();
  135. }
  136. if (mdns_test_service_instance_name_set("_http", "_tcp", "ESP WebServer")) {
  137. abort();
  138. }
  139. if (
  140. mdns_test_service_add("_afpovertcp", "_tcp", 548)
  141. || mdns_test_service_add("_rfb", "_tcp", 885)
  142. || mdns_test_service_add("_smb", "_tcp", 885)
  143. || mdns_test_service_add("_adisk", "_tcp", 885)
  144. || mdns_test_service_add("_airport", "_tcp", 885)
  145. || mdns_test_service_add("_printer", "_tcp", 885)
  146. || mdns_test_service_add("_airplay", "_tcp", 885)
  147. || mdns_test_service_add("_raop", "_tcp", 885)
  148. || mdns_test_service_add("_uscan", "_tcp", 885)
  149. || mdns_test_service_add("_uscans", "_tcp", 885)
  150. || mdns_test_service_add("_ippusb", "_tcp", 885)
  151. || mdns_test_service_add("_scanner", "_tcp", 885)
  152. || mdns_test_service_add("_ipp", "_tcp", 885)
  153. || mdns_test_service_add("_ipps", "_tcp", 885)
  154. || mdns_test_service_add("_pdl-datastream", "_tcp", 885)
  155. || mdns_test_service_add("_ptp", "_tcp", 885)
  156. || mdns_test_service_add("_sleep-proxy", "_udp", 885))
  157. {
  158. abort();
  159. }
  160. mdns_result_t * results = NULL;
  161. FILE *file;
  162. size_t nread;
  163. #ifdef INSTR_IS_OFF
  164. size_t len = 1460;
  165. memset(buf, 0, 1460);
  166. if (argc != 2)
  167. {
  168. printf("Non-instrumentation mode: please supply a file name created by AFL to reproduce crash\n");
  169. return 1;
  170. }
  171. else
  172. {
  173. //
  174. // Note: parameter1 is a file (mangled packet) which caused the crash
  175. file = fopen(argv[1], "r");
  176. assert(file >= 0 );
  177. fclose(file);
  178. }
  179. for (i=0; i<1; i++) {
  180. #else
  181. while (__AFL_LOOP(1000)) {
  182. memset(buf, 0, 1460);
  183. size_t len = read(0, buf, 1460);
  184. #endif
  185. mypbuf.payload = buf;
  186. mypbuf.len = len;
  187. g_packet.pb = &mypbuf;
  188. mdns_test_query("_afpovertcp", "_tcp");
  189. mdns_parse_packet(&g_packet);
  190. }
  191. ForceTaskDelete();
  192. mdns_free();
  193. return 0;
  194. }