1 /*
2  * Copyright (C) 2021 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.carrierapi.cts;
18 
19 import static com.android.compatibility.common.util.UiccUtil.UiccCertificate.CTS_UICC_LEGACY;
20 
21 import static com.google.common.truth.Truth.assertWithMessage;
22 
23 import static org.junit.Assume.assumeTrue;
24 
25 import android.content.Context;
26 import android.telephony.TelephonyManager;
27 
28 import androidx.test.InstrumentationRegistry;
29 
30 import com.android.compatibility.common.util.AmUtils;
31 import com.android.compatibility.common.util.FeatureUtil;
32 import com.android.compatibility.common.util.UiccUtil;
33 
34 import org.junit.Before;
35 
36 /**
37  * Common test base to ensure uniform preconditions checking. This class will check for:
38  *
39  * <ol>
40  *   <li>{@link android.content.pm.PackageManager#FEATURE_TELEPHONY}
41  *   <li>A SIM that grants us carrier privileges is currently active in the device
42  * </ol>
43  *
44  * Just inherit from this class when writing your test, then you are able to assume in the subclass
45  * {@code Before} method that preconditions have all passed. The setup and test methods will not be
46  * executed if preconditions are not met.
47  */
48 public abstract class BaseCarrierApiTest {
49     protected static final String NO_CARRIER_PRIVILEGES_FAILURE_MESSAGE =
50             "This test requires a SIM card with carrier privilege rules on it.\n"
51                     + "Visit https://source.android.com/devices/tech/config/uicc.html";
52     // More specific message when the test suite detects an outdated legacy SIM.
53     private static final String DEPRECATED_TEST_SIM_FAILURE_MESSAGE =
54             "This test requires a 2021-compliant SIM card with carrier privilege rules on it.\n"
55                 + "The current SIM card appears to be outdated and is not compliant with the 2021"
56                 + " CTS SIM specification published with Android 12 (\"S\").\n"
57                 + "As of Android 13 (\"T\"), you must use a 2021-compliant SIM card to pass this"
58                 + " suite. The 2021-compliant SIM is backward compatible with the legacy"
59                 + " specification, so it may also be used to run this suite on older Android"
60                 + " releases.\n"
61                 + "2021-compliant SIMs received directly from Google have \"2021 CTS\" printed on"
62                 + " them.\n"
63                 + "Visit https://source.android.com/devices/tech/config/uicc#prepare_uicc";
64 
getContext()65     protected Context getContext() {
66         return InstrumentationRegistry.getInstrumentation().getTargetContext();
67     }
68 
69     private boolean mPreconditionsSatisfied = false;
70 
werePreconditionsSatisfied()71     protected boolean werePreconditionsSatisfied() {
72         return mPreconditionsSatisfied;
73     }
74 
75     /**
76      * Subclasses do NOT need to explicitly call or override this method. Per the JUnit docs, a
77      * superclass {@code Before} method always executes before a subclass {@code Before} method.
78      *
79      * <p>If preconditions fail, neither the subclass {@code Before} method(s) nor the actual {@code
80      * Test} method will execute, but {@code After} methods will still execute. If a subclass does
81      * work in an {@code After} method, then it should first check {@link
82      * #werePreconditionsSatisfied} and return early without doing any work if it's {@code false}.
83      */
84     @Before
ensurePreconditionsMet()85     public void ensurePreconditionsMet() {
86         mPreconditionsSatisfied = false;
87         // Bail out if no cellular support.
88         assumeTrue(
89                 "No cellular support, CarrierAPI."
90                         + getClass().getSimpleName()
91                         + " cases will be skipped",
92                 FeatureUtil.hasTelephony());
93 
94         // Wait for the broadcast queue to be handled completely to make sure
95         // CarrierPrivilegesTracker has received the ACTION_PACKAGE_ADDED for this test package and
96         // updated the carrier privileges in advance to avoid flakiness.
97         AmUtils.waitForBroadcastBarrier();
98 
99         // We must run with carrier privileges. As of 2022, all devices must run CTS with a SIM
100         // compliant with the 2021 spec, which has a new certificate. To make results very clear, we
101         // still explicitly check for the legacy certificate, and if we don't have carrier
102         // privileges but detect the legacy cert, we tell the tester they must upgrade to pass this
103         // test suite.
104         assertWithMessage(
105                         UiccUtil.uiccHasCertificate(CTS_UICC_LEGACY)
106                                 ? DEPRECATED_TEST_SIM_FAILURE_MESSAGE
107                                 : NO_CARRIER_PRIVILEGES_FAILURE_MESSAGE)
108                 .that(getContext().getSystemService(TelephonyManager.class).hasCarrierPrivileges())
109                 .isTrue();
110         mPreconditionsSatisfied = true;
111     }
112 }
113