| /* |
| * Copyright (C) 2017 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.wifi.hotspot2.pps; |
| |
| import static org.junit.Assert.assertFalse; |
| import static org.junit.Assert.assertTrue; |
| |
| import android.os.Parcel; |
| import android.test.suitebuilder.annotation.SmallTest; |
| import android.util.Base64; |
| |
| import org.junit.Test; |
| |
| import java.nio.charset.StandardCharsets; |
| import java.util.ArrayList; |
| import java.util.Arrays; |
| import java.util.Collections; |
| import java.util.HashMap; |
| import java.util.Map; |
| |
| /** |
| * Unit tests for {@link android.net.wifi.hotspot2.pps.Policy}. |
| */ |
| @SmallTest |
| public class PolicyTest { |
| private static final int MAX_NUMBER_OF_EXCLUDED_SSIDS = 128; |
| private static final int MAX_SSID_BYTES = 32; |
| private static final int MAX_PORT_STRING_BYTES = 64; |
| |
| /** |
| * Helper function for creating a {@link Policy} for testing. |
| * |
| * @return {@link Policy} |
| */ |
| private static Policy createPolicy() { |
| Policy policy = new Policy(); |
| policy.minHomeDownlinkBandwidth = 123; |
| policy.minHomeUplinkBandwidth = 345; |
| policy.minRoamingDownlinkBandwidth = 567; |
| policy.minRoamingUplinkBandwidth = 789; |
| policy.excludedSsidList = new String[] {"ssid1", "ssid2"}; |
| policy.requiredProtoPortMap = new HashMap<>(); |
| policy.requiredProtoPortMap.put(12, "23,342,123"); |
| policy.requiredProtoPortMap.put(23, "789,372,1235"); |
| policy.maximumBssLoadValue = 12; |
| |
| policy.preferredRoamingPartnerList = new ArrayList<>(); |
| Policy.RoamingPartner partner1 = new Policy.RoamingPartner(); |
| partner1.fqdn = "partner1.com"; |
| partner1.fqdnExactMatch = true; |
| partner1.priority = 12; |
| partner1.countries = "us,jp"; |
| Policy.RoamingPartner partner2 = new Policy.RoamingPartner(); |
| partner2.fqdn = "partner2.com"; |
| partner2.fqdnExactMatch = false; |
| partner2.priority = 42; |
| partner2.countries = "ca,fr"; |
| policy.preferredRoamingPartnerList.add(partner1); |
| policy.preferredRoamingPartnerList.add(partner2); |
| |
| policy.policyUpdate = new UpdateParameter(); |
| policy.policyUpdate.updateIntervalInMinutes = 1712; |
| policy.policyUpdate.updateMethod = UpdateParameter.UPDATE_METHOD_OMADM; |
| policy.policyUpdate.restriction = UpdateParameter.UPDATE_RESTRICTION_HOMESP; |
| policy.policyUpdate.serverUri = "policy.update.com"; |
| policy.policyUpdate.username = "username"; |
| policy.policyUpdate.base64EncodedPassword = |
| Base64.encodeToString("password".getBytes(), Base64.DEFAULT); |
| policy.policyUpdate.trustRootCertUrl = "trust.cert.com"; |
| policy.policyUpdate.trustRootCertSha256Fingerprint = new byte[32]; |
| |
| return policy; |
| } |
| |
| /** |
| * Helper function for verifying Policy after parcel write then read. |
| * @param policyToWrite |
| * @throws Exception |
| */ |
| private static void verifyParcel(Policy policyToWrite) throws Exception { |
| Parcel parcel = Parcel.obtain(); |
| policyToWrite.writeToParcel(parcel, 0); |
| |
| parcel.setDataPosition(0); // Rewind data position back to the beginning for read. |
| Policy policyFromRead = Policy.CREATOR.createFromParcel(parcel); |
| assertTrue(policyFromRead.equals(policyToWrite)); |
| } |
| |
| /** |
| * Verify parcel read/write for an empty Policy. |
| * |
| * @throws Exception |
| */ |
| @Test |
| public void verifyParcelWithEmptyPolicy() throws Exception { |
| verifyParcel(new Policy()); |
| } |
| |
| /** |
| * Verify parcel read/write for a Policy with all fields set. |
| * |
| * @throws Exception |
| */ |
| @Test |
| public void verifyParcelWithFullPolicy() throws Exception { |
| verifyParcel(createPolicy()); |
| } |
| |
| /** |
| * Verify parcel read/write for a Policy without protocol port map. |
| * |
| * @throws Exception |
| */ |
| @Test |
| public void verifyParcelWithoutProtoPortMap() throws Exception { |
| Policy policy = createPolicy(); |
| policy.requiredProtoPortMap = null; |
| verifyParcel(policy); |
| } |
| |
| /** |
| * Verify parcel read/write for a Policy without preferred roaming partner list. |
| * |
| * @throws Exception |
| */ |
| @Test |
| public void verifyParcelWithoutPreferredRoamingPartnerList() throws Exception { |
| Policy policy = createPolicy(); |
| policy.preferredRoamingPartnerList = null; |
| verifyParcel(policy); |
| } |
| |
| /** |
| * Verify parcel read/write for a Policy without policy update parameters. |
| * |
| * @throws Exception |
| */ |
| @Test |
| public void verifyParcelWithoutPolicyUpdate() throws Exception { |
| Policy policy = createPolicy(); |
| policy.policyUpdate = null; |
| verifyParcel(policy); |
| } |
| |
| /** |
| * Verify that policy created using copy constructor with null source should be the same |
| * as the policy created using default constructor. |
| * |
| * @throws Exception |
| */ |
| @Test |
| public void verifyCopyConstructionWithNullSource() throws Exception { |
| Policy copyPolicy = new Policy(null); |
| Policy defaultPolicy = new Policy(); |
| assertTrue(defaultPolicy.equals(copyPolicy)); |
| } |
| |
| /** |
| * Verify that policy created using copy constructor with a valid source should be the |
| * same as the source. |
| * |
| * @throws Exception |
| */ |
| @Test |
| public void verifyCopyConstructionWithFullPolicy() throws Exception { |
| Policy policy = createPolicy(); |
| Policy copyPolicy = new Policy(policy); |
| assertTrue(policy.equals(copyPolicy)); |
| } |
| |
| /** |
| * Verify that a default policy (with no informatio) is invalid. |
| * |
| * @throws Exception |
| */ |
| @Test |
| public void validatePolicyWithDefault() throws Exception { |
| Policy policy = new Policy(); |
| assertFalse(policy.validate()); |
| } |
| |
| /** |
| * Verify that a policy created using {@link #createPolicy} is valid, since all fields are |
| * filled in with valid values. |
| * |
| * @throws Exception |
| */ |
| @Test |
| public void validatePolicyWithFullPolicy() throws Exception { |
| assertTrue(createPolicy().validate()); |
| } |
| |
| /** |
| * Verify that a policy without policy update parameters is invalid. |
| * |
| * @throws Exception |
| */ |
| @Test |
| public void validatePolicyWithoutPolicyUpdate() throws Exception { |
| Policy policy = createPolicy(); |
| policy.policyUpdate = null; |
| assertFalse(policy.validate()); |
| } |
| |
| /** |
| * Verify that a policy with invalid policy update parameters is invalid. |
| * |
| * @throws Exception |
| */ |
| @Test |
| public void validatePolicyWithInvalidPolicyUpdate() throws Exception { |
| Policy policy = createPolicy(); |
| policy.policyUpdate = new UpdateParameter(); |
| assertFalse(policy.validate()); |
| } |
| |
| /** |
| * Verify that a policy with a preferred roaming partner with FQDN not specified is invalid. |
| * |
| * @throws Exception |
| */ |
| @Test |
| public void validatePolicyWithRoamingPartnerWithoutFQDN() throws Exception { |
| Policy policy = createPolicy(); |
| Policy.RoamingPartner partner = new Policy.RoamingPartner(); |
| partner.fqdnExactMatch = true; |
| partner.priority = 12; |
| partner.countries = "us,jp"; |
| policy.preferredRoamingPartnerList.add(partner); |
| assertFalse(policy.validate()); |
| } |
| |
| /** |
| * Verify that a policy with a preferred roaming partner with countries not specified is |
| * invalid. |
| * |
| * @throws Exception |
| */ |
| @Test |
| public void validatePolicyWithRoamingPartnerWithoutCountries() throws Exception { |
| Policy policy = createPolicy(); |
| Policy.RoamingPartner partner = new Policy.RoamingPartner(); |
| partner.fqdn = "test.com"; |
| partner.fqdnExactMatch = true; |
| partner.priority = 12; |
| policy.preferredRoamingPartnerList.add(partner); |
| assertFalse(policy.validate()); |
| } |
| |
| /** |
| * Verify that a policy with a proto-port tuple that contains an invalid port string is |
| * invalid. |
| * |
| * @throws Exception |
| */ |
| @Test |
| public void validatePolicyWithInvalidPortStringInProtoPortMap() throws Exception { |
| Policy policy = createPolicy(); |
| byte[] rawPortBytes = new byte[MAX_PORT_STRING_BYTES + 1]; |
| policy.requiredProtoPortMap.put(324, new String(rawPortBytes, StandardCharsets.UTF_8)); |
| assertFalse(policy.validate()); |
| } |
| |
| /** |
| * Verify that a policy with number of excluded SSIDs exceeded the max is invalid. |
| * |
| * @throws Exception |
| */ |
| @Test |
| public void validatePolicyWithSsidExclusionListSizeExceededMax() throws Exception { |
| Policy policy = createPolicy(); |
| policy.excludedSsidList = new String[MAX_NUMBER_OF_EXCLUDED_SSIDS + 1]; |
| Arrays.fill(policy.excludedSsidList, "ssid"); |
| assertFalse(policy.validate()); |
| } |
| |
| /** |
| * Verify that a policy with an invalid SSID in the excluded SSID list is invalid. |
| * |
| * @throws Exception |
| */ |
| @Test |
| public void validatePolicyWithInvalidSsid() throws Exception { |
| Policy policy = createPolicy(); |
| byte[] rawSsidBytes = new byte[MAX_SSID_BYTES + 1]; |
| Arrays.fill(rawSsidBytes, (byte) 'a'); |
| policy.excludedSsidList = new String[] {new String(rawSsidBytes, StandardCharsets.UTF_8)}; |
| assertFalse(policy.validate()); |
| } |
| } |