Joe Hershberger | bfacad7 | 2015-03-22 17:09:15 -0500 | [diff] [blame] | 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> |
Joe Hershberger | 7ece1c6 | 2015-03-22 17:09:20 -0500 | [diff] [blame] | 17 | #include <asm/eth.h> |
Joe Hershberger | bfacad7 | 2015-03-22 17:09:15 -0500 | [diff] [blame] | 18 | |
| 19 | DECLARE_GLOBAL_DATA_PTR; |
| 20 | |
| 21 | static int dm_test_eth(struct dm_test_state *dms) |
| 22 | { |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame^] | 23 | net_ping_ip = string_to_ip("1.1.2.2"); |
Joe Hershberger | bfacad7 | 2015-03-22 17:09:15 -0500 | [diff] [blame] | 24 | |
| 25 | setenv("ethact", "eth@10002000"); |
| 26 | ut_assertok(NetLoop(PING)); |
| 27 | ut_asserteq_str("eth@10002000", getenv("ethact")); |
| 28 | |
| 29 | setenv("ethact", "eth@10003000"); |
| 30 | ut_assertok(NetLoop(PING)); |
| 31 | ut_asserteq_str("eth@10003000", getenv("ethact")); |
| 32 | |
| 33 | setenv("ethact", "eth@10004000"); |
| 34 | ut_assertok(NetLoop(PING)); |
| 35 | ut_asserteq_str("eth@10004000", getenv("ethact")); |
| 36 | |
| 37 | return 0; |
| 38 | } |
| 39 | DM_TEST(dm_test_eth, DM_TESTF_SCAN_FDT); |
Joe Hershberger | e58780d | 2015-03-22 17:09:16 -0500 | [diff] [blame] | 40 | |
| 41 | static int dm_test_eth_alias(struct dm_test_state *dms) |
| 42 | { |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame^] | 43 | net_ping_ip = string_to_ip("1.1.2.2"); |
Joe Hershberger | e58780d | 2015-03-22 17:09:16 -0500 | [diff] [blame] | 44 | setenv("ethact", "eth0"); |
| 45 | ut_assertok(NetLoop(PING)); |
| 46 | ut_asserteq_str("eth@10002000", getenv("ethact")); |
| 47 | |
| 48 | setenv("ethact", "eth1"); |
| 49 | ut_assertok(NetLoop(PING)); |
| 50 | 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"); |
| 54 | ut_assertok(NetLoop(PING)); |
| 55 | ut_asserteq_str("eth@10002000", getenv("ethact")); |
| 56 | |
| 57 | setenv("ethact", "eth5"); |
| 58 | ut_assertok(NetLoop(PING)); |
| 59 | ut_asserteq_str("eth@10003000", getenv("ethact")); |
| 60 | |
| 61 | return 0; |
| 62 | } |
| 63 | DM_TEST(dm_test_eth_alias, DM_TESTF_SCAN_FDT); |
Joe Hershberger | 6536b9b | 2015-03-22 17:09:17 -0500 | [diff] [blame] | 64 | |
| 65 | static int dm_test_eth_prime(struct dm_test_state *dms) |
| 66 | { |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame^] | 67 | net_ping_ip = string_to_ip("1.1.2.2"); |
Joe Hershberger | 6536b9b | 2015-03-22 17:09:17 -0500 | [diff] [blame] | 68 | |
| 69 | /* Expected to be "eth@10003000" because of ethprime variable */ |
| 70 | setenv("ethact", NULL); |
| 71 | setenv("ethprime", "eth5"); |
| 72 | ut_assertok(NetLoop(PING)); |
| 73 | 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); |
| 78 | ut_assertok(NetLoop(PING)); |
| 79 | ut_asserteq_str("eth@10002000", getenv("ethact")); |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | DM_TEST(dm_test_eth_prime, DM_TESTF_SCAN_FDT); |
Joe Hershberger | 7d104ea | 2015-03-22 17:09:18 -0500 | [diff] [blame] | 84 | |
| 85 | static int dm_test_eth_rotate(struct dm_test_state *dms) |
| 86 | { |
| 87 | char ethaddr[18]; |
| 88 | |
| 89 | /* Invalidate eth1's MAC address */ |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame^] | 90 | net_ping_ip = string_to_ip("1.1.2.2"); |
Joe Hershberger | 7d104ea | 2015-03-22 17:09:18 -0500 | [diff] [blame] | 91 | strcpy(ethaddr, getenv("eth1addr")); |
| 92 | setenv("eth1addr", NULL); |
| 93 | |
| 94 | /* Make sure that the default is to rotate to the next interface */ |
| 95 | setenv("ethact", "eth@10004000"); |
| 96 | ut_assertok(NetLoop(PING)); |
| 97 | ut_asserteq_str("eth@10002000", getenv("ethact")); |
| 98 | |
| 99 | /* If ethrotate is no, then we should fail on a bad MAC */ |
| 100 | setenv("ethact", "eth@10004000"); |
| 101 | setenv("ethrotate", "no"); |
Joe Hershberger | 6030459 | 2015-03-22 17:09:24 -0500 | [diff] [blame] | 102 | ut_asserteq(-EINVAL, NetLoop(PING)); |
Joe Hershberger | 7d104ea | 2015-03-22 17:09:18 -0500 | [diff] [blame] | 103 | ut_asserteq_str("eth@10004000", getenv("ethact")); |
| 104 | |
| 105 | /* Restore the env */ |
| 106 | setenv("eth1addr", ethaddr); |
| 107 | setenv("ethrotate", NULL); |
| 108 | |
| 109 | /* Invalidate eth0's MAC address */ |
| 110 | strcpy(ethaddr, getenv("ethaddr")); |
| 111 | setenv(".flags", "ethaddr"); |
| 112 | setenv("ethaddr", NULL); |
| 113 | |
| 114 | /* Make sure we can skip invalid devices */ |
| 115 | setenv("ethact", "eth@10004000"); |
| 116 | ut_assertok(NetLoop(PING)); |
| 117 | ut_asserteq_str("eth@10004000", getenv("ethact")); |
| 118 | |
| 119 | /* Restore the env */ |
| 120 | setenv("ethaddr", ethaddr); |
| 121 | setenv(".flags", NULL); |
| 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | DM_TEST(dm_test_eth_rotate, DM_TESTF_SCAN_FDT); |
Joe Hershberger | 7ece1c6 | 2015-03-22 17:09:20 -0500 | [diff] [blame] | 126 | |
| 127 | static int dm_test_net_retry(struct dm_test_state *dms) |
| 128 | { |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame^] | 129 | net_ping_ip = string_to_ip("1.1.2.2"); |
Joe Hershberger | 7ece1c6 | 2015-03-22 17:09:20 -0500 | [diff] [blame] | 130 | |
| 131 | /* |
| 132 | * eth1 is disabled and netretry is yes, so the ping should succeed and |
| 133 | * the active device should be eth0 |
| 134 | */ |
| 135 | sandbox_eth_disable_response(1, true); |
| 136 | setenv("ethact", "eth@10004000"); |
| 137 | setenv("netretry", "yes"); |
| 138 | ut_assertok(NetLoop(PING)); |
| 139 | ut_asserteq_str("eth@10002000", getenv("ethact")); |
| 140 | |
| 141 | /* |
| 142 | * eth1 is disabled and netretry is no, so the ping should fail and the |
| 143 | * active device should be eth1 |
| 144 | */ |
| 145 | setenv("ethact", "eth@10004000"); |
| 146 | setenv("netretry", "no"); |
Joe Hershberger | 6030459 | 2015-03-22 17:09:24 -0500 | [diff] [blame] | 147 | ut_asserteq(-ETIMEDOUT, NetLoop(PING)); |
Joe Hershberger | 7ece1c6 | 2015-03-22 17:09:20 -0500 | [diff] [blame] | 148 | ut_asserteq_str("eth@10004000", getenv("ethact")); |
| 149 | |
| 150 | /* Restore the env */ |
| 151 | setenv("netretry", NULL); |
| 152 | sandbox_eth_disable_response(1, false); |
| 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | DM_TEST(dm_test_net_retry, DM_TESTF_SCAN_FDT); |