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