1195972f6Sopenharmony_ci/*
2195972f6Sopenharmony_ci * Copyright (c) 2015 Verisure Innovation AB
3195972f6Sopenharmony_ci * All rights reserved.
4195972f6Sopenharmony_ci *
5195972f6Sopenharmony_ci * Redistribution and use in source and binary forms, with or without modification,
6195972f6Sopenharmony_ci * are permitted provided that the following conditions are met:
7195972f6Sopenharmony_ci *
8195972f6Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright notice,
9195972f6Sopenharmony_ci *    this list of conditions and the following disclaimer.
10195972f6Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright notice,
11195972f6Sopenharmony_ci *    this list of conditions and the following disclaimer in the documentation
12195972f6Sopenharmony_ci *    and/or other materials provided with the distribution.
13195972f6Sopenharmony_ci * 3. The name of the author may not be used to endorse or promote products
14195972f6Sopenharmony_ci *    derived from this software without specific prior written permission.
15195972f6Sopenharmony_ci *
16195972f6Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17195972f6Sopenharmony_ci * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18195972f6Sopenharmony_ci * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19195972f6Sopenharmony_ci * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20195972f6Sopenharmony_ci * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21195972f6Sopenharmony_ci * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22195972f6Sopenharmony_ci * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23195972f6Sopenharmony_ci * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24195972f6Sopenharmony_ci * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
25195972f6Sopenharmony_ci * OF SUCH DAMAGE.
26195972f6Sopenharmony_ci *
27195972f6Sopenharmony_ci * This file is part of the lwIP TCP/IP stack.
28195972f6Sopenharmony_ci *
29195972f6Sopenharmony_ci * Author: Erik Ekman <erik@kryo.se>
30195972f6Sopenharmony_ci *
31195972f6Sopenharmony_ci */
32195972f6Sopenharmony_ci
33195972f6Sopenharmony_ci#include "test_mdns.h"
34195972f6Sopenharmony_ci
35195972f6Sopenharmony_ci#include "lwip/pbuf.h"
36195972f6Sopenharmony_ci#include "lwip/apps/mdns.h"
37195972f6Sopenharmony_ci#include "lwip/apps/mdns_priv.h"
38195972f6Sopenharmony_ci
39195972f6Sopenharmony_ciSTART_TEST(readname_basic)
40195972f6Sopenharmony_ci{
41195972f6Sopenharmony_ci  static const u8_t data[] = { 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't', 0x00 };
42195972f6Sopenharmony_ci  struct pbuf *p;
43195972f6Sopenharmony_ci  struct mdns_domain domain;
44195972f6Sopenharmony_ci  u16_t offset;
45195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(_i);
46195972f6Sopenharmony_ci
47195972f6Sopenharmony_ci  p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
48195972f6Sopenharmony_ci  fail_if(p == NULL);
49195972f6Sopenharmony_ci  p->payload = (void *)(size_t)data;
50195972f6Sopenharmony_ci  offset = mdns_readname(p, 0, &domain);
51195972f6Sopenharmony_ci  pbuf_free(p);
52195972f6Sopenharmony_ci  fail_unless(offset == sizeof(data));
53195972f6Sopenharmony_ci  fail_unless(domain.length == sizeof(data));
54195972f6Sopenharmony_ci  fail_if(memcmp(&domain.name, data, sizeof(data)));
55195972f6Sopenharmony_ci}
56195972f6Sopenharmony_ciEND_TEST
57195972f6Sopenharmony_ci
58195972f6Sopenharmony_ciSTART_TEST(readname_anydata)
59195972f6Sopenharmony_ci{
60195972f6Sopenharmony_ci  static const u8_t data[] = { 0x05, 0x00, 0xFF, 0x08, 0xc0, 0x0f, 0x04, 0x7f, 0x80, 0x82, 0x88, 0x00 };
61195972f6Sopenharmony_ci  struct pbuf *p;
62195972f6Sopenharmony_ci  struct mdns_domain domain;
63195972f6Sopenharmony_ci  u16_t offset;
64195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(_i);
65195972f6Sopenharmony_ci
66195972f6Sopenharmony_ci  p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
67195972f6Sopenharmony_ci  fail_if(p == NULL);
68195972f6Sopenharmony_ci  p->payload = (void *)(size_t)data;
69195972f6Sopenharmony_ci  offset = mdns_readname(p, 0, &domain);
70195972f6Sopenharmony_ci  pbuf_free(p);
71195972f6Sopenharmony_ci  fail_unless(offset == sizeof(data));
72195972f6Sopenharmony_ci  fail_unless(domain.length == sizeof(data));
73195972f6Sopenharmony_ci  fail_if(memcmp(&domain.name, data, sizeof(data)));
74195972f6Sopenharmony_ci}
75195972f6Sopenharmony_ciEND_TEST
76195972f6Sopenharmony_ci
77195972f6Sopenharmony_ciSTART_TEST(readname_short_buf)
78195972f6Sopenharmony_ci{
79195972f6Sopenharmony_ci  static const u8_t data[] = { 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a' };
80195972f6Sopenharmony_ci  struct pbuf *p;
81195972f6Sopenharmony_ci  struct mdns_domain domain;
82195972f6Sopenharmony_ci  u16_t offset;
83195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(_i);
84195972f6Sopenharmony_ci
85195972f6Sopenharmony_ci  p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
86195972f6Sopenharmony_ci  fail_if(p == NULL);
87195972f6Sopenharmony_ci  p->payload = (void *)(size_t)data;
88195972f6Sopenharmony_ci  offset = mdns_readname(p, 0, &domain);
89195972f6Sopenharmony_ci  pbuf_free(p);
90195972f6Sopenharmony_ci  fail_unless(offset == MDNS_READNAME_ERROR);
91195972f6Sopenharmony_ci}
92195972f6Sopenharmony_ciEND_TEST
93195972f6Sopenharmony_ci
94195972f6Sopenharmony_ciSTART_TEST(readname_long_label)
95195972f6Sopenharmony_ci{
96195972f6Sopenharmony_ci  static const u8_t data[] = {
97195972f6Sopenharmony_ci      0x05, 'm', 'u', 'l', 't', 'i',
98195972f6Sopenharmony_ci      0x52, 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a',
99195972f6Sopenharmony_ci      'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a',
100195972f6Sopenharmony_ci      'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a',
101195972f6Sopenharmony_ci      'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a',
102195972f6Sopenharmony_ci      'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a',
103195972f6Sopenharmony_ci      'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 0x00
104195972f6Sopenharmony_ci  };
105195972f6Sopenharmony_ci  struct pbuf *p;
106195972f6Sopenharmony_ci  struct mdns_domain domain;
107195972f6Sopenharmony_ci  u16_t offset;
108195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(_i);
109195972f6Sopenharmony_ci
110195972f6Sopenharmony_ci  p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
111195972f6Sopenharmony_ci  fail_if(p == NULL);
112195972f6Sopenharmony_ci  p->payload = (void *)(size_t)data;
113195972f6Sopenharmony_ci  offset = mdns_readname(p, 0, &domain);
114195972f6Sopenharmony_ci  pbuf_free(p);
115195972f6Sopenharmony_ci  fail_unless(offset == MDNS_READNAME_ERROR);
116195972f6Sopenharmony_ci}
117195972f6Sopenharmony_ciEND_TEST
118195972f6Sopenharmony_ci
119195972f6Sopenharmony_ciSTART_TEST(readname_overflow)
120195972f6Sopenharmony_ci{
121195972f6Sopenharmony_ci  static const u8_t data[] = {
122195972f6Sopenharmony_ci      0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
123195972f6Sopenharmony_ci      0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
124195972f6Sopenharmony_ci      0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
125195972f6Sopenharmony_ci      0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
126195972f6Sopenharmony_ci      0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
127195972f6Sopenharmony_ci      0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
128195972f6Sopenharmony_ci      0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
129195972f6Sopenharmony_ci      0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
130195972f6Sopenharmony_ci      0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
131195972f6Sopenharmony_ci      0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
132195972f6Sopenharmony_ci      0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
133195972f6Sopenharmony_ci      0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
134195972f6Sopenharmony_ci      0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
135195972f6Sopenharmony_ci      0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
136195972f6Sopenharmony_ci      0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
137195972f6Sopenharmony_ci      0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
138195972f6Sopenharmony_ci      0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
139195972f6Sopenharmony_ci      0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
140195972f6Sopenharmony_ci      0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
141195972f6Sopenharmony_ci      0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
142195972f6Sopenharmony_ci      0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
143195972f6Sopenharmony_ci      0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
144195972f6Sopenharmony_ci      0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
145195972f6Sopenharmony_ci      0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
146195972f6Sopenharmony_ci      0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
147195972f6Sopenharmony_ci      0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
148195972f6Sopenharmony_ci      0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
149195972f6Sopenharmony_ci      0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
150195972f6Sopenharmony_ci      0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
151195972f6Sopenharmony_ci      0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
152195972f6Sopenharmony_ci      0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
153195972f6Sopenharmony_ci      0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
154195972f6Sopenharmony_ci      0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
155195972f6Sopenharmony_ci      0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
156195972f6Sopenharmony_ci      0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
157195972f6Sopenharmony_ci      0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
158195972f6Sopenharmony_ci      0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
159195972f6Sopenharmony_ci      0x00
160195972f6Sopenharmony_ci  };
161195972f6Sopenharmony_ci  struct pbuf *p;
162195972f6Sopenharmony_ci  struct mdns_domain domain;
163195972f6Sopenharmony_ci  u16_t offset;
164195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(_i);
165195972f6Sopenharmony_ci
166195972f6Sopenharmony_ci  p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
167195972f6Sopenharmony_ci  fail_if(p == NULL);
168195972f6Sopenharmony_ci  p->payload = (void *)(size_t)data;
169195972f6Sopenharmony_ci  offset = mdns_readname(p, 0, &domain);
170195972f6Sopenharmony_ci  pbuf_free(p);
171195972f6Sopenharmony_ci  fail_unless(offset == MDNS_READNAME_ERROR);
172195972f6Sopenharmony_ci}
173195972f6Sopenharmony_ciEND_TEST
174195972f6Sopenharmony_ci
175195972f6Sopenharmony_ciSTART_TEST(readname_jump_earlier)
176195972f6Sopenharmony_ci{
177195972f6Sopenharmony_ci  static const u8_t data[] = {
178195972f6Sopenharmony_ci      /* Some padding needed, not supported to jump to bytes containing dns header */
179195972f6Sopenharmony_ci      /*  0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
180195972f6Sopenharmony_ci      /* 10 */ 0x0f, 0x0e, 0x05, 'l', 'o', 'c', 'a', 'l', 0x00, 0xab,
181195972f6Sopenharmony_ci      /* 20 */ 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't', 0xc0, 0x0c
182195972f6Sopenharmony_ci  };
183195972f6Sopenharmony_ci  static const u8_t fullname[] = {
184195972f6Sopenharmony_ci      0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't', 0x05, 'l', 'o', 'c', 'a', 'l', 0x00
185195972f6Sopenharmony_ci  };
186195972f6Sopenharmony_ci  struct pbuf *p;
187195972f6Sopenharmony_ci  struct mdns_domain domain;
188195972f6Sopenharmony_ci  u16_t offset;
189195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(_i);
190195972f6Sopenharmony_ci
191195972f6Sopenharmony_ci  p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
192195972f6Sopenharmony_ci  fail_if(p == NULL);
193195972f6Sopenharmony_ci  p->payload = (void *)(size_t)data;
194195972f6Sopenharmony_ci  offset = mdns_readname(p, 20, &domain);
195195972f6Sopenharmony_ci  pbuf_free(p);
196195972f6Sopenharmony_ci  fail_unless(offset == sizeof(data));
197195972f6Sopenharmony_ci  fail_unless(domain.length == sizeof(fullname));
198195972f6Sopenharmony_ci
199195972f6Sopenharmony_ci  fail_if(memcmp(&domain.name, fullname, sizeof(fullname)));
200195972f6Sopenharmony_ci}
201195972f6Sopenharmony_ciEND_TEST
202195972f6Sopenharmony_ci
203195972f6Sopenharmony_ciSTART_TEST(readname_jump_earlier_jump)
204195972f6Sopenharmony_ci{
205195972f6Sopenharmony_ci  static const u8_t data[] = {
206195972f6Sopenharmony_ci      /* Some padding needed, not supported to jump to bytes containing dns header */
207195972f6Sopenharmony_ci      /* 0x00 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
208195972f6Sopenharmony_ci      /* 0x08 */ 0x00, 0x00, 0x00, 0x00, 0x03, 0x0b, 0x0a, 0xf2,
209195972f6Sopenharmony_ci      /* 0x10 */ 0x04, 'c', 'a', 's', 't', 0x00, 0xc0, 0x10,
210195972f6Sopenharmony_ci      /* 0x18 */ 0x05, 'm', 'u', 'l', 't', 'i', 0xc0, 0x16
211195972f6Sopenharmony_ci  };
212195972f6Sopenharmony_ci  static const u8_t fullname[] = {
213195972f6Sopenharmony_ci      0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't', 0x00
214195972f6Sopenharmony_ci  };
215195972f6Sopenharmony_ci  struct pbuf *p;
216195972f6Sopenharmony_ci  struct mdns_domain domain;
217195972f6Sopenharmony_ci  u16_t offset;
218195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(_i);
219195972f6Sopenharmony_ci
220195972f6Sopenharmony_ci  p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
221195972f6Sopenharmony_ci  fail_if(p == NULL);
222195972f6Sopenharmony_ci  p->payload = (void *)(size_t)data;
223195972f6Sopenharmony_ci  offset = mdns_readname(p, 0x18, &domain);
224195972f6Sopenharmony_ci  pbuf_free(p);
225195972f6Sopenharmony_ci  fail_unless(offset == sizeof(data));
226195972f6Sopenharmony_ci  fail_unless(domain.length == sizeof(fullname));
227195972f6Sopenharmony_ci
228195972f6Sopenharmony_ci  fail_if(memcmp(&domain.name, fullname, sizeof(fullname)));
229195972f6Sopenharmony_ci}
230195972f6Sopenharmony_ciEND_TEST
231195972f6Sopenharmony_ci
232195972f6Sopenharmony_ciSTART_TEST(readname_jump_maxdepth)
233195972f6Sopenharmony_ci{
234195972f6Sopenharmony_ci  static const u8_t data[] = {
235195972f6Sopenharmony_ci      /* Some padding needed, not supported to jump to bytes containing dns header */
236195972f6Sopenharmony_ci      /* 0x00 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
237195972f6Sopenharmony_ci      /* 0x08 */ 0x00, 0x00, 0x00, 0x00, 0x03, 0x0b, 0x0a, 0xf2,
238195972f6Sopenharmony_ci      /* 0x10 */ 0x04, 'n', 'a', 'm', 'e', 0xc0, 0x27, 0x03,
239195972f6Sopenharmony_ci      /* 0x18 */ 0x03, 'd', 'n', 's', 0xc0, 0x10, 0xc0, 0x10,
240195972f6Sopenharmony_ci      /* 0x20 */ 0x04, 'd', 'e', 'e', 'p', 0xc0, 0x18, 0x00,
241195972f6Sopenharmony_ci      /* 0x28 */ 0x04, 'c', 'a', 's', 't', 0xc0, 0x20, 0xb0,
242195972f6Sopenharmony_ci      /* 0x30 */ 0x05, 'm', 'u', 'l', 't', 'i', 0xc0, 0x28
243195972f6Sopenharmony_ci  };
244195972f6Sopenharmony_ci  static const u8_t fullname[] = {
245195972f6Sopenharmony_ci      0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't',
246195972f6Sopenharmony_ci      0x04, 'd', 'e', 'e', 'p', 0x03, 'd', 'n', 's',
247195972f6Sopenharmony_ci      0x04, 'n', 'a', 'm', 'e', 0x00
248195972f6Sopenharmony_ci  };
249195972f6Sopenharmony_ci  struct pbuf *p;
250195972f6Sopenharmony_ci  struct mdns_domain domain;
251195972f6Sopenharmony_ci  u16_t offset;
252195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(_i);
253195972f6Sopenharmony_ci
254195972f6Sopenharmony_ci  p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
255195972f6Sopenharmony_ci  fail_if(p == NULL);
256195972f6Sopenharmony_ci  p->payload = (void *)(size_t)data;
257195972f6Sopenharmony_ci  offset = mdns_readname(p, 0x30, &domain);
258195972f6Sopenharmony_ci  pbuf_free(p);
259195972f6Sopenharmony_ci  fail_unless(offset == sizeof(data));
260195972f6Sopenharmony_ci  fail_unless(domain.length == sizeof(fullname));
261195972f6Sopenharmony_ci
262195972f6Sopenharmony_ci  fail_if(memcmp(&domain.name, fullname, sizeof(fullname)));
263195972f6Sopenharmony_ci}
264195972f6Sopenharmony_ciEND_TEST
265195972f6Sopenharmony_ci
266195972f6Sopenharmony_ciSTART_TEST(readname_jump_later)
267195972f6Sopenharmony_ci{
268195972f6Sopenharmony_ci  static const u8_t data[] = {
269195972f6Sopenharmony_ci      /* 0x00 */ 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't', 0xc0, 0x10, 0x00, 0x01, 0x40,
270195972f6Sopenharmony_ci      /* 0x10 */ 0x05, 'l', 'o', 'c', 'a', 'l', 0x00, 0xab
271195972f6Sopenharmony_ci  };
272195972f6Sopenharmony_ci  static const u8_t fullname[] = {
273195972f6Sopenharmony_ci      0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't', 0x05, 'l', 'o', 'c', 'a', 'l', 0x00
274195972f6Sopenharmony_ci  };
275195972f6Sopenharmony_ci  struct pbuf *p;
276195972f6Sopenharmony_ci  struct mdns_domain domain;
277195972f6Sopenharmony_ci  u16_t offset;
278195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(_i);
279195972f6Sopenharmony_ci
280195972f6Sopenharmony_ci  p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
281195972f6Sopenharmony_ci  fail_if(p == NULL);
282195972f6Sopenharmony_ci  p->payload = (void *)(size_t)data;
283195972f6Sopenharmony_ci  offset = mdns_readname(p, 0, &domain);
284195972f6Sopenharmony_ci  pbuf_free(p);
285195972f6Sopenharmony_ci  fail_unless(offset == 13);
286195972f6Sopenharmony_ci  fail_unless(domain.length == sizeof(fullname));
287195972f6Sopenharmony_ci
288195972f6Sopenharmony_ci  fail_if(memcmp(&domain.name, fullname, sizeof(fullname)));
289195972f6Sopenharmony_ci}
290195972f6Sopenharmony_ciEND_TEST
291195972f6Sopenharmony_ci
292195972f6Sopenharmony_ciSTART_TEST(readname_half_jump)
293195972f6Sopenharmony_ci{
294195972f6Sopenharmony_ci  static const u8_t data[] = {
295195972f6Sopenharmony_ci      0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't', 0xc0
296195972f6Sopenharmony_ci  };
297195972f6Sopenharmony_ci  struct pbuf *p;
298195972f6Sopenharmony_ci  struct mdns_domain domain;
299195972f6Sopenharmony_ci  u16_t offset;
300195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(_i);
301195972f6Sopenharmony_ci
302195972f6Sopenharmony_ci  p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
303195972f6Sopenharmony_ci  fail_if(p == NULL);
304195972f6Sopenharmony_ci  p->payload = (void *)(size_t)data;
305195972f6Sopenharmony_ci  offset = mdns_readname(p, 0, &domain);
306195972f6Sopenharmony_ci  pbuf_free(p);
307195972f6Sopenharmony_ci  fail_unless(offset == MDNS_READNAME_ERROR);
308195972f6Sopenharmony_ci}
309195972f6Sopenharmony_ciEND_TEST
310195972f6Sopenharmony_ci
311195972f6Sopenharmony_ciSTART_TEST(readname_jump_toolong)
312195972f6Sopenharmony_ci{
313195972f6Sopenharmony_ci  static const u8_t data[] = {
314195972f6Sopenharmony_ci      0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't', 0xc2, 0x10, 0x00, 0x01, 0x40
315195972f6Sopenharmony_ci  };
316195972f6Sopenharmony_ci  struct pbuf *p;
317195972f6Sopenharmony_ci  struct mdns_domain domain;
318195972f6Sopenharmony_ci  u16_t offset;
319195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(_i);
320195972f6Sopenharmony_ci
321195972f6Sopenharmony_ci  p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
322195972f6Sopenharmony_ci  fail_if(p == NULL);
323195972f6Sopenharmony_ci  p->payload = (void *)(size_t)data;
324195972f6Sopenharmony_ci  offset = mdns_readname(p, 0, &domain);
325195972f6Sopenharmony_ci  pbuf_free(p);
326195972f6Sopenharmony_ci  fail_unless(offset == MDNS_READNAME_ERROR);
327195972f6Sopenharmony_ci}
328195972f6Sopenharmony_ciEND_TEST
329195972f6Sopenharmony_ci
330195972f6Sopenharmony_ciSTART_TEST(readname_jump_loop_label)
331195972f6Sopenharmony_ci{
332195972f6Sopenharmony_ci  static const u8_t data[] = {
333195972f6Sopenharmony_ci      /*  0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
334195972f6Sopenharmony_ci      /* 10 */ 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't', 0xc0, 0x10
335195972f6Sopenharmony_ci  };
336195972f6Sopenharmony_ci  struct pbuf *p;
337195972f6Sopenharmony_ci  struct mdns_domain domain;
338195972f6Sopenharmony_ci  u16_t offset;
339195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(_i);
340195972f6Sopenharmony_ci
341195972f6Sopenharmony_ci  p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
342195972f6Sopenharmony_ci  fail_if(p == NULL);
343195972f6Sopenharmony_ci  p->payload = (void *)(size_t)data;
344195972f6Sopenharmony_ci  offset = mdns_readname(p, 10, &domain);
345195972f6Sopenharmony_ci  pbuf_free(p);
346195972f6Sopenharmony_ci  fail_unless(offset == MDNS_READNAME_ERROR);
347195972f6Sopenharmony_ci}
348195972f6Sopenharmony_ciEND_TEST
349195972f6Sopenharmony_ci
350195972f6Sopenharmony_ciSTART_TEST(readname_jump_loop_jump)
351195972f6Sopenharmony_ci{
352195972f6Sopenharmony_ci  static const u8_t data[] = {
353195972f6Sopenharmony_ci      /*  0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
354195972f6Sopenharmony_ci      /* 10 */ 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't', 0xc0, 0x15
355195972f6Sopenharmony_ci  };
356195972f6Sopenharmony_ci  struct pbuf *p;
357195972f6Sopenharmony_ci  struct mdns_domain domain;
358195972f6Sopenharmony_ci  u16_t offset;
359195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(_i);
360195972f6Sopenharmony_ci
361195972f6Sopenharmony_ci  p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
362195972f6Sopenharmony_ci  fail_if(p == NULL);
363195972f6Sopenharmony_ci  p->payload = (void *)(size_t)data;
364195972f6Sopenharmony_ci  offset = mdns_readname(p, 10, &domain);
365195972f6Sopenharmony_ci  pbuf_free(p);
366195972f6Sopenharmony_ci  fail_unless(offset == MDNS_READNAME_ERROR);
367195972f6Sopenharmony_ci}
368195972f6Sopenharmony_ciEND_TEST
369195972f6Sopenharmony_ci
370195972f6Sopenharmony_ciSTART_TEST(add_label_basic)
371195972f6Sopenharmony_ci{
372195972f6Sopenharmony_ci  static const u8_t data[] = { 0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't', 0x00 };
373195972f6Sopenharmony_ci  struct mdns_domain domain;
374195972f6Sopenharmony_ci  err_t res;
375195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(_i);
376195972f6Sopenharmony_ci
377195972f6Sopenharmony_ci  memset(&domain, 0, sizeof(domain));
378195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain, "multi", 5);
379195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
380195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain, "cast", 4);
381195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
382195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain, NULL, 0);
383195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
384195972f6Sopenharmony_ci  fail_unless(domain.length == sizeof(data));
385195972f6Sopenharmony_ci  fail_if(memcmp(&domain.name, data, sizeof(data)));
386195972f6Sopenharmony_ci}
387195972f6Sopenharmony_ciEND_TEST
388195972f6Sopenharmony_ci
389195972f6Sopenharmony_ciSTART_TEST(add_label_long_label)
390195972f6Sopenharmony_ci{
391195972f6Sopenharmony_ci  static const char *toolong = "abcdefghijklmnopqrstuvwxyz0123456789-abcdefghijklmnopqrstuvwxyz0123456789-abcdefghijklmnopqrstuvwxyz0123456789-";
392195972f6Sopenharmony_ci  struct mdns_domain domain;
393195972f6Sopenharmony_ci  err_t res;
394195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(_i);
395195972f6Sopenharmony_ci
396195972f6Sopenharmony_ci  memset(&domain, 0, sizeof(domain));
397195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain, "multi", 5);
398195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
399195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain, toolong, (u8_t)strlen(toolong));
400195972f6Sopenharmony_ci  fail_unless(res == ERR_VAL);
401195972f6Sopenharmony_ci}
402195972f6Sopenharmony_ciEND_TEST
403195972f6Sopenharmony_ci
404195972f6Sopenharmony_ciSTART_TEST(add_label_full)
405195972f6Sopenharmony_ci{
406195972f6Sopenharmony_ci  static const char *label = "0123456789abcdef0123456789abcdef";
407195972f6Sopenharmony_ci  struct mdns_domain domain;
408195972f6Sopenharmony_ci  err_t res;
409195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(_i);
410195972f6Sopenharmony_ci
411195972f6Sopenharmony_ci  memset(&domain, 0, sizeof(domain));
412195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain, label, (u8_t)strlen(label));
413195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
414195972f6Sopenharmony_ci  fail_unless(domain.length == 33);
415195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain, label, (u8_t)strlen(label));
416195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
417195972f6Sopenharmony_ci  fail_unless(domain.length == 66);
418195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain, label, (u8_t)strlen(label));
419195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
420195972f6Sopenharmony_ci  fail_unless(domain.length == 99);
421195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain, label, (u8_t)strlen(label));
422195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
423195972f6Sopenharmony_ci  fail_unless(domain.length == 132);
424195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain, label, (u8_t)strlen(label));
425195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
426195972f6Sopenharmony_ci  fail_unless(domain.length == 165);
427195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain, label, (u8_t)strlen(label));
428195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
429195972f6Sopenharmony_ci  fail_unless(domain.length == 198);
430195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain, label, (u8_t)strlen(label));
431195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
432195972f6Sopenharmony_ci  fail_unless(domain.length == 231);
433195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain, label, (u8_t)strlen(label));
434195972f6Sopenharmony_ci  fail_unless(res == ERR_VAL);
435195972f6Sopenharmony_ci  fail_unless(domain.length == 231);
436195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain, label, 25);
437195972f6Sopenharmony_ci  fail_unless(res == ERR_VAL);
438195972f6Sopenharmony_ci  fail_unless(domain.length == 231);
439195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain, label, 24);
440195972f6Sopenharmony_ci  fail_unless(res == ERR_VAL);
441195972f6Sopenharmony_ci  fail_unless(domain.length == 231);
442195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain, label, 23);
443195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
444195972f6Sopenharmony_ci  fail_unless(domain.length == 255);
445195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain, NULL, 0);
446195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
447195972f6Sopenharmony_ci  fail_unless(domain.length == 256);
448195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain, NULL, 0);
449195972f6Sopenharmony_ci  fail_unless(res == ERR_VAL);
450195972f6Sopenharmony_ci  fail_unless(domain.length == 256);
451195972f6Sopenharmony_ci}
452195972f6Sopenharmony_ciEND_TEST
453195972f6Sopenharmony_ci
454195972f6Sopenharmony_ciSTART_TEST(domain_eq_basic)
455195972f6Sopenharmony_ci{
456195972f6Sopenharmony_ci  static const u8_t data[] = {
457195972f6Sopenharmony_ci      0x05, 'm', 'u', 'l', 't', 'i', 0x04, 'c', 'a', 's', 't', 0x00
458195972f6Sopenharmony_ci  };
459195972f6Sopenharmony_ci  struct mdns_domain domain1, domain2;
460195972f6Sopenharmony_ci  err_t res;
461195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(_i);
462195972f6Sopenharmony_ci
463195972f6Sopenharmony_ci  memset(&domain1, 0, sizeof(domain1));
464195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain1, "multi", 5);
465195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
466195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain1, "cast", 4);
467195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
468195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain1, NULL, 0);
469195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
470195972f6Sopenharmony_ci  fail_unless(domain1.length == sizeof(data));
471195972f6Sopenharmony_ci
472195972f6Sopenharmony_ci  memset(&domain2, 0, sizeof(domain2));
473195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain2, "multi", 5);
474195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
475195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain2, "cast", 4);
476195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
477195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain2, NULL, 0);
478195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
479195972f6Sopenharmony_ci
480195972f6Sopenharmony_ci  fail_unless(mdns_domain_eq(&domain1, &domain2));
481195972f6Sopenharmony_ci}
482195972f6Sopenharmony_ciEND_TEST
483195972f6Sopenharmony_ci
484195972f6Sopenharmony_ciSTART_TEST(domain_eq_diff)
485195972f6Sopenharmony_ci{
486195972f6Sopenharmony_ci  struct mdns_domain domain1, domain2;
487195972f6Sopenharmony_ci  err_t res;
488195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(_i);
489195972f6Sopenharmony_ci
490195972f6Sopenharmony_ci  memset(&domain1, 0, sizeof(domain1));
491195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain1, "multi", 5);
492195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
493195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain1, "base", 4);
494195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
495195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain1, NULL, 0);
496195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
497195972f6Sopenharmony_ci
498195972f6Sopenharmony_ci  memset(&domain2, 0, sizeof(domain2));
499195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain2, "multi", 5);
500195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
501195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain2, "cast", 4);
502195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
503195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain2, NULL, 0);
504195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
505195972f6Sopenharmony_ci
506195972f6Sopenharmony_ci  fail_if(mdns_domain_eq(&domain1, &domain2));
507195972f6Sopenharmony_ci}
508195972f6Sopenharmony_ciEND_TEST
509195972f6Sopenharmony_ci
510195972f6Sopenharmony_ciSTART_TEST(domain_eq_case)
511195972f6Sopenharmony_ci{
512195972f6Sopenharmony_ci  struct mdns_domain domain1, domain2;
513195972f6Sopenharmony_ci  err_t res;
514195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(_i);
515195972f6Sopenharmony_ci
516195972f6Sopenharmony_ci  memset(&domain1, 0, sizeof(domain1));
517195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain1, "multi", 5);
518195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
519195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain1, "cast", 4);
520195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
521195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain1, NULL, 0);
522195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
523195972f6Sopenharmony_ci
524195972f6Sopenharmony_ci  memset(&domain2, 0, sizeof(domain2));
525195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain2, "MulTI", 5);
526195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
527195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain2, "casT", 4);
528195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
529195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain2, NULL, 0);
530195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
531195972f6Sopenharmony_ci
532195972f6Sopenharmony_ci  fail_unless(mdns_domain_eq(&domain1, &domain2));
533195972f6Sopenharmony_ci}
534195972f6Sopenharmony_ciEND_TEST
535195972f6Sopenharmony_ci
536195972f6Sopenharmony_ciSTART_TEST(domain_eq_anydata)
537195972f6Sopenharmony_ci{
538195972f6Sopenharmony_ci  static const u8_t data1[] = { 0x05, 0xcc, 0xdc, 0x00, 0xa0 };
539195972f6Sopenharmony_ci  static const u8_t data2[] = { 0x7f, 0x8c, 0x01, 0xff, 0xcf };
540195972f6Sopenharmony_ci  struct mdns_domain domain1, domain2;
541195972f6Sopenharmony_ci  err_t res;
542195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(_i);
543195972f6Sopenharmony_ci
544195972f6Sopenharmony_ci  memset(&domain1, 0, sizeof(domain1));
545195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain1, (const char*)data1, sizeof(data1));
546195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
547195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain1, "cast", 4);
548195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
549195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain1, (const char*)data2, sizeof(data2));
550195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
551195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain1, NULL, 0);
552195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
553195972f6Sopenharmony_ci
554195972f6Sopenharmony_ci  memset(&domain2, 0, sizeof(domain2));
555195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain2, (const char*)data1, sizeof(data1));
556195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
557195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain2, "casT", 4);
558195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
559195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain2, (const char*)data2, sizeof(data2));
560195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
561195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain2, NULL, 0);
562195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
563195972f6Sopenharmony_ci
564195972f6Sopenharmony_ci  fail_unless(mdns_domain_eq(&domain1, &domain2));
565195972f6Sopenharmony_ci}
566195972f6Sopenharmony_ciEND_TEST
567195972f6Sopenharmony_ci
568195972f6Sopenharmony_ciSTART_TEST(domain_eq_length)
569195972f6Sopenharmony_ci{
570195972f6Sopenharmony_ci  struct mdns_domain domain1, domain2;
571195972f6Sopenharmony_ci  err_t res;
572195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(_i);
573195972f6Sopenharmony_ci
574195972f6Sopenharmony_ci  memset(&domain1, 0, sizeof(domain1));
575195972f6Sopenharmony_ci  memset(domain1.name, 0xAA, sizeof(MDNS_DOMAIN_MAXLEN));
576195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain1, "multi", 5);
577195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
578195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain1, "cast", 4);
579195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
580195972f6Sopenharmony_ci
581195972f6Sopenharmony_ci  memset(&domain2, 0, sizeof(domain2));
582195972f6Sopenharmony_ci  memset(domain2.name, 0xBB, sizeof(MDNS_DOMAIN_MAXLEN));
583195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain2, "multi", 5);
584195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
585195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain2, "cast", 4);
586195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
587195972f6Sopenharmony_ci
588195972f6Sopenharmony_ci  fail_unless(mdns_domain_eq(&domain1, &domain2));
589195972f6Sopenharmony_ci}
590195972f6Sopenharmony_ciEND_TEST
591195972f6Sopenharmony_ci
592195972f6Sopenharmony_ciSTART_TEST(compress_full_match)
593195972f6Sopenharmony_ci{
594195972f6Sopenharmony_ci  static const u8_t data[] = {
595195972f6Sopenharmony_ci      0x00, 0x00,
596195972f6Sopenharmony_ci      0x06, 'f', 'o', 'o', 'b', 'a', 'r', 0x05, 'l', 'o', 'c', 'a', 'l', 0x00
597195972f6Sopenharmony_ci  };
598195972f6Sopenharmony_ci  struct pbuf *p;
599195972f6Sopenharmony_ci  struct mdns_domain domain;
600195972f6Sopenharmony_ci  u16_t offset;
601195972f6Sopenharmony_ci  u16_t length;
602195972f6Sopenharmony_ci  err_t res;
603195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(_i);
604195972f6Sopenharmony_ci
605195972f6Sopenharmony_ci  p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
606195972f6Sopenharmony_ci  fail_if(p == NULL);
607195972f6Sopenharmony_ci  p->payload = (void *)(size_t)data;
608195972f6Sopenharmony_ci
609195972f6Sopenharmony_ci  memset(&domain, 0, sizeof(domain));
610195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain, "foobar", 6);
611195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
612195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain, "local", 5);
613195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
614195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain, NULL, 0);
615195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
616195972f6Sopenharmony_ci
617195972f6Sopenharmony_ci  offset = 2;
618195972f6Sopenharmony_ci  length = mdns_compress_domain(p, &offset, &domain);
619195972f6Sopenharmony_ci  /* Write 0 bytes, then a jump to addr 2 */
620195972f6Sopenharmony_ci  fail_unless(length == 0);
621195972f6Sopenharmony_ci  fail_unless(offset == 2);
622195972f6Sopenharmony_ci
623195972f6Sopenharmony_ci  pbuf_free(p);
624195972f6Sopenharmony_ci}
625195972f6Sopenharmony_ciEND_TEST
626195972f6Sopenharmony_ci
627195972f6Sopenharmony_ciSTART_TEST(compress_full_match_subset)
628195972f6Sopenharmony_ci{
629195972f6Sopenharmony_ci  static const u8_t data[] = {
630195972f6Sopenharmony_ci      0x00, 0x00,
631195972f6Sopenharmony_ci      0x02, 'g', 'o', 0x06, 'f', 'o', 'o', 'b', 'a', 'r', 0x05, 'l', 'o', 'c', 'a', 'l', 0x00
632195972f6Sopenharmony_ci  };
633195972f6Sopenharmony_ci  struct pbuf *p;
634195972f6Sopenharmony_ci  struct mdns_domain domain;
635195972f6Sopenharmony_ci  u16_t offset;
636195972f6Sopenharmony_ci  u16_t length;
637195972f6Sopenharmony_ci  err_t res;
638195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(_i);
639195972f6Sopenharmony_ci
640195972f6Sopenharmony_ci  p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
641195972f6Sopenharmony_ci  fail_if(p == NULL);
642195972f6Sopenharmony_ci  p->payload = (void *)(size_t)data;
643195972f6Sopenharmony_ci
644195972f6Sopenharmony_ci  memset(&domain, 0, sizeof(domain));
645195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain, "foobar", 6);
646195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
647195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain, "local", 5);
648195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
649195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain, NULL, 0);
650195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
651195972f6Sopenharmony_ci
652195972f6Sopenharmony_ci  offset = 2;
653195972f6Sopenharmony_ci  length = mdns_compress_domain(p, &offset, &domain);
654195972f6Sopenharmony_ci  /* Write 0 bytes, then a jump to addr 5 */
655195972f6Sopenharmony_ci  fail_unless(length == 0);
656195972f6Sopenharmony_ci  fail_unless(offset == 5);
657195972f6Sopenharmony_ci
658195972f6Sopenharmony_ci  pbuf_free(p);
659195972f6Sopenharmony_ci}
660195972f6Sopenharmony_ciEND_TEST
661195972f6Sopenharmony_ci
662195972f6Sopenharmony_ciSTART_TEST(compress_full_match_jump)
663195972f6Sopenharmony_ci{
664195972f6Sopenharmony_ci  static const u8_t data[] = {
665195972f6Sopenharmony_ci    /* 0x00 */ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
666195972f6Sopenharmony_ci               0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
667195972f6Sopenharmony_ci    /* 0x10 */ 0x04, 'l', 'w', 'i', 'p', 0x05, 'l', 'o', 'c', 'a', 'l', 0x00, 0xc0, 0x00, 0x02, 0x00,
668195972f6Sopenharmony_ci    /* 0x20 */ 0x06, 'f', 'o', 'o', 'b', 'a', 'r', 0xc0, 0x15
669195972f6Sopenharmony_ci  };
670195972f6Sopenharmony_ci  struct pbuf *p;
671195972f6Sopenharmony_ci  struct mdns_domain domain;
672195972f6Sopenharmony_ci  u16_t offset;
673195972f6Sopenharmony_ci  u16_t length;
674195972f6Sopenharmony_ci  err_t res;
675195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(_i);
676195972f6Sopenharmony_ci
677195972f6Sopenharmony_ci  p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
678195972f6Sopenharmony_ci  fail_if(p == NULL);
679195972f6Sopenharmony_ci  p->payload = (void *)(size_t)data;
680195972f6Sopenharmony_ci
681195972f6Sopenharmony_ci  memset(&domain, 0, sizeof(domain));
682195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain, "foobar", 6);
683195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
684195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain, "local", 5);
685195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
686195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain, NULL, 0);
687195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
688195972f6Sopenharmony_ci
689195972f6Sopenharmony_ci  offset = 0x20;
690195972f6Sopenharmony_ci  length = mdns_compress_domain(p, &offset, &domain);
691195972f6Sopenharmony_ci  /* Write 0 bytes, then a jump to addr 0x20 */
692195972f6Sopenharmony_ci  fail_unless(length == 0);
693195972f6Sopenharmony_ci  fail_unless(offset == 0x20);
694195972f6Sopenharmony_ci
695195972f6Sopenharmony_ci  pbuf_free(p);
696195972f6Sopenharmony_ci}
697195972f6Sopenharmony_ciEND_TEST
698195972f6Sopenharmony_ci
699195972f6Sopenharmony_ciSTART_TEST(compress_no_match)
700195972f6Sopenharmony_ci{
701195972f6Sopenharmony_ci  static const u8_t data[] = {
702195972f6Sopenharmony_ci      0x00, 0x00,
703195972f6Sopenharmony_ci      0x04, 'l', 'w', 'i', 'p', 0x05, 'w', 'i', 'k', 'i', 'a', 0x03, 'c', 'o', 'm', 0x00
704195972f6Sopenharmony_ci  };
705195972f6Sopenharmony_ci  struct pbuf *p;
706195972f6Sopenharmony_ci  struct mdns_domain domain;
707195972f6Sopenharmony_ci  u16_t offset;
708195972f6Sopenharmony_ci  u16_t length;
709195972f6Sopenharmony_ci  err_t res;
710195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(_i);
711195972f6Sopenharmony_ci
712195972f6Sopenharmony_ci  p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
713195972f6Sopenharmony_ci  fail_if(p == NULL);
714195972f6Sopenharmony_ci  p->payload = (void *)(size_t)data;
715195972f6Sopenharmony_ci
716195972f6Sopenharmony_ci  memset(&domain, 0, sizeof(domain));
717195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain, "foobar", 6);
718195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
719195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain, "local", 5);
720195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
721195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain, NULL, 0);
722195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
723195972f6Sopenharmony_ci
724195972f6Sopenharmony_ci  offset = 2;
725195972f6Sopenharmony_ci  length = mdns_compress_domain(p, &offset, &domain);
726195972f6Sopenharmony_ci  /* Write all bytes, no jump */
727195972f6Sopenharmony_ci  fail_unless(length == domain.length);
728195972f6Sopenharmony_ci
729195972f6Sopenharmony_ci  pbuf_free(p);
730195972f6Sopenharmony_ci}
731195972f6Sopenharmony_ciEND_TEST
732195972f6Sopenharmony_ci
733195972f6Sopenharmony_ciSTART_TEST(compress_2nd_label)
734195972f6Sopenharmony_ci{
735195972f6Sopenharmony_ci  static const u8_t data[] = {
736195972f6Sopenharmony_ci      0x00, 0x00,
737195972f6Sopenharmony_ci      0x06, 'f', 'o', 'o', 'b', 'a', 'r', 0x05, 'l', 'o', 'c', 'a', 'l', 0x00
738195972f6Sopenharmony_ci  };
739195972f6Sopenharmony_ci  struct pbuf *p;
740195972f6Sopenharmony_ci  struct mdns_domain domain;
741195972f6Sopenharmony_ci  u16_t offset;
742195972f6Sopenharmony_ci  u16_t length;
743195972f6Sopenharmony_ci  err_t res;
744195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(_i);
745195972f6Sopenharmony_ci
746195972f6Sopenharmony_ci  p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
747195972f6Sopenharmony_ci  fail_if(p == NULL);
748195972f6Sopenharmony_ci  p->payload = (void *)(size_t)data;
749195972f6Sopenharmony_ci
750195972f6Sopenharmony_ci  memset(&domain, 0, sizeof(domain));
751195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain, "lwip", 4);
752195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
753195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain, "local", 5);
754195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
755195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain, NULL, 0);
756195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
757195972f6Sopenharmony_ci
758195972f6Sopenharmony_ci  offset = 2;
759195972f6Sopenharmony_ci  length = mdns_compress_domain(p, &offset, &domain);
760195972f6Sopenharmony_ci  /* Write 5 bytes, then a jump to addr 9 */
761195972f6Sopenharmony_ci  fail_unless(length == 5);
762195972f6Sopenharmony_ci  fail_unless(offset == 9);
763195972f6Sopenharmony_ci
764195972f6Sopenharmony_ci  pbuf_free(p);
765195972f6Sopenharmony_ci}
766195972f6Sopenharmony_ciEND_TEST
767195972f6Sopenharmony_ci
768195972f6Sopenharmony_ciSTART_TEST(compress_2nd_label_short)
769195972f6Sopenharmony_ci{
770195972f6Sopenharmony_ci  static const u8_t data[] = {
771195972f6Sopenharmony_ci      0x00, 0x00,
772195972f6Sopenharmony_ci      0x04, 'l', 'w', 'i', 'p', 0x05, 'l', 'o', 'c', 'a', 'l', 0x00
773195972f6Sopenharmony_ci  };
774195972f6Sopenharmony_ci  struct pbuf *p;
775195972f6Sopenharmony_ci  struct mdns_domain domain;
776195972f6Sopenharmony_ci  u16_t offset;
777195972f6Sopenharmony_ci  u16_t length;
778195972f6Sopenharmony_ci  err_t res;
779195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(_i);
780195972f6Sopenharmony_ci
781195972f6Sopenharmony_ci  p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
782195972f6Sopenharmony_ci  fail_if(p == NULL);
783195972f6Sopenharmony_ci  p->payload = (void *)(size_t)data;
784195972f6Sopenharmony_ci
785195972f6Sopenharmony_ci  memset(&domain, 0, sizeof(domain));
786195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain, "foobar", 6);
787195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
788195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain, "local", 5);
789195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
790195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain, NULL, 0);
791195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
792195972f6Sopenharmony_ci
793195972f6Sopenharmony_ci  offset = 2;
794195972f6Sopenharmony_ci  length = mdns_compress_domain(p, &offset, &domain);
795195972f6Sopenharmony_ci  /* Write 5 bytes, then a jump to addr 7 */
796195972f6Sopenharmony_ci  fail_unless(length == 7);
797195972f6Sopenharmony_ci  fail_unless(offset == 7);
798195972f6Sopenharmony_ci
799195972f6Sopenharmony_ci  pbuf_free(p);
800195972f6Sopenharmony_ci}
801195972f6Sopenharmony_ciEND_TEST
802195972f6Sopenharmony_ci
803195972f6Sopenharmony_ciSTART_TEST(compress_jump_to_jump)
804195972f6Sopenharmony_ci{
805195972f6Sopenharmony_ci  static const u8_t data[] = {
806195972f6Sopenharmony_ci      /* 0x00 */ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
807195972f6Sopenharmony_ci                 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
808195972f6Sopenharmony_ci      /* 0x10 */ 0x04, 'l', 'w', 'i', 'p', 0x05, 'l', 'o', 'c', 'a', 'l', 0x00, 0xc0, 0x00, 0x02, 0x00,
809195972f6Sopenharmony_ci      /* 0x20 */ 0x07, 'b', 'a', 'n', 'a', 'n', 'a', 's', 0xc0, 0x15
810195972f6Sopenharmony_ci  };
811195972f6Sopenharmony_ci  struct pbuf *p;
812195972f6Sopenharmony_ci  struct mdns_domain domain;
813195972f6Sopenharmony_ci  u16_t offset;
814195972f6Sopenharmony_ci  u16_t length;
815195972f6Sopenharmony_ci  err_t res;
816195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(_i);
817195972f6Sopenharmony_ci
818195972f6Sopenharmony_ci  p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
819195972f6Sopenharmony_ci  fail_if(p == NULL);
820195972f6Sopenharmony_ci  p->payload = (void *)(size_t)data;
821195972f6Sopenharmony_ci
822195972f6Sopenharmony_ci  memset(&domain, 0, sizeof(domain));
823195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain, "foobar", 6);
824195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
825195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain, "local", 5);
826195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
827195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain, NULL, 0);
828195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
829195972f6Sopenharmony_ci
830195972f6Sopenharmony_ci  offset = 0x20;
831195972f6Sopenharmony_ci  length = mdns_compress_domain(p, &offset, &domain);
832195972f6Sopenharmony_ci  /* Dont compress if jump would be to a jump */
833195972f6Sopenharmony_ci  fail_unless(length == domain.length);
834195972f6Sopenharmony_ci
835195972f6Sopenharmony_ci  offset = 0x10;
836195972f6Sopenharmony_ci  length = mdns_compress_domain(p, &offset, &domain);
837195972f6Sopenharmony_ci  /* Write 7 bytes, then a jump to addr 0x15 */
838195972f6Sopenharmony_ci  fail_unless(length == 7);
839195972f6Sopenharmony_ci  fail_unless(offset == 0x15);
840195972f6Sopenharmony_ci
841195972f6Sopenharmony_ci  pbuf_free(p);
842195972f6Sopenharmony_ci}
843195972f6Sopenharmony_ciEND_TEST
844195972f6Sopenharmony_ci
845195972f6Sopenharmony_ciSTART_TEST(compress_long_match)
846195972f6Sopenharmony_ci{
847195972f6Sopenharmony_ci  static const u8_t data[] = {
848195972f6Sopenharmony_ci      0x00, 0x00,
849195972f6Sopenharmony_ci      0x06, 'f', 'o', 'o', 'b', 'a', 'r', 0x05, 'l', 'o', 'c', 'a', 'l', 0x03, 'c', 'o', 'm', 0x00
850195972f6Sopenharmony_ci  };
851195972f6Sopenharmony_ci  struct pbuf *p;
852195972f6Sopenharmony_ci  struct mdns_domain domain;
853195972f6Sopenharmony_ci  u16_t offset;
854195972f6Sopenharmony_ci  u16_t length;
855195972f6Sopenharmony_ci  err_t res;
856195972f6Sopenharmony_ci  LWIP_UNUSED_ARG(_i);
857195972f6Sopenharmony_ci
858195972f6Sopenharmony_ci  p = pbuf_alloc(PBUF_RAW, sizeof(data), PBUF_ROM);
859195972f6Sopenharmony_ci  fail_if(p == NULL);
860195972f6Sopenharmony_ci  p->payload = (void *)(size_t)data;
861195972f6Sopenharmony_ci
862195972f6Sopenharmony_ci  memset(&domain, 0, sizeof(domain));
863195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain, "foobar", 6);
864195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
865195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain, "local", 5);
866195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
867195972f6Sopenharmony_ci  res = mdns_domain_add_label(&domain, NULL, 0);
868195972f6Sopenharmony_ci  fail_unless(res == ERR_OK);
869195972f6Sopenharmony_ci
870195972f6Sopenharmony_ci  offset = 2;
871195972f6Sopenharmony_ci  length = mdns_compress_domain(p, &offset, &domain);
872195972f6Sopenharmony_ci  fail_unless(length == domain.length);
873195972f6Sopenharmony_ci
874195972f6Sopenharmony_ci  pbuf_free(p);
875195972f6Sopenharmony_ci}
876195972f6Sopenharmony_ciEND_TEST
877195972f6Sopenharmony_ci
878195972f6Sopenharmony_ciSuite* mdns_suite(void)
879195972f6Sopenharmony_ci{
880195972f6Sopenharmony_ci  testfunc tests[] = {
881195972f6Sopenharmony_ci    TESTFUNC(readname_basic),
882195972f6Sopenharmony_ci    TESTFUNC(readname_anydata),
883195972f6Sopenharmony_ci    TESTFUNC(readname_short_buf),
884195972f6Sopenharmony_ci    TESTFUNC(readname_long_label),
885195972f6Sopenharmony_ci    TESTFUNC(readname_overflow),
886195972f6Sopenharmony_ci    TESTFUNC(readname_jump_earlier),
887195972f6Sopenharmony_ci    TESTFUNC(readname_jump_earlier_jump),
888195972f6Sopenharmony_ci    TESTFUNC(readname_jump_maxdepth),
889195972f6Sopenharmony_ci    TESTFUNC(readname_jump_later),
890195972f6Sopenharmony_ci    TESTFUNC(readname_half_jump),
891195972f6Sopenharmony_ci    TESTFUNC(readname_jump_toolong),
892195972f6Sopenharmony_ci    TESTFUNC(readname_jump_loop_label),
893195972f6Sopenharmony_ci    TESTFUNC(readname_jump_loop_jump),
894195972f6Sopenharmony_ci
895195972f6Sopenharmony_ci    TESTFUNC(add_label_basic),
896195972f6Sopenharmony_ci    TESTFUNC(add_label_long_label),
897195972f6Sopenharmony_ci    TESTFUNC(add_label_full),
898195972f6Sopenharmony_ci
899195972f6Sopenharmony_ci    TESTFUNC(domain_eq_basic),
900195972f6Sopenharmony_ci    TESTFUNC(domain_eq_diff),
901195972f6Sopenharmony_ci    TESTFUNC(domain_eq_case),
902195972f6Sopenharmony_ci    TESTFUNC(domain_eq_anydata),
903195972f6Sopenharmony_ci    TESTFUNC(domain_eq_length),
904195972f6Sopenharmony_ci
905195972f6Sopenharmony_ci    TESTFUNC(compress_full_match),
906195972f6Sopenharmony_ci    TESTFUNC(compress_full_match_subset),
907195972f6Sopenharmony_ci    TESTFUNC(compress_full_match_jump),
908195972f6Sopenharmony_ci    TESTFUNC(compress_no_match),
909195972f6Sopenharmony_ci    TESTFUNC(compress_2nd_label),
910195972f6Sopenharmony_ci    TESTFUNC(compress_2nd_label_short),
911195972f6Sopenharmony_ci    TESTFUNC(compress_jump_to_jump),
912195972f6Sopenharmony_ci    TESTFUNC(compress_long_match),
913195972f6Sopenharmony_ci  };
914195972f6Sopenharmony_ci  return create_suite("MDNS", tests, sizeof(tests)/sizeof(testfunc), NULL, NULL);
915195972f6Sopenharmony_ci}
916