test_mdns.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  1. /*
  2. * Copyright (c) 2015 Verisure Innovation AB
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  19. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  21. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  24. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  25. * OF SUCH DAMAGE.
  26. *
  27. * This file is part of the lwIP TCP/IP stack.
  28. *
  29. * Author: Erik Ekman <[email protected]>
  30. *
  31. */
  32. #include "test_mdns.h"
  33. #include "lwip/pbuf.h"
  34. #include "lwip/apps/mdns.h"
  35. #include "lwip/apps/mdns_priv.h"
  36. START_TEST(readname_basic)
  37. {
  38. static const u8_t data[] = { 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't', 0x00 };
  39. struct pbuf *p;
  40. struct mdns_domain domain;
  41. u16_t offset;
  42. LWIP_UNUSED_ARG(_i);
  43. p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
  44. p->payload = (void *)(size_t)data;
  45. fail_if(p == NULL);
  46. offset = mdns_readname(p, 0, &domain);
  47. pbuf_free(p);
  48. fail_unless(offset == sizeof(data));
  49. fail_unless(domain.length == sizeof(data));
  50. fail_if(memcmp(&domain.name, data, sizeof(data)));
  51. }
  52. END_TEST
  53. START_TEST(readname_anydata)
  54. {
  55. static const u8_t data[] = { 0x05, 0x00, 0xFF, 0x08, 0xc0, 0x0f, 0x04, 0x7f, 0x80, 0x82, 0x88, 0x00 };
  56. struct pbuf *p;
  57. struct mdns_domain domain;
  58. u16_t offset;
  59. LWIP_UNUSED_ARG(_i);
  60. p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
  61. p->payload = (void *)(size_t)data;
  62. fail_if(p == NULL);
  63. offset = mdns_readname(p, 0, &domain);
  64. pbuf_free(p);
  65. fail_unless(offset == sizeof(data));
  66. fail_unless(domain.length == sizeof(data));
  67. fail_if(memcmp(&domain.name, data, sizeof(data)));
  68. }
  69. END_TEST
  70. START_TEST(readname_short_buf)
  71. {
  72. static const u8_t data[] = { 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a' };
  73. struct pbuf *p;
  74. struct mdns_domain domain;
  75. u16_t offset;
  76. LWIP_UNUSED_ARG(_i);
  77. p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
  78. p->payload = (void *)(size_t)data;
  79. fail_if(p == NULL);
  80. offset = mdns_readname(p, 0, &domain);
  81. pbuf_free(p);
  82. fail_unless(offset == MDNS_READNAME_ERROR);
  83. }
  84. END_TEST
  85. START_TEST(readname_long_label)
  86. {
  87. static const u8_t data[] = {
  88. 0x05, 'm', 'u', 'l', 't', 'i',
  89. 0x52, 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a',
  90. 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a',
  91. 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a',
  92. 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a',
  93. 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a',
  94. 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 0x00
  95. };
  96. struct pbuf *p;
  97. struct mdns_domain domain;
  98. u16_t offset;
  99. LWIP_UNUSED_ARG(_i);
  100. p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
  101. p->payload = (void *)(size_t)data;
  102. fail_if(p == NULL);
  103. offset = mdns_readname(p, 0, &domain);
  104. pbuf_free(p);
  105. fail_unless(offset == MDNS_READNAME_ERROR);
  106. }
  107. END_TEST
  108. START_TEST(readname_overflow)
  109. {
  110. static const u8_t data[] = {
  111. 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
  112. 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
  113. 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
  114. 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
  115. 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
  116. 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
  117. 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
  118. 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
  119. 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
  120. 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
  121. 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
  122. 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
  123. 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
  124. 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
  125. 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
  126. 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
  127. 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
  128. 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
  129. 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
  130. 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
  131. 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
  132. 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
  133. 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
  134. 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
  135. 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
  136. 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
  137. 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
  138. 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
  139. 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
  140. 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
  141. 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
  142. 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
  143. 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
  144. 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
  145. 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
  146. 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
  147. 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
  148. 0x00
  149. };
  150. struct pbuf *p;
  151. struct mdns_domain domain;
  152. u16_t offset;
  153. LWIP_UNUSED_ARG(_i);
  154. p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
  155. p->payload = (void *)(size_t)data;
  156. fail_if(p == NULL);
  157. offset = mdns_readname(p, 0, &domain);
  158. pbuf_free(p);
  159. fail_unless(offset == MDNS_READNAME_ERROR);
  160. }
  161. END_TEST
  162. START_TEST(readname_jump_earlier)
  163. {
  164. static const u8_t data[] = {
  165. /* Some padding needed, not supported to jump to bytes containing dns header */
  166. /* 0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  167. /* 10 */ 0x0f, 0x0e, 0x05, 'l', 'o', 'c', 'a', 'l', 0x00, 0xab,
  168. /* 20 */ 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't', 0xc0, 0x0c
  169. };
  170. static const u8_t fullname[] = {
  171. 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't', 0x05, 'l', 'o', 'c', 'a', 'l', 0x00
  172. };
  173. struct pbuf *p;
  174. struct mdns_domain domain;
  175. u16_t offset;
  176. LWIP_UNUSED_ARG(_i);
  177. p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
  178. p->payload = (void *)(size_t)data;
  179. fail_if(p == NULL);
  180. offset = mdns_readname(p, 20, &domain);
  181. pbuf_free(p);
  182. fail_unless(offset == sizeof(data));
  183. fail_unless(domain.length == sizeof(fullname));
  184. fail_if(memcmp(&domain.name, fullname, sizeof(fullname)));
  185. }
  186. END_TEST
  187. START_TEST(readname_jump_earlier_jump)
  188. {
  189. static const u8_t data[] = {
  190. /* Some padding needed, not supported to jump to bytes containing dns header */
  191. /* 0x00 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  192. /* 0x08 */ 0x00, 0x00, 0x00, 0x00, 0x03, 0x0b, 0x0a, 0xf2,
  193. /* 0x10 */ 0x04, 'c', 'a', 's', 't', 0x00, 0xc0, 0x10,
  194. /* 0x18 */ 0x05, 'm', 'u', 'l', 't', 'i', 0xc0, 0x16
  195. };
  196. static const u8_t fullname[] = {
  197. 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't', 0x00
  198. };
  199. struct pbuf *p;
  200. struct mdns_domain domain;
  201. u16_t offset;
  202. LWIP_UNUSED_ARG(_i);
  203. p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
  204. p->payload = (void *)(size_t)data;
  205. fail_if(p == NULL);
  206. offset = mdns_readname(p, 0x18, &domain);
  207. pbuf_free(p);
  208. fail_unless(offset == sizeof(data));
  209. fail_unless(domain.length == sizeof(fullname));
  210. fail_if(memcmp(&domain.name, fullname, sizeof(fullname)));
  211. }
  212. END_TEST
  213. START_TEST(readname_jump_maxdepth)
  214. {
  215. static const u8_t data[] = {
  216. /* Some padding needed, not supported to jump to bytes containing dns header */
  217. /* 0x00 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  218. /* 0x08 */ 0x00, 0x00, 0x00, 0x00, 0x03, 0x0b, 0x0a, 0xf2,
  219. /* 0x10 */ 0x04, 'n', 'a', 'm', 'e', 0xc0, 0x27, 0x03,
  220. /* 0x18 */ 0x03, 'd', 'n', 's', 0xc0, 0x10, 0xc0, 0x10,
  221. /* 0x20 */ 0x04, 'd', 'e', 'e', 'p', 0xc0, 0x18, 0x00,
  222. /* 0x28 */ 0x04, 'c', 'a', 's', 't', 0xc0, 0x20, 0xb0,
  223. /* 0x30 */ 0x05, 'm', 'u', 'l', 't', 'i', 0xc0, 0x28
  224. };
  225. static const u8_t fullname[] = {
  226. 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
  227. 0x04, 'd', 'e', 'e', 'p', 0x03, 'd', 'n', 's',
  228. 0x04, 'n', 'a', 'm', 'e', 0x00
  229. };
  230. struct pbuf *p;
  231. struct mdns_domain domain;
  232. u16_t offset;
  233. LWIP_UNUSED_ARG(_i);
  234. p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
  235. p->payload = (void *)(size_t)data;
  236. fail_if(p == NULL);
  237. offset = mdns_readname(p, 0x30, &domain);
  238. pbuf_free(p);
  239. fail_unless(offset == sizeof(data));
  240. fail_unless(domain.length == sizeof(fullname));
  241. fail_if(memcmp(&domain.name, fullname, sizeof(fullname)));
  242. }
  243. END_TEST
  244. START_TEST(readname_jump_later)
  245. {
  246. static const u8_t data[] = {
  247. /* 0x00 */ 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't', 0xc0, 0x10, 0x00, 0x01, 0x40,
  248. /* 0x10 */ 0x05, 'l', 'o', 'c', 'a', 'l', 0x00, 0xab
  249. };
  250. static const u8_t fullname[] = {
  251. 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't', 0x05, 'l', 'o', 'c', 'a', 'l', 0x00
  252. };
  253. struct pbuf *p;
  254. struct mdns_domain domain;
  255. u16_t offset;
  256. LWIP_UNUSED_ARG(_i);
  257. p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
  258. p->payload = (void *)(size_t)data;
  259. fail_if(p == NULL);
  260. offset = mdns_readname(p, 0, &domain);
  261. pbuf_free(p);
  262. fail_unless(offset == 13);
  263. fail_unless(domain.length == sizeof(fullname));
  264. fail_if(memcmp(&domain.name, fullname, sizeof(fullname)));
  265. }
  266. END_TEST
  267. START_TEST(readname_half_jump)
  268. {
  269. static const u8_t data[] = {
  270. 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't', 0xc0
  271. };
  272. struct pbuf *p;
  273. struct mdns_domain domain;
  274. u16_t offset;
  275. LWIP_UNUSED_ARG(_i);
  276. p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
  277. p->payload = (void *)(size_t)data;
  278. fail_if(p == NULL);
  279. offset = mdns_readname(p, 0, &domain);
  280. pbuf_free(p);
  281. fail_unless(offset == MDNS_READNAME_ERROR);
  282. }
  283. END_TEST
  284. START_TEST(readname_jump_toolong)
  285. {
  286. static const u8_t data[] = {
  287. 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't', 0xc2, 0x10, 0x00, 0x01, 0x40
  288. };
  289. struct pbuf *p;
  290. struct mdns_domain domain;
  291. u16_t offset;
  292. LWIP_UNUSED_ARG(_i);
  293. p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
  294. p->payload = (void *)(size_t)data;
  295. fail_if(p == NULL);
  296. offset = mdns_readname(p, 0, &domain);
  297. pbuf_free(p);
  298. fail_unless(offset == MDNS_READNAME_ERROR);
  299. }
  300. END_TEST
  301. START_TEST(readname_jump_loop_label)
  302. {
  303. static const u8_t data[] = {
  304. /* 0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  305. /* 10 */ 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't', 0xc0, 0x10
  306. };
  307. struct pbuf *p;
  308. struct mdns_domain domain;
  309. u16_t offset;
  310. LWIP_UNUSED_ARG(_i);
  311. p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
  312. p->payload = (void *)(size_t)data;
  313. fail_if(p == NULL);
  314. offset = mdns_readname(p, 10, &domain);
  315. pbuf_free(p);
  316. fail_unless(offset == MDNS_READNAME_ERROR);
  317. }
  318. END_TEST
  319. START_TEST(readname_jump_loop_jump)
  320. {
  321. static const u8_t data[] = {
  322. /* 0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  323. /* 10 */ 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't', 0xc0, 0x15
  324. };
  325. struct pbuf *p;
  326. struct mdns_domain domain;
  327. u16_t offset;
  328. LWIP_UNUSED_ARG(_i);
  329. p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
  330. p->payload = (void *)(size_t)data;
  331. fail_if(p == NULL);
  332. offset = mdns_readname(p, 10, &domain);
  333. pbuf_free(p);
  334. fail_unless(offset == MDNS_READNAME_ERROR);
  335. }
  336. END_TEST
  337. START_TEST(add_label_basic)
  338. {
  339. static const u8_t data[] = { 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't', 0x00 };
  340. struct mdns_domain domain;
  341. err_t res;
  342. LWIP_UNUSED_ARG(_i);
  343. memset(&domain, 0, sizeof(domain));
  344. res = mdns_domain_add_label(&domain, "multi", 5);
  345. fail_unless(res == ERR_OK);
  346. res = mdns_domain_add_label(&domain, "cast", 4);
  347. fail_unless(res == ERR_OK);
  348. res = mdns_domain_add_label(&domain, NULL, 0);
  349. fail_unless(res == ERR_OK);
  350. fail_unless(domain.length == sizeof(data));
  351. fail_if(memcmp(&domain.name, data, sizeof(data)));
  352. }
  353. END_TEST
  354. START_TEST(add_label_long_label)
  355. {
  356. static const char *toolong = "abcdefghijklmnopqrstuvwxyz0123456789-abcdefghijklmnopqrstuvwxyz0123456789-abcdefghijklmnopqrstuvwxyz0123456789-";
  357. struct mdns_domain domain;
  358. err_t res;
  359. LWIP_UNUSED_ARG(_i);
  360. memset(&domain, 0, sizeof(domain));
  361. res = mdns_domain_add_label(&domain, "multi", 5);
  362. fail_unless(res == ERR_OK);
  363. res = mdns_domain_add_label(&domain, toolong, (u8_t)strlen(toolong));
  364. fail_unless(res == ERR_VAL);
  365. }
  366. END_TEST
  367. START_TEST(add_label_full)
  368. {
  369. static const char *label = "0123456789abcdef0123456789abcdef";
  370. struct mdns_domain domain;
  371. err_t res;
  372. LWIP_UNUSED_ARG(_i);
  373. memset(&domain, 0, sizeof(domain));
  374. res = mdns_domain_add_label(&domain, label, (u8_t)strlen(label));
  375. fail_unless(res == ERR_OK);
  376. fail_unless(domain.length == 33);
  377. res = mdns_domain_add_label(&domain, label, (u8_t)strlen(label));
  378. fail_unless(res == ERR_OK);
  379. fail_unless(domain.length == 66);
  380. res = mdns_domain_add_label(&domain, label, (u8_t)strlen(label));
  381. fail_unless(res == ERR_OK);
  382. fail_unless(domain.length == 99);
  383. res = mdns_domain_add_label(&domain, label, (u8_t)strlen(label));
  384. fail_unless(res == ERR_OK);
  385. fail_unless(domain.length == 132);
  386. res = mdns_domain_add_label(&domain, label, (u8_t)strlen(label));
  387. fail_unless(res == ERR_OK);
  388. fail_unless(domain.length == 165);
  389. res = mdns_domain_add_label(&domain, label, (u8_t)strlen(label));
  390. fail_unless(res == ERR_OK);
  391. fail_unless(domain.length == 198);
  392. res = mdns_domain_add_label(&domain, label, (u8_t)strlen(label));
  393. fail_unless(res == ERR_OK);
  394. fail_unless(domain.length == 231);
  395. res = mdns_domain_add_label(&domain, label, (u8_t)strlen(label));
  396. fail_unless(res == ERR_VAL);
  397. fail_unless(domain.length == 231);
  398. res = mdns_domain_add_label(&domain, label, 25);
  399. fail_unless(res == ERR_VAL);
  400. fail_unless(domain.length == 231);
  401. res = mdns_domain_add_label(&domain, label, 24);
  402. fail_unless(res == ERR_VAL);
  403. fail_unless(domain.length == 231);
  404. res = mdns_domain_add_label(&domain, label, 23);
  405. fail_unless(res == ERR_OK);
  406. fail_unless(domain.length == 255);
  407. res = mdns_domain_add_label(&domain, NULL, 0);
  408. fail_unless(res == ERR_OK);
  409. fail_unless(domain.length == 256);
  410. res = mdns_domain_add_label(&domain, NULL, 0);
  411. fail_unless(res == ERR_VAL);
  412. fail_unless(domain.length == 256);
  413. }
  414. END_TEST
  415. START_TEST(domain_eq_basic)
  416. {
  417. static const u8_t data[] = {
  418. 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't', 0x00
  419. };
  420. struct mdns_domain domain1, domain2;
  421. err_t res;
  422. LWIP_UNUSED_ARG(_i);
  423. memset(&domain1, 0, sizeof(domain1));
  424. res = mdns_domain_add_label(&domain1, "multi", 5);
  425. fail_unless(res == ERR_OK);
  426. res = mdns_domain_add_label(&domain1, "cast", 4);
  427. fail_unless(res == ERR_OK);
  428. res = mdns_domain_add_label(&domain1, NULL, 0);
  429. fail_unless(res == ERR_OK);
  430. fail_unless(domain1.length == sizeof(data));
  431. memset(&domain2, 0, sizeof(domain2));
  432. res = mdns_domain_add_label(&domain2, "multi", 5);
  433. fail_unless(res == ERR_OK);
  434. res = mdns_domain_add_label(&domain2, "cast", 4);
  435. fail_unless(res == ERR_OK);
  436. res = mdns_domain_add_label(&domain2, NULL, 0);
  437. fail_unless(res == ERR_OK);
  438. fail_unless(mdns_domain_eq(&domain1, &domain2));
  439. }
  440. END_TEST
  441. START_TEST(domain_eq_diff)
  442. {
  443. struct mdns_domain domain1, domain2;
  444. err_t res;
  445. LWIP_UNUSED_ARG(_i);
  446. memset(&domain1, 0, sizeof(domain1));
  447. res = mdns_domain_add_label(&domain1, "multi", 5);
  448. fail_unless(res == ERR_OK);
  449. res = mdns_domain_add_label(&domain1, "base", 4);
  450. fail_unless(res == ERR_OK);
  451. res = mdns_domain_add_label(&domain1, NULL, 0);
  452. fail_unless(res == ERR_OK);
  453. memset(&domain2, 0, sizeof(domain2));
  454. res = mdns_domain_add_label(&domain2, "multi", 5);
  455. fail_unless(res == ERR_OK);
  456. res = mdns_domain_add_label(&domain2, "cast", 4);
  457. fail_unless(res == ERR_OK);
  458. res = mdns_domain_add_label(&domain2, NULL, 0);
  459. fail_unless(res == ERR_OK);
  460. fail_if(mdns_domain_eq(&domain1, &domain2));
  461. }
  462. END_TEST
  463. START_TEST(domain_eq_case)
  464. {
  465. struct mdns_domain domain1, domain2;
  466. err_t res;
  467. LWIP_UNUSED_ARG(_i);
  468. memset(&domain1, 0, sizeof(domain1));
  469. res = mdns_domain_add_label(&domain1, "multi", 5);
  470. fail_unless(res == ERR_OK);
  471. res = mdns_domain_add_label(&domain1, "cast", 4);
  472. fail_unless(res == ERR_OK);
  473. res = mdns_domain_add_label(&domain1, NULL, 0);
  474. fail_unless(res == ERR_OK);
  475. memset(&domain2, 0, sizeof(domain2));
  476. res = mdns_domain_add_label(&domain2, "MulTI", 5);
  477. fail_unless(res == ERR_OK);
  478. res = mdns_domain_add_label(&domain2, "casT", 4);
  479. fail_unless(res == ERR_OK);
  480. res = mdns_domain_add_label(&domain2, NULL, 0);
  481. fail_unless(res == ERR_OK);
  482. fail_unless(mdns_domain_eq(&domain1, &domain2));
  483. }
  484. END_TEST
  485. START_TEST(domain_eq_anydata)
  486. {
  487. static const u8_t data1[] = { 0x05, 0xcc, 0xdc, 0x00, 0xa0 };
  488. static const u8_t data2[] = { 0x7f, 0x8c, 0x01, 0xff, 0xcf };
  489. struct mdns_domain domain1, domain2;
  490. err_t res;
  491. LWIP_UNUSED_ARG(_i);
  492. memset(&domain1, 0, sizeof(domain1));
  493. res = mdns_domain_add_label(&domain1, (const char*)data1, sizeof(data1));
  494. fail_unless(res == ERR_OK);
  495. res = mdns_domain_add_label(&domain1, "cast", 4);
  496. fail_unless(res == ERR_OK);
  497. res = mdns_domain_add_label(&domain1, (const char*)data2, sizeof(data2));
  498. fail_unless(res == ERR_OK);
  499. res = mdns_domain_add_label(&domain1, NULL, 0);
  500. fail_unless(res == ERR_OK);
  501. memset(&domain2, 0, sizeof(domain2));
  502. res = mdns_domain_add_label(&domain2, (const char*)data1, sizeof(data1));
  503. fail_unless(res == ERR_OK);
  504. res = mdns_domain_add_label(&domain2, "casT", 4);
  505. fail_unless(res == ERR_OK);
  506. res = mdns_domain_add_label(&domain2, (const char*)data2, sizeof(data2));
  507. fail_unless(res == ERR_OK);
  508. res = mdns_domain_add_label(&domain2, NULL, 0);
  509. fail_unless(res == ERR_OK);
  510. fail_unless(mdns_domain_eq(&domain1, &domain2));
  511. }
  512. END_TEST
  513. START_TEST(domain_eq_length)
  514. {
  515. struct mdns_domain domain1, domain2;
  516. err_t res;
  517. LWIP_UNUSED_ARG(_i);
  518. memset(&domain1, 0, sizeof(domain1));
  519. memset(domain1.name, 0xAA, sizeof(MDNS_DOMAIN_MAXLEN));
  520. res = mdns_domain_add_label(&domain1, "multi", 5);
  521. fail_unless(res == ERR_OK);
  522. res = mdns_domain_add_label(&domain1, "cast", 4);
  523. fail_unless(res == ERR_OK);
  524. memset(&domain2, 0, sizeof(domain2));
  525. memset(domain2.name, 0xBB, sizeof(MDNS_DOMAIN_MAXLEN));
  526. res = mdns_domain_add_label(&domain2, "multi", 5);
  527. fail_unless(res == ERR_OK);
  528. res = mdns_domain_add_label(&domain2, "cast", 4);
  529. fail_unless(res == ERR_OK);
  530. fail_unless(mdns_domain_eq(&domain1, &domain2));
  531. }
  532. END_TEST
  533. START_TEST(compress_full_match)
  534. {
  535. static const u8_t data[] = {
  536. 0x00, 0x00,
  537. 0x06, 'f', 'o', 'o', 'b', 'a', 'r', 0x05, 'l', 'o', 'c', 'a', 'l', 0x00
  538. };
  539. struct pbuf *p;
  540. struct mdns_domain domain;
  541. u16_t offset;
  542. u16_t length;
  543. err_t res;
  544. LWIP_UNUSED_ARG(_i);
  545. p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
  546. p->payload = (void *)(size_t)data;
  547. fail_if(p == NULL);
  548. memset(&domain, 0, sizeof(domain));
  549. res = mdns_domain_add_label(&domain, "foobar", 6);
  550. fail_unless(res == ERR_OK);
  551. res = mdns_domain_add_label(&domain, "local", 5);
  552. fail_unless(res == ERR_OK);
  553. res = mdns_domain_add_label(&domain, NULL, 0);
  554. fail_unless(res == ERR_OK);
  555. offset = 2;
  556. length = mdns_compress_domain(p, &offset, &domain);
  557. /* Write 0 bytes, then a jump to addr 2 */
  558. fail_unless(length == 0);
  559. fail_unless(offset == 2);
  560. pbuf_free(p);
  561. }
  562. END_TEST
  563. START_TEST(compress_full_match_subset)
  564. {
  565. static const u8_t data[] = {
  566. 0x00, 0x00,
  567. 0x02, 'g', 'o', 0x06, 'f', 'o', 'o', 'b', 'a', 'r', 0x05, 'l', 'o', 'c', 'a', 'l', 0x00
  568. };
  569. struct pbuf *p;
  570. struct mdns_domain domain;
  571. u16_t offset;
  572. u16_t length;
  573. err_t res;
  574. LWIP_UNUSED_ARG(_i);
  575. p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
  576. p->payload = (void *)(size_t)data;
  577. fail_if(p == NULL);
  578. memset(&domain, 0, sizeof(domain));
  579. res = mdns_domain_add_label(&domain, "foobar", 6);
  580. fail_unless(res == ERR_OK);
  581. res = mdns_domain_add_label(&domain, "local", 5);
  582. fail_unless(res == ERR_OK);
  583. res = mdns_domain_add_label(&domain, NULL, 0);
  584. fail_unless(res == ERR_OK);
  585. offset = 2;
  586. length = mdns_compress_domain(p, &offset, &domain);
  587. /* Write 0 bytes, then a jump to addr 5 */
  588. fail_unless(length == 0);
  589. fail_unless(offset == 5);
  590. pbuf_free(p);
  591. }
  592. END_TEST
  593. START_TEST(compress_full_match_jump)
  594. {
  595. static const u8_t data[] = {
  596. /* 0x00 */ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  597. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  598. /* 0x10 */ 0x04, 'l', 'w', 'i', 'p', 0x05, 'l', 'o', 'c', 'a', 'l', 0x00, 0xc0, 0x00, 0x02, 0x00,
  599. /* 0x20 */ 0x06, 'f', 'o', 'o', 'b', 'a', 'r', 0xc0, 0x15
  600. };
  601. struct pbuf *p;
  602. struct mdns_domain domain;
  603. u16_t offset;
  604. u16_t length;
  605. err_t res;
  606. LWIP_UNUSED_ARG(_i);
  607. p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
  608. p->payload = (void *)(size_t)data;
  609. fail_if(p == NULL);
  610. memset(&domain, 0, sizeof(domain));
  611. res = mdns_domain_add_label(&domain, "foobar", 6);
  612. fail_unless(res == ERR_OK);
  613. res = mdns_domain_add_label(&domain, "local", 5);
  614. fail_unless(res == ERR_OK);
  615. res = mdns_domain_add_label(&domain, NULL, 0);
  616. fail_unless(res == ERR_OK);
  617. offset = 0x20;
  618. length = mdns_compress_domain(p, &offset, &domain);
  619. /* Write 0 bytes, then a jump to addr 0x20 */
  620. fail_unless(length == 0);
  621. fail_unless(offset == 0x20);
  622. pbuf_free(p);
  623. }
  624. END_TEST
  625. START_TEST(compress_no_match)
  626. {
  627. static const u8_t data[] = {
  628. 0x00, 0x00,
  629. 0x04, 'l', 'w', 'i', 'p', 0x05, 'w', 'i', 'k', 'i', 'a', 0x03, 'c', 'o', 'm', 0x00
  630. };
  631. struct pbuf *p;
  632. struct mdns_domain domain;
  633. u16_t offset;
  634. u16_t length;
  635. err_t res;
  636. LWIP_UNUSED_ARG(_i);
  637. p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
  638. p->payload = (void *)(size_t)data;
  639. fail_if(p == NULL);
  640. memset(&domain, 0, sizeof(domain));
  641. res = mdns_domain_add_label(&domain, "foobar", 6);
  642. fail_unless(res == ERR_OK);
  643. res = mdns_domain_add_label(&domain, "local", 5);
  644. fail_unless(res == ERR_OK);
  645. res = mdns_domain_add_label(&domain, NULL, 0);
  646. fail_unless(res == ERR_OK);
  647. offset = 2;
  648. length = mdns_compress_domain(p, &offset, &domain);
  649. /* Write all bytes, no jump */
  650. fail_unless(length == domain.length);
  651. pbuf_free(p);
  652. }
  653. END_TEST
  654. START_TEST(compress_2nd_label)
  655. {
  656. static const u8_t data[] = {
  657. 0x00, 0x00,
  658. 0x06, 'f', 'o', 'o', 'b', 'a', 'r', 0x05, 'l', 'o', 'c', 'a', 'l', 0x00
  659. };
  660. struct pbuf *p;
  661. struct mdns_domain domain;
  662. u16_t offset;
  663. u16_t length;
  664. err_t res;
  665. LWIP_UNUSED_ARG(_i);
  666. p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
  667. p->payload = (void *)(size_t)data;
  668. fail_if(p == NULL);
  669. memset(&domain, 0, sizeof(domain));
  670. res = mdns_domain_add_label(&domain, "lwip", 4);
  671. fail_unless(res == ERR_OK);
  672. res = mdns_domain_add_label(&domain, "local", 5);
  673. fail_unless(res == ERR_OK);
  674. res = mdns_domain_add_label(&domain, NULL, 0);
  675. fail_unless(res == ERR_OK);
  676. offset = 2;
  677. length = mdns_compress_domain(p, &offset, &domain);
  678. /* Write 5 bytes, then a jump to addr 9 */
  679. fail_unless(length == 5);
  680. fail_unless(offset == 9);
  681. pbuf_free(p);
  682. }
  683. END_TEST
  684. START_TEST(compress_2nd_label_short)
  685. {
  686. static const u8_t data[] = {
  687. 0x00, 0x00,
  688. 0x04, 'l', 'w', 'i', 'p', 0x05, 'l', 'o', 'c', 'a', 'l', 0x00
  689. };
  690. struct pbuf *p;
  691. struct mdns_domain domain;
  692. u16_t offset;
  693. u16_t length;
  694. err_t res;
  695. LWIP_UNUSED_ARG(_i);
  696. p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
  697. p->payload = (void *)(size_t)data;
  698. fail_if(p == NULL);
  699. memset(&domain, 0, sizeof(domain));
  700. res = mdns_domain_add_label(&domain, "foobar", 6);
  701. fail_unless(res == ERR_OK);
  702. res = mdns_domain_add_label(&domain, "local", 5);
  703. fail_unless(res == ERR_OK);
  704. res = mdns_domain_add_label(&domain, NULL, 0);
  705. fail_unless(res == ERR_OK);
  706. offset = 2;
  707. length = mdns_compress_domain(p, &offset, &domain);
  708. /* Write 5 bytes, then a jump to addr 7 */
  709. fail_unless(length == 7);
  710. fail_unless(offset == 7);
  711. pbuf_free(p);
  712. }
  713. END_TEST
  714. START_TEST(compress_jump_to_jump)
  715. {
  716. static const u8_t data[] = {
  717. /* 0x00 */ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  718. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  719. /* 0x10 */ 0x04, 'l', 'w', 'i', 'p', 0x05, 'l', 'o', 'c', 'a', 'l', 0x00, 0xc0, 0x00, 0x02, 0x00,
  720. /* 0x20 */ 0x07, 'b', 'a', 'n', 'a', 'n', 'a', 's', 0xc0, 0x15
  721. };
  722. struct pbuf *p;
  723. struct mdns_domain domain;
  724. u16_t offset;
  725. u16_t length;
  726. err_t res;
  727. LWIP_UNUSED_ARG(_i);
  728. p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
  729. p->payload = (void *)(size_t)data;
  730. fail_if(p == NULL);
  731. memset(&domain, 0, sizeof(domain));
  732. res = mdns_domain_add_label(&domain, "foobar", 6);
  733. fail_unless(res == ERR_OK);
  734. res = mdns_domain_add_label(&domain, "local", 5);
  735. fail_unless(res == ERR_OK);
  736. res = mdns_domain_add_label(&domain, NULL, 0);
  737. fail_unless(res == ERR_OK);
  738. offset = 0x20;
  739. length = mdns_compress_domain(p, &offset, &domain);
  740. /* Dont compress if jump would be to a jump */
  741. fail_unless(length == domain.length);
  742. offset = 0x10;
  743. length = mdns_compress_domain(p, &offset, &domain);
  744. /* Write 7 bytes, then a jump to addr 0x15 */
  745. fail_unless(length == 7);
  746. fail_unless(offset == 0x15);
  747. pbuf_free(p);
  748. }
  749. END_TEST
  750. START_TEST(compress_long_match)
  751. {
  752. static const u8_t data[] = {
  753. 0x00, 0x00,
  754. 0x06, 'f', 'o', 'o', 'b', 'a', 'r', 0x05, 'l', 'o', 'c', 'a', 'l', 0x03, 'c', 'o', 'm', 0x00
  755. };
  756. struct pbuf *p;
  757. struct mdns_domain domain;
  758. u16_t offset;
  759. u16_t length;
  760. err_t res;
  761. LWIP_UNUSED_ARG(_i);
  762. p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
  763. p->payload = (void *)(size_t)data;
  764. fail_if(p == NULL);
  765. memset(&domain, 0, sizeof(domain));
  766. res = mdns_domain_add_label(&domain, "foobar", 6);
  767. fail_unless(res == ERR_OK);
  768. res = mdns_domain_add_label(&domain, "local", 5);
  769. fail_unless(res == ERR_OK);
  770. res = mdns_domain_add_label(&domain, NULL, 0);
  771. fail_unless(res == ERR_OK);
  772. offset = 2;
  773. length = mdns_compress_domain(p, &offset, &domain);
  774. fail_unless(length == domain.length);
  775. pbuf_free(p);
  776. }
  777. END_TEST
  778. Suite* mdns_suite(void)
  779. {
  780. testfunc tests[] = {
  781. TESTFUNC(readname_basic),
  782. TESTFUNC(readname_anydata),
  783. TESTFUNC(readname_short_buf),
  784. TESTFUNC(readname_long_label),
  785. TESTFUNC(readname_overflow),
  786. TESTFUNC(readname_jump_earlier),
  787. TESTFUNC(readname_jump_earlier_jump),
  788. TESTFUNC(readname_jump_maxdepth),
  789. TESTFUNC(readname_jump_later),
  790. TESTFUNC(readname_half_jump),
  791. TESTFUNC(readname_jump_toolong),
  792. TESTFUNC(readname_jump_loop_label),
  793. TESTFUNC(readname_jump_loop_jump),
  794. TESTFUNC(add_label_basic),
  795. TESTFUNC(add_label_long_label),
  796. TESTFUNC(add_label_full),
  797. TESTFUNC(domain_eq_basic),
  798. TESTFUNC(domain_eq_diff),
  799. TESTFUNC(domain_eq_case),
  800. TESTFUNC(domain_eq_anydata),
  801. TESTFUNC(domain_eq_length),
  802. TESTFUNC(compress_full_match),
  803. TESTFUNC(compress_full_match_subset),
  804. TESTFUNC(compress_full_match_jump),
  805. TESTFUNC(compress_no_match),
  806. TESTFUNC(compress_2nd_label),
  807. TESTFUNC(compress_2nd_label_short),
  808. TESTFUNC(compress_jump_to_jump),
  809. TESTFUNC(compress_long_match),
  810. };
  811. return create_suite("MDNS", tests, sizeof(tests)/sizeof(testfunc), NULL, NULL);
  812. }