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; |
| 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[]"); |
| 434 | jbyteArray array2 = env_->NewByteArray(10); |
| 435 | env_->ReleaseBooleanArrayElements(reinterpret_cast<jbooleanArray>(array2), elements, 0); |
| 436 | jni_abort_catcher.Check( |
| 437 | check_jni ? "incompatible array type byte[] expected boolean[]" |
| 438 | : "attempt to release boolean primitive array elements with an object of type byte[]"); |
| 439 | jobject object = env_->NewStringUTF("Test String"); |
| 440 | env_->ReleaseBooleanArrayElements(reinterpret_cast<jbooleanArray>(object), elements, 0); |
| 441 | jni_abort_catcher.Check( |
| 442 | check_jni ? "jarray argument has non-array type: java.lang.String" |
| 443 | : "attempt to release boolean primitive array elements with an object of type " |
| 444 | "java.lang.String"); |
| 445 | |
| 446 | EXPECT_EQ(check_jni, vm_->SetCheckJniEnabled(old_check_jni)); |
| 447 | } |
| 448 | |
| 449 | void GetReleasePrimitiveArrayCriticalOfWrongType(bool check_jni) { |
| 450 | bool old_check_jni = vm_->SetCheckJniEnabled(check_jni); |
| 451 | CheckJniAbortCatcher jni_abort_catcher; |
| 452 | |
| 453 | jobject object = env_->NewStringUTF("Test String"); |
| 454 | jboolean is_copy; |
| 455 | void* elements = env_->GetPrimitiveArrayCritical(reinterpret_cast<jarray>(object), &is_copy); |
| 456 | jni_abort_catcher.Check(check_jni ? "jarray argument has non-array type: java.lang.String" |
| 457 | : "expected primitive array, given java.lang.String"); |
| 458 | env_->ReleasePrimitiveArrayCritical(reinterpret_cast<jarray>(object), elements, 0); |
| 459 | jni_abort_catcher.Check(check_jni ? "jarray argument has non-array type: java.lang.String" |
| 460 | : "expected primitive array, given java.lang.String"); |
| 461 | |
| 462 | EXPECT_EQ(check_jni, vm_->SetCheckJniEnabled(old_check_jni)); |
| 463 | } |
| 464 | |
| 465 | void GetPrimitiveArrayRegionElementsOfWrongType(bool check_jni) { |
| 466 | bool old_check_jni = vm_->SetCheckJniEnabled(check_jni); |
| 467 | CheckJniAbortCatcher jni_abort_catcher; |
| 468 | constexpr size_t kLength = 10; |
| 469 | jbooleanArray array = env_->NewBooleanArray(kLength); |
| 470 | ASSERT_TRUE(array != nullptr); |
| 471 | jboolean elements[kLength]; |
| 472 | env_->GetByteArrayRegion(reinterpret_cast<jbyteArray>(array), 0, kLength, |
| 473 | reinterpret_cast<jbyte*>(elements)); |
| 474 | jni_abort_catcher.Check( |
| 475 | check_jni ? "incompatible array type boolean[] expected byte[]" |
| 476 | : "attempt to get region of byte primitive array elements with an object of type boolean[]"); |
| 477 | env_->GetShortArrayRegion(reinterpret_cast<jshortArray>(array), 0, kLength, |
| 478 | reinterpret_cast<jshort*>(elements)); |
| 479 | jni_abort_catcher.Check( |
| 480 | check_jni ? "incompatible array type boolean[] expected short[]" |
| 481 | : "attempt to get region of short primitive array elements with an object of type boolean[]"); |
| 482 | env_->GetCharArrayRegion(reinterpret_cast<jcharArray>(array), 0, kLength, |
| 483 | reinterpret_cast<jchar*>(elements)); |
| 484 | jni_abort_catcher.Check( |
| 485 | check_jni ? "incompatible array type boolean[] expected char[]" |
| 486 | : "attempt to get region of char primitive array elements with an object of type boolean[]"); |
| 487 | env_->GetIntArrayRegion(reinterpret_cast<jintArray>(array), 0, kLength, |
| 488 | reinterpret_cast<jint*>(elements)); |
| 489 | jni_abort_catcher.Check( |
| 490 | check_jni ? "incompatible array type boolean[] expected int[]" |
| 491 | : "attempt to get region of int primitive array elements with an object of type boolean[]"); |
| 492 | env_->GetLongArrayRegion(reinterpret_cast<jlongArray>(array), 0, kLength, |
| 493 | reinterpret_cast<jlong*>(elements)); |
| 494 | jni_abort_catcher.Check( |
| 495 | check_jni ? "incompatible array type boolean[] expected long[]" |
| 496 | : "attempt to get region of long primitive array elements with an object of type boolean[]"); |
| 497 | env_->GetFloatArrayRegion(reinterpret_cast<jfloatArray>(array), 0, kLength, |
| 498 | reinterpret_cast<jfloat*>(elements)); |
| 499 | jni_abort_catcher.Check( |
| 500 | check_jni ? "incompatible array type boolean[] expected float[]" |
| 501 | : "attempt to get region of float primitive array elements with an object of type boolean[]"); |
| 502 | env_->GetDoubleArrayRegion(reinterpret_cast<jdoubleArray>(array), 0, kLength, |
| 503 | reinterpret_cast<jdouble*>(elements)); |
| 504 | jni_abort_catcher.Check( |
| 505 | check_jni ? "incompatible array type boolean[] expected double[]" |
| 506 | : "attempt to get region of double primitive array elements with an object of type boolean[]"); |
| 507 | jbyteArray array2 = env_->NewByteArray(10); |
| 508 | env_->GetBooleanArrayRegion(reinterpret_cast<jbooleanArray>(array2), 0, kLength, |
| 509 | reinterpret_cast<jboolean*>(elements)); |
| 510 | jni_abort_catcher.Check( |
| 511 | check_jni ? "incompatible array type byte[] expected boolean[]" |
| 512 | : "attempt to get region of boolean primitive array elements with an object of type byte[]"); |
| 513 | jobject object = env_->NewStringUTF("Test String"); |
| 514 | env_->GetBooleanArrayRegion(reinterpret_cast<jbooleanArray>(object), 0, kLength, |
| 515 | reinterpret_cast<jboolean*>(elements)); |
| 516 | jni_abort_catcher.Check(check_jni ? "jarray argument has non-array type: java.lang.String" |
| 517 | : "attempt to get region of boolean primitive array elements with an object of type " |
| 518 | "java.lang.String"); |
| 519 | |
| 520 | EXPECT_EQ(check_jni, vm_->SetCheckJniEnabled(old_check_jni)); |
| 521 | } |
| 522 | |
| 523 | void SetPrimitiveArrayRegionElementsOfWrongType(bool check_jni) { |
| 524 | bool old_check_jni = vm_->SetCheckJniEnabled(check_jni); |
| 525 | CheckJniAbortCatcher jni_abort_catcher; |
| 526 | constexpr size_t kLength = 10; |
| 527 | jbooleanArray array = env_->NewBooleanArray(kLength); |
| 528 | ASSERT_TRUE(array != nullptr); |
| 529 | jboolean elements[kLength]; |
| 530 | env_->SetByteArrayRegion(reinterpret_cast<jbyteArray>(array), 0, kLength, |
| 531 | reinterpret_cast<jbyte*>(elements)); |
| 532 | jni_abort_catcher.Check( |
| 533 | check_jni ? "incompatible array type boolean[] expected byte[]" |
| 534 | : "attempt to set region of byte primitive array elements with an object of type boolean[]"); |
| 535 | env_->SetShortArrayRegion(reinterpret_cast<jshortArray>(array), 0, kLength, |
| 536 | reinterpret_cast<jshort*>(elements)); |
| 537 | jni_abort_catcher.Check( |
| 538 | check_jni ? "incompatible array type boolean[] expected short[]" |
| 539 | : "attempt to set region of short primitive array elements with an object of type boolean[]"); |
| 540 | env_->SetCharArrayRegion(reinterpret_cast<jcharArray>(array), 0, kLength, |
| 541 | reinterpret_cast<jchar*>(elements)); |
| 542 | jni_abort_catcher.Check( |
| 543 | check_jni ? "incompatible array type boolean[] expected char[]" |
| 544 | : "attempt to set region of char primitive array elements with an object of type boolean[]"); |
| 545 | env_->SetIntArrayRegion(reinterpret_cast<jintArray>(array), 0, kLength, |
| 546 | reinterpret_cast<jint*>(elements)); |
| 547 | jni_abort_catcher.Check( |
| 548 | check_jni ? "incompatible array type boolean[] expected int[]" |
| 549 | : "attempt to set region of int primitive array elements with an object of type boolean[]"); |
| 550 | env_->SetLongArrayRegion(reinterpret_cast<jlongArray>(array), 0, kLength, |
| 551 | reinterpret_cast<jlong*>(elements)); |
| 552 | jni_abort_catcher.Check( |
| 553 | check_jni ? "incompatible array type boolean[] expected long[]" |
| 554 | : "attempt to set region of long primitive array elements with an object of type boolean[]"); |
| 555 | env_->SetFloatArrayRegion(reinterpret_cast<jfloatArray>(array), 0, kLength, |
| 556 | reinterpret_cast<jfloat*>(elements)); |
| 557 | jni_abort_catcher.Check( |
| 558 | check_jni ? "incompatible array type boolean[] expected float[]" |
| 559 | : "attempt to set region of float primitive array elements with an object of type boolean[]"); |
| 560 | env_->SetDoubleArrayRegion(reinterpret_cast<jdoubleArray>(array), 0, kLength, |
| 561 | reinterpret_cast<jdouble*>(elements)); |
| 562 | jni_abort_catcher.Check( |
| 563 | check_jni ? "incompatible array type boolean[] expected double[]" |
| 564 | : "attempt to set region of double primitive array elements with an object of type boolean[]"); |
| 565 | jbyteArray array2 = env_->NewByteArray(10); |
| 566 | env_->SetBooleanArrayRegion(reinterpret_cast<jbooleanArray>(array2), 0, kLength, |
| 567 | reinterpret_cast<jboolean*>(elements)); |
| 568 | jni_abort_catcher.Check( |
| 569 | check_jni ? "incompatible array type byte[] expected boolean[]" |
| 570 | : "attempt to set region of boolean primitive array elements with an object of type byte[]"); |
| 571 | jobject object = env_->NewStringUTF("Test String"); |
| 572 | env_->SetBooleanArrayRegion(reinterpret_cast<jbooleanArray>(object), 0, kLength, |
| 573 | reinterpret_cast<jboolean*>(elements)); |
| 574 | jni_abort_catcher.Check(check_jni ? "jarray argument has non-array type: java.lang.String" |
| 575 | : "attempt to set region of boolean primitive array elements with an object of type " |
| 576 | "java.lang.String"); |
| 577 | EXPECT_EQ(check_jni, vm_->SetCheckJniEnabled(old_check_jni)); |
| 578 | } |
| 579 | |
| 580 | void NewObjectArrayBadArguments(bool check_jni) { |
| 581 | bool old_check_jni = vm_->SetCheckJniEnabled(check_jni); |
| 582 | CheckJniAbortCatcher jni_abort_catcher; |
| 583 | |
| 584 | jclass element_class = env_->FindClass("java/lang/String"); |
| 585 | ASSERT_NE(element_class, nullptr); |
| 586 | |
| 587 | env_->NewObjectArray(-1, element_class, nullptr); |
| 588 | jni_abort_catcher.Check(check_jni ? "negative jsize: -1" : "negative array length: -1"); |
| 589 | |
| 590 | env_->NewObjectArray(std::numeric_limits<jint>::min(), element_class, nullptr); |
| 591 | jni_abort_catcher.Check(check_jni ? "negative jsize: -2147483648" |
| 592 | : "negative array length: -2147483648"); |
| 593 | |
| 594 | EXPECT_EQ(check_jni, vm_->SetCheckJniEnabled(old_check_jni)); |
| 595 | } |
| 596 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 597 | JavaVMExt* vm_; |
Brian Carlstrom | 4d57143 | 2012-05-16 00:21:41 -0700 | [diff] [blame] | 598 | JNIEnv* env_; |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 599 | jclass aioobe_; |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 600 | jclass ase_; |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 601 | jclass sioobe_; |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 602 | }; |
| 603 | |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 604 | TEST_F(JniInternalTest, AllocObject) { |
| 605 | jclass c = env_->FindClass("java/lang/String"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 606 | ASSERT_NE(c, nullptr); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 607 | jobject o = env_->AllocObject(c); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 608 | ASSERT_NE(o, nullptr); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 609 | |
| 610 | // We have an instance of the class we asked for... |
| 611 | ASSERT_TRUE(env_->IsInstanceOf(o, c)); |
| 612 | // ...whose fields haven't been initialized because |
| 613 | // we didn't call a constructor. |
| 614 | ASSERT_EQ(0, env_->GetIntField(o, env_->GetFieldID(c, "count", "I"))); |
| 615 | ASSERT_EQ(0, env_->GetIntField(o, env_->GetFieldID(c, "offset", "I"))); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 616 | ASSERT_TRUE(env_->GetObjectField(o, env_->GetFieldID(c, "value", "[C")) == nullptr); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 617 | } |
| 618 | |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 619 | TEST_F(JniInternalTest, GetVersion) { |
| 620 | ASSERT_EQ(JNI_VERSION_1_6, env_->GetVersion()); |
| 621 | } |
| 622 | |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 623 | TEST_F(JniInternalTest, FindClass) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 624 | FindClassTest(false); |
| 625 | FindClassTest(true); |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 626 | } |
| 627 | |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 628 | TEST_F(JniInternalTest, GetFieldID) { |
| 629 | jclass jlnsfe = env_->FindClass("java/lang/NoSuchFieldError"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 630 | ASSERT_NE(jlnsfe, nullptr); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 631 | jclass c = env_->FindClass("java/lang/String"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 632 | ASSERT_NE(c, nullptr); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 633 | |
| 634 | // Wrong type. |
| 635 | jfieldID fid = env_->GetFieldID(c, "count", "J"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 636 | EXPECT_EQ(nullptr, fid); |
| 637 | ExpectException(jlnsfe); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 638 | |
Ian Rogers | b17d08b | 2011-09-02 16:16:49 -0700 | [diff] [blame] | 639 | // Wrong type where type doesn't exist. |
| 640 | fid = env_->GetFieldID(c, "count", "Lrod/jane/freddy;"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 641 | EXPECT_EQ(nullptr, fid); |
| 642 | ExpectException(jlnsfe); |
Ian Rogers | b17d08b | 2011-09-02 16:16:49 -0700 | [diff] [blame] | 643 | |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 644 | // Wrong name. |
| 645 | fid = env_->GetFieldID(c, "Count", "I"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 646 | EXPECT_EQ(nullptr, fid); |
| 647 | ExpectException(jlnsfe); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 648 | |
| 649 | // Good declared field lookup. |
| 650 | fid = env_->GetFieldID(c, "count", "I"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 651 | EXPECT_NE(nullptr, fid); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 652 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 653 | |
| 654 | // Good superclass field lookup. |
| 655 | c = env_->FindClass("java/lang/StringBuilder"); |
| 656 | fid = env_->GetFieldID(c, "count", "I"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 657 | EXPECT_NE(nullptr, fid); |
| 658 | EXPECT_NE(fid, nullptr); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 659 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 660 | |
| 661 | // Not instance. |
| 662 | fid = env_->GetFieldID(c, "CASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 663 | EXPECT_EQ(nullptr, fid); |
| 664 | ExpectException(jlnsfe); |
| 665 | |
| 666 | // Bad arguments. |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 667 | GetFieldIdBadArgumentTest(false); |
| 668 | GetFieldIdBadArgumentTest(true); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | TEST_F(JniInternalTest, GetStaticFieldID) { |
| 672 | jclass jlnsfe = env_->FindClass("java/lang/NoSuchFieldError"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 673 | ASSERT_NE(jlnsfe, nullptr); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 674 | jclass c = env_->FindClass("java/lang/String"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 675 | ASSERT_NE(c, nullptr); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 676 | |
| 677 | // Wrong type. |
| 678 | jfieldID fid = env_->GetStaticFieldID(c, "CASE_INSENSITIVE_ORDER", "J"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 679 | EXPECT_EQ(nullptr, fid); |
| 680 | ExpectException(jlnsfe); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 681 | |
Ian Rogers | b17d08b | 2011-09-02 16:16:49 -0700 | [diff] [blame] | 682 | // Wrong type where type doesn't exist. |
| 683 | fid = env_->GetStaticFieldID(c, "CASE_INSENSITIVE_ORDER", "Lrod/jane/freddy;"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 684 | EXPECT_EQ(nullptr, fid); |
| 685 | ExpectException(jlnsfe); |
Ian Rogers | b17d08b | 2011-09-02 16:16:49 -0700 | [diff] [blame] | 686 | |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 687 | // Wrong name. |
| 688 | fid = env_->GetStaticFieldID(c, "cASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 689 | EXPECT_EQ(nullptr, fid); |
| 690 | ExpectException(jlnsfe); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 691 | |
| 692 | // Good declared field lookup. |
| 693 | fid = env_->GetStaticFieldID(c, "CASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 694 | EXPECT_NE(nullptr, fid); |
| 695 | EXPECT_NE(fid, nullptr); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 696 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 697 | |
| 698 | // Not static. |
| 699 | fid = env_->GetStaticFieldID(c, "count", "I"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 700 | EXPECT_EQ(nullptr, fid); |
| 701 | ExpectException(jlnsfe); |
| 702 | |
| 703 | // Bad arguments. |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 704 | GetStaticFieldIdBadArgumentTest(false); |
| 705 | GetStaticFieldIdBadArgumentTest(true); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 706 | } |
| 707 | |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 708 | TEST_F(JniInternalTest, GetMethodID) { |
| 709 | jclass jlobject = env_->FindClass("java/lang/Object"); |
| 710 | jclass jlstring = env_->FindClass("java/lang/String"); |
| 711 | jclass jlnsme = env_->FindClass("java/lang/NoSuchMethodError"); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 712 | jclass jncrbc = env_->FindClass("java/nio/channels/ReadableByteChannel"); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 713 | |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 714 | // Sanity check that no exceptions are pending. |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 715 | ASSERT_FALSE(env_->ExceptionCheck()); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 716 | |
| 717 | // Check that java.lang.Object.foo() doesn't exist and NoSuchMethodError is |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 718 | // a pending exception. |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 719 | jmethodID method = env_->GetMethodID(jlobject, "foo", "()V"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 720 | EXPECT_EQ(nullptr, method); |
| 721 | ExpectException(jlnsme); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 722 | |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 723 | // Check that java.lang.Object.equals() does exist. |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 724 | method = env_->GetMethodID(jlobject, "equals", "(Ljava/lang/Object;)Z"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 725 | EXPECT_NE(nullptr, method); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 726 | EXPECT_FALSE(env_->ExceptionCheck()); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 727 | |
| 728 | // Check that GetMethodID for java.lang.String.valueOf(int) fails as the |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 729 | // method is static. |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 730 | method = env_->GetMethodID(jlstring, "valueOf", "(I)Ljava/lang/String;"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 731 | EXPECT_EQ(nullptr, method); |
| 732 | ExpectException(jlnsme); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 733 | |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 734 | // Check that GetMethodID for java.lang.NoSuchMethodError.<init>(String) finds the constructor. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 735 | method = env_->GetMethodID(jlnsme, "<init>", "(Ljava/lang/String;)V"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 736 | EXPECT_NE(nullptr, method); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 737 | EXPECT_FALSE(env_->ExceptionCheck()); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 738 | |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 739 | // Check that GetMethodID can find a interface method inherited from another interface. |
| 740 | method = env_->GetMethodID(jncrbc, "close", "()V"); |
| 741 | EXPECT_NE(nullptr, method); |
| 742 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 743 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 744 | // Bad arguments. |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 745 | GetMethodIdBadArgumentTest(false); |
| 746 | GetMethodIdBadArgumentTest(true); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 747 | } |
| 748 | |
Mathieu Chartier | a1e78fa | 2014-08-20 12:09:38 -0700 | [diff] [blame] | 749 | TEST_F(JniInternalTest, CallVoidMethodNullReceiver) { |
| 750 | jclass jlobject = env_->FindClass("java/lang/Object"); |
| 751 | jmethodID method; |
| 752 | |
| 753 | // Check that GetMethodID for java.lang.NoSuchMethodError.<init>(String) finds the constructor. |
| 754 | method = env_->GetMethodID(jlobject, "<init>", "()V"); |
| 755 | EXPECT_NE(nullptr, method); |
| 756 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 757 | |
| 758 | // Null object to CallVoidMethod. |
| 759 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 760 | env_->CallVoidMethod(nullptr, method); |
| 761 | check_jni_abort_catcher.Check("null"); |
| 762 | } |
| 763 | |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 764 | TEST_F(JniInternalTest, GetStaticMethodID) { |
| 765 | jclass jlobject = env_->FindClass("java/lang/Object"); |
| 766 | jclass jlnsme = env_->FindClass("java/lang/NoSuchMethodError"); |
| 767 | |
| 768 | // Sanity check that no exceptions are pending |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 769 | ASSERT_FALSE(env_->ExceptionCheck()); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 770 | |
| 771 | // Check that java.lang.Object.foo() doesn't exist and NoSuchMethodError is |
| 772 | // a pending exception |
| 773 | jmethodID method = env_->GetStaticMethodID(jlobject, "foo", "()V"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 774 | EXPECT_EQ(nullptr, method); |
| 775 | ExpectException(jlnsme); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 776 | |
| 777 | // Check that GetStaticMethodID for java.lang.Object.equals(Object) fails as |
| 778 | // the method is not static |
| 779 | method = env_->GetStaticMethodID(jlobject, "equals", "(Ljava/lang/Object;)Z"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 780 | EXPECT_EQ(nullptr, method); |
| 781 | ExpectException(jlnsme); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 782 | |
| 783 | // Check that java.lang.String.valueOf(int) does exist |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 784 | jclass jlstring = env_->FindClass("java/lang/String"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 785 | method = env_->GetStaticMethodID(jlstring, "valueOf", "(I)Ljava/lang/String;"); |
| 786 | EXPECT_NE(nullptr, method); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 787 | EXPECT_FALSE(env_->ExceptionCheck()); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 788 | |
| 789 | // Bad arguments. |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 790 | GetStaticMethodIdBadArgumentTest(false); |
| 791 | GetStaticMethodIdBadArgumentTest(true); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 792 | } |
| 793 | |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 794 | TEST_F(JniInternalTest, FromReflectedField_ToReflectedField) { |
| 795 | jclass jlrField = env_->FindClass("java/lang/reflect/Field"); |
| 796 | jclass c = env_->FindClass("java/lang/String"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 797 | ASSERT_NE(c, nullptr); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 798 | jfieldID fid = env_->GetFieldID(c, "count", "I"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 799 | ASSERT_NE(fid, nullptr); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 800 | // Turn the fid into a java.lang.reflect.Field... |
| 801 | jobject field = env_->ToReflectedField(c, fid, JNI_FALSE); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 802 | ASSERT_NE(c, nullptr); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 803 | ASSERT_TRUE(env_->IsInstanceOf(field, jlrField)); |
| 804 | // ...and back again. |
| 805 | jfieldID fid2 = env_->FromReflectedField(field); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 806 | ASSERT_NE(fid2, nullptr); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 807 | // Make sure we can actually use it. |
| 808 | jstring s = env_->NewStringUTF("poop"); |
| 809 | ASSERT_EQ(4, env_->GetIntField(s, fid2)); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 810 | |
| 811 | // Bad arguments. |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 812 | GetFromReflectedField_ToReflectedFieldBadArgumentTest(false); |
| 813 | GetFromReflectedField_ToReflectedFieldBadArgumentTest(true); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | TEST_F(JniInternalTest, FromReflectedMethod_ToReflectedMethod) { |
| 817 | jclass jlrMethod = env_->FindClass("java/lang/reflect/Method"); |
Sebastien Hertz | d333376 | 2014-06-26 14:45:07 +0200 | [diff] [blame] | 818 | ASSERT_NE(jlrMethod, nullptr); |
| 819 | jclass jlrConstructor = env_->FindClass("java/lang/reflect/Constructor"); |
| 820 | ASSERT_NE(jlrConstructor, nullptr); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 821 | jclass c = env_->FindClass("java/lang/String"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 822 | ASSERT_NE(c, nullptr); |
Sebastien Hertz | d333376 | 2014-06-26 14:45:07 +0200 | [diff] [blame] | 823 | |
| 824 | jmethodID mid = env_->GetMethodID(c, "<init>", "()V"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 825 | ASSERT_NE(mid, nullptr); |
Sebastien Hertz | d333376 | 2014-06-26 14:45:07 +0200 | [diff] [blame] | 826 | // Turn the mid into a java.lang.reflect.Constructor... |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 827 | jobject method = env_->ToReflectedMethod(c, mid, JNI_FALSE); |
Sebastien Hertz | d333376 | 2014-06-26 14:45:07 +0200 | [diff] [blame] | 828 | ASSERT_NE(method, nullptr); |
| 829 | ASSERT_TRUE(env_->IsInstanceOf(method, jlrConstructor)); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 830 | // ...and back again. |
| 831 | jmethodID mid2 = env_->FromReflectedMethod(method); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 832 | ASSERT_NE(mid2, nullptr); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 833 | // Make sure we can actually use it. |
Sebastien Hertz | d333376 | 2014-06-26 14:45:07 +0200 | [diff] [blame] | 834 | jstring s = reinterpret_cast<jstring>(env_->AllocObject(c)); |
| 835 | ASSERT_NE(s, nullptr); |
| 836 | env_->CallVoidMethod(s, mid2); |
| 837 | ASSERT_EQ(JNI_FALSE, env_->ExceptionCheck()); |
| 838 | |
| 839 | mid = env_->GetMethodID(c, "length", "()I"); |
| 840 | ASSERT_NE(mid, nullptr); |
| 841 | // Turn the mid into a java.lang.reflect.Method... |
| 842 | method = env_->ToReflectedMethod(c, mid, JNI_FALSE); |
| 843 | ASSERT_NE(method, nullptr); |
| 844 | ASSERT_TRUE(env_->IsInstanceOf(method, jlrMethod)); |
| 845 | // ...and back again. |
| 846 | mid2 = env_->FromReflectedMethod(method); |
| 847 | ASSERT_NE(mid2, nullptr); |
| 848 | // Make sure we can actually use it. |
| 849 | s = env_->NewStringUTF("poop"); |
| 850 | ASSERT_NE(s, nullptr); |
Ian Rogers | 5d27faf | 2014-05-02 17:17:18 -0700 | [diff] [blame] | 851 | ASSERT_EQ(4, env_->CallIntMethod(s, mid2)); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 852 | |
| 853 | // Bad arguments. |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 854 | GetFromReflectedMethod_ToReflectedMethodBadArgumentTest(false); |
| 855 | GetFromReflectedMethod_ToReflectedMethodBadArgumentTest(true); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 856 | } |
| 857 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 858 | static void BogusMethod() { |
| 859 | // You can't pass nullptr function pointers to RegisterNatives. |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 860 | } |
| 861 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 862 | TEST_F(JniInternalTest, RegisterAndUnregisterNatives) { |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 863 | jclass jlobject = env_->FindClass("java/lang/Object"); |
| 864 | jclass jlnsme = env_->FindClass("java/lang/NoSuchMethodError"); |
Sebastien Hertz | fa65e84 | 2014-07-03 09:39:53 +0200 | [diff] [blame] | 865 | void* native_function = reinterpret_cast<void*>(BogusMethod); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 866 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 867 | // Sanity check that no exceptions are pending. |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 868 | ASSERT_FALSE(env_->ExceptionCheck()); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 869 | |
Sebastien Hertz | fa65e84 | 2014-07-03 09:39:53 +0200 | [diff] [blame] | 870 | // Check that registering method without name causes a NoSuchMethodError. |
| 871 | { |
| 872 | JNINativeMethod methods[] = { { nullptr, "()V", native_function } }; |
| 873 | EXPECT_EQ(env_->RegisterNatives(jlobject, methods, 1), JNI_ERR); |
| 874 | } |
| 875 | ExpectException(jlnsme); |
| 876 | |
| 877 | // Check that registering method without signature causes a NoSuchMethodError. |
| 878 | { |
| 879 | JNINativeMethod methods[] = { { "notify", nullptr, native_function } }; |
| 880 | EXPECT_EQ(env_->RegisterNatives(jlobject, methods, 1), JNI_ERR); |
| 881 | } |
| 882 | ExpectException(jlnsme); |
| 883 | |
| 884 | // Check that registering method without function causes a NoSuchMethodError. |
| 885 | { |
| 886 | JNINativeMethod methods[] = { { "notify", "()V", nullptr } }; |
| 887 | EXPECT_EQ(env_->RegisterNatives(jlobject, methods, 1), JNI_ERR); |
| 888 | } |
| 889 | ExpectException(jlnsme); |
| 890 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 891 | // Check that registering to a non-existent java.lang.Object.foo() causes a NoSuchMethodError. |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 892 | { |
Sebastien Hertz | fa65e84 | 2014-07-03 09:39:53 +0200 | [diff] [blame] | 893 | JNINativeMethod methods[] = { { "foo", "()V", native_function } }; |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 894 | EXPECT_EQ(env_->RegisterNatives(jlobject, methods, 1), JNI_ERR); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 895 | } |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 896 | ExpectException(jlnsme); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 897 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 898 | // Check that registering non-native methods causes a NoSuchMethodError. |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 899 | { |
Sebastien Hertz | fa65e84 | 2014-07-03 09:39:53 +0200 | [diff] [blame] | 900 | JNINativeMethod methods[] = { { "equals", "(Ljava/lang/Object;)Z", native_function } }; |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 901 | EXPECT_EQ(env_->RegisterNatives(jlobject, methods, 1), JNI_ERR); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 902 | } |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 903 | ExpectException(jlnsme); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 904 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 905 | // Check that registering native methods is successful. |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 906 | { |
Sebastien Hertz | fa65e84 | 2014-07-03 09:39:53 +0200 | [diff] [blame] | 907 | JNINativeMethod methods[] = { { "notify", "()V", native_function } }; |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 908 | EXPECT_EQ(env_->RegisterNatives(jlobject, methods, 1), JNI_OK); |
| 909 | } |
| 910 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 911 | EXPECT_EQ(env_->UnregisterNatives(jlobject), JNI_OK); |
| 912 | |
| 913 | // Check that registering no methods isn't a failure. |
| 914 | { |
| 915 | JNINativeMethod methods[] = { }; |
| 916 | EXPECT_EQ(env_->RegisterNatives(jlobject, methods, 0), JNI_OK); |
| 917 | } |
| 918 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 919 | EXPECT_EQ(env_->UnregisterNatives(jlobject), JNI_OK); |
| 920 | |
| 921 | // Check that registering a -ve number of methods is a failure. |
| 922 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 923 | for (int i = -10; i < 0; ++i) { |
| 924 | JNINativeMethod methods[] = { }; |
| 925 | EXPECT_EQ(env_->RegisterNatives(jlobject, methods, i), JNI_ERR); |
| 926 | check_jni_abort_catcher.Check("negative method count: "); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 927 | } |
| 928 | EXPECT_FALSE(env_->ExceptionCheck()); |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 929 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 930 | // Unregistering a class with no natives is a warning. |
| 931 | EXPECT_EQ(env_->UnregisterNatives(jlnsme), JNI_OK); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 932 | |
| 933 | RegisterAndUnregisterNativesBadArguments(false, &check_jni_abort_catcher); |
| 934 | RegisterAndUnregisterNativesBadArguments(true, &check_jni_abort_catcher); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 935 | } |
| 936 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 937 | #define EXPECT_PRIMITIVE_ARRAY(new_fn, \ |
| 938 | get_region_fn, \ |
| 939 | set_region_fn, \ |
| 940 | get_elements_fn, \ |
| 941 | release_elements_fn, \ |
| 942 | scalar_type, \ |
| 943 | expected_class_descriptor) \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 944 | jsize size = 4; \ |
| 945 | \ |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 946 | { \ |
| 947 | CheckJniAbortCatcher jni_abort_catcher; \ |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 948 | down_cast<JNIEnvExt*>(env_)->SetCheckJniEnabled(false); \ |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 949 | /* 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] | 950 | EXPECT_EQ(env_->new_fn(-1), nullptr); \ |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 951 | jni_abort_catcher.Check("negative array length: -1"); \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 952 | EXPECT_EQ(env_->new_fn(std::numeric_limits<jint>::min()), nullptr); \ |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 953 | jni_abort_catcher.Check("negative array length: -2147483648"); \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 954 | /* Pass the array as null. */ \ |
| 955 | EXPECT_EQ(0, env_->GetArrayLength(nullptr)); \ |
| 956 | jni_abort_catcher.Check("java_array == null"); \ |
| 957 | env_->get_region_fn(nullptr, 0, 0, nullptr); \ |
| 958 | jni_abort_catcher.Check("java_array == null"); \ |
| 959 | env_->set_region_fn(nullptr, 0, 0, nullptr); \ |
| 960 | jni_abort_catcher.Check("java_array == null"); \ |
| 961 | env_->get_elements_fn(nullptr, nullptr); \ |
| 962 | jni_abort_catcher.Check("java_array == null"); \ |
| 963 | env_->release_elements_fn(nullptr, nullptr, 0); \ |
| 964 | jni_abort_catcher.Check("java_array == null"); \ |
| 965 | /* Pass the elements for region as null. */ \ |
| 966 | scalar_type ## Array a = env_->new_fn(size); \ |
| 967 | env_->get_region_fn(a, 0, size, nullptr); \ |
| 968 | jni_abort_catcher.Check("buf == null"); \ |
| 969 | env_->set_region_fn(a, 0, size, nullptr); \ |
| 970 | jni_abort_catcher.Check("buf == null"); \ |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 971 | down_cast<JNIEnvExt*>(env_)->SetCheckJniEnabled(true); \ |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 972 | } \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 973 | /* Allocate an array and check it has the right type and length. */ \ |
| 974 | scalar_type ## Array a = env_->new_fn(size); \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 975 | EXPECT_NE(a, nullptr); \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 976 | EXPECT_TRUE(env_->IsInstanceOf(a, env_->FindClass(expected_class_descriptor))); \ |
| 977 | EXPECT_EQ(size, env_->GetArrayLength(a)); \ |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 978 | \ |
| 979 | /* GetPrimitiveArrayRegion/SetPrimitiveArrayRegion */ \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 980 | /* AIOOBE for negative start offset. */ \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 981 | env_->get_region_fn(a, -1, 1, nullptr); \ |
| 982 | ExpectException(aioobe_); \ |
| 983 | env_->set_region_fn(a, -1, 1, nullptr); \ |
| 984 | ExpectException(aioobe_); \ |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 985 | \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 986 | /* AIOOBE for negative length. */ \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 987 | env_->get_region_fn(a, 0, -1, nullptr); \ |
| 988 | ExpectException(aioobe_); \ |
| 989 | env_->set_region_fn(a, 0, -1, nullptr); \ |
| 990 | ExpectException(aioobe_); \ |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 991 | \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 992 | /* AIOOBE for buffer overrun. */ \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 993 | env_->get_region_fn(a, size - 1, size, nullptr); \ |
| 994 | ExpectException(aioobe_); \ |
| 995 | env_->set_region_fn(a, size - 1, size, nullptr); \ |
| 996 | ExpectException(aioobe_); \ |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 997 | \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 998 | /* It's okay for the buffer to be nullptr as long as the length is 0. */ \ |
| 999 | env_->get_region_fn(a, 2, 0, nullptr); \ |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 1000 | /* Even if the offset is invalid... */ \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1001 | env_->get_region_fn(a, 123, 0, nullptr); \ |
| 1002 | ExpectException(aioobe_); \ |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 1003 | \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1004 | /* It's okay for the buffer to be nullptr as long as the length is 0. */ \ |
| 1005 | env_->set_region_fn(a, 2, 0, nullptr); \ |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 1006 | /* Even if the offset is invalid... */ \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1007 | env_->set_region_fn(a, 123, 0, nullptr); \ |
| 1008 | ExpectException(aioobe_); \ |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 1009 | \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1010 | /* Prepare a couple of buffers. */ \ |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1011 | std::unique_ptr<scalar_type[]> src_buf(new scalar_type[size]); \ |
| 1012 | std::unique_ptr<scalar_type[]> dst_buf(new scalar_type[size]); \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1013 | for (jsize i = 0; i < size; ++i) { src_buf[i] = scalar_type(i); } \ |
| 1014 | 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] | 1015 | \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1016 | /* Copy all of src_buf onto the heap. */ \ |
Elliott Hughes | ee0fa76 | 2012-03-26 17:12:41 -0700 | [diff] [blame] | 1017 | env_->set_region_fn(a, 0, size, &src_buf[0]); \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1018 | /* Copy back only part. */ \ |
| 1019 | env_->get_region_fn(a, 1, size - 2, &dst_buf[1]); \ |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1020 | EXPECT_NE(memcmp(&src_buf[0], &dst_buf[0], size * sizeof(scalar_type)), 0) \ |
| 1021 | << "short copy equal"; \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1022 | /* Copy the missing pieces. */ \ |
Elliott Hughes | ee0fa76 | 2012-03-26 17:12:41 -0700 | [diff] [blame] | 1023 | env_->get_region_fn(a, 0, 1, &dst_buf[0]); \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1024 | env_->get_region_fn(a, size - 1, 1, &dst_buf[size - 1]); \ |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1025 | EXPECT_EQ(memcmp(&src_buf[0], &dst_buf[0], size * sizeof(scalar_type)), 0) \ |
| 1026 | << "fixed copy not equal"; \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1027 | /* Copy back the whole array. */ \ |
Elliott Hughes | ee0fa76 | 2012-03-26 17:12:41 -0700 | [diff] [blame] | 1028 | env_->get_region_fn(a, 0, size, &dst_buf[0]); \ |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1029 | EXPECT_EQ(memcmp(&src_buf[0], &dst_buf[0], size * sizeof(scalar_type)), 0) \ |
| 1030 | << "full copy not equal"; \ |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1031 | /* GetPrimitiveArrayCritical */ \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1032 | void* v = env_->GetPrimitiveArrayCritical(a, nullptr); \ |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1033 | EXPECT_EQ(memcmp(&src_buf[0], v, size * sizeof(scalar_type)), 0) \ |
| 1034 | << "GetPrimitiveArrayCritical not equal"; \ |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1035 | env_->ReleasePrimitiveArrayCritical(a, v, 0); \ |
| 1036 | /* GetXArrayElements */ \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1037 | scalar_type* xs = env_->get_elements_fn(a, nullptr); \ |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1038 | EXPECT_EQ(memcmp(&src_buf[0], xs, size * sizeof(scalar_type)), 0) \ |
| 1039 | << # get_elements_fn " not equal"; \ |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1040 | env_->release_elements_fn(a, xs, 0); \ |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 1041 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1042 | TEST_F(JniInternalTest, BooleanArrays) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1043 | EXPECT_PRIMITIVE_ARRAY(NewBooleanArray, GetBooleanArrayRegion, SetBooleanArrayRegion, |
| 1044 | GetBooleanArrayElements, ReleaseBooleanArrayElements, jboolean, "[Z"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1045 | } |
| 1046 | TEST_F(JniInternalTest, ByteArrays) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1047 | EXPECT_PRIMITIVE_ARRAY(NewByteArray, GetByteArrayRegion, SetByteArrayRegion, |
| 1048 | GetByteArrayElements, ReleaseByteArrayElements, jbyte, "[B"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1049 | } |
| 1050 | TEST_F(JniInternalTest, CharArrays) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1051 | EXPECT_PRIMITIVE_ARRAY(NewCharArray, GetCharArrayRegion, SetCharArrayRegion, |
| 1052 | GetCharArrayElements, ReleaseCharArrayElements, jchar, "[C"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1053 | } |
| 1054 | TEST_F(JniInternalTest, DoubleArrays) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1055 | EXPECT_PRIMITIVE_ARRAY(NewDoubleArray, GetDoubleArrayRegion, SetDoubleArrayRegion, |
| 1056 | GetDoubleArrayElements, ReleaseDoubleArrayElements, jdouble, "[D"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1057 | } |
| 1058 | TEST_F(JniInternalTest, FloatArrays) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1059 | EXPECT_PRIMITIVE_ARRAY(NewFloatArray, GetFloatArrayRegion, SetFloatArrayRegion, |
| 1060 | GetFloatArrayElements, ReleaseFloatArrayElements, jfloat, "[F"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1061 | } |
| 1062 | TEST_F(JniInternalTest, IntArrays) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1063 | EXPECT_PRIMITIVE_ARRAY(NewIntArray, GetIntArrayRegion, SetIntArrayRegion, |
| 1064 | GetIntArrayElements, ReleaseIntArrayElements, jint, "[I"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1065 | } |
| 1066 | TEST_F(JniInternalTest, LongArrays) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1067 | EXPECT_PRIMITIVE_ARRAY(NewLongArray, GetLongArrayRegion, SetLongArrayRegion, |
| 1068 | GetLongArrayElements, ReleaseLongArrayElements, jlong, "[J"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1069 | } |
| 1070 | TEST_F(JniInternalTest, ShortArrays) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1071 | EXPECT_PRIMITIVE_ARRAY(NewShortArray, GetShortArrayRegion, SetShortArrayRegion, |
| 1072 | GetShortArrayElements, ReleaseShortArrayElements, jshort, "[S"); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 1073 | } |
| 1074 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1075 | TEST_F(JniInternalTest, GetPrimitiveArrayElementsOfWrongType) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1076 | GetPrimitiveArrayElementsOfWrongType(false); |
| 1077 | GetPrimitiveArrayElementsOfWrongType(true); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | TEST_F(JniInternalTest, ReleasePrimitiveArrayElementsOfWrongType) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1081 | ReleasePrimitiveArrayElementsOfWrongType(false); |
| 1082 | ReleasePrimitiveArrayElementsOfWrongType(true); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1083 | } |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1084 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1085 | TEST_F(JniInternalTest, GetReleasePrimitiveArrayCriticalOfWrongType) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1086 | GetReleasePrimitiveArrayCriticalOfWrongType(false); |
| 1087 | GetReleasePrimitiveArrayCriticalOfWrongType(true); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1088 | } |
| 1089 | |
| 1090 | TEST_F(JniInternalTest, GetPrimitiveArrayRegionElementsOfWrongType) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1091 | GetPrimitiveArrayRegionElementsOfWrongType(false); |
| 1092 | GetPrimitiveArrayRegionElementsOfWrongType(true); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | TEST_F(JniInternalTest, SetPrimitiveArrayRegionElementsOfWrongType) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1096 | SetPrimitiveArrayRegionElementsOfWrongType(false); |
| 1097 | SetPrimitiveArrayRegionElementsOfWrongType(true); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1098 | } |
| 1099 | |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 1100 | TEST_F(JniInternalTest, NewObjectArray) { |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 1101 | jclass element_class = env_->FindClass("java/lang/String"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1102 | ASSERT_NE(element_class, nullptr); |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 1103 | jclass array_class = env_->FindClass("[Ljava/lang/String;"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1104 | ASSERT_NE(array_class, nullptr); |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 1105 | |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 1106 | jobjectArray a = env_->NewObjectArray(0, element_class, nullptr); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1107 | EXPECT_NE(a, nullptr); |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 1108 | EXPECT_TRUE(env_->IsInstanceOf(a, array_class)); |
| 1109 | EXPECT_EQ(0, env_->GetArrayLength(a)); |
| 1110 | |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 1111 | a = env_->NewObjectArray(1, element_class, nullptr); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1112 | EXPECT_NE(a, nullptr); |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 1113 | EXPECT_TRUE(env_->IsInstanceOf(a, array_class)); |
| 1114 | EXPECT_EQ(1, env_->GetArrayLength(a)); |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 1115 | EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(a, 0), nullptr)); |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 1116 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1117 | // Negative array length checks. |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1118 | NewObjectArrayBadArguments(false); |
| 1119 | NewObjectArrayBadArguments(true); |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 1120 | } |
| 1121 | |
| 1122 | TEST_F(JniInternalTest, NewObjectArrayWithPrimitiveClasses) { |
| 1123 | const char* primitive_descriptors = "VZBSCIJFD"; |
| 1124 | const char* primitive_names[] = { |
| 1125 | "void", "boolean", "byte", "short", "char", "int", "long", "float", "double" |
| 1126 | }; |
| 1127 | ASSERT_EQ(strlen(primitive_descriptors), arraysize(primitive_names)); |
| 1128 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1129 | bool old_check_jni = vm_->SetCheckJniEnabled(false); |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 1130 | CheckJniAbortCatcher jni_abort_catcher; |
| 1131 | for (size_t i = 0; i < strlen(primitive_descriptors); ++i) { |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1132 | env_->NewObjectArray(0, nullptr, nullptr); |
| 1133 | jni_abort_catcher.Check("element_jclass == null"); |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 1134 | jclass primitive_class = GetPrimitiveClass(primitive_descriptors[i]); |
| 1135 | env_->NewObjectArray(1, primitive_class, nullptr); |
| 1136 | std::string error_msg(StringPrintf("not an object type: %s", primitive_names[i])); |
| 1137 | jni_abort_catcher.Check(error_msg.c_str()); |
| 1138 | } |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1139 | EXPECT_FALSE(vm_->SetCheckJniEnabled(true)); |
| 1140 | for (size_t i = 0; i < strlen(primitive_descriptors); ++i) { |
| 1141 | env_->NewObjectArray(0, nullptr, nullptr); |
| 1142 | jni_abort_catcher.Check("NewObjectArray received NULL jclass"); |
| 1143 | jclass primitive_class = GetPrimitiveClass(primitive_descriptors[i]); |
| 1144 | env_->NewObjectArray(1, primitive_class, nullptr); |
| 1145 | std::string error_msg(StringPrintf("not an object type: %s", primitive_names[i])); |
| 1146 | jni_abort_catcher.Check(error_msg.c_str()); |
| 1147 | } |
| 1148 | EXPECT_TRUE(vm_->SetCheckJniEnabled(old_check_jni)); |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 1149 | } |
| 1150 | |
| 1151 | TEST_F(JniInternalTest, NewObjectArrayWithInitialValue) { |
| 1152 | jclass element_class = env_->FindClass("java/lang/String"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1153 | ASSERT_NE(element_class, nullptr); |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 1154 | jclass array_class = env_->FindClass("[Ljava/lang/String;"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1155 | ASSERT_NE(array_class, nullptr); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1156 | |
| 1157 | jstring s = env_->NewStringUTF("poop"); |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 1158 | jobjectArray a = env_->NewObjectArray(2, element_class, s); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1159 | EXPECT_NE(a, nullptr); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1160 | EXPECT_TRUE(env_->IsInstanceOf(a, array_class)); |
| 1161 | EXPECT_EQ(2, env_->GetArrayLength(a)); |
| 1162 | EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(a, 0), s)); |
| 1163 | EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(a, 1), s)); |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 1164 | |
| 1165 | // Attempt to incorrect create an array of strings with initial value of string arrays. |
| 1166 | CheckJniAbortCatcher jni_abort_catcher; |
| 1167 | env_->NewObjectArray(2, element_class, a); |
| 1168 | jni_abort_catcher.Check("cannot assign object of type 'java.lang.String[]' to array with element " |
| 1169 | "type of 'java.lang.String'"); |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 1170 | } |
| 1171 | |
| 1172 | TEST_F(JniInternalTest, GetArrayLength) { |
| 1173 | // Already tested in NewObjectArray/NewPrimitiveArray. |
Elliott Hughes | 8a26c5c | 2011-08-15 18:35:43 -0700 | [diff] [blame] | 1174 | } |
| 1175 | |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1176 | TEST_F(JniInternalTest, GetObjectClass) { |
| 1177 | jclass string_class = env_->FindClass("java/lang/String"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1178 | ASSERT_NE(string_class, nullptr); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1179 | jclass class_class = env_->FindClass("java/lang/Class"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1180 | ASSERT_NE(class_class, nullptr); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1181 | |
| 1182 | jstring s = env_->NewStringUTF("poop"); |
| 1183 | jclass c = env_->GetObjectClass(s); |
| 1184 | ASSERT_TRUE(env_->IsSameObject(string_class, c)); |
| 1185 | |
| 1186 | jclass c2 = env_->GetObjectClass(c); |
| 1187 | ASSERT_TRUE(env_->IsSameObject(class_class, env_->GetObjectClass(c2))); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1188 | |
| 1189 | // Null as object should fail. |
| 1190 | CheckJniAbortCatcher jni_abort_catcher; |
| 1191 | EXPECT_EQ(env_->GetObjectClass(nullptr), nullptr); |
| 1192 | jni_abort_catcher.Check("java_object == null"); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1193 | } |
| 1194 | |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1195 | TEST_F(JniInternalTest, GetSuperclass) { |
| 1196 | jclass object_class = env_->FindClass("java/lang/Object"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1197 | ASSERT_NE(object_class, nullptr); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1198 | jclass string_class = env_->FindClass("java/lang/String"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1199 | ASSERT_NE(string_class, nullptr); |
Ian Rogers | dc18020 | 2012-01-29 14:47:29 -0800 | [diff] [blame] | 1200 | jclass runnable_interface = env_->FindClass("java/lang/Runnable"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1201 | ASSERT_NE(runnable_interface, nullptr); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1202 | ASSERT_TRUE(env_->IsSameObject(object_class, env_->GetSuperclass(string_class))); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1203 | ASSERT_EQ(env_->GetSuperclass(object_class), nullptr); |
Ian Rogers | dc18020 | 2012-01-29 14:47:29 -0800 | [diff] [blame] | 1204 | ASSERT_TRUE(env_->IsSameObject(object_class, env_->GetSuperclass(runnable_interface))); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1205 | |
| 1206 | // Null as class should fail. |
| 1207 | CheckJniAbortCatcher jni_abort_catcher; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1208 | bool old_check_jni = vm_->SetCheckJniEnabled(false); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1209 | EXPECT_EQ(env_->GetSuperclass(nullptr), nullptr); |
| 1210 | jni_abort_catcher.Check("java_class == null"); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1211 | EXPECT_FALSE(vm_->SetCheckJniEnabled(true)); |
| 1212 | EXPECT_EQ(env_->GetSuperclass(nullptr), nullptr); |
| 1213 | jni_abort_catcher.Check("GetSuperclass received NULL jclass"); |
| 1214 | EXPECT_TRUE(vm_->SetCheckJniEnabled(old_check_jni)); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1215 | } |
| 1216 | |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1217 | TEST_F(JniInternalTest, IsAssignableFrom) { |
| 1218 | jclass object_class = env_->FindClass("java/lang/Object"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1219 | ASSERT_NE(object_class, nullptr); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1220 | jclass string_class = env_->FindClass("java/lang/String"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1221 | ASSERT_NE(string_class, nullptr); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1222 | |
Narayan Kamath | 1268b74 | 2014-07-11 19:15:11 +0100 | [diff] [blame] | 1223 | // A superclass is assignable from an instance of its |
| 1224 | // subclass but not vice versa. |
| 1225 | ASSERT_TRUE(env_->IsAssignableFrom(string_class, object_class)); |
| 1226 | ASSERT_FALSE(env_->IsAssignableFrom(object_class, string_class)); |
| 1227 | |
| 1228 | jclass charsequence_interface = env_->FindClass("java/lang/CharSequence"); |
| 1229 | ASSERT_NE(charsequence_interface, nullptr); |
| 1230 | |
| 1231 | // An interface is assignable from an instance of an implementing |
| 1232 | // class but not vice versa. |
| 1233 | ASSERT_TRUE(env_->IsAssignableFrom(string_class, charsequence_interface)); |
| 1234 | ASSERT_FALSE(env_->IsAssignableFrom(charsequence_interface, string_class)); |
| 1235 | |
| 1236 | // Check that arrays are covariant. |
| 1237 | jclass string_array_class = env_->FindClass("[Ljava/lang/String;"); |
| 1238 | ASSERT_NE(string_array_class, nullptr); |
| 1239 | jclass object_array_class = env_->FindClass("[Ljava/lang/Object;"); |
| 1240 | ASSERT_NE(object_array_class, nullptr); |
| 1241 | ASSERT_TRUE(env_->IsAssignableFrom(string_array_class, object_array_class)); |
| 1242 | ASSERT_FALSE(env_->IsAssignableFrom(object_array_class, string_array_class)); |
| 1243 | |
| 1244 | // Primitive types are tested in 004-JniTest. |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1245 | |
| 1246 | // Null as either class should fail. |
| 1247 | CheckJniAbortCatcher jni_abort_catcher; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1248 | bool old_check_jni = vm_->SetCheckJniEnabled(false); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1249 | EXPECT_EQ(env_->IsAssignableFrom(nullptr, string_class), JNI_FALSE); |
| 1250 | jni_abort_catcher.Check("java_class1 == null"); |
| 1251 | EXPECT_EQ(env_->IsAssignableFrom(object_class, nullptr), JNI_FALSE); |
| 1252 | jni_abort_catcher.Check("java_class2 == null"); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1253 | EXPECT_FALSE(vm_->SetCheckJniEnabled(true)); |
| 1254 | EXPECT_EQ(env_->IsAssignableFrom(nullptr, string_class), JNI_FALSE); |
| 1255 | jni_abort_catcher.Check("IsAssignableFrom received NULL jclass"); |
| 1256 | EXPECT_EQ(env_->IsAssignableFrom(object_class, nullptr), JNI_FALSE); |
| 1257 | jni_abort_catcher.Check("IsAssignableFrom received NULL jclass"); |
| 1258 | EXPECT_TRUE(vm_->SetCheckJniEnabled(old_check_jni)); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1259 | } |
| 1260 | |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1261 | TEST_F(JniInternalTest, GetObjectRefType) { |
| 1262 | jclass local = env_->FindClass("java/lang/Object"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1263 | ASSERT_TRUE(local != nullptr); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1264 | EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(local)); |
| 1265 | |
| 1266 | jobject global = env_->NewGlobalRef(local); |
| 1267 | EXPECT_EQ(JNIGlobalRefType, env_->GetObjectRefType(global)); |
| 1268 | |
| 1269 | jweak weak_global = env_->NewWeakGlobalRef(local); |
| 1270 | EXPECT_EQ(JNIWeakGlobalRefType, env_->GetObjectRefType(weak_global)); |
| 1271 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame^] | 1272 | CheckJniAbortCatcher jni_abort_catcher; |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1273 | jobject invalid = reinterpret_cast<jobject>(this); |
| 1274 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(invalid)); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame^] | 1275 | jni_abort_catcher.Check("use of invalid jobject"); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1276 | |
| 1277 | // 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] | 1278 | |
| 1279 | // Null as object should fail. |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1280 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(nullptr)); |
| 1281 | jni_abort_catcher.Check("java_object == null"); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1282 | } |
| 1283 | |
Mathieu Chartier | 0859999 | 2013-12-20 17:17:55 -0800 | [diff] [blame] | 1284 | TEST_F(JniInternalTest, StaleWeakGlobal) { |
| 1285 | jclass java_lang_Class = env_->FindClass("java/lang/Class"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1286 | ASSERT_NE(java_lang_Class, nullptr); |
| 1287 | jobjectArray local_ref = env_->NewObjectArray(1, java_lang_Class, nullptr); |
| 1288 | ASSERT_NE(local_ref, nullptr); |
Mathieu Chartier | 0859999 | 2013-12-20 17:17:55 -0800 | [diff] [blame] | 1289 | jweak weak_global = env_->NewWeakGlobalRef(local_ref); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1290 | ASSERT_NE(weak_global, nullptr); |
Mathieu Chartier | 0859999 | 2013-12-20 17:17:55 -0800 | [diff] [blame] | 1291 | env_->DeleteLocalRef(local_ref); |
| 1292 | Runtime::Current()->GetHeap()->CollectGarbage(false); // GC should clear the weak global. |
| 1293 | jobject new_global_ref = env_->NewGlobalRef(weak_global); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1294 | EXPECT_EQ(new_global_ref, nullptr); |
Mathieu Chartier | 0859999 | 2013-12-20 17:17:55 -0800 | [diff] [blame] | 1295 | jobject new_local_ref = env_->NewLocalRef(weak_global); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1296 | EXPECT_EQ(new_local_ref, nullptr); |
Mathieu Chartier | 0859999 | 2013-12-20 17:17:55 -0800 | [diff] [blame] | 1297 | } |
| 1298 | |
Elliott Hughes | 8a26c5c | 2011-08-15 18:35:43 -0700 | [diff] [blame] | 1299 | TEST_F(JniInternalTest, NewStringUTF) { |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1300 | EXPECT_EQ(env_->NewStringUTF(nullptr), nullptr); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1301 | jstring s; |
| 1302 | |
| 1303 | s = env_->NewStringUTF(""); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1304 | EXPECT_NE(s, nullptr); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1305 | EXPECT_EQ(0, env_->GetStringLength(s)); |
| 1306 | EXPECT_EQ(0, env_->GetStringUTFLength(s)); |
| 1307 | s = env_->NewStringUTF("hello"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1308 | EXPECT_NE(s, nullptr); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1309 | EXPECT_EQ(5, env_->GetStringLength(s)); |
| 1310 | EXPECT_EQ(5, env_->GetStringUTFLength(s)); |
| 1311 | |
Elliott Hughes | 8a26c5c | 2011-08-15 18:35:43 -0700 | [diff] [blame] | 1312 | // TODO: check some non-ASCII strings. |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 1313 | } |
| 1314 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1315 | TEST_F(JniInternalTest, NewString) { |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1316 | jchar chars[] = { 'h', 'i' }; |
| 1317 | jstring s; |
| 1318 | s = env_->NewString(chars, 0); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1319 | EXPECT_NE(s, nullptr); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1320 | EXPECT_EQ(0, env_->GetStringLength(s)); |
| 1321 | EXPECT_EQ(0, env_->GetStringUTFLength(s)); |
| 1322 | s = env_->NewString(chars, 2); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1323 | EXPECT_NE(s, nullptr); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1324 | EXPECT_EQ(2, env_->GetStringLength(s)); |
| 1325 | EXPECT_EQ(2, env_->GetStringUTFLength(s)); |
| 1326 | |
| 1327 | // TODO: check some non-ASCII strings. |
| 1328 | } |
| 1329 | |
Jesse Wilson | 25e79a5 | 2011-11-18 15:31:58 -0500 | [diff] [blame] | 1330 | TEST_F(JniInternalTest, NewStringNullCharsZeroLength) { |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 1331 | jstring s = env_->NewString(nullptr, 0); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1332 | EXPECT_NE(s, nullptr); |
Jesse Wilson | 25e79a5 | 2011-11-18 15:31:58 -0500 | [diff] [blame] | 1333 | EXPECT_EQ(0, env_->GetStringLength(s)); |
| 1334 | } |
| 1335 | |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 1336 | TEST_F(JniInternalTest, NewStringNullCharsNonzeroLength) { |
| 1337 | CheckJniAbortCatcher jni_abort_catcher; |
| 1338 | env_->NewString(nullptr, 1); |
| 1339 | jni_abort_catcher.Check("chars == null && char_count > 0"); |
| 1340 | } |
| 1341 | |
| 1342 | TEST_F(JniInternalTest, NewStringNegativeLength) { |
| 1343 | CheckJniAbortCatcher jni_abort_catcher; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1344 | bool old_check_jni = vm_->SetCheckJniEnabled(false); |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 1345 | env_->NewString(nullptr, -1); |
| 1346 | jni_abort_catcher.Check("char_count < 0: -1"); |
| 1347 | env_->NewString(nullptr, std::numeric_limits<jint>::min()); |
| 1348 | jni_abort_catcher.Check("char_count < 0: -2147483648"); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1349 | EXPECT_FALSE(vm_->SetCheckJniEnabled(true)); |
| 1350 | env_->NewString(nullptr, -1); |
| 1351 | jni_abort_catcher.Check("negative jsize: -1"); |
| 1352 | env_->NewString(nullptr, std::numeric_limits<jint>::min()); |
| 1353 | jni_abort_catcher.Check("negative jsize: -2147483648"); |
| 1354 | EXPECT_TRUE(vm_->SetCheckJniEnabled(old_check_jni)); |
Jesse Wilson | 25e79a5 | 2011-11-18 15:31:58 -0500 | [diff] [blame] | 1355 | } |
| 1356 | |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1357 | TEST_F(JniInternalTest, GetStringLength_GetStringUTFLength) { |
| 1358 | // Already tested in the NewString/NewStringUTF tests. |
| 1359 | } |
| 1360 | |
| 1361 | TEST_F(JniInternalTest, GetStringRegion_GetStringUTFRegion) { |
| 1362 | jstring s = env_->NewStringUTF("hello"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1363 | ASSERT_TRUE(s != nullptr); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1364 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1365 | env_->GetStringRegion(s, -1, 0, nullptr); |
| 1366 | ExpectException(sioobe_); |
| 1367 | env_->GetStringRegion(s, 0, -1, nullptr); |
| 1368 | ExpectException(sioobe_); |
| 1369 | env_->GetStringRegion(s, 0, 10, nullptr); |
| 1370 | ExpectException(sioobe_); |
| 1371 | env_->GetStringRegion(s, 10, 1, nullptr); |
| 1372 | ExpectException(sioobe_); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1373 | |
| 1374 | jchar chars[4] = { 'x', 'x', 'x', 'x' }; |
| 1375 | env_->GetStringRegion(s, 1, 2, &chars[1]); |
| 1376 | EXPECT_EQ('x', chars[0]); |
| 1377 | EXPECT_EQ('e', chars[1]); |
| 1378 | EXPECT_EQ('l', chars[2]); |
| 1379 | EXPECT_EQ('x', chars[3]); |
| 1380 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1381 | // It's okay for the buffer to be nullptr as long as the length is 0. |
| 1382 | env_->GetStringRegion(s, 2, 0, nullptr); |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 1383 | // Even if the offset is invalid... |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1384 | env_->GetStringRegion(s, 123, 0, nullptr); |
| 1385 | ExpectException(sioobe_); |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 1386 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1387 | env_->GetStringUTFRegion(s, -1, 0, nullptr); |
| 1388 | ExpectException(sioobe_); |
| 1389 | env_->GetStringUTFRegion(s, 0, -1, nullptr); |
| 1390 | ExpectException(sioobe_); |
| 1391 | env_->GetStringUTFRegion(s, 0, 10, nullptr); |
| 1392 | ExpectException(sioobe_); |
| 1393 | env_->GetStringUTFRegion(s, 10, 1, nullptr); |
| 1394 | ExpectException(sioobe_); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1395 | |
| 1396 | char bytes[4] = { 'x', 'x', 'x', 'x' }; |
| 1397 | env_->GetStringUTFRegion(s, 1, 2, &bytes[1]); |
| 1398 | EXPECT_EQ('x', bytes[0]); |
| 1399 | EXPECT_EQ('e', bytes[1]); |
| 1400 | EXPECT_EQ('l', bytes[2]); |
| 1401 | EXPECT_EQ('x', bytes[3]); |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 1402 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1403 | // It's okay for the buffer to be nullptr as long as the length is 0. |
| 1404 | env_->GetStringUTFRegion(s, 2, 0, nullptr); |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 1405 | // Even if the offset is invalid... |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1406 | env_->GetStringUTFRegion(s, 123, 0, nullptr); |
| 1407 | ExpectException(sioobe_); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1408 | } |
| 1409 | |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1410 | TEST_F(JniInternalTest, GetStringUTFChars_ReleaseStringUTFChars) { |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1411 | // 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] | 1412 | bool old_check_jni = vm_->SetCheckJniEnabled(false); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1413 | { |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1414 | CheckJniAbortCatcher check_jni_abort_catcher; |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1415 | EXPECT_EQ(env_->GetStringUTFChars(nullptr, nullptr), nullptr); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1416 | } |
| 1417 | { |
| 1418 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 1419 | EXPECT_FALSE(vm_->SetCheckJniEnabled(true)); |
| 1420 | EXPECT_EQ(env_->GetStringUTFChars(nullptr, nullptr), nullptr); |
| 1421 | check_jni_abort_catcher.Check("GetStringUTFChars received NULL jstring"); |
| 1422 | EXPECT_TRUE(vm_->SetCheckJniEnabled(old_check_jni)); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1423 | } |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1424 | |
| 1425 | jstring s = env_->NewStringUTF("hello"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1426 | ASSERT_TRUE(s != nullptr); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1427 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1428 | const char* utf = env_->GetStringUTFChars(s, nullptr); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1429 | EXPECT_STREQ("hello", utf); |
| 1430 | env_->ReleaseStringUTFChars(s, utf); |
| 1431 | |
| 1432 | jboolean is_copy = JNI_FALSE; |
| 1433 | utf = env_->GetStringUTFChars(s, &is_copy); |
| 1434 | EXPECT_EQ(JNI_TRUE, is_copy); |
| 1435 | EXPECT_STREQ("hello", utf); |
| 1436 | env_->ReleaseStringUTFChars(s, utf); |
| 1437 | } |
| 1438 | |
| 1439 | TEST_F(JniInternalTest, GetStringChars_ReleaseStringChars) { |
| 1440 | jstring s = env_->NewStringUTF("hello"); |
Fred Shih | 56890e2 | 2014-06-02 11:11:52 -0700 | [diff] [blame] | 1441 | ScopedObjectAccess soa(env_); |
| 1442 | mirror::String* s_m = soa.Decode<mirror::String*>(s); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1443 | ASSERT_TRUE(s != nullptr); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1444 | |
| 1445 | jchar expected[] = { 'h', 'e', 'l', 'l', 'o' }; |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1446 | const jchar* chars = env_->GetStringChars(s, nullptr); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1447 | EXPECT_EQ(expected[0], chars[0]); |
| 1448 | EXPECT_EQ(expected[1], chars[1]); |
| 1449 | EXPECT_EQ(expected[2], chars[2]); |
| 1450 | EXPECT_EQ(expected[3], chars[3]); |
| 1451 | EXPECT_EQ(expected[4], chars[4]); |
| 1452 | env_->ReleaseStringChars(s, chars); |
| 1453 | |
| 1454 | jboolean is_copy = JNI_FALSE; |
| 1455 | chars = env_->GetStringChars(s, &is_copy); |
Fred Shih | 56890e2 | 2014-06-02 11:11:52 -0700 | [diff] [blame] | 1456 | if (Runtime::Current()->GetHeap()->IsMovableObject(s_m->GetCharArray())) { |
| 1457 | EXPECT_EQ(JNI_TRUE, is_copy); |
| 1458 | } else { |
| 1459 | EXPECT_EQ(JNI_FALSE, is_copy); |
| 1460 | } |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1461 | EXPECT_EQ(expected[0], chars[0]); |
| 1462 | EXPECT_EQ(expected[1], chars[1]); |
| 1463 | EXPECT_EQ(expected[2], chars[2]); |
| 1464 | EXPECT_EQ(expected[3], chars[3]); |
| 1465 | EXPECT_EQ(expected[4], chars[4]); |
| 1466 | env_->ReleaseStringChars(s, chars); |
| 1467 | } |
| 1468 | |
| 1469 | TEST_F(JniInternalTest, GetStringCritical_ReleaseStringCritical) { |
| 1470 | jstring s = env_->NewStringUTF("hello"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1471 | ASSERT_TRUE(s != nullptr); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1472 | |
| 1473 | jchar expected[] = { 'h', 'e', 'l', 'l', 'o' }; |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1474 | const jchar* chars = env_->GetStringCritical(s, nullptr); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1475 | EXPECT_EQ(expected[0], chars[0]); |
| 1476 | EXPECT_EQ(expected[1], chars[1]); |
| 1477 | EXPECT_EQ(expected[2], chars[2]); |
| 1478 | EXPECT_EQ(expected[3], chars[3]); |
| 1479 | EXPECT_EQ(expected[4], chars[4]); |
| 1480 | env_->ReleaseStringCritical(s, chars); |
| 1481 | |
Fred Shih | 56890e2 | 2014-06-02 11:11:52 -0700 | [diff] [blame] | 1482 | jboolean is_copy = JNI_TRUE; |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1483 | chars = env_->GetStringCritical(s, &is_copy); |
Fred Shih | 56890e2 | 2014-06-02 11:11:52 -0700 | [diff] [blame] | 1484 | EXPECT_EQ(JNI_FALSE, is_copy); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1485 | EXPECT_EQ(expected[0], chars[0]); |
| 1486 | EXPECT_EQ(expected[1], chars[1]); |
| 1487 | EXPECT_EQ(expected[2], chars[2]); |
| 1488 | EXPECT_EQ(expected[3], chars[3]); |
| 1489 | EXPECT_EQ(expected[4], chars[4]); |
| 1490 | env_->ReleaseStringCritical(s, chars); |
| 1491 | } |
| 1492 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1493 | TEST_F(JniInternalTest, GetObjectArrayElement_SetObjectArrayElement) { |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1494 | jclass java_lang_Class = env_->FindClass("java/lang/Class"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1495 | ASSERT_TRUE(java_lang_Class != nullptr); |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 1496 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1497 | jobjectArray array = env_->NewObjectArray(1, java_lang_Class, nullptr); |
| 1498 | EXPECT_NE(array, nullptr); |
| 1499 | EXPECT_EQ(env_->GetObjectArrayElement(array, 0), nullptr); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1500 | env_->SetObjectArrayElement(array, 0, java_lang_Class); |
| 1501 | EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(array, 0), java_lang_Class)); |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 1502 | |
| 1503 | // ArrayIndexOutOfBounds for negative index. |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1504 | env_->SetObjectArrayElement(array, -1, java_lang_Class); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1505 | ExpectException(aioobe_); |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 1506 | |
| 1507 | // ArrayIndexOutOfBounds for too-large index. |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1508 | env_->SetObjectArrayElement(array, 1, java_lang_Class); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1509 | ExpectException(aioobe_); |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 1510 | |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1511 | // ArrayStoreException thrown for bad types. |
| 1512 | env_->SetObjectArrayElement(array, 0, env_->NewStringUTF("not a jclass!")); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1513 | ExpectException(ase_); |
| 1514 | |
| 1515 | // Null as array should fail. |
| 1516 | CheckJniAbortCatcher jni_abort_catcher; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1517 | bool old_check_jni = vm_->SetCheckJniEnabled(false); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1518 | EXPECT_EQ(nullptr, env_->GetObjectArrayElement(nullptr, 0)); |
| 1519 | jni_abort_catcher.Check("java_array == null"); |
| 1520 | env_->SetObjectArrayElement(nullptr, 0, nullptr); |
| 1521 | jni_abort_catcher.Check("java_array == null"); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1522 | EXPECT_FALSE(vm_->SetCheckJniEnabled(true)); |
| 1523 | EXPECT_EQ(nullptr, env_->GetObjectArrayElement(nullptr, 0)); |
| 1524 | jni_abort_catcher.Check("jarray was NULL"); |
| 1525 | env_->SetObjectArrayElement(nullptr, 0, nullptr); |
| 1526 | jni_abort_catcher.Check("jarray was NULL"); |
| 1527 | EXPECT_TRUE(vm_->SetCheckJniEnabled(old_check_jni)); |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 1528 | } |
| 1529 | |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1530 | #define EXPECT_STATIC_PRIMITIVE_FIELD(type, field_name, sig, value1, value2) \ |
| 1531 | do { \ |
| 1532 | jfieldID fid = env_->GetStaticFieldID(c, field_name, sig); \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1533 | EXPECT_NE(fid, nullptr); \ |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1534 | env_->SetStatic ## type ## Field(c, fid, value1); \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1535 | EXPECT_EQ(value1, env_->GetStatic ## type ## Field(c, fid)); \ |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1536 | env_->SetStatic ## type ## Field(c, fid, value2); \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1537 | EXPECT_EQ(value2, env_->GetStatic ## type ## Field(c, fid)); \ |
| 1538 | \ |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1539 | bool old_check_jni = vm_->SetCheckJniEnabled(false); \ |
| 1540 | { \ |
| 1541 | CheckJniAbortCatcher jni_abort_catcher; \ |
| 1542 | env_->GetStatic ## type ## Field(nullptr, fid); \ |
| 1543 | env_->SetStatic ## type ## Field(nullptr, fid, value1); \ |
| 1544 | } \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1545 | CheckJniAbortCatcher jni_abort_catcher; \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1546 | env_->GetStatic ## type ## Field(c, nullptr); \ |
| 1547 | jni_abort_catcher.Check("fid == null"); \ |
| 1548 | env_->SetStatic ## type ## Field(c, nullptr, value1); \ |
| 1549 | jni_abort_catcher.Check("fid == null"); \ |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1550 | \ |
| 1551 | EXPECT_FALSE(vm_->SetCheckJniEnabled(true)); \ |
| 1552 | env_->GetStatic ## type ## Field(nullptr, fid); \ |
| 1553 | jni_abort_catcher.Check("received NULL jclass"); \ |
| 1554 | env_->SetStatic ## type ## Field(nullptr, fid, value1); \ |
| 1555 | jni_abort_catcher.Check("received NULL jclass"); \ |
| 1556 | env_->GetStatic ## type ## Field(c, nullptr); \ |
| 1557 | jni_abort_catcher.Check("jfieldID was NULL"); \ |
| 1558 | env_->SetStatic ## type ## Field(c, nullptr, value1); \ |
| 1559 | jni_abort_catcher.Check("jfieldID was NULL"); \ |
| 1560 | EXPECT_TRUE(vm_->SetCheckJniEnabled(old_check_jni)); \ |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1561 | } while (false) |
| 1562 | |
| 1563 | #define EXPECT_PRIMITIVE_FIELD(instance, type, field_name, sig, value1, value2) \ |
| 1564 | do { \ |
| 1565 | jfieldID fid = env_->GetFieldID(c, field_name, sig); \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1566 | EXPECT_NE(fid, nullptr); \ |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1567 | env_->Set ## type ## Field(instance, fid, value1); \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1568 | EXPECT_EQ(value1, env_->Get ## type ## Field(instance, fid)); \ |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1569 | env_->Set ## type ## Field(instance, fid, value2); \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1570 | EXPECT_EQ(value2, env_->Get ## type ## Field(instance, fid)); \ |
| 1571 | \ |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1572 | bool old_check_jni = vm_->SetCheckJniEnabled(false); \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1573 | CheckJniAbortCatcher jni_abort_catcher; \ |
| 1574 | env_->Get ## type ## Field(nullptr, fid); \ |
| 1575 | jni_abort_catcher.Check("obj == null"); \ |
| 1576 | env_->Set ## type ## Field(nullptr, fid, value1); \ |
| 1577 | jni_abort_catcher.Check("obj == null"); \ |
| 1578 | env_->Get ## type ## Field(instance, nullptr); \ |
| 1579 | jni_abort_catcher.Check("fid == null"); \ |
| 1580 | env_->Set ## type ## Field(instance, nullptr, value1); \ |
| 1581 | jni_abort_catcher.Check("fid == null"); \ |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1582 | EXPECT_FALSE(vm_->SetCheckJniEnabled(true)); \ |
| 1583 | env_->Get ## type ## Field(nullptr, fid); \ |
| 1584 | jni_abort_catcher.Check("field operation on NULL object:"); \ |
| 1585 | env_->Set ## type ## Field(nullptr, fid, value1); \ |
| 1586 | jni_abort_catcher.Check("field operation on NULL object:"); \ |
| 1587 | env_->Get ## type ## Field(instance, nullptr); \ |
| 1588 | jni_abort_catcher.Check("jfieldID was NULL"); \ |
| 1589 | env_->Set ## type ## Field(instance, nullptr, value1); \ |
| 1590 | jni_abort_catcher.Check("jfieldID was NULL"); \ |
| 1591 | EXPECT_TRUE(vm_->SetCheckJniEnabled(old_check_jni)); \ |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1592 | } while (false) |
| 1593 | |
| 1594 | |
| 1595 | TEST_F(JniInternalTest, GetPrimitiveField_SetPrimitiveField) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1596 | TEST_DISABLED_FOR_PORTABLE(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1597 | Thread::Current()->TransitionFromSuspendedToRunnable(); |
| 1598 | LoadDex("AllFields"); |
Brian Carlstrom | bd86bcc | 2013-03-10 20:26:16 -0700 | [diff] [blame] | 1599 | bool started = runtime_->Start(); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1600 | ASSERT_TRUE(started); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1601 | |
| 1602 | jclass c = env_->FindClass("AllFields"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1603 | ASSERT_NE(c, nullptr); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1604 | jobject o = env_->AllocObject(c); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1605 | ASSERT_NE(o, nullptr); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1606 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1607 | EXPECT_STATIC_PRIMITIVE_FIELD(Boolean, "sZ", "Z", JNI_TRUE, JNI_FALSE); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1608 | EXPECT_STATIC_PRIMITIVE_FIELD(Byte, "sB", "B", 1, 2); |
| 1609 | EXPECT_STATIC_PRIMITIVE_FIELD(Char, "sC", "C", 'a', 'b'); |
| 1610 | EXPECT_STATIC_PRIMITIVE_FIELD(Double, "sD", "D", 1.0, 2.0); |
| 1611 | EXPECT_STATIC_PRIMITIVE_FIELD(Float, "sF", "F", 1.0, 2.0); |
| 1612 | EXPECT_STATIC_PRIMITIVE_FIELD(Int, "sI", "I", 1, 2); |
| 1613 | EXPECT_STATIC_PRIMITIVE_FIELD(Long, "sJ", "J", 1, 2); |
| 1614 | EXPECT_STATIC_PRIMITIVE_FIELD(Short, "sS", "S", 1, 2); |
| 1615 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1616 | EXPECT_PRIMITIVE_FIELD(o, Boolean, "iZ", "Z", JNI_TRUE, JNI_FALSE); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1617 | EXPECT_PRIMITIVE_FIELD(o, Byte, "iB", "B", 1, 2); |
| 1618 | EXPECT_PRIMITIVE_FIELD(o, Char, "iC", "C", 'a', 'b'); |
| 1619 | EXPECT_PRIMITIVE_FIELD(o, Double, "iD", "D", 1.0, 2.0); |
| 1620 | EXPECT_PRIMITIVE_FIELD(o, Float, "iF", "F", 1.0, 2.0); |
| 1621 | EXPECT_PRIMITIVE_FIELD(o, Int, "iI", "I", 1, 2); |
| 1622 | EXPECT_PRIMITIVE_FIELD(o, Long, "iJ", "J", 1, 2); |
| 1623 | EXPECT_PRIMITIVE_FIELD(o, Short, "iS", "S", 1, 2); |
| 1624 | } |
| 1625 | |
| 1626 | TEST_F(JniInternalTest, GetObjectField_SetObjectField) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1627 | TEST_DISABLED_FOR_PORTABLE(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1628 | Thread::Current()->TransitionFromSuspendedToRunnable(); |
| 1629 | LoadDex("AllFields"); |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 1630 | runtime_->Start(); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1631 | |
| 1632 | jclass c = env_->FindClass("AllFields"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1633 | ASSERT_NE(c, nullptr); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1634 | jobject o = env_->AllocObject(c); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1635 | ASSERT_NE(o, nullptr); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1636 | |
| 1637 | jstring s1 = env_->NewStringUTF("hello"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1638 | ASSERT_NE(s1, nullptr); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1639 | jstring s2 = env_->NewStringUTF("world"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1640 | ASSERT_NE(s2, nullptr); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1641 | |
| 1642 | jfieldID s_fid = env_->GetStaticFieldID(c, "sObject", "Ljava/lang/Object;"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1643 | ASSERT_NE(s_fid, nullptr); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1644 | jfieldID i_fid = env_->GetFieldID(c, "iObject", "Ljava/lang/Object;"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1645 | ASSERT_NE(i_fid, nullptr); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1646 | |
| 1647 | env_->SetStaticObjectField(c, s_fid, s1); |
| 1648 | ASSERT_TRUE(env_->IsSameObject(s1, env_->GetStaticObjectField(c, s_fid))); |
| 1649 | env_->SetStaticObjectField(c, s_fid, s2); |
| 1650 | ASSERT_TRUE(env_->IsSameObject(s2, env_->GetStaticObjectField(c, s_fid))); |
| 1651 | |
| 1652 | env_->SetObjectField(o, i_fid, s1); |
| 1653 | ASSERT_TRUE(env_->IsSameObject(s1, env_->GetObjectField(o, i_fid))); |
| 1654 | env_->SetObjectField(o, i_fid, s2); |
| 1655 | ASSERT_TRUE(env_->IsSameObject(s2, env_->GetObjectField(o, i_fid))); |
| 1656 | } |
| 1657 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1658 | TEST_F(JniInternalTest, NewLocalRef_nullptr) { |
| 1659 | EXPECT_EQ(env_->NewLocalRef(nullptr), nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1660 | } |
| 1661 | |
| 1662 | TEST_F(JniInternalTest, NewLocalRef) { |
| 1663 | jstring s = env_->NewStringUTF(""); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1664 | ASSERT_NE(s, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1665 | jobject o = env_->NewLocalRef(s); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1666 | EXPECT_NE(o, nullptr); |
| 1667 | EXPECT_NE(o, s); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1668 | |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1669 | EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(o)); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1670 | } |
| 1671 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1672 | TEST_F(JniInternalTest, DeleteLocalRef_nullptr) { |
| 1673 | env_->DeleteLocalRef(nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1674 | } |
| 1675 | |
| 1676 | TEST_F(JniInternalTest, DeleteLocalRef) { |
| 1677 | jstring s = env_->NewStringUTF(""); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1678 | ASSERT_NE(s, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1679 | env_->DeleteLocalRef(s); |
| 1680 | |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 1681 | // Currently, deleting an already-deleted reference is just a CheckJNI warning. |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1682 | { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1683 | bool old_check_jni = vm_->SetCheckJniEnabled(false); |
| 1684 | { |
| 1685 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 1686 | env_->DeleteLocalRef(s); |
| 1687 | } |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1688 | CheckJniAbortCatcher check_jni_abort_catcher; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1689 | EXPECT_FALSE(vm_->SetCheckJniEnabled(true)); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1690 | env_->DeleteLocalRef(s); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame^] | 1691 | std::string expected(StringPrintf("use of deleted local reference %p", s)); |
Elliott Hughes | a9137c6 | 2013-01-09 10:55:21 -0800 | [diff] [blame] | 1692 | check_jni_abort_catcher.Check(expected.c_str()); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1693 | EXPECT_TRUE(vm_->SetCheckJniEnabled(old_check_jni)); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1694 | } |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1695 | |
| 1696 | s = env_->NewStringUTF(""); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1697 | ASSERT_NE(s, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1698 | jobject o = env_->NewLocalRef(s); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1699 | ASSERT_NE(o, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1700 | |
| 1701 | env_->DeleteLocalRef(s); |
| 1702 | env_->DeleteLocalRef(o); |
| 1703 | } |
| 1704 | |
Elliott Hughes | aa836f7 | 2013-08-20 16:57:23 -0700 | [diff] [blame] | 1705 | TEST_F(JniInternalTest, PushLocalFrame_10395422) { |
| 1706 | // The JNI specification is ambiguous about whether the given capacity is to be interpreted as a |
| 1707 | // maximum or as a minimum, but it seems like it's supposed to be a minimum, and that's how |
| 1708 | // Android historically treated it, and it's how the RI treats it. It's also the more useful |
| 1709 | // interpretation! |
| 1710 | ASSERT_EQ(JNI_OK, env_->PushLocalFrame(0)); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1711 | env_->PopLocalFrame(nullptr); |
Elliott Hughes | aa836f7 | 2013-08-20 16:57:23 -0700 | [diff] [blame] | 1712 | |
| 1713 | // Negative capacities are not allowed. |
| 1714 | ASSERT_EQ(JNI_ERR, env_->PushLocalFrame(-1)); |
| 1715 | |
| 1716 | // And it's okay to have an upper limit. Ours is currently 512. |
| 1717 | ASSERT_EQ(JNI_ERR, env_->PushLocalFrame(8192)); |
| 1718 | } |
| 1719 | |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1720 | TEST_F(JniInternalTest, PushLocalFrame_PopLocalFrame) { |
| 1721 | jobject original = env_->NewStringUTF(""); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1722 | ASSERT_NE(original, nullptr); |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1723 | |
| 1724 | jobject outer; |
| 1725 | jobject inner1, inner2; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1726 | ScopedObjectAccess soa(env_); |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1727 | { |
Elliott Hughes | aa836f7 | 2013-08-20 16:57:23 -0700 | [diff] [blame] | 1728 | ASSERT_EQ(JNI_OK, env_->PushLocalFrame(4)); |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1729 | outer = env_->NewLocalRef(original); |
| 1730 | |
| 1731 | { |
Elliott Hughes | aa836f7 | 2013-08-20 16:57:23 -0700 | [diff] [blame] | 1732 | ASSERT_EQ(JNI_OK, env_->PushLocalFrame(4)); |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1733 | inner1 = env_->NewLocalRef(outer); |
| 1734 | inner2 = env_->NewStringUTF("survivor"); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame^] | 1735 | EXPECT_NE(env_->PopLocalFrame(inner2), nullptr); |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1736 | } |
| 1737 | |
| 1738 | EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(original)); |
| 1739 | EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(outer)); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame^] | 1740 | { |
| 1741 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 1742 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(inner1)); |
| 1743 | check_jni_abort_catcher.Check("use of deleted local reference"); |
| 1744 | } |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1745 | |
| 1746 | // Our local reference for the survivor is invalid because the survivor |
| 1747 | // gets a new local reference... |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame^] | 1748 | { |
| 1749 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 1750 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(inner2)); |
| 1751 | check_jni_abort_catcher.Check("use of deleted local reference"); |
| 1752 | } |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1753 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame^] | 1754 | EXPECT_EQ(env_->PopLocalFrame(nullptr), nullptr); |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1755 | } |
| 1756 | EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(original)); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame^] | 1757 | CheckJniAbortCatcher check_jni_abort_catcher; |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1758 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(outer)); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame^] | 1759 | check_jni_abort_catcher.Check("use of deleted local reference"); |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1760 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(inner1)); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame^] | 1761 | check_jni_abort_catcher.Check("use of deleted local reference"); |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1762 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(inner2)); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame^] | 1763 | check_jni_abort_catcher.Check("use of deleted local reference"); |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1764 | } |
| 1765 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1766 | TEST_F(JniInternalTest, NewGlobalRef_nullptr) { |
| 1767 | EXPECT_EQ(env_->NewGlobalRef(nullptr), nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1768 | } |
| 1769 | |
| 1770 | TEST_F(JniInternalTest, NewGlobalRef) { |
| 1771 | jstring s = env_->NewStringUTF(""); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1772 | ASSERT_NE(s, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1773 | jobject o = env_->NewGlobalRef(s); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1774 | EXPECT_NE(o, nullptr); |
| 1775 | EXPECT_NE(o, s); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1776 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1777 | EXPECT_EQ(env_->GetObjectRefType(o), JNIGlobalRefType); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1778 | } |
| 1779 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1780 | TEST_F(JniInternalTest, DeleteGlobalRef_nullptr) { |
| 1781 | env_->DeleteGlobalRef(nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1782 | } |
| 1783 | |
| 1784 | TEST_F(JniInternalTest, DeleteGlobalRef) { |
| 1785 | jstring s = env_->NewStringUTF(""); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1786 | ASSERT_NE(s, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1787 | |
| 1788 | jobject o = env_->NewGlobalRef(s); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1789 | ASSERT_NE(o, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1790 | env_->DeleteGlobalRef(o); |
| 1791 | |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 1792 | // Currently, deleting an already-deleted reference is just a CheckJNI warning. |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1793 | { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1794 | bool old_check_jni = vm_->SetCheckJniEnabled(false); |
| 1795 | { |
| 1796 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 1797 | env_->DeleteGlobalRef(o); |
| 1798 | } |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1799 | CheckJniAbortCatcher check_jni_abort_catcher; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1800 | EXPECT_FALSE(vm_->SetCheckJniEnabled(true)); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1801 | env_->DeleteGlobalRef(o); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame^] | 1802 | std::string expected(StringPrintf("use of deleted global reference %p", o)); |
Elliott Hughes | a9137c6 | 2013-01-09 10:55:21 -0800 | [diff] [blame] | 1803 | check_jni_abort_catcher.Check(expected.c_str()); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1804 | EXPECT_TRUE(vm_->SetCheckJniEnabled(old_check_jni)); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1805 | } |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1806 | |
| 1807 | jobject o1 = env_->NewGlobalRef(s); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1808 | ASSERT_NE(o1, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1809 | jobject o2 = env_->NewGlobalRef(s); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1810 | ASSERT_NE(o2, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1811 | |
| 1812 | env_->DeleteGlobalRef(o1); |
| 1813 | env_->DeleteGlobalRef(o2); |
| 1814 | } |
| 1815 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1816 | TEST_F(JniInternalTest, NewWeakGlobalRef_nullptr) { |
| 1817 | EXPECT_EQ(env_->NewWeakGlobalRef(nullptr), nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1818 | } |
| 1819 | |
| 1820 | TEST_F(JniInternalTest, NewWeakGlobalRef) { |
| 1821 | jstring s = env_->NewStringUTF(""); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1822 | ASSERT_NE(s, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1823 | jobject o = env_->NewWeakGlobalRef(s); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1824 | EXPECT_NE(o, nullptr); |
| 1825 | EXPECT_NE(o, s); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1826 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1827 | EXPECT_EQ(env_->GetObjectRefType(o), JNIWeakGlobalRefType); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1828 | } |
| 1829 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1830 | TEST_F(JniInternalTest, DeleteWeakGlobalRef_nullptr) { |
| 1831 | env_->DeleteWeakGlobalRef(nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1832 | } |
| 1833 | |
| 1834 | TEST_F(JniInternalTest, DeleteWeakGlobalRef) { |
| 1835 | jstring s = env_->NewStringUTF(""); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1836 | ASSERT_NE(s, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1837 | |
| 1838 | jobject o = env_->NewWeakGlobalRef(s); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1839 | ASSERT_NE(o, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1840 | env_->DeleteWeakGlobalRef(o); |
| 1841 | |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 1842 | // Currently, deleting an already-deleted reference is just a CheckJNI warning. |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1843 | { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1844 | bool old_check_jni = vm_->SetCheckJniEnabled(false); |
| 1845 | { |
| 1846 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 1847 | env_->DeleteWeakGlobalRef(o); |
| 1848 | } |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1849 | CheckJniAbortCatcher check_jni_abort_catcher; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1850 | EXPECT_FALSE(vm_->SetCheckJniEnabled(true)); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1851 | env_->DeleteWeakGlobalRef(o); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame^] | 1852 | std::string expected(StringPrintf("use of deleted weak global reference %p", o)); |
Elliott Hughes | a9137c6 | 2013-01-09 10:55:21 -0800 | [diff] [blame] | 1853 | check_jni_abort_catcher.Check(expected.c_str()); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1854 | EXPECT_TRUE(vm_->SetCheckJniEnabled(old_check_jni)); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1855 | } |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1856 | |
| 1857 | jobject o1 = env_->NewWeakGlobalRef(s); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1858 | ASSERT_NE(o1, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1859 | jobject o2 = env_->NewWeakGlobalRef(s); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1860 | ASSERT_NE(o2, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1861 | |
| 1862 | env_->DeleteWeakGlobalRef(o1); |
| 1863 | env_->DeleteWeakGlobalRef(o2); |
| 1864 | } |
| 1865 | |
Alexei Zavjalov | 3a1444c | 2014-06-25 16:04:55 +0700 | [diff] [blame] | 1866 | TEST_F(JniInternalTest, ExceptionDescribe) { |
| 1867 | // This checks how ExceptionDescribe handles call without exception. |
| 1868 | env_->ExceptionClear(); |
| 1869 | env_->ExceptionDescribe(); |
| 1870 | } |
| 1871 | |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1872 | TEST_F(JniInternalTest, Throw) { |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1873 | jclass exception_class = env_->FindClass("java/lang/RuntimeException"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1874 | ASSERT_TRUE(exception_class != nullptr); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1875 | jthrowable exception = reinterpret_cast<jthrowable>(env_->AllocObject(exception_class)); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1876 | ASSERT_TRUE(exception != nullptr); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1877 | |
| 1878 | EXPECT_EQ(JNI_OK, env_->Throw(exception)); |
| 1879 | EXPECT_TRUE(env_->ExceptionCheck()); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1880 | jthrowable thrown_exception = env_->ExceptionOccurred(); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1881 | env_->ExceptionClear(); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1882 | EXPECT_TRUE(env_->IsSameObject(exception, thrown_exception)); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1883 | |
| 1884 | // Bad argument. |
| 1885 | bool old_check_jni = vm_->SetCheckJniEnabled(false); |
| 1886 | EXPECT_EQ(JNI_ERR, env_->Throw(nullptr)); |
| 1887 | EXPECT_FALSE(vm_->SetCheckJniEnabled(true)); |
| 1888 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 1889 | EXPECT_EQ(JNI_ERR, env_->Throw(nullptr)); |
| 1890 | check_jni_abort_catcher.Check("Throw received NULL jthrowable"); |
| 1891 | EXPECT_TRUE(vm_->SetCheckJniEnabled(old_check_jni)); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1892 | } |
| 1893 | |
| 1894 | TEST_F(JniInternalTest, ThrowNew) { |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1895 | jclass exception_class = env_->FindClass("java/lang/RuntimeException"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1896 | ASSERT_TRUE(exception_class != nullptr); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1897 | |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 1898 | jthrowable thrown_exception; |
| 1899 | |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1900 | EXPECT_EQ(JNI_OK, env_->ThrowNew(exception_class, "hello world")); |
| 1901 | EXPECT_TRUE(env_->ExceptionCheck()); |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 1902 | thrown_exception = env_->ExceptionOccurred(); |
| 1903 | env_->ExceptionClear(); |
| 1904 | EXPECT_TRUE(env_->IsInstanceOf(thrown_exception, exception_class)); |
| 1905 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1906 | EXPECT_EQ(JNI_OK, env_->ThrowNew(exception_class, nullptr)); |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 1907 | EXPECT_TRUE(env_->ExceptionCheck()); |
| 1908 | thrown_exception = env_->ExceptionOccurred(); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1909 | env_->ExceptionClear(); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1910 | EXPECT_TRUE(env_->IsInstanceOf(thrown_exception, exception_class)); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1911 | |
| 1912 | // Bad argument. |
| 1913 | bool old_check_jni = vm_->SetCheckJniEnabled(false); |
| 1914 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 1915 | EXPECT_EQ(JNI_ERR, env_->ThrowNew(nullptr, nullptr)); |
| 1916 | check_jni_abort_catcher.Check("c == null"); |
| 1917 | EXPECT_FALSE(vm_->SetCheckJniEnabled(true)); |
| 1918 | EXPECT_EQ(JNI_ERR, env_->ThrowNew(nullptr, nullptr)); |
| 1919 | check_jni_abort_catcher.Check("ThrowNew received NULL jclass"); |
| 1920 | EXPECT_TRUE(vm_->SetCheckJniEnabled(old_check_jni)); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1921 | } |
| 1922 | |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 1923 | TEST_F(JniInternalTest, NewDirectBuffer_GetDirectBufferAddress_GetDirectBufferCapacity) { |
| 1924 | // Start runtime. |
| 1925 | Thread* self = Thread::Current(); |
| 1926 | self->TransitionFromSuspendedToRunnable(); |
| 1927 | MakeExecutable(nullptr, "java.lang.Class"); |
| 1928 | MakeExecutable(nullptr, "java.lang.Object"); |
| 1929 | MakeExecutable(nullptr, "java.nio.DirectByteBuffer"); |
| 1930 | MakeExecutable(nullptr, "java.nio.MemoryBlock"); |
| 1931 | MakeExecutable(nullptr, "java.nio.MemoryBlock$UnmanagedBlock"); |
| 1932 | MakeExecutable(nullptr, "java.nio.MappedByteBuffer"); |
| 1933 | MakeExecutable(nullptr, "java.nio.ByteBuffer"); |
| 1934 | MakeExecutable(nullptr, "java.nio.Buffer"); |
| 1935 | // TODO: we only load a dex file here as starting the runtime relies upon it. |
| 1936 | const char* class_name = "StaticLeafMethods"; |
| 1937 | LoadDex(class_name); |
| 1938 | bool started = runtime_->Start(); |
| 1939 | ASSERT_TRUE(started); |
| 1940 | |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1941 | jclass buffer_class = env_->FindClass("java/nio/Buffer"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1942 | ASSERT_NE(buffer_class, nullptr); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1943 | |
| 1944 | char bytes[1024]; |
| 1945 | jobject buffer = env_->NewDirectByteBuffer(bytes, sizeof(bytes)); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1946 | ASSERT_NE(buffer, nullptr); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1947 | ASSERT_TRUE(env_->IsInstanceOf(buffer, buffer_class)); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1948 | ASSERT_EQ(env_->GetDirectBufferAddress(buffer), bytes); |
Ian Rogers | 1ee9935 | 2014-05-14 14:38:16 -0700 | [diff] [blame] | 1949 | ASSERT_EQ(env_->GetDirectBufferCapacity(buffer), static_cast<jlong>(sizeof(bytes))); |
Brian Carlstrom | 45d26c8 | 2014-06-24 23:36:28 -0700 | [diff] [blame] | 1950 | |
| 1951 | { |
| 1952 | CheckJniAbortCatcher check_jni_abort_catcher; |
Brian Carlstrom | 85a9336 | 2014-06-25 09:30:52 -0700 | [diff] [blame] | 1953 | env_->NewDirectByteBuffer(bytes, static_cast<jlong>(INT_MAX) + 1); |
Brian Carlstrom | 45d26c8 | 2014-06-24 23:36:28 -0700 | [diff] [blame] | 1954 | check_jni_abort_catcher.Check("in call to NewDirectByteBuffer"); |
| 1955 | } |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1956 | } |
| 1957 | |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 1958 | TEST_F(JniInternalTest, MonitorEnterExit) { |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1959 | // Create an object to torture. |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 1960 | jclass object_class = env_->FindClass("java/lang/Object"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1961 | ASSERT_NE(object_class, nullptr); |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 1962 | jobject object = env_->AllocObject(object_class); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1963 | ASSERT_NE(object, nullptr); |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 1964 | |
| 1965 | // Expected class of exceptions |
| 1966 | jclass imse_class = env_->FindClass("java/lang/IllegalMonitorStateException"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1967 | ASSERT_NE(imse_class, nullptr); |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 1968 | |
| 1969 | jthrowable thrown_exception; |
| 1970 | |
| 1971 | // Unlock of unowned monitor |
| 1972 | env_->MonitorExit(object); |
| 1973 | EXPECT_TRUE(env_->ExceptionCheck()); |
| 1974 | thrown_exception = env_->ExceptionOccurred(); |
| 1975 | env_->ExceptionClear(); |
| 1976 | EXPECT_TRUE(env_->IsInstanceOf(thrown_exception, imse_class)); |
| 1977 | |
| 1978 | // Lock of unowned monitor |
| 1979 | env_->MonitorEnter(object); |
| 1980 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 1981 | // Regular unlock |
| 1982 | env_->MonitorExit(object); |
| 1983 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 1984 | |
| 1985 | // Recursively lock a lot |
| 1986 | size_t max_recursive_lock = 1024; |
| 1987 | for (size_t i = 0; i < max_recursive_lock; i++) { |
| 1988 | env_->MonitorEnter(object); |
| 1989 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 1990 | } |
| 1991 | // Recursively unlock a lot |
| 1992 | for (size_t i = 0; i < max_recursive_lock; i++) { |
| 1993 | env_->MonitorExit(object); |
| 1994 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 1995 | } |
| 1996 | |
| 1997 | // Unlock of unowned monitor |
| 1998 | env_->MonitorExit(object); |
| 1999 | EXPECT_TRUE(env_->ExceptionCheck()); |
| 2000 | thrown_exception = env_->ExceptionOccurred(); |
| 2001 | env_->ExceptionClear(); |
| 2002 | EXPECT_TRUE(env_->IsInstanceOf(thrown_exception, imse_class)); |
Elliott Hughes | a92853e | 2012-02-07 16:09:27 -0800 | [diff] [blame] | 2003 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 2004 | // It's an error to call MonitorEnter or MonitorExit on nullptr. |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 2005 | { |
| 2006 | CheckJniAbortCatcher check_jni_abort_catcher; |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 2007 | env_->MonitorEnter(nullptr); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 2008 | check_jni_abort_catcher.Check("in call to MonitorEnter"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 2009 | env_->MonitorExit(nullptr); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 2010 | check_jni_abort_catcher.Check("in call to MonitorExit"); |
| 2011 | } |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 2012 | } |
| 2013 | |
Brian Carlstrom | 4d57143 | 2012-05-16 00:21:41 -0700 | [diff] [blame] | 2014 | TEST_F(JniInternalTest, DetachCurrentThread) { |
| 2015 | CleanUpJniEnv(); // cleanup now so TearDown won't have junk from wrong JNIEnv |
| 2016 | jint ok = vm_->DetachCurrentThread(); |
| 2017 | EXPECT_EQ(JNI_OK, ok); |
| 2018 | |
| 2019 | jint err = vm_->DetachCurrentThread(); |
| 2020 | EXPECT_EQ(JNI_ERR, err); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 2021 | vm_->AttachCurrentThread(&env_, nullptr); // need attached thread for CommonRuntimeTest::TearDown |
Brian Carlstrom | 4d57143 | 2012-05-16 00:21:41 -0700 | [diff] [blame] | 2022 | } |
| 2023 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2024 | } // namespace art |