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