test.c 6.5 KB

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