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