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 | 2bb2fbd | 2018-11-13 18:24:26 +0000 | [diff] [blame] | 19 | #include "base/sdk_version.h" |
Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 20 | #include "common_runtime_test.h" |
Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 21 | #include "jni/jni_internal.h" |
David Brazdil | 1f9d3c3 | 2018-05-02 16:53:06 +0100 | [diff] [blame] | 22 | #include "proxy_test.h" |
Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 23 | |
| 24 | namespace art { |
| 25 | |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 26 | using hiddenapi::detail::MemberSignature; |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 27 | using hiddenapi::detail::ShouldDenyAccessToMemberImpl; |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 28 | |
Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 29 | class HiddenApiTest : public CommonRuntimeTest { |
| 30 | protected: |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 31 | void SetUp() override { |
Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 32 | // Do the normal setup. |
| 33 | CommonRuntimeTest::SetUp(); |
| 34 | self_ = Thread::Current(); |
| 35 | self_->TransitionFromSuspendedToRunnable(); |
David Brazdil | 1f9d3c3 | 2018-05-02 16:53:06 +0100 | [diff] [blame] | 36 | jclass_loader_ = LoadDex("HiddenApiSignatures"); |
Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 37 | 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 Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 72 | 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 Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 81 | protected: |
| 82 | Thread* self_; |
David Brazdil | 1f9d3c3 | 2018-05-02 16:53:06 +0100 | [diff] [blame] | 83 | jobject jclass_loader_; |
Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 84 | 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 Inwood | a8503d9 | 2018-04-05 16:10:25 +0100 | [diff] [blame] | 100 | TEST_F(HiddenApiTest, CheckGetActionFromRuntimeFlags) { |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 101 | ScopedObjectAccess soa(self_); |
| 102 | |
| 103 | runtime_->SetHiddenApiEnforcementPolicy(hiddenapi::EnforcementPolicy::kDisabled); |
David Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame] | 104 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Whitelist()), false); |
| 105 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Greylist()), false); |
David Brazdil | 80d1628 | 2018-11-01 09:55:09 +0000 | [diff] [blame] | 106 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::GreylistMaxP()), false); |
David Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame] | 107 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::GreylistMaxO()), false); |
| 108 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Blacklist()), false); |
Mathew Inwood | a8503d9 | 2018-04-05 16:10:25 +0100 | [diff] [blame] | 109 | |
| 110 | runtime_->SetHiddenApiEnforcementPolicy(hiddenapi::EnforcementPolicy::kJustWarn); |
David Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame] | 111 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Whitelist()), false); |
| 112 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Greylist()), false); |
David Brazdil | 80d1628 | 2018-11-01 09:55:09 +0000 | [diff] [blame] | 113 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::GreylistMaxP()), false); |
David Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame] | 114 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::GreylistMaxO()), false); |
| 115 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Blacklist()), false); |
Mathew Inwood | a8503d9 | 2018-04-05 16:10:25 +0100 | [diff] [blame] | 116 | |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 117 | runtime_->SetHiddenApiEnforcementPolicy(hiddenapi::EnforcementPolicy::kEnabled); |
David Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame] | 118 | 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 Brazdil | 80d1628 | 2018-11-01 09:55:09 +0000 | [diff] [blame] | 122 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::GreylistMaxP()), false); |
David Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame] | 123 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::GreylistMaxO()), false); |
| 124 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Blacklist()), true); |
Mathew Inwood | a8503d9 | 2018-04-05 16:10:25 +0100 | [diff] [blame] | 125 | |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 126 | runtime_->SetHiddenApiEnforcementPolicy(hiddenapi::EnforcementPolicy::kEnabled); |
David Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame] | 127 | 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 Brazdil | 80d1628 | 2018-11-01 09:55:09 +0000 | [diff] [blame] | 131 | 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 Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame] | 141 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::GreylistMaxO()), true); |
| 142 | ASSERT_EQ(ShouldDenyAccess(hiddenapi::ApiList::Blacklist()), true); |
Mathew Inwood | a8503d9 | 2018-04-05 16:10:25 +0100 | [diff] [blame] | 143 | } |
| 144 | |
Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 145 | TEST_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 | |
| 162 | TEST_F(HiddenApiTest, CheckEverythingMatchesL) { |
| 163 | ScopedObjectAccess soa(self_); |
| 164 | std::string prefix("L"); |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 165 | 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 Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | TEST_F(HiddenApiTest, CheckPackageMatch) { |
| 182 | ScopedObjectAccess soa(self_); |
| 183 | std::string prefix("Lmypackage/packagea/"); |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 184 | 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 Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | TEST_F(HiddenApiTest, CheckClassMatch) { |
| 201 | ScopedObjectAccess soa(self_); |
| 202 | std::string prefix("Lmypackage/packagea/Class1"); |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 203 | 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 Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | TEST_F(HiddenApiTest, CheckClassExactMatch) { |
| 217 | ScopedObjectAccess soa(self_); |
| 218 | std::string prefix("Lmypackage/packagea/Class1;"); |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 219 | 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 Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | TEST_F(HiddenApiTest, CheckMethodMatch) { |
| 232 | ScopedObjectAccess soa(self_); |
| 233 | std::string prefix("Lmypackage/packagea/Class1;->method1"); |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 234 | 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 Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | TEST_F(HiddenApiTest, CheckMethodExactMatch) { |
| 245 | ScopedObjectAccess soa(self_); |
| 246 | std::string prefix("Lmypackage/packagea/Class1;->method1("); |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 247 | 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 Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | TEST_F(HiddenApiTest, CheckMethodSignatureMatch) { |
| 256 | ScopedObjectAccess soa(self_); |
| 257 | std::string prefix("Lmypackage/packagea/Class1;->method1(I)"); |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 258 | 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 Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | TEST_F(HiddenApiTest, CheckMethodSignatureAndReturnMatch) { |
| 266 | ScopedObjectAccess soa(self_); |
| 267 | std::string prefix("Lmypackage/packagea/Class1;->method1()V"); |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 268 | 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 Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | TEST_F(HiddenApiTest, CheckFieldMatch) { |
| 276 | ScopedObjectAccess soa(self_); |
| 277 | std::string prefix("Lmypackage/packagea/Class1;->field1"); |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 278 | 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 Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | TEST_F(HiddenApiTest, CheckFieldExactMatch) { |
| 286 | ScopedObjectAccess soa(self_); |
| 287 | std::string prefix("Lmypackage/packagea/Class1;->field1:"); |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 288 | ASSERT_TRUE(MemberSignature(class1_field1_).DoesPrefixMatch(prefix)); |
| 289 | ASSERT_FALSE(MemberSignature(class1_field12_).DoesPrefixMatch(prefix)); |
| 290 | ASSERT_FALSE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix)); |
Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | TEST_F(HiddenApiTest, CheckFieldTypeMatch) { |
| 294 | ScopedObjectAccess soa(self_); |
| 295 | std::string prefix("Lmypackage/packagea/Class1;->field1:I"); |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 296 | ASSERT_TRUE(MemberSignature(class1_field1_).DoesPrefixMatch(prefix)); |
| 297 | ASSERT_FALSE(MemberSignature(class1_field12_).DoesPrefixMatch(prefix)); |
| 298 | ASSERT_FALSE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix)); |
Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | TEST_F(HiddenApiTest, CheckConstructorMatch) { |
| 302 | ScopedObjectAccess soa(self_); |
| 303 | std::string prefix("Lmypackage/packagea/Class1;-><init>"); |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 304 | ASSERT_TRUE(MemberSignature(class1_init_).DoesPrefixMatch(prefix)); |
| 305 | ASSERT_FALSE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix)); |
Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | TEST_F(HiddenApiTest, CheckConstructorExactMatch) { |
| 309 | ScopedObjectAccess soa(self_); |
| 310 | std::string prefix("Lmypackage/packagea/Class1;-><init>()V"); |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 311 | ASSERT_TRUE(MemberSignature(class1_init_).DoesPrefixMatch(prefix)); |
| 312 | ASSERT_FALSE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix)); |
Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | TEST_F(HiddenApiTest, CheckMethodSignatureTrailingCharsNoMatch) { |
| 316 | ScopedObjectAccess soa(self_); |
| 317 | std::string prefix("Lmypackage/packagea/Class1;->method1()Vfoo"); |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 318 | ASSERT_FALSE(MemberSignature(class1_method1_).DoesPrefixMatch(prefix)); |
Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | TEST_F(HiddenApiTest, CheckConstructorTrailingCharsNoMatch) { |
| 322 | ScopedObjectAccess soa(self_); |
| 323 | std::string prefix("Lmypackage/packagea/Class1;-><init>()Vfoo"); |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 324 | ASSERT_FALSE(MemberSignature(class1_init_).DoesPrefixMatch(prefix)); |
Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | TEST_F(HiddenApiTest, CheckFieldTrailingCharsNoMatch) { |
| 328 | ScopedObjectAccess soa(self_); |
| 329 | std::string prefix("Lmypackage/packagea/Class1;->field1:Ifoo"); |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 330 | ASSERT_FALSE(MemberSignature(class1_field1_).DoesPrefixMatch(prefix)); |
Mathew Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 331 | } |
| 332 | |
David Brazdil | 1f9d3c3 | 2018-05-02 16:53:06 +0100 | [diff] [blame] | 333 | TEST_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 Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 345 | std::vector<Handle<mirror::Class>> interfaces; |
| 346 | interfaces.push_back(h_iface); |
David Brazdil | 1f9d3c3 | 2018-05-02 16:53:06 +0100 | [diff] [blame] | 347 | 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 Inwood | 7d74ef5 | 2018-03-16 14:18:33 +0000 | [diff] [blame] | 385 | } // namespace art |