test_mdns.c 27 KB

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