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" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 20 | #include "mirror/art_method-inl.h" |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 21 | #include "ScopedLocalRef.h" |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 22 | |
| 23 | namespace art { |
| 24 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 25 | // TODO: Convert to CommonRuntimeTest. Currently MakeExecutable is used. |
| 26 | class JniInternalTest : public CommonCompilerTest { |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 27 | protected: |
| 28 | virtual void SetUp() { |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 29 | CommonCompilerTest::SetUp(); |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 30 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 31 | vm_ = Runtime::Current()->GetJavaVM(); |
| 32 | |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 33 | // Turn on -verbose:jni for the JNI tests. |
Ian Rogers | 7971363 | 2013-08-21 19:06:15 -0700 | [diff] [blame] | 34 | // gLogVerbosity.jni = true; |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 35 | |
Brian Carlstrom | 4d57143 | 2012-05-16 00:21:41 -0700 | [diff] [blame] | 36 | vm_->AttachCurrentThread(&env_, NULL); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 37 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 38 | ScopedLocalRef<jclass> aioobe(env_, |
| 39 | env_->FindClass("java/lang/ArrayIndexOutOfBoundsException")); |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 40 | CHECK(aioobe.get() != NULL); |
| 41 | aioobe_ = reinterpret_cast<jclass>(env_->NewGlobalRef(aioobe.get())); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 42 | |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 43 | ScopedLocalRef<jclass> ase(env_, env_->FindClass("java/lang/ArrayStoreException")); |
| 44 | CHECK(ase.get() != NULL); |
| 45 | ase_ = reinterpret_cast<jclass>(env_->NewGlobalRef(ase.get())); |
| 46 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 47 | ScopedLocalRef<jclass> sioobe(env_, |
| 48 | env_->FindClass("java/lang/StringIndexOutOfBoundsException")); |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 49 | CHECK(sioobe.get() != NULL); |
| 50 | sioobe_ = reinterpret_cast<jclass>(env_->NewGlobalRef(sioobe.get())); |
| 51 | } |
| 52 | |
Brian Carlstrom | 4d57143 | 2012-05-16 00:21:41 -0700 | [diff] [blame] | 53 | void CleanUpJniEnv() { |
| 54 | if (aioobe_ != NULL) { |
| 55 | env_->DeleteGlobalRef(aioobe_); |
| 56 | aioobe_ = NULL; |
| 57 | } |
| 58 | if (ase_ != NULL) { |
| 59 | env_->DeleteGlobalRef(ase_); |
| 60 | ase_ = NULL; |
| 61 | } |
| 62 | if (sioobe_ != NULL) { |
| 63 | env_->DeleteGlobalRef(sioobe_); |
| 64 | sioobe_ = NULL; |
| 65 | } |
| 66 | } |
| 67 | |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 68 | virtual void TearDown() OVERRIDE { |
Brian Carlstrom | 4d57143 | 2012-05-16 00:21:41 -0700 | [diff] [blame] | 69 | CleanUpJniEnv(); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 70 | CommonCompilerTest::TearDown(); |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 71 | } |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 72 | |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 73 | jclass GetPrimitiveClass(char descriptor) { |
| 74 | ScopedObjectAccess soa(env_); |
| 75 | mirror::Class* c = class_linker_->FindPrimitiveClass(descriptor); |
| 76 | CHECK(c != nullptr); |
| 77 | return soa.AddLocalReference<jclass>(c); |
| 78 | } |
| 79 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 80 | JavaVMExt* vm_; |
Brian Carlstrom | 4d57143 | 2012-05-16 00:21:41 -0700 | [diff] [blame] | 81 | JNIEnv* env_; |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 82 | jclass aioobe_; |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 83 | jclass ase_; |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 84 | jclass sioobe_; |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 85 | }; |
| 86 | |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 87 | TEST_F(JniInternalTest, AllocObject) { |
| 88 | jclass c = env_->FindClass("java/lang/String"); |
| 89 | ASSERT_TRUE(c != NULL); |
| 90 | jobject o = env_->AllocObject(c); |
| 91 | ASSERT_TRUE(o != NULL); |
| 92 | |
| 93 | // We have an instance of the class we asked for... |
| 94 | ASSERT_TRUE(env_->IsInstanceOf(o, c)); |
| 95 | // ...whose fields haven't been initialized because |
| 96 | // we didn't call a constructor. |
| 97 | ASSERT_EQ(0, env_->GetIntField(o, env_->GetFieldID(c, "count", "I"))); |
| 98 | ASSERT_EQ(0, env_->GetIntField(o, env_->GetFieldID(c, "offset", "I"))); |
| 99 | ASSERT_TRUE(env_->GetObjectField(o, env_->GetFieldID(c, "value", "[C")) == NULL); |
| 100 | } |
| 101 | |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 102 | TEST_F(JniInternalTest, GetVersion) { |
| 103 | ASSERT_EQ(JNI_VERSION_1_6, env_->GetVersion()); |
| 104 | } |
| 105 | |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 106 | #define EXPECT_CLASS_FOUND(NAME) \ |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 107 | EXPECT_TRUE(env_->FindClass(NAME) != NULL); \ |
| 108 | EXPECT_FALSE(env_->ExceptionCheck()) |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 109 | |
| 110 | #define EXPECT_CLASS_NOT_FOUND(NAME) \ |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 111 | EXPECT_TRUE(env_->FindClass(NAME) == NULL); \ |
| 112 | EXPECT_TRUE(env_->ExceptionCheck()); \ |
| 113 | env_->ExceptionClear() |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 114 | |
| 115 | TEST_F(JniInternalTest, FindClass) { |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 116 | // Reference types... |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 117 | EXPECT_CLASS_FOUND("java/lang/String"); |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 118 | // ...for arrays too, where you must include "L;". |
| 119 | EXPECT_CLASS_FOUND("[Ljava/lang/String;"); |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 120 | // Primitive arrays are okay too, if the primitive type is valid. |
| 121 | EXPECT_CLASS_FOUND("[C"); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 122 | |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 123 | { |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 124 | // We support . as well as / for compatibility, if -Xcheck:jni is off. |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 125 | CheckJniAbortCatcher check_jni_abort_catcher; |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 126 | EXPECT_CLASS_FOUND("java.lang.String"); |
Elliott Hughes | 56ef042 | 2012-06-19 14:35:04 -0700 | [diff] [blame] | 127 | check_jni_abort_catcher.Check("illegal class name 'java.lang.String'"); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 128 | EXPECT_CLASS_NOT_FOUND("Ljava.lang.String;"); |
Elliott Hughes | 56ef042 | 2012-06-19 14:35:04 -0700 | [diff] [blame] | 129 | check_jni_abort_catcher.Check("illegal class name 'Ljava.lang.String;'"); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 130 | EXPECT_CLASS_FOUND("[Ljava.lang.String;"); |
Elliott Hughes | 56ef042 | 2012-06-19 14:35:04 -0700 | [diff] [blame] | 131 | check_jni_abort_catcher.Check("illegal class name '[Ljava.lang.String;'"); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 132 | EXPECT_CLASS_NOT_FOUND("[java.lang.String"); |
Elliott Hughes | 56ef042 | 2012-06-19 14:35:04 -0700 | [diff] [blame] | 133 | check_jni_abort_catcher.Check("illegal class name '[java.lang.String'"); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 134 | |
| 135 | // You can't include the "L;" in a JNI class descriptor. |
| 136 | EXPECT_CLASS_NOT_FOUND("Ljava/lang/String;"); |
Elliott Hughes | 56ef042 | 2012-06-19 14:35:04 -0700 | [diff] [blame] | 137 | check_jni_abort_catcher.Check("illegal class name 'Ljava/lang/String;'"); |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 138 | |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 139 | // But you must include it for an array of any reference type. |
| 140 | EXPECT_CLASS_NOT_FOUND("[java/lang/String"); |
Elliott Hughes | 56ef042 | 2012-06-19 14:35:04 -0700 | [diff] [blame] | 141 | check_jni_abort_catcher.Check("illegal class name '[java/lang/String'"); |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 142 | |
| 143 | EXPECT_CLASS_NOT_FOUND("[K"); |
Elliott Hughes | 56ef042 | 2012-06-19 14:35:04 -0700 | [diff] [blame] | 144 | check_jni_abort_catcher.Check("illegal class name '[K'"); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 145 | } |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 146 | |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 147 | // But primitive types aren't allowed... |
| 148 | EXPECT_CLASS_NOT_FOUND("C"); |
| 149 | EXPECT_CLASS_NOT_FOUND("K"); |
| 150 | } |
| 151 | |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 152 | #define EXPECT_EXCEPTION(exception_class) \ |
| 153 | do { \ |
| 154 | EXPECT_TRUE(env_->ExceptionCheck()); \ |
| 155 | jthrowable exception = env_->ExceptionOccurred(); \ |
| 156 | EXPECT_NE(static_cast<jthrowable>(NULL), exception); \ |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 157 | env_->ExceptionClear(); \ |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 158 | EXPECT_TRUE(env_->IsInstanceOf(exception, exception_class)); \ |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 159 | } while (false) |
| 160 | |
| 161 | TEST_F(JniInternalTest, GetFieldID) { |
| 162 | jclass jlnsfe = env_->FindClass("java/lang/NoSuchFieldError"); |
| 163 | ASSERT_TRUE(jlnsfe != NULL); |
| 164 | jclass c = env_->FindClass("java/lang/String"); |
| 165 | ASSERT_TRUE(c != NULL); |
| 166 | |
| 167 | // Wrong type. |
| 168 | jfieldID fid = env_->GetFieldID(c, "count", "J"); |
| 169 | EXPECT_EQ(static_cast<jfieldID>(NULL), fid); |
| 170 | EXPECT_EXCEPTION(jlnsfe); |
| 171 | |
Ian Rogers | b17d08b | 2011-09-02 16:16:49 -0700 | [diff] [blame] | 172 | // Wrong type where type doesn't exist. |
| 173 | fid = env_->GetFieldID(c, "count", "Lrod/jane/freddy;"); |
| 174 | EXPECT_EQ(static_cast<jfieldID>(NULL), fid); |
| 175 | EXPECT_EXCEPTION(jlnsfe); |
| 176 | |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 177 | // Wrong name. |
| 178 | fid = env_->GetFieldID(c, "Count", "I"); |
| 179 | EXPECT_EQ(static_cast<jfieldID>(NULL), fid); |
| 180 | EXPECT_EXCEPTION(jlnsfe); |
| 181 | |
| 182 | // Good declared field lookup. |
| 183 | fid = env_->GetFieldID(c, "count", "I"); |
| 184 | EXPECT_NE(static_cast<jfieldID>(NULL), fid); |
| 185 | EXPECT_TRUE(fid != NULL); |
| 186 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 187 | |
| 188 | // Good superclass field lookup. |
| 189 | c = env_->FindClass("java/lang/StringBuilder"); |
| 190 | fid = env_->GetFieldID(c, "count", "I"); |
| 191 | EXPECT_NE(static_cast<jfieldID>(NULL), fid); |
| 192 | EXPECT_TRUE(fid != NULL); |
| 193 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 194 | |
| 195 | // Not instance. |
| 196 | fid = env_->GetFieldID(c, "CASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;"); |
| 197 | EXPECT_EQ(static_cast<jfieldID>(NULL), fid); |
| 198 | EXPECT_EXCEPTION(jlnsfe); |
| 199 | } |
| 200 | |
| 201 | TEST_F(JniInternalTest, GetStaticFieldID) { |
| 202 | jclass jlnsfe = env_->FindClass("java/lang/NoSuchFieldError"); |
| 203 | ASSERT_TRUE(jlnsfe != NULL); |
| 204 | jclass c = env_->FindClass("java/lang/String"); |
| 205 | ASSERT_TRUE(c != NULL); |
| 206 | |
| 207 | // Wrong type. |
| 208 | jfieldID fid = env_->GetStaticFieldID(c, "CASE_INSENSITIVE_ORDER", "J"); |
| 209 | EXPECT_EQ(static_cast<jfieldID>(NULL), fid); |
| 210 | EXPECT_EXCEPTION(jlnsfe); |
| 211 | |
Ian Rogers | b17d08b | 2011-09-02 16:16:49 -0700 | [diff] [blame] | 212 | // Wrong type where type doesn't exist. |
| 213 | fid = env_->GetStaticFieldID(c, "CASE_INSENSITIVE_ORDER", "Lrod/jane/freddy;"); |
| 214 | EXPECT_EQ(static_cast<jfieldID>(NULL), fid); |
| 215 | EXPECT_EXCEPTION(jlnsfe); |
| 216 | |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 217 | // Wrong name. |
| 218 | fid = env_->GetStaticFieldID(c, "cASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;"); |
| 219 | EXPECT_EQ(static_cast<jfieldID>(NULL), fid); |
| 220 | EXPECT_EXCEPTION(jlnsfe); |
| 221 | |
| 222 | // Good declared field lookup. |
| 223 | fid = env_->GetStaticFieldID(c, "CASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;"); |
| 224 | EXPECT_NE(static_cast<jfieldID>(NULL), fid); |
| 225 | EXPECT_TRUE(fid != NULL); |
| 226 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 227 | |
| 228 | // Not static. |
| 229 | fid = env_->GetStaticFieldID(c, "count", "I"); |
| 230 | EXPECT_EQ(static_cast<jfieldID>(NULL), fid); |
| 231 | EXPECT_EXCEPTION(jlnsfe); |
| 232 | } |
| 233 | |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 234 | TEST_F(JniInternalTest, GetMethodID) { |
| 235 | jclass jlobject = env_->FindClass("java/lang/Object"); |
| 236 | jclass jlstring = env_->FindClass("java/lang/String"); |
| 237 | jclass jlnsme = env_->FindClass("java/lang/NoSuchMethodError"); |
| 238 | |
| 239 | // Sanity check that no exceptions are pending |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 240 | ASSERT_FALSE(env_->ExceptionCheck()); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 241 | |
| 242 | // Check that java.lang.Object.foo() doesn't exist and NoSuchMethodError is |
| 243 | // a pending exception |
| 244 | jmethodID method = env_->GetMethodID(jlobject, "foo", "()V"); |
| 245 | EXPECT_EQ(static_cast<jmethodID>(NULL), method); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 246 | EXPECT_EXCEPTION(jlnsme); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 247 | |
| 248 | // Check that java.lang.Object.equals() does exist |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 249 | method = env_->GetMethodID(jlobject, "equals", "(Ljava/lang/Object;)Z"); |
| 250 | EXPECT_NE(static_cast<jmethodID>(NULL), method); |
| 251 | EXPECT_FALSE(env_->ExceptionCheck()); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 252 | |
| 253 | // Check that GetMethodID for java.lang.String.valueOf(int) fails as the |
| 254 | // method is static |
| 255 | method = env_->GetMethodID(jlstring, "valueOf", "(I)Ljava/lang/String;"); |
| 256 | EXPECT_EQ(static_cast<jmethodID>(NULL), method); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 257 | EXPECT_EXCEPTION(jlnsme); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 258 | |
| 259 | // Check that GetMethodID for java.lang.NoSuchMethodError.<init>(String) finds the constructor |
| 260 | method = env_->GetMethodID(jlnsme, "<init>", "(Ljava/lang/String;)V"); |
| 261 | EXPECT_NE(static_cast<jmethodID>(NULL), method); |
| 262 | EXPECT_FALSE(env_->ExceptionCheck()); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | TEST_F(JniInternalTest, GetStaticMethodID) { |
| 266 | jclass jlobject = env_->FindClass("java/lang/Object"); |
| 267 | jclass jlnsme = env_->FindClass("java/lang/NoSuchMethodError"); |
| 268 | |
| 269 | // Sanity check that no exceptions are pending |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 270 | ASSERT_FALSE(env_->ExceptionCheck()); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 271 | |
| 272 | // Check that java.lang.Object.foo() doesn't exist and NoSuchMethodError is |
| 273 | // a pending exception |
| 274 | jmethodID method = env_->GetStaticMethodID(jlobject, "foo", "()V"); |
| 275 | EXPECT_EQ(static_cast<jmethodID>(NULL), method); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 276 | EXPECT_EXCEPTION(jlnsme); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 277 | |
| 278 | // Check that GetStaticMethodID for java.lang.Object.equals(Object) fails as |
| 279 | // the method is not static |
| 280 | method = env_->GetStaticMethodID(jlobject, "equals", "(Ljava/lang/Object;)Z"); |
| 281 | EXPECT_EQ(static_cast<jmethodID>(NULL), method); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 282 | EXPECT_EXCEPTION(jlnsme); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 283 | |
| 284 | // Check that java.lang.String.valueOf(int) does exist |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 285 | jclass jlstring = env_->FindClass("java/lang/String"); |
| 286 | method = env_->GetStaticMethodID(jlstring, "valueOf", |
| 287 | "(I)Ljava/lang/String;"); |
| 288 | EXPECT_NE(static_cast<jmethodID>(NULL), method); |
| 289 | EXPECT_FALSE(env_->ExceptionCheck()); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 292 | TEST_F(JniInternalTest, FromReflectedField_ToReflectedField) { |
| 293 | jclass jlrField = env_->FindClass("java/lang/reflect/Field"); |
| 294 | jclass c = env_->FindClass("java/lang/String"); |
| 295 | ASSERT_TRUE(c != NULL); |
| 296 | jfieldID fid = env_->GetFieldID(c, "count", "I"); |
| 297 | ASSERT_TRUE(fid != NULL); |
| 298 | // Turn the fid into a java.lang.reflect.Field... |
| 299 | jobject field = env_->ToReflectedField(c, fid, JNI_FALSE); |
| 300 | ASSERT_TRUE(c != NULL); |
| 301 | ASSERT_TRUE(env_->IsInstanceOf(field, jlrField)); |
| 302 | // ...and back again. |
| 303 | jfieldID fid2 = env_->FromReflectedField(field); |
| 304 | ASSERT_TRUE(fid2 != NULL); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 305 | // Make sure we can actually use it. |
| 306 | jstring s = env_->NewStringUTF("poop"); |
| 307 | ASSERT_EQ(4, env_->GetIntField(s, fid2)); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | TEST_F(JniInternalTest, FromReflectedMethod_ToReflectedMethod) { |
| 311 | jclass jlrMethod = env_->FindClass("java/lang/reflect/Method"); |
| 312 | jclass c = env_->FindClass("java/lang/String"); |
| 313 | ASSERT_TRUE(c != NULL); |
| 314 | jmethodID mid = env_->GetMethodID(c, "length", "()I"); |
| 315 | ASSERT_TRUE(mid != NULL); |
| 316 | // Turn the mid into a java.lang.reflect.Method... |
| 317 | jobject method = env_->ToReflectedMethod(c, mid, JNI_FALSE); |
| 318 | ASSERT_TRUE(c != NULL); |
| 319 | ASSERT_TRUE(env_->IsInstanceOf(method, jlrMethod)); |
| 320 | // ...and back again. |
| 321 | jmethodID mid2 = env_->FromReflectedMethod(method); |
| 322 | ASSERT_TRUE(mid2 != NULL); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 323 | // Make sure we can actually use it. |
| 324 | jstring s = env_->NewStringUTF("poop"); |
Ian Rogers | 5d27faf | 2014-05-02 17:17:18 -0700 | [diff] [blame^] | 325 | ASSERT_EQ(4, env_->CallIntMethod(s, mid2)); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 326 | } |
| 327 | |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 328 | void BogusMethod() { |
| 329 | // You can't pass NULL function pointers to RegisterNatives. |
| 330 | } |
| 331 | |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 332 | TEST_F(JniInternalTest, RegisterNatives) { |
| 333 | jclass jlobject = env_->FindClass("java/lang/Object"); |
| 334 | jclass jlnsme = env_->FindClass("java/lang/NoSuchMethodError"); |
| 335 | |
| 336 | // Sanity check that no exceptions are pending |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 337 | ASSERT_FALSE(env_->ExceptionCheck()); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 338 | |
| 339 | // Check that registering to a non-existent java.lang.Object.foo() causes a |
| 340 | // NoSuchMethodError |
| 341 | { |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 342 | JNINativeMethod methods[] = { { "foo", "()V", NULL } }; |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 343 | env_->RegisterNatives(jlobject, methods, 1); |
| 344 | } |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 345 | EXPECT_EXCEPTION(jlnsme); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 346 | |
| 347 | // Check that registering non-native methods causes a NoSuchMethodError |
| 348 | { |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 349 | JNINativeMethod methods[] = { { "equals", "(Ljava/lang/Object;)Z", NULL } }; |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 350 | env_->RegisterNatives(jlobject, methods, 1); |
| 351 | } |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 352 | EXPECT_EXCEPTION(jlnsme); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 353 | |
| 354 | // Check that registering native methods is successful |
| 355 | { |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 356 | JNINativeMethod methods[] = { { "notify", "()V", reinterpret_cast<void*>(BogusMethod) } }; |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 357 | env_->RegisterNatives(jlobject, methods, 1); |
| 358 | } |
| 359 | EXPECT_FALSE(env_->ExceptionCheck()); |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 360 | |
| 361 | env_->UnregisterNatives(jlobject); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 362 | } |
| 363 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 364 | #define EXPECT_PRIMITIVE_ARRAY(new_fn, \ |
| 365 | get_region_fn, \ |
| 366 | set_region_fn, \ |
| 367 | get_elements_fn, \ |
| 368 | release_elements_fn, \ |
| 369 | scalar_type, \ |
| 370 | expected_class_descriptor) \ |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 371 | { \ |
| 372 | CheckJniAbortCatcher jni_abort_catcher; \ |
| 373 | /* Allocate an negative sized array and check it has the right failure type. */ \ |
| 374 | env_->new_fn(-1); \ |
| 375 | jni_abort_catcher.Check("negative array length: -1"); \ |
| 376 | env_->new_fn(std::numeric_limits<jint>::min()); \ |
| 377 | jni_abort_catcher.Check("negative array length: -2147483648"); \ |
| 378 | } \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 379 | jsize size = 4; \ |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 380 | \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 381 | /* Allocate an array and check it has the right type and length. */ \ |
| 382 | scalar_type ## Array a = env_->new_fn(size); \ |
| 383 | EXPECT_TRUE(a != NULL); \ |
| 384 | EXPECT_TRUE(env_->IsInstanceOf(a, env_->FindClass(expected_class_descriptor))); \ |
| 385 | EXPECT_EQ(size, env_->GetArrayLength(a)); \ |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 386 | \ |
| 387 | /* GetPrimitiveArrayRegion/SetPrimitiveArrayRegion */ \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 388 | /* AIOOBE for negative start offset. */ \ |
| 389 | env_->get_region_fn(a, -1, 1, NULL); \ |
| 390 | EXPECT_EXCEPTION(aioobe_); \ |
| 391 | env_->set_region_fn(a, -1, 1, NULL); \ |
| 392 | EXPECT_EXCEPTION(aioobe_); \ |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 393 | \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 394 | /* AIOOBE for negative length. */ \ |
| 395 | env_->get_region_fn(a, 0, -1, NULL); \ |
| 396 | EXPECT_EXCEPTION(aioobe_); \ |
| 397 | env_->set_region_fn(a, 0, -1, NULL); \ |
| 398 | EXPECT_EXCEPTION(aioobe_); \ |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 399 | \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 400 | /* AIOOBE for buffer overrun. */ \ |
| 401 | env_->get_region_fn(a, size - 1, size, NULL); \ |
| 402 | EXPECT_EXCEPTION(aioobe_); \ |
| 403 | env_->set_region_fn(a, size - 1, size, NULL); \ |
| 404 | EXPECT_EXCEPTION(aioobe_); \ |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 405 | \ |
| 406 | /* It's okay for the buffer to be NULL as long as the length is 0. */ \ |
| 407 | env_->get_region_fn(a, 2, 0, NULL); \ |
| 408 | /* Even if the offset is invalid... */ \ |
| 409 | env_->get_region_fn(a, 123, 0, NULL); \ |
| 410 | EXPECT_EXCEPTION(aioobe_); \ |
| 411 | \ |
| 412 | /* It's okay for the buffer to be NULL as long as the length is 0. */ \ |
| 413 | env_->set_region_fn(a, 2, 0, NULL); \ |
| 414 | /* Even if the offset is invalid... */ \ |
| 415 | env_->set_region_fn(a, 123, 0, NULL); \ |
| 416 | EXPECT_EXCEPTION(aioobe_); \ |
| 417 | \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 418 | /* Prepare a couple of buffers. */ \ |
Elliott Hughes | ee0fa76 | 2012-03-26 17:12:41 -0700 | [diff] [blame] | 419 | UniquePtr<scalar_type[]> src_buf(new scalar_type[size]); \ |
| 420 | UniquePtr<scalar_type[]> dst_buf(new scalar_type[size]); \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 421 | for (jsize i = 0; i < size; ++i) { src_buf[i] = scalar_type(i); } \ |
| 422 | 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] | 423 | \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 424 | /* Copy all of src_buf onto the heap. */ \ |
Elliott Hughes | ee0fa76 | 2012-03-26 17:12:41 -0700 | [diff] [blame] | 425 | env_->set_region_fn(a, 0, size, &src_buf[0]); \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 426 | /* Copy back only part. */ \ |
| 427 | env_->get_region_fn(a, 1, size - 2, &dst_buf[1]); \ |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 428 | EXPECT_NE(memcmp(&src_buf[0], &dst_buf[0], size * sizeof(scalar_type)), 0) \ |
| 429 | << "short copy equal"; \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 430 | /* Copy the missing pieces. */ \ |
Elliott Hughes | ee0fa76 | 2012-03-26 17:12:41 -0700 | [diff] [blame] | 431 | env_->get_region_fn(a, 0, 1, &dst_buf[0]); \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 432 | env_->get_region_fn(a, size - 1, 1, &dst_buf[size - 1]); \ |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 433 | EXPECT_EQ(memcmp(&src_buf[0], &dst_buf[0], size * sizeof(scalar_type)), 0) \ |
| 434 | << "fixed copy not equal"; \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 435 | /* Copy back the whole array. */ \ |
Elliott Hughes | ee0fa76 | 2012-03-26 17:12:41 -0700 | [diff] [blame] | 436 | env_->get_region_fn(a, 0, size, &dst_buf[0]); \ |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 437 | EXPECT_EQ(memcmp(&src_buf[0], &dst_buf[0], size * sizeof(scalar_type)), 0) \ |
| 438 | << "full copy not equal"; \ |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 439 | /* GetPrimitiveArrayCritical */ \ |
| 440 | void* v = env_->GetPrimitiveArrayCritical(a, NULL); \ |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 441 | EXPECT_EQ(memcmp(&src_buf[0], v, size * sizeof(scalar_type)), 0) \ |
| 442 | << "GetPrimitiveArrayCritical not equal"; \ |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 443 | env_->ReleasePrimitiveArrayCritical(a, v, 0); \ |
| 444 | /* GetXArrayElements */ \ |
| 445 | scalar_type* xs = env_->get_elements_fn(a, NULL); \ |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 446 | EXPECT_EQ(memcmp(&src_buf[0], xs, size * sizeof(scalar_type)), 0) \ |
| 447 | << # get_elements_fn " not equal"; \ |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 448 | env_->release_elements_fn(a, xs, 0); \ |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 449 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 450 | TEST_F(JniInternalTest, BooleanArrays) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 451 | EXPECT_PRIMITIVE_ARRAY(NewBooleanArray, GetBooleanArrayRegion, SetBooleanArrayRegion, |
| 452 | GetBooleanArrayElements, ReleaseBooleanArrayElements, jboolean, "[Z"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 453 | } |
| 454 | TEST_F(JniInternalTest, ByteArrays) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 455 | EXPECT_PRIMITIVE_ARRAY(NewByteArray, GetByteArrayRegion, SetByteArrayRegion, |
| 456 | GetByteArrayElements, ReleaseByteArrayElements, jbyte, "[B"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 457 | } |
| 458 | TEST_F(JniInternalTest, CharArrays) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 459 | EXPECT_PRIMITIVE_ARRAY(NewCharArray, GetCharArrayRegion, SetCharArrayRegion, |
| 460 | GetCharArrayElements, ReleaseCharArrayElements, jchar, "[C"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 461 | } |
| 462 | TEST_F(JniInternalTest, DoubleArrays) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 463 | EXPECT_PRIMITIVE_ARRAY(NewDoubleArray, GetDoubleArrayRegion, SetDoubleArrayRegion, |
| 464 | GetDoubleArrayElements, ReleaseDoubleArrayElements, jdouble, "[D"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 465 | } |
| 466 | TEST_F(JniInternalTest, FloatArrays) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 467 | EXPECT_PRIMITIVE_ARRAY(NewFloatArray, GetFloatArrayRegion, SetFloatArrayRegion, |
| 468 | GetFloatArrayElements, ReleaseFloatArrayElements, jfloat, "[F"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 469 | } |
| 470 | TEST_F(JniInternalTest, IntArrays) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 471 | EXPECT_PRIMITIVE_ARRAY(NewIntArray, GetIntArrayRegion, SetIntArrayRegion, |
| 472 | GetIntArrayElements, ReleaseIntArrayElements, jint, "[I"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 473 | } |
| 474 | TEST_F(JniInternalTest, LongArrays) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 475 | EXPECT_PRIMITIVE_ARRAY(NewLongArray, GetLongArrayRegion, SetLongArrayRegion, |
| 476 | GetLongArrayElements, ReleaseLongArrayElements, jlong, "[J"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 477 | } |
| 478 | TEST_F(JniInternalTest, ShortArrays) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 479 | EXPECT_PRIMITIVE_ARRAY(NewShortArray, GetShortArrayRegion, SetShortArrayRegion, |
| 480 | GetShortArrayElements, ReleaseShortArrayElements, jshort, "[S"); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 481 | } |
| 482 | |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 483 | TEST_F(JniInternalTest, NewObjectArray) { |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 484 | jclass element_class = env_->FindClass("java/lang/String"); |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 485 | ASSERT_TRUE(element_class != nullptr); |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 486 | jclass array_class = env_->FindClass("[Ljava/lang/String;"); |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 487 | ASSERT_TRUE(array_class != nullptr); |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 488 | |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 489 | jobjectArray a = env_->NewObjectArray(0, element_class, nullptr); |
| 490 | EXPECT_TRUE(a != nullptr); |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 491 | EXPECT_TRUE(env_->IsInstanceOf(a, array_class)); |
| 492 | EXPECT_EQ(0, env_->GetArrayLength(a)); |
| 493 | |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 494 | a = env_->NewObjectArray(1, element_class, nullptr); |
| 495 | EXPECT_TRUE(a != nullptr); |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 496 | EXPECT_TRUE(env_->IsInstanceOf(a, array_class)); |
| 497 | EXPECT_EQ(1, env_->GetArrayLength(a)); |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 498 | EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(a, 0), nullptr)); |
| 499 | } |
| 500 | |
| 501 | TEST_F(JniInternalTest, NewObjectArrayWithNegativeLength) { |
| 502 | jclass element_class = env_->FindClass("java/lang/String"); |
| 503 | ASSERT_TRUE(element_class != nullptr); |
| 504 | jclass array_class = env_->FindClass("[Ljava/lang/String;"); |
| 505 | ASSERT_TRUE(array_class != nullptr); |
| 506 | CheckJniAbortCatcher jni_abort_catcher; |
| 507 | |
| 508 | env_->NewObjectArray(-1, element_class, nullptr); |
| 509 | jni_abort_catcher.Check("negative array length: -1"); |
| 510 | |
| 511 | env_->NewObjectArray(std::numeric_limits<jint>::min(), element_class, nullptr); |
| 512 | jni_abort_catcher.Check("negative array length: -2147483648"); |
| 513 | } |
| 514 | |
| 515 | TEST_F(JniInternalTest, NewObjectArrayWithPrimitiveClasses) { |
| 516 | const char* primitive_descriptors = "VZBSCIJFD"; |
| 517 | const char* primitive_names[] = { |
| 518 | "void", "boolean", "byte", "short", "char", "int", "long", "float", "double" |
| 519 | }; |
| 520 | ASSERT_EQ(strlen(primitive_descriptors), arraysize(primitive_names)); |
| 521 | |
| 522 | CheckJniAbortCatcher jni_abort_catcher; |
| 523 | for (size_t i = 0; i < strlen(primitive_descriptors); ++i) { |
| 524 | jclass primitive_class = GetPrimitiveClass(primitive_descriptors[i]); |
| 525 | env_->NewObjectArray(1, primitive_class, nullptr); |
| 526 | std::string error_msg(StringPrintf("not an object type: %s", primitive_names[i])); |
| 527 | jni_abort_catcher.Check(error_msg.c_str()); |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | TEST_F(JniInternalTest, NewObjectArrayWithInitialValue) { |
| 532 | jclass element_class = env_->FindClass("java/lang/String"); |
| 533 | ASSERT_TRUE(element_class != nullptr); |
| 534 | jclass array_class = env_->FindClass("[Ljava/lang/String;"); |
| 535 | ASSERT_TRUE(array_class != nullptr); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 536 | |
| 537 | jstring s = env_->NewStringUTF("poop"); |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 538 | jobjectArray a = env_->NewObjectArray(2, element_class, s); |
| 539 | EXPECT_TRUE(a != nullptr); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 540 | EXPECT_TRUE(env_->IsInstanceOf(a, array_class)); |
| 541 | EXPECT_EQ(2, env_->GetArrayLength(a)); |
| 542 | EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(a, 0), s)); |
| 543 | EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(a, 1), s)); |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 544 | |
| 545 | // Attempt to incorrect create an array of strings with initial value of string arrays. |
| 546 | CheckJniAbortCatcher jni_abort_catcher; |
| 547 | env_->NewObjectArray(2, element_class, a); |
| 548 | jni_abort_catcher.Check("cannot assign object of type 'java.lang.String[]' to array with element " |
| 549 | "type of 'java.lang.String'"); |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | TEST_F(JniInternalTest, GetArrayLength) { |
| 553 | // Already tested in NewObjectArray/NewPrimitiveArray. |
Elliott Hughes | 8a26c5c | 2011-08-15 18:35:43 -0700 | [diff] [blame] | 554 | } |
| 555 | |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 556 | TEST_F(JniInternalTest, GetObjectClass) { |
| 557 | jclass string_class = env_->FindClass("java/lang/String"); |
| 558 | ASSERT_TRUE(string_class != NULL); |
| 559 | jclass class_class = env_->FindClass("java/lang/Class"); |
| 560 | ASSERT_TRUE(class_class != NULL); |
| 561 | |
| 562 | jstring s = env_->NewStringUTF("poop"); |
| 563 | jclass c = env_->GetObjectClass(s); |
| 564 | ASSERT_TRUE(env_->IsSameObject(string_class, c)); |
| 565 | |
| 566 | jclass c2 = env_->GetObjectClass(c); |
| 567 | ASSERT_TRUE(env_->IsSameObject(class_class, env_->GetObjectClass(c2))); |
| 568 | } |
| 569 | |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 570 | TEST_F(JniInternalTest, GetSuperclass) { |
| 571 | jclass object_class = env_->FindClass("java/lang/Object"); |
| 572 | ASSERT_TRUE(object_class != NULL); |
| 573 | jclass string_class = env_->FindClass("java/lang/String"); |
| 574 | ASSERT_TRUE(string_class != NULL); |
Ian Rogers | dc18020 | 2012-01-29 14:47:29 -0800 | [diff] [blame] | 575 | jclass runnable_interface = env_->FindClass("java/lang/Runnable"); |
| 576 | ASSERT_TRUE(runnable_interface != NULL); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 577 | ASSERT_TRUE(env_->IsSameObject(object_class, env_->GetSuperclass(string_class))); |
| 578 | ASSERT_TRUE(env_->GetSuperclass(object_class) == NULL); |
Ian Rogers | dc18020 | 2012-01-29 14:47:29 -0800 | [diff] [blame] | 579 | ASSERT_TRUE(env_->IsSameObject(object_class, env_->GetSuperclass(runnable_interface))); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 580 | } |
| 581 | |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 582 | TEST_F(JniInternalTest, IsAssignableFrom) { |
| 583 | jclass object_class = env_->FindClass("java/lang/Object"); |
| 584 | ASSERT_TRUE(object_class != NULL); |
| 585 | jclass string_class = env_->FindClass("java/lang/String"); |
| 586 | ASSERT_TRUE(string_class != NULL); |
| 587 | |
| 588 | ASSERT_TRUE(env_->IsAssignableFrom(object_class, string_class)); |
| 589 | ASSERT_FALSE(env_->IsAssignableFrom(string_class, object_class)); |
| 590 | } |
| 591 | |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 592 | TEST_F(JniInternalTest, GetObjectRefType) { |
| 593 | jclass local = env_->FindClass("java/lang/Object"); |
| 594 | ASSERT_TRUE(local != NULL); |
| 595 | EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(local)); |
| 596 | |
| 597 | jobject global = env_->NewGlobalRef(local); |
| 598 | EXPECT_EQ(JNIGlobalRefType, env_->GetObjectRefType(global)); |
| 599 | |
| 600 | jweak weak_global = env_->NewWeakGlobalRef(local); |
| 601 | EXPECT_EQ(JNIWeakGlobalRefType, env_->GetObjectRefType(weak_global)); |
| 602 | |
| 603 | jobject invalid = reinterpret_cast<jobject>(this); |
| 604 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(invalid)); |
| 605 | |
| 606 | // TODO: invoke a native method and test that its arguments are considered local references. |
| 607 | } |
| 608 | |
Mathieu Chartier | 0859999 | 2013-12-20 17:17:55 -0800 | [diff] [blame] | 609 | TEST_F(JniInternalTest, StaleWeakGlobal) { |
| 610 | jclass java_lang_Class = env_->FindClass("java/lang/Class"); |
| 611 | ASSERT_TRUE(java_lang_Class != NULL); |
| 612 | jobjectArray local_ref = env_->NewObjectArray(1, java_lang_Class, NULL); |
| 613 | ASSERT_TRUE(local_ref != NULL); |
| 614 | jweak weak_global = env_->NewWeakGlobalRef(local_ref); |
| 615 | ASSERT_TRUE(weak_global != NULL); |
| 616 | env_->DeleteLocalRef(local_ref); |
| 617 | Runtime::Current()->GetHeap()->CollectGarbage(false); // GC should clear the weak global. |
| 618 | jobject new_global_ref = env_->NewGlobalRef(weak_global); |
| 619 | EXPECT_TRUE(new_global_ref == NULL); |
| 620 | jobject new_local_ref = env_->NewLocalRef(weak_global); |
| 621 | EXPECT_TRUE(new_local_ref == NULL); |
| 622 | } |
| 623 | |
Elliott Hughes | 8a26c5c | 2011-08-15 18:35:43 -0700 | [diff] [blame] | 624 | TEST_F(JniInternalTest, NewStringUTF) { |
| 625 | EXPECT_TRUE(env_->NewStringUTF(NULL) == NULL); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 626 | jstring s; |
| 627 | |
| 628 | s = env_->NewStringUTF(""); |
| 629 | EXPECT_TRUE(s != NULL); |
| 630 | EXPECT_EQ(0, env_->GetStringLength(s)); |
| 631 | EXPECT_EQ(0, env_->GetStringUTFLength(s)); |
| 632 | s = env_->NewStringUTF("hello"); |
| 633 | EXPECT_TRUE(s != NULL); |
| 634 | EXPECT_EQ(5, env_->GetStringLength(s)); |
| 635 | EXPECT_EQ(5, env_->GetStringUTFLength(s)); |
| 636 | |
Elliott Hughes | 8a26c5c | 2011-08-15 18:35:43 -0700 | [diff] [blame] | 637 | // TODO: check some non-ASCII strings. |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 638 | } |
| 639 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 640 | TEST_F(JniInternalTest, NewString) { |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 641 | jchar chars[] = { 'h', 'i' }; |
| 642 | jstring s; |
| 643 | s = env_->NewString(chars, 0); |
| 644 | EXPECT_TRUE(s != NULL); |
| 645 | EXPECT_EQ(0, env_->GetStringLength(s)); |
| 646 | EXPECT_EQ(0, env_->GetStringUTFLength(s)); |
| 647 | s = env_->NewString(chars, 2); |
| 648 | EXPECT_TRUE(s != NULL); |
| 649 | EXPECT_EQ(2, env_->GetStringLength(s)); |
| 650 | EXPECT_EQ(2, env_->GetStringUTFLength(s)); |
| 651 | |
| 652 | // TODO: check some non-ASCII strings. |
| 653 | } |
| 654 | |
Jesse Wilson | 25e79a5 | 2011-11-18 15:31:58 -0500 | [diff] [blame] | 655 | TEST_F(JniInternalTest, NewStringNullCharsZeroLength) { |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 656 | jstring s = env_->NewString(nullptr, 0); |
| 657 | EXPECT_TRUE(s != nullptr); |
Jesse Wilson | 25e79a5 | 2011-11-18 15:31:58 -0500 | [diff] [blame] | 658 | EXPECT_EQ(0, env_->GetStringLength(s)); |
| 659 | } |
| 660 | |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 661 | TEST_F(JniInternalTest, NewStringNullCharsNonzeroLength) { |
| 662 | CheckJniAbortCatcher jni_abort_catcher; |
| 663 | env_->NewString(nullptr, 1); |
| 664 | jni_abort_catcher.Check("chars == null && char_count > 0"); |
| 665 | } |
| 666 | |
| 667 | TEST_F(JniInternalTest, NewStringNegativeLength) { |
| 668 | CheckJniAbortCatcher jni_abort_catcher; |
| 669 | env_->NewString(nullptr, -1); |
| 670 | jni_abort_catcher.Check("char_count < 0: -1"); |
| 671 | env_->NewString(nullptr, std::numeric_limits<jint>::min()); |
| 672 | jni_abort_catcher.Check("char_count < 0: -2147483648"); |
Jesse Wilson | 25e79a5 | 2011-11-18 15:31:58 -0500 | [diff] [blame] | 673 | } |
| 674 | |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 675 | TEST_F(JniInternalTest, GetStringLength_GetStringUTFLength) { |
| 676 | // Already tested in the NewString/NewStringUTF tests. |
| 677 | } |
| 678 | |
| 679 | TEST_F(JniInternalTest, GetStringRegion_GetStringUTFRegion) { |
| 680 | jstring s = env_->NewStringUTF("hello"); |
| 681 | ASSERT_TRUE(s != NULL); |
| 682 | |
| 683 | env_->GetStringRegion(s, -1, 0, NULL); |
| 684 | EXPECT_EXCEPTION(sioobe_); |
| 685 | env_->GetStringRegion(s, 0, -1, NULL); |
| 686 | EXPECT_EXCEPTION(sioobe_); |
| 687 | env_->GetStringRegion(s, 0, 10, NULL); |
| 688 | EXPECT_EXCEPTION(sioobe_); |
| 689 | env_->GetStringRegion(s, 10, 1, NULL); |
| 690 | EXPECT_EXCEPTION(sioobe_); |
| 691 | |
| 692 | jchar chars[4] = { 'x', 'x', 'x', 'x' }; |
| 693 | env_->GetStringRegion(s, 1, 2, &chars[1]); |
| 694 | EXPECT_EQ('x', chars[0]); |
| 695 | EXPECT_EQ('e', chars[1]); |
| 696 | EXPECT_EQ('l', chars[2]); |
| 697 | EXPECT_EQ('x', chars[3]); |
| 698 | |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 699 | // It's okay for the buffer to be NULL as long as the length is 0. |
| 700 | env_->GetStringRegion(s, 2, 0, NULL); |
| 701 | // Even if the offset is invalid... |
| 702 | env_->GetStringRegion(s, 123, 0, NULL); |
| 703 | EXPECT_EXCEPTION(sioobe_); |
| 704 | |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 705 | env_->GetStringUTFRegion(s, -1, 0, NULL); |
| 706 | EXPECT_EXCEPTION(sioobe_); |
| 707 | env_->GetStringUTFRegion(s, 0, -1, NULL); |
| 708 | EXPECT_EXCEPTION(sioobe_); |
| 709 | env_->GetStringUTFRegion(s, 0, 10, NULL); |
| 710 | EXPECT_EXCEPTION(sioobe_); |
| 711 | env_->GetStringUTFRegion(s, 10, 1, NULL); |
| 712 | EXPECT_EXCEPTION(sioobe_); |
| 713 | |
| 714 | char bytes[4] = { 'x', 'x', 'x', 'x' }; |
| 715 | env_->GetStringUTFRegion(s, 1, 2, &bytes[1]); |
| 716 | EXPECT_EQ('x', bytes[0]); |
| 717 | EXPECT_EQ('e', bytes[1]); |
| 718 | EXPECT_EQ('l', bytes[2]); |
| 719 | EXPECT_EQ('x', bytes[3]); |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 720 | |
| 721 | // It's okay for the buffer to be NULL as long as the length is 0. |
| 722 | env_->GetStringUTFRegion(s, 2, 0, NULL); |
| 723 | // Even if the offset is invalid... |
| 724 | env_->GetStringUTFRegion(s, 123, 0, NULL); |
| 725 | EXPECT_EXCEPTION(sioobe_); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 726 | } |
| 727 | |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 728 | TEST_F(JniInternalTest, GetStringUTFChars_ReleaseStringUTFChars) { |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 729 | // Passing in a NULL jstring is ignored normally, but caught by -Xcheck:jni. |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 730 | { |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 731 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 732 | EXPECT_TRUE(env_->GetStringUTFChars(NULL, NULL) == NULL); |
Elliott Hughes | 56ef042 | 2012-06-19 14:35:04 -0700 | [diff] [blame] | 733 | check_jni_abort_catcher.Check("GetStringUTFChars received null jstring"); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 734 | } |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 735 | |
| 736 | jstring s = env_->NewStringUTF("hello"); |
| 737 | ASSERT_TRUE(s != NULL); |
| 738 | |
| 739 | const char* utf = env_->GetStringUTFChars(s, NULL); |
| 740 | EXPECT_STREQ("hello", utf); |
| 741 | env_->ReleaseStringUTFChars(s, utf); |
| 742 | |
| 743 | jboolean is_copy = JNI_FALSE; |
| 744 | utf = env_->GetStringUTFChars(s, &is_copy); |
| 745 | EXPECT_EQ(JNI_TRUE, is_copy); |
| 746 | EXPECT_STREQ("hello", utf); |
| 747 | env_->ReleaseStringUTFChars(s, utf); |
| 748 | } |
| 749 | |
| 750 | TEST_F(JniInternalTest, GetStringChars_ReleaseStringChars) { |
| 751 | jstring s = env_->NewStringUTF("hello"); |
| 752 | ASSERT_TRUE(s != NULL); |
| 753 | |
| 754 | jchar expected[] = { 'h', 'e', 'l', 'l', 'o' }; |
| 755 | const jchar* chars = env_->GetStringChars(s, NULL); |
| 756 | EXPECT_EQ(expected[0], chars[0]); |
| 757 | EXPECT_EQ(expected[1], chars[1]); |
| 758 | EXPECT_EQ(expected[2], chars[2]); |
| 759 | EXPECT_EQ(expected[3], chars[3]); |
| 760 | EXPECT_EQ(expected[4], chars[4]); |
| 761 | env_->ReleaseStringChars(s, chars); |
| 762 | |
| 763 | jboolean is_copy = JNI_FALSE; |
| 764 | chars = env_->GetStringChars(s, &is_copy); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 765 | EXPECT_EQ(JNI_TRUE, is_copy); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 766 | EXPECT_EQ(expected[0], chars[0]); |
| 767 | EXPECT_EQ(expected[1], chars[1]); |
| 768 | EXPECT_EQ(expected[2], chars[2]); |
| 769 | EXPECT_EQ(expected[3], chars[3]); |
| 770 | EXPECT_EQ(expected[4], chars[4]); |
| 771 | env_->ReleaseStringChars(s, chars); |
| 772 | } |
| 773 | |
| 774 | TEST_F(JniInternalTest, GetStringCritical_ReleaseStringCritical) { |
| 775 | jstring s = env_->NewStringUTF("hello"); |
| 776 | ASSERT_TRUE(s != NULL); |
| 777 | |
| 778 | jchar expected[] = { 'h', 'e', 'l', 'l', 'o' }; |
| 779 | const jchar* chars = env_->GetStringCritical(s, NULL); |
| 780 | EXPECT_EQ(expected[0], chars[0]); |
| 781 | EXPECT_EQ(expected[1], chars[1]); |
| 782 | EXPECT_EQ(expected[2], chars[2]); |
| 783 | EXPECT_EQ(expected[3], chars[3]); |
| 784 | EXPECT_EQ(expected[4], chars[4]); |
| 785 | env_->ReleaseStringCritical(s, chars); |
| 786 | |
| 787 | jboolean is_copy = JNI_FALSE; |
| 788 | chars = env_->GetStringCritical(s, &is_copy); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 789 | // TODO: Fix GetStringCritical to use the same mechanism as GetPrimitiveArrayElementsCritical. |
| 790 | EXPECT_EQ(JNI_TRUE, is_copy); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 791 | EXPECT_EQ(expected[0], chars[0]); |
| 792 | EXPECT_EQ(expected[1], chars[1]); |
| 793 | EXPECT_EQ(expected[2], chars[2]); |
| 794 | EXPECT_EQ(expected[3], chars[3]); |
| 795 | EXPECT_EQ(expected[4], chars[4]); |
| 796 | env_->ReleaseStringCritical(s, chars); |
| 797 | } |
| 798 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 799 | TEST_F(JniInternalTest, GetObjectArrayElement_SetObjectArrayElement) { |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 800 | jclass java_lang_Class = env_->FindClass("java/lang/Class"); |
| 801 | ASSERT_TRUE(java_lang_Class != NULL); |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 802 | |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 803 | jobjectArray array = env_->NewObjectArray(1, java_lang_Class, NULL); |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 804 | EXPECT_TRUE(array != NULL); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 805 | EXPECT_TRUE(env_->GetObjectArrayElement(array, 0) == NULL); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 806 | env_->SetObjectArrayElement(array, 0, java_lang_Class); |
| 807 | EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(array, 0), java_lang_Class)); |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 808 | |
| 809 | // ArrayIndexOutOfBounds for negative index. |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 810 | env_->SetObjectArrayElement(array, -1, java_lang_Class); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 811 | EXPECT_EXCEPTION(aioobe_); |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 812 | |
| 813 | // ArrayIndexOutOfBounds for too-large index. |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 814 | env_->SetObjectArrayElement(array, 1, java_lang_Class); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 815 | EXPECT_EXCEPTION(aioobe_); |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 816 | |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 817 | // ArrayStoreException thrown for bad types. |
| 818 | env_->SetObjectArrayElement(array, 0, env_->NewStringUTF("not a jclass!")); |
| 819 | EXPECT_EXCEPTION(ase_); |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 820 | } |
| 821 | |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 822 | #define EXPECT_STATIC_PRIMITIVE_FIELD(type, field_name, sig, value1, value2) \ |
| 823 | do { \ |
| 824 | jfieldID fid = env_->GetStaticFieldID(c, field_name, sig); \ |
| 825 | EXPECT_TRUE(fid != NULL); \ |
| 826 | env_->SetStatic ## type ## Field(c, fid, value1); \ |
Brian Carlstrom | 2e3d1b2 | 2012-01-09 18:01:56 -0800 | [diff] [blame] | 827 | EXPECT_TRUE(value1 == env_->GetStatic ## type ## Field(c, fid)); \ |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 828 | env_->SetStatic ## type ## Field(c, fid, value2); \ |
Brian Carlstrom | 2e3d1b2 | 2012-01-09 18:01:56 -0800 | [diff] [blame] | 829 | EXPECT_TRUE(value2 == env_->GetStatic ## type ## Field(c, fid)); \ |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 830 | } while (false) |
| 831 | |
| 832 | #define EXPECT_PRIMITIVE_FIELD(instance, type, field_name, sig, value1, value2) \ |
| 833 | do { \ |
| 834 | jfieldID fid = env_->GetFieldID(c, field_name, sig); \ |
| 835 | EXPECT_TRUE(fid != NULL); \ |
| 836 | env_->Set ## type ## Field(instance, fid, value1); \ |
Brian Carlstrom | 2e3d1b2 | 2012-01-09 18:01:56 -0800 | [diff] [blame] | 837 | EXPECT_TRUE(value1 == env_->Get ## type ## Field(instance, fid)); \ |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 838 | env_->Set ## type ## Field(instance, fid, value2); \ |
Brian Carlstrom | 2e3d1b2 | 2012-01-09 18:01:56 -0800 | [diff] [blame] | 839 | EXPECT_TRUE(value2 == env_->Get ## type ## Field(instance, fid)); \ |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 840 | } while (false) |
| 841 | |
| 842 | |
| 843 | TEST_F(JniInternalTest, GetPrimitiveField_SetPrimitiveField) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 844 | TEST_DISABLED_FOR_PORTABLE(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 845 | Thread::Current()->TransitionFromSuspendedToRunnable(); |
| 846 | LoadDex("AllFields"); |
Brian Carlstrom | bd86bcc | 2013-03-10 20:26:16 -0700 | [diff] [blame] | 847 | bool started = runtime_->Start(); |
| 848 | CHECK(started); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 849 | |
| 850 | jclass c = env_->FindClass("AllFields"); |
| 851 | ASSERT_TRUE(c != NULL); |
| 852 | jobject o = env_->AllocObject(c); |
| 853 | ASSERT_TRUE(o != NULL); |
| 854 | |
| 855 | EXPECT_STATIC_PRIMITIVE_FIELD(Boolean, "sZ", "Z", true, false); |
| 856 | EXPECT_STATIC_PRIMITIVE_FIELD(Byte, "sB", "B", 1, 2); |
| 857 | EXPECT_STATIC_PRIMITIVE_FIELD(Char, "sC", "C", 'a', 'b'); |
| 858 | EXPECT_STATIC_PRIMITIVE_FIELD(Double, "sD", "D", 1.0, 2.0); |
| 859 | EXPECT_STATIC_PRIMITIVE_FIELD(Float, "sF", "F", 1.0, 2.0); |
| 860 | EXPECT_STATIC_PRIMITIVE_FIELD(Int, "sI", "I", 1, 2); |
| 861 | EXPECT_STATIC_PRIMITIVE_FIELD(Long, "sJ", "J", 1, 2); |
| 862 | EXPECT_STATIC_PRIMITIVE_FIELD(Short, "sS", "S", 1, 2); |
| 863 | |
| 864 | EXPECT_PRIMITIVE_FIELD(o, Boolean, "iZ", "Z", true, false); |
| 865 | EXPECT_PRIMITIVE_FIELD(o, Byte, "iB", "B", 1, 2); |
| 866 | EXPECT_PRIMITIVE_FIELD(o, Char, "iC", "C", 'a', 'b'); |
| 867 | EXPECT_PRIMITIVE_FIELD(o, Double, "iD", "D", 1.0, 2.0); |
| 868 | EXPECT_PRIMITIVE_FIELD(o, Float, "iF", "F", 1.0, 2.0); |
| 869 | EXPECT_PRIMITIVE_FIELD(o, Int, "iI", "I", 1, 2); |
| 870 | EXPECT_PRIMITIVE_FIELD(o, Long, "iJ", "J", 1, 2); |
| 871 | EXPECT_PRIMITIVE_FIELD(o, Short, "iS", "S", 1, 2); |
| 872 | } |
| 873 | |
| 874 | TEST_F(JniInternalTest, GetObjectField_SetObjectField) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 875 | TEST_DISABLED_FOR_PORTABLE(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 876 | Thread::Current()->TransitionFromSuspendedToRunnable(); |
| 877 | LoadDex("AllFields"); |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 878 | runtime_->Start(); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 879 | |
| 880 | jclass c = env_->FindClass("AllFields"); |
| 881 | ASSERT_TRUE(c != NULL); |
| 882 | jobject o = env_->AllocObject(c); |
| 883 | ASSERT_TRUE(o != NULL); |
| 884 | |
| 885 | jstring s1 = env_->NewStringUTF("hello"); |
| 886 | ASSERT_TRUE(s1 != NULL); |
| 887 | jstring s2 = env_->NewStringUTF("world"); |
| 888 | ASSERT_TRUE(s2 != NULL); |
| 889 | |
| 890 | jfieldID s_fid = env_->GetStaticFieldID(c, "sObject", "Ljava/lang/Object;"); |
| 891 | ASSERT_TRUE(s_fid != NULL); |
| 892 | jfieldID i_fid = env_->GetFieldID(c, "iObject", "Ljava/lang/Object;"); |
| 893 | ASSERT_TRUE(i_fid != NULL); |
| 894 | |
| 895 | env_->SetStaticObjectField(c, s_fid, s1); |
| 896 | ASSERT_TRUE(env_->IsSameObject(s1, env_->GetStaticObjectField(c, s_fid))); |
| 897 | env_->SetStaticObjectField(c, s_fid, s2); |
| 898 | ASSERT_TRUE(env_->IsSameObject(s2, env_->GetStaticObjectField(c, s_fid))); |
| 899 | |
| 900 | env_->SetObjectField(o, i_fid, s1); |
| 901 | ASSERT_TRUE(env_->IsSameObject(s1, env_->GetObjectField(o, i_fid))); |
| 902 | env_->SetObjectField(o, i_fid, s2); |
| 903 | ASSERT_TRUE(env_->IsSameObject(s2, env_->GetObjectField(o, i_fid))); |
| 904 | } |
| 905 | |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 906 | TEST_F(JniInternalTest, NewLocalRef_NULL) { |
| 907 | EXPECT_TRUE(env_->NewLocalRef(NULL) == NULL); |
| 908 | } |
| 909 | |
| 910 | TEST_F(JniInternalTest, NewLocalRef) { |
| 911 | jstring s = env_->NewStringUTF(""); |
| 912 | ASSERT_TRUE(s != NULL); |
| 913 | jobject o = env_->NewLocalRef(s); |
| 914 | EXPECT_TRUE(o != NULL); |
| 915 | EXPECT_TRUE(o != s); |
| 916 | |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 917 | EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(o)); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | TEST_F(JniInternalTest, DeleteLocalRef_NULL) { |
| 921 | env_->DeleteLocalRef(NULL); |
| 922 | } |
| 923 | |
| 924 | TEST_F(JniInternalTest, DeleteLocalRef) { |
| 925 | jstring s = env_->NewStringUTF(""); |
| 926 | ASSERT_TRUE(s != NULL); |
| 927 | env_->DeleteLocalRef(s); |
| 928 | |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 929 | // Currently, deleting an already-deleted reference is just a CheckJNI warning. |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 930 | { |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 931 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 932 | env_->DeleteLocalRef(s); |
Elliott Hughes | a9137c6 | 2013-01-09 10:55:21 -0800 | [diff] [blame] | 933 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 934 | std::string expected(StringPrintf("native code passing in reference to " |
| 935 | "invalid local reference: %p", s)); |
Elliott Hughes | a9137c6 | 2013-01-09 10:55:21 -0800 | [diff] [blame] | 936 | check_jni_abort_catcher.Check(expected.c_str()); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 937 | } |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 938 | |
| 939 | s = env_->NewStringUTF(""); |
| 940 | ASSERT_TRUE(s != NULL); |
| 941 | jobject o = env_->NewLocalRef(s); |
| 942 | ASSERT_TRUE(o != NULL); |
| 943 | |
| 944 | env_->DeleteLocalRef(s); |
| 945 | env_->DeleteLocalRef(o); |
| 946 | } |
| 947 | |
Elliott Hughes | aa836f7 | 2013-08-20 16:57:23 -0700 | [diff] [blame] | 948 | TEST_F(JniInternalTest, PushLocalFrame_10395422) { |
| 949 | // The JNI specification is ambiguous about whether the given capacity is to be interpreted as a |
| 950 | // maximum or as a minimum, but it seems like it's supposed to be a minimum, and that's how |
| 951 | // Android historically treated it, and it's how the RI treats it. It's also the more useful |
| 952 | // interpretation! |
| 953 | ASSERT_EQ(JNI_OK, env_->PushLocalFrame(0)); |
| 954 | env_->PopLocalFrame(NULL); |
| 955 | |
| 956 | // Negative capacities are not allowed. |
| 957 | ASSERT_EQ(JNI_ERR, env_->PushLocalFrame(-1)); |
| 958 | |
| 959 | // And it's okay to have an upper limit. Ours is currently 512. |
| 960 | ASSERT_EQ(JNI_ERR, env_->PushLocalFrame(8192)); |
| 961 | } |
| 962 | |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 963 | TEST_F(JniInternalTest, PushLocalFrame_PopLocalFrame) { |
| 964 | jobject original = env_->NewStringUTF(""); |
| 965 | ASSERT_TRUE(original != NULL); |
| 966 | |
| 967 | jobject outer; |
| 968 | jobject inner1, inner2; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 969 | ScopedObjectAccess soa(env_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 970 | mirror::Object* inner2_direct_pointer; |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 971 | { |
Elliott Hughes | aa836f7 | 2013-08-20 16:57:23 -0700 | [diff] [blame] | 972 | ASSERT_EQ(JNI_OK, env_->PushLocalFrame(4)); |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 973 | outer = env_->NewLocalRef(original); |
| 974 | |
| 975 | { |
Elliott Hughes | aa836f7 | 2013-08-20 16:57:23 -0700 | [diff] [blame] | 976 | ASSERT_EQ(JNI_OK, env_->PushLocalFrame(4)); |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 977 | inner1 = env_->NewLocalRef(outer); |
| 978 | inner2 = env_->NewStringUTF("survivor"); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 979 | inner2_direct_pointer = soa.Decode<mirror::Object*>(inner2); |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 980 | env_->PopLocalFrame(inner2); |
| 981 | } |
| 982 | |
| 983 | EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(original)); |
| 984 | EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(outer)); |
| 985 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(inner1)); |
| 986 | |
| 987 | // Our local reference for the survivor is invalid because the survivor |
| 988 | // gets a new local reference... |
| 989 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(inner2)); |
| 990 | // ...but the survivor should be in the local reference table. |
Brian Carlstrom | 4d57143 | 2012-05-16 00:21:41 -0700 | [diff] [blame] | 991 | JNIEnvExt* env = reinterpret_cast<JNIEnvExt*>(env_); |
| 992 | EXPECT_TRUE(env->locals.ContainsDirectPointer(inner2_direct_pointer)); |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 993 | |
| 994 | env_->PopLocalFrame(NULL); |
| 995 | } |
| 996 | EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(original)); |
| 997 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(outer)); |
| 998 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(inner1)); |
| 999 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(inner2)); |
| 1000 | } |
| 1001 | |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1002 | TEST_F(JniInternalTest, NewGlobalRef_NULL) { |
| 1003 | EXPECT_TRUE(env_->NewGlobalRef(NULL) == NULL); |
| 1004 | } |
| 1005 | |
| 1006 | TEST_F(JniInternalTest, NewGlobalRef) { |
| 1007 | jstring s = env_->NewStringUTF(""); |
| 1008 | ASSERT_TRUE(s != NULL); |
| 1009 | jobject o = env_->NewGlobalRef(s); |
| 1010 | EXPECT_TRUE(o != NULL); |
| 1011 | EXPECT_TRUE(o != s); |
| 1012 | |
| 1013 | // TODO: check that o is a global reference. |
| 1014 | } |
| 1015 | |
| 1016 | TEST_F(JniInternalTest, DeleteGlobalRef_NULL) { |
| 1017 | env_->DeleteGlobalRef(NULL); |
| 1018 | } |
| 1019 | |
| 1020 | TEST_F(JniInternalTest, DeleteGlobalRef) { |
| 1021 | jstring s = env_->NewStringUTF(""); |
| 1022 | ASSERT_TRUE(s != NULL); |
| 1023 | |
| 1024 | jobject o = env_->NewGlobalRef(s); |
| 1025 | ASSERT_TRUE(o != NULL); |
| 1026 | env_->DeleteGlobalRef(o); |
| 1027 | |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 1028 | // Currently, deleting an already-deleted reference is just a CheckJNI warning. |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1029 | { |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1030 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 1031 | env_->DeleteGlobalRef(o); |
Elliott Hughes | a9137c6 | 2013-01-09 10:55:21 -0800 | [diff] [blame] | 1032 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1033 | std::string expected(StringPrintf("native code passing in reference to " |
| 1034 | "invalid global reference: %p", o)); |
Elliott Hughes | a9137c6 | 2013-01-09 10:55:21 -0800 | [diff] [blame] | 1035 | check_jni_abort_catcher.Check(expected.c_str()); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1036 | } |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1037 | |
| 1038 | jobject o1 = env_->NewGlobalRef(s); |
| 1039 | ASSERT_TRUE(o1 != NULL); |
| 1040 | jobject o2 = env_->NewGlobalRef(s); |
| 1041 | ASSERT_TRUE(o2 != NULL); |
| 1042 | |
| 1043 | env_->DeleteGlobalRef(o1); |
| 1044 | env_->DeleteGlobalRef(o2); |
| 1045 | } |
| 1046 | |
| 1047 | TEST_F(JniInternalTest, NewWeakGlobalRef_NULL) { |
| 1048 | EXPECT_TRUE(env_->NewWeakGlobalRef(NULL) == NULL); |
| 1049 | } |
| 1050 | |
| 1051 | TEST_F(JniInternalTest, NewWeakGlobalRef) { |
| 1052 | jstring s = env_->NewStringUTF(""); |
| 1053 | ASSERT_TRUE(s != NULL); |
| 1054 | jobject o = env_->NewWeakGlobalRef(s); |
| 1055 | EXPECT_TRUE(o != NULL); |
| 1056 | EXPECT_TRUE(o != s); |
| 1057 | |
| 1058 | // TODO: check that o is a weak global reference. |
| 1059 | } |
| 1060 | |
| 1061 | TEST_F(JniInternalTest, DeleteWeakGlobalRef_NULL) { |
| 1062 | env_->DeleteWeakGlobalRef(NULL); |
| 1063 | } |
| 1064 | |
| 1065 | TEST_F(JniInternalTest, DeleteWeakGlobalRef) { |
| 1066 | jstring s = env_->NewStringUTF(""); |
| 1067 | ASSERT_TRUE(s != NULL); |
| 1068 | |
| 1069 | jobject o = env_->NewWeakGlobalRef(s); |
| 1070 | ASSERT_TRUE(o != NULL); |
| 1071 | env_->DeleteWeakGlobalRef(o); |
| 1072 | |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 1073 | // Currently, deleting an already-deleted reference is just a CheckJNI warning. |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1074 | { |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1075 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 1076 | env_->DeleteWeakGlobalRef(o); |
Elliott Hughes | a9137c6 | 2013-01-09 10:55:21 -0800 | [diff] [blame] | 1077 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1078 | std::string expected(StringPrintf("native code passing in reference to " |
| 1079 | "invalid weak global reference: %p", o)); |
Elliott Hughes | a9137c6 | 2013-01-09 10:55:21 -0800 | [diff] [blame] | 1080 | check_jni_abort_catcher.Check(expected.c_str()); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1081 | } |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1082 | |
| 1083 | jobject o1 = env_->NewWeakGlobalRef(s); |
| 1084 | ASSERT_TRUE(o1 != NULL); |
| 1085 | jobject o2 = env_->NewWeakGlobalRef(s); |
| 1086 | ASSERT_TRUE(o2 != NULL); |
| 1087 | |
| 1088 | env_->DeleteWeakGlobalRef(o1); |
| 1089 | env_->DeleteWeakGlobalRef(o2); |
| 1090 | } |
| 1091 | |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1092 | TEST_F(JniInternalTest, Throw) { |
| 1093 | EXPECT_EQ(JNI_ERR, env_->Throw(NULL)); |
| 1094 | |
| 1095 | jclass exception_class = env_->FindClass("java/lang/RuntimeException"); |
| 1096 | ASSERT_TRUE(exception_class != NULL); |
| 1097 | jthrowable exception = reinterpret_cast<jthrowable>(env_->AllocObject(exception_class)); |
| 1098 | ASSERT_TRUE(exception != NULL); |
| 1099 | |
| 1100 | EXPECT_EQ(JNI_OK, env_->Throw(exception)); |
| 1101 | EXPECT_TRUE(env_->ExceptionCheck()); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1102 | jthrowable thrown_exception = env_->ExceptionOccurred(); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1103 | env_->ExceptionClear(); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1104 | EXPECT_TRUE(env_->IsSameObject(exception, thrown_exception)); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1105 | } |
| 1106 | |
| 1107 | TEST_F(JniInternalTest, ThrowNew) { |
| 1108 | EXPECT_EQ(JNI_ERR, env_->Throw(NULL)); |
| 1109 | |
| 1110 | jclass exception_class = env_->FindClass("java/lang/RuntimeException"); |
| 1111 | ASSERT_TRUE(exception_class != NULL); |
| 1112 | |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 1113 | jthrowable thrown_exception; |
| 1114 | |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1115 | EXPECT_EQ(JNI_OK, env_->ThrowNew(exception_class, "hello world")); |
| 1116 | EXPECT_TRUE(env_->ExceptionCheck()); |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 1117 | thrown_exception = env_->ExceptionOccurred(); |
| 1118 | env_->ExceptionClear(); |
| 1119 | EXPECT_TRUE(env_->IsInstanceOf(thrown_exception, exception_class)); |
| 1120 | |
| 1121 | EXPECT_EQ(JNI_OK, env_->ThrowNew(exception_class, NULL)); |
| 1122 | EXPECT_TRUE(env_->ExceptionCheck()); |
| 1123 | thrown_exception = env_->ExceptionOccurred(); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1124 | env_->ExceptionClear(); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1125 | EXPECT_TRUE(env_->IsInstanceOf(thrown_exception, exception_class)); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1126 | } |
| 1127 | |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 1128 | TEST_F(JniInternalTest, NewDirectBuffer_GetDirectBufferAddress_GetDirectBufferCapacity) { |
| 1129 | // Start runtime. |
| 1130 | Thread* self = Thread::Current(); |
| 1131 | self->TransitionFromSuspendedToRunnable(); |
| 1132 | MakeExecutable(nullptr, "java.lang.Class"); |
| 1133 | MakeExecutable(nullptr, "java.lang.Object"); |
| 1134 | MakeExecutable(nullptr, "java.nio.DirectByteBuffer"); |
| 1135 | MakeExecutable(nullptr, "java.nio.MemoryBlock"); |
| 1136 | MakeExecutable(nullptr, "java.nio.MemoryBlock$UnmanagedBlock"); |
| 1137 | MakeExecutable(nullptr, "java.nio.MappedByteBuffer"); |
| 1138 | MakeExecutable(nullptr, "java.nio.ByteBuffer"); |
| 1139 | MakeExecutable(nullptr, "java.nio.Buffer"); |
| 1140 | // TODO: we only load a dex file here as starting the runtime relies upon it. |
| 1141 | const char* class_name = "StaticLeafMethods"; |
| 1142 | LoadDex(class_name); |
| 1143 | bool started = runtime_->Start(); |
| 1144 | ASSERT_TRUE(started); |
| 1145 | |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1146 | jclass buffer_class = env_->FindClass("java/nio/Buffer"); |
| 1147 | ASSERT_TRUE(buffer_class != NULL); |
| 1148 | |
| 1149 | char bytes[1024]; |
| 1150 | jobject buffer = env_->NewDirectByteBuffer(bytes, sizeof(bytes)); |
| 1151 | ASSERT_TRUE(buffer != NULL); |
| 1152 | ASSERT_TRUE(env_->IsInstanceOf(buffer, buffer_class)); |
| 1153 | ASSERT_TRUE(env_->GetDirectBufferAddress(buffer) == bytes); |
| 1154 | ASSERT_TRUE(env_->GetDirectBufferCapacity(buffer) == sizeof(bytes)); |
| 1155 | } |
| 1156 | |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 1157 | TEST_F(JniInternalTest, MonitorEnterExit) { |
| 1158 | // Create an object to torture |
| 1159 | jclass object_class = env_->FindClass("java/lang/Object"); |
| 1160 | ASSERT_TRUE(object_class != NULL); |
| 1161 | jobject object = env_->AllocObject(object_class); |
| 1162 | ASSERT_TRUE(object != NULL); |
| 1163 | |
| 1164 | // Expected class of exceptions |
| 1165 | jclass imse_class = env_->FindClass("java/lang/IllegalMonitorStateException"); |
| 1166 | ASSERT_TRUE(imse_class != NULL); |
| 1167 | |
| 1168 | jthrowable thrown_exception; |
| 1169 | |
| 1170 | // Unlock of unowned monitor |
| 1171 | env_->MonitorExit(object); |
| 1172 | EXPECT_TRUE(env_->ExceptionCheck()); |
| 1173 | thrown_exception = env_->ExceptionOccurred(); |
| 1174 | env_->ExceptionClear(); |
| 1175 | EXPECT_TRUE(env_->IsInstanceOf(thrown_exception, imse_class)); |
| 1176 | |
| 1177 | // Lock of unowned monitor |
| 1178 | env_->MonitorEnter(object); |
| 1179 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 1180 | // Regular unlock |
| 1181 | env_->MonitorExit(object); |
| 1182 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 1183 | |
| 1184 | // Recursively lock a lot |
| 1185 | size_t max_recursive_lock = 1024; |
| 1186 | for (size_t i = 0; i < max_recursive_lock; i++) { |
| 1187 | env_->MonitorEnter(object); |
| 1188 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 1189 | } |
| 1190 | // Recursively unlock a lot |
| 1191 | for (size_t i = 0; i < max_recursive_lock; i++) { |
| 1192 | env_->MonitorExit(object); |
| 1193 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 1194 | } |
| 1195 | |
| 1196 | // Unlock of unowned monitor |
| 1197 | env_->MonitorExit(object); |
| 1198 | EXPECT_TRUE(env_->ExceptionCheck()); |
| 1199 | thrown_exception = env_->ExceptionOccurred(); |
| 1200 | env_->ExceptionClear(); |
| 1201 | EXPECT_TRUE(env_->IsInstanceOf(thrown_exception, imse_class)); |
Elliott Hughes | a92853e | 2012-02-07 16:09:27 -0800 | [diff] [blame] | 1202 | |
| 1203 | // It's an error to call MonitorEnter or MonitorExit on NULL. |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1204 | { |
| 1205 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 1206 | env_->MonitorEnter(NULL); |
| 1207 | check_jni_abort_catcher.Check("in call to MonitorEnter"); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1208 | |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1209 | env_->MonitorExit(NULL); |
| 1210 | check_jni_abort_catcher.Check("in call to MonitorExit"); |
| 1211 | } |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 1212 | } |
| 1213 | |
Brian Carlstrom | 4d57143 | 2012-05-16 00:21:41 -0700 | [diff] [blame] | 1214 | TEST_F(JniInternalTest, DetachCurrentThread) { |
| 1215 | CleanUpJniEnv(); // cleanup now so TearDown won't have junk from wrong JNIEnv |
| 1216 | jint ok = vm_->DetachCurrentThread(); |
| 1217 | EXPECT_EQ(JNI_OK, ok); |
| 1218 | |
| 1219 | jint err = vm_->DetachCurrentThread(); |
| 1220 | EXPECT_EQ(JNI_ERR, err); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 1221 | vm_->AttachCurrentThread(&env_, NULL); // need attached thread for CommonRuntimeTest::TearDown |
Brian Carlstrom | 4d57143 | 2012-05-16 00:21:41 -0700 | [diff] [blame] | 1222 | } |
| 1223 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1224 | } // namespace art |