Home | History | Annotate | Download | only in pps
      1 /*
      2  * Copyright (C) 2017 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.wifi.hotspot2.pps;
     18 
     19 import static org.junit.Assert.assertFalse;
     20 import static org.junit.Assert.assertTrue;
     21 
     22 import android.os.Parcel;
     23 import android.support.test.filters.SmallTest;
     24 import android.util.Base64;
     25 
     26 import org.junit.Test;
     27 
     28 import java.nio.charset.StandardCharsets;
     29 import java.util.ArrayList;
     30 import java.util.Arrays;
     31 import java.util.Collections;
     32 import java.util.HashMap;
     33 import java.util.List;
     34 import java.util.Map;
     35 
     36 /**
     37  * Unit tests for {@link android.net.wifi.hotspot2.pps.Policy}.
     38  */
     39 @SmallTest
     40 public class PolicyTest {
     41     private static final int MAX_NUMBER_OF_EXCLUDED_SSIDS = 128;
     42     private static final int MAX_SSID_BYTES = 32;
     43     private static final int MAX_PORT_STRING_BYTES = 64;
     44 
     45     /**
     46      * Helper function for creating a {@link Policy} for testing.
     47      *
     48      * @return {@link Policy}
     49      */
     50     private static Policy createPolicy() {
     51         Policy policy = new Policy();
     52         policy.setMinHomeDownlinkBandwidth(123);
     53         policy.setMinHomeUplinkBandwidth(345);
     54         policy.setMinRoamingDownlinkBandwidth(567);
     55         policy.setMinRoamingUplinkBandwidth(789);
     56         policy.setExcludedSsidList(new String[] {"ssid1", "ssid2"});
     57         Map<Integer, String> requiredProtoPortMap = new HashMap<>();
     58         requiredProtoPortMap.put(12, "23,342,123");
     59         requiredProtoPortMap.put(23, "789,372,1235");
     60         policy.setRequiredProtoPortMap(requiredProtoPortMap);
     61         policy.setMaximumBssLoadValue(12);
     62 
     63         List<Policy.RoamingPartner> preferredRoamingPartnerList = new ArrayList<>();
     64         Policy.RoamingPartner partner1 = new Policy.RoamingPartner();
     65         partner1.setFqdn("partner1.com");
     66         partner1.setFqdnExactMatch(true);
     67         partner1.setPriority(12);
     68         partner1.setCountries("us,jp");
     69         Policy.RoamingPartner partner2 = new Policy.RoamingPartner();
     70         partner2.setFqdn("partner2.com");
     71         partner2.setFqdnExactMatch(false);
     72         partner2.setPriority(42);
     73         partner2.setCountries("ca,fr");
     74         preferredRoamingPartnerList.add(partner1);
     75         preferredRoamingPartnerList.add(partner2);
     76         policy.setPreferredRoamingPartnerList(preferredRoamingPartnerList);
     77 
     78         UpdateParameter policyUpdate = new UpdateParameter();
     79         policyUpdate.setUpdateIntervalInMinutes(1712);
     80         policyUpdate.setUpdateMethod(UpdateParameter.UPDATE_METHOD_OMADM);
     81         policyUpdate.setRestriction(UpdateParameter.UPDATE_RESTRICTION_HOMESP);
     82         policyUpdate.setServerUri("policy.update.com");
     83         policyUpdate.setUsername("username");
     84         policyUpdate.setBase64EncodedPassword(
     85                 Base64.encodeToString("password".getBytes(), Base64.DEFAULT));
     86         policyUpdate.setTrustRootCertUrl("trust.cert.com");
     87         policyUpdate.setTrustRootCertSha256Fingerprint(new byte[32]);
     88         policy.setPolicyUpdate(policyUpdate);
     89 
     90         return policy;
     91     }
     92 
     93     /**
     94      * Helper function for verifying Policy after parcel write then read.
     95      * @param policyToWrite
     96      * @throws Exception
     97      */
     98     private static void verifyParcel(Policy policyToWrite) throws Exception {
     99         Parcel parcel = Parcel.obtain();
    100         policyToWrite.writeToParcel(parcel, 0);
    101 
    102         parcel.setDataPosition(0);    // Rewind data position back to the beginning for read.
    103         Policy policyFromRead = Policy.CREATOR.createFromParcel(parcel);
    104         assertTrue(policyFromRead.equals(policyToWrite));
    105     }
    106 
    107     /**
    108      * Verify parcel read/write for an empty Policy.
    109      *
    110      * @throws Exception
    111      */
    112     @Test
    113     public void verifyParcelWithEmptyPolicy() throws Exception {
    114         verifyParcel(new Policy());
    115     }
    116 
    117     /**
    118      * Verify parcel read/write for a Policy with all fields set.
    119      *
    120      * @throws Exception
    121      */
    122     @Test
    123     public void verifyParcelWithFullPolicy() throws Exception {
    124         verifyParcel(createPolicy());
    125     }
    126 
    127     /**
    128      * Verify parcel read/write for a Policy without protocol port map.
    129      *
    130      * @throws Exception
    131      */
    132     @Test
    133     public void verifyParcelWithoutProtoPortMap() throws Exception {
    134         Policy policy = createPolicy();
    135         policy.setRequiredProtoPortMap(null);
    136         verifyParcel(policy);
    137     }
    138 
    139     /**
    140      * Verify parcel read/write for a Policy without preferred roaming partner list.
    141      *
    142      * @throws Exception
    143      */
    144     @Test
    145     public void verifyParcelWithoutPreferredRoamingPartnerList() throws Exception {
    146         Policy policy = createPolicy();
    147         policy.setPreferredRoamingPartnerList(null);
    148         verifyParcel(policy);
    149     }
    150 
    151     /**
    152      * Verify parcel read/write for a Policy without policy update parameters.
    153      *
    154      * @throws Exception
    155      */
    156     @Test
    157     public void verifyParcelWithoutPolicyUpdate() throws Exception {
    158         Policy policy = createPolicy();
    159         policy.setPolicyUpdate(null);
    160         verifyParcel(policy);
    161     }
    162 
    163     /**
    164      * Verify that policy created using copy constructor with null source should be the same
    165      * as the policy created using default constructor.
    166      *
    167      * @throws Exception
    168      */
    169     @Test
    170     public void verifyCopyConstructionWithNullSource() throws Exception {
    171         Policy copyPolicy = new Policy(null);
    172         Policy defaultPolicy = new Policy();
    173         assertTrue(defaultPolicy.equals(copyPolicy));
    174     }
    175 
    176     /**
    177      * Verify that policy created using copy constructor with a valid source should be the
    178      * same as the source.
    179      *
    180      * @throws Exception
    181      */
    182     @Test
    183     public void verifyCopyConstructionWithFullPolicy() throws Exception {
    184         Policy policy = createPolicy();
    185         Policy copyPolicy = new Policy(policy);
    186         assertTrue(policy.equals(copyPolicy));
    187     }
    188 
    189     /**
    190      * Verify that a default policy (with no informatio) is invalid.
    191      *
    192      * @throws Exception
    193      */
    194     @Test
    195     public void validatePolicyWithDefault() throws Exception {
    196         Policy policy = new Policy();
    197         assertFalse(policy.validate());
    198     }
    199 
    200     /**
    201      * Verify that a policy created using {@link #createPolicy} is valid, since all fields are
    202      * filled in with valid values.
    203      *
    204      * @throws Exception
    205      */
    206     @Test
    207     public void validatePolicyWithFullPolicy() throws Exception {
    208         assertTrue(createPolicy().validate());
    209     }
    210 
    211     /**
    212      * Verify that a policy without policy update parameters is invalid.
    213      *
    214      * @throws Exception
    215      */
    216     @Test
    217     public void validatePolicyWithoutPolicyUpdate() throws Exception {
    218         Policy policy = createPolicy();
    219         policy.setPolicyUpdate(null);
    220         assertFalse(policy.validate());
    221     }
    222 
    223     /**
    224      * Verify that a policy with invalid policy update parameters is invalid.
    225      *
    226      * @throws Exception
    227      */
    228     @Test
    229     public void validatePolicyWithInvalidPolicyUpdate() throws Exception {
    230         Policy policy = createPolicy();
    231         policy.setPolicyUpdate(new UpdateParameter());
    232         assertFalse(policy.validate());
    233     }
    234 
    235     /**
    236      * Verify that a policy with a preferred roaming partner with FQDN not specified is invalid.
    237      *
    238      * @throws Exception
    239      */
    240     @Test
    241     public void validatePolicyWithRoamingPartnerWithoutFQDN() throws Exception {
    242         Policy policy = createPolicy();
    243         Policy.RoamingPartner partner = new Policy.RoamingPartner();
    244         partner.setFqdnExactMatch(true);
    245         partner.setPriority(12);
    246         partner.setCountries("us,jp");
    247         policy.getPreferredRoamingPartnerList().add(partner);
    248         assertFalse(policy.validate());
    249     }
    250 
    251     /**
    252      * Verify that a policy with a preferred roaming partner with countries not specified is
    253      * invalid.
    254      *
    255      * @throws Exception
    256      */
    257     @Test
    258     public void validatePolicyWithRoamingPartnerWithoutCountries() throws Exception {
    259         Policy policy = createPolicy();
    260         Policy.RoamingPartner partner = new Policy.RoamingPartner();
    261         partner.setFqdn("test.com");
    262         partner.setFqdnExactMatch(true);
    263         partner.setPriority(12);
    264         policy.getPreferredRoamingPartnerList().add(partner);
    265         assertFalse(policy.validate());
    266     }
    267 
    268     /**
    269      * Verify that a policy with a proto-port tuple that contains an invalid port string is
    270      * invalid.
    271      *
    272      * @throws Exception
    273      */
    274     @Test
    275     public void validatePolicyWithInvalidPortStringInProtoPortMap() throws Exception {
    276         Policy policy = createPolicy();
    277         byte[] rawPortBytes = new byte[MAX_PORT_STRING_BYTES + 1];
    278         policy.getRequiredProtoPortMap().put(
    279                 324, new String(rawPortBytes, StandardCharsets.UTF_8));
    280         assertFalse(policy.validate());
    281     }
    282 
    283     /**
    284      * Verify that a policy with number of excluded SSIDs exceeded the max is invalid.
    285      *
    286      * @throws Exception
    287      */
    288     @Test
    289     public void validatePolicyWithSsidExclusionListSizeExceededMax() throws Exception {
    290         Policy policy = createPolicy();
    291         String[] excludedSsidList = new String[MAX_NUMBER_OF_EXCLUDED_SSIDS + 1];
    292         Arrays.fill(excludedSsidList, "ssid");
    293         policy.setExcludedSsidList(excludedSsidList);
    294         assertFalse(policy.validate());
    295     }
    296 
    297     /**
    298      * Verify that a policy with an invalid SSID in the excluded SSID list is invalid.
    299      *
    300      * @throws Exception
    301      */
    302     @Test
    303     public void validatePolicyWithInvalidSsid() throws Exception {
    304         Policy policy = createPolicy();
    305         byte[] rawSsidBytes = new byte[MAX_SSID_BYTES + 1];
    306         Arrays.fill(rawSsidBytes, (byte) 'a');
    307         String[] excludedSsidList = new String[] {
    308                 new String(rawSsidBytes, StandardCharsets.UTF_8)};
    309         policy.setExcludedSsidList(excludedSsidList);
    310         assertFalse(policy.validate());
    311     }
    312 }
    313