2 * Copyright (c) 2015 National Instruments
5 * Joe Hershberger <joe.hershberger@ni.com>
7 * SPDX-License-Identifier: GPL-2.0
19 DECLARE_GLOBAL_DATA_PTR;
21 static int dm_test_eth(struct unit_test_state *uts)
23 net_ping_ip = string_to_ip("1.1.2.2");
25 setenv("ethact", "eth@10002000");
26 ut_assertok(net_loop(PING));
27 ut_asserteq_str("eth@10002000", getenv("ethact"));
29 setenv("ethact", "eth@10003000");
30 ut_assertok(net_loop(PING));
31 ut_asserteq_str("eth@10003000", getenv("ethact"));
33 setenv("ethact", "eth@10004000");
34 ut_assertok(net_loop(PING));
35 ut_asserteq_str("eth@10004000", getenv("ethact"));
39 DM_TEST(dm_test_eth, DM_TESTF_SCAN_FDT);
41 static int dm_test_eth_alias(struct unit_test_state *uts)
43 net_ping_ip = string_to_ip("1.1.2.2");
44 setenv("ethact", "eth0");
45 ut_assertok(net_loop(PING));
46 ut_asserteq_str("eth@10002000", getenv("ethact"));
48 setenv("ethact", "eth1");
49 ut_assertok(net_loop(PING));
50 ut_asserteq_str("eth@10004000", getenv("ethact"));
52 /* Expected to fail since eth2 is not defined in the device tree */
53 setenv("ethact", "eth2");
54 ut_assertok(net_loop(PING));
55 ut_asserteq_str("eth@10002000", getenv("ethact"));
57 setenv("ethact", "eth5");
58 ut_assertok(net_loop(PING));
59 ut_asserteq_str("eth@10003000", getenv("ethact"));
63 DM_TEST(dm_test_eth_alias, DM_TESTF_SCAN_FDT);
65 static int dm_test_eth_prime(struct unit_test_state *uts)
67 net_ping_ip = string_to_ip("1.1.2.2");
69 /* Expected to be "eth@10003000" because of ethprime variable */
70 setenv("ethact", NULL);
71 setenv("ethprime", "eth5");
72 ut_assertok(net_loop(PING));
73 ut_asserteq_str("eth@10003000", getenv("ethact"));
75 /* Expected to be "eth@10002000" because it is first */
76 setenv("ethact", NULL);
77 setenv("ethprime", NULL);
78 ut_assertok(net_loop(PING));
79 ut_asserteq_str("eth@10002000", getenv("ethact"));
83 DM_TEST(dm_test_eth_prime, DM_TESTF_SCAN_FDT);
85 /* The asserts include a return on fail; cleanup in the caller */
86 static int _dm_test_eth_rotate1(struct unit_test_state *uts)
88 /* Make sure that the default is to rotate to the next interface */
89 setenv("ethact", "eth@10004000");
90 ut_assertok(net_loop(PING));
91 ut_asserteq_str("eth@10002000", getenv("ethact"));
93 /* If ethrotate is no, then we should fail on a bad MAC */
94 setenv("ethact", "eth@10004000");
95 setenv("ethrotate", "no");
96 ut_asserteq(-EINVAL, net_loop(PING));
97 ut_asserteq_str("eth@10004000", getenv("ethact"));
102 static int _dm_test_eth_rotate2(struct unit_test_state *uts)
104 /* Make sure we can skip invalid devices */
105 setenv("ethact", "eth@10004000");
106 ut_assertok(net_loop(PING));
107 ut_asserteq_str("eth@10004000", getenv("ethact"));
109 /* Make sure we can handle device name which is not eth# */
110 setenv("ethact", "sbe5");
111 ut_assertok(net_loop(PING));
112 ut_asserteq_str("sbe5", getenv("ethact"));
117 static int dm_test_eth_rotate(struct unit_test_state *uts)
122 /* Set target IP to mock ping */
123 net_ping_ip = string_to_ip("1.1.2.2");
125 /* Invalidate eth1's MAC address */
126 strcpy(ethaddr, getenv("eth1addr"));
127 /* Must disable access protection for eth1addr before clearing */
128 setenv(".flags", "eth1addr");
129 setenv("eth1addr", NULL);
131 retval = _dm_test_eth_rotate1(uts);
133 /* Restore the env */
134 setenv("eth1addr", ethaddr);
135 setenv("ethrotate", NULL);
138 /* Invalidate eth0's MAC address */
139 strcpy(ethaddr, getenv("ethaddr"));
140 /* Must disable access protection for ethaddr before clearing */
141 setenv(".flags", "ethaddr");
142 setenv("ethaddr", NULL);
144 retval = _dm_test_eth_rotate2(uts);
146 /* Restore the env */
147 setenv("ethaddr", ethaddr);
149 /* Restore the env */
150 setenv(".flags", NULL);
154 DM_TEST(dm_test_eth_rotate, DM_TESTF_SCAN_FDT);
156 /* The asserts include a return on fail; cleanup in the caller */
157 static int _dm_test_net_retry(struct unit_test_state *uts)
160 * eth1 is disabled and netretry is yes, so the ping should succeed and
161 * the active device should be eth0
163 sandbox_eth_disable_response(1, true);
164 setenv("ethact", "eth@10004000");
165 setenv("netretry", "yes");
166 sandbox_eth_skip_timeout();
167 ut_assertok(net_loop(PING));
168 ut_asserteq_str("eth@10002000", getenv("ethact"));
171 * eth1 is disabled and netretry is no, so the ping should fail and the
172 * active device should be eth1
174 setenv("ethact", "eth@10004000");
175 setenv("netretry", "no");
176 sandbox_eth_skip_timeout();
177 ut_asserteq(-ETIMEDOUT, net_loop(PING));
178 ut_asserteq_str("eth@10004000", getenv("ethact"));
183 static int dm_test_net_retry(struct unit_test_state *uts)
187 net_ping_ip = string_to_ip("1.1.2.2");
189 retval = _dm_test_net_retry(uts);
191 /* Restore the env */
192 setenv("netretry", NULL);
193 sandbox_eth_disable_response(1, false);
197 DM_TEST(dm_test_net_retry, DM_TESTF_SCAN_FDT);