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.appsecurity.cts;
18 
19 import static org.junit.Assert.assertNull;
20 
21 import android.platform.test.annotations.AppModeFull;
22 
23 import com.android.compatibility.common.tradefed.build.CompatibilityBuildHelper;
24 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
25 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test;
26 
27 import org.junit.After;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 
32 import java.io.File;
33 import java.io.FileNotFoundException;
34 
35 /**
36  * Tests for the instant cookie APIs
37  */
38 @RunWith(DeviceJUnit4ClassRunner.class)
39 @AppModeFull(reason = "Already handles instant installs when needed")
40 public class InstantCookieHostTest extends BaseHostJUnit4Test {
41     private static final String INSTANT_COOKIE_APP_APK = "CtsInstantCookieApp.apk";
42     private static final String INSTANT_COOKIE_APP_PKG = "test.instant.cookie";
43 
44     private static final String INSTANT_COOKIE_APP_APK_2 = "CtsInstantCookieApp2.apk";
45     private static final String INSTANT_COOKIE_APP_PKG_2 = "test.instant.cookie";
46 
47     @Before
setUp()48     public void setUp() throws Exception {
49         Utils.prepareSingleUser(getDevice());
50         uninstallPackage(INSTANT_COOKIE_APP_PKG);
51         clearAppCookieData();
52     }
53 
54     @After
tearDown()55     public void tearDown() throws Exception {
56         uninstallPackage(INSTANT_COOKIE_APP_PKG);
57         clearAppCookieData();
58     }
59 
60     @Test
testCookieUpdateAndRetrieval()61     public void testCookieUpdateAndRetrieval() throws Exception {
62         assertNull(installPackage(INSTANT_COOKIE_APP_APK, false, true));
63         runDeviceTests(INSTANT_COOKIE_APP_PKG, "test.instant.cookie.CookieTest",
64                 "testCookieUpdateAndRetrieval");
65     }
66 
67     @Test
testCookiePersistedAcrossInstantInstalls()68     public void testCookiePersistedAcrossInstantInstalls() throws Exception {
69         assertNull(installPackage(INSTANT_COOKIE_APP_APK, false, true));
70         runDeviceTests(INSTANT_COOKIE_APP_PKG, "test.instant.cookie.CookieTest",
71                 "testCookiePersistedAcrossInstantInstalls1");
72         uninstallPackage(INSTANT_COOKIE_APP_PKG);
73         assertNull(installPackage(INSTANT_COOKIE_APP_APK, false, true));
74         runDeviceTests(INSTANT_COOKIE_APP_PKG, "test.instant.cookie.CookieTest",
75                 "testCookiePersistedAcrossInstantInstalls2");
76     }
77 
78     @Test
testCookiePersistedUpgradeFromInstant()79     public void testCookiePersistedUpgradeFromInstant() throws Exception {
80         assertNull(installPackage(INSTANT_COOKIE_APP_APK, false, true));
81         runDeviceTests(INSTANT_COOKIE_APP_PKG, "test.instant.cookie.CookieTest",
82                 "testCookiePersistedUpgradeFromInstant1");
83         assertNull(installPackage(INSTANT_COOKIE_APP_APK, true, false));
84         runDeviceTests(INSTANT_COOKIE_APP_PKG, "test.instant.cookie.CookieTest",
85                 "testCookiePersistedUpgradeFromInstant2");
86     }
87 
88     @Test
testCookieResetOnNonInstantReinstall()89     public void testCookieResetOnNonInstantReinstall() throws Exception {
90         assertNull(installPackage(INSTANT_COOKIE_APP_APK, false, false));
91         runDeviceTests(INSTANT_COOKIE_APP_PKG, "test.instant.cookie.CookieTest",
92                 "testCookieResetOnNonInstantReinstall1");
93         uninstallPackage(INSTANT_COOKIE_APP_PKG);
94         assertNull(installPackage(INSTANT_COOKIE_APP_APK, true, false));
95         runDeviceTests(INSTANT_COOKIE_APP_PKG, "test.instant.cookie.CookieTest",
96                 "testCookieResetOnNonInstantReinstall2");
97     }
98 
99     @Test
testCookieValidWhenSignedWithTwoCerts()100     public void testCookieValidWhenSignedWithTwoCerts() throws Exception {
101         assertNull(installPackage(INSTANT_COOKIE_APP_APK, false, true));
102         runDeviceTests(INSTANT_COOKIE_APP_PKG, "test.instant.cookie.CookieTest",
103                 "testCookiePersistedAcrossInstantInstalls1");
104         uninstallPackage(INSTANT_COOKIE_APP_PKG);
105         assertNull(installPackage(INSTANT_COOKIE_APP_APK_2, true, true));
106         runDeviceTests(INSTANT_COOKIE_APP_PKG_2, "test.instant.cookie.CookieTest",
107                 "testCookiePersistedAcrossInstantInstalls2");
108     }
109 
installPackage(String apk, boolean replace, boolean instant)110     private String installPackage(String apk, boolean replace, boolean instant) throws Exception {
111         return getDevice().installPackage(getTestAppFile(apk), replace,
112                 instant ? "--instant" : "--full");
113     }
114 
getTestAppFile(String fileName)115     private File getTestAppFile(String fileName) throws FileNotFoundException {
116         CompatibilityBuildHelper buildHelper = new CompatibilityBuildHelper(getBuild());
117         return buildHelper.getTestFile(fileName);
118     }
119 
clearAppCookieData()120     private void clearAppCookieData() throws Exception {
121         getDevice().executeShellCommand("pm clear " + INSTANT_COOKIE_APP_PKG);
122     }
123 }
124