Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | */ |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 16 | |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 17 | #include "jni_internal.h" |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 18 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 19 | #include "common_compiler_test.h" |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 20 | #include "java_vm_ext.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 21 | #include "mirror/art_method-inl.h" |
Fred Shih | 56890e2 | 2014-06-02 11:11:52 -0700 | [diff] [blame] | 22 | #include "mirror/string-inl.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 23 | #include "scoped_thread_state_change.h" |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 24 | #include "ScopedLocalRef.h" |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 25 | |
| 26 | namespace art { |
| 27 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 28 | // TODO: Convert to CommonRuntimeTest. Currently MakeExecutable is used. |
| 29 | class JniInternalTest : public CommonCompilerTest { |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 30 | protected: |
| 31 | virtual void SetUp() { |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 32 | CommonCompilerTest::SetUp(); |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 33 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 34 | vm_ = Runtime::Current()->GetJavaVM(); |
| 35 | |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 36 | // Turn on -verbose:jni for the JNI tests. |
Ian Rogers | 7971363 | 2013-08-21 19:06:15 -0700 | [diff] [blame] | 37 | // gLogVerbosity.jni = true; |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 38 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 39 | vm_->AttachCurrentThread(&env_, nullptr); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 40 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 41 | ScopedLocalRef<jclass> aioobe(env_, |
| 42 | env_->FindClass("java/lang/ArrayIndexOutOfBoundsException")); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 43 | CHECK(aioobe.get() != nullptr); |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 44 | aioobe_ = reinterpret_cast<jclass>(env_->NewGlobalRef(aioobe.get())); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 45 | |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 46 | ScopedLocalRef<jclass> ase(env_, env_->FindClass("java/lang/ArrayStoreException")); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 47 | CHECK(ase.get() != nullptr); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 48 | ase_ = reinterpret_cast<jclass>(env_->NewGlobalRef(ase.get())); |
| 49 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 50 | ScopedLocalRef<jclass> sioobe(env_, |
| 51 | env_->FindClass("java/lang/StringIndexOutOfBoundsException")); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 52 | CHECK(sioobe.get() != nullptr); |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 53 | sioobe_ = reinterpret_cast<jclass>(env_->NewGlobalRef(sioobe.get())); |
| 54 | } |
| 55 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 56 | void ExpectException(jclass exception_class) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 57 | ScopedObjectAccess soa(env_); |
| 58 | EXPECT_TRUE(env_->ExceptionCheck()) |
| 59 | << PrettyDescriptor(soa.Decode<mirror::Class*>(exception_class)); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 60 | jthrowable exception = env_->ExceptionOccurred(); |
| 61 | EXPECT_NE(nullptr, exception); |
| 62 | env_->ExceptionClear(); |
| 63 | EXPECT_TRUE(env_->IsInstanceOf(exception, exception_class)); |
| 64 | } |
| 65 | |
Brian Carlstrom | 4d57143 | 2012-05-16 00:21:41 -0700 | [diff] [blame] | 66 | void CleanUpJniEnv() { |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 67 | if (aioobe_ != nullptr) { |
Brian Carlstrom | 4d57143 | 2012-05-16 00:21:41 -0700 | [diff] [blame] | 68 | env_->DeleteGlobalRef(aioobe_); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 69 | aioobe_ = nullptr; |
Brian Carlstrom | 4d57143 | 2012-05-16 00:21:41 -0700 | [diff] [blame] | 70 | } |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 71 | if (ase_ != nullptr) { |
Brian Carlstrom | 4d57143 | 2012-05-16 00:21:41 -0700 | [diff] [blame] | 72 | env_->DeleteGlobalRef(ase_); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 73 | ase_ = nullptr; |
Brian Carlstrom | 4d57143 | 2012-05-16 00:21:41 -0700 | [diff] [blame] | 74 | } |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 75 | if (sioobe_ != nullptr) { |
Brian Carlstrom | 4d57143 | 2012-05-16 00:21:41 -0700 | [diff] [blame] | 76 | env_->DeleteGlobalRef(sioobe_); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 77 | sioobe_ = nullptr; |
Brian Carlstrom | 4d57143 | 2012-05-16 00:21:41 -0700 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 81 | virtual void TearDown() OVERRIDE { |
Brian Carlstrom | 4d57143 | 2012-05-16 00:21:41 -0700 | [diff] [blame] | 82 | CleanUpJniEnv(); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 83 | CommonCompilerTest::TearDown(); |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 84 | } |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 85 | |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 86 | jclass GetPrimitiveClass(char descriptor) { |
| 87 | ScopedObjectAccess soa(env_); |
| 88 | mirror::Class* c = class_linker_->FindPrimitiveClass(descriptor); |
| 89 | CHECK(c != nullptr); |
| 90 | return soa.AddLocalReference<jclass>(c); |
| 91 | } |
| 92 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 93 | void ExpectClassFound(const char* name) { |
| 94 | EXPECT_NE(env_->FindClass(name), nullptr) << name; |
| 95 | EXPECT_FALSE(env_->ExceptionCheck()) << name; |
| 96 | } |
| 97 | |
| 98 | void ExpectClassNotFound(const char* name, bool check_jni, const char* check_jni_msg, |
| 99 | CheckJniAbortCatcher* abort_catcher) { |
| 100 | EXPECT_EQ(env_->FindClass(name), nullptr) << name; |
| 101 | if (!check_jni || check_jni_msg == nullptr) { |
| 102 | EXPECT_TRUE(env_->ExceptionCheck()) << name; |
| 103 | env_->ExceptionClear(); |
| 104 | } else { |
| 105 | abort_catcher->Check(check_jni_msg); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | void FindClassTest(bool check_jni) { |
| 110 | bool old_check_jni = vm_->SetCheckJniEnabled(check_jni); |
| 111 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 112 | |
| 113 | // Null argument is always an abort. |
| 114 | env_->FindClass(nullptr); |
| 115 | check_jni_abort_catcher.Check(check_jni ? "non-nullable const char* was NULL" |
| 116 | : "name == null"); |
| 117 | |
| 118 | // Reference types... |
| 119 | ExpectClassFound("java/lang/String"); |
| 120 | // ...for arrays too, where you must include "L;". |
| 121 | ExpectClassFound("[Ljava/lang/String;"); |
| 122 | // Primitive arrays are okay too, if the primitive type is valid. |
| 123 | ExpectClassFound("[C"); |
| 124 | |
| 125 | // But primitive types aren't allowed... |
| 126 | ExpectClassNotFound("C", check_jni, nullptr, &check_jni_abort_catcher); |
| 127 | ExpectClassNotFound("V", check_jni, nullptr, &check_jni_abort_catcher); |
| 128 | ExpectClassNotFound("K", check_jni, nullptr, &check_jni_abort_catcher); |
| 129 | |
| 130 | if (check_jni) { |
| 131 | // Check JNI will reject invalid class names as aborts but without pending exceptions. |
| 132 | EXPECT_EQ(env_->FindClass("java.lang.String"), nullptr); |
| 133 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 134 | check_jni_abort_catcher.Check("illegal class name 'java.lang.String'"); |
| 135 | |
| 136 | EXPECT_EQ(env_->FindClass("[Ljava.lang.String;"), nullptr); |
| 137 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 138 | check_jni_abort_catcher.Check("illegal class name '[Ljava.lang.String;'"); |
| 139 | } else { |
| 140 | // Without check JNI we're tolerant and replace '.' with '/'. |
| 141 | ExpectClassFound("java.lang.String"); |
| 142 | ExpectClassFound("[Ljava.lang.String;"); |
| 143 | } |
| 144 | |
| 145 | ExpectClassNotFound("Ljava.lang.String;", check_jni, "illegal class name 'Ljava.lang.String;'", |
| 146 | &check_jni_abort_catcher); |
| 147 | ExpectClassNotFound("[java.lang.String", check_jni, "illegal class name '[java.lang.String'", |
| 148 | &check_jni_abort_catcher); |
| 149 | |
| 150 | // You can't include the "L;" in a JNI class descriptor. |
| 151 | ExpectClassNotFound("Ljava/lang/String;", check_jni, "illegal class name 'Ljava/lang/String;'", |
| 152 | &check_jni_abort_catcher); |
| 153 | |
| 154 | // But you must include it for an array of any reference type. |
| 155 | ExpectClassNotFound("[java/lang/String", check_jni, "illegal class name '[java/lang/String'", |
| 156 | &check_jni_abort_catcher); |
| 157 | |
| 158 | ExpectClassNotFound("[K", check_jni, "illegal class name '[K'", &check_jni_abort_catcher); |
| 159 | |
| 160 | // Void arrays aren't allowed. |
| 161 | ExpectClassNotFound("[V", check_jni, "illegal class name '[V'", &check_jni_abort_catcher); |
| 162 | |
| 163 | EXPECT_EQ(check_jni, vm_->SetCheckJniEnabled(old_check_jni)); |
| 164 | } |
| 165 | |
| 166 | void GetFieldIdBadArgumentTest(bool check_jni) { |
| 167 | bool old_check_jni = vm_->SetCheckJniEnabled(check_jni); |
| 168 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 169 | |
| 170 | jclass c = env_->FindClass("java/lang/String"); |
| 171 | ASSERT_NE(c, nullptr); |
| 172 | |
| 173 | jfieldID fid = env_->GetFieldID(nullptr, "count", "I"); |
| 174 | EXPECT_EQ(nullptr, fid); |
| 175 | check_jni_abort_catcher.Check(check_jni ? "GetFieldID received NULL jclass" |
| 176 | : "java_class == null"); |
| 177 | fid = env_->GetFieldID(c, nullptr, "I"); |
| 178 | EXPECT_EQ(nullptr, fid); |
| 179 | check_jni_abort_catcher.Check(check_jni ? "non-nullable const char* was NULL" |
| 180 | : "name == null"); |
| 181 | fid = env_->GetFieldID(c, "count", nullptr); |
| 182 | EXPECT_EQ(nullptr, fid); |
| 183 | check_jni_abort_catcher.Check(check_jni ? "non-nullable const char* was NULL" |
| 184 | : "sig == null"); |
| 185 | |
| 186 | EXPECT_EQ(check_jni, vm_->SetCheckJniEnabled(old_check_jni)); |
| 187 | } |
| 188 | |
| 189 | void GetStaticFieldIdBadArgumentTest(bool check_jni) { |
| 190 | bool old_check_jni = vm_->SetCheckJniEnabled(check_jni); |
| 191 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 192 | |
| 193 | jclass c = env_->FindClass("java/lang/String"); |
| 194 | ASSERT_NE(c, nullptr); |
| 195 | |
| 196 | jfieldID fid = env_->GetStaticFieldID(nullptr, "CASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;"); |
| 197 | EXPECT_EQ(nullptr, fid); |
| 198 | check_jni_abort_catcher.Check(check_jni ? "GetStaticFieldID received NULL jclass" |
| 199 | : "java_class == null"); |
| 200 | fid = env_->GetStaticFieldID(c, nullptr, "Ljava/util/Comparator;"); |
| 201 | EXPECT_EQ(nullptr, fid); |
| 202 | check_jni_abort_catcher.Check(check_jni ? "non-nullable const char* was NULL" |
| 203 | : "name == null"); |
| 204 | fid = env_->GetStaticFieldID(c, "CASE_INSENSITIVE_ORDER", nullptr); |
| 205 | EXPECT_EQ(nullptr, fid); |
| 206 | check_jni_abort_catcher.Check(check_jni ? "non-nullable const char* was NULL" |
| 207 | : "sig == null"); |
| 208 | |
| 209 | EXPECT_EQ(check_jni, vm_->SetCheckJniEnabled(old_check_jni)); |
| 210 | } |
| 211 | |
| 212 | void GetMethodIdBadArgumentTest(bool check_jni) { |
| 213 | bool old_check_jni = vm_->SetCheckJniEnabled(check_jni); |
| 214 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 215 | |
| 216 | jmethodID method = env_->GetMethodID(nullptr, "<init>", "(Ljava/lang/String;)V"); |
| 217 | EXPECT_EQ(nullptr, method); |
| 218 | check_jni_abort_catcher.Check(check_jni ? "GetMethodID received NULL jclass" |
| 219 | : "java_class == null"); |
| 220 | jclass jlnsme = env_->FindClass("java/lang/NoSuchMethodError"); |
| 221 | ASSERT_TRUE(jlnsme != nullptr); |
| 222 | method = env_->GetMethodID(jlnsme, nullptr, "(Ljava/lang/String;)V"); |
| 223 | EXPECT_EQ(nullptr, method); |
| 224 | check_jni_abort_catcher.Check(check_jni ? "non-nullable const char* was NULL" |
| 225 | : "name == null"); |
| 226 | method = env_->GetMethodID(jlnsme, "<init>", nullptr); |
| 227 | EXPECT_EQ(nullptr, method); |
| 228 | check_jni_abort_catcher.Check(check_jni ? "non-nullable const char* was NULL" |
| 229 | : "sig == null"); |
| 230 | |
| 231 | EXPECT_EQ(check_jni, vm_->SetCheckJniEnabled(old_check_jni)); |
| 232 | } |
| 233 | |
| 234 | void GetStaticMethodIdBadArgumentTest(bool check_jni) { |
| 235 | bool old_check_jni = vm_->SetCheckJniEnabled(check_jni); |
| 236 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 237 | |
| 238 | jmethodID method = env_->GetStaticMethodID(nullptr, "valueOf", "(I)Ljava/lang/String;"); |
| 239 | EXPECT_EQ(nullptr, method); |
| 240 | check_jni_abort_catcher.Check(check_jni ? "GetStaticMethodID received NULL jclass" |
| 241 | : "java_class == null"); |
| 242 | jclass jlstring = env_->FindClass("java/lang/String"); |
| 243 | method = env_->GetStaticMethodID(jlstring, nullptr, "(I)Ljava/lang/String;"); |
| 244 | EXPECT_EQ(nullptr, method); |
| 245 | check_jni_abort_catcher.Check(check_jni ? "non-nullable const char* was NULL" |
| 246 | : "name == null"); |
| 247 | method = env_->GetStaticMethodID(jlstring, "valueOf", nullptr); |
| 248 | EXPECT_EQ(nullptr, method); |
| 249 | check_jni_abort_catcher.Check(check_jni ? "non-nullable const char* was NULL" |
| 250 | : "sig == null"); |
| 251 | |
| 252 | EXPECT_EQ(check_jni, vm_->SetCheckJniEnabled(old_check_jni)); |
| 253 | } |
| 254 | |
| 255 | void GetFromReflectedField_ToReflectedFieldBadArgumentTest(bool check_jni) { |
| 256 | bool old_check_jni = vm_->SetCheckJniEnabled(check_jni); |
| 257 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 258 | |
| 259 | jclass c = env_->FindClass("java/lang/String"); |
| 260 | ASSERT_NE(c, nullptr); |
| 261 | jfieldID fid = env_->GetFieldID(c, "count", "I"); |
| 262 | ASSERT_NE(fid, nullptr); |
| 263 | |
| 264 | // Check class argument for null argument, not checked in non-check JNI. |
| 265 | jobject field = env_->ToReflectedField(nullptr, fid, JNI_FALSE); |
| 266 | if (check_jni) { |
| 267 | EXPECT_EQ(field, nullptr); |
| 268 | check_jni_abort_catcher.Check("ToReflectedField received NULL jclass"); |
| 269 | } else { |
| 270 | EXPECT_NE(field, nullptr); |
| 271 | } |
| 272 | |
| 273 | field = env_->ToReflectedField(c, nullptr, JNI_FALSE); |
| 274 | EXPECT_EQ(field, nullptr); |
| 275 | check_jni_abort_catcher.Check(check_jni ? "jfieldID was NULL" |
| 276 | : "fid == null"); |
| 277 | |
| 278 | fid = env_->FromReflectedField(nullptr); |
| 279 | ASSERT_EQ(fid, nullptr); |
| 280 | check_jni_abort_catcher.Check(check_jni ? "expected non-null java.lang.reflect.Field" |
| 281 | : "jlr_field == null"); |
| 282 | |
| 283 | EXPECT_EQ(check_jni, vm_->SetCheckJniEnabled(old_check_jni)); |
| 284 | } |
| 285 | |
| 286 | void GetFromReflectedMethod_ToReflectedMethodBadArgumentTest(bool check_jni) { |
| 287 | bool old_check_jni = vm_->SetCheckJniEnabled(check_jni); |
| 288 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 289 | |
| 290 | jclass c = env_->FindClass("java/lang/String"); |
| 291 | ASSERT_NE(c, nullptr); |
| 292 | jmethodID mid = env_->GetMethodID(c, "<init>", "()V"); |
| 293 | ASSERT_NE(mid, nullptr); |
| 294 | |
| 295 | // Check class argument for null argument, not checked in non-check JNI. |
| 296 | jobject method = env_->ToReflectedMethod(nullptr, mid, JNI_FALSE); |
| 297 | if (check_jni) { |
| 298 | EXPECT_EQ(method, nullptr); |
| 299 | check_jni_abort_catcher.Check("ToReflectedMethod received NULL jclass"); |
| 300 | } else { |
| 301 | EXPECT_NE(method, nullptr); |
| 302 | } |
| 303 | |
| 304 | method = env_->ToReflectedMethod(c, nullptr, JNI_FALSE); |
| 305 | EXPECT_EQ(method, nullptr); |
| 306 | check_jni_abort_catcher.Check(check_jni ? "jmethodID was NULL" |
| 307 | : "mid == null"); |
| 308 | mid = env_->FromReflectedMethod(method); |
| 309 | ASSERT_EQ(mid, nullptr); |
| 310 | check_jni_abort_catcher.Check(check_jni ? "expected non-null method" : "jlr_method == null"); |
| 311 | |
| 312 | EXPECT_EQ(check_jni, vm_->SetCheckJniEnabled(old_check_jni)); |
| 313 | } |
| 314 | |
| 315 | void RegisterAndUnregisterNativesBadArguments(bool check_jni, |
| 316 | CheckJniAbortCatcher* check_jni_abort_catcher) { |
| 317 | bool old_check_jni = vm_->SetCheckJniEnabled(check_jni); |
| 318 | // Passing a class of null is a failure. |
| 319 | { |
| 320 | JNINativeMethod methods[] = { }; |
| 321 | EXPECT_EQ(env_->RegisterNatives(nullptr, methods, 0), JNI_ERR); |
| 322 | check_jni_abort_catcher->Check(check_jni ? "RegisterNatives received NULL jclass" |
| 323 | : "java_class == null"); |
| 324 | } |
| 325 | |
| 326 | // Passing methods as null is a failure. |
| 327 | jclass jlobject = env_->FindClass("java/lang/Object"); |
| 328 | EXPECT_EQ(env_->RegisterNatives(jlobject, nullptr, 1), JNI_ERR); |
| 329 | check_jni_abort_catcher->Check("methods == null"); |
| 330 | |
| 331 | // Unregisters null is a failure. |
| 332 | EXPECT_EQ(env_->UnregisterNatives(nullptr), JNI_ERR); |
| 333 | check_jni_abort_catcher->Check(check_jni ? "UnregisterNatives received NULL jclass" |
| 334 | : "java_class == null"); |
| 335 | |
| 336 | EXPECT_EQ(check_jni, vm_->SetCheckJniEnabled(old_check_jni)); |
| 337 | } |
| 338 | |
| 339 | |
| 340 | void GetPrimitiveArrayElementsOfWrongType(bool check_jni) { |
| 341 | bool old_check_jni = vm_->SetCheckJniEnabled(check_jni); |
| 342 | CheckJniAbortCatcher jni_abort_catcher; |
| 343 | |
| 344 | jbooleanArray array = env_->NewBooleanArray(10); |
| 345 | jboolean is_copy; |
| 346 | EXPECT_EQ(env_->GetByteArrayElements(reinterpret_cast<jbyteArray>(array), &is_copy), nullptr); |
| 347 | jni_abort_catcher.Check( |
| 348 | check_jni ? "incompatible array type boolean[] expected byte[]" |
| 349 | : "attempt to get byte primitive array elements with an object of type boolean[]"); |
| 350 | EXPECT_EQ(env_->GetShortArrayElements(reinterpret_cast<jshortArray>(array), &is_copy), nullptr); |
| 351 | jni_abort_catcher.Check( |
| 352 | check_jni ? "incompatible array type boolean[] expected short[]" |
| 353 | : "attempt to get short primitive array elements with an object of type boolean[]"); |
| 354 | EXPECT_EQ(env_->GetCharArrayElements(reinterpret_cast<jcharArray>(array), &is_copy), nullptr); |
| 355 | jni_abort_catcher.Check( |
| 356 | check_jni ? "incompatible array type boolean[] expected char[]" |
| 357 | : "attempt to get char primitive array elements with an object of type boolean[]"); |
| 358 | EXPECT_EQ(env_->GetIntArrayElements(reinterpret_cast<jintArray>(array), &is_copy), nullptr); |
| 359 | jni_abort_catcher.Check( |
| 360 | check_jni ? "incompatible array type boolean[] expected int[]" |
| 361 | : "attempt to get int primitive array elements with an object of type boolean[]"); |
| 362 | EXPECT_EQ(env_->GetLongArrayElements(reinterpret_cast<jlongArray>(array), &is_copy), nullptr); |
| 363 | jni_abort_catcher.Check( |
| 364 | check_jni ? "incompatible array type boolean[] expected long[]" |
| 365 | : "attempt to get long primitive array elements with an object of type boolean[]"); |
| 366 | EXPECT_EQ(env_->GetFloatArrayElements(reinterpret_cast<jfloatArray>(array), &is_copy), nullptr); |
| 367 | jni_abort_catcher.Check( |
| 368 | check_jni ? "incompatible array type boolean[] expected float[]" |
| 369 | : "attempt to get float primitive array elements with an object of type boolean[]"); |
| 370 | EXPECT_EQ(env_->GetDoubleArrayElements(reinterpret_cast<jdoubleArray>(array), &is_copy), nullptr); |
| 371 | jni_abort_catcher.Check( |
| 372 | check_jni ? "incompatible array type boolean[] expected double[]" |
| 373 | : "attempt to get double primitive array elements with an object of type boolean[]"); |
| 374 | jbyteArray array2 = env_->NewByteArray(10); |
| 375 | EXPECT_EQ(env_->GetBooleanArrayElements(reinterpret_cast<jbooleanArray>(array2), &is_copy), |
| 376 | nullptr); |
| 377 | jni_abort_catcher.Check( |
| 378 | check_jni ? "incompatible array type byte[] expected boolean[]" |
| 379 | : "attempt to get boolean primitive array elements with an object of type byte[]"); |
| 380 | jobject object = env_->NewStringUTF("Test String"); |
| 381 | EXPECT_EQ(env_->GetBooleanArrayElements(reinterpret_cast<jbooleanArray>(object), &is_copy), |
| 382 | nullptr); |
| 383 | jni_abort_catcher.Check( |
| 384 | check_jni ? "jarray argument has non-array type: java.lang.String" |
| 385 | : "attempt to get boolean primitive array elements with an object of type java.lang.String"); |
| 386 | |
| 387 | EXPECT_EQ(check_jni, vm_->SetCheckJniEnabled(old_check_jni)); |
| 388 | } |
| 389 | |
| 390 | void ReleasePrimitiveArrayElementsOfWrongType(bool check_jni) { |
| 391 | bool old_check_jni = vm_->SetCheckJniEnabled(check_jni); |
| 392 | CheckJniAbortCatcher jni_abort_catcher; |
Ian Rogers | dd11d2a | 2014-11-19 10:06:46 -0800 | [diff] [blame] | 393 | { |
| 394 | jbooleanArray array = env_->NewBooleanArray(10); |
| 395 | ASSERT_TRUE(array != nullptr); |
| 396 | jboolean is_copy; |
| 397 | jboolean* elements = env_->GetBooleanArrayElements(array, &is_copy); |
| 398 | ASSERT_TRUE(elements != nullptr); |
| 399 | env_->ReleaseByteArrayElements(reinterpret_cast<jbyteArray>(array), |
| 400 | reinterpret_cast<jbyte*>(elements), 0); |
| 401 | jni_abort_catcher.Check( |
| 402 | check_jni ? "incompatible array type boolean[] expected byte[]" |
| 403 | : "attempt to release byte primitive array elements with an object of type boolean[]"); |
| 404 | env_->ReleaseShortArrayElements(reinterpret_cast<jshortArray>(array), |
| 405 | reinterpret_cast<jshort*>(elements), 0); |
| 406 | jni_abort_catcher.Check( |
| 407 | check_jni ? "incompatible array type boolean[] expected short[]" |
| 408 | : "attempt to release short primitive array elements with an object of type boolean[]"); |
| 409 | env_->ReleaseCharArrayElements(reinterpret_cast<jcharArray>(array), |
| 410 | reinterpret_cast<jchar*>(elements), 0); |
| 411 | jni_abort_catcher.Check( |
| 412 | check_jni ? "incompatible array type boolean[] expected char[]" |
| 413 | : "attempt to release char primitive array elements with an object of type boolean[]"); |
| 414 | env_->ReleaseIntArrayElements(reinterpret_cast<jintArray>(array), |
| 415 | reinterpret_cast<jint*>(elements), 0); |
| 416 | jni_abort_catcher.Check( |
| 417 | check_jni ? "incompatible array type boolean[] expected int[]" |
| 418 | : "attempt to release int primitive array elements with an object of type boolean[]"); |
| 419 | env_->ReleaseLongArrayElements(reinterpret_cast<jlongArray>(array), |
| 420 | reinterpret_cast<jlong*>(elements), 0); |
| 421 | jni_abort_catcher.Check( |
| 422 | check_jni ? "incompatible array type boolean[] expected long[]" |
| 423 | : "attempt to release long primitive array elements with an object of type boolean[]"); |
| 424 | env_->ReleaseFloatArrayElements(reinterpret_cast<jfloatArray>(array), |
| 425 | reinterpret_cast<jfloat*>(elements), 0); |
| 426 | jni_abort_catcher.Check( |
| 427 | check_jni ? "incompatible array type boolean[] expected float[]" |
| 428 | : "attempt to release float primitive array elements with an object of type boolean[]"); |
| 429 | env_->ReleaseDoubleArrayElements(reinterpret_cast<jdoubleArray>(array), |
| 430 | reinterpret_cast<jdouble*>(elements), 0); |
| 431 | jni_abort_catcher.Check( |
| 432 | check_jni ? "incompatible array type boolean[] expected double[]" |
| 433 | : "attempt to release double primitive array elements with an object of type boolean[]"); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 434 | |
Ian Rogers | dd11d2a | 2014-11-19 10:06:46 -0800 | [diff] [blame] | 435 | // Don't leak the elements array. |
| 436 | env_->ReleaseBooleanArrayElements(array, elements, 0); |
| 437 | } |
| 438 | { |
| 439 | jbyteArray array = env_->NewByteArray(10); |
| 440 | jboolean is_copy; |
| 441 | jbyte* elements = env_->GetByteArrayElements(array, &is_copy); |
| 442 | |
| 443 | env_->ReleaseBooleanArrayElements(reinterpret_cast<jbooleanArray>(array), |
| 444 | reinterpret_cast<jboolean*>(elements), 0); |
| 445 | jni_abort_catcher.Check( |
| 446 | check_jni ? "incompatible array type byte[] expected boolean[]" |
| 447 | : "attempt to release boolean primitive array elements with an object of type byte[]"); |
| 448 | jobject object = env_->NewStringUTF("Test String"); |
| 449 | env_->ReleaseBooleanArrayElements(reinterpret_cast<jbooleanArray>(object), |
| 450 | reinterpret_cast<jboolean*>(elements), 0); |
| 451 | jni_abort_catcher.Check( |
| 452 | check_jni ? "jarray argument has non-array type: java.lang.String" |
| 453 | : "attempt to release boolean primitive array elements with an object of type " |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 454 | "java.lang.String"); |
| 455 | |
Ian Rogers | dd11d2a | 2014-11-19 10:06:46 -0800 | [diff] [blame] | 456 | // Don't leak the elements array. |
| 457 | env_->ReleaseByteArrayElements(array, elements, 0); |
| 458 | } |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 459 | EXPECT_EQ(check_jni, vm_->SetCheckJniEnabled(old_check_jni)); |
| 460 | } |
| 461 | |
| 462 | void GetReleasePrimitiveArrayCriticalOfWrongType(bool check_jni) { |
| 463 | bool old_check_jni = vm_->SetCheckJniEnabled(check_jni); |
| 464 | CheckJniAbortCatcher jni_abort_catcher; |
| 465 | |
| 466 | jobject object = env_->NewStringUTF("Test String"); |
| 467 | jboolean is_copy; |
| 468 | void* elements = env_->GetPrimitiveArrayCritical(reinterpret_cast<jarray>(object), &is_copy); |
| 469 | jni_abort_catcher.Check(check_jni ? "jarray argument has non-array type: java.lang.String" |
| 470 | : "expected primitive array, given java.lang.String"); |
| 471 | env_->ReleasePrimitiveArrayCritical(reinterpret_cast<jarray>(object), elements, 0); |
| 472 | jni_abort_catcher.Check(check_jni ? "jarray argument has non-array type: java.lang.String" |
| 473 | : "expected primitive array, given java.lang.String"); |
| 474 | |
| 475 | EXPECT_EQ(check_jni, vm_->SetCheckJniEnabled(old_check_jni)); |
| 476 | } |
| 477 | |
| 478 | void GetPrimitiveArrayRegionElementsOfWrongType(bool check_jni) { |
| 479 | bool old_check_jni = vm_->SetCheckJniEnabled(check_jni); |
| 480 | CheckJniAbortCatcher jni_abort_catcher; |
| 481 | constexpr size_t kLength = 10; |
| 482 | jbooleanArray array = env_->NewBooleanArray(kLength); |
| 483 | ASSERT_TRUE(array != nullptr); |
| 484 | jboolean elements[kLength]; |
| 485 | env_->GetByteArrayRegion(reinterpret_cast<jbyteArray>(array), 0, kLength, |
| 486 | reinterpret_cast<jbyte*>(elements)); |
| 487 | jni_abort_catcher.Check( |
| 488 | check_jni ? "incompatible array type boolean[] expected byte[]" |
| 489 | : "attempt to get region of byte primitive array elements with an object of type boolean[]"); |
| 490 | env_->GetShortArrayRegion(reinterpret_cast<jshortArray>(array), 0, kLength, |
| 491 | reinterpret_cast<jshort*>(elements)); |
| 492 | jni_abort_catcher.Check( |
| 493 | check_jni ? "incompatible array type boolean[] expected short[]" |
| 494 | : "attempt to get region of short primitive array elements with an object of type boolean[]"); |
| 495 | env_->GetCharArrayRegion(reinterpret_cast<jcharArray>(array), 0, kLength, |
| 496 | reinterpret_cast<jchar*>(elements)); |
| 497 | jni_abort_catcher.Check( |
| 498 | check_jni ? "incompatible array type boolean[] expected char[]" |
| 499 | : "attempt to get region of char primitive array elements with an object of type boolean[]"); |
| 500 | env_->GetIntArrayRegion(reinterpret_cast<jintArray>(array), 0, kLength, |
| 501 | reinterpret_cast<jint*>(elements)); |
| 502 | jni_abort_catcher.Check( |
| 503 | check_jni ? "incompatible array type boolean[] expected int[]" |
| 504 | : "attempt to get region of int primitive array elements with an object of type boolean[]"); |
| 505 | env_->GetLongArrayRegion(reinterpret_cast<jlongArray>(array), 0, kLength, |
| 506 | reinterpret_cast<jlong*>(elements)); |
| 507 | jni_abort_catcher.Check( |
| 508 | check_jni ? "incompatible array type boolean[] expected long[]" |
| 509 | : "attempt to get region of long primitive array elements with an object of type boolean[]"); |
| 510 | env_->GetFloatArrayRegion(reinterpret_cast<jfloatArray>(array), 0, kLength, |
| 511 | reinterpret_cast<jfloat*>(elements)); |
| 512 | jni_abort_catcher.Check( |
| 513 | check_jni ? "incompatible array type boolean[] expected float[]" |
| 514 | : "attempt to get region of float primitive array elements with an object of type boolean[]"); |
| 515 | env_->GetDoubleArrayRegion(reinterpret_cast<jdoubleArray>(array), 0, kLength, |
| 516 | reinterpret_cast<jdouble*>(elements)); |
| 517 | jni_abort_catcher.Check( |
| 518 | check_jni ? "incompatible array type boolean[] expected double[]" |
| 519 | : "attempt to get region of double primitive array elements with an object of type boolean[]"); |
| 520 | jbyteArray array2 = env_->NewByteArray(10); |
| 521 | env_->GetBooleanArrayRegion(reinterpret_cast<jbooleanArray>(array2), 0, kLength, |
| 522 | reinterpret_cast<jboolean*>(elements)); |
| 523 | jni_abort_catcher.Check( |
| 524 | check_jni ? "incompatible array type byte[] expected boolean[]" |
| 525 | : "attempt to get region of boolean primitive array elements with an object of type byte[]"); |
| 526 | jobject object = env_->NewStringUTF("Test String"); |
| 527 | env_->GetBooleanArrayRegion(reinterpret_cast<jbooleanArray>(object), 0, kLength, |
| 528 | reinterpret_cast<jboolean*>(elements)); |
| 529 | jni_abort_catcher.Check(check_jni ? "jarray argument has non-array type: java.lang.String" |
| 530 | : "attempt to get region of boolean primitive array elements with an object of type " |
| 531 | "java.lang.String"); |
| 532 | |
| 533 | EXPECT_EQ(check_jni, vm_->SetCheckJniEnabled(old_check_jni)); |
| 534 | } |
| 535 | |
| 536 | void SetPrimitiveArrayRegionElementsOfWrongType(bool check_jni) { |
| 537 | bool old_check_jni = vm_->SetCheckJniEnabled(check_jni); |
| 538 | CheckJniAbortCatcher jni_abort_catcher; |
| 539 | constexpr size_t kLength = 10; |
| 540 | jbooleanArray array = env_->NewBooleanArray(kLength); |
| 541 | ASSERT_TRUE(array != nullptr); |
| 542 | jboolean elements[kLength]; |
| 543 | env_->SetByteArrayRegion(reinterpret_cast<jbyteArray>(array), 0, kLength, |
| 544 | reinterpret_cast<jbyte*>(elements)); |
| 545 | jni_abort_catcher.Check( |
| 546 | check_jni ? "incompatible array type boolean[] expected byte[]" |
| 547 | : "attempt to set region of byte primitive array elements with an object of type boolean[]"); |
| 548 | env_->SetShortArrayRegion(reinterpret_cast<jshortArray>(array), 0, kLength, |
| 549 | reinterpret_cast<jshort*>(elements)); |
| 550 | jni_abort_catcher.Check( |
| 551 | check_jni ? "incompatible array type boolean[] expected short[]" |
| 552 | : "attempt to set region of short primitive array elements with an object of type boolean[]"); |
| 553 | env_->SetCharArrayRegion(reinterpret_cast<jcharArray>(array), 0, kLength, |
| 554 | reinterpret_cast<jchar*>(elements)); |
| 555 | jni_abort_catcher.Check( |
| 556 | check_jni ? "incompatible array type boolean[] expected char[]" |
| 557 | : "attempt to set region of char primitive array elements with an object of type boolean[]"); |
| 558 | env_->SetIntArrayRegion(reinterpret_cast<jintArray>(array), 0, kLength, |
| 559 | reinterpret_cast<jint*>(elements)); |
| 560 | jni_abort_catcher.Check( |
| 561 | check_jni ? "incompatible array type boolean[] expected int[]" |
| 562 | : "attempt to set region of int primitive array elements with an object of type boolean[]"); |
| 563 | env_->SetLongArrayRegion(reinterpret_cast<jlongArray>(array), 0, kLength, |
| 564 | reinterpret_cast<jlong*>(elements)); |
| 565 | jni_abort_catcher.Check( |
| 566 | check_jni ? "incompatible array type boolean[] expected long[]" |
| 567 | : "attempt to set region of long primitive array elements with an object of type boolean[]"); |
| 568 | env_->SetFloatArrayRegion(reinterpret_cast<jfloatArray>(array), 0, kLength, |
| 569 | reinterpret_cast<jfloat*>(elements)); |
| 570 | jni_abort_catcher.Check( |
| 571 | check_jni ? "incompatible array type boolean[] expected float[]" |
| 572 | : "attempt to set region of float primitive array elements with an object of type boolean[]"); |
| 573 | env_->SetDoubleArrayRegion(reinterpret_cast<jdoubleArray>(array), 0, kLength, |
| 574 | reinterpret_cast<jdouble*>(elements)); |
| 575 | jni_abort_catcher.Check( |
| 576 | check_jni ? "incompatible array type boolean[] expected double[]" |
| 577 | : "attempt to set region of double primitive array elements with an object of type boolean[]"); |
| 578 | jbyteArray array2 = env_->NewByteArray(10); |
| 579 | env_->SetBooleanArrayRegion(reinterpret_cast<jbooleanArray>(array2), 0, kLength, |
| 580 | reinterpret_cast<jboolean*>(elements)); |
| 581 | jni_abort_catcher.Check( |
| 582 | check_jni ? "incompatible array type byte[] expected boolean[]" |
| 583 | : "attempt to set region of boolean primitive array elements with an object of type byte[]"); |
| 584 | jobject object = env_->NewStringUTF("Test String"); |
| 585 | env_->SetBooleanArrayRegion(reinterpret_cast<jbooleanArray>(object), 0, kLength, |
| 586 | reinterpret_cast<jboolean*>(elements)); |
| 587 | jni_abort_catcher.Check(check_jni ? "jarray argument has non-array type: java.lang.String" |
| 588 | : "attempt to set region of boolean primitive array elements with an object of type " |
| 589 | "java.lang.String"); |
| 590 | EXPECT_EQ(check_jni, vm_->SetCheckJniEnabled(old_check_jni)); |
| 591 | } |
| 592 | |
| 593 | void NewObjectArrayBadArguments(bool check_jni) { |
| 594 | bool old_check_jni = vm_->SetCheckJniEnabled(check_jni); |
| 595 | CheckJniAbortCatcher jni_abort_catcher; |
| 596 | |
| 597 | jclass element_class = env_->FindClass("java/lang/String"); |
| 598 | ASSERT_NE(element_class, nullptr); |
| 599 | |
| 600 | env_->NewObjectArray(-1, element_class, nullptr); |
| 601 | jni_abort_catcher.Check(check_jni ? "negative jsize: -1" : "negative array length: -1"); |
| 602 | |
| 603 | env_->NewObjectArray(std::numeric_limits<jint>::min(), element_class, nullptr); |
| 604 | jni_abort_catcher.Check(check_jni ? "negative jsize: -2147483648" |
| 605 | : "negative array length: -2147483648"); |
| 606 | |
| 607 | EXPECT_EQ(check_jni, vm_->SetCheckJniEnabled(old_check_jni)); |
| 608 | } |
| 609 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 610 | JavaVMExt* vm_; |
Brian Carlstrom | 4d57143 | 2012-05-16 00:21:41 -0700 | [diff] [blame] | 611 | JNIEnv* env_; |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 612 | jclass aioobe_; |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 613 | jclass ase_; |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 614 | jclass sioobe_; |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 615 | }; |
| 616 | |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 617 | TEST_F(JniInternalTest, AllocObject) { |
| 618 | jclass c = env_->FindClass("java/lang/String"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 619 | ASSERT_NE(c, nullptr); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 620 | jobject o = env_->AllocObject(c); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 621 | ASSERT_NE(o, nullptr); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 622 | |
| 623 | // We have an instance of the class we asked for... |
| 624 | ASSERT_TRUE(env_->IsInstanceOf(o, c)); |
| 625 | // ...whose fields haven't been initialized because |
| 626 | // we didn't call a constructor. |
| 627 | ASSERT_EQ(0, env_->GetIntField(o, env_->GetFieldID(c, "count", "I"))); |
| 628 | ASSERT_EQ(0, env_->GetIntField(o, env_->GetFieldID(c, "offset", "I"))); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 629 | ASSERT_TRUE(env_->GetObjectField(o, env_->GetFieldID(c, "value", "[C")) == nullptr); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 630 | } |
| 631 | |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 632 | TEST_F(JniInternalTest, GetVersion) { |
| 633 | ASSERT_EQ(JNI_VERSION_1_6, env_->GetVersion()); |
| 634 | } |
| 635 | |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 636 | TEST_F(JniInternalTest, FindClass) { |
Andreas Gampe | 369810a | 2015-01-14 19:53:31 -0800 | [diff] [blame] | 637 | // This tests leads to warnings in the log. |
| 638 | ScopedLogSeverity sls(LogSeverity::ERROR); |
| 639 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 640 | FindClassTest(false); |
| 641 | FindClassTest(true); |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 642 | } |
| 643 | |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 644 | TEST_F(JniInternalTest, GetFieldID) { |
| 645 | jclass jlnsfe = env_->FindClass("java/lang/NoSuchFieldError"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 646 | ASSERT_NE(jlnsfe, nullptr); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 647 | jclass c = env_->FindClass("java/lang/String"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 648 | ASSERT_NE(c, nullptr); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 649 | |
| 650 | // Wrong type. |
| 651 | jfieldID fid = env_->GetFieldID(c, "count", "J"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 652 | EXPECT_EQ(nullptr, fid); |
| 653 | ExpectException(jlnsfe); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 654 | |
Ian Rogers | b17d08b | 2011-09-02 16:16:49 -0700 | [diff] [blame] | 655 | // Wrong type where type doesn't exist. |
| 656 | fid = env_->GetFieldID(c, "count", "Lrod/jane/freddy;"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 657 | EXPECT_EQ(nullptr, fid); |
| 658 | ExpectException(jlnsfe); |
Ian Rogers | b17d08b | 2011-09-02 16:16:49 -0700 | [diff] [blame] | 659 | |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 660 | // Wrong name. |
| 661 | fid = env_->GetFieldID(c, "Count", "I"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 662 | EXPECT_EQ(nullptr, fid); |
| 663 | ExpectException(jlnsfe); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 664 | |
| 665 | // Good declared field lookup. |
| 666 | fid = env_->GetFieldID(c, "count", "I"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 667 | EXPECT_NE(nullptr, fid); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 668 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 669 | |
| 670 | // Good superclass field lookup. |
| 671 | c = env_->FindClass("java/lang/StringBuilder"); |
| 672 | fid = env_->GetFieldID(c, "count", "I"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 673 | EXPECT_NE(nullptr, fid); |
| 674 | EXPECT_NE(fid, nullptr); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 675 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 676 | |
| 677 | // Not instance. |
| 678 | fid = env_->GetFieldID(c, "CASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 679 | EXPECT_EQ(nullptr, fid); |
| 680 | ExpectException(jlnsfe); |
| 681 | |
| 682 | // Bad arguments. |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 683 | GetFieldIdBadArgumentTest(false); |
| 684 | GetFieldIdBadArgumentTest(true); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | TEST_F(JniInternalTest, GetStaticFieldID) { |
| 688 | jclass jlnsfe = env_->FindClass("java/lang/NoSuchFieldError"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 689 | ASSERT_NE(jlnsfe, nullptr); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 690 | jclass c = env_->FindClass("java/lang/String"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 691 | ASSERT_NE(c, nullptr); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 692 | |
| 693 | // Wrong type. |
| 694 | jfieldID fid = env_->GetStaticFieldID(c, "CASE_INSENSITIVE_ORDER", "J"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 695 | EXPECT_EQ(nullptr, fid); |
| 696 | ExpectException(jlnsfe); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 697 | |
Ian Rogers | b17d08b | 2011-09-02 16:16:49 -0700 | [diff] [blame] | 698 | // Wrong type where type doesn't exist. |
| 699 | fid = env_->GetStaticFieldID(c, "CASE_INSENSITIVE_ORDER", "Lrod/jane/freddy;"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 700 | EXPECT_EQ(nullptr, fid); |
| 701 | ExpectException(jlnsfe); |
Ian Rogers | b17d08b | 2011-09-02 16:16:49 -0700 | [diff] [blame] | 702 | |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 703 | // Wrong name. |
| 704 | fid = env_->GetStaticFieldID(c, "cASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 705 | EXPECT_EQ(nullptr, fid); |
| 706 | ExpectException(jlnsfe); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 707 | |
| 708 | // Good declared field lookup. |
| 709 | fid = env_->GetStaticFieldID(c, "CASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 710 | EXPECT_NE(nullptr, fid); |
| 711 | EXPECT_NE(fid, nullptr); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 712 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 713 | |
| 714 | // Not static. |
| 715 | fid = env_->GetStaticFieldID(c, "count", "I"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 716 | EXPECT_EQ(nullptr, fid); |
| 717 | ExpectException(jlnsfe); |
| 718 | |
| 719 | // Bad arguments. |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 720 | GetStaticFieldIdBadArgumentTest(false); |
| 721 | GetStaticFieldIdBadArgumentTest(true); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 722 | } |
| 723 | |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 724 | TEST_F(JniInternalTest, GetMethodID) { |
| 725 | jclass jlobject = env_->FindClass("java/lang/Object"); |
| 726 | jclass jlstring = env_->FindClass("java/lang/String"); |
| 727 | jclass jlnsme = env_->FindClass("java/lang/NoSuchMethodError"); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 728 | jclass jncrbc = env_->FindClass("java/nio/channels/ReadableByteChannel"); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 729 | |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 730 | // Sanity check that no exceptions are pending. |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 731 | ASSERT_FALSE(env_->ExceptionCheck()); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 732 | |
| 733 | // Check that java.lang.Object.foo() doesn't exist and NoSuchMethodError is |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 734 | // a pending exception. |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 735 | jmethodID method = env_->GetMethodID(jlobject, "foo", "()V"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 736 | EXPECT_EQ(nullptr, method); |
| 737 | ExpectException(jlnsme); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 738 | |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 739 | // Check that java.lang.Object.equals() does exist. |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 740 | method = env_->GetMethodID(jlobject, "equals", "(Ljava/lang/Object;)Z"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 741 | EXPECT_NE(nullptr, method); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 742 | EXPECT_FALSE(env_->ExceptionCheck()); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 743 | |
| 744 | // Check that GetMethodID for java.lang.String.valueOf(int) fails as the |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 745 | // method is static. |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 746 | method = env_->GetMethodID(jlstring, "valueOf", "(I)Ljava/lang/String;"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 747 | EXPECT_EQ(nullptr, method); |
| 748 | ExpectException(jlnsme); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 749 | |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 750 | // Check that GetMethodID for java.lang.NoSuchMethodError.<init>(String) finds the constructor. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 751 | method = env_->GetMethodID(jlnsme, "<init>", "(Ljava/lang/String;)V"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 752 | EXPECT_NE(nullptr, method); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 753 | EXPECT_FALSE(env_->ExceptionCheck()); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 754 | |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 755 | // Check that GetMethodID can find a interface method inherited from another interface. |
| 756 | method = env_->GetMethodID(jncrbc, "close", "()V"); |
| 757 | EXPECT_NE(nullptr, method); |
| 758 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 759 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 760 | // Bad arguments. |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 761 | GetMethodIdBadArgumentTest(false); |
| 762 | GetMethodIdBadArgumentTest(true); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 763 | } |
| 764 | |
Mathieu Chartier | a1e78fa | 2014-08-20 12:09:38 -0700 | [diff] [blame] | 765 | TEST_F(JniInternalTest, CallVoidMethodNullReceiver) { |
| 766 | jclass jlobject = env_->FindClass("java/lang/Object"); |
| 767 | jmethodID method; |
| 768 | |
| 769 | // Check that GetMethodID for java.lang.NoSuchMethodError.<init>(String) finds the constructor. |
| 770 | method = env_->GetMethodID(jlobject, "<init>", "()V"); |
| 771 | EXPECT_NE(nullptr, method); |
| 772 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 773 | |
| 774 | // Null object to CallVoidMethod. |
| 775 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 776 | env_->CallVoidMethod(nullptr, method); |
| 777 | check_jni_abort_catcher.Check("null"); |
| 778 | } |
| 779 | |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 780 | TEST_F(JniInternalTest, GetStaticMethodID) { |
| 781 | jclass jlobject = env_->FindClass("java/lang/Object"); |
| 782 | jclass jlnsme = env_->FindClass("java/lang/NoSuchMethodError"); |
| 783 | |
| 784 | // Sanity check that no exceptions are pending |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 785 | ASSERT_FALSE(env_->ExceptionCheck()); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 786 | |
| 787 | // Check that java.lang.Object.foo() doesn't exist and NoSuchMethodError is |
| 788 | // a pending exception |
| 789 | jmethodID method = env_->GetStaticMethodID(jlobject, "foo", "()V"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 790 | EXPECT_EQ(nullptr, method); |
| 791 | ExpectException(jlnsme); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 792 | |
| 793 | // Check that GetStaticMethodID for java.lang.Object.equals(Object) fails as |
| 794 | // the method is not static |
| 795 | method = env_->GetStaticMethodID(jlobject, "equals", "(Ljava/lang/Object;)Z"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 796 | EXPECT_EQ(nullptr, method); |
| 797 | ExpectException(jlnsme); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 798 | |
| 799 | // Check that java.lang.String.valueOf(int) does exist |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 800 | jclass jlstring = env_->FindClass("java/lang/String"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 801 | method = env_->GetStaticMethodID(jlstring, "valueOf", "(I)Ljava/lang/String;"); |
| 802 | EXPECT_NE(nullptr, method); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 803 | EXPECT_FALSE(env_->ExceptionCheck()); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 804 | |
| 805 | // Bad arguments. |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 806 | GetStaticMethodIdBadArgumentTest(false); |
| 807 | GetStaticMethodIdBadArgumentTest(true); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 808 | } |
| 809 | |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 810 | TEST_F(JniInternalTest, FromReflectedField_ToReflectedField) { |
| 811 | jclass jlrField = env_->FindClass("java/lang/reflect/Field"); |
| 812 | jclass c = env_->FindClass("java/lang/String"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 813 | ASSERT_NE(c, nullptr); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 814 | jfieldID fid = env_->GetFieldID(c, "count", "I"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 815 | ASSERT_NE(fid, nullptr); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 816 | // Turn the fid into a java.lang.reflect.Field... |
| 817 | jobject field = env_->ToReflectedField(c, fid, JNI_FALSE); |
Mathieu Chartier | 7895946 | 2014-11-17 10:24:04 -0800 | [diff] [blame] | 818 | for (size_t i = 0; i <= kLocalsMax; ++i) { |
Mathieu Chartier | 41da596 | 2014-11-15 13:07:39 -0800 | [diff] [blame] | 819 | // Regression test for b/18396311, ToReflectedField leaking local refs causing a local |
| 820 | // reference table overflows with 512 references to ArtField |
| 821 | env_->DeleteLocalRef(env_->ToReflectedField(c, fid, JNI_FALSE)); |
| 822 | } |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 823 | ASSERT_NE(c, nullptr); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 824 | ASSERT_TRUE(env_->IsInstanceOf(field, jlrField)); |
| 825 | // ...and back again. |
| 826 | jfieldID fid2 = env_->FromReflectedField(field); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 827 | ASSERT_NE(fid2, nullptr); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 828 | // Make sure we can actually use it. |
| 829 | jstring s = env_->NewStringUTF("poop"); |
| 830 | ASSERT_EQ(4, env_->GetIntField(s, fid2)); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 831 | |
| 832 | // Bad arguments. |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 833 | GetFromReflectedField_ToReflectedFieldBadArgumentTest(false); |
| 834 | GetFromReflectedField_ToReflectedFieldBadArgumentTest(true); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | TEST_F(JniInternalTest, FromReflectedMethod_ToReflectedMethod) { |
| 838 | jclass jlrMethod = env_->FindClass("java/lang/reflect/Method"); |
Sebastien Hertz | d333376 | 2014-06-26 14:45:07 +0200 | [diff] [blame] | 839 | ASSERT_NE(jlrMethod, nullptr); |
| 840 | jclass jlrConstructor = env_->FindClass("java/lang/reflect/Constructor"); |
| 841 | ASSERT_NE(jlrConstructor, nullptr); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 842 | jclass c = env_->FindClass("java/lang/String"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 843 | ASSERT_NE(c, nullptr); |
Sebastien Hertz | d333376 | 2014-06-26 14:45:07 +0200 | [diff] [blame] | 844 | |
| 845 | jmethodID mid = env_->GetMethodID(c, "<init>", "()V"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 846 | ASSERT_NE(mid, nullptr); |
Sebastien Hertz | d333376 | 2014-06-26 14:45:07 +0200 | [diff] [blame] | 847 | // Turn the mid into a java.lang.reflect.Constructor... |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 848 | jobject method = env_->ToReflectedMethod(c, mid, JNI_FALSE); |
Mathieu Chartier | 7895946 | 2014-11-17 10:24:04 -0800 | [diff] [blame] | 849 | for (size_t i = 0; i <= kLocalsMax; ++i) { |
Mathieu Chartier | 41da596 | 2014-11-15 13:07:39 -0800 | [diff] [blame] | 850 | // Regression test for b/18396311, ToReflectedMethod leaking local refs causing a local |
| 851 | // reference table overflows with 512 references to ArtMethod |
| 852 | env_->DeleteLocalRef(env_->ToReflectedMethod(c, mid, JNI_FALSE)); |
| 853 | } |
Sebastien Hertz | d333376 | 2014-06-26 14:45:07 +0200 | [diff] [blame] | 854 | ASSERT_NE(method, nullptr); |
| 855 | ASSERT_TRUE(env_->IsInstanceOf(method, jlrConstructor)); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 856 | // ...and back again. |
| 857 | jmethodID mid2 = env_->FromReflectedMethod(method); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 858 | ASSERT_NE(mid2, nullptr); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 859 | // Make sure we can actually use it. |
Sebastien Hertz | d333376 | 2014-06-26 14:45:07 +0200 | [diff] [blame] | 860 | jstring s = reinterpret_cast<jstring>(env_->AllocObject(c)); |
| 861 | ASSERT_NE(s, nullptr); |
| 862 | env_->CallVoidMethod(s, mid2); |
| 863 | ASSERT_EQ(JNI_FALSE, env_->ExceptionCheck()); |
| 864 | |
| 865 | mid = env_->GetMethodID(c, "length", "()I"); |
| 866 | ASSERT_NE(mid, nullptr); |
| 867 | // Turn the mid into a java.lang.reflect.Method... |
| 868 | method = env_->ToReflectedMethod(c, mid, JNI_FALSE); |
| 869 | ASSERT_NE(method, nullptr); |
| 870 | ASSERT_TRUE(env_->IsInstanceOf(method, jlrMethod)); |
| 871 | // ...and back again. |
| 872 | mid2 = env_->FromReflectedMethod(method); |
| 873 | ASSERT_NE(mid2, nullptr); |
| 874 | // Make sure we can actually use it. |
| 875 | s = env_->NewStringUTF("poop"); |
| 876 | ASSERT_NE(s, nullptr); |
Ian Rogers | 5d27faf | 2014-05-02 17:17:18 -0700 | [diff] [blame] | 877 | ASSERT_EQ(4, env_->CallIntMethod(s, mid2)); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 878 | |
| 879 | // Bad arguments. |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 880 | GetFromReflectedMethod_ToReflectedMethodBadArgumentTest(false); |
| 881 | GetFromReflectedMethod_ToReflectedMethodBadArgumentTest(true); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 882 | } |
| 883 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 884 | static void BogusMethod() { |
| 885 | // You can't pass nullptr function pointers to RegisterNatives. |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 886 | } |
| 887 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 888 | TEST_F(JniInternalTest, RegisterAndUnregisterNatives) { |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 889 | jclass jlobject = env_->FindClass("java/lang/Object"); |
| 890 | jclass jlnsme = env_->FindClass("java/lang/NoSuchMethodError"); |
Sebastien Hertz | fa65e84 | 2014-07-03 09:39:53 +0200 | [diff] [blame] | 891 | void* native_function = reinterpret_cast<void*>(BogusMethod); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 892 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 893 | // Sanity check that no exceptions are pending. |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 894 | ASSERT_FALSE(env_->ExceptionCheck()); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 895 | |
Andreas Gampe | 369810a | 2015-01-14 19:53:31 -0800 | [diff] [blame] | 896 | // The following can print errors to the log we'd like to ignore. |
Sebastien Hertz | fa65e84 | 2014-07-03 09:39:53 +0200 | [diff] [blame] | 897 | { |
Andreas Gampe | 369810a | 2015-01-14 19:53:31 -0800 | [diff] [blame] | 898 | ScopedLogSeverity sls(LogSeverity::FATAL); |
| 899 | // Check that registering method without name causes a NoSuchMethodError. |
| 900 | { |
| 901 | JNINativeMethod methods[] = { { nullptr, "()V", native_function } }; |
| 902 | EXPECT_EQ(env_->RegisterNatives(jlobject, methods, 1), JNI_ERR); |
| 903 | } |
| 904 | ExpectException(jlnsme); |
Sebastien Hertz | fa65e84 | 2014-07-03 09:39:53 +0200 | [diff] [blame] | 905 | |
Andreas Gampe | 369810a | 2015-01-14 19:53:31 -0800 | [diff] [blame] | 906 | // Check that registering method without signature causes a NoSuchMethodError. |
| 907 | { |
| 908 | JNINativeMethod methods[] = { { "notify", nullptr, native_function } }; |
| 909 | EXPECT_EQ(env_->RegisterNatives(jlobject, methods, 1), JNI_ERR); |
| 910 | } |
| 911 | ExpectException(jlnsme); |
Sebastien Hertz | fa65e84 | 2014-07-03 09:39:53 +0200 | [diff] [blame] | 912 | |
Andreas Gampe | 369810a | 2015-01-14 19:53:31 -0800 | [diff] [blame] | 913 | // Check that registering method without function causes a NoSuchMethodError. |
| 914 | { |
| 915 | JNINativeMethod methods[] = { { "notify", "()V", nullptr } }; |
| 916 | EXPECT_EQ(env_->RegisterNatives(jlobject, methods, 1), JNI_ERR); |
| 917 | } |
| 918 | ExpectException(jlnsme); |
Sebastien Hertz | fa65e84 | 2014-07-03 09:39:53 +0200 | [diff] [blame] | 919 | |
Andreas Gampe | 369810a | 2015-01-14 19:53:31 -0800 | [diff] [blame] | 920 | // Check that registering to a non-existent java.lang.Object.foo() causes a NoSuchMethodError. |
| 921 | { |
| 922 | JNINativeMethod methods[] = { { "foo", "()V", native_function } }; |
| 923 | EXPECT_EQ(env_->RegisterNatives(jlobject, methods, 1), JNI_ERR); |
| 924 | } |
| 925 | ExpectException(jlnsme); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 926 | |
Andreas Gampe | 369810a | 2015-01-14 19:53:31 -0800 | [diff] [blame] | 927 | // Check that registering non-native methods causes a NoSuchMethodError. |
| 928 | { |
| 929 | JNINativeMethod methods[] = { { "equals", "(Ljava/lang/Object;)Z", native_function } }; |
| 930 | EXPECT_EQ(env_->RegisterNatives(jlobject, methods, 1), JNI_ERR); |
| 931 | } |
| 932 | ExpectException(jlnsme); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 933 | } |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 934 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 935 | // Check that registering native methods is successful. |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 936 | { |
Sebastien Hertz | fa65e84 | 2014-07-03 09:39:53 +0200 | [diff] [blame] | 937 | JNINativeMethod methods[] = { { "notify", "()V", native_function } }; |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 938 | EXPECT_EQ(env_->RegisterNatives(jlobject, methods, 1), JNI_OK); |
| 939 | } |
| 940 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 941 | EXPECT_EQ(env_->UnregisterNatives(jlobject), JNI_OK); |
| 942 | |
| 943 | // Check that registering no methods isn't a failure. |
| 944 | { |
| 945 | JNINativeMethod methods[] = { }; |
| 946 | EXPECT_EQ(env_->RegisterNatives(jlobject, methods, 0), JNI_OK); |
| 947 | } |
| 948 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 949 | EXPECT_EQ(env_->UnregisterNatives(jlobject), JNI_OK); |
| 950 | |
| 951 | // Check that registering a -ve number of methods is a failure. |
| 952 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 953 | for (int i = -10; i < 0; ++i) { |
| 954 | JNINativeMethod methods[] = { }; |
| 955 | EXPECT_EQ(env_->RegisterNatives(jlobject, methods, i), JNI_ERR); |
| 956 | check_jni_abort_catcher.Check("negative method count: "); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 957 | } |
| 958 | EXPECT_FALSE(env_->ExceptionCheck()); |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 959 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 960 | // Unregistering a class with no natives is a warning. |
| 961 | EXPECT_EQ(env_->UnregisterNatives(jlnsme), JNI_OK); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 962 | |
| 963 | RegisterAndUnregisterNativesBadArguments(false, &check_jni_abort_catcher); |
| 964 | RegisterAndUnregisterNativesBadArguments(true, &check_jni_abort_catcher); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 965 | } |
| 966 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 967 | #define EXPECT_PRIMITIVE_ARRAY(new_fn, \ |
| 968 | get_region_fn, \ |
| 969 | set_region_fn, \ |
| 970 | get_elements_fn, \ |
| 971 | release_elements_fn, \ |
| 972 | scalar_type, \ |
| 973 | expected_class_descriptor) \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 974 | jsize size = 4; \ |
| 975 | \ |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 976 | { \ |
| 977 | CheckJniAbortCatcher jni_abort_catcher; \ |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 978 | down_cast<JNIEnvExt*>(env_)->SetCheckJniEnabled(false); \ |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 979 | /* Allocate an negative sized array and check it has the right failure type. */ \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 980 | EXPECT_EQ(env_->new_fn(-1), nullptr); \ |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 981 | jni_abort_catcher.Check("negative array length: -1"); \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 982 | EXPECT_EQ(env_->new_fn(std::numeric_limits<jint>::min()), nullptr); \ |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 983 | jni_abort_catcher.Check("negative array length: -2147483648"); \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 984 | /* Pass the array as null. */ \ |
| 985 | EXPECT_EQ(0, env_->GetArrayLength(nullptr)); \ |
| 986 | jni_abort_catcher.Check("java_array == null"); \ |
| 987 | env_->get_region_fn(nullptr, 0, 0, nullptr); \ |
| 988 | jni_abort_catcher.Check("java_array == null"); \ |
| 989 | env_->set_region_fn(nullptr, 0, 0, nullptr); \ |
| 990 | jni_abort_catcher.Check("java_array == null"); \ |
| 991 | env_->get_elements_fn(nullptr, nullptr); \ |
| 992 | jni_abort_catcher.Check("java_array == null"); \ |
| 993 | env_->release_elements_fn(nullptr, nullptr, 0); \ |
| 994 | jni_abort_catcher.Check("java_array == null"); \ |
| 995 | /* Pass the elements for region as null. */ \ |
| 996 | scalar_type ## Array a = env_->new_fn(size); \ |
| 997 | env_->get_region_fn(a, 0, size, nullptr); \ |
| 998 | jni_abort_catcher.Check("buf == null"); \ |
| 999 | env_->set_region_fn(a, 0, size, nullptr); \ |
| 1000 | jni_abort_catcher.Check("buf == null"); \ |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1001 | down_cast<JNIEnvExt*>(env_)->SetCheckJniEnabled(true); \ |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 1002 | } \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1003 | /* Allocate an array and check it has the right type and length. */ \ |
| 1004 | scalar_type ## Array a = env_->new_fn(size); \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1005 | EXPECT_NE(a, nullptr); \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1006 | EXPECT_TRUE(env_->IsInstanceOf(a, env_->FindClass(expected_class_descriptor))); \ |
| 1007 | EXPECT_EQ(size, env_->GetArrayLength(a)); \ |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 1008 | \ |
| 1009 | /* GetPrimitiveArrayRegion/SetPrimitiveArrayRegion */ \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1010 | /* AIOOBE for negative start offset. */ \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1011 | env_->get_region_fn(a, -1, 1, nullptr); \ |
| 1012 | ExpectException(aioobe_); \ |
| 1013 | env_->set_region_fn(a, -1, 1, nullptr); \ |
| 1014 | ExpectException(aioobe_); \ |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 1015 | \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1016 | /* AIOOBE for negative length. */ \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1017 | env_->get_region_fn(a, 0, -1, nullptr); \ |
| 1018 | ExpectException(aioobe_); \ |
| 1019 | env_->set_region_fn(a, 0, -1, nullptr); \ |
| 1020 | ExpectException(aioobe_); \ |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 1021 | \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1022 | /* AIOOBE for buffer overrun. */ \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1023 | env_->get_region_fn(a, size - 1, size, nullptr); \ |
| 1024 | ExpectException(aioobe_); \ |
| 1025 | env_->set_region_fn(a, size - 1, size, nullptr); \ |
| 1026 | ExpectException(aioobe_); \ |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 1027 | \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1028 | /* It's okay for the buffer to be nullptr as long as the length is 0. */ \ |
| 1029 | env_->get_region_fn(a, 2, 0, nullptr); \ |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 1030 | /* Even if the offset is invalid... */ \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1031 | env_->get_region_fn(a, 123, 0, nullptr); \ |
| 1032 | ExpectException(aioobe_); \ |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 1033 | \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1034 | /* It's okay for the buffer to be nullptr as long as the length is 0. */ \ |
| 1035 | env_->set_region_fn(a, 2, 0, nullptr); \ |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 1036 | /* Even if the offset is invalid... */ \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1037 | env_->set_region_fn(a, 123, 0, nullptr); \ |
| 1038 | ExpectException(aioobe_); \ |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 1039 | \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1040 | /* Prepare a couple of buffers. */ \ |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1041 | std::unique_ptr<scalar_type[]> src_buf(new scalar_type[size]); \ |
| 1042 | std::unique_ptr<scalar_type[]> dst_buf(new scalar_type[size]); \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1043 | for (jsize i = 0; i < size; ++i) { src_buf[i] = scalar_type(i); } \ |
| 1044 | for (jsize i = 0; i < size; ++i) { dst_buf[i] = scalar_type(-1); } \ |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 1045 | \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1046 | /* Copy all of src_buf onto the heap. */ \ |
Elliott Hughes | ee0fa76 | 2012-03-26 17:12:41 -0700 | [diff] [blame] | 1047 | env_->set_region_fn(a, 0, size, &src_buf[0]); \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1048 | /* Copy back only part. */ \ |
| 1049 | env_->get_region_fn(a, 1, size - 2, &dst_buf[1]); \ |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1050 | EXPECT_NE(memcmp(&src_buf[0], &dst_buf[0], size * sizeof(scalar_type)), 0) \ |
| 1051 | << "short copy equal"; \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1052 | /* Copy the missing pieces. */ \ |
Elliott Hughes | ee0fa76 | 2012-03-26 17:12:41 -0700 | [diff] [blame] | 1053 | env_->get_region_fn(a, 0, 1, &dst_buf[0]); \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1054 | env_->get_region_fn(a, size - 1, 1, &dst_buf[size - 1]); \ |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1055 | EXPECT_EQ(memcmp(&src_buf[0], &dst_buf[0], size * sizeof(scalar_type)), 0) \ |
| 1056 | << "fixed copy not equal"; \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1057 | /* Copy back the whole array. */ \ |
Elliott Hughes | ee0fa76 | 2012-03-26 17:12:41 -0700 | [diff] [blame] | 1058 | env_->get_region_fn(a, 0, size, &dst_buf[0]); \ |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1059 | EXPECT_EQ(memcmp(&src_buf[0], &dst_buf[0], size * sizeof(scalar_type)), 0) \ |
| 1060 | << "full copy not equal"; \ |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1061 | /* GetPrimitiveArrayCritical */ \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1062 | void* v = env_->GetPrimitiveArrayCritical(a, nullptr); \ |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1063 | EXPECT_EQ(memcmp(&src_buf[0], v, size * sizeof(scalar_type)), 0) \ |
| 1064 | << "GetPrimitiveArrayCritical not equal"; \ |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1065 | env_->ReleasePrimitiveArrayCritical(a, v, 0); \ |
| 1066 | /* GetXArrayElements */ \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1067 | scalar_type* xs = env_->get_elements_fn(a, nullptr); \ |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1068 | EXPECT_EQ(memcmp(&src_buf[0], xs, size * sizeof(scalar_type)), 0) \ |
| 1069 | << # get_elements_fn " not equal"; \ |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1070 | env_->release_elements_fn(a, xs, 0); \ |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 1071 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1072 | TEST_F(JniInternalTest, BooleanArrays) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1073 | EXPECT_PRIMITIVE_ARRAY(NewBooleanArray, GetBooleanArrayRegion, SetBooleanArrayRegion, |
| 1074 | GetBooleanArrayElements, ReleaseBooleanArrayElements, jboolean, "[Z"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1075 | } |
| 1076 | TEST_F(JniInternalTest, ByteArrays) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1077 | EXPECT_PRIMITIVE_ARRAY(NewByteArray, GetByteArrayRegion, SetByteArrayRegion, |
| 1078 | GetByteArrayElements, ReleaseByteArrayElements, jbyte, "[B"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1079 | } |
| 1080 | TEST_F(JniInternalTest, CharArrays) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1081 | EXPECT_PRIMITIVE_ARRAY(NewCharArray, GetCharArrayRegion, SetCharArrayRegion, |
| 1082 | GetCharArrayElements, ReleaseCharArrayElements, jchar, "[C"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1083 | } |
| 1084 | TEST_F(JniInternalTest, DoubleArrays) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1085 | EXPECT_PRIMITIVE_ARRAY(NewDoubleArray, GetDoubleArrayRegion, SetDoubleArrayRegion, |
| 1086 | GetDoubleArrayElements, ReleaseDoubleArrayElements, jdouble, "[D"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1087 | } |
| 1088 | TEST_F(JniInternalTest, FloatArrays) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1089 | EXPECT_PRIMITIVE_ARRAY(NewFloatArray, GetFloatArrayRegion, SetFloatArrayRegion, |
| 1090 | GetFloatArrayElements, ReleaseFloatArrayElements, jfloat, "[F"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1091 | } |
| 1092 | TEST_F(JniInternalTest, IntArrays) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1093 | EXPECT_PRIMITIVE_ARRAY(NewIntArray, GetIntArrayRegion, SetIntArrayRegion, |
| 1094 | GetIntArrayElements, ReleaseIntArrayElements, jint, "[I"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1095 | } |
| 1096 | TEST_F(JniInternalTest, LongArrays) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1097 | EXPECT_PRIMITIVE_ARRAY(NewLongArray, GetLongArrayRegion, SetLongArrayRegion, |
| 1098 | GetLongArrayElements, ReleaseLongArrayElements, jlong, "[J"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1099 | } |
| 1100 | TEST_F(JniInternalTest, ShortArrays) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1101 | EXPECT_PRIMITIVE_ARRAY(NewShortArray, GetShortArrayRegion, SetShortArrayRegion, |
| 1102 | GetShortArrayElements, ReleaseShortArrayElements, jshort, "[S"); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 1103 | } |
| 1104 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1105 | TEST_F(JniInternalTest, GetPrimitiveArrayElementsOfWrongType) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1106 | GetPrimitiveArrayElementsOfWrongType(false); |
| 1107 | GetPrimitiveArrayElementsOfWrongType(true); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1108 | } |
| 1109 | |
| 1110 | TEST_F(JniInternalTest, ReleasePrimitiveArrayElementsOfWrongType) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1111 | ReleasePrimitiveArrayElementsOfWrongType(false); |
| 1112 | ReleasePrimitiveArrayElementsOfWrongType(true); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1113 | } |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1114 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1115 | TEST_F(JniInternalTest, GetReleasePrimitiveArrayCriticalOfWrongType) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1116 | GetReleasePrimitiveArrayCriticalOfWrongType(false); |
| 1117 | GetReleasePrimitiveArrayCriticalOfWrongType(true); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | TEST_F(JniInternalTest, GetPrimitiveArrayRegionElementsOfWrongType) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1121 | GetPrimitiveArrayRegionElementsOfWrongType(false); |
| 1122 | GetPrimitiveArrayRegionElementsOfWrongType(true); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1123 | } |
| 1124 | |
| 1125 | TEST_F(JniInternalTest, SetPrimitiveArrayRegionElementsOfWrongType) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1126 | SetPrimitiveArrayRegionElementsOfWrongType(false); |
| 1127 | SetPrimitiveArrayRegionElementsOfWrongType(true); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1128 | } |
| 1129 | |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 1130 | TEST_F(JniInternalTest, NewObjectArray) { |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 1131 | jclass element_class = env_->FindClass("java/lang/String"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1132 | ASSERT_NE(element_class, nullptr); |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 1133 | jclass array_class = env_->FindClass("[Ljava/lang/String;"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1134 | ASSERT_NE(array_class, nullptr); |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 1135 | |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 1136 | jobjectArray a = env_->NewObjectArray(0, element_class, nullptr); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1137 | EXPECT_NE(a, nullptr); |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 1138 | EXPECT_TRUE(env_->IsInstanceOf(a, array_class)); |
| 1139 | EXPECT_EQ(0, env_->GetArrayLength(a)); |
| 1140 | |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 1141 | a = env_->NewObjectArray(1, element_class, nullptr); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1142 | EXPECT_NE(a, nullptr); |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 1143 | EXPECT_TRUE(env_->IsInstanceOf(a, array_class)); |
| 1144 | EXPECT_EQ(1, env_->GetArrayLength(a)); |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 1145 | EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(a, 0), nullptr)); |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 1146 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1147 | // Negative array length checks. |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1148 | NewObjectArrayBadArguments(false); |
| 1149 | NewObjectArrayBadArguments(true); |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 1150 | } |
| 1151 | |
| 1152 | TEST_F(JniInternalTest, NewObjectArrayWithPrimitiveClasses) { |
| 1153 | const char* primitive_descriptors = "VZBSCIJFD"; |
| 1154 | const char* primitive_names[] = { |
| 1155 | "void", "boolean", "byte", "short", "char", "int", "long", "float", "double" |
| 1156 | }; |
| 1157 | ASSERT_EQ(strlen(primitive_descriptors), arraysize(primitive_names)); |
| 1158 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1159 | bool old_check_jni = vm_->SetCheckJniEnabled(false); |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 1160 | CheckJniAbortCatcher jni_abort_catcher; |
| 1161 | for (size_t i = 0; i < strlen(primitive_descriptors); ++i) { |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1162 | env_->NewObjectArray(0, nullptr, nullptr); |
| 1163 | jni_abort_catcher.Check("element_jclass == null"); |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 1164 | jclass primitive_class = GetPrimitiveClass(primitive_descriptors[i]); |
| 1165 | env_->NewObjectArray(1, primitive_class, nullptr); |
| 1166 | std::string error_msg(StringPrintf("not an object type: %s", primitive_names[i])); |
| 1167 | jni_abort_catcher.Check(error_msg.c_str()); |
| 1168 | } |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1169 | EXPECT_FALSE(vm_->SetCheckJniEnabled(true)); |
| 1170 | for (size_t i = 0; i < strlen(primitive_descriptors); ++i) { |
| 1171 | env_->NewObjectArray(0, nullptr, nullptr); |
| 1172 | jni_abort_catcher.Check("NewObjectArray received NULL jclass"); |
| 1173 | jclass primitive_class = GetPrimitiveClass(primitive_descriptors[i]); |
| 1174 | env_->NewObjectArray(1, primitive_class, nullptr); |
| 1175 | std::string error_msg(StringPrintf("not an object type: %s", primitive_names[i])); |
| 1176 | jni_abort_catcher.Check(error_msg.c_str()); |
| 1177 | } |
| 1178 | EXPECT_TRUE(vm_->SetCheckJniEnabled(old_check_jni)); |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 1179 | } |
| 1180 | |
| 1181 | TEST_F(JniInternalTest, NewObjectArrayWithInitialValue) { |
| 1182 | jclass element_class = env_->FindClass("java/lang/String"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1183 | ASSERT_NE(element_class, nullptr); |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 1184 | jclass array_class = env_->FindClass("[Ljava/lang/String;"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1185 | ASSERT_NE(array_class, nullptr); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1186 | |
| 1187 | jstring s = env_->NewStringUTF("poop"); |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 1188 | jobjectArray a = env_->NewObjectArray(2, element_class, s); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1189 | EXPECT_NE(a, nullptr); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1190 | EXPECT_TRUE(env_->IsInstanceOf(a, array_class)); |
| 1191 | EXPECT_EQ(2, env_->GetArrayLength(a)); |
| 1192 | EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(a, 0), s)); |
| 1193 | EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(a, 1), s)); |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 1194 | |
| 1195 | // Attempt to incorrect create an array of strings with initial value of string arrays. |
| 1196 | CheckJniAbortCatcher jni_abort_catcher; |
| 1197 | env_->NewObjectArray(2, element_class, a); |
| 1198 | jni_abort_catcher.Check("cannot assign object of type 'java.lang.String[]' to array with element " |
| 1199 | "type of 'java.lang.String'"); |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | TEST_F(JniInternalTest, GetArrayLength) { |
Ian Rogers | 2f022bd | 2014-11-11 08:43:05 -0800 | [diff] [blame] | 1203 | // Already tested in NewObjectArray/NewPrimitiveArray except for NULL. |
| 1204 | CheckJniAbortCatcher jni_abort_catcher; |
| 1205 | bool old_check_jni = vm_->SetCheckJniEnabled(false); |
| 1206 | EXPECT_EQ(0, env_->GetArrayLength(nullptr)); |
| 1207 | jni_abort_catcher.Check("java_array == null"); |
| 1208 | EXPECT_FALSE(vm_->SetCheckJniEnabled(true)); |
| 1209 | EXPECT_EQ(JNI_ERR, env_->GetArrayLength(nullptr)); |
| 1210 | jni_abort_catcher.Check("jarray was NULL"); |
| 1211 | EXPECT_TRUE(vm_->SetCheckJniEnabled(old_check_jni)); |
Elliott Hughes | 8a26c5c | 2011-08-15 18:35:43 -0700 | [diff] [blame] | 1212 | } |
| 1213 | |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1214 | TEST_F(JniInternalTest, GetObjectClass) { |
| 1215 | jclass string_class = env_->FindClass("java/lang/String"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1216 | ASSERT_NE(string_class, nullptr); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1217 | jclass class_class = env_->FindClass("java/lang/Class"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1218 | ASSERT_NE(class_class, nullptr); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1219 | |
| 1220 | jstring s = env_->NewStringUTF("poop"); |
| 1221 | jclass c = env_->GetObjectClass(s); |
| 1222 | ASSERT_TRUE(env_->IsSameObject(string_class, c)); |
| 1223 | |
| 1224 | jclass c2 = env_->GetObjectClass(c); |
| 1225 | ASSERT_TRUE(env_->IsSameObject(class_class, env_->GetObjectClass(c2))); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1226 | |
| 1227 | // Null as object should fail. |
| 1228 | CheckJniAbortCatcher jni_abort_catcher; |
| 1229 | EXPECT_EQ(env_->GetObjectClass(nullptr), nullptr); |
| 1230 | jni_abort_catcher.Check("java_object == null"); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1231 | } |
| 1232 | |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1233 | TEST_F(JniInternalTest, GetSuperclass) { |
| 1234 | jclass object_class = env_->FindClass("java/lang/Object"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1235 | ASSERT_NE(object_class, nullptr); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1236 | jclass string_class = env_->FindClass("java/lang/String"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1237 | ASSERT_NE(string_class, nullptr); |
Ian Rogers | dc18020 | 2012-01-29 14:47:29 -0800 | [diff] [blame] | 1238 | jclass runnable_interface = env_->FindClass("java/lang/Runnable"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1239 | ASSERT_NE(runnable_interface, nullptr); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1240 | ASSERT_TRUE(env_->IsSameObject(object_class, env_->GetSuperclass(string_class))); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1241 | ASSERT_EQ(env_->GetSuperclass(object_class), nullptr); |
Ian Rogers | dc18020 | 2012-01-29 14:47:29 -0800 | [diff] [blame] | 1242 | ASSERT_TRUE(env_->IsSameObject(object_class, env_->GetSuperclass(runnable_interface))); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1243 | |
| 1244 | // Null as class should fail. |
| 1245 | CheckJniAbortCatcher jni_abort_catcher; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1246 | bool old_check_jni = vm_->SetCheckJniEnabled(false); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1247 | EXPECT_EQ(env_->GetSuperclass(nullptr), nullptr); |
| 1248 | jni_abort_catcher.Check("java_class == null"); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1249 | EXPECT_FALSE(vm_->SetCheckJniEnabled(true)); |
| 1250 | EXPECT_EQ(env_->GetSuperclass(nullptr), nullptr); |
| 1251 | jni_abort_catcher.Check("GetSuperclass received NULL jclass"); |
| 1252 | EXPECT_TRUE(vm_->SetCheckJniEnabled(old_check_jni)); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1253 | } |
| 1254 | |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1255 | TEST_F(JniInternalTest, IsAssignableFrom) { |
| 1256 | jclass object_class = env_->FindClass("java/lang/Object"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1257 | ASSERT_NE(object_class, nullptr); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1258 | jclass string_class = env_->FindClass("java/lang/String"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1259 | ASSERT_NE(string_class, nullptr); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1260 | |
Narayan Kamath | 1268b74 | 2014-07-11 19:15:11 +0100 | [diff] [blame] | 1261 | // A superclass is assignable from an instance of its |
| 1262 | // subclass but not vice versa. |
| 1263 | ASSERT_TRUE(env_->IsAssignableFrom(string_class, object_class)); |
| 1264 | ASSERT_FALSE(env_->IsAssignableFrom(object_class, string_class)); |
| 1265 | |
| 1266 | jclass charsequence_interface = env_->FindClass("java/lang/CharSequence"); |
| 1267 | ASSERT_NE(charsequence_interface, nullptr); |
| 1268 | |
| 1269 | // An interface is assignable from an instance of an implementing |
| 1270 | // class but not vice versa. |
| 1271 | ASSERT_TRUE(env_->IsAssignableFrom(string_class, charsequence_interface)); |
| 1272 | ASSERT_FALSE(env_->IsAssignableFrom(charsequence_interface, string_class)); |
| 1273 | |
| 1274 | // Check that arrays are covariant. |
| 1275 | jclass string_array_class = env_->FindClass("[Ljava/lang/String;"); |
| 1276 | ASSERT_NE(string_array_class, nullptr); |
| 1277 | jclass object_array_class = env_->FindClass("[Ljava/lang/Object;"); |
| 1278 | ASSERT_NE(object_array_class, nullptr); |
| 1279 | ASSERT_TRUE(env_->IsAssignableFrom(string_array_class, object_array_class)); |
| 1280 | ASSERT_FALSE(env_->IsAssignableFrom(object_array_class, string_array_class)); |
| 1281 | |
| 1282 | // Primitive types are tested in 004-JniTest. |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1283 | |
| 1284 | // Null as either class should fail. |
| 1285 | CheckJniAbortCatcher jni_abort_catcher; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1286 | bool old_check_jni = vm_->SetCheckJniEnabled(false); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1287 | EXPECT_EQ(env_->IsAssignableFrom(nullptr, string_class), JNI_FALSE); |
| 1288 | jni_abort_catcher.Check("java_class1 == null"); |
| 1289 | EXPECT_EQ(env_->IsAssignableFrom(object_class, nullptr), JNI_FALSE); |
| 1290 | jni_abort_catcher.Check("java_class2 == null"); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1291 | EXPECT_FALSE(vm_->SetCheckJniEnabled(true)); |
| 1292 | EXPECT_EQ(env_->IsAssignableFrom(nullptr, string_class), JNI_FALSE); |
| 1293 | jni_abort_catcher.Check("IsAssignableFrom received NULL jclass"); |
| 1294 | EXPECT_EQ(env_->IsAssignableFrom(object_class, nullptr), JNI_FALSE); |
| 1295 | jni_abort_catcher.Check("IsAssignableFrom received NULL jclass"); |
| 1296 | EXPECT_TRUE(vm_->SetCheckJniEnabled(old_check_jni)); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1297 | } |
| 1298 | |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1299 | TEST_F(JniInternalTest, GetObjectRefType) { |
| 1300 | jclass local = env_->FindClass("java/lang/Object"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1301 | ASSERT_TRUE(local != nullptr); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1302 | EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(local)); |
| 1303 | |
| 1304 | jobject global = env_->NewGlobalRef(local); |
| 1305 | EXPECT_EQ(JNIGlobalRefType, env_->GetObjectRefType(global)); |
| 1306 | |
| 1307 | jweak weak_global = env_->NewWeakGlobalRef(local); |
| 1308 | EXPECT_EQ(JNIWeakGlobalRefType, env_->GetObjectRefType(weak_global)); |
| 1309 | |
Andreas Gampe | a876307 | 2014-12-20 00:08:35 -0800 | [diff] [blame] | 1310 | { |
| 1311 | CheckJniAbortCatcher jni_abort_catcher; |
| 1312 | jobject invalid = reinterpret_cast<jobject>(this); |
| 1313 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(invalid)); |
| 1314 | jni_abort_catcher.Check("use of invalid jobject"); |
| 1315 | } |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1316 | |
| 1317 | // TODO: invoke a native method and test that its arguments are considered local references. |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1318 | |
Andreas Gampe | a876307 | 2014-12-20 00:08:35 -0800 | [diff] [blame] | 1319 | // Null as pointer should not fail and return invalid-ref. b/18820997 |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1320 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(nullptr)); |
Andreas Gampe | a876307 | 2014-12-20 00:08:35 -0800 | [diff] [blame] | 1321 | |
| 1322 | // TODO: Null as reference should return the original type. |
| 1323 | // This requires running a GC so a non-null object gets freed. |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1324 | } |
| 1325 | |
Mathieu Chartier | 0859999 | 2013-12-20 17:17:55 -0800 | [diff] [blame] | 1326 | TEST_F(JniInternalTest, StaleWeakGlobal) { |
| 1327 | jclass java_lang_Class = env_->FindClass("java/lang/Class"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1328 | ASSERT_NE(java_lang_Class, nullptr); |
| 1329 | jobjectArray local_ref = env_->NewObjectArray(1, java_lang_Class, nullptr); |
| 1330 | ASSERT_NE(local_ref, nullptr); |
Mathieu Chartier | 0859999 | 2013-12-20 17:17:55 -0800 | [diff] [blame] | 1331 | jweak weak_global = env_->NewWeakGlobalRef(local_ref); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1332 | ASSERT_NE(weak_global, nullptr); |
Mathieu Chartier | 0859999 | 2013-12-20 17:17:55 -0800 | [diff] [blame] | 1333 | env_->DeleteLocalRef(local_ref); |
| 1334 | Runtime::Current()->GetHeap()->CollectGarbage(false); // GC should clear the weak global. |
| 1335 | jobject new_global_ref = env_->NewGlobalRef(weak_global); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1336 | EXPECT_EQ(new_global_ref, nullptr); |
Mathieu Chartier | 0859999 | 2013-12-20 17:17:55 -0800 | [diff] [blame] | 1337 | jobject new_local_ref = env_->NewLocalRef(weak_global); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1338 | EXPECT_EQ(new_local_ref, nullptr); |
Mathieu Chartier | 0859999 | 2013-12-20 17:17:55 -0800 | [diff] [blame] | 1339 | } |
| 1340 | |
Elliott Hughes | 8a26c5c | 2011-08-15 18:35:43 -0700 | [diff] [blame] | 1341 | TEST_F(JniInternalTest, NewStringUTF) { |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1342 | EXPECT_EQ(env_->NewStringUTF(nullptr), nullptr); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1343 | jstring s; |
| 1344 | |
| 1345 | s = env_->NewStringUTF(""); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1346 | EXPECT_NE(s, nullptr); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1347 | EXPECT_EQ(0, env_->GetStringLength(s)); |
| 1348 | EXPECT_EQ(0, env_->GetStringUTFLength(s)); |
| 1349 | s = env_->NewStringUTF("hello"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1350 | EXPECT_NE(s, nullptr); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1351 | EXPECT_EQ(5, env_->GetStringLength(s)); |
| 1352 | EXPECT_EQ(5, env_->GetStringUTFLength(s)); |
| 1353 | |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 1354 | // Encoded surrogate pair. |
| 1355 | s = env_->NewStringUTF("\xed\xa0\x81\xed\xb0\x80"); |
| 1356 | EXPECT_NE(s, nullptr); |
| 1357 | EXPECT_EQ(2, env_->GetStringLength(s)); |
Narayan Kamath | e16dad1 | 2015-02-13 11:49:22 +0000 | [diff] [blame] | 1358 | |
| 1359 | // The surrogate pair gets encoded into a 4 byte UTF sequence.. |
| 1360 | EXPECT_EQ(4, env_->GetStringUTFLength(s)); |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 1361 | const char* chars = env_->GetStringUTFChars(s, nullptr); |
Narayan Kamath | e16dad1 | 2015-02-13 11:49:22 +0000 | [diff] [blame] | 1362 | EXPECT_STREQ("\xf0\x90\x90\x80", chars); |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 1363 | env_->ReleaseStringUTFChars(s, chars); |
| 1364 | |
Narayan Kamath | e16dad1 | 2015-02-13 11:49:22 +0000 | [diff] [blame] | 1365 | // .. but is stored as is in the utf-16 representation. |
| 1366 | const jchar* jchars = env_->GetStringChars(s, nullptr); |
| 1367 | EXPECT_EQ(0xd801, jchars[0]); |
| 1368 | EXPECT_EQ(0xdc00, jchars[1]); |
| 1369 | env_->ReleaseStringChars(s, jchars); |
| 1370 | |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 1371 | // 4 byte UTF sequence appended to an encoded surrogate pair. |
| 1372 | s = env_->NewStringUTF("\xed\xa0\x81\xed\xb0\x80 \xf0\x9f\x8f\xa0"); |
| 1373 | EXPECT_NE(s, nullptr); |
Narayan Kamath | e16dad1 | 2015-02-13 11:49:22 +0000 | [diff] [blame] | 1374 | |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 1375 | // The 4 byte sequence {0xf0, 0x9f, 0x8f, 0xa0} is converted into a surrogate |
Narayan Kamath | e16dad1 | 2015-02-13 11:49:22 +0000 | [diff] [blame] | 1376 | // pair {0xd83c, 0xdfe0}. |
| 1377 | EXPECT_EQ(5, env_->GetStringLength(s)); |
| 1378 | jchars = env_->GetStringChars(s, nullptr); |
| 1379 | // The first surrogate pair, encoded as such in the input. |
| 1380 | EXPECT_EQ(0xd801, jchars[0]); |
| 1381 | EXPECT_EQ(0xdc00, jchars[1]); |
| 1382 | // The second surrogate pair, from the 4 byte UTF sequence in the input. |
| 1383 | EXPECT_EQ(0xd83c, jchars[3]); |
| 1384 | EXPECT_EQ(0xdfe0, jchars[4]); |
| 1385 | env_->ReleaseStringChars(s, jchars); |
| 1386 | |
| 1387 | EXPECT_EQ(9, env_->GetStringUTFLength(s)); |
| 1388 | chars = env_->GetStringUTFChars(s, nullptr); |
| 1389 | EXPECT_STREQ("\xf0\x90\x90\x80 \xf0\x9f\x8f\xa0", chars); |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 1390 | env_->ReleaseStringUTFChars(s, chars); |
| 1391 | |
| 1392 | // A string with 1, 2, 3 and 4 byte UTF sequences with spaces |
| 1393 | // between them |
| 1394 | s = env_->NewStringUTF("\x24 \xc2\xa2 \xe2\x82\xac \xf0\x9f\x8f\xa0"); |
| 1395 | EXPECT_NE(s, nullptr); |
| 1396 | EXPECT_EQ(8, env_->GetStringLength(s)); |
Narayan Kamath | e16dad1 | 2015-02-13 11:49:22 +0000 | [diff] [blame] | 1397 | EXPECT_EQ(13, env_->GetStringUTFLength(s)); |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 1398 | } |
| 1399 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1400 | TEST_F(JniInternalTest, NewString) { |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1401 | jchar chars[] = { 'h', 'i' }; |
| 1402 | jstring s; |
| 1403 | s = env_->NewString(chars, 0); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1404 | EXPECT_NE(s, nullptr); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1405 | EXPECT_EQ(0, env_->GetStringLength(s)); |
| 1406 | EXPECT_EQ(0, env_->GetStringUTFLength(s)); |
| 1407 | s = env_->NewString(chars, 2); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1408 | EXPECT_NE(s, nullptr); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1409 | EXPECT_EQ(2, env_->GetStringLength(s)); |
| 1410 | EXPECT_EQ(2, env_->GetStringUTFLength(s)); |
| 1411 | |
| 1412 | // TODO: check some non-ASCII strings. |
| 1413 | } |
| 1414 | |
Jesse Wilson | 25e79a5 | 2011-11-18 15:31:58 -0500 | [diff] [blame] | 1415 | TEST_F(JniInternalTest, NewStringNullCharsZeroLength) { |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 1416 | jstring s = env_->NewString(nullptr, 0); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1417 | EXPECT_NE(s, nullptr); |
Jesse Wilson | 25e79a5 | 2011-11-18 15:31:58 -0500 | [diff] [blame] | 1418 | EXPECT_EQ(0, env_->GetStringLength(s)); |
| 1419 | } |
| 1420 | |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 1421 | TEST_F(JniInternalTest, NewStringNullCharsNonzeroLength) { |
| 1422 | CheckJniAbortCatcher jni_abort_catcher; |
| 1423 | env_->NewString(nullptr, 1); |
| 1424 | jni_abort_catcher.Check("chars == null && char_count > 0"); |
| 1425 | } |
| 1426 | |
| 1427 | TEST_F(JniInternalTest, NewStringNegativeLength) { |
| 1428 | CheckJniAbortCatcher jni_abort_catcher; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1429 | bool old_check_jni = vm_->SetCheckJniEnabled(false); |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 1430 | env_->NewString(nullptr, -1); |
| 1431 | jni_abort_catcher.Check("char_count < 0: -1"); |
| 1432 | env_->NewString(nullptr, std::numeric_limits<jint>::min()); |
| 1433 | jni_abort_catcher.Check("char_count < 0: -2147483648"); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1434 | EXPECT_FALSE(vm_->SetCheckJniEnabled(true)); |
| 1435 | env_->NewString(nullptr, -1); |
| 1436 | jni_abort_catcher.Check("negative jsize: -1"); |
| 1437 | env_->NewString(nullptr, std::numeric_limits<jint>::min()); |
| 1438 | jni_abort_catcher.Check("negative jsize: -2147483648"); |
| 1439 | EXPECT_TRUE(vm_->SetCheckJniEnabled(old_check_jni)); |
Jesse Wilson | 25e79a5 | 2011-11-18 15:31:58 -0500 | [diff] [blame] | 1440 | } |
| 1441 | |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1442 | TEST_F(JniInternalTest, GetStringLength_GetStringUTFLength) { |
| 1443 | // Already tested in the NewString/NewStringUTF tests. |
| 1444 | } |
| 1445 | |
| 1446 | TEST_F(JniInternalTest, GetStringRegion_GetStringUTFRegion) { |
| 1447 | jstring s = env_->NewStringUTF("hello"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1448 | ASSERT_TRUE(s != nullptr); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1449 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1450 | env_->GetStringRegion(s, -1, 0, nullptr); |
| 1451 | ExpectException(sioobe_); |
| 1452 | env_->GetStringRegion(s, 0, -1, nullptr); |
| 1453 | ExpectException(sioobe_); |
| 1454 | env_->GetStringRegion(s, 0, 10, nullptr); |
| 1455 | ExpectException(sioobe_); |
| 1456 | env_->GetStringRegion(s, 10, 1, nullptr); |
| 1457 | ExpectException(sioobe_); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1458 | |
| 1459 | jchar chars[4] = { 'x', 'x', 'x', 'x' }; |
| 1460 | env_->GetStringRegion(s, 1, 2, &chars[1]); |
| 1461 | EXPECT_EQ('x', chars[0]); |
| 1462 | EXPECT_EQ('e', chars[1]); |
| 1463 | EXPECT_EQ('l', chars[2]); |
| 1464 | EXPECT_EQ('x', chars[3]); |
| 1465 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1466 | // It's okay for the buffer to be nullptr as long as the length is 0. |
| 1467 | env_->GetStringRegion(s, 2, 0, nullptr); |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 1468 | // Even if the offset is invalid... |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1469 | env_->GetStringRegion(s, 123, 0, nullptr); |
| 1470 | ExpectException(sioobe_); |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 1471 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1472 | env_->GetStringUTFRegion(s, -1, 0, nullptr); |
| 1473 | ExpectException(sioobe_); |
| 1474 | env_->GetStringUTFRegion(s, 0, -1, nullptr); |
| 1475 | ExpectException(sioobe_); |
| 1476 | env_->GetStringUTFRegion(s, 0, 10, nullptr); |
| 1477 | ExpectException(sioobe_); |
| 1478 | env_->GetStringUTFRegion(s, 10, 1, nullptr); |
| 1479 | ExpectException(sioobe_); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1480 | |
| 1481 | char bytes[4] = { 'x', 'x', 'x', 'x' }; |
| 1482 | env_->GetStringUTFRegion(s, 1, 2, &bytes[1]); |
| 1483 | EXPECT_EQ('x', bytes[0]); |
| 1484 | EXPECT_EQ('e', bytes[1]); |
| 1485 | EXPECT_EQ('l', bytes[2]); |
| 1486 | EXPECT_EQ('x', bytes[3]); |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 1487 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1488 | // It's okay for the buffer to be nullptr as long as the length is 0. |
| 1489 | env_->GetStringUTFRegion(s, 2, 0, nullptr); |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 1490 | // Even if the offset is invalid... |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1491 | env_->GetStringUTFRegion(s, 123, 0, nullptr); |
| 1492 | ExpectException(sioobe_); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1493 | } |
| 1494 | |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1495 | TEST_F(JniInternalTest, GetStringUTFChars_ReleaseStringUTFChars) { |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1496 | // Passing in a nullptr jstring is ignored normally, but caught by -Xcheck:jni. |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1497 | bool old_check_jni = vm_->SetCheckJniEnabled(false); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1498 | { |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1499 | CheckJniAbortCatcher check_jni_abort_catcher; |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1500 | EXPECT_EQ(env_->GetStringUTFChars(nullptr, nullptr), nullptr); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1501 | } |
| 1502 | { |
| 1503 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 1504 | EXPECT_FALSE(vm_->SetCheckJniEnabled(true)); |
| 1505 | EXPECT_EQ(env_->GetStringUTFChars(nullptr, nullptr), nullptr); |
| 1506 | check_jni_abort_catcher.Check("GetStringUTFChars received NULL jstring"); |
| 1507 | EXPECT_TRUE(vm_->SetCheckJniEnabled(old_check_jni)); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1508 | } |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1509 | |
| 1510 | jstring s = env_->NewStringUTF("hello"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1511 | ASSERT_TRUE(s != nullptr); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1512 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1513 | const char* utf = env_->GetStringUTFChars(s, nullptr); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1514 | EXPECT_STREQ("hello", utf); |
| 1515 | env_->ReleaseStringUTFChars(s, utf); |
| 1516 | |
| 1517 | jboolean is_copy = JNI_FALSE; |
| 1518 | utf = env_->GetStringUTFChars(s, &is_copy); |
| 1519 | EXPECT_EQ(JNI_TRUE, is_copy); |
| 1520 | EXPECT_STREQ("hello", utf); |
| 1521 | env_->ReleaseStringUTFChars(s, utf); |
| 1522 | } |
| 1523 | |
| 1524 | TEST_F(JniInternalTest, GetStringChars_ReleaseStringChars) { |
| 1525 | jstring s = env_->NewStringUTF("hello"); |
Fred Shih | 56890e2 | 2014-06-02 11:11:52 -0700 | [diff] [blame] | 1526 | ScopedObjectAccess soa(env_); |
| 1527 | mirror::String* s_m = soa.Decode<mirror::String*>(s); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1528 | ASSERT_TRUE(s != nullptr); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1529 | |
| 1530 | jchar expected[] = { 'h', 'e', 'l', 'l', 'o' }; |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1531 | const jchar* chars = env_->GetStringChars(s, nullptr); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1532 | EXPECT_EQ(expected[0], chars[0]); |
| 1533 | EXPECT_EQ(expected[1], chars[1]); |
| 1534 | EXPECT_EQ(expected[2], chars[2]); |
| 1535 | EXPECT_EQ(expected[3], chars[3]); |
| 1536 | EXPECT_EQ(expected[4], chars[4]); |
| 1537 | env_->ReleaseStringChars(s, chars); |
| 1538 | |
| 1539 | jboolean is_copy = JNI_FALSE; |
| 1540 | chars = env_->GetStringChars(s, &is_copy); |
Fred Shih | 56890e2 | 2014-06-02 11:11:52 -0700 | [diff] [blame] | 1541 | if (Runtime::Current()->GetHeap()->IsMovableObject(s_m->GetCharArray())) { |
| 1542 | EXPECT_EQ(JNI_TRUE, is_copy); |
| 1543 | } else { |
| 1544 | EXPECT_EQ(JNI_FALSE, is_copy); |
| 1545 | } |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1546 | EXPECT_EQ(expected[0], chars[0]); |
| 1547 | EXPECT_EQ(expected[1], chars[1]); |
| 1548 | EXPECT_EQ(expected[2], chars[2]); |
| 1549 | EXPECT_EQ(expected[3], chars[3]); |
| 1550 | EXPECT_EQ(expected[4], chars[4]); |
| 1551 | env_->ReleaseStringChars(s, chars); |
| 1552 | } |
| 1553 | |
| 1554 | TEST_F(JniInternalTest, GetStringCritical_ReleaseStringCritical) { |
| 1555 | jstring s = env_->NewStringUTF("hello"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1556 | ASSERT_TRUE(s != nullptr); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1557 | |
| 1558 | jchar expected[] = { 'h', 'e', 'l', 'l', 'o' }; |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1559 | const jchar* chars = env_->GetStringCritical(s, nullptr); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1560 | EXPECT_EQ(expected[0], chars[0]); |
| 1561 | EXPECT_EQ(expected[1], chars[1]); |
| 1562 | EXPECT_EQ(expected[2], chars[2]); |
| 1563 | EXPECT_EQ(expected[3], chars[3]); |
| 1564 | EXPECT_EQ(expected[4], chars[4]); |
| 1565 | env_->ReleaseStringCritical(s, chars); |
| 1566 | |
Fred Shih | 56890e2 | 2014-06-02 11:11:52 -0700 | [diff] [blame] | 1567 | jboolean is_copy = JNI_TRUE; |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1568 | chars = env_->GetStringCritical(s, &is_copy); |
Fred Shih | 56890e2 | 2014-06-02 11:11:52 -0700 | [diff] [blame] | 1569 | EXPECT_EQ(JNI_FALSE, is_copy); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1570 | EXPECT_EQ(expected[0], chars[0]); |
| 1571 | EXPECT_EQ(expected[1], chars[1]); |
| 1572 | EXPECT_EQ(expected[2], chars[2]); |
| 1573 | EXPECT_EQ(expected[3], chars[3]); |
| 1574 | EXPECT_EQ(expected[4], chars[4]); |
| 1575 | env_->ReleaseStringCritical(s, chars); |
| 1576 | } |
| 1577 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1578 | TEST_F(JniInternalTest, GetObjectArrayElement_SetObjectArrayElement) { |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1579 | jclass java_lang_Class = env_->FindClass("java/lang/Class"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1580 | ASSERT_TRUE(java_lang_Class != nullptr); |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 1581 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1582 | jobjectArray array = env_->NewObjectArray(1, java_lang_Class, nullptr); |
| 1583 | EXPECT_NE(array, nullptr); |
| 1584 | EXPECT_EQ(env_->GetObjectArrayElement(array, 0), nullptr); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1585 | env_->SetObjectArrayElement(array, 0, java_lang_Class); |
| 1586 | EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(array, 0), java_lang_Class)); |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 1587 | |
| 1588 | // ArrayIndexOutOfBounds for negative index. |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1589 | env_->SetObjectArrayElement(array, -1, java_lang_Class); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1590 | ExpectException(aioobe_); |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 1591 | |
| 1592 | // ArrayIndexOutOfBounds for too-large index. |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1593 | env_->SetObjectArrayElement(array, 1, java_lang_Class); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1594 | ExpectException(aioobe_); |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 1595 | |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1596 | // ArrayStoreException thrown for bad types. |
| 1597 | env_->SetObjectArrayElement(array, 0, env_->NewStringUTF("not a jclass!")); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1598 | ExpectException(ase_); |
| 1599 | |
| 1600 | // Null as array should fail. |
| 1601 | CheckJniAbortCatcher jni_abort_catcher; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1602 | bool old_check_jni = vm_->SetCheckJniEnabled(false); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1603 | EXPECT_EQ(nullptr, env_->GetObjectArrayElement(nullptr, 0)); |
| 1604 | jni_abort_catcher.Check("java_array == null"); |
| 1605 | env_->SetObjectArrayElement(nullptr, 0, nullptr); |
| 1606 | jni_abort_catcher.Check("java_array == null"); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1607 | EXPECT_FALSE(vm_->SetCheckJniEnabled(true)); |
| 1608 | EXPECT_EQ(nullptr, env_->GetObjectArrayElement(nullptr, 0)); |
| 1609 | jni_abort_catcher.Check("jarray was NULL"); |
| 1610 | env_->SetObjectArrayElement(nullptr, 0, nullptr); |
| 1611 | jni_abort_catcher.Check("jarray was NULL"); |
| 1612 | EXPECT_TRUE(vm_->SetCheckJniEnabled(old_check_jni)); |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 1613 | } |
| 1614 | |
Ian Rogers | 647b1a8 | 2014-10-10 11:02:11 -0700 | [diff] [blame] | 1615 | #define EXPECT_STATIC_PRIMITIVE_FIELD(expect_eq, type, field_name, sig, value1, value2) \ |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1616 | do { \ |
| 1617 | jfieldID fid = env_->GetStaticFieldID(c, field_name, sig); \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1618 | EXPECT_NE(fid, nullptr); \ |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1619 | env_->SetStatic ## type ## Field(c, fid, value1); \ |
Ian Rogers | 647b1a8 | 2014-10-10 11:02:11 -0700 | [diff] [blame] | 1620 | expect_eq(value1, env_->GetStatic ## type ## Field(c, fid)); \ |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1621 | env_->SetStatic ## type ## Field(c, fid, value2); \ |
Ian Rogers | 647b1a8 | 2014-10-10 11:02:11 -0700 | [diff] [blame] | 1622 | expect_eq(value2, env_->GetStatic ## type ## Field(c, fid)); \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1623 | \ |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1624 | bool old_check_jni = vm_->SetCheckJniEnabled(false); \ |
| 1625 | { \ |
| 1626 | CheckJniAbortCatcher jni_abort_catcher; \ |
| 1627 | env_->GetStatic ## type ## Field(nullptr, fid); \ |
| 1628 | env_->SetStatic ## type ## Field(nullptr, fid, value1); \ |
| 1629 | } \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1630 | CheckJniAbortCatcher jni_abort_catcher; \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1631 | env_->GetStatic ## type ## Field(c, nullptr); \ |
| 1632 | jni_abort_catcher.Check("fid == null"); \ |
| 1633 | env_->SetStatic ## type ## Field(c, nullptr, value1); \ |
| 1634 | jni_abort_catcher.Check("fid == null"); \ |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1635 | \ |
| 1636 | EXPECT_FALSE(vm_->SetCheckJniEnabled(true)); \ |
| 1637 | env_->GetStatic ## type ## Field(nullptr, fid); \ |
| 1638 | jni_abort_catcher.Check("received NULL jclass"); \ |
| 1639 | env_->SetStatic ## type ## Field(nullptr, fid, value1); \ |
| 1640 | jni_abort_catcher.Check("received NULL jclass"); \ |
| 1641 | env_->GetStatic ## type ## Field(c, nullptr); \ |
| 1642 | jni_abort_catcher.Check("jfieldID was NULL"); \ |
| 1643 | env_->SetStatic ## type ## Field(c, nullptr, value1); \ |
| 1644 | jni_abort_catcher.Check("jfieldID was NULL"); \ |
| 1645 | EXPECT_TRUE(vm_->SetCheckJniEnabled(old_check_jni)); \ |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1646 | } while (false) |
| 1647 | |
Ian Rogers | 647b1a8 | 2014-10-10 11:02:11 -0700 | [diff] [blame] | 1648 | #define EXPECT_PRIMITIVE_FIELD(expect_eq, instance, type, field_name, sig, value1, value2) \ |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1649 | do { \ |
| 1650 | jfieldID fid = env_->GetFieldID(c, field_name, sig); \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1651 | EXPECT_NE(fid, nullptr); \ |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1652 | env_->Set ## type ## Field(instance, fid, value1); \ |
Ian Rogers | 647b1a8 | 2014-10-10 11:02:11 -0700 | [diff] [blame] | 1653 | expect_eq(value1, env_->Get ## type ## Field(instance, fid)); \ |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1654 | env_->Set ## type ## Field(instance, fid, value2); \ |
Ian Rogers | 647b1a8 | 2014-10-10 11:02:11 -0700 | [diff] [blame] | 1655 | expect_eq(value2, env_->Get ## type ## Field(instance, fid)); \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1656 | \ |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1657 | bool old_check_jni = vm_->SetCheckJniEnabled(false); \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1658 | CheckJniAbortCatcher jni_abort_catcher; \ |
| 1659 | env_->Get ## type ## Field(nullptr, fid); \ |
| 1660 | jni_abort_catcher.Check("obj == null"); \ |
| 1661 | env_->Set ## type ## Field(nullptr, fid, value1); \ |
| 1662 | jni_abort_catcher.Check("obj == null"); \ |
| 1663 | env_->Get ## type ## Field(instance, nullptr); \ |
| 1664 | jni_abort_catcher.Check("fid == null"); \ |
| 1665 | env_->Set ## type ## Field(instance, nullptr, value1); \ |
| 1666 | jni_abort_catcher.Check("fid == null"); \ |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1667 | EXPECT_FALSE(vm_->SetCheckJniEnabled(true)); \ |
| 1668 | env_->Get ## type ## Field(nullptr, fid); \ |
| 1669 | jni_abort_catcher.Check("field operation on NULL object:"); \ |
| 1670 | env_->Set ## type ## Field(nullptr, fid, value1); \ |
| 1671 | jni_abort_catcher.Check("field operation on NULL object:"); \ |
| 1672 | env_->Get ## type ## Field(instance, nullptr); \ |
| 1673 | jni_abort_catcher.Check("jfieldID was NULL"); \ |
| 1674 | env_->Set ## type ## Field(instance, nullptr, value1); \ |
| 1675 | jni_abort_catcher.Check("jfieldID was NULL"); \ |
| 1676 | EXPECT_TRUE(vm_->SetCheckJniEnabled(old_check_jni)); \ |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1677 | } while (false) |
| 1678 | |
| 1679 | |
| 1680 | TEST_F(JniInternalTest, GetPrimitiveField_SetPrimitiveField) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1681 | Thread::Current()->TransitionFromSuspendedToRunnable(); |
| 1682 | LoadDex("AllFields"); |
Brian Carlstrom | bd86bcc | 2013-03-10 20:26:16 -0700 | [diff] [blame] | 1683 | bool started = runtime_->Start(); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1684 | ASSERT_TRUE(started); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1685 | |
| 1686 | jclass c = env_->FindClass("AllFields"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1687 | ASSERT_NE(c, nullptr); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1688 | jobject o = env_->AllocObject(c); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1689 | ASSERT_NE(o, nullptr); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1690 | |
Ian Rogers | 647b1a8 | 2014-10-10 11:02:11 -0700 | [diff] [blame] | 1691 | EXPECT_STATIC_PRIMITIVE_FIELD(EXPECT_EQ, Boolean, "sZ", "Z", JNI_TRUE, JNI_FALSE); |
| 1692 | EXPECT_STATIC_PRIMITIVE_FIELD(EXPECT_EQ, Byte, "sB", "B", 1, 2); |
| 1693 | EXPECT_STATIC_PRIMITIVE_FIELD(EXPECT_EQ, Char, "sC", "C", 'a', 'b'); |
| 1694 | EXPECT_STATIC_PRIMITIVE_FIELD(EXPECT_DOUBLE_EQ, Double, "sD", "D", 1.0, 2.0); |
| 1695 | EXPECT_STATIC_PRIMITIVE_FIELD(EXPECT_FLOAT_EQ, Float, "sF", "F", 1.0, 2.0); |
| 1696 | EXPECT_STATIC_PRIMITIVE_FIELD(EXPECT_EQ, Int, "sI", "I", 1, 2); |
| 1697 | EXPECT_STATIC_PRIMITIVE_FIELD(EXPECT_EQ, Long, "sJ", "J", 1, 2); |
| 1698 | EXPECT_STATIC_PRIMITIVE_FIELD(EXPECT_EQ, Short, "sS", "S", 1, 2); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1699 | |
Ian Rogers | 647b1a8 | 2014-10-10 11:02:11 -0700 | [diff] [blame] | 1700 | EXPECT_PRIMITIVE_FIELD(EXPECT_EQ, o, Boolean, "iZ", "Z", JNI_TRUE, JNI_FALSE); |
| 1701 | EXPECT_PRIMITIVE_FIELD(EXPECT_EQ, o, Byte, "iB", "B", 1, 2); |
| 1702 | EXPECT_PRIMITIVE_FIELD(EXPECT_EQ, o, Char, "iC", "C", 'a', 'b'); |
| 1703 | EXPECT_PRIMITIVE_FIELD(EXPECT_DOUBLE_EQ, o, Double, "iD", "D", 1.0, 2.0); |
| 1704 | EXPECT_PRIMITIVE_FIELD(EXPECT_FLOAT_EQ, o, Float, "iF", "F", 1.0, 2.0); |
| 1705 | EXPECT_PRIMITIVE_FIELD(EXPECT_EQ, o, Int, "iI", "I", 1, 2); |
| 1706 | EXPECT_PRIMITIVE_FIELD(EXPECT_EQ, o, Long, "iJ", "J", 1, 2); |
| 1707 | EXPECT_PRIMITIVE_FIELD(EXPECT_EQ, o, Short, "iS", "S", 1, 2); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1708 | } |
| 1709 | |
| 1710 | TEST_F(JniInternalTest, GetObjectField_SetObjectField) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1711 | Thread::Current()->TransitionFromSuspendedToRunnable(); |
| 1712 | LoadDex("AllFields"); |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 1713 | runtime_->Start(); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1714 | |
| 1715 | jclass c = env_->FindClass("AllFields"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1716 | ASSERT_NE(c, nullptr); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1717 | jobject o = env_->AllocObject(c); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1718 | ASSERT_NE(o, nullptr); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1719 | |
| 1720 | jstring s1 = env_->NewStringUTF("hello"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1721 | ASSERT_NE(s1, nullptr); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1722 | jstring s2 = env_->NewStringUTF("world"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1723 | ASSERT_NE(s2, nullptr); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1724 | |
| 1725 | jfieldID s_fid = env_->GetStaticFieldID(c, "sObject", "Ljava/lang/Object;"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1726 | ASSERT_NE(s_fid, nullptr); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1727 | jfieldID i_fid = env_->GetFieldID(c, "iObject", "Ljava/lang/Object;"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1728 | ASSERT_NE(i_fid, nullptr); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1729 | |
| 1730 | env_->SetStaticObjectField(c, s_fid, s1); |
| 1731 | ASSERT_TRUE(env_->IsSameObject(s1, env_->GetStaticObjectField(c, s_fid))); |
| 1732 | env_->SetStaticObjectField(c, s_fid, s2); |
| 1733 | ASSERT_TRUE(env_->IsSameObject(s2, env_->GetStaticObjectField(c, s_fid))); |
| 1734 | |
| 1735 | env_->SetObjectField(o, i_fid, s1); |
| 1736 | ASSERT_TRUE(env_->IsSameObject(s1, env_->GetObjectField(o, i_fid))); |
| 1737 | env_->SetObjectField(o, i_fid, s2); |
| 1738 | ASSERT_TRUE(env_->IsSameObject(s2, env_->GetObjectField(o, i_fid))); |
| 1739 | } |
| 1740 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1741 | TEST_F(JniInternalTest, NewLocalRef_nullptr) { |
| 1742 | EXPECT_EQ(env_->NewLocalRef(nullptr), nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1743 | } |
| 1744 | |
| 1745 | TEST_F(JniInternalTest, NewLocalRef) { |
| 1746 | jstring s = env_->NewStringUTF(""); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1747 | ASSERT_NE(s, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1748 | jobject o = env_->NewLocalRef(s); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1749 | EXPECT_NE(o, nullptr); |
| 1750 | EXPECT_NE(o, s); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1751 | |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1752 | EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(o)); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1753 | } |
| 1754 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1755 | TEST_F(JniInternalTest, DeleteLocalRef_nullptr) { |
| 1756 | env_->DeleteLocalRef(nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1757 | } |
| 1758 | |
| 1759 | TEST_F(JniInternalTest, DeleteLocalRef) { |
Andreas Gampe | 369810a | 2015-01-14 19:53:31 -0800 | [diff] [blame] | 1760 | // This tests leads to warnings and errors in the log. |
| 1761 | ScopedLogSeverity sls(LogSeverity::FATAL); |
| 1762 | |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1763 | jstring s = env_->NewStringUTF(""); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1764 | ASSERT_NE(s, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1765 | env_->DeleteLocalRef(s); |
| 1766 | |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 1767 | // Currently, deleting an already-deleted reference is just a CheckJNI warning. |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1768 | { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1769 | bool old_check_jni = vm_->SetCheckJniEnabled(false); |
| 1770 | { |
| 1771 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 1772 | env_->DeleteLocalRef(s); |
| 1773 | } |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1774 | CheckJniAbortCatcher check_jni_abort_catcher; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1775 | EXPECT_FALSE(vm_->SetCheckJniEnabled(true)); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1776 | env_->DeleteLocalRef(s); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1777 | std::string expected(StringPrintf("use of deleted local reference %p", s)); |
Elliott Hughes | a9137c6 | 2013-01-09 10:55:21 -0800 | [diff] [blame] | 1778 | check_jni_abort_catcher.Check(expected.c_str()); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1779 | EXPECT_TRUE(vm_->SetCheckJniEnabled(old_check_jni)); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1780 | } |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1781 | |
| 1782 | s = env_->NewStringUTF(""); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1783 | ASSERT_NE(s, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1784 | jobject o = env_->NewLocalRef(s); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1785 | ASSERT_NE(o, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1786 | |
| 1787 | env_->DeleteLocalRef(s); |
| 1788 | env_->DeleteLocalRef(o); |
| 1789 | } |
| 1790 | |
Elliott Hughes | aa836f7 | 2013-08-20 16:57:23 -0700 | [diff] [blame] | 1791 | TEST_F(JniInternalTest, PushLocalFrame_10395422) { |
| 1792 | // The JNI specification is ambiguous about whether the given capacity is to be interpreted as a |
| 1793 | // maximum or as a minimum, but it seems like it's supposed to be a minimum, and that's how |
| 1794 | // Android historically treated it, and it's how the RI treats it. It's also the more useful |
| 1795 | // interpretation! |
| 1796 | ASSERT_EQ(JNI_OK, env_->PushLocalFrame(0)); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1797 | env_->PopLocalFrame(nullptr); |
Elliott Hughes | aa836f7 | 2013-08-20 16:57:23 -0700 | [diff] [blame] | 1798 | |
Andreas Gampe | 369810a | 2015-01-14 19:53:31 -0800 | [diff] [blame] | 1799 | // The following two tests will print errors to the log. |
| 1800 | ScopedLogSeverity sls(LogSeverity::FATAL); |
| 1801 | |
Elliott Hughes | aa836f7 | 2013-08-20 16:57:23 -0700 | [diff] [blame] | 1802 | // Negative capacities are not allowed. |
| 1803 | ASSERT_EQ(JNI_ERR, env_->PushLocalFrame(-1)); |
| 1804 | |
| 1805 | // And it's okay to have an upper limit. Ours is currently 512. |
| 1806 | ASSERT_EQ(JNI_ERR, env_->PushLocalFrame(8192)); |
| 1807 | } |
| 1808 | |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1809 | TEST_F(JniInternalTest, PushLocalFrame_PopLocalFrame) { |
Andreas Gampe | 369810a | 2015-01-14 19:53:31 -0800 | [diff] [blame] | 1810 | // This tests leads to errors in the log. |
| 1811 | ScopedLogSeverity sls(LogSeverity::FATAL); |
| 1812 | |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1813 | jobject original = env_->NewStringUTF(""); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1814 | ASSERT_NE(original, nullptr); |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1815 | |
| 1816 | jobject outer; |
| 1817 | jobject inner1, inner2; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1818 | ScopedObjectAccess soa(env_); |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1819 | { |
Elliott Hughes | aa836f7 | 2013-08-20 16:57:23 -0700 | [diff] [blame] | 1820 | ASSERT_EQ(JNI_OK, env_->PushLocalFrame(4)); |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1821 | outer = env_->NewLocalRef(original); |
| 1822 | |
| 1823 | { |
Elliott Hughes | aa836f7 | 2013-08-20 16:57:23 -0700 | [diff] [blame] | 1824 | ASSERT_EQ(JNI_OK, env_->PushLocalFrame(4)); |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1825 | inner1 = env_->NewLocalRef(outer); |
| 1826 | inner2 = env_->NewStringUTF("survivor"); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1827 | EXPECT_NE(env_->PopLocalFrame(inner2), nullptr); |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1828 | } |
| 1829 | |
| 1830 | EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(original)); |
| 1831 | EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(outer)); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1832 | { |
| 1833 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 1834 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(inner1)); |
| 1835 | check_jni_abort_catcher.Check("use of deleted local reference"); |
| 1836 | } |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1837 | |
| 1838 | // Our local reference for the survivor is invalid because the survivor |
| 1839 | // gets a new local reference... |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1840 | { |
| 1841 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 1842 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(inner2)); |
| 1843 | check_jni_abort_catcher.Check("use of deleted local reference"); |
| 1844 | } |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1845 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1846 | EXPECT_EQ(env_->PopLocalFrame(nullptr), nullptr); |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1847 | } |
| 1848 | EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(original)); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1849 | CheckJniAbortCatcher check_jni_abort_catcher; |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1850 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(outer)); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1851 | check_jni_abort_catcher.Check("use of deleted local reference"); |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1852 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(inner1)); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1853 | check_jni_abort_catcher.Check("use of deleted local reference"); |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1854 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(inner2)); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1855 | check_jni_abort_catcher.Check("use of deleted local reference"); |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1856 | } |
| 1857 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1858 | TEST_F(JniInternalTest, NewGlobalRef_nullptr) { |
| 1859 | EXPECT_EQ(env_->NewGlobalRef(nullptr), nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1860 | } |
| 1861 | |
| 1862 | TEST_F(JniInternalTest, NewGlobalRef) { |
| 1863 | jstring s = env_->NewStringUTF(""); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1864 | ASSERT_NE(s, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1865 | jobject o = env_->NewGlobalRef(s); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1866 | EXPECT_NE(o, nullptr); |
| 1867 | EXPECT_NE(o, s); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1868 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1869 | EXPECT_EQ(env_->GetObjectRefType(o), JNIGlobalRefType); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1870 | } |
| 1871 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1872 | TEST_F(JniInternalTest, DeleteGlobalRef_nullptr) { |
| 1873 | env_->DeleteGlobalRef(nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1874 | } |
| 1875 | |
| 1876 | TEST_F(JniInternalTest, DeleteGlobalRef) { |
Andreas Gampe | 369810a | 2015-01-14 19:53:31 -0800 | [diff] [blame] | 1877 | // This tests leads to warnings and errors in the log. |
| 1878 | ScopedLogSeverity sls(LogSeverity::FATAL); |
| 1879 | |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1880 | jstring s = env_->NewStringUTF(""); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1881 | ASSERT_NE(s, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1882 | |
| 1883 | jobject o = env_->NewGlobalRef(s); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1884 | ASSERT_NE(o, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1885 | env_->DeleteGlobalRef(o); |
| 1886 | |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 1887 | // Currently, deleting an already-deleted reference is just a CheckJNI warning. |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1888 | { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1889 | bool old_check_jni = vm_->SetCheckJniEnabled(false); |
| 1890 | { |
| 1891 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 1892 | env_->DeleteGlobalRef(o); |
| 1893 | } |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1894 | CheckJniAbortCatcher check_jni_abort_catcher; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1895 | EXPECT_FALSE(vm_->SetCheckJniEnabled(true)); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1896 | env_->DeleteGlobalRef(o); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1897 | std::string expected(StringPrintf("use of deleted global reference %p", o)); |
Elliott Hughes | a9137c6 | 2013-01-09 10:55:21 -0800 | [diff] [blame] | 1898 | check_jni_abort_catcher.Check(expected.c_str()); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1899 | EXPECT_TRUE(vm_->SetCheckJniEnabled(old_check_jni)); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1900 | } |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1901 | |
| 1902 | jobject o1 = env_->NewGlobalRef(s); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1903 | ASSERT_NE(o1, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1904 | jobject o2 = env_->NewGlobalRef(s); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1905 | ASSERT_NE(o2, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1906 | |
| 1907 | env_->DeleteGlobalRef(o1); |
| 1908 | env_->DeleteGlobalRef(o2); |
| 1909 | } |
| 1910 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1911 | TEST_F(JniInternalTest, NewWeakGlobalRef_nullptr) { |
| 1912 | EXPECT_EQ(env_->NewWeakGlobalRef(nullptr), nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1913 | } |
| 1914 | |
| 1915 | TEST_F(JniInternalTest, NewWeakGlobalRef) { |
| 1916 | jstring s = env_->NewStringUTF(""); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1917 | ASSERT_NE(s, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1918 | jobject o = env_->NewWeakGlobalRef(s); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1919 | EXPECT_NE(o, nullptr); |
| 1920 | EXPECT_NE(o, s); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1921 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1922 | EXPECT_EQ(env_->GetObjectRefType(o), JNIWeakGlobalRefType); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1923 | } |
| 1924 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1925 | TEST_F(JniInternalTest, DeleteWeakGlobalRef_nullptr) { |
| 1926 | env_->DeleteWeakGlobalRef(nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1927 | } |
| 1928 | |
| 1929 | TEST_F(JniInternalTest, DeleteWeakGlobalRef) { |
Andreas Gampe | 369810a | 2015-01-14 19:53:31 -0800 | [diff] [blame] | 1930 | // This tests leads to warnings and errors in the log. |
| 1931 | ScopedLogSeverity sls(LogSeverity::FATAL); |
| 1932 | |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1933 | jstring s = env_->NewStringUTF(""); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1934 | ASSERT_NE(s, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1935 | |
| 1936 | jobject o = env_->NewWeakGlobalRef(s); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1937 | ASSERT_NE(o, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1938 | env_->DeleteWeakGlobalRef(o); |
| 1939 | |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 1940 | // Currently, deleting an already-deleted reference is just a CheckJNI warning. |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1941 | { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1942 | bool old_check_jni = vm_->SetCheckJniEnabled(false); |
| 1943 | { |
| 1944 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 1945 | env_->DeleteWeakGlobalRef(o); |
| 1946 | } |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1947 | CheckJniAbortCatcher check_jni_abort_catcher; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1948 | EXPECT_FALSE(vm_->SetCheckJniEnabled(true)); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1949 | env_->DeleteWeakGlobalRef(o); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 1950 | std::string expected(StringPrintf("use of deleted weak global reference %p", o)); |
Elliott Hughes | a9137c6 | 2013-01-09 10:55:21 -0800 | [diff] [blame] | 1951 | check_jni_abort_catcher.Check(expected.c_str()); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1952 | EXPECT_TRUE(vm_->SetCheckJniEnabled(old_check_jni)); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1953 | } |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1954 | |
| 1955 | jobject o1 = env_->NewWeakGlobalRef(s); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1956 | ASSERT_NE(o1, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1957 | jobject o2 = env_->NewWeakGlobalRef(s); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1958 | ASSERT_NE(o2, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1959 | |
| 1960 | env_->DeleteWeakGlobalRef(o1); |
| 1961 | env_->DeleteWeakGlobalRef(o2); |
| 1962 | } |
| 1963 | |
Alexei Zavjalov | 3a1444c | 2014-06-25 16:04:55 +0700 | [diff] [blame] | 1964 | TEST_F(JniInternalTest, ExceptionDescribe) { |
| 1965 | // This checks how ExceptionDescribe handles call without exception. |
| 1966 | env_->ExceptionClear(); |
| 1967 | env_->ExceptionDescribe(); |
| 1968 | } |
| 1969 | |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1970 | TEST_F(JniInternalTest, Throw) { |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1971 | jclass exception_class = env_->FindClass("java/lang/RuntimeException"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1972 | ASSERT_TRUE(exception_class != nullptr); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1973 | jthrowable exception = reinterpret_cast<jthrowable>(env_->AllocObject(exception_class)); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1974 | ASSERT_TRUE(exception != nullptr); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1975 | |
| 1976 | EXPECT_EQ(JNI_OK, env_->Throw(exception)); |
| 1977 | EXPECT_TRUE(env_->ExceptionCheck()); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1978 | jthrowable thrown_exception = env_->ExceptionOccurred(); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1979 | env_->ExceptionClear(); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1980 | EXPECT_TRUE(env_->IsSameObject(exception, thrown_exception)); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1981 | |
| 1982 | // Bad argument. |
| 1983 | bool old_check_jni = vm_->SetCheckJniEnabled(false); |
| 1984 | EXPECT_EQ(JNI_ERR, env_->Throw(nullptr)); |
| 1985 | EXPECT_FALSE(vm_->SetCheckJniEnabled(true)); |
| 1986 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 1987 | EXPECT_EQ(JNI_ERR, env_->Throw(nullptr)); |
| 1988 | check_jni_abort_catcher.Check("Throw received NULL jthrowable"); |
| 1989 | EXPECT_TRUE(vm_->SetCheckJniEnabled(old_check_jni)); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1990 | } |
| 1991 | |
| 1992 | TEST_F(JniInternalTest, ThrowNew) { |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1993 | jclass exception_class = env_->FindClass("java/lang/RuntimeException"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1994 | ASSERT_TRUE(exception_class != nullptr); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1995 | |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 1996 | jthrowable thrown_exception; |
| 1997 | |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1998 | EXPECT_EQ(JNI_OK, env_->ThrowNew(exception_class, "hello world")); |
| 1999 | EXPECT_TRUE(env_->ExceptionCheck()); |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 2000 | thrown_exception = env_->ExceptionOccurred(); |
| 2001 | env_->ExceptionClear(); |
| 2002 | EXPECT_TRUE(env_->IsInstanceOf(thrown_exception, exception_class)); |
| 2003 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 2004 | EXPECT_EQ(JNI_OK, env_->ThrowNew(exception_class, nullptr)); |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 2005 | EXPECT_TRUE(env_->ExceptionCheck()); |
| 2006 | thrown_exception = env_->ExceptionOccurred(); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 2007 | env_->ExceptionClear(); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 2008 | EXPECT_TRUE(env_->IsInstanceOf(thrown_exception, exception_class)); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 2009 | |
| 2010 | // Bad argument. |
| 2011 | bool old_check_jni = vm_->SetCheckJniEnabled(false); |
| 2012 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 2013 | EXPECT_EQ(JNI_ERR, env_->ThrowNew(nullptr, nullptr)); |
| 2014 | check_jni_abort_catcher.Check("c == null"); |
| 2015 | EXPECT_FALSE(vm_->SetCheckJniEnabled(true)); |
| 2016 | EXPECT_EQ(JNI_ERR, env_->ThrowNew(nullptr, nullptr)); |
| 2017 | check_jni_abort_catcher.Check("ThrowNew received NULL jclass"); |
| 2018 | EXPECT_TRUE(vm_->SetCheckJniEnabled(old_check_jni)); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 2019 | } |
| 2020 | |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 2021 | TEST_F(JniInternalTest, NewDirectBuffer_GetDirectBufferAddress_GetDirectBufferCapacity) { |
| 2022 | // Start runtime. |
| 2023 | Thread* self = Thread::Current(); |
| 2024 | self->TransitionFromSuspendedToRunnable(); |
| 2025 | MakeExecutable(nullptr, "java.lang.Class"); |
| 2026 | MakeExecutable(nullptr, "java.lang.Object"); |
| 2027 | MakeExecutable(nullptr, "java.nio.DirectByteBuffer"); |
| 2028 | MakeExecutable(nullptr, "java.nio.MemoryBlock"); |
| 2029 | MakeExecutable(nullptr, "java.nio.MemoryBlock$UnmanagedBlock"); |
| 2030 | MakeExecutable(nullptr, "java.nio.MappedByteBuffer"); |
| 2031 | MakeExecutable(nullptr, "java.nio.ByteBuffer"); |
| 2032 | MakeExecutable(nullptr, "java.nio.Buffer"); |
| 2033 | // TODO: we only load a dex file here as starting the runtime relies upon it. |
| 2034 | const char* class_name = "StaticLeafMethods"; |
| 2035 | LoadDex(class_name); |
| 2036 | bool started = runtime_->Start(); |
| 2037 | ASSERT_TRUE(started); |
| 2038 | |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 2039 | jclass buffer_class = env_->FindClass("java/nio/Buffer"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 2040 | ASSERT_NE(buffer_class, nullptr); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 2041 | |
| 2042 | char bytes[1024]; |
| 2043 | jobject buffer = env_->NewDirectByteBuffer(bytes, sizeof(bytes)); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 2044 | ASSERT_NE(buffer, nullptr); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 2045 | ASSERT_TRUE(env_->IsInstanceOf(buffer, buffer_class)); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 2046 | ASSERT_EQ(env_->GetDirectBufferAddress(buffer), bytes); |
Ian Rogers | 1ee9935 | 2014-05-14 14:38:16 -0700 | [diff] [blame] | 2047 | ASSERT_EQ(env_->GetDirectBufferCapacity(buffer), static_cast<jlong>(sizeof(bytes))); |
Brian Carlstrom | 45d26c8 | 2014-06-24 23:36:28 -0700 | [diff] [blame] | 2048 | |
| 2049 | { |
| 2050 | CheckJniAbortCatcher check_jni_abort_catcher; |
Brian Carlstrom | 85a9336 | 2014-06-25 09:30:52 -0700 | [diff] [blame] | 2051 | env_->NewDirectByteBuffer(bytes, static_cast<jlong>(INT_MAX) + 1); |
Brian Carlstrom | 45d26c8 | 2014-06-24 23:36:28 -0700 | [diff] [blame] | 2052 | check_jni_abort_catcher.Check("in call to NewDirectByteBuffer"); |
| 2053 | } |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 2054 | } |
| 2055 | |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 2056 | TEST_F(JniInternalTest, MonitorEnterExit) { |
Andreas Gampe | 369810a | 2015-01-14 19:53:31 -0800 | [diff] [blame] | 2057 | // This will print some error messages. Suppress. |
| 2058 | ScopedLogSeverity sls(LogSeverity::FATAL); |
| 2059 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 2060 | // Create an object to torture. |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 2061 | jclass object_class = env_->FindClass("java/lang/Object"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 2062 | ASSERT_NE(object_class, nullptr); |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 2063 | jobject object = env_->AllocObject(object_class); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 2064 | ASSERT_NE(object, nullptr); |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 2065 | |
| 2066 | // Expected class of exceptions |
| 2067 | jclass imse_class = env_->FindClass("java/lang/IllegalMonitorStateException"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 2068 | ASSERT_NE(imse_class, nullptr); |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 2069 | |
| 2070 | jthrowable thrown_exception; |
| 2071 | |
| 2072 | // Unlock of unowned monitor |
| 2073 | env_->MonitorExit(object); |
| 2074 | EXPECT_TRUE(env_->ExceptionCheck()); |
| 2075 | thrown_exception = env_->ExceptionOccurred(); |
| 2076 | env_->ExceptionClear(); |
| 2077 | EXPECT_TRUE(env_->IsInstanceOf(thrown_exception, imse_class)); |
| 2078 | |
| 2079 | // Lock of unowned monitor |
| 2080 | env_->MonitorEnter(object); |
| 2081 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 2082 | // Regular unlock |
| 2083 | env_->MonitorExit(object); |
| 2084 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 2085 | |
| 2086 | // Recursively lock a lot |
| 2087 | size_t max_recursive_lock = 1024; |
| 2088 | for (size_t i = 0; i < max_recursive_lock; i++) { |
| 2089 | env_->MonitorEnter(object); |
| 2090 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 2091 | } |
| 2092 | // Recursively unlock a lot |
| 2093 | for (size_t i = 0; i < max_recursive_lock; i++) { |
| 2094 | env_->MonitorExit(object); |
| 2095 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 2096 | } |
| 2097 | |
| 2098 | // Unlock of unowned monitor |
| 2099 | env_->MonitorExit(object); |
| 2100 | EXPECT_TRUE(env_->ExceptionCheck()); |
| 2101 | thrown_exception = env_->ExceptionOccurred(); |
| 2102 | env_->ExceptionClear(); |
| 2103 | EXPECT_TRUE(env_->IsInstanceOf(thrown_exception, imse_class)); |
Elliott Hughes | a92853e | 2012-02-07 16:09:27 -0800 | [diff] [blame] | 2104 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 2105 | // It's an error to call MonitorEnter or MonitorExit on nullptr. |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 2106 | { |
| 2107 | CheckJniAbortCatcher check_jni_abort_catcher; |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 2108 | env_->MonitorEnter(nullptr); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 2109 | check_jni_abort_catcher.Check("in call to MonitorEnter"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 2110 | env_->MonitorExit(nullptr); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 2111 | check_jni_abort_catcher.Check("in call to MonitorExit"); |
| 2112 | } |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 2113 | } |
| 2114 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2115 | } // namespace art |