Home | History | Annotate | Download | only in cookie
      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 test.instant.cookie;
     18 
     19 import static org.junit.Assert.assertEquals;
     20 import static org.junit.Assert.assertFalse;
     21 import static org.junit.Assert.assertTrue;
     22 import static org.junit.Assert.fail;
     23 
     24 import android.content.pm.PackageManager;
     25 
     26 import androidx.test.InstrumentationRegistry;
     27 import androidx.test.runner.AndroidJUnit4;
     28 
     29 import org.junit.Test;
     30 import org.junit.runner.RunWith;
     31 
     32 @RunWith(AndroidJUnit4.class)
     33 public class CookieTest {
     34     @Test
     35     public void testCookieUpdateAndRetrieval() throws Exception {
     36         PackageManager pm = InstrumentationRegistry.getContext().getPackageManager();
     37 
     38         // We should be an instant app
     39         assertTrue(pm.isInstantApp());
     40 
     41         // The max cookie size is greater than zero
     42         assertTrue(pm.getInstantAppCookieMaxBytes() > 0);
     43 
     44         // Initially there is no cookie
     45         byte[] cookie = pm.getInstantAppCookie();
     46         assertTrue(cookie != null && cookie.length == 0);
     47 
     48         // Setting a cookie below max size should work
     49         pm.updateInstantAppCookie("1".getBytes());
     50 
     51         // Setting a cookie above max size should not work
     52         try {
     53             pm.updateInstantAppCookie(
     54                     new byte[pm.getInstantAppCookieMaxBytes() + 1]);
     55             fail("Shouldn't be able to set a cookie larger than max size");
     56         } catch (IllegalArgumentException e) {
     57             /* expected */
     58         }
     59 
     60         // Ensure cookie not modified
     61         assertEquals("1", new String(pm.getInstantAppCookie()));
     62     }
     63 
     64     @Test
     65     public void testCookiePersistedAcrossInstantInstalls1() throws Exception {
     66         PackageManager pm = InstrumentationRegistry.getContext().getPackageManager();
     67 
     68         // Set a cookie to later check when reinstalled as instant app
     69         pm.updateInstantAppCookie("2".getBytes());
     70     }
     71 
     72     @Test
     73     public void testCookiePersistedAcrossInstantInstalls2() throws Exception {
     74         PackageManager pm = InstrumentationRegistry.getContext().getPackageManager();
     75 
     76         // After the upgrade the cookie should be the same
     77         assertEquals("2", new String(pm.getInstantAppCookie()));
     78     }
     79 
     80     @Test
     81     public void testCookiePersistedUpgradeFromInstant1() throws Exception {
     82         PackageManager pm = InstrumentationRegistry.getContext().getPackageManager();
     83 
     84         // Make sure we are an instant app
     85         assertTrue(pm.isInstantApp());
     86 
     87         // Set a cookie to later check when upgrade to a normal app
     88         pm.updateInstantAppCookie("3".getBytes());
     89     }
     90 
     91     @Test
     92     public void testCookiePersistedUpgradeFromInstant2() throws Exception {
     93         PackageManager pm = InstrumentationRegistry.getContext().getPackageManager();
     94 
     95         // Make sure we are not an instant app
     96         assertFalse(pm.isInstantApp());
     97 
     98         // The cookie survives the upgrade to a normal app
     99         assertEquals("3", new String(pm.getInstantAppCookie()));
    100     }
    101 
    102     @Test
    103     public void testCookieResetOnNonInstantReinstall1() throws Exception {
    104         PackageManager pm = InstrumentationRegistry.getContext().getPackageManager();
    105 
    106         // Set a cookie to later check when reinstalled as normal app
    107         pm.updateInstantAppCookie("4".getBytes());
    108     }
    109 
    110     @Test
    111     public void testCookieResetOnNonInstantReinstall2() throws Exception {
    112         PackageManager pm = InstrumentationRegistry.getContext().getPackageManager();
    113 
    114         // The cookie should have been wiped if non-instant app is uninstalled
    115         byte[] cookie = pm.getInstantAppCookie();
    116         assertTrue(cookie != null && cookie.length == 0);
    117     }
    118 }
    119