blob: 595f0779e26def1227b9c9a757d9efc766cf8f5b [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 Brazdil2bb2fbd2018-11-13 18:24:26 +000019#include "base/sdk_version.h"
Mathew Inwood7d74ef52018-03-16 14:18:33 +000020#include "common_runtime_test.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010021#include "jni/jni_internal.h"
David Brazdil1f9d3c32018-05-02 16:53:06 +010022#include "proxy_test.h"
Mathew Inwood7d74ef52018-03-16 14:18:33 +000023
24namespace art {
25
Andreas Gampeaa120012018-03-28 16:23:24 -070026using hiddenapi::detail::MemberSignature;
David Brazdilf50ac102018-10-17 18:00:06 +010027using hiddenapi::detail::ShouldDenyAccessToMemberImpl;
Andreas Gampeaa120012018-03-28 16:23:24 -070028
Mathew Inwood7d74ef52018-03-16 14:18:33 +000029class HiddenApiTest : public CommonRuntimeTest {
30 protected:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010031 void SetUp() override {
Mathew Inwood7d74ef52018-03-16 14:18:33 +000032 // Do the normal setup.
33 CommonRuntimeTest::SetUp();
34 self_ = Thread::Current();
35 self_->TransitionFromSuspendedToRunnable();
David Brazdil1f9d3c32018-05-02 16:53:06 +010036 jclass_loader_ = LoadDex("HiddenApiSignatures");
Mathew Inwood7d74ef52018-03-16 14:18:33 +000037 bool started = runtime_->Start();
38 CHECK(started);
39
40 class1_field1_ = getArtField("mypackage/packagea/Class1", "field1", "I");
41 class1_field12_ = getArtField("mypackage/packagea/Class1", "field12", "I");
42 class1_init_ = getArtMethod("mypackage/packagea/Class1", "<init>", "()V");
43 class1_method1_ = getArtMethod("mypackage/packagea/Class1", "method1", "()V");
44 class1_method1_i_ = getArtMethod("mypackage/packagea/Class1", "method1", "(I)V");
45 class1_method12_ = getArtMethod("mypackage/packagea/Class1", "method12", "()V");
46 class12_field1_ = getArtField("mypackage/packagea/Class12", "field1", "I");
47 class12_method1_ = getArtMethod("mypackage/packagea/Class12", "method1", "()V");
48 class2_field1_ = getArtField("mypackage/packagea/Class2", "field1", "I");
49 class2_method1_ = getArtMethod("mypackage/packagea/Class2", "method1", "()V");
50 class2_method1_i_ = getArtMethod("mypackage/packagea/Class2", "method1", "(I)V");
51 class3_field1_ = getArtField("mypackage/packageb/Class3", "field1", "I");
52 class3_method1_ = getArtMethod("mypackage/packageb/Class3", "method1", "()V");
53 class3_method1_i_ = getArtMethod("mypackage/packageb/Class3", "method1", "(I)V");
54 }
55
56 ArtMethod* getArtMethod(const char* class_name, const char* name, const char* signature) {
57 JNIEnv* env = Thread::Current()->GetJniEnv();
58 jclass klass = env->FindClass(class_name);
59 jmethodID method_id = env->GetMethodID(klass, name, signature);
60 ArtMethod* art_method = jni::DecodeArtMethod(method_id);
61 return art_method;
62 }
63
64 ArtField* getArtField(const char* class_name, const char* name, const char* signature) {
65 JNIEnv* env = Thread::Current()->GetJniEnv();
66 jclass klass = env->FindClass(class_name);
67 jfieldID field_id = env->GetFieldID(klass, name, signature);
68 ArtField* art_field = jni::DecodeArtField(field_id);
69 return art_field;
70 }
71
David Brazdilf50ac102018-10-17 18:00:06 +010072 bool ShouldDenyAccess(hiddenapi::ApiList list) REQUIRES_SHARED(Locks::mutator_lock_) {
73 // Choose parameters such that there are no side effects (AccessMethod::kNone)
74 // and that the member is not on the exemptions list (here we choose one which
75 // is not even in boot class path).
76 return ShouldDenyAccessToMemberImpl(/* member= */ class1_field1_,
77 list,
78 /* access_method= */ hiddenapi::AccessMethod::kNone);
79 }
80
Mathew Inwood7d74ef52018-03-16 14:18:33 +000081 protected:
82 Thread* self_;
David Brazdil1f9d3c32018-05-02 16:53:06 +010083 jobject jclass_loader_;
Mathew Inwood7d74ef52018-03-16 14:18:33 +000084 ArtField* class1_field1_;
85 ArtField* class1_field12_;
86 ArtMethod* class1_init_;
87 ArtMethod* class1_method1_;
88 ArtMethod* class1_method1_i_;
89 ArtMethod* class1_method12_;
90 ArtField* class12_field1_;
91 ArtMethod* class12_method1_;
92 ArtField* class2_field1_;
93 ArtMethod* class2_method1_;
94 ArtMethod* class2_method1_i_;
95 ArtField* class3_field1_;
96 ArtMethod* class3_method1_;
97 ArtMethod* class3_method1_i_;
98};
99
Mathew Inwooda8503d92018-04-05 16:10:25 +0100100TEST_F(HiddenApiTest, CheckGetActionFromRuntimeFlags) {
David Brazdilf50ac102018-10-17 18:00:06 +0100101 ScopedObjectAccess soa(self_);
102
103 runtime_->SetHiddenApiEnforcementPolicy(hiddenapi::EnforcementPolicy::kDisabled);
David Brazdildcfa89b2018-10-31 11:04:10 +0000104 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Whitelist()), false);
105 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Greylist()), false);
David Brazdil80d16282018-11-01 09:55:09 +0000106 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::GreylistMaxP()), false);
David Brazdildcfa89b2018-10-31 11:04:10 +0000107 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::GreylistMaxO()), false);
108 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Blacklist()), false);
Mathew Inwooda8503d92018-04-05 16:10:25 +0100109
110 runtime_->SetHiddenApiEnforcementPolicy(hiddenapi::EnforcementPolicy::kJustWarn);
David Brazdildcfa89b2018-10-31 11:04:10 +0000111 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Whitelist()), false);
112 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Greylist()), false);
David Brazdil80d16282018-11-01 09:55:09 +0000113 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::GreylistMaxP()), false);
David Brazdildcfa89b2018-10-31 11:04:10 +0000114 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::GreylistMaxO()), false);
115 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Blacklist()), false);
Mathew Inwooda8503d92018-04-05 16:10:25 +0100116
David Brazdilf50ac102018-10-17 18:00:06 +0100117 runtime_->SetHiddenApiEnforcementPolicy(hiddenapi::EnforcementPolicy::kEnabled);
David Brazdildcfa89b2018-10-31 11:04:10 +0000118 runtime_->SetTargetSdkVersion(
119 static_cast<uint32_t>(hiddenapi::ApiList::GreylistMaxO().GetMaxAllowedSdkVersion()));
120 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Whitelist()), false);
121 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Greylist()), false);
David Brazdil80d16282018-11-01 09:55:09 +0000122 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::GreylistMaxP()), false);
David Brazdildcfa89b2018-10-31 11:04:10 +0000123 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::GreylistMaxO()), false);
124 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Blacklist()), true);
Mathew Inwooda8503d92018-04-05 16:10:25 +0100125
David Brazdilf50ac102018-10-17 18:00:06 +0100126 runtime_->SetHiddenApiEnforcementPolicy(hiddenapi::EnforcementPolicy::kEnabled);
David Brazdildcfa89b2018-10-31 11:04:10 +0000127 runtime_->SetTargetSdkVersion(
128 static_cast<uint32_t>(hiddenapi::ApiList::GreylistMaxO().GetMaxAllowedSdkVersion()) + 1);
129 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Whitelist()), false);
130 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Greylist()), false);
David Brazdil80d16282018-11-01 09:55:09 +0000131 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::GreylistMaxP()), false);
132 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::GreylistMaxO()), true);
133 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Blacklist()), true);
134
135 runtime_->SetHiddenApiEnforcementPolicy(hiddenapi::EnforcementPolicy::kEnabled);
136 runtime_->SetTargetSdkVersion(
137 static_cast<uint32_t>(hiddenapi::ApiList::GreylistMaxP().GetMaxAllowedSdkVersion()) + 1);
138 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Whitelist()), false);
139 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Greylist()), false);
140 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::GreylistMaxP()), true);
David Brazdildcfa89b2018-10-31 11:04:10 +0000141 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::GreylistMaxO()), true);
142 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Blacklist()), true);
Mathew Inwooda8503d92018-04-05 16:10:25 +0100143}
144
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000145TEST_F(HiddenApiTest, CheckMembersRead) {
146 ASSERT_NE(nullptr, class1_field1_);
147 ASSERT_NE(nullptr, class1_field12_);
148 ASSERT_NE(nullptr, class1_init_);
149 ASSERT_NE(nullptr, class1_method1_);
150 ASSERT_NE(nullptr, class1_method1_i_);
151 ASSERT_NE(nullptr, class1_method12_);
152 ASSERT_NE(nullptr, class12_field1_);
153 ASSERT_NE(nullptr, class12_method1_);
154 ASSERT_NE(nullptr, class2_field1_);
155 ASSERT_NE(nullptr, class2_method1_);
156 ASSERT_NE(nullptr, class2_method1_i_);
157 ASSERT_NE(nullptr, class3_field1_);
158 ASSERT_NE(nullptr, class3_method1_);
159 ASSERT_NE(nullptr, class3_method1_i_);
160}
161
162TEST_F(HiddenApiTest, CheckEverythingMatchesL) {
163 ScopedObjectAccess soa(self_);
164 std::string prefix("L");
Andreas Gampeaa120012018-03-28 16:23:24 -0700165 ASSERT_TRUE(MemberSignature(class1_field1_).DoesPrefixMatch(prefix));
166 ASSERT_TRUE(MemberSignature(class1_field12_).DoesPrefixMatch(prefix));
167 ASSERT_TRUE(MemberSignature(class1_init_).DoesPrefixMatch(prefix));
168 ASSERT_TRUE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix));
169 ASSERT_TRUE(MemberSignature(class1_method1_i_).DoesPrefixMatch(prefix));
170 ASSERT_TRUE(MemberSignature(class12_field1_).DoesPrefixMatch(prefix));
171 ASSERT_TRUE(MemberSignature(class12_method1_).DoesPrefixMatch(prefix));
172 ASSERT_TRUE(MemberSignature(class1_method12_).DoesPrefixMatch(prefix));
173 ASSERT_TRUE(MemberSignature(class2_field1_).DoesPrefixMatch(prefix));
174 ASSERT_TRUE(MemberSignature(class2_method1_).DoesPrefixMatch(prefix));
175 ASSERT_TRUE(MemberSignature(class2_method1_i_).DoesPrefixMatch(prefix));
176 ASSERT_TRUE(MemberSignature(class3_field1_).DoesPrefixMatch(prefix));
177 ASSERT_TRUE(MemberSignature(class3_method1_).DoesPrefixMatch(prefix));
178 ASSERT_TRUE(MemberSignature(class3_method1_i_).DoesPrefixMatch(prefix));
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000179}
180
181TEST_F(HiddenApiTest, CheckPackageMatch) {
182 ScopedObjectAccess soa(self_);
183 std::string prefix("Lmypackage/packagea/");
Andreas Gampeaa120012018-03-28 16:23:24 -0700184 ASSERT_TRUE(MemberSignature(class1_field1_).DoesPrefixMatch(prefix));
185 ASSERT_TRUE(MemberSignature(class1_field12_).DoesPrefixMatch(prefix));
186 ASSERT_TRUE(MemberSignature(class1_init_).DoesPrefixMatch(prefix));
187 ASSERT_TRUE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix));
188 ASSERT_TRUE(MemberSignature(class1_method1_i_).DoesPrefixMatch(prefix));
189 ASSERT_TRUE(MemberSignature(class1_method12_).DoesPrefixMatch(prefix));
190 ASSERT_TRUE(MemberSignature(class12_field1_).DoesPrefixMatch(prefix));
191 ASSERT_TRUE(MemberSignature(class12_method1_).DoesPrefixMatch(prefix));
192 ASSERT_TRUE(MemberSignature(class2_field1_).DoesPrefixMatch(prefix));
193 ASSERT_TRUE(MemberSignature(class2_method1_).DoesPrefixMatch(prefix));
194 ASSERT_TRUE(MemberSignature(class2_method1_i_).DoesPrefixMatch(prefix));
195 ASSERT_FALSE(MemberSignature(class3_field1_).DoesPrefixMatch(prefix));
196 ASSERT_FALSE(MemberSignature(class3_method1_).DoesPrefixMatch(prefix));
197 ASSERT_FALSE(MemberSignature(class3_method1_i_).DoesPrefixMatch(prefix));
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000198}
199
200TEST_F(HiddenApiTest, CheckClassMatch) {
201 ScopedObjectAccess soa(self_);
202 std::string prefix("Lmypackage/packagea/Class1");
Andreas Gampeaa120012018-03-28 16:23:24 -0700203 ASSERT_TRUE(MemberSignature(class1_field1_).DoesPrefixMatch(prefix));
204 ASSERT_TRUE(MemberSignature(class1_field12_).DoesPrefixMatch(prefix));
205 ASSERT_TRUE(MemberSignature(class1_init_).DoesPrefixMatch(prefix));
206 ASSERT_TRUE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix));
207 ASSERT_TRUE(MemberSignature(class1_method1_i_).DoesPrefixMatch(prefix));
208 ASSERT_TRUE(MemberSignature(class1_method12_).DoesPrefixMatch(prefix));
209 ASSERT_TRUE(MemberSignature(class12_field1_).DoesPrefixMatch(prefix));
210 ASSERT_TRUE(MemberSignature(class12_method1_).DoesPrefixMatch(prefix));
211 ASSERT_FALSE(MemberSignature(class2_field1_).DoesPrefixMatch(prefix));
212 ASSERT_FALSE(MemberSignature(class2_method1_).DoesPrefixMatch(prefix));
213 ASSERT_FALSE(MemberSignature(class2_method1_i_).DoesPrefixMatch(prefix));
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000214}
215
216TEST_F(HiddenApiTest, CheckClassExactMatch) {
217 ScopedObjectAccess soa(self_);
218 std::string prefix("Lmypackage/packagea/Class1;");
Andreas Gampeaa120012018-03-28 16:23:24 -0700219 ASSERT_TRUE(MemberSignature(class1_field1_).DoesPrefixMatch(prefix));
220 ASSERT_TRUE(MemberSignature(class1_field12_).DoesPrefixMatch(prefix));
221 ASSERT_TRUE(MemberSignature(class1_init_).DoesPrefixMatch(prefix));
222 ASSERT_TRUE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix));
223 ASSERT_TRUE(MemberSignature(class1_method1_i_).DoesPrefixMatch(prefix));
224 ASSERT_FALSE(MemberSignature(class12_field1_).DoesPrefixMatch(prefix));
225 ASSERT_FALSE(MemberSignature(class12_method1_).DoesPrefixMatch(prefix));
226 ASSERT_FALSE(MemberSignature(class2_field1_).DoesPrefixMatch(prefix));
227 ASSERT_FALSE(MemberSignature(class2_method1_).DoesPrefixMatch(prefix));
228 ASSERT_FALSE(MemberSignature(class2_method1_i_).DoesPrefixMatch(prefix));
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000229}
230
231TEST_F(HiddenApiTest, CheckMethodMatch) {
232 ScopedObjectAccess soa(self_);
233 std::string prefix("Lmypackage/packagea/Class1;->method1");
Andreas Gampeaa120012018-03-28 16:23:24 -0700234 ASSERT_FALSE(MemberSignature(class1_field1_).DoesPrefixMatch(prefix));
235 ASSERT_FALSE(MemberSignature(class1_field12_).DoesPrefixMatch(prefix));
236 ASSERT_FALSE(MemberSignature(class1_init_).DoesPrefixMatch(prefix));
237 ASSERT_TRUE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix));
238 ASSERT_TRUE(MemberSignature(class1_method1_i_).DoesPrefixMatch(prefix));
239 ASSERT_TRUE(MemberSignature(class1_method12_).DoesPrefixMatch(prefix));
240 ASSERT_FALSE(MemberSignature(class12_field1_).DoesPrefixMatch(prefix));
241 ASSERT_FALSE(MemberSignature(class12_method1_).DoesPrefixMatch(prefix));
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000242}
243
244TEST_F(HiddenApiTest, CheckMethodExactMatch) {
245 ScopedObjectAccess soa(self_);
246 std::string prefix("Lmypackage/packagea/Class1;->method1(");
Andreas Gampeaa120012018-03-28 16:23:24 -0700247 ASSERT_FALSE(MemberSignature(class1_field1_).DoesPrefixMatch(prefix));
248 ASSERT_FALSE(MemberSignature(class1_field12_).DoesPrefixMatch(prefix));
249 ASSERT_FALSE(MemberSignature(class1_init_).DoesPrefixMatch(prefix));
250 ASSERT_TRUE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix));
251 ASSERT_TRUE(MemberSignature(class1_method1_i_).DoesPrefixMatch(prefix));
252 ASSERT_FALSE(MemberSignature(class1_method12_).DoesPrefixMatch(prefix));
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000253}
254
255TEST_F(HiddenApiTest, CheckMethodSignatureMatch) {
256 ScopedObjectAccess soa(self_);
257 std::string prefix("Lmypackage/packagea/Class1;->method1(I)");
Andreas Gampeaa120012018-03-28 16:23:24 -0700258 ASSERT_FALSE(MemberSignature(class1_field1_).DoesPrefixMatch(prefix));
259 ASSERT_FALSE(MemberSignature(class1_field12_).DoesPrefixMatch(prefix));
260 ASSERT_FALSE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix));
261 ASSERT_TRUE(MemberSignature(class1_method1_i_).DoesPrefixMatch(prefix));
262 ASSERT_FALSE(MemberSignature(class1_method12_).DoesPrefixMatch(prefix));
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000263}
264
265TEST_F(HiddenApiTest, CheckMethodSignatureAndReturnMatch) {
266 ScopedObjectAccess soa(self_);
267 std::string prefix("Lmypackage/packagea/Class1;->method1()V");
Andreas Gampeaa120012018-03-28 16:23:24 -0700268 ASSERT_FALSE(MemberSignature(class1_field1_).DoesPrefixMatch(prefix));
269 ASSERT_FALSE(MemberSignature(class1_field12_).DoesPrefixMatch(prefix));
270 ASSERT_TRUE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix));
271 ASSERT_FALSE(MemberSignature(class1_method1_i_).DoesPrefixMatch(prefix));
272 ASSERT_FALSE(MemberSignature(class1_method12_).DoesPrefixMatch(prefix));
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000273}
274
275TEST_F(HiddenApiTest, CheckFieldMatch) {
276 ScopedObjectAccess soa(self_);
277 std::string prefix("Lmypackage/packagea/Class1;->field1");
Andreas Gampeaa120012018-03-28 16:23:24 -0700278 ASSERT_TRUE(MemberSignature(class1_field1_).DoesPrefixMatch(prefix));
279 ASSERT_TRUE(MemberSignature(class1_field12_).DoesPrefixMatch(prefix));
280 ASSERT_FALSE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix));
281 ASSERT_FALSE(MemberSignature(class1_method1_i_).DoesPrefixMatch(prefix));
282 ASSERT_FALSE(MemberSignature(class1_method12_).DoesPrefixMatch(prefix));
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000283}
284
285TEST_F(HiddenApiTest, CheckFieldExactMatch) {
286 ScopedObjectAccess soa(self_);
287 std::string prefix("Lmypackage/packagea/Class1;->field1:");
Andreas Gampeaa120012018-03-28 16:23:24 -0700288 ASSERT_TRUE(MemberSignature(class1_field1_).DoesPrefixMatch(prefix));
289 ASSERT_FALSE(MemberSignature(class1_field12_).DoesPrefixMatch(prefix));
290 ASSERT_FALSE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix));
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000291}
292
293TEST_F(HiddenApiTest, CheckFieldTypeMatch) {
294 ScopedObjectAccess soa(self_);
295 std::string prefix("Lmypackage/packagea/Class1;->field1:I");
Andreas Gampeaa120012018-03-28 16:23:24 -0700296 ASSERT_TRUE(MemberSignature(class1_field1_).DoesPrefixMatch(prefix));
297 ASSERT_FALSE(MemberSignature(class1_field12_).DoesPrefixMatch(prefix));
298 ASSERT_FALSE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix));
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000299}
300
301TEST_F(HiddenApiTest, CheckConstructorMatch) {
302 ScopedObjectAccess soa(self_);
303 std::string prefix("Lmypackage/packagea/Class1;-><init>");
Andreas Gampeaa120012018-03-28 16:23:24 -0700304 ASSERT_TRUE(MemberSignature(class1_init_).DoesPrefixMatch(prefix));
305 ASSERT_FALSE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix));
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000306}
307
308TEST_F(HiddenApiTest, CheckConstructorExactMatch) {
309 ScopedObjectAccess soa(self_);
310 std::string prefix("Lmypackage/packagea/Class1;-><init>()V");
Andreas Gampeaa120012018-03-28 16:23:24 -0700311 ASSERT_TRUE(MemberSignature(class1_init_).DoesPrefixMatch(prefix));
312 ASSERT_FALSE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix));
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000313}
314
315TEST_F(HiddenApiTest, CheckMethodSignatureTrailingCharsNoMatch) {
316 ScopedObjectAccess soa(self_);
317 std::string prefix("Lmypackage/packagea/Class1;->method1()Vfoo");
Andreas Gampeaa120012018-03-28 16:23:24 -0700318 ASSERT_FALSE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix));
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000319}
320
321TEST_F(HiddenApiTest, CheckConstructorTrailingCharsNoMatch) {
322 ScopedObjectAccess soa(self_);
323 std::string prefix("Lmypackage/packagea/Class1;-><init>()Vfoo");
Andreas Gampeaa120012018-03-28 16:23:24 -0700324 ASSERT_FALSE(MemberSignature(class1_init_).DoesPrefixMatch(prefix));
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000325}
326
327TEST_F(HiddenApiTest, CheckFieldTrailingCharsNoMatch) {
328 ScopedObjectAccess soa(self_);
329 std::string prefix("Lmypackage/packagea/Class1;->field1:Ifoo");
Andreas Gampeaa120012018-03-28 16:23:24 -0700330 ASSERT_FALSE(MemberSignature(class1_field1_).DoesPrefixMatch(prefix));
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000331}
332
David Brazdil1f9d3c32018-05-02 16:53:06 +0100333TEST_F(HiddenApiTest, CheckMemberSignatureForProxyClass) {
334 ScopedObjectAccess soa(self_);
335 StackHandleScope<4> hs(soa.Self());
336 Handle<mirror::ClassLoader> class_loader(
337 hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader_)));
338
339 // Find interface we will create a proxy for.
340 Handle<mirror::Class> h_iface(hs.NewHandle(
341 class_linker_->FindClass(soa.Self(), "Lmypackage/packagea/Interface;", class_loader)));
342 ASSERT_TRUE(h_iface != nullptr);
343
344 // Create the proxy class.
Vladimir Markoa8bba7d2018-05-30 15:18:48 +0100345 std::vector<Handle<mirror::Class>> interfaces;
346 interfaces.push_back(h_iface);
David Brazdil1f9d3c32018-05-02 16:53:06 +0100347 Handle<mirror::Class> proxyClass = hs.NewHandle(proxy_test::GenerateProxyClass(
348 soa, jclass_loader_, runtime_->GetClassLinker(), "$Proxy1234", interfaces));
349 ASSERT_TRUE(proxyClass != nullptr);
350 ASSERT_TRUE(proxyClass->IsProxyClass());
351 ASSERT_TRUE(proxyClass->IsInitialized());
352
353 // Find the "method" virtual method.
354 ArtMethod* method = nullptr;
355 for (auto& m : proxyClass->GetDeclaredVirtualMethods(kRuntimePointerSize)) {
356 if (strcmp("method", m.GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetName()) == 0) {
357 method = &m;
358 break;
359 }
360 }
361 ASSERT_TRUE(method != nullptr);
362
363 // Find the "interfaces" static field. This is generated for all proxies.
364 ArtField* field = nullptr;
365 for (size_t i = 0; i < proxyClass->NumStaticFields(); ++i) {
366 ArtField* f = proxyClass->GetStaticField(i);
367 if (strcmp("interfaces", f->GetName()) == 0) {
368 field = f;
369 break;
370 }
371 }
372 ASSERT_TRUE(field != nullptr);
373
374 // Test the signature. We expect the signature from the interface class.
375 std::ostringstream ss_method;
376 MemberSignature(method).Dump(ss_method);
377 ASSERT_EQ("Lmypackage/packagea/Interface;->method()V", ss_method.str());
378
379 // Test the signature. We expect the signature of the proxy class.
380 std::ostringstream ss_field;
381 MemberSignature(field).Dump(ss_field);
382 ASSERT_EQ("L$Proxy1234;->interfaces:[Ljava/lang/Class;", ss_field.str());
383}
384
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000385} // namespace art