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" |
Fred Shih | 56890e2 | 2014-06-02 11:11:52 -0700 | [diff] [blame] | 21 | #include "mirror/string-inl.h" |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 22 | #include "ScopedLocalRef.h" |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 23 | |
| 24 | namespace art { |
| 25 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 26 | // TODO: Convert to CommonRuntimeTest. Currently MakeExecutable is used. |
| 27 | class JniInternalTest : public CommonCompilerTest { |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 28 | protected: |
| 29 | virtual void SetUp() { |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 30 | CommonCompilerTest::SetUp(); |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 31 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 32 | vm_ = Runtime::Current()->GetJavaVM(); |
| 33 | |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 34 | // Turn on -verbose:jni for the JNI tests. |
Ian Rogers | 7971363 | 2013-08-21 19:06:15 -0700 | [diff] [blame] | 35 | // gLogVerbosity.jni = true; |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 36 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 37 | vm_->AttachCurrentThread(&env_, nullptr); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 38 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 39 | ScopedLocalRef<jclass> aioobe(env_, |
| 40 | env_->FindClass("java/lang/ArrayIndexOutOfBoundsException")); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 41 | CHECK(aioobe.get() != nullptr); |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 42 | aioobe_ = reinterpret_cast<jclass>(env_->NewGlobalRef(aioobe.get())); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 43 | |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 44 | ScopedLocalRef<jclass> ase(env_, env_->FindClass("java/lang/ArrayStoreException")); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 45 | CHECK(ase.get() != nullptr); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 46 | ase_ = reinterpret_cast<jclass>(env_->NewGlobalRef(ase.get())); |
| 47 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 48 | ScopedLocalRef<jclass> sioobe(env_, |
| 49 | env_->FindClass("java/lang/StringIndexOutOfBoundsException")); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 50 | CHECK(sioobe.get() != nullptr); |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 51 | sioobe_ = reinterpret_cast<jclass>(env_->NewGlobalRef(sioobe.get())); |
| 52 | } |
| 53 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 54 | void ExpectException(jclass exception_class) { |
| 55 | EXPECT_TRUE(env_->ExceptionCheck()); |
| 56 | jthrowable exception = env_->ExceptionOccurred(); |
| 57 | EXPECT_NE(nullptr, exception); |
| 58 | env_->ExceptionClear(); |
| 59 | EXPECT_TRUE(env_->IsInstanceOf(exception, exception_class)); |
| 60 | } |
| 61 | |
| 62 | void ExpectClassFound(const char* name) { |
| 63 | EXPECT_NE(env_->FindClass(name), nullptr) << name; |
| 64 | EXPECT_FALSE(env_->ExceptionCheck()) << name; |
| 65 | } |
| 66 | |
| 67 | void ExpectClassNotFound(const char* name) { |
| 68 | EXPECT_EQ(env_->FindClass(name), nullptr) << name; |
| 69 | EXPECT_TRUE(env_->ExceptionCheck()) << name; |
| 70 | env_->ExceptionClear(); |
| 71 | } |
| 72 | |
Brian Carlstrom | 4d57143 | 2012-05-16 00:21:41 -0700 | [diff] [blame] | 73 | void CleanUpJniEnv() { |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 74 | if (aioobe_ != nullptr) { |
Brian Carlstrom | 4d57143 | 2012-05-16 00:21:41 -0700 | [diff] [blame] | 75 | env_->DeleteGlobalRef(aioobe_); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 76 | aioobe_ = nullptr; |
Brian Carlstrom | 4d57143 | 2012-05-16 00:21:41 -0700 | [diff] [blame] | 77 | } |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 78 | if (ase_ != nullptr) { |
Brian Carlstrom | 4d57143 | 2012-05-16 00:21:41 -0700 | [diff] [blame] | 79 | env_->DeleteGlobalRef(ase_); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 80 | ase_ = nullptr; |
Brian Carlstrom | 4d57143 | 2012-05-16 00:21:41 -0700 | [diff] [blame] | 81 | } |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 82 | if (sioobe_ != nullptr) { |
Brian Carlstrom | 4d57143 | 2012-05-16 00:21:41 -0700 | [diff] [blame] | 83 | env_->DeleteGlobalRef(sioobe_); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 84 | sioobe_ = nullptr; |
Brian Carlstrom | 4d57143 | 2012-05-16 00:21:41 -0700 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 88 | virtual void TearDown() OVERRIDE { |
Brian Carlstrom | 4d57143 | 2012-05-16 00:21:41 -0700 | [diff] [blame] | 89 | CleanUpJniEnv(); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 90 | CommonCompilerTest::TearDown(); |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 91 | } |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 92 | |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 93 | jclass GetPrimitiveClass(char descriptor) { |
| 94 | ScopedObjectAccess soa(env_); |
| 95 | mirror::Class* c = class_linker_->FindPrimitiveClass(descriptor); |
| 96 | CHECK(c != nullptr); |
| 97 | return soa.AddLocalReference<jclass>(c); |
| 98 | } |
| 99 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 100 | JavaVMExt* vm_; |
Brian Carlstrom | 4d57143 | 2012-05-16 00:21:41 -0700 | [diff] [blame] | 101 | JNIEnv* env_; |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 102 | jclass aioobe_; |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 103 | jclass ase_; |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 104 | jclass sioobe_; |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 105 | }; |
| 106 | |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 107 | TEST_F(JniInternalTest, AllocObject) { |
| 108 | jclass c = env_->FindClass("java/lang/String"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 109 | ASSERT_NE(c, nullptr); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 110 | jobject o = env_->AllocObject(c); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 111 | ASSERT_NE(o, nullptr); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 112 | |
| 113 | // We have an instance of the class we asked for... |
| 114 | ASSERT_TRUE(env_->IsInstanceOf(o, c)); |
| 115 | // ...whose fields haven't been initialized because |
| 116 | // we didn't call a constructor. |
| 117 | ASSERT_EQ(0, env_->GetIntField(o, env_->GetFieldID(c, "count", "I"))); |
| 118 | ASSERT_EQ(0, env_->GetIntField(o, env_->GetFieldID(c, "offset", "I"))); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 119 | ASSERT_TRUE(env_->GetObjectField(o, env_->GetFieldID(c, "value", "[C")) == nullptr); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 122 | TEST_F(JniInternalTest, GetVersion) { |
| 123 | ASSERT_EQ(JNI_VERSION_1_6, env_->GetVersion()); |
| 124 | } |
| 125 | |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 126 | TEST_F(JniInternalTest, FindClass) { |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 127 | // Reference types... |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 128 | ExpectClassFound("java/lang/String"); |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 129 | // ...for arrays too, where you must include "L;". |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 130 | ExpectClassFound("[Ljava/lang/String;"); |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 131 | // Primitive arrays are okay too, if the primitive type is valid. |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 132 | ExpectClassFound("[C"); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 133 | |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 134 | { |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 135 | CheckJniAbortCatcher check_jni_abort_catcher; |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 136 | env_->FindClass(nullptr); |
| 137 | check_jni_abort_catcher.Check("name == null"); |
| 138 | |
| 139 | // We support . as well as / for compatibility, if -Xcheck:jni is off. |
| 140 | ExpectClassFound("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'"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 142 | ExpectClassNotFound("Ljava.lang.String;"); |
Elliott Hughes | 56ef042 | 2012-06-19 14:35:04 -0700 | [diff] [blame] | 143 | check_jni_abort_catcher.Check("illegal class name 'Ljava.lang.String;'"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 144 | ExpectClassFound("[Ljava.lang.String;"); |
Elliott Hughes | 56ef042 | 2012-06-19 14:35:04 -0700 | [diff] [blame] | 145 | check_jni_abort_catcher.Check("illegal class name '[Ljava.lang.String;'"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 146 | ExpectClassNotFound("[java.lang.String"); |
Elliott Hughes | 56ef042 | 2012-06-19 14:35:04 -0700 | [diff] [blame] | 147 | check_jni_abort_catcher.Check("illegal class name '[java.lang.String'"); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 148 | |
| 149 | // You can't include the "L;" in a JNI class descriptor. |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 150 | ExpectClassNotFound("Ljava/lang/String;"); |
Elliott Hughes | 56ef042 | 2012-06-19 14:35:04 -0700 | [diff] [blame] | 151 | check_jni_abort_catcher.Check("illegal class name 'Ljava/lang/String;'"); |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 152 | |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 153 | // But you must include it for an array of any reference type. |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 154 | ExpectClassNotFound("[java/lang/String"); |
Elliott Hughes | 56ef042 | 2012-06-19 14:35:04 -0700 | [diff] [blame] | 155 | check_jni_abort_catcher.Check("illegal class name '[java/lang/String'"); |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 156 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 157 | ExpectClassNotFound("[K"); |
Elliott Hughes | 56ef042 | 2012-06-19 14:35:04 -0700 | [diff] [blame] | 158 | check_jni_abort_catcher.Check("illegal class name '[K'"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 159 | |
| 160 | // Void arrays aren't allowed. |
| 161 | ExpectClassNotFound("[V"); |
| 162 | check_jni_abort_catcher.Check("illegal class name '[V'"); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 163 | } |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 164 | |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 165 | // But primitive types aren't allowed... |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 166 | ExpectClassNotFound("C"); |
| 167 | ExpectClassNotFound("V"); |
| 168 | ExpectClassNotFound("K"); |
Elliott Hughes | 0c9cd56 | 2011-08-12 10:59:29 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 171 | TEST_F(JniInternalTest, GetFieldID) { |
| 172 | jclass jlnsfe = env_->FindClass("java/lang/NoSuchFieldError"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 173 | ASSERT_NE(jlnsfe, nullptr); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 174 | jclass c = env_->FindClass("java/lang/String"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 175 | ASSERT_NE(c, nullptr); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 176 | |
| 177 | // Wrong type. |
| 178 | jfieldID fid = env_->GetFieldID(c, "count", "J"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 179 | EXPECT_EQ(nullptr, fid); |
| 180 | ExpectException(jlnsfe); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 181 | |
Ian Rogers | b17d08b | 2011-09-02 16:16:49 -0700 | [diff] [blame] | 182 | // Wrong type where type doesn't exist. |
| 183 | fid = env_->GetFieldID(c, "count", "Lrod/jane/freddy;"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 184 | EXPECT_EQ(nullptr, fid); |
| 185 | ExpectException(jlnsfe); |
Ian Rogers | b17d08b | 2011-09-02 16:16:49 -0700 | [diff] [blame] | 186 | |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 187 | // Wrong name. |
| 188 | fid = env_->GetFieldID(c, "Count", "I"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 189 | EXPECT_EQ(nullptr, fid); |
| 190 | ExpectException(jlnsfe); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 191 | |
| 192 | // Good declared field lookup. |
| 193 | fid = env_->GetFieldID(c, "count", "I"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 194 | EXPECT_NE(nullptr, fid); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 195 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 196 | |
| 197 | // Good superclass field lookup. |
| 198 | c = env_->FindClass("java/lang/StringBuilder"); |
| 199 | fid = env_->GetFieldID(c, "count", "I"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 200 | EXPECT_NE(nullptr, fid); |
| 201 | EXPECT_NE(fid, nullptr); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 202 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 203 | |
| 204 | // Not instance. |
| 205 | fid = env_->GetFieldID(c, "CASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 206 | EXPECT_EQ(nullptr, fid); |
| 207 | ExpectException(jlnsfe); |
| 208 | |
| 209 | // Bad arguments. |
| 210 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 211 | fid = env_->GetFieldID(nullptr, "count", "I"); |
| 212 | EXPECT_EQ(nullptr, fid); |
| 213 | check_jni_abort_catcher.Check("java_class == null"); |
| 214 | fid = env_->GetFieldID(c, nullptr, "I"); |
| 215 | EXPECT_EQ(nullptr, fid); |
| 216 | check_jni_abort_catcher.Check("name == null"); |
| 217 | fid = env_->GetFieldID(c, "count", nullptr); |
| 218 | EXPECT_EQ(nullptr, fid); |
| 219 | check_jni_abort_catcher.Check("sig == null"); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | TEST_F(JniInternalTest, GetStaticFieldID) { |
| 223 | jclass jlnsfe = env_->FindClass("java/lang/NoSuchFieldError"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 224 | ASSERT_NE(jlnsfe, nullptr); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 225 | jclass c = env_->FindClass("java/lang/String"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 226 | ASSERT_NE(c, nullptr); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 227 | |
| 228 | // Wrong type. |
| 229 | jfieldID fid = env_->GetStaticFieldID(c, "CASE_INSENSITIVE_ORDER", "J"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 230 | EXPECT_EQ(nullptr, fid); |
| 231 | ExpectException(jlnsfe); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 232 | |
Ian Rogers | b17d08b | 2011-09-02 16:16:49 -0700 | [diff] [blame] | 233 | // Wrong type where type doesn't exist. |
| 234 | fid = env_->GetStaticFieldID(c, "CASE_INSENSITIVE_ORDER", "Lrod/jane/freddy;"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 235 | EXPECT_EQ(nullptr, fid); |
| 236 | ExpectException(jlnsfe); |
Ian Rogers | b17d08b | 2011-09-02 16:16:49 -0700 | [diff] [blame] | 237 | |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 238 | // Wrong name. |
| 239 | fid = env_->GetStaticFieldID(c, "cASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 240 | EXPECT_EQ(nullptr, fid); |
| 241 | ExpectException(jlnsfe); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 242 | |
| 243 | // Good declared field lookup. |
| 244 | fid = env_->GetStaticFieldID(c, "CASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 245 | EXPECT_NE(nullptr, fid); |
| 246 | EXPECT_NE(fid, nullptr); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 247 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 248 | |
| 249 | // Not static. |
| 250 | fid = env_->GetStaticFieldID(c, "count", "I"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 251 | EXPECT_EQ(nullptr, fid); |
| 252 | ExpectException(jlnsfe); |
| 253 | |
| 254 | // Bad arguments. |
| 255 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 256 | fid = env_->GetStaticFieldID(nullptr, "CASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;"); |
| 257 | EXPECT_EQ(nullptr, fid); |
| 258 | check_jni_abort_catcher.Check("java_class == null"); |
| 259 | fid = env_->GetStaticFieldID(c, nullptr, "Ljava/util/Comparator;"); |
| 260 | EXPECT_EQ(nullptr, fid); |
| 261 | check_jni_abort_catcher.Check("name == null"); |
| 262 | fid = env_->GetStaticFieldID(c, "CASE_INSENSITIVE_ORDER", nullptr); |
| 263 | EXPECT_EQ(nullptr, fid); |
| 264 | check_jni_abort_catcher.Check("sig == null"); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 267 | TEST_F(JniInternalTest, GetMethodID) { |
| 268 | jclass jlobject = env_->FindClass("java/lang/Object"); |
| 269 | jclass jlstring = env_->FindClass("java/lang/String"); |
| 270 | jclass jlnsme = env_->FindClass("java/lang/NoSuchMethodError"); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 271 | jclass jncrbc = env_->FindClass("java/nio/channels/ReadableByteChannel"); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 272 | |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 273 | // Sanity check that no exceptions are pending. |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 274 | ASSERT_FALSE(env_->ExceptionCheck()); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 275 | |
| 276 | // Check that java.lang.Object.foo() doesn't exist and NoSuchMethodError is |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 277 | // a pending exception. |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 278 | jmethodID method = env_->GetMethodID(jlobject, "foo", "()V"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 279 | EXPECT_EQ(nullptr, method); |
| 280 | ExpectException(jlnsme); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 281 | |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 282 | // Check that java.lang.Object.equals() does exist. |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 283 | method = env_->GetMethodID(jlobject, "equals", "(Ljava/lang/Object;)Z"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 284 | EXPECT_NE(nullptr, method); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 285 | EXPECT_FALSE(env_->ExceptionCheck()); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 286 | |
| 287 | // Check that GetMethodID for java.lang.String.valueOf(int) fails as the |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 288 | // method is static. |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 289 | method = env_->GetMethodID(jlstring, "valueOf", "(I)Ljava/lang/String;"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 290 | EXPECT_EQ(nullptr, method); |
| 291 | ExpectException(jlnsme); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 292 | |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 293 | // Check that GetMethodID for java.lang.NoSuchMethodError.<init>(String) finds the constructor. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 294 | method = env_->GetMethodID(jlnsme, "<init>", "(Ljava/lang/String;)V"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 295 | EXPECT_NE(nullptr, method); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 296 | EXPECT_FALSE(env_->ExceptionCheck()); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 297 | |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 298 | // Check that GetMethodID can find a interface method inherited from another interface. |
| 299 | method = env_->GetMethodID(jncrbc, "close", "()V"); |
| 300 | EXPECT_NE(nullptr, method); |
| 301 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 302 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 303 | // Bad arguments. |
| 304 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 305 | method = env_->GetMethodID(nullptr, "<init>", "(Ljava/lang/String;)V"); |
| 306 | EXPECT_EQ(nullptr, method); |
| 307 | check_jni_abort_catcher.Check("java_class == null"); |
| 308 | method = env_->GetMethodID(jlnsme, nullptr, "(Ljava/lang/String;)V"); |
| 309 | EXPECT_EQ(nullptr, method); |
| 310 | check_jni_abort_catcher.Check("name == null"); |
| 311 | method = env_->GetMethodID(jlnsme, "<init>", nullptr); |
| 312 | EXPECT_EQ(nullptr, method); |
| 313 | check_jni_abort_catcher.Check("sig == null"); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | TEST_F(JniInternalTest, GetStaticMethodID) { |
| 317 | jclass jlobject = env_->FindClass("java/lang/Object"); |
| 318 | jclass jlnsme = env_->FindClass("java/lang/NoSuchMethodError"); |
| 319 | |
| 320 | // Sanity check that no exceptions are pending |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 321 | ASSERT_FALSE(env_->ExceptionCheck()); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 322 | |
| 323 | // Check that java.lang.Object.foo() doesn't exist and NoSuchMethodError is |
| 324 | // a pending exception |
| 325 | jmethodID method = env_->GetStaticMethodID(jlobject, "foo", "()V"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 326 | EXPECT_EQ(nullptr, method); |
| 327 | ExpectException(jlnsme); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 328 | |
| 329 | // Check that GetStaticMethodID for java.lang.Object.equals(Object) fails as |
| 330 | // the method is not static |
| 331 | method = env_->GetStaticMethodID(jlobject, "equals", "(Ljava/lang/Object;)Z"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 332 | EXPECT_EQ(nullptr, method); |
| 333 | ExpectException(jlnsme); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 334 | |
| 335 | // Check that java.lang.String.valueOf(int) does exist |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 336 | jclass jlstring = env_->FindClass("java/lang/String"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 337 | method = env_->GetStaticMethodID(jlstring, "valueOf", "(I)Ljava/lang/String;"); |
| 338 | EXPECT_NE(nullptr, method); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 339 | EXPECT_FALSE(env_->ExceptionCheck()); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 340 | |
| 341 | // Bad arguments. |
| 342 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 343 | method = env_->GetStaticMethodID(nullptr, "valueOf", "(I)Ljava/lang/String;"); |
| 344 | EXPECT_EQ(nullptr, method); |
| 345 | check_jni_abort_catcher.Check("java_class == null"); |
| 346 | method = env_->GetStaticMethodID(jlstring, nullptr, "(I)Ljava/lang/String;"); |
| 347 | EXPECT_EQ(nullptr, method); |
| 348 | check_jni_abort_catcher.Check("name == null"); |
| 349 | method = env_->GetStaticMethodID(jlstring, "valueOf", nullptr); |
| 350 | EXPECT_EQ(nullptr, method); |
| 351 | check_jni_abort_catcher.Check("sig == null"); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 352 | } |
| 353 | |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 354 | TEST_F(JniInternalTest, FromReflectedField_ToReflectedField) { |
| 355 | jclass jlrField = env_->FindClass("java/lang/reflect/Field"); |
| 356 | jclass c = env_->FindClass("java/lang/String"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 357 | ASSERT_NE(c, nullptr); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 358 | jfieldID fid = env_->GetFieldID(c, "count", "I"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 359 | ASSERT_NE(fid, nullptr); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 360 | // Turn the fid into a java.lang.reflect.Field... |
| 361 | jobject field = env_->ToReflectedField(c, fid, JNI_FALSE); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 362 | ASSERT_NE(c, nullptr); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 363 | ASSERT_TRUE(env_->IsInstanceOf(field, jlrField)); |
| 364 | // ...and back again. |
| 365 | jfieldID fid2 = env_->FromReflectedField(field); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 366 | ASSERT_NE(fid2, nullptr); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 367 | // Make sure we can actually use it. |
| 368 | jstring s = env_->NewStringUTF("poop"); |
| 369 | ASSERT_EQ(4, env_->GetIntField(s, fid2)); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 370 | |
| 371 | // Bad arguments. |
| 372 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 373 | field = env_->ToReflectedField(c, nullptr, JNI_FALSE); |
| 374 | EXPECT_EQ(field, nullptr); |
| 375 | check_jni_abort_catcher.Check("fid == null"); |
| 376 | fid2 = env_->FromReflectedField(nullptr); |
| 377 | ASSERT_EQ(fid2, nullptr); |
| 378 | check_jni_abort_catcher.Check("jlr_field == null"); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | TEST_F(JniInternalTest, FromReflectedMethod_ToReflectedMethod) { |
| 382 | jclass jlrMethod = env_->FindClass("java/lang/reflect/Method"); |
Sebastien Hertz | d333376 | 2014-06-26 14:45:07 +0200 | [diff] [blame] | 383 | ASSERT_NE(jlrMethod, nullptr); |
| 384 | jclass jlrConstructor = env_->FindClass("java/lang/reflect/Constructor"); |
| 385 | ASSERT_NE(jlrConstructor, nullptr); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 386 | jclass c = env_->FindClass("java/lang/String"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 387 | ASSERT_NE(c, nullptr); |
Sebastien Hertz | d333376 | 2014-06-26 14:45:07 +0200 | [diff] [blame] | 388 | |
| 389 | jmethodID mid = env_->GetMethodID(c, "<init>", "()V"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 390 | ASSERT_NE(mid, nullptr); |
Sebastien Hertz | d333376 | 2014-06-26 14:45:07 +0200 | [diff] [blame] | 391 | // Turn the mid into a java.lang.reflect.Constructor... |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 392 | jobject method = env_->ToReflectedMethod(c, mid, JNI_FALSE); |
Sebastien Hertz | d333376 | 2014-06-26 14:45:07 +0200 | [diff] [blame] | 393 | ASSERT_NE(method, nullptr); |
| 394 | ASSERT_TRUE(env_->IsInstanceOf(method, jlrConstructor)); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 395 | // ...and back again. |
| 396 | jmethodID mid2 = env_->FromReflectedMethod(method); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 397 | ASSERT_NE(mid2, nullptr); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 398 | // Make sure we can actually use it. |
Sebastien Hertz | d333376 | 2014-06-26 14:45:07 +0200 | [diff] [blame] | 399 | jstring s = reinterpret_cast<jstring>(env_->AllocObject(c)); |
| 400 | ASSERT_NE(s, nullptr); |
| 401 | env_->CallVoidMethod(s, mid2); |
| 402 | ASSERT_EQ(JNI_FALSE, env_->ExceptionCheck()); |
| 403 | |
| 404 | mid = env_->GetMethodID(c, "length", "()I"); |
| 405 | ASSERT_NE(mid, nullptr); |
| 406 | // Turn the mid into a java.lang.reflect.Method... |
| 407 | method = env_->ToReflectedMethod(c, mid, JNI_FALSE); |
| 408 | ASSERT_NE(method, nullptr); |
| 409 | ASSERT_TRUE(env_->IsInstanceOf(method, jlrMethod)); |
| 410 | // ...and back again. |
| 411 | mid2 = env_->FromReflectedMethod(method); |
| 412 | ASSERT_NE(mid2, nullptr); |
| 413 | // Make sure we can actually use it. |
| 414 | s = env_->NewStringUTF("poop"); |
| 415 | ASSERT_NE(s, nullptr); |
Ian Rogers | 5d27faf | 2014-05-02 17:17:18 -0700 | [diff] [blame] | 416 | ASSERT_EQ(4, env_->CallIntMethod(s, mid2)); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 417 | |
| 418 | // Bad arguments. |
| 419 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 420 | method = env_->ToReflectedMethod(c, nullptr, JNI_FALSE); |
| 421 | EXPECT_EQ(method, nullptr); |
| 422 | check_jni_abort_catcher.Check("mid == null"); |
| 423 | mid2 = env_->FromReflectedMethod(method); |
| 424 | ASSERT_EQ(mid2, nullptr); |
| 425 | check_jni_abort_catcher.Check("jlr_method == null"); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 426 | } |
| 427 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 428 | static void BogusMethod() { |
| 429 | // You can't pass nullptr function pointers to RegisterNatives. |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 430 | } |
| 431 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 432 | TEST_F(JniInternalTest, RegisterAndUnregisterNatives) { |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 433 | jclass jlobject = env_->FindClass("java/lang/Object"); |
| 434 | jclass jlnsme = env_->FindClass("java/lang/NoSuchMethodError"); |
| 435 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 436 | // Sanity check that no exceptions are pending. |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 437 | ASSERT_FALSE(env_->ExceptionCheck()); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 438 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 439 | // 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] | 440 | { |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 441 | JNINativeMethod methods[] = { { "foo", "()V", nullptr } }; |
| 442 | EXPECT_EQ(env_->RegisterNatives(jlobject, methods, 1), JNI_ERR); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 443 | } |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 444 | ExpectException(jlnsme); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 445 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 446 | // Check that registering non-native methods causes a NoSuchMethodError. |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 447 | { |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 448 | JNINativeMethod methods[] = { { "equals", "(Ljava/lang/Object;)Z", nullptr } }; |
| 449 | EXPECT_EQ(env_->RegisterNatives(jlobject, methods, 1), JNI_ERR); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 450 | } |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 451 | ExpectException(jlnsme); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 452 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 453 | // Check that registering native methods is successful. |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 454 | { |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 455 | JNINativeMethod methods[] = { { "notify", "()V", reinterpret_cast<void*>(BogusMethod) } }; |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 456 | EXPECT_EQ(env_->RegisterNatives(jlobject, methods, 1), JNI_OK); |
| 457 | } |
| 458 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 459 | EXPECT_EQ(env_->UnregisterNatives(jlobject), JNI_OK); |
| 460 | |
| 461 | // Check that registering no methods isn't a failure. |
| 462 | { |
| 463 | JNINativeMethod methods[] = { }; |
| 464 | EXPECT_EQ(env_->RegisterNatives(jlobject, methods, 0), JNI_OK); |
| 465 | } |
| 466 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 467 | EXPECT_EQ(env_->UnregisterNatives(jlobject), JNI_OK); |
| 468 | |
| 469 | // Check that registering a -ve number of methods is a failure. |
| 470 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 471 | for (int i = -10; i < 0; ++i) { |
| 472 | JNINativeMethod methods[] = { }; |
| 473 | EXPECT_EQ(env_->RegisterNatives(jlobject, methods, i), JNI_ERR); |
| 474 | check_jni_abort_catcher.Check("negative method count: "); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 475 | } |
| 476 | EXPECT_FALSE(env_->ExceptionCheck()); |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 477 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 478 | // Passing a class of null is a failure. |
| 479 | { |
| 480 | JNINativeMethod methods[] = { }; |
| 481 | EXPECT_EQ(env_->RegisterNatives(nullptr, methods, 0), JNI_ERR); |
| 482 | check_jni_abort_catcher.Check("java_class == null"); |
| 483 | } |
| 484 | |
| 485 | // Passing methods as null is a failure. |
| 486 | EXPECT_EQ(env_->RegisterNatives(jlobject, nullptr, 1), JNI_ERR); |
| 487 | check_jni_abort_catcher.Check("methods == null"); |
| 488 | |
| 489 | // Unregisters null is a failure. |
| 490 | EXPECT_EQ(env_->UnregisterNatives(nullptr), JNI_ERR); |
| 491 | check_jni_abort_catcher.Check("java_class == null"); |
| 492 | |
| 493 | // Unregistering a class with no natives is a warning. |
| 494 | EXPECT_EQ(env_->UnregisterNatives(jlnsme), JNI_OK); |
Ian Rogers | 4dd71f1 | 2011-08-16 14:16:02 -0700 | [diff] [blame] | 495 | } |
| 496 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 497 | #define EXPECT_PRIMITIVE_ARRAY(new_fn, \ |
| 498 | get_region_fn, \ |
| 499 | set_region_fn, \ |
| 500 | get_elements_fn, \ |
| 501 | release_elements_fn, \ |
| 502 | scalar_type, \ |
| 503 | expected_class_descriptor) \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 504 | jsize size = 4; \ |
| 505 | \ |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 506 | { \ |
| 507 | CheckJniAbortCatcher jni_abort_catcher; \ |
| 508 | /* 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] | 509 | EXPECT_EQ(env_->new_fn(-1), nullptr); \ |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 510 | jni_abort_catcher.Check("negative array length: -1"); \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 511 | EXPECT_EQ(env_->new_fn(std::numeric_limits<jint>::min()), nullptr); \ |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 512 | jni_abort_catcher.Check("negative array length: -2147483648"); \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 513 | /* Pass the array as null. */ \ |
| 514 | EXPECT_EQ(0, env_->GetArrayLength(nullptr)); \ |
| 515 | jni_abort_catcher.Check("java_array == null"); \ |
| 516 | env_->get_region_fn(nullptr, 0, 0, nullptr); \ |
| 517 | jni_abort_catcher.Check("java_array == null"); \ |
| 518 | env_->set_region_fn(nullptr, 0, 0, nullptr); \ |
| 519 | jni_abort_catcher.Check("java_array == null"); \ |
| 520 | env_->get_elements_fn(nullptr, nullptr); \ |
| 521 | jni_abort_catcher.Check("java_array == null"); \ |
| 522 | env_->release_elements_fn(nullptr, nullptr, 0); \ |
| 523 | jni_abort_catcher.Check("java_array == null"); \ |
| 524 | /* Pass the elements for region as null. */ \ |
| 525 | scalar_type ## Array a = env_->new_fn(size); \ |
| 526 | env_->get_region_fn(a, 0, size, nullptr); \ |
| 527 | jni_abort_catcher.Check("buf == null"); \ |
| 528 | env_->set_region_fn(a, 0, size, nullptr); \ |
| 529 | jni_abort_catcher.Check("buf == null"); \ |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 530 | } \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 531 | /* Allocate an array and check it has the right type and length. */ \ |
| 532 | scalar_type ## Array a = env_->new_fn(size); \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 533 | EXPECT_NE(a, nullptr); \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 534 | EXPECT_TRUE(env_->IsInstanceOf(a, env_->FindClass(expected_class_descriptor))); \ |
| 535 | EXPECT_EQ(size, env_->GetArrayLength(a)); \ |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 536 | \ |
| 537 | /* GetPrimitiveArrayRegion/SetPrimitiveArrayRegion */ \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 538 | /* AIOOBE for negative start offset. */ \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 539 | env_->get_region_fn(a, -1, 1, nullptr); \ |
| 540 | ExpectException(aioobe_); \ |
| 541 | env_->set_region_fn(a, -1, 1, nullptr); \ |
| 542 | ExpectException(aioobe_); \ |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 543 | \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 544 | /* AIOOBE for negative length. */ \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 545 | env_->get_region_fn(a, 0, -1, nullptr); \ |
| 546 | ExpectException(aioobe_); \ |
| 547 | env_->set_region_fn(a, 0, -1, nullptr); \ |
| 548 | ExpectException(aioobe_); \ |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 549 | \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 550 | /* AIOOBE for buffer overrun. */ \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 551 | env_->get_region_fn(a, size - 1, size, nullptr); \ |
| 552 | ExpectException(aioobe_); \ |
| 553 | env_->set_region_fn(a, size - 1, size, nullptr); \ |
| 554 | ExpectException(aioobe_); \ |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 555 | \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 556 | /* It's okay for the buffer to be nullptr as long as the length is 0. */ \ |
| 557 | env_->get_region_fn(a, 2, 0, nullptr); \ |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 558 | /* Even if the offset is invalid... */ \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 559 | env_->get_region_fn(a, 123, 0, nullptr); \ |
| 560 | ExpectException(aioobe_); \ |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 561 | \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 562 | /* It's okay for the buffer to be nullptr as long as the length is 0. */ \ |
| 563 | env_->set_region_fn(a, 2, 0, nullptr); \ |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 564 | /* Even if the offset is invalid... */ \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 565 | env_->set_region_fn(a, 123, 0, nullptr); \ |
| 566 | ExpectException(aioobe_); \ |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 567 | \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 568 | /* Prepare a couple of buffers. */ \ |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 569 | std::unique_ptr<scalar_type[]> src_buf(new scalar_type[size]); \ |
| 570 | std::unique_ptr<scalar_type[]> dst_buf(new scalar_type[size]); \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 571 | for (jsize i = 0; i < size; ++i) { src_buf[i] = scalar_type(i); } \ |
| 572 | 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] | 573 | \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 574 | /* Copy all of src_buf onto the heap. */ \ |
Elliott Hughes | ee0fa76 | 2012-03-26 17:12:41 -0700 | [diff] [blame] | 575 | env_->set_region_fn(a, 0, size, &src_buf[0]); \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 576 | /* Copy back only part. */ \ |
| 577 | env_->get_region_fn(a, 1, size - 2, &dst_buf[1]); \ |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 578 | EXPECT_NE(memcmp(&src_buf[0], &dst_buf[0], size * sizeof(scalar_type)), 0) \ |
| 579 | << "short copy equal"; \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 580 | /* Copy the missing pieces. */ \ |
Elliott Hughes | ee0fa76 | 2012-03-26 17:12:41 -0700 | [diff] [blame] | 581 | env_->get_region_fn(a, 0, 1, &dst_buf[0]); \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 582 | env_->get_region_fn(a, size - 1, 1, &dst_buf[size - 1]); \ |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 583 | EXPECT_EQ(memcmp(&src_buf[0], &dst_buf[0], size * sizeof(scalar_type)), 0) \ |
| 584 | << "fixed copy not equal"; \ |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 585 | /* Copy back the whole array. */ \ |
Elliott Hughes | ee0fa76 | 2012-03-26 17:12:41 -0700 | [diff] [blame] | 586 | env_->get_region_fn(a, 0, size, &dst_buf[0]); \ |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 587 | EXPECT_EQ(memcmp(&src_buf[0], &dst_buf[0], size * sizeof(scalar_type)), 0) \ |
| 588 | << "full copy not equal"; \ |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 589 | /* GetPrimitiveArrayCritical */ \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 590 | void* v = env_->GetPrimitiveArrayCritical(a, nullptr); \ |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 591 | EXPECT_EQ(memcmp(&src_buf[0], v, size * sizeof(scalar_type)), 0) \ |
| 592 | << "GetPrimitiveArrayCritical not equal"; \ |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 593 | env_->ReleasePrimitiveArrayCritical(a, v, 0); \ |
| 594 | /* GetXArrayElements */ \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 595 | scalar_type* xs = env_->get_elements_fn(a, nullptr); \ |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 596 | EXPECT_EQ(memcmp(&src_buf[0], xs, size * sizeof(scalar_type)), 0) \ |
| 597 | << # get_elements_fn " not equal"; \ |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 598 | env_->release_elements_fn(a, xs, 0); \ |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 599 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 600 | TEST_F(JniInternalTest, BooleanArrays) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 601 | EXPECT_PRIMITIVE_ARRAY(NewBooleanArray, GetBooleanArrayRegion, SetBooleanArrayRegion, |
| 602 | GetBooleanArrayElements, ReleaseBooleanArrayElements, jboolean, "[Z"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 603 | } |
| 604 | TEST_F(JniInternalTest, ByteArrays) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 605 | EXPECT_PRIMITIVE_ARRAY(NewByteArray, GetByteArrayRegion, SetByteArrayRegion, |
| 606 | GetByteArrayElements, ReleaseByteArrayElements, jbyte, "[B"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 607 | } |
| 608 | TEST_F(JniInternalTest, CharArrays) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 609 | EXPECT_PRIMITIVE_ARRAY(NewCharArray, GetCharArrayRegion, SetCharArrayRegion, |
| 610 | GetCharArrayElements, ReleaseCharArrayElements, jchar, "[C"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 611 | } |
| 612 | TEST_F(JniInternalTest, DoubleArrays) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 613 | EXPECT_PRIMITIVE_ARRAY(NewDoubleArray, GetDoubleArrayRegion, SetDoubleArrayRegion, |
| 614 | GetDoubleArrayElements, ReleaseDoubleArrayElements, jdouble, "[D"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 615 | } |
| 616 | TEST_F(JniInternalTest, FloatArrays) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 617 | EXPECT_PRIMITIVE_ARRAY(NewFloatArray, GetFloatArrayRegion, SetFloatArrayRegion, |
| 618 | GetFloatArrayElements, ReleaseFloatArrayElements, jfloat, "[F"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 619 | } |
| 620 | TEST_F(JniInternalTest, IntArrays) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 621 | EXPECT_PRIMITIVE_ARRAY(NewIntArray, GetIntArrayRegion, SetIntArrayRegion, |
| 622 | GetIntArrayElements, ReleaseIntArrayElements, jint, "[I"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 623 | } |
| 624 | TEST_F(JniInternalTest, LongArrays) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 625 | EXPECT_PRIMITIVE_ARRAY(NewLongArray, GetLongArrayRegion, SetLongArrayRegion, |
| 626 | GetLongArrayElements, ReleaseLongArrayElements, jlong, "[J"); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 627 | } |
| 628 | TEST_F(JniInternalTest, ShortArrays) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 629 | EXPECT_PRIMITIVE_ARRAY(NewShortArray, GetShortArrayRegion, SetShortArrayRegion, |
| 630 | GetShortArrayElements, ReleaseShortArrayElements, jshort, "[S"); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 631 | } |
| 632 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 633 | TEST_F(JniInternalTest, GetPrimitiveArrayElementsOfWrongType) { |
| 634 | CheckJniAbortCatcher jni_abort_catcher; |
| 635 | jbooleanArray array = env_->NewBooleanArray(10); |
| 636 | jboolean is_copy; |
| 637 | EXPECT_EQ(env_->GetByteArrayElements(reinterpret_cast<jbyteArray>(array), &is_copy), nullptr); |
| 638 | jni_abort_catcher.Check( |
| 639 | "attempt to get byte primitive array elements with an object of type boolean[]"); |
| 640 | EXPECT_EQ(env_->GetShortArrayElements(reinterpret_cast<jshortArray>(array), &is_copy), nullptr); |
| 641 | jni_abort_catcher.Check( |
| 642 | "attempt to get short primitive array elements with an object of type boolean[]"); |
| 643 | EXPECT_EQ(env_->GetCharArrayElements(reinterpret_cast<jcharArray>(array), &is_copy), nullptr); |
| 644 | jni_abort_catcher.Check( |
| 645 | "attempt to get char primitive array elements with an object of type boolean[]"); |
| 646 | EXPECT_EQ(env_->GetIntArrayElements(reinterpret_cast<jintArray>(array), &is_copy), nullptr); |
| 647 | jni_abort_catcher.Check( |
| 648 | "attempt to get int primitive array elements with an object of type boolean[]"); |
| 649 | EXPECT_EQ(env_->GetLongArrayElements(reinterpret_cast<jlongArray>(array), &is_copy), nullptr); |
| 650 | jni_abort_catcher.Check( |
| 651 | "attempt to get long primitive array elements with an object of type boolean[]"); |
| 652 | EXPECT_EQ(env_->GetFloatArrayElements(reinterpret_cast<jfloatArray>(array), &is_copy), nullptr); |
| 653 | jni_abort_catcher.Check( |
| 654 | "attempt to get float primitive array elements with an object of type boolean[]"); |
| 655 | EXPECT_EQ(env_->GetDoubleArrayElements(reinterpret_cast<jdoubleArray>(array), &is_copy), nullptr); |
| 656 | jni_abort_catcher.Check( |
| 657 | "attempt to get double primitive array elements with an object of type boolean[]"); |
| 658 | jbyteArray array2 = env_->NewByteArray(10); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 659 | EXPECT_EQ(env_->GetBooleanArrayElements(reinterpret_cast<jbooleanArray>(array2), &is_copy), |
| 660 | nullptr); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 661 | jni_abort_catcher.Check( |
| 662 | "attempt to get boolean primitive array elements with an object of type byte[]"); |
| 663 | jobject object = env_->NewStringUTF("Test String"); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 664 | EXPECT_EQ(env_->GetBooleanArrayElements(reinterpret_cast<jbooleanArray>(object), &is_copy), |
| 665 | nullptr); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 666 | jni_abort_catcher.Check( |
| 667 | "attempt to get boolean primitive array elements with an object of type java.lang.String"); |
| 668 | } |
| 669 | |
| 670 | TEST_F(JniInternalTest, ReleasePrimitiveArrayElementsOfWrongType) { |
| 671 | CheckJniAbortCatcher jni_abort_catcher; |
| 672 | jbooleanArray array = env_->NewBooleanArray(10); |
| 673 | ASSERT_TRUE(array != nullptr); |
| 674 | jboolean is_copy; |
| 675 | jboolean* elements = env_->GetBooleanArrayElements(array, &is_copy); |
| 676 | ASSERT_TRUE(elements != nullptr); |
| 677 | env_->ReleaseByteArrayElements(reinterpret_cast<jbyteArray>(array), |
| 678 | reinterpret_cast<jbyte*>(elements), 0); |
| 679 | jni_abort_catcher.Check( |
| 680 | "attempt to release byte primitive array elements with an object of type boolean[]"); |
| 681 | env_->ReleaseShortArrayElements(reinterpret_cast<jshortArray>(array), |
| 682 | reinterpret_cast<jshort*>(elements), 0); |
| 683 | jni_abort_catcher.Check( |
| 684 | "attempt to release short primitive array elements with an object of type boolean[]"); |
| 685 | env_->ReleaseCharArrayElements(reinterpret_cast<jcharArray>(array), |
| 686 | reinterpret_cast<jchar*>(elements), 0); |
| 687 | jni_abort_catcher.Check( |
| 688 | "attempt to release char primitive array elements with an object of type boolean[]"); |
| 689 | env_->ReleaseIntArrayElements(reinterpret_cast<jintArray>(array), |
| 690 | reinterpret_cast<jint*>(elements), 0); |
| 691 | jni_abort_catcher.Check( |
| 692 | "attempt to release int primitive array elements with an object of type boolean[]"); |
| 693 | env_->ReleaseLongArrayElements(reinterpret_cast<jlongArray>(array), |
| 694 | reinterpret_cast<jlong*>(elements), 0); |
| 695 | jni_abort_catcher.Check( |
| 696 | "attempt to release long primitive array elements with an object of type boolean[]"); |
| 697 | env_->ReleaseFloatArrayElements(reinterpret_cast<jfloatArray>(array), |
| 698 | reinterpret_cast<jfloat*>(elements), 0); |
| 699 | jni_abort_catcher.Check( |
| 700 | "attempt to release float primitive array elements with an object of type boolean[]"); |
| 701 | env_->ReleaseDoubleArrayElements(reinterpret_cast<jdoubleArray>(array), |
| 702 | reinterpret_cast<jdouble*>(elements), 0); |
| 703 | jni_abort_catcher.Check( |
| 704 | "attempt to release double primitive array elements with an object of type boolean[]"); |
| 705 | jbyteArray array2 = env_->NewByteArray(10); |
| 706 | env_->ReleaseBooleanArrayElements(reinterpret_cast<jbooleanArray>(array2), elements, 0); |
| 707 | jni_abort_catcher.Check( |
| 708 | "attempt to release boolean primitive array elements with an object of type byte[]"); |
| 709 | jobject object = env_->NewStringUTF("Test String"); |
| 710 | env_->ReleaseBooleanArrayElements(reinterpret_cast<jbooleanArray>(object), elements, 0); |
| 711 | jni_abort_catcher.Check( |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 712 | "attempt to release boolean primitive array elements with an object of type " |
| 713 | "java.lang.String"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 714 | } |
| 715 | TEST_F(JniInternalTest, GetReleasePrimitiveArrayCriticalOfWrongType) { |
| 716 | CheckJniAbortCatcher jni_abort_catcher; |
| 717 | jobject object = env_->NewStringUTF("Test String"); |
| 718 | jboolean is_copy; |
| 719 | void* elements = env_->GetPrimitiveArrayCritical(reinterpret_cast<jarray>(object), &is_copy); |
| 720 | jni_abort_catcher.Check("expected primitive array, given java.lang.String"); |
| 721 | env_->ReleasePrimitiveArrayCritical(reinterpret_cast<jarray>(object), elements, 0); |
| 722 | jni_abort_catcher.Check("expected primitive array, given java.lang.String"); |
| 723 | } |
| 724 | |
| 725 | TEST_F(JniInternalTest, GetPrimitiveArrayRegionElementsOfWrongType) { |
| 726 | CheckJniAbortCatcher jni_abort_catcher; |
| 727 | constexpr size_t kLength = 10; |
| 728 | jbooleanArray array = env_->NewBooleanArray(kLength); |
| 729 | ASSERT_TRUE(array != nullptr); |
| 730 | jboolean elements[kLength]; |
| 731 | env_->GetByteArrayRegion(reinterpret_cast<jbyteArray>(array), 0, kLength, |
| 732 | reinterpret_cast<jbyte*>(elements)); |
| 733 | jni_abort_catcher.Check( |
| 734 | "attempt to get region of byte primitive array elements with an object of type boolean[]"); |
| 735 | env_->GetShortArrayRegion(reinterpret_cast<jshortArray>(array), 0, kLength, |
| 736 | reinterpret_cast<jshort*>(elements)); |
| 737 | jni_abort_catcher.Check( |
| 738 | "attempt to get region of short primitive array elements with an object of type boolean[]"); |
| 739 | env_->GetCharArrayRegion(reinterpret_cast<jcharArray>(array), 0, kLength, |
| 740 | reinterpret_cast<jchar*>(elements)); |
| 741 | jni_abort_catcher.Check( |
| 742 | "attempt to get region of char primitive array elements with an object of type boolean[]"); |
| 743 | env_->GetIntArrayRegion(reinterpret_cast<jintArray>(array), 0, kLength, |
| 744 | reinterpret_cast<jint*>(elements)); |
| 745 | jni_abort_catcher.Check( |
| 746 | "attempt to get region of int primitive array elements with an object of type boolean[]"); |
| 747 | env_->GetLongArrayRegion(reinterpret_cast<jlongArray>(array), 0, kLength, |
| 748 | reinterpret_cast<jlong*>(elements)); |
| 749 | jni_abort_catcher.Check( |
| 750 | "attempt to get region of long primitive array elements with an object of type boolean[]"); |
| 751 | env_->GetFloatArrayRegion(reinterpret_cast<jfloatArray>(array), 0, kLength, |
| 752 | reinterpret_cast<jfloat*>(elements)); |
| 753 | jni_abort_catcher.Check( |
| 754 | "attempt to get region of float primitive array elements with an object of type boolean[]"); |
| 755 | env_->GetDoubleArrayRegion(reinterpret_cast<jdoubleArray>(array), 0, kLength, |
| 756 | reinterpret_cast<jdouble*>(elements)); |
| 757 | jni_abort_catcher.Check( |
| 758 | "attempt to get region of double primitive array elements with an object of type boolean[]"); |
| 759 | jbyteArray array2 = env_->NewByteArray(10); |
| 760 | env_->GetBooleanArrayRegion(reinterpret_cast<jbooleanArray>(array2), 0, kLength, |
| 761 | reinterpret_cast<jboolean*>(elements)); |
| 762 | jni_abort_catcher.Check( |
| 763 | "attempt to get region of boolean primitive array elements with an object of type byte[]"); |
| 764 | jobject object = env_->NewStringUTF("Test String"); |
| 765 | env_->GetBooleanArrayRegion(reinterpret_cast<jbooleanArray>(object), 0, kLength, |
| 766 | reinterpret_cast<jboolean*>(elements)); |
| 767 | jni_abort_catcher.Check( |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 768 | "attempt to get region of boolean primitive array elements with an object of type " |
| 769 | "java.lang.String"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | TEST_F(JniInternalTest, SetPrimitiveArrayRegionElementsOfWrongType) { |
| 773 | CheckJniAbortCatcher jni_abort_catcher; |
| 774 | constexpr size_t kLength = 10; |
| 775 | jbooleanArray array = env_->NewBooleanArray(kLength); |
| 776 | ASSERT_TRUE(array != nullptr); |
| 777 | jboolean elements[kLength]; |
| 778 | env_->SetByteArrayRegion(reinterpret_cast<jbyteArray>(array), 0, kLength, |
| 779 | reinterpret_cast<jbyte*>(elements)); |
| 780 | jni_abort_catcher.Check( |
| 781 | "attempt to set region of byte primitive array elements with an object of type boolean[]"); |
| 782 | env_->SetShortArrayRegion(reinterpret_cast<jshortArray>(array), 0, kLength, |
| 783 | reinterpret_cast<jshort*>(elements)); |
| 784 | jni_abort_catcher.Check( |
| 785 | "attempt to set region of short primitive array elements with an object of type boolean[]"); |
| 786 | env_->SetCharArrayRegion(reinterpret_cast<jcharArray>(array), 0, kLength, |
| 787 | reinterpret_cast<jchar*>(elements)); |
| 788 | jni_abort_catcher.Check( |
| 789 | "attempt to set region of char primitive array elements with an object of type boolean[]"); |
| 790 | env_->SetIntArrayRegion(reinterpret_cast<jintArray>(array), 0, kLength, |
| 791 | reinterpret_cast<jint*>(elements)); |
| 792 | jni_abort_catcher.Check( |
| 793 | "attempt to set region of int primitive array elements with an object of type boolean[]"); |
| 794 | env_->SetLongArrayRegion(reinterpret_cast<jlongArray>(array), 0, kLength, |
| 795 | reinterpret_cast<jlong*>(elements)); |
| 796 | jni_abort_catcher.Check( |
| 797 | "attempt to set region of long primitive array elements with an object of type boolean[]"); |
| 798 | env_->SetFloatArrayRegion(reinterpret_cast<jfloatArray>(array), 0, kLength, |
| 799 | reinterpret_cast<jfloat*>(elements)); |
| 800 | jni_abort_catcher.Check( |
| 801 | "attempt to set region of float primitive array elements with an object of type boolean[]"); |
| 802 | env_->SetDoubleArrayRegion(reinterpret_cast<jdoubleArray>(array), 0, kLength, |
| 803 | reinterpret_cast<jdouble*>(elements)); |
| 804 | jni_abort_catcher.Check( |
| 805 | "attempt to set region of double primitive array elements with an object of type boolean[]"); |
| 806 | jbyteArray array2 = env_->NewByteArray(10); |
| 807 | env_->SetBooleanArrayRegion(reinterpret_cast<jbooleanArray>(array2), 0, kLength, |
| 808 | reinterpret_cast<jboolean*>(elements)); |
| 809 | jni_abort_catcher.Check( |
| 810 | "attempt to set region of boolean primitive array elements with an object of type byte[]"); |
| 811 | jobject object = env_->NewStringUTF("Test String"); |
| 812 | env_->SetBooleanArrayRegion(reinterpret_cast<jbooleanArray>(object), 0, kLength, |
| 813 | reinterpret_cast<jboolean*>(elements)); |
| 814 | jni_abort_catcher.Check( |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 815 | "attempt to set region of boolean primitive array elements with an object of type " |
| 816 | "java.lang.String"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 817 | } |
| 818 | |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 819 | TEST_F(JniInternalTest, NewObjectArray) { |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 820 | jclass element_class = env_->FindClass("java/lang/String"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 821 | ASSERT_NE(element_class, nullptr); |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 822 | jclass array_class = env_->FindClass("[Ljava/lang/String;"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 823 | ASSERT_NE(array_class, nullptr); |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 824 | |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 825 | jobjectArray a = env_->NewObjectArray(0, element_class, nullptr); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 826 | EXPECT_NE(a, nullptr); |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 827 | EXPECT_TRUE(env_->IsInstanceOf(a, array_class)); |
| 828 | EXPECT_EQ(0, env_->GetArrayLength(a)); |
| 829 | |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 830 | a = env_->NewObjectArray(1, element_class, nullptr); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 831 | EXPECT_NE(a, nullptr); |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 832 | EXPECT_TRUE(env_->IsInstanceOf(a, array_class)); |
| 833 | EXPECT_EQ(1, env_->GetArrayLength(a)); |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 834 | EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(a, 0), nullptr)); |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 835 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 836 | // Negative array length checks. |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 837 | CheckJniAbortCatcher jni_abort_catcher; |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 838 | env_->NewObjectArray(-1, element_class, nullptr); |
| 839 | jni_abort_catcher.Check("negative array length: -1"); |
| 840 | |
| 841 | env_->NewObjectArray(std::numeric_limits<jint>::min(), element_class, nullptr); |
| 842 | jni_abort_catcher.Check("negative array length: -2147483648"); |
| 843 | } |
| 844 | |
| 845 | TEST_F(JniInternalTest, NewObjectArrayWithPrimitiveClasses) { |
| 846 | const char* primitive_descriptors = "VZBSCIJFD"; |
| 847 | const char* primitive_names[] = { |
| 848 | "void", "boolean", "byte", "short", "char", "int", "long", "float", "double" |
| 849 | }; |
| 850 | ASSERT_EQ(strlen(primitive_descriptors), arraysize(primitive_names)); |
| 851 | |
| 852 | CheckJniAbortCatcher jni_abort_catcher; |
| 853 | for (size_t i = 0; i < strlen(primitive_descriptors); ++i) { |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 854 | env_->NewObjectArray(0, nullptr, nullptr); |
| 855 | jni_abort_catcher.Check("element_jclass == null"); |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 856 | jclass primitive_class = GetPrimitiveClass(primitive_descriptors[i]); |
| 857 | env_->NewObjectArray(1, primitive_class, nullptr); |
| 858 | std::string error_msg(StringPrintf("not an object type: %s", primitive_names[i])); |
| 859 | jni_abort_catcher.Check(error_msg.c_str()); |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | TEST_F(JniInternalTest, NewObjectArrayWithInitialValue) { |
| 864 | jclass element_class = env_->FindClass("java/lang/String"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 865 | ASSERT_NE(element_class, nullptr); |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 866 | jclass array_class = env_->FindClass("[Ljava/lang/String;"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 867 | ASSERT_NE(array_class, nullptr); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 868 | |
| 869 | jstring s = env_->NewStringUTF("poop"); |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 870 | jobjectArray a = env_->NewObjectArray(2, element_class, s); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 871 | EXPECT_NE(a, nullptr); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 872 | EXPECT_TRUE(env_->IsInstanceOf(a, array_class)); |
| 873 | EXPECT_EQ(2, env_->GetArrayLength(a)); |
| 874 | EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(a, 0), s)); |
| 875 | EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(a, 1), s)); |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 876 | |
| 877 | // Attempt to incorrect create an array of strings with initial value of string arrays. |
| 878 | CheckJniAbortCatcher jni_abort_catcher; |
| 879 | env_->NewObjectArray(2, element_class, a); |
| 880 | jni_abort_catcher.Check("cannot assign object of type 'java.lang.String[]' to array with element " |
| 881 | "type of 'java.lang.String'"); |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | TEST_F(JniInternalTest, GetArrayLength) { |
| 885 | // Already tested in NewObjectArray/NewPrimitiveArray. |
Elliott Hughes | 8a26c5c | 2011-08-15 18:35:43 -0700 | [diff] [blame] | 886 | } |
| 887 | |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 888 | TEST_F(JniInternalTest, GetObjectClass) { |
| 889 | jclass string_class = env_->FindClass("java/lang/String"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 890 | ASSERT_NE(string_class, nullptr); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 891 | jclass class_class = env_->FindClass("java/lang/Class"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 892 | ASSERT_NE(class_class, nullptr); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 893 | |
| 894 | jstring s = env_->NewStringUTF("poop"); |
| 895 | jclass c = env_->GetObjectClass(s); |
| 896 | ASSERT_TRUE(env_->IsSameObject(string_class, c)); |
| 897 | |
| 898 | jclass c2 = env_->GetObjectClass(c); |
| 899 | ASSERT_TRUE(env_->IsSameObject(class_class, env_->GetObjectClass(c2))); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 900 | |
| 901 | // Null as object should fail. |
| 902 | CheckJniAbortCatcher jni_abort_catcher; |
| 903 | EXPECT_EQ(env_->GetObjectClass(nullptr), nullptr); |
| 904 | jni_abort_catcher.Check("java_object == null"); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 905 | } |
| 906 | |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 907 | TEST_F(JniInternalTest, GetSuperclass) { |
| 908 | jclass object_class = env_->FindClass("java/lang/Object"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 909 | ASSERT_NE(object_class, nullptr); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 910 | jclass string_class = env_->FindClass("java/lang/String"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 911 | ASSERT_NE(string_class, nullptr); |
Ian Rogers | dc18020 | 2012-01-29 14:47:29 -0800 | [diff] [blame] | 912 | jclass runnable_interface = env_->FindClass("java/lang/Runnable"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 913 | ASSERT_NE(runnable_interface, nullptr); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 914 | ASSERT_TRUE(env_->IsSameObject(object_class, env_->GetSuperclass(string_class))); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 915 | ASSERT_EQ(env_->GetSuperclass(object_class), nullptr); |
Ian Rogers | dc18020 | 2012-01-29 14:47:29 -0800 | [diff] [blame] | 916 | ASSERT_TRUE(env_->IsSameObject(object_class, env_->GetSuperclass(runnable_interface))); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 917 | |
| 918 | // Null as class should fail. |
| 919 | CheckJniAbortCatcher jni_abort_catcher; |
| 920 | EXPECT_EQ(env_->GetSuperclass(nullptr), nullptr); |
| 921 | jni_abort_catcher.Check("java_class == null"); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 922 | } |
| 923 | |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 924 | TEST_F(JniInternalTest, IsAssignableFrom) { |
| 925 | jclass object_class = env_->FindClass("java/lang/Object"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 926 | ASSERT_NE(object_class, nullptr); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 927 | jclass string_class = env_->FindClass("java/lang/String"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 928 | ASSERT_NE(string_class, nullptr); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 929 | |
| 930 | ASSERT_TRUE(env_->IsAssignableFrom(object_class, string_class)); |
| 931 | ASSERT_FALSE(env_->IsAssignableFrom(string_class, object_class)); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 932 | |
| 933 | // Null as either class should fail. |
| 934 | CheckJniAbortCatcher jni_abort_catcher; |
| 935 | EXPECT_EQ(env_->IsAssignableFrom(nullptr, string_class), JNI_FALSE); |
| 936 | jni_abort_catcher.Check("java_class1 == null"); |
| 937 | EXPECT_EQ(env_->IsAssignableFrom(object_class, nullptr), JNI_FALSE); |
| 938 | jni_abort_catcher.Check("java_class2 == null"); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 939 | } |
| 940 | |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 941 | TEST_F(JniInternalTest, GetObjectRefType) { |
| 942 | jclass local = env_->FindClass("java/lang/Object"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 943 | ASSERT_TRUE(local != nullptr); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 944 | EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(local)); |
| 945 | |
| 946 | jobject global = env_->NewGlobalRef(local); |
| 947 | EXPECT_EQ(JNIGlobalRefType, env_->GetObjectRefType(global)); |
| 948 | |
| 949 | jweak weak_global = env_->NewWeakGlobalRef(local); |
| 950 | EXPECT_EQ(JNIWeakGlobalRefType, env_->GetObjectRefType(weak_global)); |
| 951 | |
| 952 | jobject invalid = reinterpret_cast<jobject>(this); |
| 953 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(invalid)); |
| 954 | |
| 955 | // 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] | 956 | |
| 957 | // Null as object should fail. |
| 958 | CheckJniAbortCatcher jni_abort_catcher; |
| 959 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(nullptr)); |
| 960 | jni_abort_catcher.Check("java_object == null"); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 961 | } |
| 962 | |
Mathieu Chartier | 0859999 | 2013-12-20 17:17:55 -0800 | [diff] [blame] | 963 | TEST_F(JniInternalTest, StaleWeakGlobal) { |
| 964 | jclass java_lang_Class = env_->FindClass("java/lang/Class"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 965 | ASSERT_NE(java_lang_Class, nullptr); |
| 966 | jobjectArray local_ref = env_->NewObjectArray(1, java_lang_Class, nullptr); |
| 967 | ASSERT_NE(local_ref, nullptr); |
Mathieu Chartier | 0859999 | 2013-12-20 17:17:55 -0800 | [diff] [blame] | 968 | jweak weak_global = env_->NewWeakGlobalRef(local_ref); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 969 | ASSERT_NE(weak_global, nullptr); |
Mathieu Chartier | 0859999 | 2013-12-20 17:17:55 -0800 | [diff] [blame] | 970 | env_->DeleteLocalRef(local_ref); |
| 971 | Runtime::Current()->GetHeap()->CollectGarbage(false); // GC should clear the weak global. |
| 972 | jobject new_global_ref = env_->NewGlobalRef(weak_global); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 973 | EXPECT_EQ(new_global_ref, nullptr); |
Mathieu Chartier | 0859999 | 2013-12-20 17:17:55 -0800 | [diff] [blame] | 974 | jobject new_local_ref = env_->NewLocalRef(weak_global); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 975 | EXPECT_EQ(new_local_ref, nullptr); |
Mathieu Chartier | 0859999 | 2013-12-20 17:17:55 -0800 | [diff] [blame] | 976 | } |
| 977 | |
Elliott Hughes | 8a26c5c | 2011-08-15 18:35:43 -0700 | [diff] [blame] | 978 | TEST_F(JniInternalTest, NewStringUTF) { |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 979 | EXPECT_EQ(env_->NewStringUTF(nullptr), nullptr); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 980 | jstring s; |
| 981 | |
| 982 | s = env_->NewStringUTF(""); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 983 | EXPECT_NE(s, nullptr); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 984 | EXPECT_EQ(0, env_->GetStringLength(s)); |
| 985 | EXPECT_EQ(0, env_->GetStringUTFLength(s)); |
| 986 | s = env_->NewStringUTF("hello"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 987 | EXPECT_NE(s, nullptr); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 988 | EXPECT_EQ(5, env_->GetStringLength(s)); |
| 989 | EXPECT_EQ(5, env_->GetStringUTFLength(s)); |
| 990 | |
Elliott Hughes | 8a26c5c | 2011-08-15 18:35:43 -0700 | [diff] [blame] | 991 | // TODO: check some non-ASCII strings. |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 992 | } |
| 993 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 994 | TEST_F(JniInternalTest, NewString) { |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 995 | jchar chars[] = { 'h', 'i' }; |
| 996 | jstring s; |
| 997 | s = env_->NewString(chars, 0); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 998 | EXPECT_NE(s, nullptr); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 999 | EXPECT_EQ(0, env_->GetStringLength(s)); |
| 1000 | EXPECT_EQ(0, env_->GetStringUTFLength(s)); |
| 1001 | s = env_->NewString(chars, 2); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1002 | EXPECT_NE(s, nullptr); |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1003 | EXPECT_EQ(2, env_->GetStringLength(s)); |
| 1004 | EXPECT_EQ(2, env_->GetStringUTFLength(s)); |
| 1005 | |
| 1006 | // TODO: check some non-ASCII strings. |
| 1007 | } |
| 1008 | |
Jesse Wilson | 25e79a5 | 2011-11-18 15:31:58 -0500 | [diff] [blame] | 1009 | TEST_F(JniInternalTest, NewStringNullCharsZeroLength) { |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 1010 | jstring s = env_->NewString(nullptr, 0); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1011 | EXPECT_NE(s, nullptr); |
Jesse Wilson | 25e79a5 | 2011-11-18 15:31:58 -0500 | [diff] [blame] | 1012 | EXPECT_EQ(0, env_->GetStringLength(s)); |
| 1013 | } |
| 1014 | |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 1015 | TEST_F(JniInternalTest, NewStringNullCharsNonzeroLength) { |
| 1016 | CheckJniAbortCatcher jni_abort_catcher; |
| 1017 | env_->NewString(nullptr, 1); |
| 1018 | jni_abort_catcher.Check("chars == null && char_count > 0"); |
| 1019 | } |
| 1020 | |
| 1021 | TEST_F(JniInternalTest, NewStringNegativeLength) { |
| 1022 | CheckJniAbortCatcher jni_abort_catcher; |
| 1023 | env_->NewString(nullptr, -1); |
| 1024 | jni_abort_catcher.Check("char_count < 0: -1"); |
| 1025 | env_->NewString(nullptr, std::numeric_limits<jint>::min()); |
| 1026 | jni_abort_catcher.Check("char_count < 0: -2147483648"); |
Jesse Wilson | 25e79a5 | 2011-11-18 15:31:58 -0500 | [diff] [blame] | 1027 | } |
| 1028 | |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1029 | TEST_F(JniInternalTest, GetStringLength_GetStringUTFLength) { |
| 1030 | // Already tested in the NewString/NewStringUTF tests. |
| 1031 | } |
| 1032 | |
| 1033 | TEST_F(JniInternalTest, GetStringRegion_GetStringUTFRegion) { |
| 1034 | jstring s = env_->NewStringUTF("hello"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1035 | ASSERT_TRUE(s != nullptr); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1036 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1037 | env_->GetStringRegion(s, -1, 0, nullptr); |
| 1038 | ExpectException(sioobe_); |
| 1039 | env_->GetStringRegion(s, 0, -1, nullptr); |
| 1040 | ExpectException(sioobe_); |
| 1041 | env_->GetStringRegion(s, 0, 10, nullptr); |
| 1042 | ExpectException(sioobe_); |
| 1043 | env_->GetStringRegion(s, 10, 1, nullptr); |
| 1044 | ExpectException(sioobe_); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1045 | |
| 1046 | jchar chars[4] = { 'x', 'x', 'x', 'x' }; |
| 1047 | env_->GetStringRegion(s, 1, 2, &chars[1]); |
| 1048 | EXPECT_EQ('x', chars[0]); |
| 1049 | EXPECT_EQ('e', chars[1]); |
| 1050 | EXPECT_EQ('l', chars[2]); |
| 1051 | EXPECT_EQ('x', chars[3]); |
| 1052 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1053 | // It's okay for the buffer to be nullptr as long as the length is 0. |
| 1054 | env_->GetStringRegion(s, 2, 0, nullptr); |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 1055 | // Even if the offset is invalid... |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1056 | env_->GetStringRegion(s, 123, 0, nullptr); |
| 1057 | ExpectException(sioobe_); |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 1058 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1059 | env_->GetStringUTFRegion(s, -1, 0, nullptr); |
| 1060 | ExpectException(sioobe_); |
| 1061 | env_->GetStringUTFRegion(s, 0, -1, nullptr); |
| 1062 | ExpectException(sioobe_); |
| 1063 | env_->GetStringUTFRegion(s, 0, 10, nullptr); |
| 1064 | ExpectException(sioobe_); |
| 1065 | env_->GetStringUTFRegion(s, 10, 1, nullptr); |
| 1066 | ExpectException(sioobe_); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1067 | |
| 1068 | char bytes[4] = { 'x', 'x', 'x', 'x' }; |
| 1069 | env_->GetStringUTFRegion(s, 1, 2, &bytes[1]); |
| 1070 | EXPECT_EQ('x', bytes[0]); |
| 1071 | EXPECT_EQ('e', bytes[1]); |
| 1072 | EXPECT_EQ('l', bytes[2]); |
| 1073 | EXPECT_EQ('x', bytes[3]); |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 1074 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1075 | // It's okay for the buffer to be nullptr as long as the length is 0. |
| 1076 | env_->GetStringUTFRegion(s, 2, 0, nullptr); |
Elliott Hughes | d7d7f6e | 2013-09-18 12:00:45 -0700 | [diff] [blame] | 1077 | // Even if the offset is invalid... |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1078 | env_->GetStringUTFRegion(s, 123, 0, nullptr); |
| 1079 | ExpectException(sioobe_); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1080 | } |
| 1081 | |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1082 | TEST_F(JniInternalTest, GetStringUTFChars_ReleaseStringUTFChars) { |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1083 | // Passing in a nullptr jstring is ignored normally, but caught by -Xcheck:jni. |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1084 | { |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1085 | CheckJniAbortCatcher check_jni_abort_catcher; |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1086 | EXPECT_EQ(env_->GetStringUTFChars(nullptr, nullptr), nullptr); |
Elliott Hughes | 56ef042 | 2012-06-19 14:35:04 -0700 | [diff] [blame] | 1087 | check_jni_abort_catcher.Check("GetStringUTFChars received null jstring"); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1088 | } |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1089 | |
| 1090 | jstring s = env_->NewStringUTF("hello"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1091 | ASSERT_TRUE(s != nullptr); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1092 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1093 | const char* utf = env_->GetStringUTFChars(s, nullptr); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1094 | EXPECT_STREQ("hello", utf); |
| 1095 | env_->ReleaseStringUTFChars(s, utf); |
| 1096 | |
| 1097 | jboolean is_copy = JNI_FALSE; |
| 1098 | utf = env_->GetStringUTFChars(s, &is_copy); |
| 1099 | EXPECT_EQ(JNI_TRUE, is_copy); |
| 1100 | EXPECT_STREQ("hello", utf); |
| 1101 | env_->ReleaseStringUTFChars(s, utf); |
| 1102 | } |
| 1103 | |
| 1104 | TEST_F(JniInternalTest, GetStringChars_ReleaseStringChars) { |
| 1105 | jstring s = env_->NewStringUTF("hello"); |
Fred Shih | 56890e2 | 2014-06-02 11:11:52 -0700 | [diff] [blame] | 1106 | ScopedObjectAccess soa(env_); |
| 1107 | mirror::String* s_m = soa.Decode<mirror::String*>(s); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1108 | ASSERT_TRUE(s != nullptr); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1109 | |
| 1110 | jchar expected[] = { 'h', 'e', 'l', 'l', 'o' }; |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1111 | const jchar* chars = env_->GetStringChars(s, nullptr); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1112 | EXPECT_EQ(expected[0], chars[0]); |
| 1113 | EXPECT_EQ(expected[1], chars[1]); |
| 1114 | EXPECT_EQ(expected[2], chars[2]); |
| 1115 | EXPECT_EQ(expected[3], chars[3]); |
| 1116 | EXPECT_EQ(expected[4], chars[4]); |
| 1117 | env_->ReleaseStringChars(s, chars); |
| 1118 | |
| 1119 | jboolean is_copy = JNI_FALSE; |
| 1120 | chars = env_->GetStringChars(s, &is_copy); |
Fred Shih | 56890e2 | 2014-06-02 11:11:52 -0700 | [diff] [blame] | 1121 | if (Runtime::Current()->GetHeap()->IsMovableObject(s_m->GetCharArray())) { |
| 1122 | EXPECT_EQ(JNI_TRUE, is_copy); |
| 1123 | } else { |
| 1124 | EXPECT_EQ(JNI_FALSE, is_copy); |
| 1125 | } |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1126 | EXPECT_EQ(expected[0], chars[0]); |
| 1127 | EXPECT_EQ(expected[1], chars[1]); |
| 1128 | EXPECT_EQ(expected[2], chars[2]); |
| 1129 | EXPECT_EQ(expected[3], chars[3]); |
| 1130 | EXPECT_EQ(expected[4], chars[4]); |
| 1131 | env_->ReleaseStringChars(s, chars); |
| 1132 | } |
| 1133 | |
| 1134 | TEST_F(JniInternalTest, GetStringCritical_ReleaseStringCritical) { |
| 1135 | jstring s = env_->NewStringUTF("hello"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1136 | ASSERT_TRUE(s != nullptr); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1137 | |
| 1138 | jchar expected[] = { 'h', 'e', 'l', 'l', 'o' }; |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1139 | const jchar* chars = env_->GetStringCritical(s, nullptr); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1140 | EXPECT_EQ(expected[0], chars[0]); |
| 1141 | EXPECT_EQ(expected[1], chars[1]); |
| 1142 | EXPECT_EQ(expected[2], chars[2]); |
| 1143 | EXPECT_EQ(expected[3], chars[3]); |
| 1144 | EXPECT_EQ(expected[4], chars[4]); |
| 1145 | env_->ReleaseStringCritical(s, chars); |
| 1146 | |
Fred Shih | 56890e2 | 2014-06-02 11:11:52 -0700 | [diff] [blame] | 1147 | jboolean is_copy = JNI_TRUE; |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1148 | chars = env_->GetStringCritical(s, &is_copy); |
Fred Shih | 56890e2 | 2014-06-02 11:11:52 -0700 | [diff] [blame] | 1149 | EXPECT_EQ(JNI_FALSE, is_copy); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 1150 | EXPECT_EQ(expected[0], chars[0]); |
| 1151 | EXPECT_EQ(expected[1], chars[1]); |
| 1152 | EXPECT_EQ(expected[2], chars[2]); |
| 1153 | EXPECT_EQ(expected[3], chars[3]); |
| 1154 | EXPECT_EQ(expected[4], chars[4]); |
| 1155 | env_->ReleaseStringCritical(s, chars); |
| 1156 | } |
| 1157 | |
Elliott Hughes | 814e403 | 2011-08-23 12:07:56 -0700 | [diff] [blame] | 1158 | TEST_F(JniInternalTest, GetObjectArrayElement_SetObjectArrayElement) { |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1159 | jclass java_lang_Class = env_->FindClass("java/lang/Class"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1160 | ASSERT_TRUE(java_lang_Class != nullptr); |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 1161 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1162 | jobjectArray array = env_->NewObjectArray(1, java_lang_Class, nullptr); |
| 1163 | EXPECT_NE(array, nullptr); |
| 1164 | EXPECT_EQ(env_->GetObjectArrayElement(array, 0), nullptr); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1165 | env_->SetObjectArrayElement(array, 0, java_lang_Class); |
| 1166 | EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(array, 0), java_lang_Class)); |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 1167 | |
| 1168 | // ArrayIndexOutOfBounds for negative index. |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1169 | env_->SetObjectArrayElement(array, -1, java_lang_Class); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1170 | ExpectException(aioobe_); |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 1171 | |
| 1172 | // ArrayIndexOutOfBounds for too-large index. |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1173 | env_->SetObjectArrayElement(array, 1, java_lang_Class); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1174 | ExpectException(aioobe_); |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 1175 | |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1176 | // ArrayStoreException thrown for bad types. |
| 1177 | env_->SetObjectArrayElement(array, 0, env_->NewStringUTF("not a jclass!")); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1178 | ExpectException(ase_); |
| 1179 | |
| 1180 | // Null as array should fail. |
| 1181 | CheckJniAbortCatcher jni_abort_catcher; |
| 1182 | EXPECT_EQ(nullptr, env_->GetObjectArrayElement(nullptr, 0)); |
| 1183 | jni_abort_catcher.Check("java_array == null"); |
| 1184 | env_->SetObjectArrayElement(nullptr, 0, nullptr); |
| 1185 | jni_abort_catcher.Check("java_array == null"); |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 1186 | } |
| 1187 | |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1188 | #define EXPECT_STATIC_PRIMITIVE_FIELD(type, field_name, sig, value1, value2) \ |
| 1189 | do { \ |
| 1190 | jfieldID fid = env_->GetStaticFieldID(c, field_name, sig); \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1191 | EXPECT_NE(fid, nullptr); \ |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1192 | env_->SetStatic ## type ## Field(c, fid, value1); \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1193 | EXPECT_EQ(value1, env_->GetStatic ## type ## Field(c, fid)); \ |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1194 | env_->SetStatic ## type ## Field(c, fid, value2); \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1195 | EXPECT_EQ(value2, env_->GetStatic ## type ## Field(c, fid)); \ |
| 1196 | \ |
| 1197 | CheckJniAbortCatcher jni_abort_catcher; \ |
| 1198 | env_->GetStatic ## type ## Field(nullptr, fid); \ |
| 1199 | jni_abort_catcher.Check("received null jclass"); \ |
| 1200 | env_->SetStatic ## type ## Field(nullptr, fid, value1); \ |
| 1201 | jni_abort_catcher.Check("received null jclass"); \ |
| 1202 | env_->GetStatic ## type ## Field(c, nullptr); \ |
| 1203 | jni_abort_catcher.Check("fid == null"); \ |
| 1204 | env_->SetStatic ## type ## Field(c, nullptr, value1); \ |
| 1205 | jni_abort_catcher.Check("fid == null"); \ |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1206 | } while (false) |
| 1207 | |
| 1208 | #define EXPECT_PRIMITIVE_FIELD(instance, type, field_name, sig, value1, value2) \ |
| 1209 | do { \ |
| 1210 | jfieldID fid = env_->GetFieldID(c, field_name, sig); \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1211 | EXPECT_NE(fid, nullptr); \ |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1212 | env_->Set ## type ## Field(instance, fid, value1); \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1213 | EXPECT_EQ(value1, env_->Get ## type ## Field(instance, fid)); \ |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1214 | env_->Set ## type ## Field(instance, fid, value2); \ |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1215 | EXPECT_EQ(value2, env_->Get ## type ## Field(instance, fid)); \ |
| 1216 | \ |
| 1217 | CheckJniAbortCatcher jni_abort_catcher; \ |
| 1218 | env_->Get ## type ## Field(nullptr, fid); \ |
| 1219 | jni_abort_catcher.Check("obj == null"); \ |
| 1220 | env_->Set ## type ## Field(nullptr, fid, value1); \ |
| 1221 | jni_abort_catcher.Check("obj == null"); \ |
| 1222 | env_->Get ## type ## Field(instance, nullptr); \ |
| 1223 | jni_abort_catcher.Check("fid == null"); \ |
| 1224 | env_->Set ## type ## Field(instance, nullptr, value1); \ |
| 1225 | jni_abort_catcher.Check("fid == null"); \ |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1226 | } while (false) |
| 1227 | |
| 1228 | |
| 1229 | TEST_F(JniInternalTest, GetPrimitiveField_SetPrimitiveField) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1230 | TEST_DISABLED_FOR_PORTABLE(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1231 | Thread::Current()->TransitionFromSuspendedToRunnable(); |
| 1232 | LoadDex("AllFields"); |
Brian Carlstrom | bd86bcc | 2013-03-10 20:26:16 -0700 | [diff] [blame] | 1233 | bool started = runtime_->Start(); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1234 | ASSERT_TRUE(started); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1235 | |
| 1236 | jclass c = env_->FindClass("AllFields"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1237 | ASSERT_NE(c, nullptr); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1238 | jobject o = env_->AllocObject(c); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1239 | ASSERT_NE(o, nullptr); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1240 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1241 | EXPECT_STATIC_PRIMITIVE_FIELD(Boolean, "sZ", "Z", JNI_TRUE, JNI_FALSE); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1242 | EXPECT_STATIC_PRIMITIVE_FIELD(Byte, "sB", "B", 1, 2); |
| 1243 | EXPECT_STATIC_PRIMITIVE_FIELD(Char, "sC", "C", 'a', 'b'); |
| 1244 | EXPECT_STATIC_PRIMITIVE_FIELD(Double, "sD", "D", 1.0, 2.0); |
| 1245 | EXPECT_STATIC_PRIMITIVE_FIELD(Float, "sF", "F", 1.0, 2.0); |
| 1246 | EXPECT_STATIC_PRIMITIVE_FIELD(Int, "sI", "I", 1, 2); |
| 1247 | EXPECT_STATIC_PRIMITIVE_FIELD(Long, "sJ", "J", 1, 2); |
| 1248 | EXPECT_STATIC_PRIMITIVE_FIELD(Short, "sS", "S", 1, 2); |
| 1249 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1250 | EXPECT_PRIMITIVE_FIELD(o, Boolean, "iZ", "Z", JNI_TRUE, JNI_FALSE); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1251 | EXPECT_PRIMITIVE_FIELD(o, Byte, "iB", "B", 1, 2); |
| 1252 | EXPECT_PRIMITIVE_FIELD(o, Char, "iC", "C", 'a', 'b'); |
| 1253 | EXPECT_PRIMITIVE_FIELD(o, Double, "iD", "D", 1.0, 2.0); |
| 1254 | EXPECT_PRIMITIVE_FIELD(o, Float, "iF", "F", 1.0, 2.0); |
| 1255 | EXPECT_PRIMITIVE_FIELD(o, Int, "iI", "I", 1, 2); |
| 1256 | EXPECT_PRIMITIVE_FIELD(o, Long, "iJ", "J", 1, 2); |
| 1257 | EXPECT_PRIMITIVE_FIELD(o, Short, "iS", "S", 1, 2); |
| 1258 | } |
| 1259 | |
| 1260 | TEST_F(JniInternalTest, GetObjectField_SetObjectField) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1261 | TEST_DISABLED_FOR_PORTABLE(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1262 | Thread::Current()->TransitionFromSuspendedToRunnable(); |
| 1263 | LoadDex("AllFields"); |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 1264 | runtime_->Start(); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1265 | |
| 1266 | jclass c = env_->FindClass("AllFields"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1267 | ASSERT_NE(c, nullptr); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1268 | jobject o = env_->AllocObject(c); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1269 | ASSERT_NE(o, nullptr); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1270 | |
| 1271 | jstring s1 = env_->NewStringUTF("hello"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1272 | ASSERT_NE(s1, nullptr); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1273 | jstring s2 = env_->NewStringUTF("world"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1274 | ASSERT_NE(s2, nullptr); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1275 | |
| 1276 | jfieldID s_fid = env_->GetStaticFieldID(c, "sObject", "Ljava/lang/Object;"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1277 | ASSERT_NE(s_fid, nullptr); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1278 | jfieldID i_fid = env_->GetFieldID(c, "iObject", "Ljava/lang/Object;"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1279 | ASSERT_NE(i_fid, nullptr); |
Elliott Hughes | 885c3bd | 2011-08-22 16:59:20 -0700 | [diff] [blame] | 1280 | |
| 1281 | env_->SetStaticObjectField(c, s_fid, s1); |
| 1282 | ASSERT_TRUE(env_->IsSameObject(s1, env_->GetStaticObjectField(c, s_fid))); |
| 1283 | env_->SetStaticObjectField(c, s_fid, s2); |
| 1284 | ASSERT_TRUE(env_->IsSameObject(s2, env_->GetStaticObjectField(c, s_fid))); |
| 1285 | |
| 1286 | env_->SetObjectField(o, i_fid, s1); |
| 1287 | ASSERT_TRUE(env_->IsSameObject(s1, env_->GetObjectField(o, i_fid))); |
| 1288 | env_->SetObjectField(o, i_fid, s2); |
| 1289 | ASSERT_TRUE(env_->IsSameObject(s2, env_->GetObjectField(o, i_fid))); |
| 1290 | } |
| 1291 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1292 | TEST_F(JniInternalTest, NewLocalRef_nullptr) { |
| 1293 | EXPECT_EQ(env_->NewLocalRef(nullptr), nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1294 | } |
| 1295 | |
| 1296 | TEST_F(JniInternalTest, NewLocalRef) { |
| 1297 | jstring s = env_->NewStringUTF(""); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1298 | ASSERT_NE(s, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1299 | jobject o = env_->NewLocalRef(s); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1300 | EXPECT_NE(o, nullptr); |
| 1301 | EXPECT_NE(o, s); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1302 | |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1303 | EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(o)); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1304 | } |
| 1305 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1306 | TEST_F(JniInternalTest, DeleteLocalRef_nullptr) { |
| 1307 | env_->DeleteLocalRef(nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1308 | } |
| 1309 | |
| 1310 | TEST_F(JniInternalTest, DeleteLocalRef) { |
| 1311 | jstring s = env_->NewStringUTF(""); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1312 | ASSERT_NE(s, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1313 | env_->DeleteLocalRef(s); |
| 1314 | |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 1315 | // Currently, deleting an already-deleted reference is just a CheckJNI warning. |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1316 | { |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1317 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 1318 | env_->DeleteLocalRef(s); |
Elliott Hughes | a9137c6 | 2013-01-09 10:55:21 -0800 | [diff] [blame] | 1319 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1320 | std::string expected(StringPrintf("native code passing in reference to " |
| 1321 | "invalid local reference: %p", s)); |
Elliott Hughes | a9137c6 | 2013-01-09 10:55:21 -0800 | [diff] [blame] | 1322 | check_jni_abort_catcher.Check(expected.c_str()); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1323 | } |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1324 | |
| 1325 | s = env_->NewStringUTF(""); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1326 | ASSERT_NE(s, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1327 | jobject o = env_->NewLocalRef(s); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1328 | ASSERT_NE(o, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1329 | |
| 1330 | env_->DeleteLocalRef(s); |
| 1331 | env_->DeleteLocalRef(o); |
| 1332 | } |
| 1333 | |
Elliott Hughes | aa836f7 | 2013-08-20 16:57:23 -0700 | [diff] [blame] | 1334 | TEST_F(JniInternalTest, PushLocalFrame_10395422) { |
| 1335 | // The JNI specification is ambiguous about whether the given capacity is to be interpreted as a |
| 1336 | // maximum or as a minimum, but it seems like it's supposed to be a minimum, and that's how |
| 1337 | // Android historically treated it, and it's how the RI treats it. It's also the more useful |
| 1338 | // interpretation! |
| 1339 | ASSERT_EQ(JNI_OK, env_->PushLocalFrame(0)); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1340 | env_->PopLocalFrame(nullptr); |
Elliott Hughes | aa836f7 | 2013-08-20 16:57:23 -0700 | [diff] [blame] | 1341 | |
| 1342 | // Negative capacities are not allowed. |
| 1343 | ASSERT_EQ(JNI_ERR, env_->PushLocalFrame(-1)); |
| 1344 | |
| 1345 | // And it's okay to have an upper limit. Ours is currently 512. |
| 1346 | ASSERT_EQ(JNI_ERR, env_->PushLocalFrame(8192)); |
| 1347 | } |
| 1348 | |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1349 | TEST_F(JniInternalTest, PushLocalFrame_PopLocalFrame) { |
| 1350 | jobject original = env_->NewStringUTF(""); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1351 | ASSERT_NE(original, nullptr); |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1352 | |
| 1353 | jobject outer; |
| 1354 | jobject inner1, inner2; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1355 | ScopedObjectAccess soa(env_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1356 | mirror::Object* inner2_direct_pointer; |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1357 | { |
Elliott Hughes | aa836f7 | 2013-08-20 16:57:23 -0700 | [diff] [blame] | 1358 | ASSERT_EQ(JNI_OK, env_->PushLocalFrame(4)); |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1359 | outer = env_->NewLocalRef(original); |
| 1360 | |
| 1361 | { |
Elliott Hughes | aa836f7 | 2013-08-20 16:57:23 -0700 | [diff] [blame] | 1362 | ASSERT_EQ(JNI_OK, env_->PushLocalFrame(4)); |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1363 | inner1 = env_->NewLocalRef(outer); |
| 1364 | inner2 = env_->NewStringUTF("survivor"); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1365 | inner2_direct_pointer = soa.Decode<mirror::Object*>(inner2); |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1366 | env_->PopLocalFrame(inner2); |
| 1367 | } |
| 1368 | |
| 1369 | EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(original)); |
| 1370 | EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(outer)); |
| 1371 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(inner1)); |
| 1372 | |
| 1373 | // Our local reference for the survivor is invalid because the survivor |
| 1374 | // gets a new local reference... |
| 1375 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(inner2)); |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1376 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1377 | env_->PopLocalFrame(nullptr); |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1378 | } |
| 1379 | EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(original)); |
| 1380 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(outer)); |
| 1381 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(inner1)); |
| 1382 | EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(inner2)); |
| 1383 | } |
| 1384 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1385 | TEST_F(JniInternalTest, NewGlobalRef_nullptr) { |
| 1386 | EXPECT_EQ(env_->NewGlobalRef(nullptr), nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1387 | } |
| 1388 | |
| 1389 | TEST_F(JniInternalTest, NewGlobalRef) { |
| 1390 | jstring s = env_->NewStringUTF(""); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1391 | ASSERT_NE(s, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1392 | jobject o = env_->NewGlobalRef(s); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1393 | EXPECT_NE(o, nullptr); |
| 1394 | EXPECT_NE(o, s); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1395 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1396 | EXPECT_EQ(env_->GetObjectRefType(o), JNIGlobalRefType); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1397 | } |
| 1398 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1399 | TEST_F(JniInternalTest, DeleteGlobalRef_nullptr) { |
| 1400 | env_->DeleteGlobalRef(nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1401 | } |
| 1402 | |
| 1403 | TEST_F(JniInternalTest, DeleteGlobalRef) { |
| 1404 | jstring s = env_->NewStringUTF(""); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1405 | ASSERT_NE(s, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1406 | |
| 1407 | jobject o = env_->NewGlobalRef(s); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1408 | ASSERT_NE(o, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1409 | env_->DeleteGlobalRef(o); |
| 1410 | |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 1411 | // Currently, deleting an already-deleted reference is just a CheckJNI warning. |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1412 | { |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1413 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 1414 | env_->DeleteGlobalRef(o); |
Elliott Hughes | a9137c6 | 2013-01-09 10:55:21 -0800 | [diff] [blame] | 1415 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1416 | std::string expected(StringPrintf("native code passing in reference to " |
| 1417 | "invalid global reference: %p", o)); |
Elliott Hughes | a9137c6 | 2013-01-09 10:55:21 -0800 | [diff] [blame] | 1418 | check_jni_abort_catcher.Check(expected.c_str()); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1419 | } |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1420 | |
| 1421 | jobject o1 = env_->NewGlobalRef(s); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1422 | ASSERT_NE(o1, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1423 | jobject o2 = env_->NewGlobalRef(s); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1424 | ASSERT_NE(o2, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1425 | |
| 1426 | env_->DeleteGlobalRef(o1); |
| 1427 | env_->DeleteGlobalRef(o2); |
| 1428 | } |
| 1429 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1430 | TEST_F(JniInternalTest, NewWeakGlobalRef_nullptr) { |
| 1431 | EXPECT_EQ(env_->NewWeakGlobalRef(nullptr), nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1432 | } |
| 1433 | |
| 1434 | TEST_F(JniInternalTest, NewWeakGlobalRef) { |
| 1435 | jstring s = env_->NewStringUTF(""); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1436 | ASSERT_NE(s, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1437 | jobject o = env_->NewWeakGlobalRef(s); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1438 | EXPECT_NE(o, nullptr); |
| 1439 | EXPECT_NE(o, s); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1440 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1441 | EXPECT_EQ(env_->GetObjectRefType(o), JNIWeakGlobalRefType); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1442 | } |
| 1443 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1444 | TEST_F(JniInternalTest, DeleteWeakGlobalRef_nullptr) { |
| 1445 | env_->DeleteWeakGlobalRef(nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1446 | } |
| 1447 | |
| 1448 | TEST_F(JniInternalTest, DeleteWeakGlobalRef) { |
| 1449 | jstring s = env_->NewStringUTF(""); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1450 | ASSERT_NE(s, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1451 | |
| 1452 | jobject o = env_->NewWeakGlobalRef(s); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1453 | ASSERT_NE(o, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1454 | env_->DeleteWeakGlobalRef(o); |
| 1455 | |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 1456 | // Currently, deleting an already-deleted reference is just a CheckJNI warning. |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1457 | { |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1458 | CheckJniAbortCatcher check_jni_abort_catcher; |
| 1459 | env_->DeleteWeakGlobalRef(o); |
Elliott Hughes | a9137c6 | 2013-01-09 10:55:21 -0800 | [diff] [blame] | 1460 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1461 | std::string expected(StringPrintf("native code passing in reference to " |
| 1462 | "invalid weak global reference: %p", o)); |
Elliott Hughes | a9137c6 | 2013-01-09 10:55:21 -0800 | [diff] [blame] | 1463 | check_jni_abort_catcher.Check(expected.c_str()); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1464 | } |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1465 | |
| 1466 | jobject o1 = env_->NewWeakGlobalRef(s); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1467 | ASSERT_NE(o1, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1468 | jobject o2 = env_->NewWeakGlobalRef(s); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1469 | ASSERT_NE(o2, nullptr); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 1470 | |
| 1471 | env_->DeleteWeakGlobalRef(o1); |
| 1472 | env_->DeleteWeakGlobalRef(o2); |
| 1473 | } |
| 1474 | |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1475 | TEST_F(JniInternalTest, Throw) { |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1476 | EXPECT_EQ(JNI_ERR, env_->Throw(nullptr)); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1477 | |
| 1478 | jclass exception_class = env_->FindClass("java/lang/RuntimeException"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1479 | ASSERT_TRUE(exception_class != nullptr); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1480 | jthrowable exception = reinterpret_cast<jthrowable>(env_->AllocObject(exception_class)); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1481 | ASSERT_TRUE(exception != nullptr); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1482 | |
| 1483 | EXPECT_EQ(JNI_OK, env_->Throw(exception)); |
| 1484 | EXPECT_TRUE(env_->ExceptionCheck()); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1485 | jthrowable thrown_exception = env_->ExceptionOccurred(); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1486 | env_->ExceptionClear(); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1487 | EXPECT_TRUE(env_->IsSameObject(exception, thrown_exception)); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1488 | } |
| 1489 | |
| 1490 | TEST_F(JniInternalTest, ThrowNew) { |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1491 | EXPECT_EQ(JNI_ERR, env_->Throw(nullptr)); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1492 | |
| 1493 | jclass exception_class = env_->FindClass("java/lang/RuntimeException"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1494 | ASSERT_TRUE(exception_class != nullptr); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1495 | |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 1496 | jthrowable thrown_exception; |
| 1497 | |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1498 | EXPECT_EQ(JNI_OK, env_->ThrowNew(exception_class, "hello world")); |
| 1499 | EXPECT_TRUE(env_->ExceptionCheck()); |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 1500 | thrown_exception = env_->ExceptionOccurred(); |
| 1501 | env_->ExceptionClear(); |
| 1502 | EXPECT_TRUE(env_->IsInstanceOf(thrown_exception, exception_class)); |
| 1503 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1504 | EXPECT_EQ(JNI_OK, env_->ThrowNew(exception_class, nullptr)); |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 1505 | EXPECT_TRUE(env_->ExceptionCheck()); |
| 1506 | thrown_exception = env_->ExceptionOccurred(); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1507 | env_->ExceptionClear(); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1508 | EXPECT_TRUE(env_->IsInstanceOf(thrown_exception, exception_class)); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1509 | } |
| 1510 | |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 1511 | TEST_F(JniInternalTest, NewDirectBuffer_GetDirectBufferAddress_GetDirectBufferCapacity) { |
| 1512 | // Start runtime. |
| 1513 | Thread* self = Thread::Current(); |
| 1514 | self->TransitionFromSuspendedToRunnable(); |
| 1515 | MakeExecutable(nullptr, "java.lang.Class"); |
| 1516 | MakeExecutable(nullptr, "java.lang.Object"); |
| 1517 | MakeExecutable(nullptr, "java.nio.DirectByteBuffer"); |
| 1518 | MakeExecutable(nullptr, "java.nio.MemoryBlock"); |
| 1519 | MakeExecutable(nullptr, "java.nio.MemoryBlock$UnmanagedBlock"); |
| 1520 | MakeExecutable(nullptr, "java.nio.MappedByteBuffer"); |
| 1521 | MakeExecutable(nullptr, "java.nio.ByteBuffer"); |
| 1522 | MakeExecutable(nullptr, "java.nio.Buffer"); |
| 1523 | // TODO: we only load a dex file here as starting the runtime relies upon it. |
| 1524 | const char* class_name = "StaticLeafMethods"; |
| 1525 | LoadDex(class_name); |
| 1526 | bool started = runtime_->Start(); |
| 1527 | ASSERT_TRUE(started); |
| 1528 | |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1529 | jclass buffer_class = env_->FindClass("java/nio/Buffer"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1530 | ASSERT_NE(buffer_class, nullptr); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1531 | |
| 1532 | char bytes[1024]; |
| 1533 | jobject buffer = env_->NewDirectByteBuffer(bytes, sizeof(bytes)); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1534 | ASSERT_NE(buffer, nullptr); |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1535 | ASSERT_TRUE(env_->IsInstanceOf(buffer, buffer_class)); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1536 | ASSERT_EQ(env_->GetDirectBufferAddress(buffer), bytes); |
Ian Rogers | 1ee9935 | 2014-05-14 14:38:16 -0700 | [diff] [blame] | 1537 | ASSERT_EQ(env_->GetDirectBufferCapacity(buffer), static_cast<jlong>(sizeof(bytes))); |
Brian Carlstrom | 45d26c8 | 2014-06-24 23:36:28 -0700 | [diff] [blame] | 1538 | |
| 1539 | { |
| 1540 | CheckJniAbortCatcher check_jni_abort_catcher; |
Brian Carlstrom | 85a9336 | 2014-06-25 09:30:52 -0700 | [diff] [blame] | 1541 | env_->NewDirectByteBuffer(bytes, static_cast<jlong>(INT_MAX) + 1); |
Brian Carlstrom | 45d26c8 | 2014-06-24 23:36:28 -0700 | [diff] [blame] | 1542 | check_jni_abort_catcher.Check("in call to NewDirectByteBuffer"); |
| 1543 | } |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 1544 | } |
| 1545 | |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 1546 | TEST_F(JniInternalTest, MonitorEnterExit) { |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1547 | // Create an object to torture. |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 1548 | jclass object_class = env_->FindClass("java/lang/Object"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1549 | ASSERT_NE(object_class, nullptr); |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 1550 | jobject object = env_->AllocObject(object_class); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1551 | ASSERT_NE(object, nullptr); |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 1552 | |
| 1553 | // Expected class of exceptions |
| 1554 | jclass imse_class = env_->FindClass("java/lang/IllegalMonitorStateException"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1555 | ASSERT_NE(imse_class, nullptr); |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 1556 | |
| 1557 | jthrowable thrown_exception; |
| 1558 | |
| 1559 | // Unlock of unowned monitor |
| 1560 | env_->MonitorExit(object); |
| 1561 | EXPECT_TRUE(env_->ExceptionCheck()); |
| 1562 | thrown_exception = env_->ExceptionOccurred(); |
| 1563 | env_->ExceptionClear(); |
| 1564 | EXPECT_TRUE(env_->IsInstanceOf(thrown_exception, imse_class)); |
| 1565 | |
| 1566 | // Lock of unowned monitor |
| 1567 | env_->MonitorEnter(object); |
| 1568 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 1569 | // Regular unlock |
| 1570 | env_->MonitorExit(object); |
| 1571 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 1572 | |
| 1573 | // Recursively lock a lot |
| 1574 | size_t max_recursive_lock = 1024; |
| 1575 | for (size_t i = 0; i < max_recursive_lock; i++) { |
| 1576 | env_->MonitorEnter(object); |
| 1577 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 1578 | } |
| 1579 | // Recursively unlock a lot |
| 1580 | for (size_t i = 0; i < max_recursive_lock; i++) { |
| 1581 | env_->MonitorExit(object); |
| 1582 | EXPECT_FALSE(env_->ExceptionCheck()); |
| 1583 | } |
| 1584 | |
| 1585 | // Unlock of unowned monitor |
| 1586 | env_->MonitorExit(object); |
| 1587 | EXPECT_TRUE(env_->ExceptionCheck()); |
| 1588 | thrown_exception = env_->ExceptionOccurred(); |
| 1589 | env_->ExceptionClear(); |
| 1590 | EXPECT_TRUE(env_->IsInstanceOf(thrown_exception, imse_class)); |
Elliott Hughes | a92853e | 2012-02-07 16:09:27 -0800 | [diff] [blame] | 1591 | |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1592 | // It's an error to call MonitorEnter or MonitorExit on nullptr. |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1593 | { |
| 1594 | CheckJniAbortCatcher check_jni_abort_catcher; |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1595 | env_->MonitorEnter(nullptr); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1596 | check_jni_abort_catcher.Check("in call to MonitorEnter"); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1597 | env_->MonitorExit(nullptr); |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 1598 | check_jni_abort_catcher.Check("in call to MonitorExit"); |
| 1599 | } |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 1600 | } |
| 1601 | |
Brian Carlstrom | 4d57143 | 2012-05-16 00:21:41 -0700 | [diff] [blame] | 1602 | TEST_F(JniInternalTest, DetachCurrentThread) { |
| 1603 | CleanUpJniEnv(); // cleanup now so TearDown won't have junk from wrong JNIEnv |
| 1604 | jint ok = vm_->DetachCurrentThread(); |
| 1605 | EXPECT_EQ(JNI_OK, ok); |
| 1606 | |
| 1607 | jint err = vm_->DetachCurrentThread(); |
| 1608 | EXPECT_EQ(JNI_ERR, err); |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 1609 | vm_->AttachCurrentThread(&env_, nullptr); // need attached thread for CommonRuntimeTest::TearDown |
Brian Carlstrom | 4d57143 | 2012-05-16 00:21:41 -0700 | [diff] [blame] | 1610 | } |
| 1611 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1612 | } // namespace art |