blob: 700abdddbdd72c953fd5e766818de202737d38db [file] [log] [blame]
Joe Hershbergerbfacad72015-03-22 17:09:15 -05001/*
2 * Copyright (c) 2015 National Instruments
3 *
4 * (C) Copyright 2015
5 * Joe Hershberger <joe.hershberger@ni.com>
6 *
7 * SPDX-License-Identifier: GPL-2.0
8 */
9
10#include <common.h>
11#include <dm.h>
Joe Hershbergerbfacad72015-03-22 17:09:15 -050012#include <fdtdec.h>
13#include <malloc.h>
14#include <net.h>
Joe Hershbergere721b882015-05-20 14:27:27 -050015#include <dm/test.h>
Joe Hershberger7ece1c62015-03-22 17:09:20 -050016#include <asm/eth.h>
Joe Hershbergere721b882015-05-20 14:27:27 -050017#include <test/ut.h>
Joe Hershbergerbfacad72015-03-22 17:09:15 -050018
19DECLARE_GLOBAL_DATA_PTR;
20
Joe Hershbergere721b882015-05-20 14:27:27 -050021static int dm_test_eth(struct unit_test_state *uts)
Joe Hershbergerbfacad72015-03-22 17:09:15 -050022{
Joe Hershberger049a95a2015-04-08 01:41:01 -050023 net_ping_ip = string_to_ip("1.1.2.2");
Joe Hershbergerbfacad72015-03-22 17:09:15 -050024
25 setenv("ethact", "eth@10002000");
Joe Hershbergerbc0571f2015-04-08 01:41:21 -050026 ut_assertok(net_loop(PING));
Joe Hershbergerbfacad72015-03-22 17:09:15 -050027 ut_asserteq_str("eth@10002000", getenv("ethact"));
28
29 setenv("ethact", "eth@10003000");
Joe Hershbergerbc0571f2015-04-08 01:41:21 -050030 ut_assertok(net_loop(PING));
Joe Hershbergerbfacad72015-03-22 17:09:15 -050031 ut_asserteq_str("eth@10003000", getenv("ethact"));
32
33 setenv("ethact", "eth@10004000");
Joe Hershbergerbc0571f2015-04-08 01:41:21 -050034 ut_assertok(net_loop(PING));
Joe Hershbergerbfacad72015-03-22 17:09:15 -050035 ut_asserteq_str("eth@10004000", getenv("ethact"));
36
37 return 0;
38}
39DM_TEST(dm_test_eth, DM_TESTF_SCAN_FDT);
Joe Hershbergere58780d2015-03-22 17:09:16 -050040
Joe Hershbergere721b882015-05-20 14:27:27 -050041static int dm_test_eth_alias(struct unit_test_state *uts)
Joe Hershbergere58780d2015-03-22 17:09:16 -050042{
Joe Hershberger049a95a2015-04-08 01:41:01 -050043 net_ping_ip = string_to_ip("1.1.2.2");
Joe Hershbergere58780d2015-03-22 17:09:16 -050044 setenv("ethact", "eth0");
Joe Hershbergerbc0571f2015-04-08 01:41:21 -050045 ut_assertok(net_loop(PING));
Joe Hershbergere58780d2015-03-22 17:09:16 -050046 ut_asserteq_str("eth@10002000", getenv("ethact"));
47
48 setenv("ethact", "eth1");
Joe Hershbergerbc0571f2015-04-08 01:41:21 -050049 ut_assertok(net_loop(PING));
Joe Hershbergere58780d2015-03-22 17:09:16 -050050 ut_asserteq_str("eth@10004000", getenv("ethact"));
51
52 /* Expected to fail since eth2 is not defined in the device tree */
53 setenv("ethact", "eth2");
Joe Hershbergerbc0571f2015-04-08 01:41:21 -050054 ut_assertok(net_loop(PING));
Joe Hershbergere58780d2015-03-22 17:09:16 -050055 ut_asserteq_str("eth@10002000", getenv("ethact"));
56
57 setenv("ethact", "eth5");
Joe Hershbergerbc0571f2015-04-08 01:41:21 -050058 ut_assertok(net_loop(PING));
Joe Hershbergere58780d2015-03-22 17:09:16 -050059 ut_asserteq_str("eth@10003000", getenv("ethact"));
60
61 return 0;
62}
63DM_TEST(dm_test_eth_alias, DM_TESTF_SCAN_FDT);
Joe Hershberger6536b9b2015-03-22 17:09:17 -050064
Joe Hershbergere721b882015-05-20 14:27:27 -050065static int dm_test_eth_prime(struct unit_test_state *uts)
Joe Hershberger6536b9b2015-03-22 17:09:17 -050066{
Joe Hershberger049a95a2015-04-08 01:41:01 -050067 net_ping_ip = string_to_ip("1.1.2.2");
Joe Hershberger6536b9b2015-03-22 17:09:17 -050068
69 /* Expected to be "eth@10003000" because of ethprime variable */
70 setenv("ethact", NULL);
71 setenv("ethprime", "eth5");
Joe Hershbergerbc0571f2015-04-08 01:41:21 -050072 ut_assertok(net_loop(PING));
Joe Hershberger6536b9b2015-03-22 17:09:17 -050073 ut_asserteq_str("eth@10003000", getenv("ethact"));
74
75 /* Expected to be "eth@10002000" because it is first */
76 setenv("ethact", NULL);
77 setenv("ethprime", NULL);
Joe Hershbergerbc0571f2015-04-08 01:41:21 -050078 ut_assertok(net_loop(PING));
Joe Hershberger6536b9b2015-03-22 17:09:17 -050079 ut_asserteq_str("eth@10002000", getenv("ethact"));
80
81 return 0;
82}
83DM_TEST(dm_test_eth_prime, DM_TESTF_SCAN_FDT);
Joe Hershberger7d104ea2015-03-22 17:09:18 -050084
Joe Hershberger09129be2015-05-20 14:27:33 -050085/* The asserts include a return on fail; cleanup in the caller */
86static int _dm_test_eth_rotate1(struct unit_test_state *uts)
Joe Hershberger7d104ea2015-03-22 17:09:18 -050087{
Joe Hershberger7d104ea2015-03-22 17:09:18 -050088 /* Make sure that the default is to rotate to the next interface */
89 setenv("ethact", "eth@10004000");
Joe Hershbergerbc0571f2015-04-08 01:41:21 -050090 ut_assertok(net_loop(PING));
Joe Hershberger7d104ea2015-03-22 17:09:18 -050091 ut_asserteq_str("eth@10002000", getenv("ethact"));
92
93 /* If ethrotate is no, then we should fail on a bad MAC */
94 setenv("ethact", "eth@10004000");
95 setenv("ethrotate", "no");
Joe Hershbergerbc0571f2015-04-08 01:41:21 -050096 ut_asserteq(-EINVAL, net_loop(PING));
Joe Hershberger7d104ea2015-03-22 17:09:18 -050097 ut_asserteq_str("eth@10004000", getenv("ethact"));
98
Joe Hershberger09129be2015-05-20 14:27:33 -050099 return 0;
100}
Joe Hershberger7d104ea2015-03-22 17:09:18 -0500101
Joe Hershberger09129be2015-05-20 14:27:33 -0500102static int _dm_test_eth_rotate2(struct unit_test_state *uts)
103{
Joe Hershberger7d104ea2015-03-22 17:09:18 -0500104 /* Make sure we can skip invalid devices */
105 setenv("ethact", "eth@10004000");
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500106 ut_assertok(net_loop(PING));
Joe Hershberger7d104ea2015-03-22 17:09:18 -0500107 ut_asserteq_str("eth@10004000", getenv("ethact"));
108
Joe Hershberger09129be2015-05-20 14:27:33 -0500109 return 0;
110}
111
112static int dm_test_eth_rotate(struct unit_test_state *uts)
113{
114 char ethaddr[18];
115 int retval;
116
117 /* Set target IP to mock ping */
118 net_ping_ip = string_to_ip("1.1.2.2");
119
120 /* Invalidate eth1's MAC address */
121 strcpy(ethaddr, getenv("eth1addr"));
122 /* Must disable access protection for eth1addr before clearing */
123 setenv(".flags", "eth1addr");
124 setenv("eth1addr", NULL);
125
126 retval = _dm_test_eth_rotate1(uts);
127
Joe Hershberger7d104ea2015-03-22 17:09:18 -0500128 /* Restore the env */
Joe Hershberger09129be2015-05-20 14:27:33 -0500129 setenv("eth1addr", ethaddr);
130 setenv("ethrotate", NULL);
131
132 if (!retval) {
133 /* Invalidate eth0's MAC address */
134 strcpy(ethaddr, getenv("ethaddr"));
135 /* Must disable access protection for ethaddr before clearing */
136 setenv(".flags", "ethaddr");
137 setenv("ethaddr", NULL);
138
139 retval = _dm_test_eth_rotate2(uts);
140
141 /* Restore the env */
142 setenv("ethaddr", ethaddr);
143 }
144 /* Restore the env */
Joe Hershberger7d104ea2015-03-22 17:09:18 -0500145 setenv(".flags", NULL);
146
Joe Hershberger09129be2015-05-20 14:27:33 -0500147 return retval;
Joe Hershberger7d104ea2015-03-22 17:09:18 -0500148}
149DM_TEST(dm_test_eth_rotate, DM_TESTF_SCAN_FDT);
Joe Hershberger7ece1c62015-03-22 17:09:20 -0500150
Joe Hershberger09129be2015-05-20 14:27:33 -0500151/* The asserts include a return on fail; cleanup in the caller */
152static int _dm_test_net_retry(struct unit_test_state *uts)
Joe Hershberger7ece1c62015-03-22 17:09:20 -0500153{
Joe Hershberger7ece1c62015-03-22 17:09:20 -0500154 /*
155 * eth1 is disabled and netretry is yes, so the ping should succeed and
156 * the active device should be eth0
157 */
158 sandbox_eth_disable_response(1, true);
159 setenv("ethact", "eth@10004000");
160 setenv("netretry", "yes");
Joe Hershberger172a31b2015-04-21 13:57:20 -0500161 sandbox_eth_skip_timeout();
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500162 ut_assertok(net_loop(PING));
Joe Hershberger7ece1c62015-03-22 17:09:20 -0500163 ut_asserteq_str("eth@10002000", getenv("ethact"));
164
165 /*
166 * eth1 is disabled and netretry is no, so the ping should fail and the
167 * active device should be eth1
168 */
169 setenv("ethact", "eth@10004000");
170 setenv("netretry", "no");
Joe Hershberger172a31b2015-04-21 13:57:20 -0500171 sandbox_eth_skip_timeout();
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500172 ut_asserteq(-ETIMEDOUT, net_loop(PING));
Joe Hershberger7ece1c62015-03-22 17:09:20 -0500173 ut_asserteq_str("eth@10004000", getenv("ethact"));
174
Joe Hershberger09129be2015-05-20 14:27:33 -0500175 return 0;
176}
177
178static int dm_test_net_retry(struct unit_test_state *uts)
179{
180 int retval;
181
182 net_ping_ip = string_to_ip("1.1.2.2");
183
184 retval = _dm_test_net_retry(uts);
185
Joe Hershberger7ece1c62015-03-22 17:09:20 -0500186 /* Restore the env */
187 setenv("netretry", NULL);
188 sandbox_eth_disable_response(1, false);
189
Joe Hershberger09129be2015-05-20 14:27:33 -0500190 return retval;
Joe Hershberger7ece1c62015-03-22 17:09:20 -0500191}
192DM_TEST(dm_test_net_retry, DM_TESTF_SCAN_FDT);