test_mdns.c 28 KB

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