test: dm: eth: Add testing for ethrotate env var
[platform/kernel/u-boot.git] / test / dm / eth.c
1 /*
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>
12 #include <dm/test.h>
13 #include <dm/ut.h>
14 #include <fdtdec.h>
15 #include <malloc.h>
16 #include <net.h>
17
18 DECLARE_GLOBAL_DATA_PTR;
19
20 static int dm_test_eth(struct dm_test_state *dms)
21 {
22         NetPingIP = string_to_ip("1.1.2.2");
23
24         setenv("ethact", "eth@10002000");
25         ut_assertok(NetLoop(PING));
26         ut_asserteq_str("eth@10002000", getenv("ethact"));
27
28         setenv("ethact", "eth@10003000");
29         ut_assertok(NetLoop(PING));
30         ut_asserteq_str("eth@10003000", getenv("ethact"));
31
32         setenv("ethact", "eth@10004000");
33         ut_assertok(NetLoop(PING));
34         ut_asserteq_str("eth@10004000", getenv("ethact"));
35
36         return 0;
37 }
38 DM_TEST(dm_test_eth, DM_TESTF_SCAN_FDT);
39
40 static int dm_test_eth_alias(struct dm_test_state *dms)
41 {
42         NetPingIP = string_to_ip("1.1.2.2");
43         setenv("ethact", "eth0");
44         ut_assertok(NetLoop(PING));
45         ut_asserteq_str("eth@10002000", getenv("ethact"));
46
47         setenv("ethact", "eth1");
48         ut_assertok(NetLoop(PING));
49         ut_asserteq_str("eth@10004000", getenv("ethact"));
50
51         /* Expected to fail since eth2 is not defined in the device tree */
52         setenv("ethact", "eth2");
53         ut_assertok(NetLoop(PING));
54         ut_asserteq_str("eth@10002000", getenv("ethact"));
55
56         setenv("ethact", "eth5");
57         ut_assertok(NetLoop(PING));
58         ut_asserteq_str("eth@10003000", getenv("ethact"));
59
60         return 0;
61 }
62 DM_TEST(dm_test_eth_alias, DM_TESTF_SCAN_FDT);
63
64 static int dm_test_eth_prime(struct dm_test_state *dms)
65 {
66         NetPingIP = string_to_ip("1.1.2.2");
67
68         /* Expected to be "eth@10003000" because of ethprime variable */
69         setenv("ethact", NULL);
70         setenv("ethprime", "eth5");
71         ut_assertok(NetLoop(PING));
72         ut_asserteq_str("eth@10003000", getenv("ethact"));
73
74         /* Expected to be "eth@10002000" because it is first */
75         setenv("ethact", NULL);
76         setenv("ethprime", NULL);
77         ut_assertok(NetLoop(PING));
78         ut_asserteq_str("eth@10002000", getenv("ethact"));
79
80         return 0;
81 }
82 DM_TEST(dm_test_eth_prime, DM_TESTF_SCAN_FDT);
83
84 static int dm_test_eth_rotate(struct dm_test_state *dms)
85 {
86         char ethaddr[18];
87
88         /* Invalidate eth1's MAC address */
89         NetPingIP = string_to_ip("1.1.2.2");
90         strcpy(ethaddr, getenv("eth1addr"));
91         setenv("eth1addr", NULL);
92
93         /* Make sure that the default is to rotate to the next interface */
94         setenv("ethact", "eth@10004000");
95         ut_assertok(NetLoop(PING));
96         ut_asserteq_str("eth@10002000", getenv("ethact"));
97
98         /* If ethrotate is no, then we should fail on a bad MAC */
99         setenv("ethact", "eth@10004000");
100         setenv("ethrotate", "no");
101         ut_asserteq(-1, NetLoop(PING));
102         ut_asserteq_str("eth@10004000", getenv("ethact"));
103
104         /* Restore the env */
105         setenv("eth1addr", ethaddr);
106         setenv("ethrotate", NULL);
107
108         /* Invalidate eth0's MAC address */
109         strcpy(ethaddr, getenv("ethaddr"));
110         setenv(".flags", "ethaddr");
111         setenv("ethaddr", NULL);
112
113         /* Make sure we can skip invalid devices */
114         setenv("ethact", "eth@10004000");
115         ut_assertok(NetLoop(PING));
116         ut_asserteq_str("eth@10004000", getenv("ethact"));
117
118         /* Restore the env */
119         setenv("ethaddr", ethaddr);
120         setenv(".flags", NULL);
121
122         return 0;
123 }
124 DM_TEST(dm_test_eth_rotate, DM_TESTF_SCAN_FDT);