1/*
2 * Copyright (C) 2014 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
19import static org.junit.Assert.assertEquals;
20import static org.junit.Assert.assertFalse;
21import static org.junit.Assert.assertNotNull;
22import static org.junit.Assert.assertNull;
23import static org.junit.Assert.assertTrue;
24
25import android.os.Parcel;
26import android.support.test.filters.SmallTest;
27import android.support.test.runner.AndroidJUnit4;
28
29import java.net.InetAddress;
30import java.util.HashSet;
31import java.util.Objects;
32
33import org.junit.Test;
34import org.junit.runner.RunWith;
35
36@RunWith(AndroidJUnit4.class)
37@SmallTest
38public class StaticIpConfigurationTest {
39
40    private static final String ADDRSTR = "192.0.2.2/25";
41    private static final LinkAddress ADDR = new LinkAddress(ADDRSTR);
42    private static final InetAddress GATEWAY = IpAddress("192.0.2.1");
43    private static final InetAddress OFFLINKGATEWAY = IpAddress("192.0.2.129");
44    private static final InetAddress DNS1 = IpAddress("8.8.8.8");
45    private static final InetAddress DNS2 = IpAddress("8.8.4.4");
46    private static final InetAddress DNS3 = IpAddress("4.2.2.2");
47    private static final String IFACE = "eth0";
48
49    private static InetAddress IpAddress(String addr) {
50        return InetAddress.parseNumericAddress(addr);
51    }
52
53    private void checkEmpty(StaticIpConfiguration s) {
54        assertNull(s.ipAddress);
55        assertNull(s.gateway);
56        assertNull(s.domains);
57        assertEquals(0, s.dnsServers.size());
58    }
59
60    private static <T> void assertNotEquals(T t1, T t2) {
61        assertFalse(Objects.equals(t1, t2));
62    }
63
64    private StaticIpConfiguration makeTestObject() {
65        StaticIpConfiguration s = new StaticIpConfiguration();
66        s.ipAddress = ADDR;
67        s.gateway = GATEWAY;
68        s.dnsServers.add(DNS1);
69        s.dnsServers.add(DNS2);
70        s.dnsServers.add(DNS3);
71        s.domains = "google.com";
72        return s;
73    }
74
75    @Test
76    public void testConstructor() {
77        StaticIpConfiguration s = new StaticIpConfiguration();
78        checkEmpty(s);
79    }
80
81    @Test
82    public void testCopyAndClear() {
83        StaticIpConfiguration empty = new StaticIpConfiguration((StaticIpConfiguration) null);
84        checkEmpty(empty);
85
86        StaticIpConfiguration s1 = makeTestObject();
87        StaticIpConfiguration s2 = new StaticIpConfiguration(s1);
88        assertEquals(s1, s2);
89        s2.clear();
90        assertEquals(empty, s2);
91    }
92
93    @Test
94    public void testHashCodeAndEquals() {
95        HashSet<Integer> hashCodes = new HashSet();
96        hashCodes.add(0);
97
98        StaticIpConfiguration s = new StaticIpConfiguration();
99        // Check that this hash code is nonzero and different from all the ones seen so far.
100        assertTrue(hashCodes.add(s.hashCode()));
101
102        s.ipAddress = ADDR;
103        assertTrue(hashCodes.add(s.hashCode()));
104
105        s.gateway = GATEWAY;
106        assertTrue(hashCodes.add(s.hashCode()));
107
108        s.dnsServers.add(DNS1);
109        assertTrue(hashCodes.add(s.hashCode()));
110
111        s.dnsServers.add(DNS2);
112        assertTrue(hashCodes.add(s.hashCode()));
113
114        s.dnsServers.add(DNS3);
115        assertTrue(hashCodes.add(s.hashCode()));
116
117        s.domains = "example.com";
118        assertTrue(hashCodes.add(s.hashCode()));
119
120        assertFalse(s.equals(null));
121        assertEquals(s, s);
122
123        StaticIpConfiguration s2 = new StaticIpConfiguration(s);
124        assertEquals(s, s2);
125
126        s.ipAddress = new LinkAddress(DNS1, 32);
127        assertNotEquals(s, s2);
128
129        s2 = new StaticIpConfiguration(s);
130        s.domains = "foo";
131        assertNotEquals(s, s2);
132
133        s2 = new StaticIpConfiguration(s);
134        s.gateway = DNS2;
135        assertNotEquals(s, s2);
136
137        s2 = new StaticIpConfiguration(s);
138        s.dnsServers.add(DNS3);
139        assertNotEquals(s, s2);
140    }
141
142    @Test
143    public void testToLinkProperties() {
144        LinkProperties expected = new LinkProperties();
145        expected.setInterfaceName(IFACE);
146
147        StaticIpConfiguration s = new StaticIpConfiguration();
148        assertEquals(expected, s.toLinkProperties(IFACE));
149
150        final RouteInfo connectedRoute = new RouteInfo(new IpPrefix(ADDRSTR), null, IFACE);
151        s.ipAddress = ADDR;
152        expected.addLinkAddress(ADDR);
153        expected.addRoute(connectedRoute);
154        assertEquals(expected, s.toLinkProperties(IFACE));
155
156        s.gateway = GATEWAY;
157        RouteInfo defaultRoute = new RouteInfo(new IpPrefix("0.0.0.0/0"), GATEWAY, IFACE);
158        expected.addRoute(defaultRoute);
159        assertEquals(expected, s.toLinkProperties(IFACE));
160
161        s.gateway = OFFLINKGATEWAY;
162        expected.removeRoute(defaultRoute);
163        defaultRoute = new RouteInfo(new IpPrefix("0.0.0.0/0"), OFFLINKGATEWAY, IFACE);
164        expected.addRoute(defaultRoute);
165
166        RouteInfo gatewayRoute = new RouteInfo(new IpPrefix("192.0.2.129/32"), null, IFACE);
167        expected.addRoute(gatewayRoute);
168        assertEquals(expected, s.toLinkProperties(IFACE));
169
170        s.dnsServers.add(DNS1);
171        expected.addDnsServer(DNS1);
172        assertEquals(expected, s.toLinkProperties(IFACE));
173
174        s.dnsServers.add(DNS2);
175        s.dnsServers.add(DNS3);
176        expected.addDnsServer(DNS2);
177        expected.addDnsServer(DNS3);
178        assertEquals(expected, s.toLinkProperties(IFACE));
179
180        s.domains = "google.com";
181        expected.setDomains("google.com");
182        assertEquals(expected, s.toLinkProperties(IFACE));
183
184        s.gateway = null;
185        expected.removeRoute(defaultRoute);
186        expected.removeRoute(gatewayRoute);
187        assertEquals(expected, s.toLinkProperties(IFACE));
188
189        // Without knowing the IP address, we don't have a directly-connected route, so we can't
190        // tell if the gateway is off-link or not and we don't add a host route. This isn't a real
191        // configuration, but we should at least not crash.
192        s.gateway = OFFLINKGATEWAY;
193        s.ipAddress = null;
194        expected.removeLinkAddress(ADDR);
195        expected.removeRoute(connectedRoute);
196        expected.addRoute(defaultRoute);
197        assertEquals(expected, s.toLinkProperties(IFACE));
198    }
199
200    private StaticIpConfiguration passThroughParcel(StaticIpConfiguration s) {
201        Parcel p = Parcel.obtain();
202        StaticIpConfiguration s2 = null;
203        try {
204            s.writeToParcel(p, 0);
205            p.setDataPosition(0);
206            s2 = StaticIpConfiguration.CREATOR.createFromParcel(p);
207        } finally {
208            p.recycle();
209        }
210        assertNotNull(s2);
211        return s2;
212    }
213
214    @Test
215    public void testParceling() {
216        StaticIpConfiguration s = makeTestObject();
217        StaticIpConfiguration s2 = passThroughParcel(s);
218        assertEquals(s, s2);
219    }
220}
221