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.fromStableParcelable;
21 import static android.net.dhcp.DhcpResultsParcelableUtil.toStableParcelable;
22 
23 import static com.android.testutils.MiscAsserts.assertFieldCountEquals;
24 
25 import static org.junit.Assert.assertEquals;
26 
27 import android.net.DhcpResults;
28 import android.net.LinkAddress;
29 import android.net.shared.IpConfigurationParcelableUtil;
30 
31 import androidx.test.filters.SmallTest;
32 import androidx.test.runner.AndroidJUnit4;
33 
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 
38 import java.net.Inet4Address;
39 
40 /**
41  * Tests for {@link IpConfigurationParcelableUtil}.
42  */
43 @RunWith(AndroidJUnit4.class)
44 @SmallTest
45 public class DhcpResultsParcelableUtilTest {
46     private DhcpResults mDhcpResults;
47 
48     @Before
setUp()49     public void setUp() {
50         mDhcpResults = new DhcpResults();
51         mDhcpResults.ipAddress = new LinkAddress(parseNumericAddress("192.168.42.19"), 25);
52         mDhcpResults.gateway = parseNumericAddress("192.168.42.42");
53         mDhcpResults.dnsServers.add(parseNumericAddress("8.8.8.8"));
54         mDhcpResults.dnsServers.add(parseNumericAddress("192.168.43.43"));
55         mDhcpResults.domains = "example.com";
56         mDhcpResults.serverAddress = (Inet4Address) parseNumericAddress("192.168.44.44");
57         mDhcpResults.vendorInfo = "TEST_VENDOR_INFO";
58         mDhcpResults.leaseDuration = 3600;
59         mDhcpResults.serverHostName = "dhcp.example.com";
60         mDhcpResults.mtu = 1450;
61         mDhcpResults.captivePortalApiUrl = "https://example.com/testapi";
62         // Any added DhcpResults field must be included in equals() to be tested properly
63         assertFieldCountEquals(10, DhcpResults.class);
64     }
65 
66     @Test
testParcelUnparcelDhcpResults()67     public void testParcelUnparcelDhcpResults() {
68         doDhcpResultsParcelUnparcelTest();
69     }
70 
71     @Test
testParcelUnparcelDhcpResults_NullIpAddress()72     public void testParcelUnparcelDhcpResults_NullIpAddress() {
73         mDhcpResults.ipAddress = null;
74         doDhcpResultsParcelUnparcelTest();
75     }
76 
77     @Test
testParcelUnparcelDhcpResults_NullGateway()78     public void testParcelUnparcelDhcpResults_NullGateway() {
79         mDhcpResults.gateway = null;
80         doDhcpResultsParcelUnparcelTest();
81     }
82 
83     @Test
testParcelUnparcelDhcpResults_NullDomains()84     public void testParcelUnparcelDhcpResults_NullDomains() {
85         mDhcpResults.domains = null;
86         doDhcpResultsParcelUnparcelTest();
87     }
88 
89     @Test
testParcelUnparcelDhcpResults_EmptyDomains()90     public void testParcelUnparcelDhcpResults_EmptyDomains() {
91         mDhcpResults.domains = "";
92         doDhcpResultsParcelUnparcelTest();
93     }
94 
95     @Test
testParcelUnparcelDhcpResults_NullServerAddress()96     public void testParcelUnparcelDhcpResults_NullServerAddress() {
97         mDhcpResults.serverAddress = null;
98         doDhcpResultsParcelUnparcelTest();
99     }
100 
101     @Test
testParcelUnparcelDhcpResults_NullVendorInfo()102     public void testParcelUnparcelDhcpResults_NullVendorInfo() {
103         mDhcpResults.vendorInfo = null;
104         doDhcpResultsParcelUnparcelTest();
105     }
106 
107     @Test
testParcelUnparcelDhcpResults_NullServerHostName()108     public void testParcelUnparcelDhcpResults_NullServerHostName() {
109         mDhcpResults.serverHostName = null;
110         doDhcpResultsParcelUnparcelTest();
111     }
112 
113     @Test
testParcelUnparcelDhcpResults_NullCaptivePortalApiUrl()114     public void testParcelUnparcelDhcpResults_NullCaptivePortalApiUrl() {
115         mDhcpResults.captivePortalApiUrl = null;
116         doDhcpResultsParcelUnparcelTest();
117     }
118 
doDhcpResultsParcelUnparcelTest()119     private void doDhcpResultsParcelUnparcelTest() {
120         final DhcpResults unparceled = fromStableParcelable(toStableParcelable(mDhcpResults));
121         assertEquals(mDhcpResults, unparceled);
122     }
123 
124     @Test
testToString()125     public void testToString() {
126         final String expected = ""
127                 + "android.net.DhcpResultsParcelable{baseConfiguration: IP address 192.168.42.19/25"
128                 + " Gateway 192.168.42.42  DNS servers: [ 8.8.8.8 192.168.43.43 ]"
129                 + " Domains example.com, leaseDuration: 3600, mtu: 1450,"
130                 + " serverAddress: 192.168.44.44, vendorInfo: TEST_VENDOR_INFO,"
131                 + " serverHostName: dhcp.example.com,"
132                 + " captivePortalApiUrl: https://example.com/testapi}";
133         assertEquals(expected, toStableParcelable(mDhcpResults).toString());
134     }
135 }