blob: f5cd15ef4fa292b66fe777e14e9e512cf58a6b9b [file] [log] [blame]
Mathew Inwood7d74ef52018-03-16 14:18:33 +00001/*
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 Brazdila5c3a802019-03-08 14:59:41 +000019#include <fstream>
David Brazdilbfaba282019-03-15 11:35:51 +000020#include <sstream>
David Brazdila5c3a802019-03-08 14:59:41 +000021
Orion Hodson9735ccc2020-07-20 13:05:04 +010022#include "android-base/stringprintf.h"
David Brazdila5c3a802019-03-08 14:59:41 +000023#include "base/file_utils.h"
David Brazdil2bb2fbd2018-11-13 18:24:26 +000024#include "base/sdk_version.h"
David Brazdila5c3a802019-03-08 14:59:41 +000025#include "base/stl_util.h"
Mathew Inwood7d74ef52018-03-16 14:18:33 +000026#include "common_runtime_test.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010027#include "jni/jni_internal.h"
David Brazdil1f9d3c32018-05-02 16:53:06 +010028#include "proxy_test.h"
David Brazdila5c3a802019-03-08 14:59:41 +000029#include "well_known_classes.h"
Mathew Inwood7d74ef52018-03-16 14:18:33 +000030
31namespace art {
32
Orion Hodson9735ccc2020-07-20 13:05:04 +010033using android::base::StringPrintf;
Andreas Gampeaa120012018-03-28 16:23:24 -070034using hiddenapi::detail::MemberSignature;
David Brazdilf50ac102018-10-17 18:00:06 +010035using hiddenapi::detail::ShouldDenyAccessToMemberImpl;
Andreas Gampeaa120012018-03-28 16:23:24 -070036
Pedro Loureirof7fcb982020-11-24 16:44:12 +000037// 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.
atrost2dea0792020-02-25 20:11:47 +000040static constexpr uint64_t kHideMaxtargetsdkPHiddenApis = 149997251;
41static constexpr uint64_t kHideMaxtargetsdkQHiddenApis = 149994052;
Pedro Loureirof7fcb982020-11-24 16:44:12 +000042static constexpr uint64_t kAllowTestApiAccess = 166236554;
atrost2dea0792020-02-25 20:11:47 +000043
Nicolas Geoffray59c21032021-03-02 13:05:03 +000044
45static bool Copy(const std::string& src, const std::string& dst, /*out*/ std::string* error_msg) {
46 std::ifstream src_stream(src, std::ios::binary);
47 std::ofstream dst_stream(dst, std::ios::binary);
48 dst_stream << src_stream.rdbuf();
49 src_stream.close();
50 dst_stream.close();
51 if (src_stream.good() && dst_stream.good()) {
52 return true;
53 } else {
54 *error_msg = "Copy " + src + " => " + dst + " (src_good="
55 + (src_stream.good() ? "true" : "false") + ", dst_good="
56 + (dst_stream.good() ? "true" : "false") + ")";
57 return false;
58 }
59}
60
61static bool LoadDexFiles(const std::string& path,
62 ScopedObjectAccess& soa,
63 /* out */ std::vector<std::unique_ptr<const DexFile>>* dex_files,
64 /* out */ ObjPtr<mirror::ClassLoader>* class_loader,
65 /* out */ std::string* error_msg) REQUIRES_SHARED(Locks::mutator_lock_) {
66 if (!ArtDexFileLoader().Open(path.c_str(),
67 path,
68 /* verify= */ true,
69 /* verify_checksum= */ true,
70 error_msg,
71 dex_files)) {
72 return false;
73 }
74
75 ClassLinker* const linker = Runtime::Current()->GetClassLinker();
76
77 StackHandleScope<2> hs(soa.Self());
78 Handle<mirror::Class> h_class = hs.NewHandle(soa.Decode<mirror::Class>(
79 WellKnownClasses::dalvik_system_PathClassLoader));
80 Handle<mirror::ClassLoader> h_loader = hs.NewHandle(linker->CreateWellKnownClassLoader(
81 soa.Self(),
82 MakeNonOwningPointerVector(*dex_files),
83 h_class,
84 /* parent_loader= */ ScopedNullHandle<mirror::ClassLoader>(),
Brad Stenning9c924e82021-10-11 19:09:00 -070085 /* shared_libraries= */ ScopedNullHandle<mirror::ObjectArray<mirror::ClassLoader>>(),
86 /* shared_libraries_after= */ ScopedNullHandle<mirror::ObjectArray<mirror::ClassLoader>>()));
Nicolas Geoffray59c21032021-03-02 13:05:03 +000087 for (const auto& dex_file : *dex_files) {
88 linker->RegisterDexFile(*dex_file.get(), h_loader.Get());
89 }
90
91 *class_loader = h_loader.Get();
92 return true;
93}
94
95static bool Remove(const std::string& path, /*out*/ std::string* error_msg) {
96 if (TEMP_FAILURE_RETRY(remove(path.c_str())) == 0) {
97 return true;
98 }
99 *error_msg = StringPrintf("Unable to remove(\"%s\"): %s", path.c_str(), strerror(errno));
100 return false;
101}
102
103static bool CheckAllDexFilesInDomain(ObjPtr<mirror::ClassLoader> loader,
104 const std::vector<std::unique_ptr<const DexFile>>& dex_files,
105 hiddenapi::Domain expected_domain,
106 /* out */ std::string* error_msg)
107 REQUIRES_SHARED(Locks::mutator_lock_) {
108 for (const auto& dex_file : dex_files) {
109 hiddenapi::AccessContext context(loader, dex_file.get());
110 if (context.GetDomain() != expected_domain) {
111 std::stringstream ss;
112 ss << dex_file->GetLocation() << ": access context domain does not match "
113 << "(expected=" << static_cast<uint32_t>(expected_domain)
114 << ", actual=" << static_cast<uint32_t>(context.GetDomain()) << ")";
115 *error_msg = ss.str();
116 return false;
117 }
118 if (dex_file->GetHiddenapiDomain() != expected_domain) {
119 std::stringstream ss;
120 ss << dex_file->GetLocation() << ": dex file domain does not match "
121 << "(expected=" << static_cast<uint32_t>(expected_domain)
122 << ", actual=" << static_cast<uint32_t>(dex_file->GetHiddenapiDomain()) << ")";
123 *error_msg = ss.str();
124 return false;
125 }
126 }
127
128 return true;
129}
130
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000131class HiddenApiTest : public CommonRuntimeTest {
132 protected:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100133 void SetUp() override {
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000134 // Do the normal setup.
135 CommonRuntimeTest::SetUp();
136 self_ = Thread::Current();
137 self_->TransitionFromSuspendedToRunnable();
David Brazdil1f9d3c32018-05-02 16:53:06 +0100138 jclass_loader_ = LoadDex("HiddenApiSignatures");
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000139 bool started = runtime_->Start();
140 CHECK(started);
141
Nicolas Geoffray59c21032021-03-02 13:05:03 +0000142 class1_field1_ = GetArtField("mypackage/packagea/Class1", "field1", "I");
143 class1_field12_ = GetArtField("mypackage/packagea/Class1", "field12", "I");
144 class1_init_ = GetArtMethod("mypackage/packagea/Class1", "<init>", "()V");
145 class1_method1_ = GetArtMethod("mypackage/packagea/Class1", "method1", "()V");
146 class1_method1_i_ = GetArtMethod("mypackage/packagea/Class1", "method1", "(I)V");
147 class1_method12_ = GetArtMethod("mypackage/packagea/Class1", "method12", "()V");
148 class12_field1_ = GetArtField("mypackage/packagea/Class12", "field1", "I");
149 class12_method1_ = GetArtMethod("mypackage/packagea/Class12", "method1", "()V");
150 class2_field1_ = GetArtField("mypackage/packagea/Class2", "field1", "I");
151 class2_method1_ = GetArtMethod("mypackage/packagea/Class2", "method1", "()V");
152 class2_method1_i_ = GetArtMethod("mypackage/packagea/Class2", "method1", "(I)V");
153 class3_field1_ = GetArtField("mypackage/packageb/Class3", "field1", "I");
154 class3_method1_ = GetArtMethod("mypackage/packageb/Class3", "method1", "()V");
155 class3_method1_i_ = GetArtMethod("mypackage/packageb/Class3", "method1", "(I)V");
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000156 }
157
Nicolas Geoffray59c21032021-03-02 13:05:03 +0000158 ArtMethod* GetArtMethod(const char* class_name, const char* name, const char* signature) {
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000159 JNIEnv* env = Thread::Current()->GetJniEnv();
160 jclass klass = env->FindClass(class_name);
161 jmethodID method_id = env->GetMethodID(klass, name, signature);
162 ArtMethod* art_method = jni::DecodeArtMethod(method_id);
163 return art_method;
164 }
165
Nicolas Geoffray59c21032021-03-02 13:05:03 +0000166 ArtField* GetArtField(const char* class_name, const char* name, const char* signature) {
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000167 JNIEnv* env = Thread::Current()->GetJniEnv();
168 jclass klass = env->FindClass(class_name);
169 jfieldID field_id = env->GetFieldID(klass, name, signature);
170 ArtField* art_field = jni::DecodeArtField(field_id);
171 return art_field;
172 }
173
Nicolas Geoffray59c21032021-03-02 13:05:03 +0000174 void SetChangeIdState(uint64_t change, bool enabled) {
Andrei Onea037d2822020-11-19 00:20:04 +0000175 CompatFramework& compat_framework = runtime_->GetCompatFramework();
176 std::set<uint64_t> disabled_changes = compat_framework.GetDisabledCompatChanges();
atrost2dea0792020-02-25 20:11:47 +0000177 if (enabled) {
178 disabled_changes.erase(change);
179 } else {
180 disabled_changes.insert(change);
181 }
Andrei Onea037d2822020-11-19 00:20:04 +0000182 compat_framework.SetDisabledCompatChanges(disabled_changes);
atrost2dea0792020-02-25 20:11:47 +0000183 }
184
David Brazdilf50ac102018-10-17 18:00:06 +0100185 bool ShouldDenyAccess(hiddenapi::ApiList list) REQUIRES_SHARED(Locks::mutator_lock_) {
186 // Choose parameters such that there are no side effects (AccessMethod::kNone)
187 // and that the member is not on the exemptions list (here we choose one which
188 // is not even in boot class path).
189 return ShouldDenyAccessToMemberImpl(/* member= */ class1_field1_,
190 list,
191 /* access_method= */ hiddenapi::AccessMethod::kNone);
192 }
193
Nicolas Geoffray59c21032021-03-02 13:05:03 +0000194 void TestLocation(const std::string& location, hiddenapi::Domain expected_domain) {
195 ScopedObjectAccess soa(Thread::Current());
196 std::vector<std::unique_ptr<const DexFile>> dex_files;
197 std::string error_msg;
198 ObjPtr<mirror::ClassLoader> class_loader;
199
200 ASSERT_TRUE(Copy(GetTestDexFileName("Main"), location, &error_msg)) << error_msg;
201 ASSERT_TRUE(LoadDexFiles(location, soa, &dex_files, &class_loader, &error_msg))
202 << error_msg;
203 ASSERT_GE(dex_files.size(), 1u);
204 ASSERT_TRUE(CheckAllDexFilesInDomain(class_loader,
205 dex_files,
206 expected_domain,
207 &error_msg)) << error_msg;
208
209 dex_files.clear();
210 ASSERT_TRUE(Remove(location, &error_msg)) << error_msg;
211 }
212
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000213 protected:
214 Thread* self_;
David Brazdil1f9d3c32018-05-02 16:53:06 +0100215 jobject jclass_loader_;
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000216 ArtField* class1_field1_;
217 ArtField* class1_field12_;
218 ArtMethod* class1_init_;
219 ArtMethod* class1_method1_;
220 ArtMethod* class1_method1_i_;
221 ArtMethod* class1_method12_;
222 ArtField* class12_field1_;
223 ArtMethod* class12_method1_;
224 ArtField* class2_field1_;
225 ArtMethod* class2_method1_;
226 ArtMethod* class2_method1_i_;
227 ArtField* class3_field1_;
228 ArtMethod* class3_method1_;
229 ArtMethod* class3_method1_i_;
230};
231
Mathew Inwooda8503d92018-04-05 16:10:25 +0100232TEST_F(HiddenApiTest, CheckGetActionFromRuntimeFlags) {
David Brazdilf50ac102018-10-17 18:00:06 +0100233 ScopedObjectAccess soa(self_);
234
Mathew Inwooda8503d92018-04-05 16:10:25 +0100235 runtime_->SetHiddenApiEnforcementPolicy(hiddenapi::EnforcementPolicy::kJustWarn);
Andrei Oneafc12a6c2020-07-29 19:52:34 +0100236 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Sdk()), false);
237 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Unsupported()), false);
238 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::MaxTargetR()), false);
239 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::MaxTargetQ()), false);
240 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::MaxTargetP()), false);
241 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::MaxTargetO()), false);
242 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Blocked()), false);
Mathew Inwooda8503d92018-04-05 16:10:25 +0100243
David Brazdilf50ac102018-10-17 18:00:06 +0100244 runtime_->SetHiddenApiEnforcementPolicy(hiddenapi::EnforcementPolicy::kEnabled);
David Brazdildcfa89b2018-10-31 11:04:10 +0000245 runtime_->SetTargetSdkVersion(
Andrei Oneafc12a6c2020-07-29 19:52:34 +0100246 static_cast<uint32_t>(hiddenapi::ApiList::MaxTargetO().GetMaxAllowedSdkVersion()));
Nicolas Geoffray59c21032021-03-02 13:05:03 +0000247 SetChangeIdState(kHideMaxtargetsdkPHiddenApis, false);
248 SetChangeIdState(kHideMaxtargetsdkQHiddenApis, false);
Andrei Oneafc12a6c2020-07-29 19:52:34 +0100249 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Sdk()), false);
250 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Unsupported()), false);
251 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::MaxTargetR()), false);
252 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::MaxTargetQ()), false);
253 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::MaxTargetP()), false);
254 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::MaxTargetO()), false);
255 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Blocked()), true);
Mathew Inwooda8503d92018-04-05 16:10:25 +0100256
David Brazdilf50ac102018-10-17 18:00:06 +0100257 runtime_->SetHiddenApiEnforcementPolicy(hiddenapi::EnforcementPolicy::kEnabled);
David Brazdildcfa89b2018-10-31 11:04:10 +0000258 runtime_->SetTargetSdkVersion(
Andrei Oneafc12a6c2020-07-29 19:52:34 +0100259 static_cast<uint32_t>(hiddenapi::ApiList::MaxTargetO().GetMaxAllowedSdkVersion()) + 1);
Nicolas Geoffray59c21032021-03-02 13:05:03 +0000260 SetChangeIdState(kHideMaxtargetsdkPHiddenApis, false);
261 SetChangeIdState(kHideMaxtargetsdkQHiddenApis, false);
Andrei Oneafc12a6c2020-07-29 19:52:34 +0100262 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Sdk()), false);
263 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Unsupported()), false);
264 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::MaxTargetR()), false);
265 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::MaxTargetQ()), false);
266 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::MaxTargetP()), false);
267 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::MaxTargetO()), true);
268 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Blocked()), true);
Nicolas Geoffray59c21032021-03-02 13:05:03 +0000269 SetChangeIdState(kHideMaxtargetsdkQHiddenApis, true);
Andrei Oneafc12a6c2020-07-29 19:52:34 +0100270 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Sdk()), false);
271 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Unsupported()), false);
272 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::MaxTargetQ()), true);
273 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::MaxTargetP()), false);
274 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::MaxTargetO()), true);
275 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Blocked()), true);
David Brazdil80d16282018-11-01 09:55:09 +0000276
277 runtime_->SetHiddenApiEnforcementPolicy(hiddenapi::EnforcementPolicy::kEnabled);
278 runtime_->SetTargetSdkVersion(
Andrei Oneafc12a6c2020-07-29 19:52:34 +0100279 static_cast<uint32_t>(hiddenapi::ApiList::MaxTargetP().GetMaxAllowedSdkVersion()) + 1);
Nicolas Geoffray59c21032021-03-02 13:05:03 +0000280 SetChangeIdState(kHideMaxtargetsdkPHiddenApis, true);
281 SetChangeIdState(kHideMaxtargetsdkQHiddenApis, false);
Andrei Oneafc12a6c2020-07-29 19:52:34 +0100282 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Sdk()), false);
283 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Unsupported()), false);
284 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::MaxTargetR()), false);
285 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::MaxTargetQ()), false);
286 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::MaxTargetP()), true);
287 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::MaxTargetO()), true);
288 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Blocked()), true);
Artur Satayev201ffea2019-10-31 14:58:03 +0000289
290 runtime_->SetHiddenApiEnforcementPolicy(hiddenapi::EnforcementPolicy::kEnabled);
291 runtime_->SetTargetSdkVersion(
Andrei Oneafc12a6c2020-07-29 19:52:34 +0100292 static_cast<uint32_t>(hiddenapi::ApiList::MaxTargetQ().GetMaxAllowedSdkVersion()) + 1);
Nicolas Geoffray59c21032021-03-02 13:05:03 +0000293 SetChangeIdState(kHideMaxtargetsdkPHiddenApis, true);
294 SetChangeIdState(kHideMaxtargetsdkQHiddenApis, true);
Andrei Oneafc12a6c2020-07-29 19:52:34 +0100295 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Sdk()), false);
296 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Unsupported()), false);
297 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::MaxTargetR()), false);
298 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::MaxTargetQ()), true);
299 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::MaxTargetP()), true);
300 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::MaxTargetO()), true);
301 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Blocked()), true);
Artur Satayevb708fc12020-05-20 17:48:19 +0100302
303 runtime_->SetHiddenApiEnforcementPolicy(hiddenapi::EnforcementPolicy::kEnabled);
304 runtime_->SetTargetSdkVersion(
Andrei Oneafc12a6c2020-07-29 19:52:34 +0100305 static_cast<uint32_t>(hiddenapi::ApiList::MaxTargetR().GetMaxAllowedSdkVersion()) + 1);
Nicolas Geoffray59c21032021-03-02 13:05:03 +0000306 SetChangeIdState(kHideMaxtargetsdkPHiddenApis, true);
307 SetChangeIdState(kHideMaxtargetsdkQHiddenApis, true);
Andrei Oneafc12a6c2020-07-29 19:52:34 +0100308 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Sdk()), false);
309 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Unsupported()), false);
310 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::MaxTargetR()), true);
311 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::MaxTargetQ()), true);
312 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::MaxTargetP()), true);
313 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::MaxTargetO()), true);
314 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Blocked()), true);
Mathew Inwooda8503d92018-04-05 16:10:25 +0100315}
316
Artur Satayev267366c2019-10-31 14:59:26 +0000317TEST_F(HiddenApiTest, CheckTestApiEnforcement) {
318 ScopedObjectAccess soa(self_);
319
320 runtime_->SetHiddenApiEnforcementPolicy(hiddenapi::EnforcementPolicy::kEnabled);
321 runtime_->SetTargetSdkVersion(
Andrei Oneafc12a6c2020-07-29 19:52:34 +0100322 static_cast<uint32_t>(hiddenapi::ApiList::MaxTargetR().GetMaxAllowedSdkVersion()) + 1);
Artur Satayev267366c2019-10-31 14:59:26 +0000323
324 // Default case where all TestApis are treated like non-TestApi.
satayev09efc162021-02-22 17:24:57 +0000325 runtime_->SetTestApiEnforcementPolicy(hiddenapi::EnforcementPolicy::kEnabled);
Nicolas Geoffray59c21032021-03-02 13:05:03 +0000326 SetChangeIdState(kAllowTestApiAccess, false);
Artur Satayev267366c2019-10-31 14:59:26 +0000327 ASSERT_EQ(
Andrei Oneafc12a6c2020-07-29 19:52:34 +0100328 ShouldDenyAccess(hiddenapi::ApiList::TestApi() | hiddenapi::ApiList::Sdk()), false);
Artur Satayev267366c2019-10-31 14:59:26 +0000329 ASSERT_EQ(
Andrei Oneafc12a6c2020-07-29 19:52:34 +0100330 ShouldDenyAccess(hiddenapi::ApiList::TestApi() | hiddenapi::ApiList::Unsupported()), false);
Artur Satayev267366c2019-10-31 14:59:26 +0000331 ASSERT_EQ(
Andrei Oneafc12a6c2020-07-29 19:52:34 +0100332 ShouldDenyAccess(hiddenapi::ApiList::TestApi() | hiddenapi::ApiList::MaxTargetR()), true);
Artur Satayevb708fc12020-05-20 17:48:19 +0100333 ASSERT_EQ(
Andrei Oneafc12a6c2020-07-29 19:52:34 +0100334 ShouldDenyAccess(hiddenapi::ApiList::TestApi() | hiddenapi::ApiList::MaxTargetQ()), true);
Artur Satayev267366c2019-10-31 14:59:26 +0000335 ASSERT_EQ(
Andrei Oneafc12a6c2020-07-29 19:52:34 +0100336 ShouldDenyAccess(hiddenapi::ApiList::TestApi() | hiddenapi::ApiList::MaxTargetP()), true);
Artur Satayev267366c2019-10-31 14:59:26 +0000337 ASSERT_EQ(
Andrei Oneafc12a6c2020-07-29 19:52:34 +0100338 ShouldDenyAccess(hiddenapi::ApiList::TestApi() | hiddenapi::ApiList::MaxTargetO()), true);
Artur Satayev267366c2019-10-31 14:59:26 +0000339 ASSERT_EQ(
Andrei Oneafc12a6c2020-07-29 19:52:34 +0100340 ShouldDenyAccess(hiddenapi::ApiList::TestApi() | hiddenapi::ApiList::Blocked()), true);
Artur Satayev267366c2019-10-31 14:59:26 +0000341
342 // A case where we want to allow access to TestApis.
satayev09efc162021-02-22 17:24:57 +0000343 runtime_->SetTestApiEnforcementPolicy(hiddenapi::EnforcementPolicy::kDisabled);
Nicolas Geoffray59c21032021-03-02 13:05:03 +0000344 SetChangeIdState(kAllowTestApiAccess, false);
satayev09efc162021-02-22 17:24:57 +0000345 ASSERT_EQ(
346 ShouldDenyAccess(hiddenapi::ApiList::TestApi() | hiddenapi::ApiList::Sdk()), false);
347 ASSERT_EQ(
348 ShouldDenyAccess(hiddenapi::ApiList::TestApi() | hiddenapi::ApiList::Unsupported()), false);
349 ASSERT_EQ(
350 ShouldDenyAccess(hiddenapi::ApiList::TestApi() | hiddenapi::ApiList::MaxTargetR()), false);
351 ASSERT_EQ(
352 ShouldDenyAccess(hiddenapi::ApiList::TestApi() | hiddenapi::ApiList::MaxTargetQ()), false);
353 ASSERT_EQ(
354 ShouldDenyAccess(hiddenapi::ApiList::TestApi() | hiddenapi::ApiList::MaxTargetP()), false);
355 ASSERT_EQ(
356 ShouldDenyAccess(hiddenapi::ApiList::TestApi() | hiddenapi::ApiList::MaxTargetO()), false);
357 ASSERT_EQ(
358 ShouldDenyAccess(hiddenapi::ApiList::TestApi() | hiddenapi::ApiList::Blocked()), false);
359
360 // A second case where we want to allow access to TestApis.
361 runtime_->SetTestApiEnforcementPolicy(hiddenapi::EnforcementPolicy::kEnabled);
Nicolas Geoffray59c21032021-03-02 13:05:03 +0000362 SetChangeIdState(kAllowTestApiAccess, true);
Artur Satayev267366c2019-10-31 14:59:26 +0000363 ASSERT_EQ(
Andrei Oneafc12a6c2020-07-29 19:52:34 +0100364 ShouldDenyAccess(hiddenapi::ApiList::TestApi() | hiddenapi::ApiList::Sdk()), false);
Artur Satayev267366c2019-10-31 14:59:26 +0000365 ASSERT_EQ(
Andrei Oneafc12a6c2020-07-29 19:52:34 +0100366 ShouldDenyAccess(hiddenapi::ApiList::TestApi() | hiddenapi::ApiList::Unsupported()), false);
Artur Satayev267366c2019-10-31 14:59:26 +0000367 ASSERT_EQ(
Andrei Oneafc12a6c2020-07-29 19:52:34 +0100368 ShouldDenyAccess(hiddenapi::ApiList::TestApi() | hiddenapi::ApiList::MaxTargetR()), false);
Artur Satayevb708fc12020-05-20 17:48:19 +0100369 ASSERT_EQ(
Andrei Oneafc12a6c2020-07-29 19:52:34 +0100370 ShouldDenyAccess(hiddenapi::ApiList::TestApi() | hiddenapi::ApiList::MaxTargetQ()), false);
Artur Satayev267366c2019-10-31 14:59:26 +0000371 ASSERT_EQ(
Andrei Oneafc12a6c2020-07-29 19:52:34 +0100372 ShouldDenyAccess(hiddenapi::ApiList::TestApi() | hiddenapi::ApiList::MaxTargetP()), false);
Artur Satayev267366c2019-10-31 14:59:26 +0000373 ASSERT_EQ(
Andrei Oneafc12a6c2020-07-29 19:52:34 +0100374 ShouldDenyAccess(hiddenapi::ApiList::TestApi() | hiddenapi::ApiList::MaxTargetO()), false);
Artur Satayev267366c2019-10-31 14:59:26 +0000375 ASSERT_EQ(
Andrei Oneafc12a6c2020-07-29 19:52:34 +0100376 ShouldDenyAccess(hiddenapi::ApiList::TestApi() | hiddenapi::ApiList::Blocked()), false);
Artur Satayev267366c2019-10-31 14:59:26 +0000377}
378
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000379TEST_F(HiddenApiTest, CheckMembersRead) {
380 ASSERT_NE(nullptr, class1_field1_);
381 ASSERT_NE(nullptr, class1_field12_);
382 ASSERT_NE(nullptr, class1_init_);
383 ASSERT_NE(nullptr, class1_method1_);
384 ASSERT_NE(nullptr, class1_method1_i_);
385 ASSERT_NE(nullptr, class1_method12_);
386 ASSERT_NE(nullptr, class12_field1_);
387 ASSERT_NE(nullptr, class12_method1_);
388 ASSERT_NE(nullptr, class2_field1_);
389 ASSERT_NE(nullptr, class2_method1_);
390 ASSERT_NE(nullptr, class2_method1_i_);
391 ASSERT_NE(nullptr, class3_field1_);
392 ASSERT_NE(nullptr, class3_method1_);
393 ASSERT_NE(nullptr, class3_method1_i_);
394}
395
396TEST_F(HiddenApiTest, CheckEverythingMatchesL) {
397 ScopedObjectAccess soa(self_);
398 std::string prefix("L");
Andreas Gampeaa120012018-03-28 16:23:24 -0700399 ASSERT_TRUE(MemberSignature(class1_field1_).DoesPrefixMatch(prefix));
400 ASSERT_TRUE(MemberSignature(class1_field12_).DoesPrefixMatch(prefix));
401 ASSERT_TRUE(MemberSignature(class1_init_).DoesPrefixMatch(prefix));
402 ASSERT_TRUE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix));
403 ASSERT_TRUE(MemberSignature(class1_method1_i_).DoesPrefixMatch(prefix));
404 ASSERT_TRUE(MemberSignature(class12_field1_).DoesPrefixMatch(prefix));
405 ASSERT_TRUE(MemberSignature(class12_method1_).DoesPrefixMatch(prefix));
406 ASSERT_TRUE(MemberSignature(class1_method12_).DoesPrefixMatch(prefix));
407 ASSERT_TRUE(MemberSignature(class2_field1_).DoesPrefixMatch(prefix));
408 ASSERT_TRUE(MemberSignature(class2_method1_).DoesPrefixMatch(prefix));
409 ASSERT_TRUE(MemberSignature(class2_method1_i_).DoesPrefixMatch(prefix));
410 ASSERT_TRUE(MemberSignature(class3_field1_).DoesPrefixMatch(prefix));
411 ASSERT_TRUE(MemberSignature(class3_method1_).DoesPrefixMatch(prefix));
412 ASSERT_TRUE(MemberSignature(class3_method1_i_).DoesPrefixMatch(prefix));
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000413}
414
415TEST_F(HiddenApiTest, CheckPackageMatch) {
416 ScopedObjectAccess soa(self_);
417 std::string prefix("Lmypackage/packagea/");
Andreas Gampeaa120012018-03-28 16:23:24 -0700418 ASSERT_TRUE(MemberSignature(class1_field1_).DoesPrefixMatch(prefix));
419 ASSERT_TRUE(MemberSignature(class1_field12_).DoesPrefixMatch(prefix));
420 ASSERT_TRUE(MemberSignature(class1_init_).DoesPrefixMatch(prefix));
421 ASSERT_TRUE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix));
422 ASSERT_TRUE(MemberSignature(class1_method1_i_).DoesPrefixMatch(prefix));
423 ASSERT_TRUE(MemberSignature(class1_method12_).DoesPrefixMatch(prefix));
424 ASSERT_TRUE(MemberSignature(class12_field1_).DoesPrefixMatch(prefix));
425 ASSERT_TRUE(MemberSignature(class12_method1_).DoesPrefixMatch(prefix));
426 ASSERT_TRUE(MemberSignature(class2_field1_).DoesPrefixMatch(prefix));
427 ASSERT_TRUE(MemberSignature(class2_method1_).DoesPrefixMatch(prefix));
428 ASSERT_TRUE(MemberSignature(class2_method1_i_).DoesPrefixMatch(prefix));
429 ASSERT_FALSE(MemberSignature(class3_field1_).DoesPrefixMatch(prefix));
430 ASSERT_FALSE(MemberSignature(class3_method1_).DoesPrefixMatch(prefix));
431 ASSERT_FALSE(MemberSignature(class3_method1_i_).DoesPrefixMatch(prefix));
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000432}
433
434TEST_F(HiddenApiTest, CheckClassMatch) {
435 ScopedObjectAccess soa(self_);
436 std::string prefix("Lmypackage/packagea/Class1");
Andreas Gampeaa120012018-03-28 16:23:24 -0700437 ASSERT_TRUE(MemberSignature(class1_field1_).DoesPrefixMatch(prefix));
438 ASSERT_TRUE(MemberSignature(class1_field12_).DoesPrefixMatch(prefix));
439 ASSERT_TRUE(MemberSignature(class1_init_).DoesPrefixMatch(prefix));
440 ASSERT_TRUE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix));
441 ASSERT_TRUE(MemberSignature(class1_method1_i_).DoesPrefixMatch(prefix));
442 ASSERT_TRUE(MemberSignature(class1_method12_).DoesPrefixMatch(prefix));
443 ASSERT_TRUE(MemberSignature(class12_field1_).DoesPrefixMatch(prefix));
444 ASSERT_TRUE(MemberSignature(class12_method1_).DoesPrefixMatch(prefix));
445 ASSERT_FALSE(MemberSignature(class2_field1_).DoesPrefixMatch(prefix));
446 ASSERT_FALSE(MemberSignature(class2_method1_).DoesPrefixMatch(prefix));
447 ASSERT_FALSE(MemberSignature(class2_method1_i_).DoesPrefixMatch(prefix));
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000448}
449
450TEST_F(HiddenApiTest, CheckClassExactMatch) {
451 ScopedObjectAccess soa(self_);
452 std::string prefix("Lmypackage/packagea/Class1;");
Andreas Gampeaa120012018-03-28 16:23:24 -0700453 ASSERT_TRUE(MemberSignature(class1_field1_).DoesPrefixMatch(prefix));
454 ASSERT_TRUE(MemberSignature(class1_field12_).DoesPrefixMatch(prefix));
455 ASSERT_TRUE(MemberSignature(class1_init_).DoesPrefixMatch(prefix));
456 ASSERT_TRUE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix));
457 ASSERT_TRUE(MemberSignature(class1_method1_i_).DoesPrefixMatch(prefix));
458 ASSERT_FALSE(MemberSignature(class12_field1_).DoesPrefixMatch(prefix));
459 ASSERT_FALSE(MemberSignature(class12_method1_).DoesPrefixMatch(prefix));
460 ASSERT_FALSE(MemberSignature(class2_field1_).DoesPrefixMatch(prefix));
461 ASSERT_FALSE(MemberSignature(class2_method1_).DoesPrefixMatch(prefix));
462 ASSERT_FALSE(MemberSignature(class2_method1_i_).DoesPrefixMatch(prefix));
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000463}
464
465TEST_F(HiddenApiTest, CheckMethodMatch) {
466 ScopedObjectAccess soa(self_);
467 std::string prefix("Lmypackage/packagea/Class1;->method1");
Andreas Gampeaa120012018-03-28 16:23:24 -0700468 ASSERT_FALSE(MemberSignature(class1_field1_).DoesPrefixMatch(prefix));
469 ASSERT_FALSE(MemberSignature(class1_field12_).DoesPrefixMatch(prefix));
470 ASSERT_FALSE(MemberSignature(class1_init_).DoesPrefixMatch(prefix));
471 ASSERT_TRUE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix));
472 ASSERT_TRUE(MemberSignature(class1_method1_i_).DoesPrefixMatch(prefix));
473 ASSERT_TRUE(MemberSignature(class1_method12_).DoesPrefixMatch(prefix));
474 ASSERT_FALSE(MemberSignature(class12_field1_).DoesPrefixMatch(prefix));
475 ASSERT_FALSE(MemberSignature(class12_method1_).DoesPrefixMatch(prefix));
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000476}
477
478TEST_F(HiddenApiTest, CheckMethodExactMatch) {
479 ScopedObjectAccess soa(self_);
480 std::string prefix("Lmypackage/packagea/Class1;->method1(");
Andreas Gampeaa120012018-03-28 16:23:24 -0700481 ASSERT_FALSE(MemberSignature(class1_field1_).DoesPrefixMatch(prefix));
482 ASSERT_FALSE(MemberSignature(class1_field12_).DoesPrefixMatch(prefix));
483 ASSERT_FALSE(MemberSignature(class1_init_).DoesPrefixMatch(prefix));
484 ASSERT_TRUE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix));
485 ASSERT_TRUE(MemberSignature(class1_method1_i_).DoesPrefixMatch(prefix));
486 ASSERT_FALSE(MemberSignature(class1_method12_).DoesPrefixMatch(prefix));
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000487}
488
489TEST_F(HiddenApiTest, CheckMethodSignatureMatch) {
490 ScopedObjectAccess soa(self_);
491 std::string prefix("Lmypackage/packagea/Class1;->method1(I)");
Andreas Gampeaa120012018-03-28 16:23:24 -0700492 ASSERT_FALSE(MemberSignature(class1_field1_).DoesPrefixMatch(prefix));
493 ASSERT_FALSE(MemberSignature(class1_field12_).DoesPrefixMatch(prefix));
494 ASSERT_FALSE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix));
495 ASSERT_TRUE(MemberSignature(class1_method1_i_).DoesPrefixMatch(prefix));
496 ASSERT_FALSE(MemberSignature(class1_method12_).DoesPrefixMatch(prefix));
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000497}
498
499TEST_F(HiddenApiTest, CheckMethodSignatureAndReturnMatch) {
500 ScopedObjectAccess soa(self_);
501 std::string prefix("Lmypackage/packagea/Class1;->method1()V");
Andreas Gampeaa120012018-03-28 16:23:24 -0700502 ASSERT_FALSE(MemberSignature(class1_field1_).DoesPrefixMatch(prefix));
503 ASSERT_FALSE(MemberSignature(class1_field12_).DoesPrefixMatch(prefix));
504 ASSERT_TRUE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix));
505 ASSERT_FALSE(MemberSignature(class1_method1_i_).DoesPrefixMatch(prefix));
506 ASSERT_FALSE(MemberSignature(class1_method12_).DoesPrefixMatch(prefix));
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000507}
508
509TEST_F(HiddenApiTest, CheckFieldMatch) {
510 ScopedObjectAccess soa(self_);
511 std::string prefix("Lmypackage/packagea/Class1;->field1");
Andreas Gampeaa120012018-03-28 16:23:24 -0700512 ASSERT_TRUE(MemberSignature(class1_field1_).DoesPrefixMatch(prefix));
513 ASSERT_TRUE(MemberSignature(class1_field12_).DoesPrefixMatch(prefix));
514 ASSERT_FALSE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix));
515 ASSERT_FALSE(MemberSignature(class1_method1_i_).DoesPrefixMatch(prefix));
516 ASSERT_FALSE(MemberSignature(class1_method12_).DoesPrefixMatch(prefix));
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000517}
518
519TEST_F(HiddenApiTest, CheckFieldExactMatch) {
520 ScopedObjectAccess soa(self_);
521 std::string prefix("Lmypackage/packagea/Class1;->field1:");
Andreas Gampeaa120012018-03-28 16:23:24 -0700522 ASSERT_TRUE(MemberSignature(class1_field1_).DoesPrefixMatch(prefix));
523 ASSERT_FALSE(MemberSignature(class1_field12_).DoesPrefixMatch(prefix));
524 ASSERT_FALSE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix));
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000525}
526
527TEST_F(HiddenApiTest, CheckFieldTypeMatch) {
528 ScopedObjectAccess soa(self_);
529 std::string prefix("Lmypackage/packagea/Class1;->field1:I");
Andreas Gampeaa120012018-03-28 16:23:24 -0700530 ASSERT_TRUE(MemberSignature(class1_field1_).DoesPrefixMatch(prefix));
531 ASSERT_FALSE(MemberSignature(class1_field12_).DoesPrefixMatch(prefix));
532 ASSERT_FALSE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix));
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000533}
534
535TEST_F(HiddenApiTest, CheckConstructorMatch) {
536 ScopedObjectAccess soa(self_);
537 std::string prefix("Lmypackage/packagea/Class1;-><init>");
Andreas Gampeaa120012018-03-28 16:23:24 -0700538 ASSERT_TRUE(MemberSignature(class1_init_).DoesPrefixMatch(prefix));
539 ASSERT_FALSE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix));
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000540}
541
542TEST_F(HiddenApiTest, CheckConstructorExactMatch) {
543 ScopedObjectAccess soa(self_);
544 std::string prefix("Lmypackage/packagea/Class1;-><init>()V");
Andreas Gampeaa120012018-03-28 16:23:24 -0700545 ASSERT_TRUE(MemberSignature(class1_init_).DoesPrefixMatch(prefix));
546 ASSERT_FALSE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix));
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000547}
548
549TEST_F(HiddenApiTest, CheckMethodSignatureTrailingCharsNoMatch) {
550 ScopedObjectAccess soa(self_);
551 std::string prefix("Lmypackage/packagea/Class1;->method1()Vfoo");
Andreas Gampeaa120012018-03-28 16:23:24 -0700552 ASSERT_FALSE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix));
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000553}
554
555TEST_F(HiddenApiTest, CheckConstructorTrailingCharsNoMatch) {
556 ScopedObjectAccess soa(self_);
557 std::string prefix("Lmypackage/packagea/Class1;-><init>()Vfoo");
Andreas Gampeaa120012018-03-28 16:23:24 -0700558 ASSERT_FALSE(MemberSignature(class1_init_).DoesPrefixMatch(prefix));
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000559}
560
561TEST_F(HiddenApiTest, CheckFieldTrailingCharsNoMatch) {
562 ScopedObjectAccess soa(self_);
563 std::string prefix("Lmypackage/packagea/Class1;->field1:Ifoo");
Andreas Gampeaa120012018-03-28 16:23:24 -0700564 ASSERT_FALSE(MemberSignature(class1_field1_).DoesPrefixMatch(prefix));
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000565}
566
David Brazdil1f9d3c32018-05-02 16:53:06 +0100567TEST_F(HiddenApiTest, CheckMemberSignatureForProxyClass) {
568 ScopedObjectAccess soa(self_);
569 StackHandleScope<4> hs(soa.Self());
570 Handle<mirror::ClassLoader> class_loader(
571 hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader_)));
572
573 // Find interface we will create a proxy for.
574 Handle<mirror::Class> h_iface(hs.NewHandle(
575 class_linker_->FindClass(soa.Self(), "Lmypackage/packagea/Interface;", class_loader)));
576 ASSERT_TRUE(h_iface != nullptr);
577
578 // Create the proxy class.
Vladimir Markoa8bba7d2018-05-30 15:18:48 +0100579 std::vector<Handle<mirror::Class>> interfaces;
580 interfaces.push_back(h_iface);
David Brazdil1f9d3c32018-05-02 16:53:06 +0100581 Handle<mirror::Class> proxyClass = hs.NewHandle(proxy_test::GenerateProxyClass(
582 soa, jclass_loader_, runtime_->GetClassLinker(), "$Proxy1234", interfaces));
583 ASSERT_TRUE(proxyClass != nullptr);
584 ASSERT_TRUE(proxyClass->IsProxyClass());
585 ASSERT_TRUE(proxyClass->IsInitialized());
586
587 // Find the "method" virtual method.
588 ArtMethod* method = nullptr;
589 for (auto& m : proxyClass->GetDeclaredVirtualMethods(kRuntimePointerSize)) {
590 if (strcmp("method", m.GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetName()) == 0) {
591 method = &m;
592 break;
593 }
594 }
595 ASSERT_TRUE(method != nullptr);
596
597 // Find the "interfaces" static field. This is generated for all proxies.
598 ArtField* field = nullptr;
599 for (size_t i = 0; i < proxyClass->NumStaticFields(); ++i) {
600 ArtField* f = proxyClass->GetStaticField(i);
601 if (strcmp("interfaces", f->GetName()) == 0) {
602 field = f;
603 break;
604 }
605 }
606 ASSERT_TRUE(field != nullptr);
607
608 // Test the signature. We expect the signature from the interface class.
609 std::ostringstream ss_method;
David Brazdil6a1dab42019-02-28 18:45:15 +0000610 MemberSignature(method->GetInterfaceMethodIfProxy(kRuntimePointerSize)).Dump(ss_method);
David Brazdil1f9d3c32018-05-02 16:53:06 +0100611 ASSERT_EQ("Lmypackage/packagea/Interface;->method()V", ss_method.str());
612
613 // Test the signature. We expect the signature of the proxy class.
614 std::ostringstream ss_field;
615 MemberSignature(field).Dump(ss_field);
616 ASSERT_EQ("L$Proxy1234;->interfaces:[Ljava/lang/Class;", ss_field.str());
617}
618
David Brazdila5c3a802019-03-08 14:59:41 +0000619TEST_F(HiddenApiTest, DexDomain_DataDir) {
620 // Load file from a non-system directory and check that it is not flagged as framework.
621 std::string data_location_path = android_data_ + "/foo.jar";
622 ASSERT_FALSE(LocationIsOnSystemFramework(data_location_path.c_str()));
Nicolas Geoffray59c21032021-03-02 13:05:03 +0000623 TestLocation(data_location_path, hiddenapi::Domain::kApplication);
David Brazdila5c3a802019-03-08 14:59:41 +0000624}
625
626TEST_F(HiddenApiTest, DexDomain_SystemDir) {
627 // Load file from a system, non-framework directory and check that it is not flagged as framework.
628 std::string system_location_path = GetAndroidRoot() + "/foo.jar";
629 ASSERT_FALSE(LocationIsOnSystemFramework(system_location_path.c_str()));
Nicolas Geoffray59c21032021-03-02 13:05:03 +0000630 TestLocation(system_location_path, hiddenapi::Domain::kApplication);
David Brazdila5c3a802019-03-08 14:59:41 +0000631}
632
Chris Gross5477b8e2020-04-24 09:36:45 -0700633TEST_F(HiddenApiTest, DexDomain_SystemExtDir) {
Nicolas Geoffray59c21032021-03-02 13:05:03 +0000634 // Load file from a system_ext, non-framework directory and check that it is not flagged as
635 // framework.
Chris Gross5477b8e2020-04-24 09:36:45 -0700636 std::string system_ext_location_path = android_system_ext_ + "/foo.jar";
637 ASSERT_FALSE(LocationIsOnSystemExtFramework(system_ext_location_path.c_str()));
Nicolas Geoffray59c21032021-03-02 13:05:03 +0000638 TestLocation(system_ext_location_path, hiddenapi::Domain::kApplication);
639}
Chris Gross5477b8e2020-04-24 09:36:45 -0700640
Nicolas Geoffray59c21032021-03-02 13:05:03 +0000641TEST_F(HiddenApiTest, DexDomain_SystemSystemExtDir) {
642 // Load file from a system/system_ext, non-framework directory and check that it is not flagged as
643 // framework.
644 std::filesystem::create_directory(GetAndroidRoot() + "/system_ext");
645 std::string system_ext_location_path = GetAndroidRoot() + "/system_ext/foo.jar";
646 ASSERT_FALSE(LocationIsOnSystemExtFramework(system_ext_location_path.c_str()));
647 TestLocation(system_ext_location_path, hiddenapi::Domain::kApplication);
Chris Gross5477b8e2020-04-24 09:36:45 -0700648}
649
David Brazdila5c3a802019-03-08 14:59:41 +0000650TEST_F(HiddenApiTest, DexDomain_SystemFrameworkDir) {
651 // Load file from a system/framework directory and check that it is flagged as a framework dex.
David Srbecky7400a542020-07-09 13:40:57 +0100652 std::filesystem::create_directory(GetAndroidRoot() + "/framework");
David Brazdila5c3a802019-03-08 14:59:41 +0000653 std::string system_framework_location_path = GetAndroidRoot() + "/framework/foo.jar";
654 ASSERT_TRUE(LocationIsOnSystemFramework(system_framework_location_path.c_str()));
Nicolas Geoffray59c21032021-03-02 13:05:03 +0000655 TestLocation(system_framework_location_path, hiddenapi::Domain::kPlatform);
David Brazdila5c3a802019-03-08 14:59:41 +0000656}
657
Chris Gross5477b8e2020-04-24 09:36:45 -0700658TEST_F(HiddenApiTest, DexDomain_SystemExtFrameworkDir) {
659 // Load file from a system_ext/framework directory and check that it is flagged as a framework dex.
Nicolas Geoffray987a8ea2021-03-07 15:19:18 +0000660 std::filesystem::create_directory(android_system_ext_ + "/framework");
Chris Gross5477b8e2020-04-24 09:36:45 -0700661 std::string system_ext_framework_location_path = android_system_ext_ + "/framework/foo.jar";
662 ASSERT_TRUE(LocationIsOnSystemExtFramework(system_ext_framework_location_path.c_str()));
Nicolas Geoffray59c21032021-03-02 13:05:03 +0000663 TestLocation(system_ext_framework_location_path, hiddenapi::Domain::kPlatform);
664}
Chris Gross5477b8e2020-04-24 09:36:45 -0700665
Nicolas Geoffray59c21032021-03-02 13:05:03 +0000666TEST_F(HiddenApiTest, DexDomain_SystemSystemExtFrameworkDir) {
667 // Load file from a system/system_ext/framework directory and check that it is flagged as a
668 // framework dex.
669 std::filesystem::create_directory(GetAndroidRoot() + "/system_ext");
670 std::filesystem::create_directory(GetAndroidRoot() + "/system_ext/framework");
671 std::string system_ext_framework_location_path =
672 GetAndroidRoot() + "/system_ext/framework/foo.jar";
673 ASSERT_TRUE(LocationIsOnSystemExtFramework(system_ext_framework_location_path.c_str()));
674 TestLocation(system_ext_framework_location_path, hiddenapi::Domain::kPlatform);
Chris Gross5477b8e2020-04-24 09:36:45 -0700675}
676
David Brazdila5c3a802019-03-08 14:59:41 +0000677TEST_F(HiddenApiTest, DexDomain_DataDir_MultiDex) {
678 // Load multidex file from a non-system directory and check that it is not flagged as framework.
679 std::string data_multi_location_path = android_data_ + "/multifoo.jar";
680 ASSERT_FALSE(LocationIsOnSystemFramework(data_multi_location_path.c_str()));
Nicolas Geoffray59c21032021-03-02 13:05:03 +0000681 TestLocation(data_multi_location_path, hiddenapi::Domain::kApplication);
David Brazdila5c3a802019-03-08 14:59:41 +0000682}
683
684TEST_F(HiddenApiTest, DexDomain_SystemDir_MultiDex) {
685 // Load multidex file from a system, non-framework directory and check that it is not flagged
686 // as framework.
687 std::string system_multi_location_path = GetAndroidRoot() + "/multifoo.jar";
688 ASSERT_FALSE(LocationIsOnSystemFramework(system_multi_location_path.c_str()));
Nicolas Geoffray59c21032021-03-02 13:05:03 +0000689 TestLocation(system_multi_location_path, hiddenapi::Domain::kApplication);
David Brazdila5c3a802019-03-08 14:59:41 +0000690}
691
Chris Gross5477b8e2020-04-24 09:36:45 -0700692TEST_F(HiddenApiTest, DexDomain_SystemExtDir_MultiDex) {
693 // Load multidex file from a system_ext, non-framework directory and check that it is not flagged
694 // as framework.
695 std::string system_ext_multi_location_path = android_system_ext_ + "/multifoo.jar";
696 ASSERT_FALSE(LocationIsOnSystemExtFramework(system_ext_multi_location_path.c_str()));
Nicolas Geoffray59c21032021-03-02 13:05:03 +0000697 TestLocation(system_ext_multi_location_path, hiddenapi::Domain::kApplication);
698}
Chris Gross5477b8e2020-04-24 09:36:45 -0700699
Nicolas Geoffray59c21032021-03-02 13:05:03 +0000700TEST_F(HiddenApiTest, DexDomain_SystemSystemExtDir_MultiDex) {
701 // Load multidex file from a system/system_ext, non-framework directory and check that it is not
702 // flagged as framework.
Nicolas Geoffray987a8ea2021-03-07 15:19:18 +0000703 std::filesystem::create_directory(GetAndroidRoot() + "/system_ext");
Nicolas Geoffray59c21032021-03-02 13:05:03 +0000704 std::string system_ext_multi_location_path =
705 GetAndroidRoot() + "/system_ext/multifoo.jar";
706 ASSERT_FALSE(LocationIsOnSystemExtFramework(system_ext_multi_location_path.c_str()));
707 TestLocation(system_ext_multi_location_path, hiddenapi::Domain::kApplication);
Chris Gross5477b8e2020-04-24 09:36:45 -0700708}
709
David Brazdila5c3a802019-03-08 14:59:41 +0000710TEST_F(HiddenApiTest, DexDomain_SystemFrameworkDir_MultiDex) {
711 // Load multidex file from a system/framework directory and check that it is flagged as a
712 // framework dex.
Nicolas Geoffray987a8ea2021-03-07 15:19:18 +0000713 std::filesystem::create_directory(GetAndroidRoot() + "/framework");
David Brazdila5c3a802019-03-08 14:59:41 +0000714 std::string system_framework_multi_location_path = GetAndroidRoot() + "/framework/multifoo.jar";
715 ASSERT_TRUE(LocationIsOnSystemFramework(system_framework_multi_location_path.c_str()));
Nicolas Geoffray59c21032021-03-02 13:05:03 +0000716 TestLocation(system_framework_multi_location_path, hiddenapi::Domain::kPlatform);
David Brazdila5c3a802019-03-08 14:59:41 +0000717}
718
Chris Gross5477b8e2020-04-24 09:36:45 -0700719TEST_F(HiddenApiTest, DexDomain_SystemExtFrameworkDir_MultiDex) {
720 // Load multidex file from a system_ext/framework directory and check that it is flagged as a
721 // framework dex.
Nicolas Geoffray987a8ea2021-03-07 15:19:18 +0000722 std::filesystem::create_directory(android_system_ext_ + "/framework");
Nicolas Geoffray59c21032021-03-02 13:05:03 +0000723 std::string system_ext_framework_multi_location_path =
724 android_system_ext_ + "/framework/multifoo.jar";
Chris Gross5477b8e2020-04-24 09:36:45 -0700725 ASSERT_TRUE(LocationIsOnSystemExtFramework(system_ext_framework_multi_location_path.c_str()));
Nicolas Geoffray59c21032021-03-02 13:05:03 +0000726 TestLocation(system_ext_framework_multi_location_path, hiddenapi::Domain::kPlatform);
727}
Chris Gross5477b8e2020-04-24 09:36:45 -0700728
Nicolas Geoffray59c21032021-03-02 13:05:03 +0000729TEST_F(HiddenApiTest, DexDomain_SystemSystemExtFrameworkDir_MultiDex) {
730 // Load multidex file from a system/system_ext/framework directory and check that it is flagged
731 // as a framework dex.
732 std::filesystem::create_directory(GetAndroidRoot() + "/system_ext");
733 std::filesystem::create_directory(GetAndroidRoot() + "/system_ext/framework");
734 std::string system_ext_framework_multi_location_path =
735 GetAndroidRoot() + "/system_ext/framework/multifoo.jar";
736 ASSERT_TRUE(LocationIsOnSystemExtFramework(system_ext_framework_multi_location_path.c_str()));
737 TestLocation(system_ext_framework_multi_location_path, hiddenapi::Domain::kPlatform);
Chris Gross5477b8e2020-04-24 09:36:45 -0700738}
739
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000740} // namespace art