File: test_netif.c

package info (click to toggle)
lwip 2.2.1%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 10,008 kB
  • sloc: ansic: 109,524; cs: 6,714; sh: 115; makefile: 112; perl: 81
file content (285 lines) | stat: -rw-r--r-- 7,821 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include "test_netif.h"

#include "lwip/netif.h"
#include "lwip/stats.h"
#include "lwip/etharp.h"
#include "netif/ethernet.h"

#if !LWIP_NETIF_EXT_STATUS_CALLBACK
#error "This tests needs LWIP_NETIF_EXT_STATUS_CALLBACK enabled"
#endif

static struct netif net_test;


/* Setups/teardown functions */

static void
netif_setup(void)
{
  lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
}

static void
netif_teardown(void)
{
  lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
}

/* test helper functions */

static err_t
testif_tx_func(struct netif *netif, struct pbuf *p)
{
  LWIP_UNUSED_ARG(netif);
  LWIP_UNUSED_ARG(p);
  return ERR_OK;
}

static err_t
testif_init(struct netif *netif)
{
  netif->name[0] = 'c';
  netif->name[1] = 'h';
  netif->output = etharp_output;
  netif->linkoutput = testif_tx_func;
  netif->mtu = 1500;
  netif->hwaddr_len = 6;
  netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET | NETIF_FLAG_IGMP | NETIF_FLAG_MLD6;

  netif->hwaddr[0] = 0x02;
  netif->hwaddr[1] = 0x03;
  netif->hwaddr[2] = 0x04;
  netif->hwaddr[3] = 0x05;
  netif->hwaddr[4] = 0x06;
  netif->hwaddr[5] = 0x07;

  return ERR_OK;
}

#define MAX_NSC_REASON_IDX 10
static netif_nsc_reason_t expected_reasons;
static int callback_ctr;

static int dummy_active;

static void
test_netif_ext_callback_dummy(struct netif* netif, netif_nsc_reason_t reason, const netif_ext_callback_args_t* args)
{
  LWIP_UNUSED_ARG(netif);
  LWIP_UNUSED_ARG(reason);
  LWIP_UNUSED_ARG(args);

  fail_unless(dummy_active);
}

static void
test_netif_ext_callback(struct netif* netif, netif_nsc_reason_t reason, const netif_ext_callback_args_t* args)
{
  LWIP_UNUSED_ARG(args); /* @todo */
  callback_ctr++;

  fail_unless(netif == &net_test);

  fail_unless(expected_reasons == reason);
}

/* Test functions */

NETIF_DECLARE_EXT_CALLBACK(netif_callback_1)
NETIF_DECLARE_EXT_CALLBACK(netif_callback_2)
NETIF_DECLARE_EXT_CALLBACK(netif_callback_3)

START_TEST(test_netif_extcallbacks)
{
  ip4_addr_t addr;
  ip4_addr_t netmask;
  ip4_addr_t gw;
  LWIP_UNUSED_ARG(_i);

  IP4_ADDR(&addr, 0, 0, 0, 0);
  IP4_ADDR(&netmask, 0, 0, 0, 0);
  IP4_ADDR(&gw, 0, 0, 0, 0);

  netif_add_ext_callback(&netif_callback_3, test_netif_ext_callback_dummy);
  netif_add_ext_callback(&netif_callback_2, test_netif_ext_callback);
  netif_add_ext_callback(&netif_callback_1, test_netif_ext_callback_dummy);

  dummy_active = 1;

  /* positive tests: check that single events come as expected */

  expected_reasons = LWIP_NSC_NETIF_ADDED;
  callback_ctr = 0;
  netif_add(&net_test, &addr, &netmask, &gw, &net_test, testif_init, ethernet_input);
  fail_unless(callback_ctr == 1);

  expected_reasons = LWIP_NSC_LINK_CHANGED;
  callback_ctr = 0;
  netif_set_link_up(&net_test);
  fail_unless(callback_ctr == 1);

  expected_reasons = LWIP_NSC_STATUS_CHANGED;
  callback_ctr = 0;
  netif_set_up(&net_test);
  fail_unless(callback_ctr == 1);

  IP4_ADDR(&addr, 1, 2, 3, 4);
  expected_reasons = LWIP_NSC_IPV4_ADDRESS_CHANGED;
  callback_ctr = 0;
  netif_set_ipaddr(&net_test, &addr);
  fail_unless(callback_ctr == 1);

  IP4_ADDR(&netmask, 255, 255, 255, 0);
  expected_reasons = LWIP_NSC_IPV4_NETMASK_CHANGED;
  callback_ctr = 0;
  netif_set_netmask(&net_test, &netmask);
  fail_unless(callback_ctr == 1);

  IP4_ADDR(&gw, 1, 2, 3, 254);
  expected_reasons = LWIP_NSC_IPV4_GATEWAY_CHANGED;
  callback_ctr = 0;
  netif_set_gw(&net_test, &gw);
  fail_unless(callback_ctr == 1);

  IP4_ADDR(&addr, 0, 0, 0, 0);
  expected_reasons = LWIP_NSC_IPV4_ADDRESS_CHANGED;
  callback_ctr = 0;
  netif_set_ipaddr(&net_test, &addr);
  fail_unless(callback_ctr == 1);

  IP4_ADDR(&netmask, 0, 0, 0, 0);
  expected_reasons = LWIP_NSC_IPV4_NETMASK_CHANGED;
  callback_ctr = 0;
  netif_set_netmask(&net_test, &netmask);
  fail_unless(callback_ctr == 1);

  IP4_ADDR(&gw, 0, 0, 0, 0);
  expected_reasons = LWIP_NSC_IPV4_GATEWAY_CHANGED;
  callback_ctr = 0;
  netif_set_gw(&net_test, &gw);
  fail_unless(callback_ctr == 1);

  /* check for multi-events (only one combined callback expected) */

  IP4_ADDR(&addr, 1, 2, 3, 4);
  IP4_ADDR(&netmask, 255, 255, 255, 0);
  IP4_ADDR(&gw, 1, 2, 3, 254);
  expected_reasons = (netif_nsc_reason_t)(LWIP_NSC_IPV4_ADDRESS_CHANGED | LWIP_NSC_IPV4_NETMASK_CHANGED |
                                          LWIP_NSC_IPV4_GATEWAY_CHANGED | LWIP_NSC_IPV4_SETTINGS_CHANGED |
                                          LWIP_NSC_IPV4_ADDR_VALID);
  callback_ctr = 0;
  netif_set_addr(&net_test, &addr, &netmask, &gw);
  fail_unless(callback_ctr == 1);

  /* check that for no-change, no callback is expected */
  expected_reasons = LWIP_NSC_NONE;
  callback_ctr = 0;
  netif_set_ipaddr(&net_test, &addr);
  fail_unless(callback_ctr == 0);

  netif_set_netmask(&net_test, &netmask);
  callback_ctr = 0;
  fail_unless(callback_ctr == 0);

  callback_ctr = 0;
  netif_set_gw(&net_test, &gw);
  fail_unless(callback_ctr == 0);

  /* netif_set_addr() always issues at least LWIP_NSC_IPV4_ADDR_VALID */
  expected_reasons = LWIP_NSC_IPV4_ADDR_VALID;
  callback_ctr = 0;
  netif_set_addr(&net_test, &addr, &netmask, &gw);
  fail_unless(callback_ctr == 1);

  /* check for single-events */
  IP4_ADDR(&addr, 1, 2, 3, 5);
  expected_reasons = (netif_nsc_reason_t)(LWIP_NSC_IPV4_ADDRESS_CHANGED | LWIP_NSC_IPV4_SETTINGS_CHANGED |
                                          LWIP_NSC_IPV4_ADDR_VALID);
  callback_ctr = 0;
  netif_set_addr(&net_test, &addr, &netmask, &gw);
  fail_unless(callback_ctr == 1);

  expected_reasons = LWIP_NSC_STATUS_CHANGED;
  callback_ctr = 0;
  netif_set_down(&net_test);
  fail_unless(callback_ctr == 1);

  expected_reasons = LWIP_NSC_NETIF_REMOVED;
  callback_ctr = 0;
  netif_remove(&net_test);
  fail_unless(callback_ctr == 1);

  expected_reasons = LWIP_NSC_NONE;

  netif_remove_ext_callback(&netif_callback_2);
  netif_remove_ext_callback(&netif_callback_3);
  netif_remove_ext_callback(&netif_callback_1);
  dummy_active = 0;
}
END_TEST

START_TEST(test_netif_flag_set)
{
  ip4_addr_t addr;
  ip4_addr_t netmask;
  ip4_addr_t gw;
  LWIP_UNUSED_ARG(_i);

  IP4_ADDR(&addr, 0, 0, 0, 0);
  IP4_ADDR(&netmask, 0, 0, 0, 0);
  IP4_ADDR(&gw, 0, 0, 0, 0);

  netif_add(&net_test, &addr, &netmask, &gw, &net_test, testif_init, ethernet_input);

  fail_if(netif_is_flag_set(&net_test, NETIF_FLAG_UP));
  fail_unless(netif_is_flag_set(&net_test, NETIF_FLAG_BROADCAST));
  fail_if(netif_is_flag_set(&net_test, NETIF_FLAG_LINK_UP));
  fail_unless(netif_is_flag_set(&net_test, NETIF_FLAG_ETHARP));
  fail_unless(netif_is_flag_set(&net_test, NETIF_FLAG_ETHERNET));
  fail_unless(netif_is_flag_set(&net_test, NETIF_FLAG_IGMP));
  fail_unless(netif_is_flag_set(&net_test, NETIF_FLAG_MLD6));

  netif_remove(&net_test);
}
END_TEST

START_TEST(test_netif_find)
{
  struct netif net0;
  struct netif net1;
  LWIP_UNUSED_ARG(_i);

  /* No netifs available */
  fail_unless(netif_find("ch0") == NULL);

  /* Add netifs with known names */
  fail_unless(netif_add_noaddr(&net0, NULL, testif_init, ethernet_input) == &net0);
  net0.num = 0;
  fail_unless(netif_add_noaddr(&net1, NULL, testif_init, ethernet_input) == &net1);
  net1.num = 1;

  fail_unless(netif_find("ch0") == &net0);
  fail_unless(netif_find("CH0") == NULL);
  fail_unless(netif_find("ch1") == &net1);
  fail_unless(netif_find("ch3") == NULL);
  /* atoi failure is not treated as zero */
  fail_unless(netif_find("chX") == NULL);
  fail_unless(netif_find("ab0") == NULL);

  netif_remove(&net0);
  netif_remove(&net1);
}
END_TEST

/** Create the suite including all tests for this module */
Suite *
netif_suite(void)
{
  testfunc tests[] = {
    TESTFUNC(test_netif_extcallbacks),
    TESTFUNC(test_netif_flag_set),
    TESTFUNC(test_netif_find)
  };
  return create_suite("NETIF", tests, sizeof(tests)/sizeof(testfunc), netif_setup, netif_teardown);
}