1 /* 2 * Copyright (C) 2020 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.dhcp; 18 19 import static android.net.InetAddresses.parseNumericAddress; 20 import static android.net.dhcp.DhcpResultsParcelableUtil.toStableParcelable; 21 import static android.net.shared.IpConfigurationParcelableUtil.unparcelAddress; 22 23 import static com.android.testutils.MiscAsserts.assertFieldCountEquals; 24 25 import static org.junit.Assert.assertEquals; 26 import static org.junit.Assert.assertTrue; 27 28 import android.net.DhcpResults; 29 import android.net.DhcpResultsParcelable; 30 import android.net.LinkAddress; 31 import android.net.shared.IpConfigurationParcelableUtil; 32 33 import androidx.annotation.NonNull; 34 import androidx.annotation.Nullable; 35 import androidx.test.filters.SmallTest; 36 import androidx.test.runner.AndroidJUnit4; 37 38 import org.junit.Before; 39 import org.junit.Test; 40 import org.junit.runner.RunWith; 41 42 import java.net.Inet4Address; 43 import java.util.Arrays; 44 45 /** 46 * Tests for {@link IpConfigurationParcelableUtil}. 47 */ 48 @RunWith(AndroidJUnit4.class) 49 @SmallTest 50 public class DhcpResultsParcelableUtilTest { 51 private DhcpResults mDhcpResults; 52 53 @Before setUp()54 public void setUp() { 55 mDhcpResults = new DhcpResults(); 56 mDhcpResults.ipAddress = new LinkAddress(parseNumericAddress("192.168.42.19"), 25); 57 mDhcpResults.gateway = parseNumericAddress("192.168.42.42"); 58 mDhcpResults.dnsServers.add(parseNumericAddress("8.8.8.8")); 59 mDhcpResults.dnsServers.add(parseNumericAddress("192.168.43.43")); 60 mDhcpResults.domains = "example.com"; 61 mDhcpResults.serverAddress = (Inet4Address) parseNumericAddress("192.168.44.44"); 62 mDhcpResults.vendorInfo = "TEST_VENDOR_INFO"; 63 mDhcpResults.leaseDuration = 3600; 64 mDhcpResults.serverHostName = "dhcp.example.com"; 65 mDhcpResults.mtu = 1450; 66 mDhcpResults.captivePortalApiUrl = "https://example.com/testapi"; 67 mDhcpResults.dmnsrchList.addAll(Arrays.asList("google.com", "example.com")); 68 // Any added DhcpResults field must be included in equals() to be tested properly 69 assertFieldCountEquals(11, DhcpResults.class); 70 } 71 72 @Test testParcelDhcpResults()73 public void testParcelDhcpResults() { 74 doDhcpResultsParcelTest(); 75 } 76 77 @Test testParcelUnparcelDhcpResults_NullIpAddress()78 public void testParcelUnparcelDhcpResults_NullIpAddress() { 79 mDhcpResults.ipAddress = null; 80 doDhcpResultsParcelTest(); 81 } 82 83 @Test testParcelUnparcelDhcpResults_NullGateway()84 public void testParcelUnparcelDhcpResults_NullGateway() { 85 mDhcpResults.gateway = null; 86 doDhcpResultsParcelTest(); 87 } 88 89 @Test testParcelUnparcelDhcpResults_NullDomains()90 public void testParcelUnparcelDhcpResults_NullDomains() { 91 mDhcpResults.domains = null; 92 doDhcpResultsParcelTest(); 93 } 94 95 @Test testParcelUnparcelDhcpResults_EmptyDomains()96 public void testParcelUnparcelDhcpResults_EmptyDomains() { 97 mDhcpResults.domains = ""; 98 doDhcpResultsParcelTest(); 99 } 100 101 @Test testParcelUnparcelDhcpResults_NullServerAddress()102 public void testParcelUnparcelDhcpResults_NullServerAddress() { 103 mDhcpResults.serverAddress = null; 104 doDhcpResultsParcelTest(); 105 } 106 107 @Test testParcelUnparcelDhcpResults_NullVendorInfo()108 public void testParcelUnparcelDhcpResults_NullVendorInfo() { 109 mDhcpResults.vendorInfo = null; 110 doDhcpResultsParcelTest(); 111 } 112 113 @Test testParcelUnparcelDhcpResults_NullServerHostName()114 public void testParcelUnparcelDhcpResults_NullServerHostName() { 115 mDhcpResults.serverHostName = null; 116 doDhcpResultsParcelTest(); 117 } 118 119 @Test testParcelUnparcelDhcpResults_NullCaptivePortalApiUrl()120 public void testParcelUnparcelDhcpResults_NullCaptivePortalApiUrl() { 121 mDhcpResults.captivePortalApiUrl = null; 122 doDhcpResultsParcelTest(); 123 } 124 doDhcpResultsParcelTest()125 private void doDhcpResultsParcelTest() { 126 final DhcpResults unparceled = fromStableParcelable(toStableParcelable(mDhcpResults)); 127 setFieldsLostWhileParceling(unparceled); 128 assertEquals(mDhcpResults, unparceled); 129 } 130 setFieldsLostWhileParceling(@onNull DhcpResults unparceledResults)131 private void setFieldsLostWhileParceling(@NonNull DhcpResults unparceledResults) { 132 // TODO: add other fields that are not part of DhcpResultsParcelable here 133 // e.g. if the dmnsrchList field is added, 134 unparceledResults.dmnsrchList.clear(); 135 unparceledResults.dnsServers.clear(); 136 unparceledResults.dmnsrchList.addAll(mDhcpResults.dmnsrchList); 137 unparceledResults.domains = mDhcpResults.domains; 138 unparceledResults.dnsServers.addAll(mDhcpResults.dnsServers); 139 unparceledResults.gateway = mDhcpResults.gateway; 140 unparceledResults.ipAddress = mDhcpResults.ipAddress; 141 } 142 143 /** 144 * Convert a DhcpResultsParcelable to DhcpResults. 145 */ fromStableParcelable(@ullable DhcpResultsParcelable p)146 private static DhcpResults fromStableParcelable(@Nullable DhcpResultsParcelable p) { 147 if (p == null) return null; 148 final DhcpResults results = new DhcpResults(p.baseConfiguration); 149 results.leaseDuration = p.leaseDuration; 150 results.mtu = p.mtu; 151 results.serverAddress = (Inet4Address) unparcelAddress(p.serverAddress); 152 results.vendorInfo = p.vendorInfo; 153 results.serverHostName = p.serverHostName; 154 results.captivePortalApiUrl = p.captivePortalApiUrl; 155 // DhcpResultsParcelable is only used to fill the legacy DhcpInfo class in Wifi, so it 156 // should not be extended with any new field. Some fields maybe part of DhcpResults, but 157 // not DhcpResultsParcelable, as DhcpResults is used internally in NetworkStack, but 158 // DhcpResultsParcelable is used to provide info to wifi (for building DhcpInfo) 159 160 return results; 161 } 162 163 @Test testToString()164 public void testToString() { 165 final String str = toStableParcelable(mDhcpResults).toString(); 166 167 // check a few fields. Comprehensive toString tests exist in aidl_integration_test, 168 // but we want to make sure that the toString function requested in the AIDL file 169 // is there 170 assertTrue(str, str.contains("baseConfiguration")); 171 assertTrue(str, str.contains("IP address 192.168.42.19/25")); 172 assertTrue(str, str.contains("serverAddress")); 173 assertTrue(str, str.contains("192.168.44.44")); 174 } 175 } 176