Hugo Benichi | 1881890 | 2017-10-12 21:33:40 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.net; |
| 18 | |
| 19 | import static org.junit.Assert.assertEquals; |
| 20 | import static org.junit.Assert.assertTrue; |
| 21 | import static org.junit.Assert.assertFalse; |
Hugo Benichi | 87c1532 | 2017-11-09 00:22:25 +0900 | [diff] [blame^] | 22 | import static org.junit.Assert.fail; |
Hugo Benichi | 1881890 | 2017-10-12 21:33:40 +0900 | [diff] [blame] | 23 | |
| 24 | import android.net.MacAddress.MacAddressType; |
| 25 | import android.support.test.filters.SmallTest; |
| 26 | import android.support.test.runner.AndroidJUnit4; |
| 27 | |
| 28 | import java.util.Arrays; |
Hugo Benichi | 87c1532 | 2017-11-09 00:22:25 +0900 | [diff] [blame^] | 29 | import java.util.Random; |
Hugo Benichi | 1881890 | 2017-10-12 21:33:40 +0900 | [diff] [blame] | 30 | |
| 31 | import org.junit.Test; |
| 32 | import org.junit.runner.RunWith; |
| 33 | |
| 34 | @SmallTest |
| 35 | @RunWith(AndroidJUnit4.class) |
| 36 | public class MacAddressTest { |
| 37 | |
| 38 | static class AddrTypeTestCase { |
| 39 | byte[] addr; |
| 40 | MacAddressType expected; |
| 41 | |
| 42 | static AddrTypeTestCase of(MacAddressType expected, int... addr) { |
| 43 | AddrTypeTestCase t = new AddrTypeTestCase(); |
| 44 | t.expected = expected; |
| 45 | t.addr = toByteArray(addr); |
| 46 | return t; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | @Test |
| 51 | public void testMacAddrTypes() { |
| 52 | AddrTypeTestCase[] testcases = { |
| 53 | AddrTypeTestCase.of(null), |
| 54 | AddrTypeTestCase.of(null, 0), |
| 55 | AddrTypeTestCase.of(null, 1, 2, 3, 4, 5), |
| 56 | AddrTypeTestCase.of(null, 1, 2, 3, 4, 5, 6, 7), |
| 57 | AddrTypeTestCase.of(MacAddressType.UNICAST, 0xa0, 0xb0, 0xc0, 0xd0, 0xe0, 0xf0), |
| 58 | AddrTypeTestCase.of(MacAddressType.BROADCAST, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff), |
| 59 | AddrTypeTestCase.of(MacAddressType.MULTICAST, 1, 2, 3, 4, 5, 6), |
| 60 | AddrTypeTestCase.of(MacAddressType.MULTICAST, 11, 22, 33, 44, 55, 66), |
| 61 | AddrTypeTestCase.of(MacAddressType.MULTICAST, 33, 33, 0xaa, 0xbb, 0xcc, 0xdd) |
| 62 | }; |
| 63 | |
| 64 | for (AddrTypeTestCase t : testcases) { |
| 65 | MacAddressType got = MacAddress.macAddressType(t.addr); |
| 66 | String msg = String.format("expected type of %s to be %s, but got %s", |
| 67 | Arrays.toString(t.addr), t.expected, got); |
| 68 | assertEquals(msg, t.expected, got); |
Hugo Benichi | 87c1532 | 2017-11-09 00:22:25 +0900 | [diff] [blame^] | 69 | |
| 70 | if (got != null) { |
| 71 | assertEquals(got, new MacAddress(t.addr).addressType()); |
| 72 | } |
Hugo Benichi | 1881890 | 2017-10-12 21:33:40 +0900 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
Hugo Benichi | 87c1532 | 2017-11-09 00:22:25 +0900 | [diff] [blame^] | 76 | @Test |
| 77 | public void testIsMulticastAddress() { |
| 78 | MacAddress[] multicastAddresses = { |
| 79 | MacAddress.BROADCAST_ADDRESS, |
| 80 | new MacAddress("07:00:d3:56:8a:c4"), |
| 81 | new MacAddress("33:33:aa:bb:cc:dd"), |
| 82 | }; |
| 83 | MacAddress[] unicastAddresses = { |
| 84 | MacAddress.ALL_ZEROS_ADDRESS, |
| 85 | new MacAddress("00:01:44:55:66:77"), |
| 86 | new MacAddress("08:00:22:33:44:55"), |
| 87 | new MacAddress("06:00:00:00:00:00"), |
| 88 | }; |
| 89 | |
| 90 | for (MacAddress mac : multicastAddresses) { |
| 91 | String msg = mac.toString() + " expected to be a multicast address"; |
| 92 | assertTrue(msg, mac.isMulticastAddress()); |
| 93 | } |
| 94 | for (MacAddress mac : unicastAddresses) { |
| 95 | String msg = mac.toString() + " expected not to be a multicast address"; |
| 96 | assertFalse(msg, mac.isMulticastAddress()); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | @Test |
| 101 | public void testIsLocallyAssignedAddress() { |
| 102 | MacAddress[] localAddresses = { |
| 103 | new MacAddress("06:00:00:00:00:00"), |
| 104 | new MacAddress("07:00:d3:56:8a:c4"), |
| 105 | new MacAddress("33:33:aa:bb:cc:dd"), |
| 106 | }; |
| 107 | MacAddress[] universalAddresses = { |
| 108 | new MacAddress("00:01:44:55:66:77"), |
| 109 | new MacAddress("08:00:22:33:44:55"), |
| 110 | }; |
| 111 | |
| 112 | for (MacAddress mac : localAddresses) { |
| 113 | String msg = mac.toString() + " expected to be a locally assigned address"; |
| 114 | assertTrue(msg, mac.isLocallyAssigned()); |
| 115 | } |
| 116 | for (MacAddress mac : universalAddresses) { |
| 117 | String msg = mac.toString() + " expected not to be globally unique address"; |
| 118 | assertFalse(msg, mac.isLocallyAssigned()); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | @Test |
| 123 | public void testMacAddressConversions() { |
| 124 | final int iterations = 10000; |
| 125 | for (int i = 0; i < iterations; i++) { |
| 126 | MacAddress mac = MacAddress.getRandomAddress(); |
| 127 | |
| 128 | String stringRepr = mac.toString(); |
| 129 | byte[] bytesRepr = mac.toByteArray(); |
| 130 | |
| 131 | assertEquals(mac, new MacAddress(stringRepr)); |
| 132 | assertEquals(mac, new MacAddress(bytesRepr)); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | @Test |
| 137 | public void testMacAddressRandomGeneration() { |
| 138 | final int iterations = 1000; |
| 139 | final String expectedAndroidOui = "da:a1:19"; |
| 140 | for (int i = 0; i < iterations; i++) { |
| 141 | MacAddress mac = MacAddress.getRandomAddress(); |
| 142 | String stringRepr = mac.toString(); |
| 143 | |
| 144 | assertTrue(stringRepr + " expected to be a locally assigned address", |
| 145 | mac.isLocallyAssigned()); |
| 146 | assertTrue(stringRepr + " expected to begin with " + expectedAndroidOui, |
| 147 | stringRepr.startsWith(expectedAndroidOui)); |
| 148 | } |
| 149 | |
| 150 | final Random r = new Random(); |
| 151 | final String anotherOui = "24:5f:78"; |
| 152 | final String expectedLocalOui = "26:5f:78"; |
| 153 | final MacAddress base = new MacAddress(anotherOui + ":0:0:0"); |
| 154 | for (int i = 0; i < iterations; i++) { |
| 155 | MacAddress mac = MacAddress.getRandomAddress(base, r); |
| 156 | String stringRepr = mac.toString(); |
| 157 | |
| 158 | assertTrue(stringRepr + " expected to be a locally assigned address", |
| 159 | mac.isLocallyAssigned()); |
| 160 | assertTrue(stringRepr + " expected to begin with " + expectedLocalOui, |
| 161 | stringRepr.startsWith(expectedLocalOui)); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | @Test |
| 166 | public void testConstructorInputValidation() { |
| 167 | String[] invalidStringAddresses = { |
| 168 | null, |
| 169 | "", |
| 170 | "abcd", |
| 171 | "1:2:3:4:5", |
| 172 | "1:2:3:4:5:6:7", |
| 173 | "10000:2:3:4:5:6", |
| 174 | }; |
| 175 | |
| 176 | for (String s : invalidStringAddresses) { |
| 177 | try { |
| 178 | MacAddress mac = new MacAddress(s); |
| 179 | fail("new MacAddress(" + s + ") should have failed, but returned " + mac); |
| 180 | } catch (IllegalArgumentException excepted) { |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | byte[][] invalidBytesAddresses = { |
| 185 | null, |
| 186 | {}, |
| 187 | {1,2,3,4,5}, |
| 188 | {1,2,3,4,5,6,7}, |
| 189 | }; |
| 190 | |
| 191 | for (byte[] b : invalidBytesAddresses) { |
| 192 | try { |
| 193 | MacAddress mac = new MacAddress(b); |
| 194 | fail("new MacAddress(" + Arrays.toString(b) |
| 195 | + ") should have failed, but returned " + mac); |
| 196 | } catch (IllegalArgumentException excepted) { |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | static byte[] toByteArray(int... in) { |
Hugo Benichi | 1881890 | 2017-10-12 21:33:40 +0900 | [diff] [blame] | 202 | byte[] out = new byte[in.length]; |
| 203 | for (int i = 0; i < in.length; i++) { |
| 204 | out[i] = (byte) in[i]; |
| 205 | } |
| 206 | return out; |
| 207 | } |
| 208 | } |