Aaron Huang | 82948dd | 2020-04-17 17:56:24 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 | |
Chalard Jean | 059356d | 2020-07-31 20:00:30 +0900 | [diff] [blame] | 19 | import static com.android.net.module.util.Inet4AddressUtils.inet4AddressToIntHTL; |
Chalard Jean | 39175f2 | 2020-06-26 00:41:26 +0900 | [diff] [blame] | 20 | import static com.android.testutils.MiscAsserts.assertFieldCountEquals; |
| 21 | import static com.android.testutils.ParcelUtils.parcelingRoundTrip; |
Aaron Huang | 82948dd | 2020-04-17 17:56:24 +0800 | [diff] [blame] | 22 | |
| 23 | import static org.junit.Assert.assertEquals; |
| 24 | import static org.junit.Assert.assertFalse; |
| 25 | import static org.junit.Assert.assertTrue; |
| 26 | |
| 27 | import android.annotation.Nullable; |
| 28 | |
| 29 | import androidx.test.runner.AndroidJUnit4; |
| 30 | |
| 31 | import org.junit.Test; |
| 32 | import org.junit.runner.RunWith; |
| 33 | |
| 34 | import java.net.Inet4Address; |
| 35 | import java.net.InetAddress; |
| 36 | |
| 37 | @RunWith(AndroidJUnit4.class) |
| 38 | public class DhcpInfoTest { |
| 39 | private static final String STR_ADDR1 = "255.255.255.255"; |
| 40 | private static final String STR_ADDR2 = "127.0.0.1"; |
| 41 | private static final String STR_ADDR3 = "192.168.1.1"; |
| 42 | private static final String STR_ADDR4 = "192.168.1.0"; |
| 43 | private static final int LEASE_TIME = 9999; |
| 44 | |
| 45 | private int ipToInteger(String ipString) throws Exception { |
| 46 | return inet4AddressToIntHTL((Inet4Address) InetAddress.getByName(ipString)); |
| 47 | } |
| 48 | |
| 49 | private DhcpInfo createDhcpInfoObject() throws Exception { |
| 50 | final DhcpInfo dhcpInfo = new DhcpInfo(); |
| 51 | dhcpInfo.ipAddress = ipToInteger(STR_ADDR1); |
| 52 | dhcpInfo.gateway = ipToInteger(STR_ADDR2); |
| 53 | dhcpInfo.netmask = ipToInteger(STR_ADDR3); |
| 54 | dhcpInfo.dns1 = ipToInteger(STR_ADDR4); |
| 55 | dhcpInfo.dns2 = ipToInteger(STR_ADDR4); |
| 56 | dhcpInfo.serverAddress = ipToInteger(STR_ADDR2); |
| 57 | dhcpInfo.leaseDuration = LEASE_TIME; |
| 58 | return dhcpInfo; |
| 59 | } |
| 60 | |
| 61 | @Test |
| 62 | public void testConstructor() { |
| 63 | new DhcpInfo(); |
| 64 | } |
| 65 | |
| 66 | @Test |
| 67 | public void testToString() throws Exception { |
| 68 | final String expectedDefault = "ipaddr 0.0.0.0 gateway 0.0.0.0 netmask 0.0.0.0 " |
| 69 | + "dns1 0.0.0.0 dns2 0.0.0.0 DHCP server 0.0.0.0 lease 0 seconds"; |
| 70 | |
| 71 | DhcpInfo dhcpInfo = new DhcpInfo(); |
| 72 | |
| 73 | // Test default string. |
| 74 | assertEquals(expectedDefault, dhcpInfo.toString()); |
| 75 | |
| 76 | dhcpInfo = createDhcpInfoObject(); |
| 77 | |
| 78 | final String expected = "ipaddr " + STR_ADDR1 + " gateway " + STR_ADDR2 + " netmask " |
| 79 | + STR_ADDR3 + " dns1 " + STR_ADDR4 + " dns2 " + STR_ADDR4 + " DHCP server " |
| 80 | + STR_ADDR2 + " lease " + LEASE_TIME + " seconds"; |
| 81 | // Test with new values |
| 82 | assertEquals(expected, dhcpInfo.toString()); |
| 83 | } |
| 84 | |
| 85 | private boolean dhcpInfoEquals(@Nullable DhcpInfo left, @Nullable DhcpInfo right) { |
| 86 | if (left == null && right == null) return true; |
| 87 | |
| 88 | if (left == null || right == null) return false; |
| 89 | |
| 90 | return left.ipAddress == right.ipAddress |
| 91 | && left.gateway == right.gateway |
| 92 | && left.netmask == right.netmask |
| 93 | && left.dns1 == right.dns1 |
| 94 | && left.dns2 == right.dns2 |
| 95 | && left.serverAddress == right.serverAddress |
| 96 | && left.leaseDuration == right.leaseDuration; |
| 97 | } |
| 98 | |
| 99 | @Test |
| 100 | public void testParcelDhcpInfo() throws Exception { |
| 101 | // Cannot use assertParcelSane() here because this requires .equals() to work as |
| 102 | // defined, but DhcpInfo has a different legacy behavior that we cannot change. |
| 103 | final DhcpInfo dhcpInfo = createDhcpInfoObject(); |
| 104 | assertFieldCountEquals(7, DhcpInfo.class); |
| 105 | |
| 106 | final DhcpInfo dhcpInfoRoundTrip = parcelingRoundTrip(dhcpInfo); |
| 107 | assertTrue(dhcpInfoEquals(null, null)); |
| 108 | assertFalse(dhcpInfoEquals(null, dhcpInfoRoundTrip)); |
| 109 | assertFalse(dhcpInfoEquals(dhcpInfo, null)); |
| 110 | assertTrue(dhcpInfoEquals(dhcpInfo, dhcpInfoRoundTrip)); |
| 111 | } |
| 112 | } |