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