blob: 1f83c052ebc3cf62d20de550d32380c9440d2ee3 [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
Mathew Inwooda8503d92018-04-05 16:10:25 +0100103 runtime_->SetHiddenApiEnforcementPolicy(hiddenapi::EnforcementPolicy::kJustWarn);
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
David Brazdilf50ac102018-10-17 18:00:06 +0100110 runtime_->SetHiddenApiEnforcementPolicy(hiddenapi::EnforcementPolicy::kEnabled);
David Brazdildcfa89b2018-10-31 11:04:10 +0000111 runtime_->SetTargetSdkVersion(
112 static_cast<uint32_t>(hiddenapi::ApiList::GreylistMaxO().GetMaxAllowedSdkVersion()));
113 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Whitelist()), false);
114 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Greylist()), false);
David Brazdil80d16282018-11-01 09:55:09 +0000115 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::GreylistMaxP()), false);
David Brazdildcfa89b2018-10-31 11:04:10 +0000116 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::GreylistMaxO()), false);
117 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Blacklist()), true);
Mathew Inwooda8503d92018-04-05 16:10:25 +0100118
David Brazdilf50ac102018-10-17 18:00:06 +0100119 runtime_->SetHiddenApiEnforcementPolicy(hiddenapi::EnforcementPolicy::kEnabled);
David Brazdildcfa89b2018-10-31 11:04:10 +0000120 runtime_->SetTargetSdkVersion(
121 static_cast<uint32_t>(hiddenapi::ApiList::GreylistMaxO().GetMaxAllowedSdkVersion()) + 1);
122 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Whitelist()), false);
123 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Greylist()), false);
David Brazdil80d16282018-11-01 09:55:09 +0000124 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::GreylistMaxP()), false);
125 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::GreylistMaxO()), true);
126 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Blacklist()), true);
127
128 runtime_->SetHiddenApiEnforcementPolicy(hiddenapi::EnforcementPolicy::kEnabled);
129 runtime_->SetTargetSdkVersion(
130 static_cast<uint32_t>(hiddenapi::ApiList::GreylistMaxP().GetMaxAllowedSdkVersion()) + 1);
131 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Whitelist()), false);
132 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Greylist()), false);
133 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::GreylistMaxP()), true);
David Brazdildcfa89b2018-10-31 11:04:10 +0000134 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::GreylistMaxO()), true);
135 ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Blacklist()), true);
Mathew Inwooda8503d92018-04-05 16:10:25 +0100136}
137
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000138TEST_F(HiddenApiTest, CheckMembersRead) {
139 ASSERT_NE(nullptr, class1_field1_);
140 ASSERT_NE(nullptr, class1_field12_);
141 ASSERT_NE(nullptr, class1_init_);
142 ASSERT_NE(nullptr, class1_method1_);
143 ASSERT_NE(nullptr, class1_method1_i_);
144 ASSERT_NE(nullptr, class1_method12_);
145 ASSERT_NE(nullptr, class12_field1_);
146 ASSERT_NE(nullptr, class12_method1_);
147 ASSERT_NE(nullptr, class2_field1_);
148 ASSERT_NE(nullptr, class2_method1_);
149 ASSERT_NE(nullptr, class2_method1_i_);
150 ASSERT_NE(nullptr, class3_field1_);
151 ASSERT_NE(nullptr, class3_method1_);
152 ASSERT_NE(nullptr, class3_method1_i_);
153}
154
155TEST_F(HiddenApiTest, CheckEverythingMatchesL) {
156 ScopedObjectAccess soa(self_);
157 std::string prefix("L");
Andreas Gampeaa120012018-03-28 16:23:24 -0700158 ASSERT_TRUE(MemberSignature(class1_field1_).DoesPrefixMatch(prefix));
159 ASSERT_TRUE(MemberSignature(class1_field12_).DoesPrefixMatch(prefix));
160 ASSERT_TRUE(MemberSignature(class1_init_).DoesPrefixMatch(prefix));
161 ASSERT_TRUE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix));
162 ASSERT_TRUE(MemberSignature(class1_method1_i_).DoesPrefixMatch(prefix));
163 ASSERT_TRUE(MemberSignature(class12_field1_).DoesPrefixMatch(prefix));
164 ASSERT_TRUE(MemberSignature(class12_method1_).DoesPrefixMatch(prefix));
165 ASSERT_TRUE(MemberSignature(class1_method12_).DoesPrefixMatch(prefix));
166 ASSERT_TRUE(MemberSignature(class2_field1_).DoesPrefixMatch(prefix));
167 ASSERT_TRUE(MemberSignature(class2_method1_).DoesPrefixMatch(prefix));
168 ASSERT_TRUE(MemberSignature(class2_method1_i_).DoesPrefixMatch(prefix));
169 ASSERT_TRUE(MemberSignature(class3_field1_).DoesPrefixMatch(prefix));
170 ASSERT_TRUE(MemberSignature(class3_method1_).DoesPrefixMatch(prefix));
171 ASSERT_TRUE(MemberSignature(class3_method1_i_).DoesPrefixMatch(prefix));
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000172}
173
174TEST_F(HiddenApiTest, CheckPackageMatch) {
175 ScopedObjectAccess soa(self_);
176 std::string prefix("Lmypackage/packagea/");
Andreas Gampeaa120012018-03-28 16:23:24 -0700177 ASSERT_TRUE(MemberSignature(class1_field1_).DoesPrefixMatch(prefix));
178 ASSERT_TRUE(MemberSignature(class1_field12_).DoesPrefixMatch(prefix));
179 ASSERT_TRUE(MemberSignature(class1_init_).DoesPrefixMatch(prefix));
180 ASSERT_TRUE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix));
181 ASSERT_TRUE(MemberSignature(class1_method1_i_).DoesPrefixMatch(prefix));
182 ASSERT_TRUE(MemberSignature(class1_method12_).DoesPrefixMatch(prefix));
183 ASSERT_TRUE(MemberSignature(class12_field1_).DoesPrefixMatch(prefix));
184 ASSERT_TRUE(MemberSignature(class12_method1_).DoesPrefixMatch(prefix));
185 ASSERT_TRUE(MemberSignature(class2_field1_).DoesPrefixMatch(prefix));
186 ASSERT_TRUE(MemberSignature(class2_method1_).DoesPrefixMatch(prefix));
187 ASSERT_TRUE(MemberSignature(class2_method1_i_).DoesPrefixMatch(prefix));
188 ASSERT_FALSE(MemberSignature(class3_field1_).DoesPrefixMatch(prefix));
189 ASSERT_FALSE(MemberSignature(class3_method1_).DoesPrefixMatch(prefix));
190 ASSERT_FALSE(MemberSignature(class3_method1_i_).DoesPrefixMatch(prefix));
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000191}
192
193TEST_F(HiddenApiTest, CheckClassMatch) {
194 ScopedObjectAccess soa(self_);
195 std::string prefix("Lmypackage/packagea/Class1");
Andreas Gampeaa120012018-03-28 16:23:24 -0700196 ASSERT_TRUE(MemberSignature(class1_field1_).DoesPrefixMatch(prefix));
197 ASSERT_TRUE(MemberSignature(class1_field12_).DoesPrefixMatch(prefix));
198 ASSERT_TRUE(MemberSignature(class1_init_).DoesPrefixMatch(prefix));
199 ASSERT_TRUE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix));
200 ASSERT_TRUE(MemberSignature(class1_method1_i_).DoesPrefixMatch(prefix));
201 ASSERT_TRUE(MemberSignature(class1_method12_).DoesPrefixMatch(prefix));
202 ASSERT_TRUE(MemberSignature(class12_field1_).DoesPrefixMatch(prefix));
203 ASSERT_TRUE(MemberSignature(class12_method1_).DoesPrefixMatch(prefix));
204 ASSERT_FALSE(MemberSignature(class2_field1_).DoesPrefixMatch(prefix));
205 ASSERT_FALSE(MemberSignature(class2_method1_).DoesPrefixMatch(prefix));
206 ASSERT_FALSE(MemberSignature(class2_method1_i_).DoesPrefixMatch(prefix));
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000207}
208
209TEST_F(HiddenApiTest, CheckClassExactMatch) {
210 ScopedObjectAccess soa(self_);
211 std::string prefix("Lmypackage/packagea/Class1;");
Andreas Gampeaa120012018-03-28 16:23:24 -0700212 ASSERT_TRUE(MemberSignature(class1_field1_).DoesPrefixMatch(prefix));
213 ASSERT_TRUE(MemberSignature(class1_field12_).DoesPrefixMatch(prefix));
214 ASSERT_TRUE(MemberSignature(class1_init_).DoesPrefixMatch(prefix));
215 ASSERT_TRUE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix));
216 ASSERT_TRUE(MemberSignature(class1_method1_i_).DoesPrefixMatch(prefix));
217 ASSERT_FALSE(MemberSignature(class12_field1_).DoesPrefixMatch(prefix));
218 ASSERT_FALSE(MemberSignature(class12_method1_).DoesPrefixMatch(prefix));
219 ASSERT_FALSE(MemberSignature(class2_field1_).DoesPrefixMatch(prefix));
220 ASSERT_FALSE(MemberSignature(class2_method1_).DoesPrefixMatch(prefix));
221 ASSERT_FALSE(MemberSignature(class2_method1_i_).DoesPrefixMatch(prefix));
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000222}
223
224TEST_F(HiddenApiTest, CheckMethodMatch) {
225 ScopedObjectAccess soa(self_);
226 std::string prefix("Lmypackage/packagea/Class1;->method1");
Andreas Gampeaa120012018-03-28 16:23:24 -0700227 ASSERT_FALSE(MemberSignature(class1_field1_).DoesPrefixMatch(prefix));
228 ASSERT_FALSE(MemberSignature(class1_field12_).DoesPrefixMatch(prefix));
229 ASSERT_FALSE(MemberSignature(class1_init_).DoesPrefixMatch(prefix));
230 ASSERT_TRUE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix));
231 ASSERT_TRUE(MemberSignature(class1_method1_i_).DoesPrefixMatch(prefix));
232 ASSERT_TRUE(MemberSignature(class1_method12_).DoesPrefixMatch(prefix));
233 ASSERT_FALSE(MemberSignature(class12_field1_).DoesPrefixMatch(prefix));
234 ASSERT_FALSE(MemberSignature(class12_method1_).DoesPrefixMatch(prefix));
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000235}
236
237TEST_F(HiddenApiTest, CheckMethodExactMatch) {
238 ScopedObjectAccess soa(self_);
239 std::string prefix("Lmypackage/packagea/Class1;->method1(");
Andreas Gampeaa120012018-03-28 16:23:24 -0700240 ASSERT_FALSE(MemberSignature(class1_field1_).DoesPrefixMatch(prefix));
241 ASSERT_FALSE(MemberSignature(class1_field12_).DoesPrefixMatch(prefix));
242 ASSERT_FALSE(MemberSignature(class1_init_).DoesPrefixMatch(prefix));
243 ASSERT_TRUE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix));
244 ASSERT_TRUE(MemberSignature(class1_method1_i_).DoesPrefixMatch(prefix));
245 ASSERT_FALSE(MemberSignature(class1_method12_).DoesPrefixMatch(prefix));
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000246}
247
248TEST_F(HiddenApiTest, CheckMethodSignatureMatch) {
249 ScopedObjectAccess soa(self_);
250 std::string prefix("Lmypackage/packagea/Class1;->method1(I)");
Andreas Gampeaa120012018-03-28 16:23:24 -0700251 ASSERT_FALSE(MemberSignature(class1_field1_).DoesPrefixMatch(prefix));
252 ASSERT_FALSE(MemberSignature(class1_field12_).DoesPrefixMatch(prefix));
253 ASSERT_FALSE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix));
254 ASSERT_TRUE(MemberSignature(class1_method1_i_).DoesPrefixMatch(prefix));
255 ASSERT_FALSE(MemberSignature(class1_method12_).DoesPrefixMatch(prefix));
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000256}
257
258TEST_F(HiddenApiTest, CheckMethodSignatureAndReturnMatch) {
259 ScopedObjectAccess soa(self_);
260 std::string prefix("Lmypackage/packagea/Class1;->method1()V");
Andreas Gampeaa120012018-03-28 16:23:24 -0700261 ASSERT_FALSE(MemberSignature(class1_field1_).DoesPrefixMatch(prefix));
262 ASSERT_FALSE(MemberSignature(class1_field12_).DoesPrefixMatch(prefix));
263 ASSERT_TRUE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix));
264 ASSERT_FALSE(MemberSignature(class1_method1_i_).DoesPrefixMatch(prefix));
265 ASSERT_FALSE(MemberSignature(class1_method12_).DoesPrefixMatch(prefix));
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000266}
267
268TEST_F(HiddenApiTest, CheckFieldMatch) {
269 ScopedObjectAccess soa(self_);
270 std::string prefix("Lmypackage/packagea/Class1;->field1");
Andreas Gampeaa120012018-03-28 16:23:24 -0700271 ASSERT_TRUE(MemberSignature(class1_field1_).DoesPrefixMatch(prefix));
272 ASSERT_TRUE(MemberSignature(class1_field12_).DoesPrefixMatch(prefix));
273 ASSERT_FALSE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix));
274 ASSERT_FALSE(MemberSignature(class1_method1_i_).DoesPrefixMatch(prefix));
275 ASSERT_FALSE(MemberSignature(class1_method12_).DoesPrefixMatch(prefix));
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000276}
277
278TEST_F(HiddenApiTest, CheckFieldExactMatch) {
279 ScopedObjectAccess soa(self_);
280 std::string prefix("Lmypackage/packagea/Class1;->field1:");
Andreas Gampeaa120012018-03-28 16:23:24 -0700281 ASSERT_TRUE(MemberSignature(class1_field1_).DoesPrefixMatch(prefix));
282 ASSERT_FALSE(MemberSignature(class1_field12_).DoesPrefixMatch(prefix));
283 ASSERT_FALSE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix));
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000284}
285
286TEST_F(HiddenApiTest, CheckFieldTypeMatch) {
287 ScopedObjectAccess soa(self_);
288 std::string prefix("Lmypackage/packagea/Class1;->field1:I");
Andreas Gampeaa120012018-03-28 16:23:24 -0700289 ASSERT_TRUE(MemberSignature(class1_field1_).DoesPrefixMatch(prefix));
290 ASSERT_FALSE(MemberSignature(class1_field12_).DoesPrefixMatch(prefix));
291 ASSERT_FALSE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix));
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000292}
293
294TEST_F(HiddenApiTest, CheckConstructorMatch) {
295 ScopedObjectAccess soa(self_);
296 std::string prefix("Lmypackage/packagea/Class1;-><init>");
Andreas Gampeaa120012018-03-28 16:23:24 -0700297 ASSERT_TRUE(MemberSignature(class1_init_).DoesPrefixMatch(prefix));
298 ASSERT_FALSE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix));
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000299}
300
301TEST_F(HiddenApiTest, CheckConstructorExactMatch) {
302 ScopedObjectAccess soa(self_);
303 std::string prefix("Lmypackage/packagea/Class1;-><init>()V");
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, CheckMethodSignatureTrailingCharsNoMatch) {
309 ScopedObjectAccess soa(self_);
310 std::string prefix("Lmypackage/packagea/Class1;->method1()Vfoo");
Andreas Gampeaa120012018-03-28 16:23:24 -0700311 ASSERT_FALSE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix));
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000312}
313
314TEST_F(HiddenApiTest, CheckConstructorTrailingCharsNoMatch) {
315 ScopedObjectAccess soa(self_);
316 std::string prefix("Lmypackage/packagea/Class1;-><init>()Vfoo");
Andreas Gampeaa120012018-03-28 16:23:24 -0700317 ASSERT_FALSE(MemberSignature(class1_init_).DoesPrefixMatch(prefix));
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000318}
319
320TEST_F(HiddenApiTest, CheckFieldTrailingCharsNoMatch) {
321 ScopedObjectAccess soa(self_);
322 std::string prefix("Lmypackage/packagea/Class1;->field1:Ifoo");
Andreas Gampeaa120012018-03-28 16:23:24 -0700323 ASSERT_FALSE(MemberSignature(class1_field1_).DoesPrefixMatch(prefix));
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000324}
325
David Brazdil1f9d3c32018-05-02 16:53:06 +0100326TEST_F(HiddenApiTest, CheckMemberSignatureForProxyClass) {
327 ScopedObjectAccess soa(self_);
328 StackHandleScope<4> hs(soa.Self());
329 Handle<mirror::ClassLoader> class_loader(
330 hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader_)));
331
332 // Find interface we will create a proxy for.
333 Handle<mirror::Class> h_iface(hs.NewHandle(
334 class_linker_->FindClass(soa.Self(), "Lmypackage/packagea/Interface;", class_loader)));
335 ASSERT_TRUE(h_iface != nullptr);
336
337 // Create the proxy class.
Vladimir Markoa8bba7d2018-05-30 15:18:48 +0100338 std::vector<Handle<mirror::Class>> interfaces;
339 interfaces.push_back(h_iface);
David Brazdil1f9d3c32018-05-02 16:53:06 +0100340 Handle<mirror::Class> proxyClass = hs.NewHandle(proxy_test::GenerateProxyClass(
341 soa, jclass_loader_, runtime_->GetClassLinker(), "$Proxy1234", interfaces));
342 ASSERT_TRUE(proxyClass != nullptr);
343 ASSERT_TRUE(proxyClass->IsProxyClass());
344 ASSERT_TRUE(proxyClass->IsInitialized());
345
346 // Find the "method" virtual method.
347 ArtMethod* method = nullptr;
348 for (auto& m : proxyClass->GetDeclaredVirtualMethods(kRuntimePointerSize)) {
349 if (strcmp("method", m.GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetName()) == 0) {
350 method = &m;
351 break;
352 }
353 }
354 ASSERT_TRUE(method != nullptr);
355
356 // Find the "interfaces" static field. This is generated for all proxies.
357 ArtField* field = nullptr;
358 for (size_t i = 0; i < proxyClass->NumStaticFields(); ++i) {
359 ArtField* f = proxyClass->GetStaticField(i);
360 if (strcmp("interfaces", f->GetName()) == 0) {
361 field = f;
362 break;
363 }
364 }
365 ASSERT_TRUE(field != nullptr);
366
367 // Test the signature. We expect the signature from the interface class.
368 std::ostringstream ss_method;
369 MemberSignature(method).Dump(ss_method);
370 ASSERT_EQ("Lmypackage/packagea/Interface;->method()V", ss_method.str());
371
372 // Test the signature. We expect the signature of the proxy class.
373 std::ostringstream ss_field;
374 MemberSignature(field).Dump(ss_field);
375 ASSERT_EQ("L$Proxy1234;->interfaces:[Ljava/lang/Class;", ss_field.str());
376}
377
Mathew Inwood7d74ef52018-03-16 14:18:33 +0000378} // namespace art