| Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | #include "hidden_api.h" |
| 18 | |
| David Brazdil | a5c3a80 | 2019-03-08 14:59:41 +0000 | [diff] [blame] | 19 | #include <fstream> |
| David Brazdil | bfaba28 | 2019-03-15 11:35:51 +0000 | [diff] [blame] | 20 | #include <sstream> |
| David Brazdil | a5c3a80 | 2019-03-08 14:59:41 +0000 | [diff] [blame] | 21 | |
| Orion Hodson | 9735ccc | 2020-07-20 13:05:04 +0100 | [diff] [blame] | 22 | #include "android-base/stringprintf.h" |
| David Brazdil | a5c3a80 | 2019-03-08 14:59:41 +0000 | [diff] [blame] | 23 | #include "base/file_utils.h" |
| David Brazdil | 2bb2fbd | 2018-11-13 18:24:26 +0000 | [diff] [blame] | 24 | #include "base/sdk_version.h" |
| David Brazdil | a5c3a80 | 2019-03-08 14:59:41 +0000 | [diff] [blame] | 25 | #include "base/stl_util.h" |
| Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 26 | #include "common_runtime_test.h" |
| Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 27 | #include "jni/jni_internal.h" |
| David Brazdil | 1f9d3c3 | 2018-05-02 16:53:06 +0100 | [diff] [blame] | 28 | #include "proxy_test.h" |
| David Brazdil | a5c3a80 | 2019-03-08 14:59:41 +0000 | [diff] [blame] | 29 | #include "well_known_classes.h" |
| Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 30 | |
| 31 | namespace art { |
| 32 | |
| Orion Hodson | 9735ccc | 2020-07-20 13:05:04 +0100 | [diff] [blame] | 33 | using android::base::StringPrintf; |
| Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 34 | using hiddenapi::detail::MemberSignature; |
| David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 35 | using hiddenapi::detail::ShouldDenyAccessToMemberImpl; |
| Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 36 | |
| Pedro Loureiro | f7fcb98 | 2020-11-24 16:44:12 +0000 | [diff] [blame^] | 37 | // Should be the same as dalvik.system.VMRuntime.HIDE_MAXTARGETSDK_P_HIDDEN_APIS, |
| 38 | // dalvik.system.VMRuntime.HIDE_MAXTARGETSDK_Q_HIDDEN_APIS, and |
| 39 | // dalvik.system.VMRuntime.EXEMPT_TEST_API_ACCESS_VERIFICATION. |
| atrost | 2dea079 | 2020-02-25 20:11:47 +0000 | [diff] [blame] | 40 | static constexpr uint64_t kHideMaxtargetsdkPHiddenApis = 149997251; |
| 41 | static constexpr uint64_t kHideMaxtargetsdkQHiddenApis = 149994052; |
| Pedro Loureiro | f7fcb98 | 2020-11-24 16:44:12 +0000 | [diff] [blame^] | 42 | static constexpr uint64_t kAllowTestApiAccess = 166236554; |
| atrost | 2dea079 | 2020-02-25 20:11:47 +0000 | [diff] [blame] | 43 | |
| Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 44 | class HiddenApiTest : public CommonRuntimeTest { |
| 45 | protected: |
| Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 46 | void SetUp() override { |
| Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 47 | // Do the normal setup. |
| 48 | CommonRuntimeTest::SetUp(); |
| 49 | self_ = Thread::Current(); |
| 50 | self_->TransitionFromSuspendedToRunnable(); |
| David Brazdil | 1f9d3c3 | 2018-05-02 16:53:06 +0100 | [diff] [blame] | 51 | jclass_loader_ = LoadDex("HiddenApiSignatures"); |
| Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 52 | bool started = runtime_->Start(); |
| 53 | CHECK(started); |
| 54 | |
| 55 | class1_field1_ = getArtField("mypackage/packagea/Class1", "field1", "I"); |
| 56 | class1_field12_ = getArtField("mypackage/packagea/Class1", "field12", "I"); |
| 57 | class1_init_ = getArtMethod("mypackage/packagea/Class1", "<init>", "()V"); |
| 58 | class1_method1_ = getArtMethod("mypackage/packagea/Class1", "method1", "()V"); |
| 59 | class1_method1_i_ = getArtMethod("mypackage/packagea/Class1", "method1", "(I)V"); |
| 60 | class1_method12_ = getArtMethod("mypackage/packagea/Class1", "method12", "()V"); |
| 61 | class12_field1_ = getArtField("mypackage/packagea/Class12", "field1", "I"); |
| 62 | class12_method1_ = getArtMethod("mypackage/packagea/Class12", "method1", "()V"); |
| 63 | class2_field1_ = getArtField("mypackage/packagea/Class2", "field1", "I"); |
| 64 | class2_method1_ = getArtMethod("mypackage/packagea/Class2", "method1", "()V"); |
| 65 | class2_method1_i_ = getArtMethod("mypackage/packagea/Class2", "method1", "(I)V"); |
| 66 | class3_field1_ = getArtField("mypackage/packageb/Class3", "field1", "I"); |
| 67 | class3_method1_ = getArtMethod("mypackage/packageb/Class3", "method1", "()V"); |
| 68 | class3_method1_i_ = getArtMethod("mypackage/packageb/Class3", "method1", "(I)V"); |
| 69 | } |
| 70 | |
| 71 | ArtMethod* getArtMethod(const char* class_name, const char* name, const char* signature) { |
| 72 | JNIEnv* env = Thread::Current()->GetJniEnv(); |
| 73 | jclass klass = env->FindClass(class_name); |
| 74 | jmethodID method_id = env->GetMethodID(klass, name, signature); |
| 75 | ArtMethod* art_method = jni::DecodeArtMethod(method_id); |
| 76 | return art_method; |
| 77 | } |
| 78 | |
| 79 | ArtField* getArtField(const char* class_name, const char* name, const char* signature) { |
| 80 | JNIEnv* env = Thread::Current()->GetJniEnv(); |
| 81 | jclass klass = env->FindClass(class_name); |
| 82 | jfieldID field_id = env->GetFieldID(klass, name, signature); |
| 83 | ArtField* art_field = jni::DecodeArtField(field_id); |
| 84 | return art_field; |
| 85 | } |
| 86 | |
| atrost | 2dea079 | 2020-02-25 20:11:47 +0000 | [diff] [blame] | 87 | void setChangeIdState(uint64_t change, bool enabled) { |
| Andrei Onea | 037d282 | 2020-11-19 00:20:04 +0000 | [diff] [blame] | 88 | CompatFramework& compat_framework = runtime_->GetCompatFramework(); |
| 89 | std::set<uint64_t> disabled_changes = compat_framework.GetDisabledCompatChanges(); |
| atrost | 2dea079 | 2020-02-25 20:11:47 +0000 | [diff] [blame] | 90 | if (enabled) { |
| 91 | disabled_changes.erase(change); |
| 92 | } else { |
| 93 | disabled_changes.insert(change); |
| 94 | } |
| Andrei Onea | 037d282 | 2020-11-19 00:20:04 +0000 | [diff] [blame] | 95 | compat_framework.SetDisabledCompatChanges(disabled_changes); |
| atrost | 2dea079 | 2020-02-25 20:11:47 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 98 | bool ShouldDenyAccess(hiddenapi::ApiList list) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 99 | // Choose parameters such that there are no side effects (AccessMethod::kNone) |
| 100 | // and that the member is not on the exemptions list (here we choose one which |
| 101 | // is not even in boot class path). |
| 102 | return ShouldDenyAccessToMemberImpl(/* member= */ class1_field1_, |
| 103 | list, |
| 104 | /* access_method= */ hiddenapi::AccessMethod::kNone); |
| 105 | } |
| 106 | |
| Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 107 | protected: |
| 108 | Thread* self_; |
| David Brazdil | 1f9d3c3 | 2018-05-02 16:53:06 +0100 | [diff] [blame] | 109 | jobject jclass_loader_; |
| Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 110 | ArtField* class1_field1_; |
| 111 | ArtField* class1_field12_; |
| 112 | ArtMethod* class1_init_; |
| 113 | ArtMethod* class1_method1_; |
| 114 | ArtMethod* class1_method1_i_; |
| 115 | ArtMethod* class1_method12_; |
| 116 | ArtField* class12_field1_; |
| 117 | ArtMethod* class12_method1_; |
| 118 | ArtField* class2_field1_; |
| 119 | ArtMethod* class2_method1_; |
| 120 | ArtMethod* class2_method1_i_; |
| 121 | ArtField* class3_field1_; |
| 122 | ArtMethod* class3_method1_; |
| 123 | ArtMethod* class3_method1_i_; |
| 124 | }; |
| 125 | |
| Mathew Inwood | a8503d9 | 2018-04-05 16:10:25 +0100 | [diff] [blame] | 126 | TEST_F(HiddenApiTest, CheckGetActionFromRuntimeFlags) { |
| David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 127 | ScopedObjectAccess soa(self_); |
| 128 | |
| Mathew Inwood | a8503d9 | 2018-04-05 16:10:25 +0100 | [diff] [blame] | 129 | runtime_->SetHiddenApiEnforcementPolicy(hiddenapi::EnforcementPolicy::kJustWarn); |
| Andrei Onea | fc12a6c | 2020-07-29 19:52:34 +0100 | [diff] [blame] | 130 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Sdk()), false); |
| 131 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Unsupported()), false); |
| 132 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::MaxTargetR()), false); |
| 133 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::MaxTargetQ()), false); |
| 134 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::MaxTargetP()), false); |
| 135 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::MaxTargetO()), false); |
| 136 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Blocked()), false); |
| Mathew Inwood | a8503d9 | 2018-04-05 16:10:25 +0100 | [diff] [blame] | 137 | |
| David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 138 | runtime_->SetHiddenApiEnforcementPolicy(hiddenapi::EnforcementPolicy::kEnabled); |
| David Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame] | 139 | runtime_->SetTargetSdkVersion( |
| Andrei Onea | fc12a6c | 2020-07-29 19:52:34 +0100 | [diff] [blame] | 140 | static_cast<uint32_t>(hiddenapi::ApiList::MaxTargetO().GetMaxAllowedSdkVersion())); |
| atrost | 2dea079 | 2020-02-25 20:11:47 +0000 | [diff] [blame] | 141 | setChangeIdState(kHideMaxtargetsdkPHiddenApis, false); |
| 142 | setChangeIdState(kHideMaxtargetsdkQHiddenApis, false); |
| Andrei Onea | fc12a6c | 2020-07-29 19:52:34 +0100 | [diff] [blame] | 143 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Sdk()), false); |
| 144 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Unsupported()), false); |
| 145 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::MaxTargetR()), false); |
| 146 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::MaxTargetQ()), false); |
| 147 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::MaxTargetP()), false); |
| 148 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::MaxTargetO()), false); |
| 149 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Blocked()), true); |
| Mathew Inwood | a8503d9 | 2018-04-05 16:10:25 +0100 | [diff] [blame] | 150 | |
| David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 151 | runtime_->SetHiddenApiEnforcementPolicy(hiddenapi::EnforcementPolicy::kEnabled); |
| David Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame] | 152 | runtime_->SetTargetSdkVersion( |
| Andrei Onea | fc12a6c | 2020-07-29 19:52:34 +0100 | [diff] [blame] | 153 | static_cast<uint32_t>(hiddenapi::ApiList::MaxTargetO().GetMaxAllowedSdkVersion()) + 1); |
| atrost | 2dea079 | 2020-02-25 20:11:47 +0000 | [diff] [blame] | 154 | setChangeIdState(kHideMaxtargetsdkPHiddenApis, false); |
| 155 | setChangeIdState(kHideMaxtargetsdkQHiddenApis, false); |
| Andrei Onea | fc12a6c | 2020-07-29 19:52:34 +0100 | [diff] [blame] | 156 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Sdk()), false); |
| 157 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Unsupported()), false); |
| 158 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::MaxTargetR()), false); |
| 159 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::MaxTargetQ()), false); |
| 160 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::MaxTargetP()), false); |
| 161 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::MaxTargetO()), true); |
| 162 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Blocked()), true); |
| atrost | 2dea079 | 2020-02-25 20:11:47 +0000 | [diff] [blame] | 163 | setChangeIdState(kHideMaxtargetsdkQHiddenApis, true); |
| Andrei Onea | fc12a6c | 2020-07-29 19:52:34 +0100 | [diff] [blame] | 164 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Sdk()), false); |
| 165 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Unsupported()), false); |
| 166 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::MaxTargetQ()), true); |
| 167 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::MaxTargetP()), false); |
| 168 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::MaxTargetO()), true); |
| 169 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Blocked()), true); |
| David Brazdil | 80d1628 | 2018-11-01 09:55:09 +0000 | [diff] [blame] | 170 | |
| 171 | runtime_->SetHiddenApiEnforcementPolicy(hiddenapi::EnforcementPolicy::kEnabled); |
| 172 | runtime_->SetTargetSdkVersion( |
| Andrei Onea | fc12a6c | 2020-07-29 19:52:34 +0100 | [diff] [blame] | 173 | static_cast<uint32_t>(hiddenapi::ApiList::MaxTargetP().GetMaxAllowedSdkVersion()) + 1); |
| atrost | 2dea079 | 2020-02-25 20:11:47 +0000 | [diff] [blame] | 174 | setChangeIdState(kHideMaxtargetsdkPHiddenApis, true); |
| 175 | setChangeIdState(kHideMaxtargetsdkQHiddenApis, false); |
| Andrei Onea | fc12a6c | 2020-07-29 19:52:34 +0100 | [diff] [blame] | 176 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Sdk()), false); |
| 177 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Unsupported()), false); |
| 178 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::MaxTargetR()), false); |
| 179 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::MaxTargetQ()), false); |
| 180 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::MaxTargetP()), true); |
| 181 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::MaxTargetO()), true); |
| 182 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Blocked()), true); |
| Artur Satayev | 201ffea | 2019-10-31 14:58:03 +0000 | [diff] [blame] | 183 | |
| 184 | runtime_->SetHiddenApiEnforcementPolicy(hiddenapi::EnforcementPolicy::kEnabled); |
| 185 | runtime_->SetTargetSdkVersion( |
| Andrei Onea | fc12a6c | 2020-07-29 19:52:34 +0100 | [diff] [blame] | 186 | static_cast<uint32_t>(hiddenapi::ApiList::MaxTargetQ().GetMaxAllowedSdkVersion()) + 1); |
| atrost | 2dea079 | 2020-02-25 20:11:47 +0000 | [diff] [blame] | 187 | setChangeIdState(kHideMaxtargetsdkPHiddenApis, true); |
| 188 | setChangeIdState(kHideMaxtargetsdkQHiddenApis, true); |
| Andrei Onea | fc12a6c | 2020-07-29 19:52:34 +0100 | [diff] [blame] | 189 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Sdk()), false); |
| 190 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Unsupported()), false); |
| 191 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::MaxTargetR()), false); |
| 192 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::MaxTargetQ()), true); |
| 193 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::MaxTargetP()), true); |
| 194 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::MaxTargetO()), true); |
| 195 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Blocked()), true); |
| Artur Satayev | b708fc1 | 2020-05-20 17:48:19 +0100 | [diff] [blame] | 196 | |
| 197 | runtime_->SetHiddenApiEnforcementPolicy(hiddenapi::EnforcementPolicy::kEnabled); |
| 198 | runtime_->SetTargetSdkVersion( |
| Andrei Onea | fc12a6c | 2020-07-29 19:52:34 +0100 | [diff] [blame] | 199 | static_cast<uint32_t>(hiddenapi::ApiList::MaxTargetR().GetMaxAllowedSdkVersion()) + 1); |
| Artur Satayev | b708fc1 | 2020-05-20 17:48:19 +0100 | [diff] [blame] | 200 | setChangeIdState(kHideMaxtargetsdkPHiddenApis, true); |
| 201 | setChangeIdState(kHideMaxtargetsdkQHiddenApis, true); |
| Andrei Onea | fc12a6c | 2020-07-29 19:52:34 +0100 | [diff] [blame] | 202 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Sdk()), false); |
| 203 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Unsupported()), false); |
| 204 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::MaxTargetR()), true); |
| 205 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::MaxTargetQ()), true); |
| 206 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::MaxTargetP()), true); |
| 207 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::MaxTargetO()), true); |
| 208 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Blocked()), true); |
| Mathew Inwood | a8503d9 | 2018-04-05 16:10:25 +0100 | [diff] [blame] | 209 | } |
| 210 | |
| Artur Satayev | 267366c | 2019-10-31 14:59:26 +0000 | [diff] [blame] | 211 | TEST_F(HiddenApiTest, CheckTestApiEnforcement) { |
| 212 | ScopedObjectAccess soa(self_); |
| 213 | |
| 214 | runtime_->SetHiddenApiEnforcementPolicy(hiddenapi::EnforcementPolicy::kEnabled); |
| 215 | runtime_->SetTargetSdkVersion( |
| Andrei Onea | fc12a6c | 2020-07-29 19:52:34 +0100 | [diff] [blame] | 216 | static_cast<uint32_t>(hiddenapi::ApiList::MaxTargetR().GetMaxAllowedSdkVersion()) + 1); |
| Artur Satayev | 267366c | 2019-10-31 14:59:26 +0000 | [diff] [blame] | 217 | |
| 218 | // Default case where all TestApis are treated like non-TestApi. |
| 219 | runtime_->SetTestApiEnforcementPolicy(hiddenapi::EnforcementPolicy::kEnabled); |
| Pedro Loureiro | f7fcb98 | 2020-11-24 16:44:12 +0000 | [diff] [blame^] | 220 | setChangeIdState(kAllowTestApiAccess, false); |
| Artur Satayev | 267366c | 2019-10-31 14:59:26 +0000 | [diff] [blame] | 221 | ASSERT_EQ( |
| Andrei Onea | fc12a6c | 2020-07-29 19:52:34 +0100 | [diff] [blame] | 222 | ShouldDenyAccess(hiddenapi::ApiList::TestApi() | hiddenapi::ApiList::Sdk()), false); |
| Artur Satayev | 267366c | 2019-10-31 14:59:26 +0000 | [diff] [blame] | 223 | ASSERT_EQ( |
| Andrei Onea | fc12a6c | 2020-07-29 19:52:34 +0100 | [diff] [blame] | 224 | ShouldDenyAccess(hiddenapi::ApiList::TestApi() | hiddenapi::ApiList::Unsupported()), false); |
| Artur Satayev | 267366c | 2019-10-31 14:59:26 +0000 | [diff] [blame] | 225 | ASSERT_EQ( |
| Andrei Onea | fc12a6c | 2020-07-29 19:52:34 +0100 | [diff] [blame] | 226 | ShouldDenyAccess(hiddenapi::ApiList::TestApi() | hiddenapi::ApiList::MaxTargetR()), true); |
| Artur Satayev | b708fc1 | 2020-05-20 17:48:19 +0100 | [diff] [blame] | 227 | ASSERT_EQ( |
| Andrei Onea | fc12a6c | 2020-07-29 19:52:34 +0100 | [diff] [blame] | 228 | ShouldDenyAccess(hiddenapi::ApiList::TestApi() | hiddenapi::ApiList::MaxTargetQ()), true); |
| Artur Satayev | 267366c | 2019-10-31 14:59:26 +0000 | [diff] [blame] | 229 | ASSERT_EQ( |
| Andrei Onea | fc12a6c | 2020-07-29 19:52:34 +0100 | [diff] [blame] | 230 | ShouldDenyAccess(hiddenapi::ApiList::TestApi() | hiddenapi::ApiList::MaxTargetP()), true); |
| Artur Satayev | 267366c | 2019-10-31 14:59:26 +0000 | [diff] [blame] | 231 | ASSERT_EQ( |
| Andrei Onea | fc12a6c | 2020-07-29 19:52:34 +0100 | [diff] [blame] | 232 | ShouldDenyAccess(hiddenapi::ApiList::TestApi() | hiddenapi::ApiList::MaxTargetO()), true); |
| Artur Satayev | 267366c | 2019-10-31 14:59:26 +0000 | [diff] [blame] | 233 | ASSERT_EQ( |
| Andrei Onea | fc12a6c | 2020-07-29 19:52:34 +0100 | [diff] [blame] | 234 | ShouldDenyAccess(hiddenapi::ApiList::TestApi() | hiddenapi::ApiList::Blocked()), true); |
| Artur Satayev | 267366c | 2019-10-31 14:59:26 +0000 | [diff] [blame] | 235 | |
| 236 | // A case where we want to allow access to TestApis. |
| 237 | runtime_->SetTestApiEnforcementPolicy(hiddenapi::EnforcementPolicy::kDisabled); |
| Pedro Loureiro | f7fcb98 | 2020-11-24 16:44:12 +0000 | [diff] [blame^] | 238 | setChangeIdState(kAllowTestApiAccess, false); |
| 239 | ASSERT_EQ( |
| 240 | ShouldDenyAccess(hiddenapi::ApiList::TestApi() | hiddenapi::ApiList::Sdk()), false); |
| 241 | ASSERT_EQ( |
| 242 | ShouldDenyAccess(hiddenapi::ApiList::TestApi() | hiddenapi::ApiList::Unsupported()), false); |
| 243 | ASSERT_EQ( |
| 244 | ShouldDenyAccess(hiddenapi::ApiList::TestApi() | hiddenapi::ApiList::MaxTargetR()), false); |
| 245 | ASSERT_EQ( |
| 246 | ShouldDenyAccess(hiddenapi::ApiList::TestApi() | hiddenapi::ApiList::MaxTargetQ()), false); |
| 247 | ASSERT_EQ( |
| 248 | ShouldDenyAccess(hiddenapi::ApiList::TestApi() | hiddenapi::ApiList::MaxTargetP()), false); |
| 249 | ASSERT_EQ( |
| 250 | ShouldDenyAccess(hiddenapi::ApiList::TestApi() | hiddenapi::ApiList::MaxTargetO()), false); |
| 251 | ASSERT_EQ( |
| 252 | ShouldDenyAccess(hiddenapi::ApiList::TestApi() | hiddenapi::ApiList::Blocked()), false); |
| 253 | |
| 254 | // A second case where we want to allow access to TestApis. |
| 255 | runtime_->SetTestApiEnforcementPolicy(hiddenapi::EnforcementPolicy::kEnabled); |
| 256 | setChangeIdState(kAllowTestApiAccess, true); |
| Artur Satayev | 267366c | 2019-10-31 14:59:26 +0000 | [diff] [blame] | 257 | ASSERT_EQ( |
| Andrei Onea | fc12a6c | 2020-07-29 19:52:34 +0100 | [diff] [blame] | 258 | ShouldDenyAccess(hiddenapi::ApiList::TestApi() | hiddenapi::ApiList::Sdk()), false); |
| Artur Satayev | 267366c | 2019-10-31 14:59:26 +0000 | [diff] [blame] | 259 | ASSERT_EQ( |
| Andrei Onea | fc12a6c | 2020-07-29 19:52:34 +0100 | [diff] [blame] | 260 | ShouldDenyAccess(hiddenapi::ApiList::TestApi() | hiddenapi::ApiList::Unsupported()), false); |
| Artur Satayev | 267366c | 2019-10-31 14:59:26 +0000 | [diff] [blame] | 261 | ASSERT_EQ( |
| Andrei Onea | fc12a6c | 2020-07-29 19:52:34 +0100 | [diff] [blame] | 262 | ShouldDenyAccess(hiddenapi::ApiList::TestApi() | hiddenapi::ApiList::MaxTargetR()), false); |
| Artur Satayev | b708fc1 | 2020-05-20 17:48:19 +0100 | [diff] [blame] | 263 | ASSERT_EQ( |
| Andrei Onea | fc12a6c | 2020-07-29 19:52:34 +0100 | [diff] [blame] | 264 | ShouldDenyAccess(hiddenapi::ApiList::TestApi() | hiddenapi::ApiList::MaxTargetQ()), false); |
| Artur Satayev | 267366c | 2019-10-31 14:59:26 +0000 | [diff] [blame] | 265 | ASSERT_EQ( |
| Andrei Onea | fc12a6c | 2020-07-29 19:52:34 +0100 | [diff] [blame] | 266 | ShouldDenyAccess(hiddenapi::ApiList::TestApi() | hiddenapi::ApiList::MaxTargetP()), false); |
| Artur Satayev | 267366c | 2019-10-31 14:59:26 +0000 | [diff] [blame] | 267 | ASSERT_EQ( |
| Andrei Onea | fc12a6c | 2020-07-29 19:52:34 +0100 | [diff] [blame] | 268 | ShouldDenyAccess(hiddenapi::ApiList::TestApi() | hiddenapi::ApiList::MaxTargetO()), false); |
| Artur Satayev | 267366c | 2019-10-31 14:59:26 +0000 | [diff] [blame] | 269 | ASSERT_EQ( |
| Andrei Onea | fc12a6c | 2020-07-29 19:52:34 +0100 | [diff] [blame] | 270 | ShouldDenyAccess(hiddenapi::ApiList::TestApi() | hiddenapi::ApiList::Blocked()), false); |
| Artur Satayev | 267366c | 2019-10-31 14:59:26 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 273 | TEST_F(HiddenApiTest, CheckMembersRead) { |
| 274 | ASSERT_NE(nullptr, class1_field1_); |
| 275 | ASSERT_NE(nullptr, class1_field12_); |
| 276 | ASSERT_NE(nullptr, class1_init_); |
| 277 | ASSERT_NE(nullptr, class1_method1_); |
| 278 | ASSERT_NE(nullptr, class1_method1_i_); |
| 279 | ASSERT_NE(nullptr, class1_method12_); |
| 280 | ASSERT_NE(nullptr, class12_field1_); |
| 281 | ASSERT_NE(nullptr, class12_method1_); |
| 282 | ASSERT_NE(nullptr, class2_field1_); |
| 283 | ASSERT_NE(nullptr, class2_method1_); |
| 284 | ASSERT_NE(nullptr, class2_method1_i_); |
| 285 | ASSERT_NE(nullptr, class3_field1_); |
| 286 | ASSERT_NE(nullptr, class3_method1_); |
| 287 | ASSERT_NE(nullptr, class3_method1_i_); |
| 288 | } |
| 289 | |
| 290 | TEST_F(HiddenApiTest, CheckEverythingMatchesL) { |
| 291 | ScopedObjectAccess soa(self_); |
| 292 | std::string prefix("L"); |
| Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 293 | ASSERT_TRUE(MemberSignature(class1_field1_).DoesPrefixMatch(prefix)); |
| 294 | ASSERT_TRUE(MemberSignature(class1_field12_).DoesPrefixMatch(prefix)); |
| 295 | ASSERT_TRUE(MemberSignature(class1_init_).DoesPrefixMatch(prefix)); |
| 296 | ASSERT_TRUE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix)); |
| 297 | ASSERT_TRUE(MemberSignature(class1_method1_i_).DoesPrefixMatch(prefix)); |
| 298 | ASSERT_TRUE(MemberSignature(class12_field1_).DoesPrefixMatch(prefix)); |
| 299 | ASSERT_TRUE(MemberSignature(class12_method1_).DoesPrefixMatch(prefix)); |
| 300 | ASSERT_TRUE(MemberSignature(class1_method12_).DoesPrefixMatch(prefix)); |
| 301 | ASSERT_TRUE(MemberSignature(class2_field1_).DoesPrefixMatch(prefix)); |
| 302 | ASSERT_TRUE(MemberSignature(class2_method1_).DoesPrefixMatch(prefix)); |
| 303 | ASSERT_TRUE(MemberSignature(class2_method1_i_).DoesPrefixMatch(prefix)); |
| 304 | ASSERT_TRUE(MemberSignature(class3_field1_).DoesPrefixMatch(prefix)); |
| 305 | ASSERT_TRUE(MemberSignature(class3_method1_).DoesPrefixMatch(prefix)); |
| 306 | ASSERT_TRUE(MemberSignature(class3_method1_i_).DoesPrefixMatch(prefix)); |
| Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | TEST_F(HiddenApiTest, CheckPackageMatch) { |
| 310 | ScopedObjectAccess soa(self_); |
| 311 | std::string prefix("Lmypackage/packagea/"); |
| Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 312 | ASSERT_TRUE(MemberSignature(class1_field1_).DoesPrefixMatch(prefix)); |
| 313 | ASSERT_TRUE(MemberSignature(class1_field12_).DoesPrefixMatch(prefix)); |
| 314 | ASSERT_TRUE(MemberSignature(class1_init_).DoesPrefixMatch(prefix)); |
| 315 | ASSERT_TRUE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix)); |
| 316 | ASSERT_TRUE(MemberSignature(class1_method1_i_).DoesPrefixMatch(prefix)); |
| 317 | ASSERT_TRUE(MemberSignature(class1_method12_).DoesPrefixMatch(prefix)); |
| 318 | ASSERT_TRUE(MemberSignature(class12_field1_).DoesPrefixMatch(prefix)); |
| 319 | ASSERT_TRUE(MemberSignature(class12_method1_).DoesPrefixMatch(prefix)); |
| 320 | ASSERT_TRUE(MemberSignature(class2_field1_).DoesPrefixMatch(prefix)); |
| 321 | ASSERT_TRUE(MemberSignature(class2_method1_).DoesPrefixMatch(prefix)); |
| 322 | ASSERT_TRUE(MemberSignature(class2_method1_i_).DoesPrefixMatch(prefix)); |
| 323 | ASSERT_FALSE(MemberSignature(class3_field1_).DoesPrefixMatch(prefix)); |
| 324 | ASSERT_FALSE(MemberSignature(class3_method1_).DoesPrefixMatch(prefix)); |
| 325 | ASSERT_FALSE(MemberSignature(class3_method1_i_).DoesPrefixMatch(prefix)); |
| Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | TEST_F(HiddenApiTest, CheckClassMatch) { |
| 329 | ScopedObjectAccess soa(self_); |
| 330 | std::string prefix("Lmypackage/packagea/Class1"); |
| Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 331 | ASSERT_TRUE(MemberSignature(class1_field1_).DoesPrefixMatch(prefix)); |
| 332 | ASSERT_TRUE(MemberSignature(class1_field12_).DoesPrefixMatch(prefix)); |
| 333 | ASSERT_TRUE(MemberSignature(class1_init_).DoesPrefixMatch(prefix)); |
| 334 | ASSERT_TRUE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix)); |
| 335 | ASSERT_TRUE(MemberSignature(class1_method1_i_).DoesPrefixMatch(prefix)); |
| 336 | ASSERT_TRUE(MemberSignature(class1_method12_).DoesPrefixMatch(prefix)); |
| 337 | ASSERT_TRUE(MemberSignature(class12_field1_).DoesPrefixMatch(prefix)); |
| 338 | ASSERT_TRUE(MemberSignature(class12_method1_).DoesPrefixMatch(prefix)); |
| 339 | ASSERT_FALSE(MemberSignature(class2_field1_).DoesPrefixMatch(prefix)); |
| 340 | ASSERT_FALSE(MemberSignature(class2_method1_).DoesPrefixMatch(prefix)); |
| 341 | ASSERT_FALSE(MemberSignature(class2_method1_i_).DoesPrefixMatch(prefix)); |
| Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | TEST_F(HiddenApiTest, CheckClassExactMatch) { |
| 345 | ScopedObjectAccess soa(self_); |
| 346 | std::string prefix("Lmypackage/packagea/Class1;"); |
| Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 347 | ASSERT_TRUE(MemberSignature(class1_field1_).DoesPrefixMatch(prefix)); |
| 348 | ASSERT_TRUE(MemberSignature(class1_field12_).DoesPrefixMatch(prefix)); |
| 349 | ASSERT_TRUE(MemberSignature(class1_init_).DoesPrefixMatch(prefix)); |
| 350 | ASSERT_TRUE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix)); |
| 351 | ASSERT_TRUE(MemberSignature(class1_method1_i_).DoesPrefixMatch(prefix)); |
| 352 | ASSERT_FALSE(MemberSignature(class12_field1_).DoesPrefixMatch(prefix)); |
| 353 | ASSERT_FALSE(MemberSignature(class12_method1_).DoesPrefixMatch(prefix)); |
| 354 | ASSERT_FALSE(MemberSignature(class2_field1_).DoesPrefixMatch(prefix)); |
| 355 | ASSERT_FALSE(MemberSignature(class2_method1_).DoesPrefixMatch(prefix)); |
| 356 | ASSERT_FALSE(MemberSignature(class2_method1_i_).DoesPrefixMatch(prefix)); |
| Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | TEST_F(HiddenApiTest, CheckMethodMatch) { |
| 360 | ScopedObjectAccess soa(self_); |
| 361 | std::string prefix("Lmypackage/packagea/Class1;->method1"); |
| Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 362 | ASSERT_FALSE(MemberSignature(class1_field1_).DoesPrefixMatch(prefix)); |
| 363 | ASSERT_FALSE(MemberSignature(class1_field12_).DoesPrefixMatch(prefix)); |
| 364 | ASSERT_FALSE(MemberSignature(class1_init_).DoesPrefixMatch(prefix)); |
| 365 | ASSERT_TRUE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix)); |
| 366 | ASSERT_TRUE(MemberSignature(class1_method1_i_).DoesPrefixMatch(prefix)); |
| 367 | ASSERT_TRUE(MemberSignature(class1_method12_).DoesPrefixMatch(prefix)); |
| 368 | ASSERT_FALSE(MemberSignature(class12_field1_).DoesPrefixMatch(prefix)); |
| 369 | ASSERT_FALSE(MemberSignature(class12_method1_).DoesPrefixMatch(prefix)); |
| Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | TEST_F(HiddenApiTest, CheckMethodExactMatch) { |
| 373 | ScopedObjectAccess soa(self_); |
| 374 | std::string prefix("Lmypackage/packagea/Class1;->method1("); |
| Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 375 | ASSERT_FALSE(MemberSignature(class1_field1_).DoesPrefixMatch(prefix)); |
| 376 | ASSERT_FALSE(MemberSignature(class1_field12_).DoesPrefixMatch(prefix)); |
| 377 | ASSERT_FALSE(MemberSignature(class1_init_).DoesPrefixMatch(prefix)); |
| 378 | ASSERT_TRUE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix)); |
| 379 | ASSERT_TRUE(MemberSignature(class1_method1_i_).DoesPrefixMatch(prefix)); |
| 380 | ASSERT_FALSE(MemberSignature(class1_method12_).DoesPrefixMatch(prefix)); |
| Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | TEST_F(HiddenApiTest, CheckMethodSignatureMatch) { |
| 384 | ScopedObjectAccess soa(self_); |
| 385 | std::string prefix("Lmypackage/packagea/Class1;->method1(I)"); |
| Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 386 | ASSERT_FALSE(MemberSignature(class1_field1_).DoesPrefixMatch(prefix)); |
| 387 | ASSERT_FALSE(MemberSignature(class1_field12_).DoesPrefixMatch(prefix)); |
| 388 | ASSERT_FALSE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix)); |
| 389 | ASSERT_TRUE(MemberSignature(class1_method1_i_).DoesPrefixMatch(prefix)); |
| 390 | ASSERT_FALSE(MemberSignature(class1_method12_).DoesPrefixMatch(prefix)); |
| Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | TEST_F(HiddenApiTest, CheckMethodSignatureAndReturnMatch) { |
| 394 | ScopedObjectAccess soa(self_); |
| 395 | std::string prefix("Lmypackage/packagea/Class1;->method1()V"); |
| Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 396 | ASSERT_FALSE(MemberSignature(class1_field1_).DoesPrefixMatch(prefix)); |
| 397 | ASSERT_FALSE(MemberSignature(class1_field12_).DoesPrefixMatch(prefix)); |
| 398 | ASSERT_TRUE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix)); |
| 399 | ASSERT_FALSE(MemberSignature(class1_method1_i_).DoesPrefixMatch(prefix)); |
| 400 | ASSERT_FALSE(MemberSignature(class1_method12_).DoesPrefixMatch(prefix)); |
| Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | TEST_F(HiddenApiTest, CheckFieldMatch) { |
| 404 | ScopedObjectAccess soa(self_); |
| 405 | std::string prefix("Lmypackage/packagea/Class1;->field1"); |
| Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 406 | ASSERT_TRUE(MemberSignature(class1_field1_).DoesPrefixMatch(prefix)); |
| 407 | ASSERT_TRUE(MemberSignature(class1_field12_).DoesPrefixMatch(prefix)); |
| 408 | ASSERT_FALSE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix)); |
| 409 | ASSERT_FALSE(MemberSignature(class1_method1_i_).DoesPrefixMatch(prefix)); |
| 410 | ASSERT_FALSE(MemberSignature(class1_method12_).DoesPrefixMatch(prefix)); |
| Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | TEST_F(HiddenApiTest, CheckFieldExactMatch) { |
| 414 | ScopedObjectAccess soa(self_); |
| 415 | std::string prefix("Lmypackage/packagea/Class1;->field1:"); |
| Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 416 | ASSERT_TRUE(MemberSignature(class1_field1_).DoesPrefixMatch(prefix)); |
| 417 | ASSERT_FALSE(MemberSignature(class1_field12_).DoesPrefixMatch(prefix)); |
| 418 | ASSERT_FALSE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix)); |
| Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | TEST_F(HiddenApiTest, CheckFieldTypeMatch) { |
| 422 | ScopedObjectAccess soa(self_); |
| 423 | std::string prefix("Lmypackage/packagea/Class1;->field1:I"); |
| Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 424 | ASSERT_TRUE(MemberSignature(class1_field1_).DoesPrefixMatch(prefix)); |
| 425 | ASSERT_FALSE(MemberSignature(class1_field12_).DoesPrefixMatch(prefix)); |
| 426 | ASSERT_FALSE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix)); |
| Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | TEST_F(HiddenApiTest, CheckConstructorMatch) { |
| 430 | ScopedObjectAccess soa(self_); |
| 431 | std::string prefix("Lmypackage/packagea/Class1;-><init>"); |
| Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 432 | ASSERT_TRUE(MemberSignature(class1_init_).DoesPrefixMatch(prefix)); |
| 433 | ASSERT_FALSE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix)); |
| Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | TEST_F(HiddenApiTest, CheckConstructorExactMatch) { |
| 437 | ScopedObjectAccess soa(self_); |
| 438 | std::string prefix("Lmypackage/packagea/Class1;-><init>()V"); |
| Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 439 | ASSERT_TRUE(MemberSignature(class1_init_).DoesPrefixMatch(prefix)); |
| 440 | ASSERT_FALSE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix)); |
| Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | TEST_F(HiddenApiTest, CheckMethodSignatureTrailingCharsNoMatch) { |
| 444 | ScopedObjectAccess soa(self_); |
| 445 | std::string prefix("Lmypackage/packagea/Class1;->method1()Vfoo"); |
| Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 446 | ASSERT_FALSE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix)); |
| Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | TEST_F(HiddenApiTest, CheckConstructorTrailingCharsNoMatch) { |
| 450 | ScopedObjectAccess soa(self_); |
| 451 | std::string prefix("Lmypackage/packagea/Class1;-><init>()Vfoo"); |
| Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 452 | ASSERT_FALSE(MemberSignature(class1_init_).DoesPrefixMatch(prefix)); |
| Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | TEST_F(HiddenApiTest, CheckFieldTrailingCharsNoMatch) { |
| 456 | ScopedObjectAccess soa(self_); |
| 457 | std::string prefix("Lmypackage/packagea/Class1;->field1:Ifoo"); |
| Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 458 | ASSERT_FALSE(MemberSignature(class1_field1_).DoesPrefixMatch(prefix)); |
| Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 459 | } |
| 460 | |
| David Brazdil | 1f9d3c3 | 2018-05-02 16:53:06 +0100 | [diff] [blame] | 461 | TEST_F(HiddenApiTest, CheckMemberSignatureForProxyClass) { |
| 462 | ScopedObjectAccess soa(self_); |
| 463 | StackHandleScope<4> hs(soa.Self()); |
| 464 | Handle<mirror::ClassLoader> class_loader( |
| 465 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader_))); |
| 466 | |
| 467 | // Find interface we will create a proxy for. |
| 468 | Handle<mirror::Class> h_iface(hs.NewHandle( |
| 469 | class_linker_->FindClass(soa.Self(), "Lmypackage/packagea/Interface;", class_loader))); |
| 470 | ASSERT_TRUE(h_iface != nullptr); |
| 471 | |
| 472 | // Create the proxy class. |
| Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 473 | std::vector<Handle<mirror::Class>> interfaces; |
| 474 | interfaces.push_back(h_iface); |
| David Brazdil | 1f9d3c3 | 2018-05-02 16:53:06 +0100 | [diff] [blame] | 475 | Handle<mirror::Class> proxyClass = hs.NewHandle(proxy_test::GenerateProxyClass( |
| 476 | soa, jclass_loader_, runtime_->GetClassLinker(), "$Proxy1234", interfaces)); |
| 477 | ASSERT_TRUE(proxyClass != nullptr); |
| 478 | ASSERT_TRUE(proxyClass->IsProxyClass()); |
| 479 | ASSERT_TRUE(proxyClass->IsInitialized()); |
| 480 | |
| 481 | // Find the "method" virtual method. |
| 482 | ArtMethod* method = nullptr; |
| 483 | for (auto& m : proxyClass->GetDeclaredVirtualMethods(kRuntimePointerSize)) { |
| 484 | if (strcmp("method", m.GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetName()) == 0) { |
| 485 | method = &m; |
| 486 | break; |
| 487 | } |
| 488 | } |
| 489 | ASSERT_TRUE(method != nullptr); |
| 490 | |
| 491 | // Find the "interfaces" static field. This is generated for all proxies. |
| 492 | ArtField* field = nullptr; |
| 493 | for (size_t i = 0; i < proxyClass->NumStaticFields(); ++i) { |
| 494 | ArtField* f = proxyClass->GetStaticField(i); |
| 495 | if (strcmp("interfaces", f->GetName()) == 0) { |
| 496 | field = f; |
| 497 | break; |
| 498 | } |
| 499 | } |
| 500 | ASSERT_TRUE(field != nullptr); |
| 501 | |
| 502 | // Test the signature. We expect the signature from the interface class. |
| 503 | std::ostringstream ss_method; |
| David Brazdil | 6a1dab4 | 2019-02-28 18:45:15 +0000 | [diff] [blame] | 504 | MemberSignature(method->GetInterfaceMethodIfProxy(kRuntimePointerSize)).Dump(ss_method); |
| David Brazdil | 1f9d3c3 | 2018-05-02 16:53:06 +0100 | [diff] [blame] | 505 | ASSERT_EQ("Lmypackage/packagea/Interface;->method()V", ss_method.str()); |
| 506 | |
| 507 | // Test the signature. We expect the signature of the proxy class. |
| 508 | std::ostringstream ss_field; |
| 509 | MemberSignature(field).Dump(ss_field); |
| 510 | ASSERT_EQ("L$Proxy1234;->interfaces:[Ljava/lang/Class;", ss_field.str()); |
| 511 | } |
| 512 | |
| David Brazdil | a5c3a80 | 2019-03-08 14:59:41 +0000 | [diff] [blame] | 513 | static bool Copy(const std::string& src, const std::string& dst, /*out*/ std::string* error_msg) { |
| 514 | std::ifstream src_stream(src, std::ios::binary); |
| 515 | std::ofstream dst_stream(dst, std::ios::binary); |
| 516 | dst_stream << src_stream.rdbuf(); |
| 517 | src_stream.close(); |
| 518 | dst_stream.close(); |
| 519 | if (src_stream.good() && dst_stream.good()) { |
| 520 | return true; |
| 521 | } else { |
| 522 | *error_msg = "Copy " + src + " => " + dst + " (src_good=" |
| 523 | + (src_stream.good() ? "true" : "false") + ", dst_good=" |
| 524 | + (dst_stream.good() ? "true" : "false") + ")"; |
| 525 | return false; |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | static bool LoadDexFiles(const std::string& path, |
| 530 | ScopedObjectAccess& soa, |
| 531 | /* out */ std::vector<std::unique_ptr<const DexFile>>* dex_files, |
| 532 | /* out */ ObjPtr<mirror::ClassLoader>* class_loader, |
| 533 | /* out */ std::string* error_msg) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 534 | if (!ArtDexFileLoader().Open(path.c_str(), |
| 535 | path, |
| 536 | /* verify= */ true, |
| 537 | /* verify_checksum= */ true, |
| 538 | error_msg, |
| 539 | dex_files)) { |
| 540 | return false; |
| 541 | } |
| 542 | |
| 543 | ClassLinker* const linker = Runtime::Current()->GetClassLinker(); |
| 544 | |
| 545 | StackHandleScope<2> hs(soa.Self()); |
| 546 | Handle<mirror::Class> h_class = hs.NewHandle(soa.Decode<mirror::Class>( |
| 547 | WellKnownClasses::dalvik_system_PathClassLoader)); |
| 548 | Handle<mirror::ClassLoader> h_loader = hs.NewHandle(linker->CreateWellKnownClassLoader( |
| 549 | soa.Self(), |
| 550 | MakeNonOwningPointerVector(*dex_files), |
| 551 | h_class, |
| 552 | /* parent_loader= */ ScopedNullHandle<mirror::ClassLoader>(), |
| 553 | /* shared_libraries= */ ScopedNullHandle<mirror::ObjectArray<mirror::ClassLoader>>())); |
| 554 | for (const auto& dex_file : *dex_files) { |
| 555 | linker->RegisterDexFile(*dex_file.get(), h_loader.Get()); |
| 556 | } |
| 557 | |
| 558 | *class_loader = h_loader.Get(); |
| 559 | return true; |
| 560 | } |
| 561 | |
| Orion Hodson | 9735ccc | 2020-07-20 13:05:04 +0100 | [diff] [blame] | 562 | static bool Remove(const std::string& path, /*out*/ std::string* error_msg) { |
| 563 | if (TEMP_FAILURE_RETRY(remove(path.c_str())) == 0) { |
| 564 | return true; |
| 565 | } |
| 566 | *error_msg = StringPrintf("Unable to remove(\"%s\"): %s", path.c_str(), strerror(errno)); |
| 567 | return false; |
| 568 | } |
| 569 | |
| David Brazdil | a5c3a80 | 2019-03-08 14:59:41 +0000 | [diff] [blame] | 570 | static bool CheckAllDexFilesInDomain(ObjPtr<mirror::ClassLoader> loader, |
| 571 | const std::vector<std::unique_ptr<const DexFile>>& dex_files, |
| David Brazdil | bfaba28 | 2019-03-15 11:35:51 +0000 | [diff] [blame] | 572 | hiddenapi::Domain expected_domain, |
| 573 | /* out */ std::string* error_msg) |
| David Brazdil | a5c3a80 | 2019-03-08 14:59:41 +0000 | [diff] [blame] | 574 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 575 | for (const auto& dex_file : dex_files) { |
| 576 | hiddenapi::AccessContext context(loader, dex_file.get()); |
| 577 | if (context.GetDomain() != expected_domain) { |
| David Brazdil | bfaba28 | 2019-03-15 11:35:51 +0000 | [diff] [blame] | 578 | std::stringstream ss; |
| 579 | ss << dex_file->GetLocation() << ": access context domain does not match " |
| David Brazdil | a5c3a80 | 2019-03-08 14:59:41 +0000 | [diff] [blame] | 580 | << "(expected=" << static_cast<uint32_t>(expected_domain) |
| 581 | << ", actual=" << static_cast<uint32_t>(context.GetDomain()) << ")"; |
| David Brazdil | bfaba28 | 2019-03-15 11:35:51 +0000 | [diff] [blame] | 582 | *error_msg = ss.str(); |
| David Brazdil | a5c3a80 | 2019-03-08 14:59:41 +0000 | [diff] [blame] | 583 | return false; |
| 584 | } |
| 585 | if (dex_file->GetHiddenapiDomain() != expected_domain) { |
| David Brazdil | bfaba28 | 2019-03-15 11:35:51 +0000 | [diff] [blame] | 586 | std::stringstream ss; |
| 587 | ss << dex_file->GetLocation() << ": dex file domain does not match " |
| David Brazdil | a5c3a80 | 2019-03-08 14:59:41 +0000 | [diff] [blame] | 588 | << "(expected=" << static_cast<uint32_t>(expected_domain) |
| 589 | << ", actual=" << static_cast<uint32_t>(dex_file->GetHiddenapiDomain()) << ")"; |
| David Brazdil | bfaba28 | 2019-03-15 11:35:51 +0000 | [diff] [blame] | 590 | *error_msg = ss.str(); |
| David Brazdil | a5c3a80 | 2019-03-08 14:59:41 +0000 | [diff] [blame] | 591 | return false; |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | return true; |
| 596 | } |
| 597 | |
| 598 | TEST_F(HiddenApiTest, DexDomain_DataDir) { |
| 599 | // Load file from a non-system directory and check that it is not flagged as framework. |
| 600 | std::string data_location_path = android_data_ + "/foo.jar"; |
| 601 | ASSERT_FALSE(LocationIsOnSystemFramework(data_location_path.c_str())); |
| 602 | |
| 603 | ScopedObjectAccess soa(Thread::Current()); |
| 604 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 605 | std::string error_msg; |
| 606 | ObjPtr<mirror::ClassLoader> class_loader; |
| 607 | |
| 608 | ASSERT_TRUE(Copy(GetTestDexFileName("Main"), data_location_path, &error_msg)) << error_msg; |
| 609 | ASSERT_TRUE(LoadDexFiles(data_location_path, soa, &dex_files, &class_loader, &error_msg)) |
| 610 | << error_msg; |
| 611 | ASSERT_GE(dex_files.size(), 1u); |
| David Brazdil | bfaba28 | 2019-03-15 11:35:51 +0000 | [diff] [blame] | 612 | ASSERT_TRUE(CheckAllDexFilesInDomain(class_loader, |
| 613 | dex_files, |
| 614 | hiddenapi::Domain::kApplication, |
| 615 | &error_msg)) << error_msg; |
| David Brazdil | a5c3a80 | 2019-03-08 14:59:41 +0000 | [diff] [blame] | 616 | |
| 617 | dex_files.clear(); |
| Orion Hodson | 9735ccc | 2020-07-20 13:05:04 +0100 | [diff] [blame] | 618 | ASSERT_TRUE(Remove(data_location_path, &error_msg)) << error_msg; |
| David Brazdil | a5c3a80 | 2019-03-08 14:59:41 +0000 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | TEST_F(HiddenApiTest, DexDomain_SystemDir) { |
| 622 | // Load file from a system, non-framework directory and check that it is not flagged as framework. |
| 623 | std::string system_location_path = GetAndroidRoot() + "/foo.jar"; |
| 624 | ASSERT_FALSE(LocationIsOnSystemFramework(system_location_path.c_str())); |
| 625 | |
| 626 | ScopedObjectAccess soa(Thread::Current()); |
| 627 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 628 | std::string error_msg; |
| 629 | ObjPtr<mirror::ClassLoader> class_loader; |
| 630 | |
| 631 | ASSERT_TRUE(Copy(GetTestDexFileName("Main"), system_location_path, &error_msg)) << error_msg; |
| 632 | ASSERT_TRUE(LoadDexFiles(system_location_path, soa, &dex_files, &class_loader, &error_msg)) |
| 633 | << error_msg; |
| 634 | ASSERT_GE(dex_files.size(), 1u); |
| David Brazdil | bfaba28 | 2019-03-15 11:35:51 +0000 | [diff] [blame] | 635 | ASSERT_TRUE(CheckAllDexFilesInDomain(class_loader, |
| 636 | dex_files, |
| 637 | hiddenapi::Domain::kApplication, |
| 638 | &error_msg)) << error_msg; |
| David Brazdil | a5c3a80 | 2019-03-08 14:59:41 +0000 | [diff] [blame] | 639 | |
| 640 | dex_files.clear(); |
| Orion Hodson | 9735ccc | 2020-07-20 13:05:04 +0100 | [diff] [blame] | 641 | ASSERT_TRUE(Remove(system_location_path, &error_msg)) << error_msg; |
| David Brazdil | a5c3a80 | 2019-03-08 14:59:41 +0000 | [diff] [blame] | 642 | } |
| 643 | |
| Chris Gross | 5477b8e | 2020-04-24 09:36:45 -0700 | [diff] [blame] | 644 | TEST_F(HiddenApiTest, DexDomain_SystemExtDir) { |
| 645 | // Load file from a system_ext, non-framework directory and check that it is not flagged as framework. |
| 646 | std::string system_ext_location_path = android_system_ext_ + "/foo.jar"; |
| 647 | ASSERT_FALSE(LocationIsOnSystemExtFramework(system_ext_location_path.c_str())); |
| 648 | |
| 649 | ScopedObjectAccess soa(Thread::Current()); |
| 650 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 651 | std::string error_msg; |
| 652 | ObjPtr<mirror::ClassLoader> class_loader; |
| 653 | |
| 654 | ASSERT_TRUE(Copy(GetTestDexFileName("Main"), system_ext_location_path, &error_msg)) << error_msg; |
| 655 | ASSERT_TRUE(LoadDexFiles(system_ext_location_path, soa, &dex_files, &class_loader, &error_msg)) |
| 656 | << error_msg; |
| 657 | ASSERT_GE(dex_files.size(), 1u); |
| 658 | ASSERT_TRUE(CheckAllDexFilesInDomain(class_loader, |
| 659 | dex_files, |
| 660 | hiddenapi::Domain::kApplication, |
| 661 | &error_msg)) << error_msg; |
| 662 | |
| 663 | dex_files.clear(); |
| Orion Hodson | 9735ccc | 2020-07-20 13:05:04 +0100 | [diff] [blame] | 664 | ASSERT_TRUE(Remove(system_ext_location_path, &error_msg)) << error_msg; |
| Chris Gross | 5477b8e | 2020-04-24 09:36:45 -0700 | [diff] [blame] | 665 | } |
| 666 | |
| David Brazdil | a5c3a80 | 2019-03-08 14:59:41 +0000 | [diff] [blame] | 667 | TEST_F(HiddenApiTest, DexDomain_SystemFrameworkDir) { |
| 668 | // Load file from a system/framework directory and check that it is flagged as a framework dex. |
| David Srbecky | 7400a54 | 2020-07-09 13:40:57 +0100 | [diff] [blame] | 669 | std::filesystem::create_directory(GetAndroidRoot() + "/framework"); |
| David Brazdil | a5c3a80 | 2019-03-08 14:59:41 +0000 | [diff] [blame] | 670 | std::string system_framework_location_path = GetAndroidRoot() + "/framework/foo.jar"; |
| 671 | ASSERT_TRUE(LocationIsOnSystemFramework(system_framework_location_path.c_str())); |
| 672 | |
| 673 | ScopedObjectAccess soa(Thread::Current()); |
| 674 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 675 | std::string error_msg; |
| 676 | ObjPtr<mirror::ClassLoader> class_loader; |
| 677 | |
| 678 | ASSERT_TRUE(Copy(GetTestDexFileName("Main"), system_framework_location_path, &error_msg)) |
| 679 | << error_msg; |
| 680 | ASSERT_TRUE(LoadDexFiles(system_framework_location_path, |
| 681 | soa, |
| 682 | &dex_files, |
| 683 | &class_loader, |
| 684 | &error_msg)) << error_msg; |
| 685 | ASSERT_GE(dex_files.size(), 1u); |
| David Brazdil | bfaba28 | 2019-03-15 11:35:51 +0000 | [diff] [blame] | 686 | ASSERT_TRUE(CheckAllDexFilesInDomain(class_loader, |
| 687 | dex_files, |
| 688 | hiddenapi::Domain::kPlatform, |
| 689 | &error_msg)) << error_msg; |
| David Brazdil | a5c3a80 | 2019-03-08 14:59:41 +0000 | [diff] [blame] | 690 | |
| 691 | dex_files.clear(); |
| Orion Hodson | 9735ccc | 2020-07-20 13:05:04 +0100 | [diff] [blame] | 692 | ASSERT_TRUE(Remove(system_framework_location_path, &error_msg)) << error_msg; |
| David Brazdil | a5c3a80 | 2019-03-08 14:59:41 +0000 | [diff] [blame] | 693 | } |
| 694 | |
| Chris Gross | 5477b8e | 2020-04-24 09:36:45 -0700 | [diff] [blame] | 695 | TEST_F(HiddenApiTest, DexDomain_SystemExtFrameworkDir) { |
| 696 | // Load file from a system_ext/framework directory and check that it is flagged as a framework dex. |
| 697 | std::string system_ext_framework_location_path = android_system_ext_ + "/framework/foo.jar"; |
| 698 | ASSERT_TRUE(LocationIsOnSystemExtFramework(system_ext_framework_location_path.c_str())); |
| 699 | |
| 700 | ScopedObjectAccess soa(Thread::Current()); |
| 701 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 702 | std::string error_msg; |
| 703 | ObjPtr<mirror::ClassLoader> class_loader; |
| 704 | |
| 705 | ASSERT_TRUE(Copy(GetTestDexFileName("Main"), system_ext_framework_location_path, &error_msg)) |
| 706 | << error_msg; |
| 707 | ASSERT_TRUE(LoadDexFiles(system_ext_framework_location_path, |
| 708 | soa, |
| 709 | &dex_files, |
| 710 | &class_loader, |
| 711 | &error_msg)) << error_msg; |
| 712 | ASSERT_GE(dex_files.size(), 1u); |
| 713 | ASSERT_TRUE(CheckAllDexFilesInDomain(class_loader, |
| 714 | dex_files, |
| 715 | hiddenapi::Domain::kPlatform, |
| 716 | &error_msg)) << error_msg; |
| 717 | |
| 718 | dex_files.clear(); |
| Orion Hodson | 9735ccc | 2020-07-20 13:05:04 +0100 | [diff] [blame] | 719 | ASSERT_TRUE(Remove(system_ext_framework_location_path, &error_msg)) << error_msg; |
| Chris Gross | 5477b8e | 2020-04-24 09:36:45 -0700 | [diff] [blame] | 720 | } |
| 721 | |
| David Brazdil | a5c3a80 | 2019-03-08 14:59:41 +0000 | [diff] [blame] | 722 | TEST_F(HiddenApiTest, DexDomain_DataDir_MultiDex) { |
| 723 | // Load multidex file from a non-system directory and check that it is not flagged as framework. |
| 724 | std::string data_multi_location_path = android_data_ + "/multifoo.jar"; |
| 725 | ASSERT_FALSE(LocationIsOnSystemFramework(data_multi_location_path.c_str())); |
| 726 | |
| 727 | ScopedObjectAccess soa(Thread::Current()); |
| 728 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 729 | std::string error_msg; |
| 730 | ObjPtr<mirror::ClassLoader> class_loader; |
| 731 | |
| 732 | ASSERT_TRUE(Copy(GetTestDexFileName("MultiDex"), data_multi_location_path, &error_msg)) |
| 733 | << error_msg; |
| 734 | ASSERT_TRUE(LoadDexFiles(data_multi_location_path, soa, &dex_files, &class_loader, &error_msg)) |
| 735 | << error_msg; |
| 736 | ASSERT_GE(dex_files.size(), 1u); |
| David Brazdil | bfaba28 | 2019-03-15 11:35:51 +0000 | [diff] [blame] | 737 | ASSERT_TRUE(CheckAllDexFilesInDomain(class_loader, |
| 738 | dex_files, |
| 739 | hiddenapi::Domain::kApplication, |
| 740 | &error_msg)) << error_msg; |
| David Brazdil | a5c3a80 | 2019-03-08 14:59:41 +0000 | [diff] [blame] | 741 | |
| 742 | dex_files.clear(); |
| Orion Hodson | 9735ccc | 2020-07-20 13:05:04 +0100 | [diff] [blame] | 743 | ASSERT_TRUE(Remove(data_multi_location_path, &error_msg)) << error_msg; |
| David Brazdil | a5c3a80 | 2019-03-08 14:59:41 +0000 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | TEST_F(HiddenApiTest, DexDomain_SystemDir_MultiDex) { |
| 747 | // Load multidex file from a system, non-framework directory and check that it is not flagged |
| 748 | // as framework. |
| 749 | std::string system_multi_location_path = GetAndroidRoot() + "/multifoo.jar"; |
| 750 | ASSERT_FALSE(LocationIsOnSystemFramework(system_multi_location_path.c_str())); |
| 751 | |
| 752 | ScopedObjectAccess soa(Thread::Current()); |
| 753 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 754 | std::string error_msg; |
| 755 | ObjPtr<mirror::ClassLoader> class_loader; |
| 756 | |
| 757 | ASSERT_TRUE(Copy(GetTestDexFileName("MultiDex"), system_multi_location_path, &error_msg)) |
| 758 | << error_msg; |
| 759 | ASSERT_TRUE(LoadDexFiles(system_multi_location_path, soa, &dex_files, &class_loader, &error_msg)) |
| 760 | << error_msg; |
| 761 | ASSERT_GT(dex_files.size(), 1u); |
| David Brazdil | bfaba28 | 2019-03-15 11:35:51 +0000 | [diff] [blame] | 762 | ASSERT_TRUE(CheckAllDexFilesInDomain(class_loader, |
| 763 | dex_files, |
| 764 | hiddenapi::Domain::kApplication, |
| 765 | &error_msg)) << error_msg; |
| David Brazdil | a5c3a80 | 2019-03-08 14:59:41 +0000 | [diff] [blame] | 766 | |
| 767 | dex_files.clear(); |
| Orion Hodson | 9735ccc | 2020-07-20 13:05:04 +0100 | [diff] [blame] | 768 | ASSERT_TRUE(Remove(system_multi_location_path, &error_msg)) << error_msg; |
| David Brazdil | a5c3a80 | 2019-03-08 14:59:41 +0000 | [diff] [blame] | 769 | } |
| 770 | |
| Chris Gross | 5477b8e | 2020-04-24 09:36:45 -0700 | [diff] [blame] | 771 | TEST_F(HiddenApiTest, DexDomain_SystemExtDir_MultiDex) { |
| 772 | // Load multidex file from a system_ext, non-framework directory and check that it is not flagged |
| 773 | // as framework. |
| 774 | std::string system_ext_multi_location_path = android_system_ext_ + "/multifoo.jar"; |
| 775 | ASSERT_FALSE(LocationIsOnSystemExtFramework(system_ext_multi_location_path.c_str())); |
| 776 | |
| 777 | ScopedObjectAccess soa(Thread::Current()); |
| 778 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 779 | std::string error_msg; |
| 780 | ObjPtr<mirror::ClassLoader> class_loader; |
| 781 | |
| 782 | ASSERT_TRUE(Copy(GetTestDexFileName("MultiDex"), system_ext_multi_location_path, &error_msg)) |
| 783 | << error_msg; |
| 784 | ASSERT_TRUE(LoadDexFiles(system_ext_multi_location_path, soa, &dex_files, &class_loader, &error_msg)) |
| 785 | << error_msg; |
| 786 | ASSERT_GT(dex_files.size(), 1u); |
| 787 | ASSERT_TRUE(CheckAllDexFilesInDomain(class_loader, |
| 788 | dex_files, |
| 789 | hiddenapi::Domain::kApplication, |
| 790 | &error_msg)) << error_msg; |
| 791 | |
| 792 | dex_files.clear(); |
| Orion Hodson | 9735ccc | 2020-07-20 13:05:04 +0100 | [diff] [blame] | 793 | ASSERT_TRUE(Remove(system_ext_multi_location_path, &error_msg)) << error_msg; |
| Chris Gross | 5477b8e | 2020-04-24 09:36:45 -0700 | [diff] [blame] | 794 | } |
| 795 | |
| David Brazdil | a5c3a80 | 2019-03-08 14:59:41 +0000 | [diff] [blame] | 796 | TEST_F(HiddenApiTest, DexDomain_SystemFrameworkDir_MultiDex) { |
| 797 | // Load multidex file from a system/framework directory and check that it is flagged as a |
| 798 | // framework dex. |
| 799 | std::string system_framework_multi_location_path = GetAndroidRoot() + "/framework/multifoo.jar"; |
| 800 | ASSERT_TRUE(LocationIsOnSystemFramework(system_framework_multi_location_path.c_str())); |
| 801 | |
| 802 | ScopedObjectAccess soa(Thread::Current()); |
| 803 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 804 | std::string error_msg; |
| 805 | ObjPtr<mirror::ClassLoader> class_loader; |
| 806 | |
| 807 | ASSERT_TRUE(Copy(GetTestDexFileName("MultiDex"), |
| 808 | system_framework_multi_location_path, |
| 809 | &error_msg)) << error_msg; |
| 810 | ASSERT_TRUE(LoadDexFiles(system_framework_multi_location_path, |
| 811 | soa, |
| 812 | &dex_files, |
| 813 | &class_loader, |
| 814 | &error_msg)) << error_msg; |
| 815 | ASSERT_GT(dex_files.size(), 1u); |
| David Brazdil | bfaba28 | 2019-03-15 11:35:51 +0000 | [diff] [blame] | 816 | ASSERT_TRUE(CheckAllDexFilesInDomain(class_loader, |
| 817 | dex_files, |
| 818 | hiddenapi::Domain::kPlatform, |
| 819 | &error_msg)) << error_msg; |
| David Brazdil | a5c3a80 | 2019-03-08 14:59:41 +0000 | [diff] [blame] | 820 | |
| 821 | dex_files.clear(); |
| Orion Hodson | 9735ccc | 2020-07-20 13:05:04 +0100 | [diff] [blame] | 822 | ASSERT_TRUE(Remove(system_framework_multi_location_path, &error_msg)) << error_msg; |
| David Brazdil | a5c3a80 | 2019-03-08 14:59:41 +0000 | [diff] [blame] | 823 | } |
| 824 | |
| Chris Gross | 5477b8e | 2020-04-24 09:36:45 -0700 | [diff] [blame] | 825 | TEST_F(HiddenApiTest, DexDomain_SystemExtFrameworkDir_MultiDex) { |
| 826 | // Load multidex file from a system_ext/framework directory and check that it is flagged as a |
| 827 | // framework dex. |
| 828 | std::string system_ext_framework_multi_location_path = android_system_ext_ + "/framework/multifoo.jar"; |
| 829 | ASSERT_TRUE(LocationIsOnSystemExtFramework(system_ext_framework_multi_location_path.c_str())); |
| 830 | |
| 831 | ScopedObjectAccess soa(Thread::Current()); |
| 832 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 833 | std::string error_msg; |
| 834 | ObjPtr<mirror::ClassLoader> class_loader; |
| 835 | |
| 836 | ASSERT_TRUE(Copy(GetTestDexFileName("MultiDex"), |
| 837 | system_ext_framework_multi_location_path, |
| 838 | &error_msg)) << error_msg; |
| 839 | ASSERT_TRUE(LoadDexFiles(system_ext_framework_multi_location_path, |
| 840 | soa, |
| 841 | &dex_files, |
| 842 | &class_loader, |
| 843 | &error_msg)) << error_msg; |
| 844 | ASSERT_GT(dex_files.size(), 1u); |
| 845 | ASSERT_TRUE(CheckAllDexFilesInDomain(class_loader, |
| 846 | dex_files, |
| 847 | hiddenapi::Domain::kPlatform, |
| 848 | &error_msg)) << error_msg; |
| 849 | |
| 850 | dex_files.clear(); |
| Orion Hodson | 9735ccc | 2020-07-20 13:05:04 +0100 | [diff] [blame] | 851 | ASSERT_TRUE(Remove(system_ext_framework_multi_location_path, &error_msg)) << error_msg; |
| Chris Gross | 5477b8e | 2020-04-24 09:36:45 -0700 | [diff] [blame] | 852 | } |
| 853 | |
| Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 854 | } // namespace art |