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; |
| 22 | |
| 23 | import android.net.MacAddress.MacAddressType; |
| 24 | import android.support.test.filters.SmallTest; |
| 25 | import android.support.test.runner.AndroidJUnit4; |
| 26 | |
| 27 | import java.util.Arrays; |
| 28 | |
| 29 | import org.junit.Test; |
| 30 | import org.junit.runner.RunWith; |
| 31 | |
| 32 | @SmallTest |
| 33 | @RunWith(AndroidJUnit4.class) |
| 34 | public class MacAddressTest { |
| 35 | |
| 36 | static class AddrTypeTestCase { |
| 37 | byte[] addr; |
| 38 | MacAddressType expected; |
| 39 | |
| 40 | static AddrTypeTestCase of(MacAddressType expected, int... addr) { |
| 41 | AddrTypeTestCase t = new AddrTypeTestCase(); |
| 42 | t.expected = expected; |
| 43 | t.addr = toByteArray(addr); |
| 44 | return t; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | @Test |
| 49 | public void testMacAddrTypes() { |
| 50 | AddrTypeTestCase[] testcases = { |
| 51 | AddrTypeTestCase.of(null), |
| 52 | AddrTypeTestCase.of(null, 0), |
| 53 | AddrTypeTestCase.of(null, 1, 2, 3, 4, 5), |
| 54 | AddrTypeTestCase.of(null, 1, 2, 3, 4, 5, 6, 7), |
| 55 | AddrTypeTestCase.of(MacAddressType.UNICAST, 0xa0, 0xb0, 0xc0, 0xd0, 0xe0, 0xf0), |
| 56 | AddrTypeTestCase.of(MacAddressType.BROADCAST, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff), |
| 57 | AddrTypeTestCase.of(MacAddressType.MULTICAST, 1, 2, 3, 4, 5, 6), |
| 58 | AddrTypeTestCase.of(MacAddressType.MULTICAST, 11, 22, 33, 44, 55, 66), |
| 59 | AddrTypeTestCase.of(MacAddressType.MULTICAST, 33, 33, 0xaa, 0xbb, 0xcc, 0xdd) |
| 60 | }; |
| 61 | |
| 62 | for (AddrTypeTestCase t : testcases) { |
| 63 | MacAddressType got = MacAddress.macAddressType(t.addr); |
| 64 | String msg = String.format("expected type of %s to be %s, but got %s", |
| 65 | Arrays.toString(t.addr), t.expected, got); |
| 66 | assertEquals(msg, t.expected, got); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | static byte[] toByteArray(int[] in) { |
| 71 | byte[] out = new byte[in.length]; |
| 72 | for (int i = 0; i < in.length; i++) { |
| 73 | out[i] = (byte) in[i]; |
| 74 | } |
| 75 | return out; |
| 76 | } |
| 77 | } |