summaryrefslogtreecommitdiff
path: root/tests/common/java/android/net/StaticIpConfigurationTest.java
blob: b5f23bf19a3c0fafbb7e848dc3df9a9994b86f71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.net;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import android.os.Parcel;

import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import java.net.InetAddress;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

@RunWith(AndroidJUnit4.class)
@SmallTest
public class StaticIpConfigurationTest {

    private static final String ADDRSTR = "192.0.2.2/25";
    private static final LinkAddress ADDR = new LinkAddress(ADDRSTR);
    private static final InetAddress GATEWAY = IpAddress("192.0.2.1");
    private static final InetAddress OFFLINKGATEWAY = IpAddress("192.0.2.129");
    private static final InetAddress DNS1 = IpAddress("8.8.8.8");
    private static final InetAddress DNS2 = IpAddress("8.8.4.4");
    private static final InetAddress DNS3 = IpAddress("4.2.2.2");
    private static final String IFACE = "eth0";
    private static final String FAKE_DOMAINS = "google.com";

    private static InetAddress IpAddress(String addr) {
        return InetAddress.parseNumericAddress(addr);
    }

    private void checkEmpty(StaticIpConfiguration s) {
        assertNull(s.ipAddress);
        assertNull(s.gateway);
        assertNull(s.domains);
        assertEquals(0, s.dnsServers.size());
    }

    private StaticIpConfiguration makeTestObject() {
        StaticIpConfiguration s = new StaticIpConfiguration();
        s.ipAddress = ADDR;
        s.gateway = GATEWAY;
        s.dnsServers.add(DNS1);
        s.dnsServers.add(DNS2);
        s.dnsServers.add(DNS3);
        s.domains = FAKE_DOMAINS;
        return s;
    }

    @Test
    public void testConstructor() {
        StaticIpConfiguration s = new StaticIpConfiguration();
        checkEmpty(s);
    }

    @Test
    public void testCopyAndClear() {
        StaticIpConfiguration empty = new StaticIpConfiguration((StaticIpConfiguration) null);
        checkEmpty(empty);

        StaticIpConfiguration s1 = makeTestObject();
        StaticIpConfiguration s2 = new StaticIpConfiguration(s1);
        assertEquals(s1, s2);
        s2.clear();
        assertEquals(empty, s2);
    }

    @Test
    public void testHashCodeAndEquals() {
        HashSet<Integer> hashCodes = new HashSet();
        hashCodes.add(0);

        StaticIpConfiguration s = new StaticIpConfiguration();
        // Check that this hash code is nonzero and different from all the ones seen so far.
        assertTrue(hashCodes.add(s.hashCode()));

        s.ipAddress = ADDR;
        assertTrue(hashCodes.add(s.hashCode()));

        s.gateway = GATEWAY;
        assertTrue(hashCodes.add(s.hashCode()));

        s.dnsServers.add(DNS1);
        assertTrue(hashCodes.add(s.hashCode()));

        s.dnsServers.add(DNS2);
        assertTrue(hashCodes.add(s.hashCode()));

        s.dnsServers.add(DNS3);
        assertTrue(hashCodes.add(s.hashCode()));

        s.domains = "example.com";
        assertTrue(hashCodes.add(s.hashCode()));

        assertFalse(s.equals(null));
        assertEquals(s, s);

        StaticIpConfiguration s2 = new StaticIpConfiguration(s);
        assertEquals(s, s2);

        s.ipAddress = new LinkAddress(DNS1, 32);
        assertNotEquals(s, s2);

        s2 = new StaticIpConfiguration(s);
        s.domains = "foo";
        assertNotEquals(s, s2);

        s2 = new StaticIpConfiguration(s);
        s.gateway = DNS2;
        assertNotEquals(s, s2);

        s2 = new StaticIpConfiguration(s);
        s.dnsServers.add(DNS3);
        assertNotEquals(s, s2);
    }

    @Test
    public void testToLinkProperties() {
        LinkProperties expected = new LinkProperties();
        expected.setInterfaceName(IFACE);

        StaticIpConfiguration s = new StaticIpConfiguration();
        assertEquals(expected, s.toLinkProperties(IFACE));

        final RouteInfo connectedRoute = new RouteInfo(new IpPrefix(ADDRSTR), null, IFACE);
        s.ipAddress = ADDR;
        expected.addLinkAddress(ADDR);
        expected.addRoute(connectedRoute);
        assertEquals(expected, s.toLinkProperties(IFACE));

        s.gateway = GATEWAY;
        RouteInfo defaultRoute = new RouteInfo(new IpPrefix("0.0.0.0/0"), GATEWAY, IFACE);
        expected.addRoute(defaultRoute);
        assertEquals(expected, s.toLinkProperties(IFACE));

        s.gateway = OFFLINKGATEWAY;
        expected.removeRoute(defaultRoute);
        defaultRoute = new RouteInfo(new IpPrefix("0.0.0.0/0"), OFFLINKGATEWAY, IFACE);
        expected.addRoute(defaultRoute);

        RouteInfo gatewayRoute = new RouteInfo(new IpPrefix("192.0.2.129/32"), null, IFACE);
        expected.addRoute(gatewayRoute);
        assertEquals(expected, s.toLinkProperties(IFACE));

        s.dnsServers.add(DNS1);
        expected.addDnsServer(DNS1);
        assertEquals(expected, s.toLinkProperties(IFACE));

        s.dnsServers.add(DNS2);
        s.dnsServers.add(DNS3);
        expected.addDnsServer(DNS2);
        expected.addDnsServer(DNS3);
        assertEquals(expected, s.toLinkProperties(IFACE));

        s.domains = FAKE_DOMAINS;
        expected.setDomains(FAKE_DOMAINS);
        assertEquals(expected, s.toLinkProperties(IFACE));

        s.gateway = null;
        expected.removeRoute(defaultRoute);
        expected.removeRoute(gatewayRoute);
        assertEquals(expected, s.toLinkProperties(IFACE));

        // Without knowing the IP address, we don't have a directly-connected route, so we can't
        // tell if the gateway is off-link or not and we don't add a host route. This isn't a real
        // configuration, but we should at least not crash.
        s.gateway = OFFLINKGATEWAY;
        s.ipAddress = null;
        expected.removeLinkAddress(ADDR);
        expected.removeRoute(connectedRoute);
        expected.addRoute(defaultRoute);
        assertEquals(expected, s.toLinkProperties(IFACE));
    }

    private StaticIpConfiguration passThroughParcel(StaticIpConfiguration s) {
        Parcel p = Parcel.obtain();
        StaticIpConfiguration s2 = null;
        try {
            s.writeToParcel(p, 0);
            p.setDataPosition(0);
            s2 = StaticIpConfiguration.readFromParcel(p);
        } finally {
            p.recycle();
        }
        assertNotNull(s2);
        return s2;
    }

    @Test
    public void testParceling() {
        StaticIpConfiguration s = makeTestObject();
        StaticIpConfiguration s2 = passThroughParcel(s);
        assertEquals(s, s2);
    }

    @Test
    public void testBuilder() {
        final ArrayList<InetAddress> dnsServers = new ArrayList<>();
        dnsServers.add(DNS1);

        final StaticIpConfiguration s = new StaticIpConfiguration.Builder()
                .setIpAddress(ADDR)
                .setGateway(GATEWAY)
                .setDomains(FAKE_DOMAINS)
                .setDnsServers(dnsServers)
                .build();

        assertEquals(s.ipAddress, s.getIpAddress());
        assertEquals(ADDR, s.getIpAddress());
        assertEquals(s.gateway, s.getGateway());
        assertEquals(GATEWAY, s.getGateway());
        assertEquals(s.domains, s.getDomains());
        assertEquals(FAKE_DOMAINS, s.getDomains());
        assertTrue(s.dnsServers.equals(s.getDnsServers()));
        assertEquals(1, s.getDnsServers().size());
        assertEquals(DNS1, s.getDnsServers().get(0));
    }

    @Test
    public void testAddDnsServers() {
        final StaticIpConfiguration s = new StaticIpConfiguration((StaticIpConfiguration) null);
        checkEmpty(s);

        s.addDnsServer(DNS1);
        assertEquals(1, s.getDnsServers().size());
        assertEquals(DNS1, s.getDnsServers().get(0));

        s.addDnsServer(DNS2);
        s.addDnsServer(DNS3);
        assertEquals(3, s.getDnsServers().size());
        assertEquals(DNS2, s.getDnsServers().get(1));
        assertEquals(DNS3, s.getDnsServers().get(2));
    }

    @Test
    public void testGetRoutes() {
        final StaticIpConfiguration s = makeTestObject();
        final List<RouteInfo> routeInfoList = s.getRoutes(IFACE);

        assertEquals(2, routeInfoList.size());
        assertEquals(new RouteInfo(ADDR, (InetAddress) null, IFACE), routeInfoList.get(0));
        assertEquals(new RouteInfo((IpPrefix) null, GATEWAY, IFACE), routeInfoList.get(1));
    }
}