1 /*
2  * Copyright (C) 2019 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.shared;
18 
19 import static android.net.InetAddresses.parseNumericAddress;
20 import static android.net.shared.ProvisioningConfiguration.fromStableParcelable;
21 
22 import static com.android.testutils.MiscAssertsKt.assertFieldCountEquals;
23 
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotEquals;
26 
27 import android.net.LinkAddress;
28 import android.net.MacAddress;
29 import android.net.Network;
30 import android.net.StaticIpConfiguration;
31 import android.net.apf.ApfCapabilities;
32 import android.net.shared.ProvisioningConfiguration.ScanResultInfo;
33 
34 import androidx.test.filters.SmallTest;
35 import androidx.test.runner.AndroidJUnit4;
36 
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 
41 import java.nio.ByteBuffer;
42 import java.util.Collections;
43 import java.util.function.Consumer;
44 
45 /**
46  * Tests for {@link ProvisioningConfiguration}.
47  */
48 @RunWith(AndroidJUnit4.class)
49 @SmallTest
50 public class ProvisioningConfigurationTest {
51     private ProvisioningConfiguration mConfig;
52 
makeScanResultInfo(final String ssid)53     private ScanResultInfo makeScanResultInfo(final String ssid) {
54         final byte[] payload = new byte[] {
55             (byte) 0x00, (byte) 0x17, (byte) 0xF2, (byte) 0x06, (byte) 0x01,
56             (byte) 0x01, (byte) 0x03, (byte) 0x01, (byte) 0x00, (byte) 0x00,
57         };
58         final ScanResultInfo.InformationElement ie =
59                 new ScanResultInfo.InformationElement(0xdd /* vendor specific IE id */,
60                         ByteBuffer.wrap(payload));
61         return new ScanResultInfo(ssid, "01:02:03:04:05:06" /* bssid string */,
62                 Collections.singletonList(ie));
63     }
64 
65     @Before
setUp()66     public void setUp() {
67         mConfig = new ProvisioningConfiguration();
68         mConfig.mEnableIPv4 = true;
69         mConfig.mEnableIPv6 = true;
70         mConfig.mUsingMultinetworkPolicyTracker = true;
71         mConfig.mUsingIpReachabilityMonitor = true;
72         mConfig.mRequestedPreDhcpActionMs = 42;
73         mConfig.mInitialConfig = new InitialConfiguration();
74         mConfig.mInitialConfig.ipAddresses.add(
75                 new LinkAddress(parseNumericAddress("192.168.42.42"), 24));
76         mConfig.mStaticIpConfig = new StaticIpConfiguration();
77         mConfig.mStaticIpConfig.ipAddress =
78                 new LinkAddress(parseNumericAddress("2001:db8::42"), 90);
79         // Not testing other InitialConfig or StaticIpConfig members: they have their own unit tests
80         mConfig.mApfCapabilities = new ApfCapabilities(1, 2, 3);
81         mConfig.mProvisioningTimeoutMs = 4200;
82         mConfig.mIPv6AddrGenMode = 123;
83         mConfig.mNetwork = new Network(321);
84         mConfig.mDisplayName = "test_config";
85         mConfig.mEnablePreconnection = false;
86         mConfig.mScanResultInfo = makeScanResultInfo("ssid");
87         mConfig.mLayer2Info = new Layer2Information("some l2key", "some cluster",
88                 MacAddress.fromString("00:01:02:03:04:05"));
89         // Any added field must be included in equals() to be tested properly
90         assertFieldCountEquals(15, ProvisioningConfiguration.class);
91     }
92 
93     @Test
testParcelUnparcel()94     public void testParcelUnparcel() {
95         doParcelUnparcelTest();
96     }
97 
98     @Test
testParcelUnparcel_NullInitialConfiguration()99     public void testParcelUnparcel_NullInitialConfiguration() {
100         mConfig.mInitialConfig = null;
101         doParcelUnparcelTest();
102     }
103 
104     @Test
testParcelUnparcel_NullStaticConfiguration()105     public void testParcelUnparcel_NullStaticConfiguration() {
106         mConfig.mStaticIpConfig = null;
107         doParcelUnparcelTest();
108     }
109 
110     @Test
testParcelUnparcel_NullApfCapabilities()111     public void testParcelUnparcel_NullApfCapabilities() {
112         mConfig.mApfCapabilities = null;
113         doParcelUnparcelTest();
114     }
115 
116     @Test
testParcelUnparcel_NullNetwork()117     public void testParcelUnparcel_NullNetwork() {
118         mConfig.mNetwork = null;
119         doParcelUnparcelTest();
120     }
121 
122     @Test
testParcelUnparcel_NullScanResultInfo()123     public void testParcelUnparcel_NullScanResultInfo() {
124         mConfig.mScanResultInfo = null;
125         doParcelUnparcelTest();
126     }
127 
128     @Test
testParcelUnparcel_WithPreDhcpConnection()129     public void testParcelUnparcel_WithPreDhcpConnection() {
130         mConfig.mEnablePreconnection = true;
131         doParcelUnparcelTest();
132     }
133 
doParcelUnparcelTest()134     private void doParcelUnparcelTest() {
135         final ProvisioningConfiguration unparceled =
136                 fromStableParcelable(mConfig.toStableParcelable());
137         assertEquals(mConfig, unparceled);
138     }
139 
140     @Test
testEquals()141     public void testEquals() {
142         assertEquals(mConfig, new ProvisioningConfiguration(mConfig));
143 
144         assertNotEqualsAfterChange(c -> c.mEnableIPv4 = false);
145         assertNotEqualsAfterChange(c -> c.mEnableIPv6 = false);
146         assertNotEqualsAfterChange(c -> c.mUsingMultinetworkPolicyTracker = false);
147         assertNotEqualsAfterChange(c -> c.mUsingIpReachabilityMonitor = false);
148         assertNotEqualsAfterChange(c -> c.mRequestedPreDhcpActionMs++);
149         assertNotEqualsAfterChange(c -> c.mInitialConfig.ipAddresses.add(
150                 new LinkAddress(parseNumericAddress("192.168.47.47"), 16)));
151         assertNotEqualsAfterChange(c -> c.mInitialConfig = null);
152         assertNotEqualsAfterChange(c -> c.mStaticIpConfig.ipAddress =
153                 new LinkAddress(parseNumericAddress("2001:db8::47"), 64));
154         assertNotEqualsAfterChange(c -> c.mStaticIpConfig = null);
155         assertNotEqualsAfterChange(c -> c.mApfCapabilities = new ApfCapabilities(4, 5, 6));
156         assertNotEqualsAfterChange(c -> c.mApfCapabilities = null);
157         assertNotEqualsAfterChange(c -> c.mProvisioningTimeoutMs++);
158         assertNotEqualsAfterChange(c -> c.mIPv6AddrGenMode++);
159         assertNotEqualsAfterChange(c -> c.mNetwork = new Network(123));
160         assertNotEqualsAfterChange(c -> c.mNetwork = null);
161         assertNotEqualsAfterChange(c -> c.mDisplayName = "other_test");
162         assertNotEqualsAfterChange(c -> c.mDisplayName = null);
163         assertNotEqualsAfterChange(c -> c.mEnablePreconnection = true);
164         assertNotEqualsAfterChange(c -> c.mScanResultInfo = null);
165         assertNotEqualsAfterChange(c -> c.mScanResultInfo = makeScanResultInfo("another ssid"));
166         assertNotEqualsAfterChange(c -> c.mLayer2Info = new Layer2Information("another l2key",
167                 "some cluster", MacAddress.fromString("00:01:02:03:04:05")));
168         assertNotEqualsAfterChange(c -> c.mLayer2Info = new Layer2Information("some l2key",
169                 "another cluster", MacAddress.fromString("00:01:02:03:04:05")));
170         assertNotEqualsAfterChange(c -> c.mLayer2Info = new Layer2Information("some l2key",
171                 "some cluster", MacAddress.fromString("01:02:03:04:05:06")));
172         assertNotEqualsAfterChange(c -> c.mLayer2Info = null);
173         assertFieldCountEquals(15, ProvisioningConfiguration.class);
174     }
175 
assertNotEqualsAfterChange(Consumer<ProvisioningConfiguration> mutator)176     private void assertNotEqualsAfterChange(Consumer<ProvisioningConfiguration> mutator) {
177         final ProvisioningConfiguration newConfig = new ProvisioningConfiguration(mConfig);
178         mutator.accept(newConfig);
179         assertNotEquals(mConfig, newConfig);
180     }
181 }
182