blob: f182e950bd34f61baa7383a9dbbd27f625b4b08c [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 Hughes0c9cd562011-08-12 10:59:29 -070016
Carl Shapiro9b9ba282011-08-14 15:30:39 -070017#include "jni_internal.h"
Elliott Hughes0c9cd562011-08-12 10:59:29 -070018
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080019#include "common_compiler_test.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070020#include "mirror/art_method-inl.h"
Fred Shih56890e22014-06-02 11:11:52 -070021#include "mirror/string-inl.h"
Elliott Hughes726079d2011-10-07 18:43:44 -070022#include "ScopedLocalRef.h"
Elliott Hughes0c9cd562011-08-12 10:59:29 -070023
24namespace art {
25
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080026// TODO: Convert to CommonRuntimeTest. Currently MakeExecutable is used.
27class JniInternalTest : public CommonCompilerTest {
Elliott Hughesc7ac37f2011-08-12 12:21:58 -070028 protected:
29 virtual void SetUp() {
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080030 CommonCompilerTest::SetUp();
Elliott Hughes5174fe62011-08-23 15:12:35 -070031
Elliott Hughesa2501992011-08-26 19:39:54 -070032 vm_ = Runtime::Current()->GetJavaVM();
33
Elliott Hughes5174fe62011-08-23 15:12:35 -070034 // Turn on -verbose:jni for the JNI tests.
Ian Rogers79713632013-08-21 19:06:15 -070035 // gLogVerbosity.jni = true;
Elliott Hughes5174fe62011-08-23 15:12:35 -070036
Ian Rogers2d10b202014-05-12 19:15:18 -070037 vm_->AttachCurrentThread(&env_, nullptr);
Elliott Hughesb465ab02011-08-24 11:21:21 -070038
Brian Carlstromea46f952013-07-30 01:26:50 -070039 ScopedLocalRef<jclass> aioobe(env_,
40 env_->FindClass("java/lang/ArrayIndexOutOfBoundsException"));
Ian Rogers2d10b202014-05-12 19:15:18 -070041 CHECK(aioobe.get() != nullptr);
Elliott Hughes726079d2011-10-07 18:43:44 -070042 aioobe_ = reinterpret_cast<jclass>(env_->NewGlobalRef(aioobe.get()));
Elliott Hughesb465ab02011-08-24 11:21:21 -070043
Elliott Hughesb264f082012-04-06 17:10:10 -070044 ScopedLocalRef<jclass> ase(env_, env_->FindClass("java/lang/ArrayStoreException"));
Ian Rogers2d10b202014-05-12 19:15:18 -070045 CHECK(ase.get() != nullptr);
Elliott Hughesb264f082012-04-06 17:10:10 -070046 ase_ = reinterpret_cast<jclass>(env_->NewGlobalRef(ase.get()));
47
Brian Carlstromea46f952013-07-30 01:26:50 -070048 ScopedLocalRef<jclass> sioobe(env_,
49 env_->FindClass("java/lang/StringIndexOutOfBoundsException"));
Ian Rogers2d10b202014-05-12 19:15:18 -070050 CHECK(sioobe.get() != nullptr);
Elliott Hughes726079d2011-10-07 18:43:44 -070051 sioobe_ = reinterpret_cast<jclass>(env_->NewGlobalRef(sioobe.get()));
52 }
53
Ian Rogers2d10b202014-05-12 19:15:18 -070054 void ExpectException(jclass exception_class) {
55 EXPECT_TRUE(env_->ExceptionCheck());
56 jthrowable exception = env_->ExceptionOccurred();
57 EXPECT_NE(nullptr, exception);
58 env_->ExceptionClear();
59 EXPECT_TRUE(env_->IsInstanceOf(exception, exception_class));
60 }
61
62 void ExpectClassFound(const char* name) {
63 EXPECT_NE(env_->FindClass(name), nullptr) << name;
64 EXPECT_FALSE(env_->ExceptionCheck()) << name;
65 }
66
67 void ExpectClassNotFound(const char* name) {
68 EXPECT_EQ(env_->FindClass(name), nullptr) << name;
69 EXPECT_TRUE(env_->ExceptionCheck()) << name;
70 env_->ExceptionClear();
71 }
72
Brian Carlstrom4d571432012-05-16 00:21:41 -070073 void CleanUpJniEnv() {
Ian Rogers2d10b202014-05-12 19:15:18 -070074 if (aioobe_ != nullptr) {
Brian Carlstrom4d571432012-05-16 00:21:41 -070075 env_->DeleteGlobalRef(aioobe_);
Ian Rogers2d10b202014-05-12 19:15:18 -070076 aioobe_ = nullptr;
Brian Carlstrom4d571432012-05-16 00:21:41 -070077 }
Ian Rogers2d10b202014-05-12 19:15:18 -070078 if (ase_ != nullptr) {
Brian Carlstrom4d571432012-05-16 00:21:41 -070079 env_->DeleteGlobalRef(ase_);
Ian Rogers2d10b202014-05-12 19:15:18 -070080 ase_ = nullptr;
Brian Carlstrom4d571432012-05-16 00:21:41 -070081 }
Ian Rogers2d10b202014-05-12 19:15:18 -070082 if (sioobe_ != nullptr) {
Brian Carlstrom4d571432012-05-16 00:21:41 -070083 env_->DeleteGlobalRef(sioobe_);
Ian Rogers2d10b202014-05-12 19:15:18 -070084 sioobe_ = nullptr;
Brian Carlstrom4d571432012-05-16 00:21:41 -070085 }
86 }
87
Ian Rogers53b8b092014-03-13 23:45:53 -070088 virtual void TearDown() OVERRIDE {
Brian Carlstrom4d571432012-05-16 00:21:41 -070089 CleanUpJniEnv();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080090 CommonCompilerTest::TearDown();
Elliott Hughesc7ac37f2011-08-12 12:21:58 -070091 }
Elliott Hughesb465ab02011-08-24 11:21:21 -070092
Ian Rogers1d99e452014-01-02 17:36:41 -080093 jclass GetPrimitiveClass(char descriptor) {
94 ScopedObjectAccess soa(env_);
95 mirror::Class* c = class_linker_->FindPrimitiveClass(descriptor);
96 CHECK(c != nullptr);
97 return soa.AddLocalReference<jclass>(c);
98 }
99
Elliott Hughesa2501992011-08-26 19:39:54 -0700100 JavaVMExt* vm_;
Brian Carlstrom4d571432012-05-16 00:21:41 -0700101 JNIEnv* env_;
Elliott Hughes814e4032011-08-23 12:07:56 -0700102 jclass aioobe_;
Elliott Hughesb264f082012-04-06 17:10:10 -0700103 jclass ase_;
Elliott Hughesb465ab02011-08-24 11:21:21 -0700104 jclass sioobe_;
Elliott Hughes0c9cd562011-08-12 10:59:29 -0700105};
106
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700107TEST_F(JniInternalTest, AllocObject) {
108 jclass c = env_->FindClass("java/lang/String");
Ian Rogers2d10b202014-05-12 19:15:18 -0700109 ASSERT_NE(c, nullptr);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700110 jobject o = env_->AllocObject(c);
Ian Rogers2d10b202014-05-12 19:15:18 -0700111 ASSERT_NE(o, nullptr);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700112
113 // We have an instance of the class we asked for...
114 ASSERT_TRUE(env_->IsInstanceOf(o, c));
115 // ...whose fields haven't been initialized because
116 // we didn't call a constructor.
117 ASSERT_EQ(0, env_->GetIntField(o, env_->GetFieldID(c, "count", "I")));
118 ASSERT_EQ(0, env_->GetIntField(o, env_->GetFieldID(c, "offset", "I")));
Ian Rogers2d10b202014-05-12 19:15:18 -0700119 ASSERT_TRUE(env_->GetObjectField(o, env_->GetFieldID(c, "value", "[C")) == nullptr);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700120}
121
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700122TEST_F(JniInternalTest, GetVersion) {
123 ASSERT_EQ(JNI_VERSION_1_6, env_->GetVersion());
124}
125
Elliott Hughes0c9cd562011-08-12 10:59:29 -0700126TEST_F(JniInternalTest, FindClass) {
Elliott Hughes0c9cd562011-08-12 10:59:29 -0700127 // Reference types...
Ian Rogers2d10b202014-05-12 19:15:18 -0700128 ExpectClassFound("java/lang/String");
Elliott Hughes0c9cd562011-08-12 10:59:29 -0700129 // ...for arrays too, where you must include "L;".
Ian Rogers2d10b202014-05-12 19:15:18 -0700130 ExpectClassFound("[Ljava/lang/String;");
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700131 // Primitive arrays are okay too, if the primitive type is valid.
Ian Rogers2d10b202014-05-12 19:15:18 -0700132 ExpectClassFound("[C");
Elliott Hughesa2501992011-08-26 19:39:54 -0700133
Elliott Hughesb264f082012-04-06 17:10:10 -0700134 {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700135 CheckJniAbortCatcher check_jni_abort_catcher;
Ian Rogers2d10b202014-05-12 19:15:18 -0700136 env_->FindClass(nullptr);
137 check_jni_abort_catcher.Check("name == null");
138
139 // We support . as well as / for compatibility, if -Xcheck:jni is off.
140 ExpectClassFound("java.lang.String");
Elliott Hughes56ef0422012-06-19 14:35:04 -0700141 check_jni_abort_catcher.Check("illegal class name 'java.lang.String'");
Ian Rogers2d10b202014-05-12 19:15:18 -0700142 ExpectClassNotFound("Ljava.lang.String;");
Elliott Hughes56ef0422012-06-19 14:35:04 -0700143 check_jni_abort_catcher.Check("illegal class name 'Ljava.lang.String;'");
Ian Rogers2d10b202014-05-12 19:15:18 -0700144 ExpectClassFound("[Ljava.lang.String;");
Elliott Hughes56ef0422012-06-19 14:35:04 -0700145 check_jni_abort_catcher.Check("illegal class name '[Ljava.lang.String;'");
Ian Rogers2d10b202014-05-12 19:15:18 -0700146 ExpectClassNotFound("[java.lang.String");
Elliott Hughes56ef0422012-06-19 14:35:04 -0700147 check_jni_abort_catcher.Check("illegal class name '[java.lang.String'");
Elliott Hughesb264f082012-04-06 17:10:10 -0700148
149 // You can't include the "L;" in a JNI class descriptor.
Ian Rogers2d10b202014-05-12 19:15:18 -0700150 ExpectClassNotFound("Ljava/lang/String;");
Elliott Hughes56ef0422012-06-19 14:35:04 -0700151 check_jni_abort_catcher.Check("illegal class name 'Ljava/lang/String;'");
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700152
Elliott Hughesb264f082012-04-06 17:10:10 -0700153 // But you must include it for an array of any reference type.
Ian Rogers2d10b202014-05-12 19:15:18 -0700154 ExpectClassNotFound("[java/lang/String");
Elliott Hughes56ef0422012-06-19 14:35:04 -0700155 check_jni_abort_catcher.Check("illegal class name '[java/lang/String'");
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700156
Ian Rogers2d10b202014-05-12 19:15:18 -0700157 ExpectClassNotFound("[K");
Elliott Hughes56ef0422012-06-19 14:35:04 -0700158 check_jni_abort_catcher.Check("illegal class name '[K'");
Ian Rogers2d10b202014-05-12 19:15:18 -0700159
160 // Void arrays aren't allowed.
161 ExpectClassNotFound("[V");
162 check_jni_abort_catcher.Check("illegal class name '[V'");
Elliott Hughesb264f082012-04-06 17:10:10 -0700163 }
Elliott Hughesa2501992011-08-26 19:39:54 -0700164
Elliott Hughes0c9cd562011-08-12 10:59:29 -0700165 // But primitive types aren't allowed...
Ian Rogers2d10b202014-05-12 19:15:18 -0700166 ExpectClassNotFound("C");
167 ExpectClassNotFound("V");
168 ExpectClassNotFound("K");
Elliott Hughes0c9cd562011-08-12 10:59:29 -0700169}
170
Elliott Hughescdf53122011-08-19 15:46:09 -0700171TEST_F(JniInternalTest, GetFieldID) {
172 jclass jlnsfe = env_->FindClass("java/lang/NoSuchFieldError");
Ian Rogers2d10b202014-05-12 19:15:18 -0700173 ASSERT_NE(jlnsfe, nullptr);
Elliott Hughescdf53122011-08-19 15:46:09 -0700174 jclass c = env_->FindClass("java/lang/String");
Ian Rogers2d10b202014-05-12 19:15:18 -0700175 ASSERT_NE(c, nullptr);
Elliott Hughescdf53122011-08-19 15:46:09 -0700176
177 // Wrong type.
178 jfieldID fid = env_->GetFieldID(c, "count", "J");
Ian Rogers2d10b202014-05-12 19:15:18 -0700179 EXPECT_EQ(nullptr, fid);
180 ExpectException(jlnsfe);
Elliott Hughescdf53122011-08-19 15:46:09 -0700181
Ian Rogersb17d08b2011-09-02 16:16:49 -0700182 // Wrong type where type doesn't exist.
183 fid = env_->GetFieldID(c, "count", "Lrod/jane/freddy;");
Ian Rogers2d10b202014-05-12 19:15:18 -0700184 EXPECT_EQ(nullptr, fid);
185 ExpectException(jlnsfe);
Ian Rogersb17d08b2011-09-02 16:16:49 -0700186
Elliott Hughescdf53122011-08-19 15:46:09 -0700187 // Wrong name.
188 fid = env_->GetFieldID(c, "Count", "I");
Ian Rogers2d10b202014-05-12 19:15:18 -0700189 EXPECT_EQ(nullptr, fid);
190 ExpectException(jlnsfe);
Elliott Hughescdf53122011-08-19 15:46:09 -0700191
192 // Good declared field lookup.
193 fid = env_->GetFieldID(c, "count", "I");
Ian Rogers2d10b202014-05-12 19:15:18 -0700194 EXPECT_NE(nullptr, fid);
Elliott Hughescdf53122011-08-19 15:46:09 -0700195 EXPECT_FALSE(env_->ExceptionCheck());
196
197 // Good superclass field lookup.
198 c = env_->FindClass("java/lang/StringBuilder");
199 fid = env_->GetFieldID(c, "count", "I");
Ian Rogers2d10b202014-05-12 19:15:18 -0700200 EXPECT_NE(nullptr, fid);
201 EXPECT_NE(fid, nullptr);
Elliott Hughescdf53122011-08-19 15:46:09 -0700202 EXPECT_FALSE(env_->ExceptionCheck());
203
204 // Not instance.
205 fid = env_->GetFieldID(c, "CASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;");
Ian Rogers2d10b202014-05-12 19:15:18 -0700206 EXPECT_EQ(nullptr, fid);
207 ExpectException(jlnsfe);
208
209 // Bad arguments.
210 CheckJniAbortCatcher check_jni_abort_catcher;
211 fid = env_->GetFieldID(nullptr, "count", "I");
212 EXPECT_EQ(nullptr, fid);
213 check_jni_abort_catcher.Check("java_class == null");
214 fid = env_->GetFieldID(c, nullptr, "I");
215 EXPECT_EQ(nullptr, fid);
216 check_jni_abort_catcher.Check("name == null");
217 fid = env_->GetFieldID(c, "count", nullptr);
218 EXPECT_EQ(nullptr, fid);
219 check_jni_abort_catcher.Check("sig == null");
Elliott Hughescdf53122011-08-19 15:46:09 -0700220}
221
222TEST_F(JniInternalTest, GetStaticFieldID) {
223 jclass jlnsfe = env_->FindClass("java/lang/NoSuchFieldError");
Ian Rogers2d10b202014-05-12 19:15:18 -0700224 ASSERT_NE(jlnsfe, nullptr);
Elliott Hughescdf53122011-08-19 15:46:09 -0700225 jclass c = env_->FindClass("java/lang/String");
Ian Rogers2d10b202014-05-12 19:15:18 -0700226 ASSERT_NE(c, nullptr);
Elliott Hughescdf53122011-08-19 15:46:09 -0700227
228 // Wrong type.
229 jfieldID fid = env_->GetStaticFieldID(c, "CASE_INSENSITIVE_ORDER", "J");
Ian Rogers2d10b202014-05-12 19:15:18 -0700230 EXPECT_EQ(nullptr, fid);
231 ExpectException(jlnsfe);
Elliott Hughescdf53122011-08-19 15:46:09 -0700232
Ian Rogersb17d08b2011-09-02 16:16:49 -0700233 // Wrong type where type doesn't exist.
234 fid = env_->GetStaticFieldID(c, "CASE_INSENSITIVE_ORDER", "Lrod/jane/freddy;");
Ian Rogers2d10b202014-05-12 19:15:18 -0700235 EXPECT_EQ(nullptr, fid);
236 ExpectException(jlnsfe);
Ian Rogersb17d08b2011-09-02 16:16:49 -0700237
Elliott Hughescdf53122011-08-19 15:46:09 -0700238 // Wrong name.
239 fid = env_->GetStaticFieldID(c, "cASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;");
Ian Rogers2d10b202014-05-12 19:15:18 -0700240 EXPECT_EQ(nullptr, fid);
241 ExpectException(jlnsfe);
Elliott Hughescdf53122011-08-19 15:46:09 -0700242
243 // Good declared field lookup.
244 fid = env_->GetStaticFieldID(c, "CASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;");
Ian Rogers2d10b202014-05-12 19:15:18 -0700245 EXPECT_NE(nullptr, fid);
246 EXPECT_NE(fid, nullptr);
Elliott Hughescdf53122011-08-19 15:46:09 -0700247 EXPECT_FALSE(env_->ExceptionCheck());
248
249 // Not static.
250 fid = env_->GetStaticFieldID(c, "count", "I");
Ian Rogers2d10b202014-05-12 19:15:18 -0700251 EXPECT_EQ(nullptr, fid);
252 ExpectException(jlnsfe);
253
254 // Bad arguments.
255 CheckJniAbortCatcher check_jni_abort_catcher;
256 fid = env_->GetStaticFieldID(nullptr, "CASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;");
257 EXPECT_EQ(nullptr, fid);
258 check_jni_abort_catcher.Check("java_class == null");
259 fid = env_->GetStaticFieldID(c, nullptr, "Ljava/util/Comparator;");
260 EXPECT_EQ(nullptr, fid);
261 check_jni_abort_catcher.Check("name == null");
262 fid = env_->GetStaticFieldID(c, "CASE_INSENSITIVE_ORDER", nullptr);
263 EXPECT_EQ(nullptr, fid);
264 check_jni_abort_catcher.Check("sig == null");
Elliott Hughescdf53122011-08-19 15:46:09 -0700265}
266
Ian Rogers4dd71f12011-08-16 14:16:02 -0700267TEST_F(JniInternalTest, GetMethodID) {
268 jclass jlobject = env_->FindClass("java/lang/Object");
269 jclass jlstring = env_->FindClass("java/lang/String");
270 jclass jlnsme = env_->FindClass("java/lang/NoSuchMethodError");
271
272 // Sanity check that no exceptions are pending
Elliott Hughescdf53122011-08-19 15:46:09 -0700273 ASSERT_FALSE(env_->ExceptionCheck());
Ian Rogers4dd71f12011-08-16 14:16:02 -0700274
275 // Check that java.lang.Object.foo() doesn't exist and NoSuchMethodError is
276 // a pending exception
277 jmethodID method = env_->GetMethodID(jlobject, "foo", "()V");
Ian Rogers2d10b202014-05-12 19:15:18 -0700278 EXPECT_EQ(nullptr, method);
279 ExpectException(jlnsme);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700280
281 // Check that java.lang.Object.equals() does exist
Ian Rogers4dd71f12011-08-16 14:16:02 -0700282 method = env_->GetMethodID(jlobject, "equals", "(Ljava/lang/Object;)Z");
Ian Rogers2d10b202014-05-12 19:15:18 -0700283 EXPECT_NE(nullptr, method);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700284 EXPECT_FALSE(env_->ExceptionCheck());
Ian Rogers4dd71f12011-08-16 14:16:02 -0700285
286 // Check that GetMethodID for java.lang.String.valueOf(int) fails as the
287 // method is static
288 method = env_->GetMethodID(jlstring, "valueOf", "(I)Ljava/lang/String;");
Ian Rogers2d10b202014-05-12 19:15:18 -0700289 EXPECT_EQ(nullptr, method);
290 ExpectException(jlnsme);
Brian Carlstromea46f952013-07-30 01:26:50 -0700291
292 // Check that GetMethodID for java.lang.NoSuchMethodError.<init>(String) finds the constructor
293 method = env_->GetMethodID(jlnsme, "<init>", "(Ljava/lang/String;)V");
Ian Rogers2d10b202014-05-12 19:15:18 -0700294 EXPECT_NE(nullptr, method);
Brian Carlstromea46f952013-07-30 01:26:50 -0700295 EXPECT_FALSE(env_->ExceptionCheck());
Ian Rogers2d10b202014-05-12 19:15:18 -0700296
297 // Bad arguments.
298 CheckJniAbortCatcher check_jni_abort_catcher;
299 method = env_->GetMethodID(nullptr, "<init>", "(Ljava/lang/String;)V");
300 EXPECT_EQ(nullptr, method);
301 check_jni_abort_catcher.Check("java_class == null");
302 method = env_->GetMethodID(jlnsme, nullptr, "(Ljava/lang/String;)V");
303 EXPECT_EQ(nullptr, method);
304 check_jni_abort_catcher.Check("name == null");
305 method = env_->GetMethodID(jlnsme, "<init>", nullptr);
306 EXPECT_EQ(nullptr, method);
307 check_jni_abort_catcher.Check("sig == null");
Ian Rogers4dd71f12011-08-16 14:16:02 -0700308}
309
310TEST_F(JniInternalTest, GetStaticMethodID) {
311 jclass jlobject = env_->FindClass("java/lang/Object");
312 jclass jlnsme = env_->FindClass("java/lang/NoSuchMethodError");
313
314 // Sanity check that no exceptions are pending
Elliott Hughescdf53122011-08-19 15:46:09 -0700315 ASSERT_FALSE(env_->ExceptionCheck());
Ian Rogers4dd71f12011-08-16 14:16:02 -0700316
317 // Check that java.lang.Object.foo() doesn't exist and NoSuchMethodError is
318 // a pending exception
319 jmethodID method = env_->GetStaticMethodID(jlobject, "foo", "()V");
Ian Rogers2d10b202014-05-12 19:15:18 -0700320 EXPECT_EQ(nullptr, method);
321 ExpectException(jlnsme);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700322
323 // Check that GetStaticMethodID for java.lang.Object.equals(Object) fails as
324 // the method is not static
325 method = env_->GetStaticMethodID(jlobject, "equals", "(Ljava/lang/Object;)Z");
Ian Rogers2d10b202014-05-12 19:15:18 -0700326 EXPECT_EQ(nullptr, method);
327 ExpectException(jlnsme);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700328
329 // Check that java.lang.String.valueOf(int) does exist
Ian Rogers4dd71f12011-08-16 14:16:02 -0700330 jclass jlstring = env_->FindClass("java/lang/String");
Ian Rogers2d10b202014-05-12 19:15:18 -0700331 method = env_->GetStaticMethodID(jlstring, "valueOf", "(I)Ljava/lang/String;");
332 EXPECT_NE(nullptr, method);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700333 EXPECT_FALSE(env_->ExceptionCheck());
Ian Rogers2d10b202014-05-12 19:15:18 -0700334
335 // Bad arguments.
336 CheckJniAbortCatcher check_jni_abort_catcher;
337 method = env_->GetStaticMethodID(nullptr, "valueOf", "(I)Ljava/lang/String;");
338 EXPECT_EQ(nullptr, method);
339 check_jni_abort_catcher.Check("java_class == null");
340 method = env_->GetStaticMethodID(jlstring, nullptr, "(I)Ljava/lang/String;");
341 EXPECT_EQ(nullptr, method);
342 check_jni_abort_catcher.Check("name == null");
343 method = env_->GetStaticMethodID(jlstring, "valueOf", nullptr);
344 EXPECT_EQ(nullptr, method);
345 check_jni_abort_catcher.Check("sig == null");
Ian Rogers4dd71f12011-08-16 14:16:02 -0700346}
347
Elliott Hughescdf53122011-08-19 15:46:09 -0700348TEST_F(JniInternalTest, FromReflectedField_ToReflectedField) {
349 jclass jlrField = env_->FindClass("java/lang/reflect/Field");
350 jclass c = env_->FindClass("java/lang/String");
Ian Rogers2d10b202014-05-12 19:15:18 -0700351 ASSERT_NE(c, nullptr);
Elliott Hughescdf53122011-08-19 15:46:09 -0700352 jfieldID fid = env_->GetFieldID(c, "count", "I");
Ian Rogers2d10b202014-05-12 19:15:18 -0700353 ASSERT_NE(fid, nullptr);
Elliott Hughescdf53122011-08-19 15:46:09 -0700354 // Turn the fid into a java.lang.reflect.Field...
355 jobject field = env_->ToReflectedField(c, fid, JNI_FALSE);
Ian Rogers2d10b202014-05-12 19:15:18 -0700356 ASSERT_NE(c, nullptr);
Elliott Hughescdf53122011-08-19 15:46:09 -0700357 ASSERT_TRUE(env_->IsInstanceOf(field, jlrField));
358 // ...and back again.
359 jfieldID fid2 = env_->FromReflectedField(field);
Ian Rogers2d10b202014-05-12 19:15:18 -0700360 ASSERT_NE(fid2, nullptr);
Brian Carlstromea46f952013-07-30 01:26:50 -0700361 // Make sure we can actually use it.
362 jstring s = env_->NewStringUTF("poop");
363 ASSERT_EQ(4, env_->GetIntField(s, fid2));
Ian Rogers2d10b202014-05-12 19:15:18 -0700364
365 // Bad arguments.
366 CheckJniAbortCatcher check_jni_abort_catcher;
367 field = env_->ToReflectedField(c, nullptr, JNI_FALSE);
368 EXPECT_EQ(field, nullptr);
369 check_jni_abort_catcher.Check("fid == null");
370 fid2 = env_->FromReflectedField(nullptr);
371 ASSERT_EQ(fid2, nullptr);
372 check_jni_abort_catcher.Check("jlr_field == null");
Elliott Hughescdf53122011-08-19 15:46:09 -0700373}
374
375TEST_F(JniInternalTest, FromReflectedMethod_ToReflectedMethod) {
376 jclass jlrMethod = env_->FindClass("java/lang/reflect/Method");
377 jclass c = env_->FindClass("java/lang/String");
Ian Rogers2d10b202014-05-12 19:15:18 -0700378 ASSERT_NE(c, nullptr);
Elliott Hughescdf53122011-08-19 15:46:09 -0700379 jmethodID mid = env_->GetMethodID(c, "length", "()I");
Ian Rogers2d10b202014-05-12 19:15:18 -0700380 ASSERT_NE(mid, nullptr);
Elliott Hughescdf53122011-08-19 15:46:09 -0700381 // Turn the mid into a java.lang.reflect.Method...
382 jobject method = env_->ToReflectedMethod(c, mid, JNI_FALSE);
Ian Rogers2d10b202014-05-12 19:15:18 -0700383 ASSERT_NE(c, nullptr);
Elliott Hughescdf53122011-08-19 15:46:09 -0700384 ASSERT_TRUE(env_->IsInstanceOf(method, jlrMethod));
385 // ...and back again.
386 jmethodID mid2 = env_->FromReflectedMethod(method);
Ian Rogers2d10b202014-05-12 19:15:18 -0700387 ASSERT_NE(mid2, nullptr);
Brian Carlstromea46f952013-07-30 01:26:50 -0700388 // Make sure we can actually use it.
389 jstring s = env_->NewStringUTF("poop");
Ian Rogers5d27faf2014-05-02 17:17:18 -0700390 ASSERT_EQ(4, env_->CallIntMethod(s, mid2));
Ian Rogers2d10b202014-05-12 19:15:18 -0700391
392 // Bad arguments.
393 CheckJniAbortCatcher check_jni_abort_catcher;
394 method = env_->ToReflectedMethod(c, nullptr, JNI_FALSE);
395 EXPECT_EQ(method, nullptr);
396 check_jni_abort_catcher.Check("mid == null");
397 mid2 = env_->FromReflectedMethod(method);
398 ASSERT_EQ(mid2, nullptr);
399 check_jni_abort_catcher.Check("jlr_method == null");
Elliott Hughescdf53122011-08-19 15:46:09 -0700400}
401
Ian Rogers2d10b202014-05-12 19:15:18 -0700402static void BogusMethod() {
403 // You can't pass nullptr function pointers to RegisterNatives.
Elliott Hughes5174fe62011-08-23 15:12:35 -0700404}
405
Ian Rogers2d10b202014-05-12 19:15:18 -0700406TEST_F(JniInternalTest, RegisterAndUnregisterNatives) {
Ian Rogers4dd71f12011-08-16 14:16:02 -0700407 jclass jlobject = env_->FindClass("java/lang/Object");
408 jclass jlnsme = env_->FindClass("java/lang/NoSuchMethodError");
409
Ian Rogers2d10b202014-05-12 19:15:18 -0700410 // Sanity check that no exceptions are pending.
Elliott Hughescdf53122011-08-19 15:46:09 -0700411 ASSERT_FALSE(env_->ExceptionCheck());
Ian Rogers4dd71f12011-08-16 14:16:02 -0700412
Ian Rogers2d10b202014-05-12 19:15:18 -0700413 // Check that registering to a non-existent java.lang.Object.foo() causes a NoSuchMethodError.
Ian Rogers4dd71f12011-08-16 14:16:02 -0700414 {
Ian Rogers2d10b202014-05-12 19:15:18 -0700415 JNINativeMethod methods[] = { { "foo", "()V", nullptr } };
416 EXPECT_EQ(env_->RegisterNatives(jlobject, methods, 1), JNI_ERR);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700417 }
Ian Rogers2d10b202014-05-12 19:15:18 -0700418 ExpectException(jlnsme);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700419
Ian Rogers2d10b202014-05-12 19:15:18 -0700420 // Check that registering non-native methods causes a NoSuchMethodError.
Ian Rogers4dd71f12011-08-16 14:16:02 -0700421 {
Ian Rogers2d10b202014-05-12 19:15:18 -0700422 JNINativeMethod methods[] = { { "equals", "(Ljava/lang/Object;)Z", nullptr } };
423 EXPECT_EQ(env_->RegisterNatives(jlobject, methods, 1), JNI_ERR);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700424 }
Ian Rogers2d10b202014-05-12 19:15:18 -0700425 ExpectException(jlnsme);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700426
Ian Rogers2d10b202014-05-12 19:15:18 -0700427 // Check that registering native methods is successful.
Ian Rogers4dd71f12011-08-16 14:16:02 -0700428 {
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700429 JNINativeMethod methods[] = { { "notify", "()V", reinterpret_cast<void*>(BogusMethod) } };
Ian Rogers2d10b202014-05-12 19:15:18 -0700430 EXPECT_EQ(env_->RegisterNatives(jlobject, methods, 1), JNI_OK);
431 }
432 EXPECT_FALSE(env_->ExceptionCheck());
433 EXPECT_EQ(env_->UnregisterNatives(jlobject), JNI_OK);
434
435 // Check that registering no methods isn't a failure.
436 {
437 JNINativeMethod methods[] = { };
438 EXPECT_EQ(env_->RegisterNatives(jlobject, methods, 0), JNI_OK);
439 }
440 EXPECT_FALSE(env_->ExceptionCheck());
441 EXPECT_EQ(env_->UnregisterNatives(jlobject), JNI_OK);
442
443 // Check that registering a -ve number of methods is a failure.
444 CheckJniAbortCatcher check_jni_abort_catcher;
445 for (int i = -10; i < 0; ++i) {
446 JNINativeMethod methods[] = { };
447 EXPECT_EQ(env_->RegisterNatives(jlobject, methods, i), JNI_ERR);
448 check_jni_abort_catcher.Check("negative method count: ");
Ian Rogers4dd71f12011-08-16 14:16:02 -0700449 }
450 EXPECT_FALSE(env_->ExceptionCheck());
Elliott Hughes5174fe62011-08-23 15:12:35 -0700451
Ian Rogers2d10b202014-05-12 19:15:18 -0700452 // Passing a class of null is a failure.
453 {
454 JNINativeMethod methods[] = { };
455 EXPECT_EQ(env_->RegisterNatives(nullptr, methods, 0), JNI_ERR);
456 check_jni_abort_catcher.Check("java_class == null");
457 }
458
459 // Passing methods as null is a failure.
460 EXPECT_EQ(env_->RegisterNatives(jlobject, nullptr, 1), JNI_ERR);
461 check_jni_abort_catcher.Check("methods == null");
462
463 // Unregisters null is a failure.
464 EXPECT_EQ(env_->UnregisterNatives(nullptr), JNI_ERR);
465 check_jni_abort_catcher.Check("java_class == null");
466
467 // Unregistering a class with no natives is a warning.
468 EXPECT_EQ(env_->UnregisterNatives(jlnsme), JNI_OK);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700469}
470
Brian Carlstromea46f952013-07-30 01:26:50 -0700471#define EXPECT_PRIMITIVE_ARRAY(new_fn, \
472 get_region_fn, \
473 set_region_fn, \
474 get_elements_fn, \
475 release_elements_fn, \
476 scalar_type, \
477 expected_class_descriptor) \
Ian Rogers2d10b202014-05-12 19:15:18 -0700478 jsize size = 4; \
479 \
Ian Rogers1d99e452014-01-02 17:36:41 -0800480 { \
481 CheckJniAbortCatcher jni_abort_catcher; \
482 /* Allocate an negative sized array and check it has the right failure type. */ \
Ian Rogers2d10b202014-05-12 19:15:18 -0700483 EXPECT_EQ(env_->new_fn(-1), nullptr); \
Ian Rogers1d99e452014-01-02 17:36:41 -0800484 jni_abort_catcher.Check("negative array length: -1"); \
Ian Rogers2d10b202014-05-12 19:15:18 -0700485 EXPECT_EQ(env_->new_fn(std::numeric_limits<jint>::min()), nullptr); \
Ian Rogers1d99e452014-01-02 17:36:41 -0800486 jni_abort_catcher.Check("negative array length: -2147483648"); \
Ian Rogers2d10b202014-05-12 19:15:18 -0700487 /* Pass the array as null. */ \
488 EXPECT_EQ(0, env_->GetArrayLength(nullptr)); \
489 jni_abort_catcher.Check("java_array == null"); \
490 env_->get_region_fn(nullptr, 0, 0, nullptr); \
491 jni_abort_catcher.Check("java_array == null"); \
492 env_->set_region_fn(nullptr, 0, 0, nullptr); \
493 jni_abort_catcher.Check("java_array == null"); \
494 env_->get_elements_fn(nullptr, nullptr); \
495 jni_abort_catcher.Check("java_array == null"); \
496 env_->release_elements_fn(nullptr, nullptr, 0); \
497 jni_abort_catcher.Check("java_array == null"); \
498 /* Pass the elements for region as null. */ \
499 scalar_type ## Array a = env_->new_fn(size); \
500 env_->get_region_fn(a, 0, size, nullptr); \
501 jni_abort_catcher.Check("buf == null"); \
502 env_->set_region_fn(a, 0, size, nullptr); \
503 jni_abort_catcher.Check("buf == null"); \
Ian Rogers1d99e452014-01-02 17:36:41 -0800504 } \
Elliott Hughes814e4032011-08-23 12:07:56 -0700505 /* Allocate an array and check it has the right type and length. */ \
506 scalar_type ## Array a = env_->new_fn(size); \
Ian Rogers2d10b202014-05-12 19:15:18 -0700507 EXPECT_NE(a, nullptr); \
Elliott Hughes814e4032011-08-23 12:07:56 -0700508 EXPECT_TRUE(env_->IsInstanceOf(a, env_->FindClass(expected_class_descriptor))); \
509 EXPECT_EQ(size, env_->GetArrayLength(a)); \
Elliott Hughesd7d7f6e2013-09-18 12:00:45 -0700510 \
511 /* GetPrimitiveArrayRegion/SetPrimitiveArrayRegion */ \
Elliott Hughes814e4032011-08-23 12:07:56 -0700512 /* AIOOBE for negative start offset. */ \
Ian Rogers2d10b202014-05-12 19:15:18 -0700513 env_->get_region_fn(a, -1, 1, nullptr); \
514 ExpectException(aioobe_); \
515 env_->set_region_fn(a, -1, 1, nullptr); \
516 ExpectException(aioobe_); \
Elliott Hughesd7d7f6e2013-09-18 12:00:45 -0700517 \
Elliott Hughes814e4032011-08-23 12:07:56 -0700518 /* AIOOBE for negative length. */ \
Ian Rogers2d10b202014-05-12 19:15:18 -0700519 env_->get_region_fn(a, 0, -1, nullptr); \
520 ExpectException(aioobe_); \
521 env_->set_region_fn(a, 0, -1, nullptr); \
522 ExpectException(aioobe_); \
Elliott Hughesd7d7f6e2013-09-18 12:00:45 -0700523 \
Elliott Hughes814e4032011-08-23 12:07:56 -0700524 /* AIOOBE for buffer overrun. */ \
Ian Rogers2d10b202014-05-12 19:15:18 -0700525 env_->get_region_fn(a, size - 1, size, nullptr); \
526 ExpectException(aioobe_); \
527 env_->set_region_fn(a, size - 1, size, nullptr); \
528 ExpectException(aioobe_); \
Elliott Hughesd7d7f6e2013-09-18 12:00:45 -0700529 \
Ian Rogers2d10b202014-05-12 19:15:18 -0700530 /* It's okay for the buffer to be nullptr as long as the length is 0. */ \
531 env_->get_region_fn(a, 2, 0, nullptr); \
Elliott Hughesd7d7f6e2013-09-18 12:00:45 -0700532 /* Even if the offset is invalid... */ \
Ian Rogers2d10b202014-05-12 19:15:18 -0700533 env_->get_region_fn(a, 123, 0, nullptr); \
534 ExpectException(aioobe_); \
Elliott Hughesd7d7f6e2013-09-18 12:00:45 -0700535 \
Ian Rogers2d10b202014-05-12 19:15:18 -0700536 /* It's okay for the buffer to be nullptr as long as the length is 0. */ \
537 env_->set_region_fn(a, 2, 0, nullptr); \
Elliott Hughesd7d7f6e2013-09-18 12:00:45 -0700538 /* Even if the offset is invalid... */ \
Ian Rogers2d10b202014-05-12 19:15:18 -0700539 env_->set_region_fn(a, 123, 0, nullptr); \
540 ExpectException(aioobe_); \
Elliott Hughesd7d7f6e2013-09-18 12:00:45 -0700541 \
Elliott Hughes814e4032011-08-23 12:07:56 -0700542 /* Prepare a couple of buffers. */ \
Ian Rogers700a4022014-05-19 16:49:03 -0700543 std::unique_ptr<scalar_type[]> src_buf(new scalar_type[size]); \
544 std::unique_ptr<scalar_type[]> dst_buf(new scalar_type[size]); \
Elliott Hughes814e4032011-08-23 12:07:56 -0700545 for (jsize i = 0; i < size; ++i) { src_buf[i] = scalar_type(i); } \
546 for (jsize i = 0; i < size; ++i) { dst_buf[i] = scalar_type(-1); } \
Elliott Hughesd7d7f6e2013-09-18 12:00:45 -0700547 \
Elliott Hughes814e4032011-08-23 12:07:56 -0700548 /* Copy all of src_buf onto the heap. */ \
Elliott Hughesee0fa762012-03-26 17:12:41 -0700549 env_->set_region_fn(a, 0, size, &src_buf[0]); \
Elliott Hughes814e4032011-08-23 12:07:56 -0700550 /* Copy back only part. */ \
551 env_->get_region_fn(a, 1, size - 2, &dst_buf[1]); \
Brian Carlstromea46f952013-07-30 01:26:50 -0700552 EXPECT_NE(memcmp(&src_buf[0], &dst_buf[0], size * sizeof(scalar_type)), 0) \
553 << "short copy equal"; \
Elliott Hughes814e4032011-08-23 12:07:56 -0700554 /* Copy the missing pieces. */ \
Elliott Hughesee0fa762012-03-26 17:12:41 -0700555 env_->get_region_fn(a, 0, 1, &dst_buf[0]); \
Elliott Hughes814e4032011-08-23 12:07:56 -0700556 env_->get_region_fn(a, size - 1, 1, &dst_buf[size - 1]); \
Brian Carlstromea46f952013-07-30 01:26:50 -0700557 EXPECT_EQ(memcmp(&src_buf[0], &dst_buf[0], size * sizeof(scalar_type)), 0) \
558 << "fixed copy not equal"; \
Elliott Hughes814e4032011-08-23 12:07:56 -0700559 /* Copy back the whole array. */ \
Elliott Hughesee0fa762012-03-26 17:12:41 -0700560 env_->get_region_fn(a, 0, size, &dst_buf[0]); \
Brian Carlstromea46f952013-07-30 01:26:50 -0700561 EXPECT_EQ(memcmp(&src_buf[0], &dst_buf[0], size * sizeof(scalar_type)), 0) \
562 << "full copy not equal"; \
Elliott Hughes75770752011-08-24 17:52:38 -0700563 /* GetPrimitiveArrayCritical */ \
Ian Rogers2d10b202014-05-12 19:15:18 -0700564 void* v = env_->GetPrimitiveArrayCritical(a, nullptr); \
Brian Carlstromea46f952013-07-30 01:26:50 -0700565 EXPECT_EQ(memcmp(&src_buf[0], v, size * sizeof(scalar_type)), 0) \
566 << "GetPrimitiveArrayCritical not equal"; \
Elliott Hughes75770752011-08-24 17:52:38 -0700567 env_->ReleasePrimitiveArrayCritical(a, v, 0); \
568 /* GetXArrayElements */ \
Ian Rogers2d10b202014-05-12 19:15:18 -0700569 scalar_type* xs = env_->get_elements_fn(a, nullptr); \
Brian Carlstromea46f952013-07-30 01:26:50 -0700570 EXPECT_EQ(memcmp(&src_buf[0], xs, size * sizeof(scalar_type)), 0) \
571 << # get_elements_fn " not equal"; \
Elliott Hughes75770752011-08-24 17:52:38 -0700572 env_->release_elements_fn(a, xs, 0); \
Elliott Hughesbd935992011-08-22 11:59:34 -0700573
Elliott Hughes814e4032011-08-23 12:07:56 -0700574TEST_F(JniInternalTest, BooleanArrays) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700575 EXPECT_PRIMITIVE_ARRAY(NewBooleanArray, GetBooleanArrayRegion, SetBooleanArrayRegion,
576 GetBooleanArrayElements, ReleaseBooleanArrayElements, jboolean, "[Z");
Elliott Hughes814e4032011-08-23 12:07:56 -0700577}
578TEST_F(JniInternalTest, ByteArrays) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700579 EXPECT_PRIMITIVE_ARRAY(NewByteArray, GetByteArrayRegion, SetByteArrayRegion,
580 GetByteArrayElements, ReleaseByteArrayElements, jbyte, "[B");
Elliott Hughes814e4032011-08-23 12:07:56 -0700581}
582TEST_F(JniInternalTest, CharArrays) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700583 EXPECT_PRIMITIVE_ARRAY(NewCharArray, GetCharArrayRegion, SetCharArrayRegion,
584 GetCharArrayElements, ReleaseCharArrayElements, jchar, "[C");
Elliott Hughes814e4032011-08-23 12:07:56 -0700585}
586TEST_F(JniInternalTest, DoubleArrays) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700587 EXPECT_PRIMITIVE_ARRAY(NewDoubleArray, GetDoubleArrayRegion, SetDoubleArrayRegion,
588 GetDoubleArrayElements, ReleaseDoubleArrayElements, jdouble, "[D");
Elliott Hughes814e4032011-08-23 12:07:56 -0700589}
590TEST_F(JniInternalTest, FloatArrays) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700591 EXPECT_PRIMITIVE_ARRAY(NewFloatArray, GetFloatArrayRegion, SetFloatArrayRegion,
592 GetFloatArrayElements, ReleaseFloatArrayElements, jfloat, "[F");
Elliott Hughes814e4032011-08-23 12:07:56 -0700593}
594TEST_F(JniInternalTest, IntArrays) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700595 EXPECT_PRIMITIVE_ARRAY(NewIntArray, GetIntArrayRegion, SetIntArrayRegion,
596 GetIntArrayElements, ReleaseIntArrayElements, jint, "[I");
Elliott Hughes814e4032011-08-23 12:07:56 -0700597}
598TEST_F(JniInternalTest, LongArrays) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700599 EXPECT_PRIMITIVE_ARRAY(NewLongArray, GetLongArrayRegion, SetLongArrayRegion,
600 GetLongArrayElements, ReleaseLongArrayElements, jlong, "[J");
Elliott Hughes814e4032011-08-23 12:07:56 -0700601}
602TEST_F(JniInternalTest, ShortArrays) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700603 EXPECT_PRIMITIVE_ARRAY(NewShortArray, GetShortArrayRegion, SetShortArrayRegion,
604 GetShortArrayElements, ReleaseShortArrayElements, jshort, "[S");
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700605}
606
Ian Rogers2d10b202014-05-12 19:15:18 -0700607TEST_F(JniInternalTest, GetPrimitiveArrayElementsOfWrongType) {
608 CheckJniAbortCatcher jni_abort_catcher;
609 jbooleanArray array = env_->NewBooleanArray(10);
610 jboolean is_copy;
611 EXPECT_EQ(env_->GetByteArrayElements(reinterpret_cast<jbyteArray>(array), &is_copy), nullptr);
612 jni_abort_catcher.Check(
613 "attempt to get byte primitive array elements with an object of type boolean[]");
614 EXPECT_EQ(env_->GetShortArrayElements(reinterpret_cast<jshortArray>(array), &is_copy), nullptr);
615 jni_abort_catcher.Check(
616 "attempt to get short primitive array elements with an object of type boolean[]");
617 EXPECT_EQ(env_->GetCharArrayElements(reinterpret_cast<jcharArray>(array), &is_copy), nullptr);
618 jni_abort_catcher.Check(
619 "attempt to get char primitive array elements with an object of type boolean[]");
620 EXPECT_EQ(env_->GetIntArrayElements(reinterpret_cast<jintArray>(array), &is_copy), nullptr);
621 jni_abort_catcher.Check(
622 "attempt to get int primitive array elements with an object of type boolean[]");
623 EXPECT_EQ(env_->GetLongArrayElements(reinterpret_cast<jlongArray>(array), &is_copy), nullptr);
624 jni_abort_catcher.Check(
625 "attempt to get long primitive array elements with an object of type boolean[]");
626 EXPECT_EQ(env_->GetFloatArrayElements(reinterpret_cast<jfloatArray>(array), &is_copy), nullptr);
627 jni_abort_catcher.Check(
628 "attempt to get float primitive array elements with an object of type boolean[]");
629 EXPECT_EQ(env_->GetDoubleArrayElements(reinterpret_cast<jdoubleArray>(array), &is_copy), nullptr);
630 jni_abort_catcher.Check(
631 "attempt to get double primitive array elements with an object of type boolean[]");
632 jbyteArray array2 = env_->NewByteArray(10);
633 EXPECT_EQ(env_->GetBooleanArrayElements(reinterpret_cast<jbooleanArray>(array2), &is_copy), nullptr);
634 jni_abort_catcher.Check(
635 "attempt to get boolean primitive array elements with an object of type byte[]");
636 jobject object = env_->NewStringUTF("Test String");
637 EXPECT_EQ(env_->GetBooleanArrayElements(reinterpret_cast<jbooleanArray>(object), &is_copy), nullptr);
638 jni_abort_catcher.Check(
639 "attempt to get boolean primitive array elements with an object of type java.lang.String");
640}
641
642TEST_F(JniInternalTest, ReleasePrimitiveArrayElementsOfWrongType) {
643 CheckJniAbortCatcher jni_abort_catcher;
644 jbooleanArray array = env_->NewBooleanArray(10);
645 ASSERT_TRUE(array != nullptr);
646 jboolean is_copy;
647 jboolean* elements = env_->GetBooleanArrayElements(array, &is_copy);
648 ASSERT_TRUE(elements != nullptr);
649 env_->ReleaseByteArrayElements(reinterpret_cast<jbyteArray>(array),
650 reinterpret_cast<jbyte*>(elements), 0);
651 jni_abort_catcher.Check(
652 "attempt to release byte primitive array elements with an object of type boolean[]");
653 env_->ReleaseShortArrayElements(reinterpret_cast<jshortArray>(array),
654 reinterpret_cast<jshort*>(elements), 0);
655 jni_abort_catcher.Check(
656 "attempt to release short primitive array elements with an object of type boolean[]");
657 env_->ReleaseCharArrayElements(reinterpret_cast<jcharArray>(array),
658 reinterpret_cast<jchar*>(elements), 0);
659 jni_abort_catcher.Check(
660 "attempt to release char primitive array elements with an object of type boolean[]");
661 env_->ReleaseIntArrayElements(reinterpret_cast<jintArray>(array),
662 reinterpret_cast<jint*>(elements), 0);
663 jni_abort_catcher.Check(
664 "attempt to release int primitive array elements with an object of type boolean[]");
665 env_->ReleaseLongArrayElements(reinterpret_cast<jlongArray>(array),
666 reinterpret_cast<jlong*>(elements), 0);
667 jni_abort_catcher.Check(
668 "attempt to release long primitive array elements with an object of type boolean[]");
669 env_->ReleaseFloatArrayElements(reinterpret_cast<jfloatArray>(array),
670 reinterpret_cast<jfloat*>(elements), 0);
671 jni_abort_catcher.Check(
672 "attempt to release float primitive array elements with an object of type boolean[]");
673 env_->ReleaseDoubleArrayElements(reinterpret_cast<jdoubleArray>(array),
674 reinterpret_cast<jdouble*>(elements), 0);
675 jni_abort_catcher.Check(
676 "attempt to release double primitive array elements with an object of type boolean[]");
677 jbyteArray array2 = env_->NewByteArray(10);
678 env_->ReleaseBooleanArrayElements(reinterpret_cast<jbooleanArray>(array2), elements, 0);
679 jni_abort_catcher.Check(
680 "attempt to release boolean primitive array elements with an object of type byte[]");
681 jobject object = env_->NewStringUTF("Test String");
682 env_->ReleaseBooleanArrayElements(reinterpret_cast<jbooleanArray>(object), elements, 0);
683 jni_abort_catcher.Check(
684 "attempt to release boolean primitive array elements with an object of type java.lang.String");
685}
686TEST_F(JniInternalTest, GetReleasePrimitiveArrayCriticalOfWrongType) {
687 CheckJniAbortCatcher jni_abort_catcher;
688 jobject object = env_->NewStringUTF("Test String");
689 jboolean is_copy;
690 void* elements = env_->GetPrimitiveArrayCritical(reinterpret_cast<jarray>(object), &is_copy);
691 jni_abort_catcher.Check("expected primitive array, given java.lang.String");
692 env_->ReleasePrimitiveArrayCritical(reinterpret_cast<jarray>(object), elements, 0);
693 jni_abort_catcher.Check("expected primitive array, given java.lang.String");
694}
695
696TEST_F(JniInternalTest, GetPrimitiveArrayRegionElementsOfWrongType) {
697 CheckJniAbortCatcher jni_abort_catcher;
698 constexpr size_t kLength = 10;
699 jbooleanArray array = env_->NewBooleanArray(kLength);
700 ASSERT_TRUE(array != nullptr);
701 jboolean elements[kLength];
702 env_->GetByteArrayRegion(reinterpret_cast<jbyteArray>(array), 0, kLength,
703 reinterpret_cast<jbyte*>(elements));
704 jni_abort_catcher.Check(
705 "attempt to get region of byte primitive array elements with an object of type boolean[]");
706 env_->GetShortArrayRegion(reinterpret_cast<jshortArray>(array), 0, kLength,
707 reinterpret_cast<jshort*>(elements));
708 jni_abort_catcher.Check(
709 "attempt to get region of short primitive array elements with an object of type boolean[]");
710 env_->GetCharArrayRegion(reinterpret_cast<jcharArray>(array), 0, kLength,
711 reinterpret_cast<jchar*>(elements));
712 jni_abort_catcher.Check(
713 "attempt to get region of char primitive array elements with an object of type boolean[]");
714 env_->GetIntArrayRegion(reinterpret_cast<jintArray>(array), 0, kLength,
715 reinterpret_cast<jint*>(elements));
716 jni_abort_catcher.Check(
717 "attempt to get region of int primitive array elements with an object of type boolean[]");
718 env_->GetLongArrayRegion(reinterpret_cast<jlongArray>(array), 0, kLength,
719 reinterpret_cast<jlong*>(elements));
720 jni_abort_catcher.Check(
721 "attempt to get region of long primitive array elements with an object of type boolean[]");
722 env_->GetFloatArrayRegion(reinterpret_cast<jfloatArray>(array), 0, kLength,
723 reinterpret_cast<jfloat*>(elements));
724 jni_abort_catcher.Check(
725 "attempt to get region of float primitive array elements with an object of type boolean[]");
726 env_->GetDoubleArrayRegion(reinterpret_cast<jdoubleArray>(array), 0, kLength,
727 reinterpret_cast<jdouble*>(elements));
728 jni_abort_catcher.Check(
729 "attempt to get region of double primitive array elements with an object of type boolean[]");
730 jbyteArray array2 = env_->NewByteArray(10);
731 env_->GetBooleanArrayRegion(reinterpret_cast<jbooleanArray>(array2), 0, kLength,
732 reinterpret_cast<jboolean*>(elements));
733 jni_abort_catcher.Check(
734 "attempt to get region of boolean primitive array elements with an object of type byte[]");
735 jobject object = env_->NewStringUTF("Test String");
736 env_->GetBooleanArrayRegion(reinterpret_cast<jbooleanArray>(object), 0, kLength,
737 reinterpret_cast<jboolean*>(elements));
738 jni_abort_catcher.Check(
739 "attempt to get region of boolean primitive array elements with an object of type java.lang.String");
740}
741
742TEST_F(JniInternalTest, SetPrimitiveArrayRegionElementsOfWrongType) {
743 CheckJniAbortCatcher jni_abort_catcher;
744 constexpr size_t kLength = 10;
745 jbooleanArray array = env_->NewBooleanArray(kLength);
746 ASSERT_TRUE(array != nullptr);
747 jboolean elements[kLength];
748 env_->SetByteArrayRegion(reinterpret_cast<jbyteArray>(array), 0, kLength,
749 reinterpret_cast<jbyte*>(elements));
750 jni_abort_catcher.Check(
751 "attempt to set region of byte primitive array elements with an object of type boolean[]");
752 env_->SetShortArrayRegion(reinterpret_cast<jshortArray>(array), 0, kLength,
753 reinterpret_cast<jshort*>(elements));
754 jni_abort_catcher.Check(
755 "attempt to set region of short primitive array elements with an object of type boolean[]");
756 env_->SetCharArrayRegion(reinterpret_cast<jcharArray>(array), 0, kLength,
757 reinterpret_cast<jchar*>(elements));
758 jni_abort_catcher.Check(
759 "attempt to set region of char primitive array elements with an object of type boolean[]");
760 env_->SetIntArrayRegion(reinterpret_cast<jintArray>(array), 0, kLength,
761 reinterpret_cast<jint*>(elements));
762 jni_abort_catcher.Check(
763 "attempt to set region of int primitive array elements with an object of type boolean[]");
764 env_->SetLongArrayRegion(reinterpret_cast<jlongArray>(array), 0, kLength,
765 reinterpret_cast<jlong*>(elements));
766 jni_abort_catcher.Check(
767 "attempt to set region of long primitive array elements with an object of type boolean[]");
768 env_->SetFloatArrayRegion(reinterpret_cast<jfloatArray>(array), 0, kLength,
769 reinterpret_cast<jfloat*>(elements));
770 jni_abort_catcher.Check(
771 "attempt to set region of float primitive array elements with an object of type boolean[]");
772 env_->SetDoubleArrayRegion(reinterpret_cast<jdoubleArray>(array), 0, kLength,
773 reinterpret_cast<jdouble*>(elements));
774 jni_abort_catcher.Check(
775 "attempt to set region of double primitive array elements with an object of type boolean[]");
776 jbyteArray array2 = env_->NewByteArray(10);
777 env_->SetBooleanArrayRegion(reinterpret_cast<jbooleanArray>(array2), 0, kLength,
778 reinterpret_cast<jboolean*>(elements));
779 jni_abort_catcher.Check(
780 "attempt to set region of boolean primitive array elements with an object of type byte[]");
781 jobject object = env_->NewStringUTF("Test String");
782 env_->SetBooleanArrayRegion(reinterpret_cast<jbooleanArray>(object), 0, kLength,
783 reinterpret_cast<jboolean*>(elements));
784 jni_abort_catcher.Check(
785 "attempt to set region of boolean primitive array elements with an object of type java.lang.String");
786}
787
Elliott Hughesf2682d52011-08-15 16:37:04 -0700788TEST_F(JniInternalTest, NewObjectArray) {
Elliott Hughesbd935992011-08-22 11:59:34 -0700789 jclass element_class = env_->FindClass("java/lang/String");
Ian Rogers2d10b202014-05-12 19:15:18 -0700790 ASSERT_NE(element_class, nullptr);
Elliott Hughesbd935992011-08-22 11:59:34 -0700791 jclass array_class = env_->FindClass("[Ljava/lang/String;");
Ian Rogers2d10b202014-05-12 19:15:18 -0700792 ASSERT_NE(array_class, nullptr);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700793
Ian Rogers1d99e452014-01-02 17:36:41 -0800794 jobjectArray a = env_->NewObjectArray(0, element_class, nullptr);
Ian Rogers2d10b202014-05-12 19:15:18 -0700795 EXPECT_NE(a, nullptr);
Elliott Hughesbd935992011-08-22 11:59:34 -0700796 EXPECT_TRUE(env_->IsInstanceOf(a, array_class));
797 EXPECT_EQ(0, env_->GetArrayLength(a));
798
Ian Rogers1d99e452014-01-02 17:36:41 -0800799 a = env_->NewObjectArray(1, element_class, nullptr);
Ian Rogers2d10b202014-05-12 19:15:18 -0700800 EXPECT_NE(a, nullptr);
Elliott Hughesbd935992011-08-22 11:59:34 -0700801 EXPECT_TRUE(env_->IsInstanceOf(a, array_class));
802 EXPECT_EQ(1, env_->GetArrayLength(a));
Ian Rogers1d99e452014-01-02 17:36:41 -0800803 EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(a, 0), nullptr));
Ian Rogers1d99e452014-01-02 17:36:41 -0800804
Ian Rogers2d10b202014-05-12 19:15:18 -0700805 // Negative array length checks.
Ian Rogers1d99e452014-01-02 17:36:41 -0800806 CheckJniAbortCatcher jni_abort_catcher;
Ian Rogers1d99e452014-01-02 17:36:41 -0800807 env_->NewObjectArray(-1, element_class, nullptr);
808 jni_abort_catcher.Check("negative array length: -1");
809
810 env_->NewObjectArray(std::numeric_limits<jint>::min(), element_class, nullptr);
811 jni_abort_catcher.Check("negative array length: -2147483648");
812}
813
814TEST_F(JniInternalTest, NewObjectArrayWithPrimitiveClasses) {
815 const char* primitive_descriptors = "VZBSCIJFD";
816 const char* primitive_names[] = {
817 "void", "boolean", "byte", "short", "char", "int", "long", "float", "double"
818 };
819 ASSERT_EQ(strlen(primitive_descriptors), arraysize(primitive_names));
820
821 CheckJniAbortCatcher jni_abort_catcher;
822 for (size_t i = 0; i < strlen(primitive_descriptors); ++i) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700823 env_->NewObjectArray(0, nullptr, nullptr);
824 jni_abort_catcher.Check("element_jclass == null");
Ian Rogers1d99e452014-01-02 17:36:41 -0800825 jclass primitive_class = GetPrimitiveClass(primitive_descriptors[i]);
826 env_->NewObjectArray(1, primitive_class, nullptr);
827 std::string error_msg(StringPrintf("not an object type: %s", primitive_names[i]));
828 jni_abort_catcher.Check(error_msg.c_str());
829 }
830}
831
832TEST_F(JniInternalTest, NewObjectArrayWithInitialValue) {
833 jclass element_class = env_->FindClass("java/lang/String");
Ian Rogers2d10b202014-05-12 19:15:18 -0700834 ASSERT_NE(element_class, nullptr);
Ian Rogers1d99e452014-01-02 17:36:41 -0800835 jclass array_class = env_->FindClass("[Ljava/lang/String;");
Ian Rogers2d10b202014-05-12 19:15:18 -0700836 ASSERT_NE(array_class, nullptr);
Elliott Hughes75770752011-08-24 17:52:38 -0700837
838 jstring s = env_->NewStringUTF("poop");
Ian Rogers1d99e452014-01-02 17:36:41 -0800839 jobjectArray a = env_->NewObjectArray(2, element_class, s);
Ian Rogers2d10b202014-05-12 19:15:18 -0700840 EXPECT_NE(a, nullptr);
Elliott Hughes75770752011-08-24 17:52:38 -0700841 EXPECT_TRUE(env_->IsInstanceOf(a, array_class));
842 EXPECT_EQ(2, env_->GetArrayLength(a));
843 EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(a, 0), s));
844 EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(a, 1), s));
Ian Rogers1d99e452014-01-02 17:36:41 -0800845
846 // Attempt to incorrect create an array of strings with initial value of string arrays.
847 CheckJniAbortCatcher jni_abort_catcher;
848 env_->NewObjectArray(2, element_class, a);
849 jni_abort_catcher.Check("cannot assign object of type 'java.lang.String[]' to array with element "
850 "type of 'java.lang.String'");
Elliott Hughesbd935992011-08-22 11:59:34 -0700851}
852
853TEST_F(JniInternalTest, GetArrayLength) {
854 // Already tested in NewObjectArray/NewPrimitiveArray.
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700855}
856
Elliott Hughes37f7a402011-08-22 18:56:01 -0700857TEST_F(JniInternalTest, GetObjectClass) {
858 jclass string_class = env_->FindClass("java/lang/String");
Ian Rogers2d10b202014-05-12 19:15:18 -0700859 ASSERT_NE(string_class, nullptr);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700860 jclass class_class = env_->FindClass("java/lang/Class");
Ian Rogers2d10b202014-05-12 19:15:18 -0700861 ASSERT_NE(class_class, nullptr);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700862
863 jstring s = env_->NewStringUTF("poop");
864 jclass c = env_->GetObjectClass(s);
865 ASSERT_TRUE(env_->IsSameObject(string_class, c));
866
867 jclass c2 = env_->GetObjectClass(c);
868 ASSERT_TRUE(env_->IsSameObject(class_class, env_->GetObjectClass(c2)));
Ian Rogers2d10b202014-05-12 19:15:18 -0700869
870 // Null as object should fail.
871 CheckJniAbortCatcher jni_abort_catcher;
872 EXPECT_EQ(env_->GetObjectClass(nullptr), nullptr);
873 jni_abort_catcher.Check("java_object == null");
Elliott Hughes37f7a402011-08-22 18:56:01 -0700874}
875
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700876TEST_F(JniInternalTest, GetSuperclass) {
877 jclass object_class = env_->FindClass("java/lang/Object");
Ian Rogers2d10b202014-05-12 19:15:18 -0700878 ASSERT_NE(object_class, nullptr);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700879 jclass string_class = env_->FindClass("java/lang/String");
Ian Rogers2d10b202014-05-12 19:15:18 -0700880 ASSERT_NE(string_class, nullptr);
Ian Rogersdc180202012-01-29 14:47:29 -0800881 jclass runnable_interface = env_->FindClass("java/lang/Runnable");
Ian Rogers2d10b202014-05-12 19:15:18 -0700882 ASSERT_NE(runnable_interface, nullptr);
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700883 ASSERT_TRUE(env_->IsSameObject(object_class, env_->GetSuperclass(string_class)));
Ian Rogers2d10b202014-05-12 19:15:18 -0700884 ASSERT_EQ(env_->GetSuperclass(object_class), nullptr);
Ian Rogersdc180202012-01-29 14:47:29 -0800885 ASSERT_TRUE(env_->IsSameObject(object_class, env_->GetSuperclass(runnable_interface)));
Ian Rogers2d10b202014-05-12 19:15:18 -0700886
887 // Null as class should fail.
888 CheckJniAbortCatcher jni_abort_catcher;
889 EXPECT_EQ(env_->GetSuperclass(nullptr), nullptr);
890 jni_abort_catcher.Check("java_class == null");
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700891}
892
Elliott Hughes37f7a402011-08-22 18:56:01 -0700893TEST_F(JniInternalTest, IsAssignableFrom) {
894 jclass object_class = env_->FindClass("java/lang/Object");
Ian Rogers2d10b202014-05-12 19:15:18 -0700895 ASSERT_NE(object_class, nullptr);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700896 jclass string_class = env_->FindClass("java/lang/String");
Ian Rogers2d10b202014-05-12 19:15:18 -0700897 ASSERT_NE(string_class, nullptr);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700898
899 ASSERT_TRUE(env_->IsAssignableFrom(object_class, string_class));
900 ASSERT_FALSE(env_->IsAssignableFrom(string_class, object_class));
Ian Rogers2d10b202014-05-12 19:15:18 -0700901
902 // Null as either class should fail.
903 CheckJniAbortCatcher jni_abort_catcher;
904 EXPECT_EQ(env_->IsAssignableFrom(nullptr, string_class), JNI_FALSE);
905 jni_abort_catcher.Check("java_class1 == null");
906 EXPECT_EQ(env_->IsAssignableFrom(object_class, nullptr), JNI_FALSE);
907 jni_abort_catcher.Check("java_class2 == null");
Elliott Hughes37f7a402011-08-22 18:56:01 -0700908}
909
Elliott Hughesb465ab02011-08-24 11:21:21 -0700910TEST_F(JniInternalTest, GetObjectRefType) {
911 jclass local = env_->FindClass("java/lang/Object");
Ian Rogers2d10b202014-05-12 19:15:18 -0700912 ASSERT_TRUE(local != nullptr);
Elliott Hughesb465ab02011-08-24 11:21:21 -0700913 EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(local));
914
915 jobject global = env_->NewGlobalRef(local);
916 EXPECT_EQ(JNIGlobalRefType, env_->GetObjectRefType(global));
917
918 jweak weak_global = env_->NewWeakGlobalRef(local);
919 EXPECT_EQ(JNIWeakGlobalRefType, env_->GetObjectRefType(weak_global));
920
921 jobject invalid = reinterpret_cast<jobject>(this);
922 EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(invalid));
923
924 // TODO: invoke a native method and test that its arguments are considered local references.
Ian Rogers2d10b202014-05-12 19:15:18 -0700925
926 // Null as object should fail.
927 CheckJniAbortCatcher jni_abort_catcher;
928 EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(nullptr));
929 jni_abort_catcher.Check("java_object == null");
Elliott Hughesb465ab02011-08-24 11:21:21 -0700930}
931
Mathieu Chartier08599992013-12-20 17:17:55 -0800932TEST_F(JniInternalTest, StaleWeakGlobal) {
933 jclass java_lang_Class = env_->FindClass("java/lang/Class");
Ian Rogers2d10b202014-05-12 19:15:18 -0700934 ASSERT_NE(java_lang_Class, nullptr);
935 jobjectArray local_ref = env_->NewObjectArray(1, java_lang_Class, nullptr);
936 ASSERT_NE(local_ref, nullptr);
Mathieu Chartier08599992013-12-20 17:17:55 -0800937 jweak weak_global = env_->NewWeakGlobalRef(local_ref);
Ian Rogers2d10b202014-05-12 19:15:18 -0700938 ASSERT_NE(weak_global, nullptr);
Mathieu Chartier08599992013-12-20 17:17:55 -0800939 env_->DeleteLocalRef(local_ref);
940 Runtime::Current()->GetHeap()->CollectGarbage(false); // GC should clear the weak global.
941 jobject new_global_ref = env_->NewGlobalRef(weak_global);
Ian Rogers2d10b202014-05-12 19:15:18 -0700942 EXPECT_EQ(new_global_ref, nullptr);
Mathieu Chartier08599992013-12-20 17:17:55 -0800943 jobject new_local_ref = env_->NewLocalRef(weak_global);
Ian Rogers2d10b202014-05-12 19:15:18 -0700944 EXPECT_EQ(new_local_ref, nullptr);
Mathieu Chartier08599992013-12-20 17:17:55 -0800945}
946
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700947TEST_F(JniInternalTest, NewStringUTF) {
Ian Rogers2d10b202014-05-12 19:15:18 -0700948 EXPECT_EQ(env_->NewStringUTF(nullptr), nullptr);
Elliott Hughes814e4032011-08-23 12:07:56 -0700949 jstring s;
950
951 s = env_->NewStringUTF("");
Ian Rogers2d10b202014-05-12 19:15:18 -0700952 EXPECT_NE(s, nullptr);
Elliott Hughes814e4032011-08-23 12:07:56 -0700953 EXPECT_EQ(0, env_->GetStringLength(s));
954 EXPECT_EQ(0, env_->GetStringUTFLength(s));
955 s = env_->NewStringUTF("hello");
Ian Rogers2d10b202014-05-12 19:15:18 -0700956 EXPECT_NE(s, nullptr);
Elliott Hughes814e4032011-08-23 12:07:56 -0700957 EXPECT_EQ(5, env_->GetStringLength(s));
958 EXPECT_EQ(5, env_->GetStringUTFLength(s));
959
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700960 // TODO: check some non-ASCII strings.
Elliott Hughesf2682d52011-08-15 16:37:04 -0700961}
962
Elliott Hughes814e4032011-08-23 12:07:56 -0700963TEST_F(JniInternalTest, NewString) {
Elliott Hughes814e4032011-08-23 12:07:56 -0700964 jchar chars[] = { 'h', 'i' };
965 jstring s;
966 s = env_->NewString(chars, 0);
Ian Rogers2d10b202014-05-12 19:15:18 -0700967 EXPECT_NE(s, nullptr);
Elliott Hughes814e4032011-08-23 12:07:56 -0700968 EXPECT_EQ(0, env_->GetStringLength(s));
969 EXPECT_EQ(0, env_->GetStringUTFLength(s));
970 s = env_->NewString(chars, 2);
Ian Rogers2d10b202014-05-12 19:15:18 -0700971 EXPECT_NE(s, nullptr);
Elliott Hughes814e4032011-08-23 12:07:56 -0700972 EXPECT_EQ(2, env_->GetStringLength(s));
973 EXPECT_EQ(2, env_->GetStringUTFLength(s));
974
975 // TODO: check some non-ASCII strings.
976}
977
Jesse Wilson25e79a52011-11-18 15:31:58 -0500978TEST_F(JniInternalTest, NewStringNullCharsZeroLength) {
Ian Rogers1d99e452014-01-02 17:36:41 -0800979 jstring s = env_->NewString(nullptr, 0);
Ian Rogers2d10b202014-05-12 19:15:18 -0700980 EXPECT_NE(s, nullptr);
Jesse Wilson25e79a52011-11-18 15:31:58 -0500981 EXPECT_EQ(0, env_->GetStringLength(s));
982}
983
Ian Rogers1d99e452014-01-02 17:36:41 -0800984TEST_F(JniInternalTest, NewStringNullCharsNonzeroLength) {
985 CheckJniAbortCatcher jni_abort_catcher;
986 env_->NewString(nullptr, 1);
987 jni_abort_catcher.Check("chars == null && char_count > 0");
988}
989
990TEST_F(JniInternalTest, NewStringNegativeLength) {
991 CheckJniAbortCatcher jni_abort_catcher;
992 env_->NewString(nullptr, -1);
993 jni_abort_catcher.Check("char_count < 0: -1");
994 env_->NewString(nullptr, std::numeric_limits<jint>::min());
995 jni_abort_catcher.Check("char_count < 0: -2147483648");
Jesse Wilson25e79a52011-11-18 15:31:58 -0500996}
997
Elliott Hughesb465ab02011-08-24 11:21:21 -0700998TEST_F(JniInternalTest, GetStringLength_GetStringUTFLength) {
999 // Already tested in the NewString/NewStringUTF tests.
1000}
1001
1002TEST_F(JniInternalTest, GetStringRegion_GetStringUTFRegion) {
1003 jstring s = env_->NewStringUTF("hello");
Ian Rogers2d10b202014-05-12 19:15:18 -07001004 ASSERT_TRUE(s != nullptr);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001005
Ian Rogers2d10b202014-05-12 19:15:18 -07001006 env_->GetStringRegion(s, -1, 0, nullptr);
1007 ExpectException(sioobe_);
1008 env_->GetStringRegion(s, 0, -1, nullptr);
1009 ExpectException(sioobe_);
1010 env_->GetStringRegion(s, 0, 10, nullptr);
1011 ExpectException(sioobe_);
1012 env_->GetStringRegion(s, 10, 1, nullptr);
1013 ExpectException(sioobe_);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001014
1015 jchar chars[4] = { 'x', 'x', 'x', 'x' };
1016 env_->GetStringRegion(s, 1, 2, &chars[1]);
1017 EXPECT_EQ('x', chars[0]);
1018 EXPECT_EQ('e', chars[1]);
1019 EXPECT_EQ('l', chars[2]);
1020 EXPECT_EQ('x', chars[3]);
1021
Ian Rogers2d10b202014-05-12 19:15:18 -07001022 // It's okay for the buffer to be nullptr as long as the length is 0.
1023 env_->GetStringRegion(s, 2, 0, nullptr);
Elliott Hughesd7d7f6e2013-09-18 12:00:45 -07001024 // Even if the offset is invalid...
Ian Rogers2d10b202014-05-12 19:15:18 -07001025 env_->GetStringRegion(s, 123, 0, nullptr);
1026 ExpectException(sioobe_);
Elliott Hughesd7d7f6e2013-09-18 12:00:45 -07001027
Ian Rogers2d10b202014-05-12 19:15:18 -07001028 env_->GetStringUTFRegion(s, -1, 0, nullptr);
1029 ExpectException(sioobe_);
1030 env_->GetStringUTFRegion(s, 0, -1, nullptr);
1031 ExpectException(sioobe_);
1032 env_->GetStringUTFRegion(s, 0, 10, nullptr);
1033 ExpectException(sioobe_);
1034 env_->GetStringUTFRegion(s, 10, 1, nullptr);
1035 ExpectException(sioobe_);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001036
1037 char bytes[4] = { 'x', 'x', 'x', 'x' };
1038 env_->GetStringUTFRegion(s, 1, 2, &bytes[1]);
1039 EXPECT_EQ('x', bytes[0]);
1040 EXPECT_EQ('e', bytes[1]);
1041 EXPECT_EQ('l', bytes[2]);
1042 EXPECT_EQ('x', bytes[3]);
Elliott Hughesd7d7f6e2013-09-18 12:00:45 -07001043
Ian Rogers2d10b202014-05-12 19:15:18 -07001044 // It's okay for the buffer to be nullptr as long as the length is 0.
1045 env_->GetStringUTFRegion(s, 2, 0, nullptr);
Elliott Hughesd7d7f6e2013-09-18 12:00:45 -07001046 // Even if the offset is invalid...
Ian Rogers2d10b202014-05-12 19:15:18 -07001047 env_->GetStringUTFRegion(s, 123, 0, nullptr);
1048 ExpectException(sioobe_);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001049}
1050
Elliott Hughes75770752011-08-24 17:52:38 -07001051TEST_F(JniInternalTest, GetStringUTFChars_ReleaseStringUTFChars) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001052 // Passing in a nullptr jstring is ignored normally, but caught by -Xcheck:jni.
Elliott Hughesb264f082012-04-06 17:10:10 -07001053 {
Elliott Hughesb264f082012-04-06 17:10:10 -07001054 CheckJniAbortCatcher check_jni_abort_catcher;
Ian Rogers2d10b202014-05-12 19:15:18 -07001055 EXPECT_EQ(env_->GetStringUTFChars(nullptr, nullptr), nullptr);
Elliott Hughes56ef0422012-06-19 14:35:04 -07001056 check_jni_abort_catcher.Check("GetStringUTFChars received null jstring");
Elliott Hughesb264f082012-04-06 17:10:10 -07001057 }
Elliott Hughes75770752011-08-24 17:52:38 -07001058
1059 jstring s = env_->NewStringUTF("hello");
Ian Rogers2d10b202014-05-12 19:15:18 -07001060 ASSERT_TRUE(s != nullptr);
Elliott Hughes75770752011-08-24 17:52:38 -07001061
Ian Rogers2d10b202014-05-12 19:15:18 -07001062 const char* utf = env_->GetStringUTFChars(s, nullptr);
Elliott Hughes75770752011-08-24 17:52:38 -07001063 EXPECT_STREQ("hello", utf);
1064 env_->ReleaseStringUTFChars(s, utf);
1065
1066 jboolean is_copy = JNI_FALSE;
1067 utf = env_->GetStringUTFChars(s, &is_copy);
1068 EXPECT_EQ(JNI_TRUE, is_copy);
1069 EXPECT_STREQ("hello", utf);
1070 env_->ReleaseStringUTFChars(s, utf);
1071}
1072
1073TEST_F(JniInternalTest, GetStringChars_ReleaseStringChars) {
1074 jstring s = env_->NewStringUTF("hello");
Fred Shih56890e22014-06-02 11:11:52 -07001075 ScopedObjectAccess soa(env_);
1076 mirror::String* s_m = soa.Decode<mirror::String*>(s);
Ian Rogers2d10b202014-05-12 19:15:18 -07001077 ASSERT_TRUE(s != nullptr);
Elliott Hughes75770752011-08-24 17:52:38 -07001078
1079 jchar expected[] = { 'h', 'e', 'l', 'l', 'o' };
Ian Rogers2d10b202014-05-12 19:15:18 -07001080 const jchar* chars = env_->GetStringChars(s, nullptr);
Elliott Hughes75770752011-08-24 17:52:38 -07001081 EXPECT_EQ(expected[0], chars[0]);
1082 EXPECT_EQ(expected[1], chars[1]);
1083 EXPECT_EQ(expected[2], chars[2]);
1084 EXPECT_EQ(expected[3], chars[3]);
1085 EXPECT_EQ(expected[4], chars[4]);
1086 env_->ReleaseStringChars(s, chars);
1087
1088 jboolean is_copy = JNI_FALSE;
1089 chars = env_->GetStringChars(s, &is_copy);
Fred Shih56890e22014-06-02 11:11:52 -07001090 if (Runtime::Current()->GetHeap()->IsMovableObject(s_m->GetCharArray())) {
1091 EXPECT_EQ(JNI_TRUE, is_copy);
1092 } else {
1093 EXPECT_EQ(JNI_FALSE, is_copy);
1094 }
Elliott Hughes75770752011-08-24 17:52:38 -07001095 EXPECT_EQ(expected[0], chars[0]);
1096 EXPECT_EQ(expected[1], chars[1]);
1097 EXPECT_EQ(expected[2], chars[2]);
1098 EXPECT_EQ(expected[3], chars[3]);
1099 EXPECT_EQ(expected[4], chars[4]);
1100 env_->ReleaseStringChars(s, chars);
1101}
1102
1103TEST_F(JniInternalTest, GetStringCritical_ReleaseStringCritical) {
1104 jstring s = env_->NewStringUTF("hello");
Ian Rogers2d10b202014-05-12 19:15:18 -07001105 ASSERT_TRUE(s != nullptr);
Elliott Hughes75770752011-08-24 17:52:38 -07001106
1107 jchar expected[] = { 'h', 'e', 'l', 'l', 'o' };
Ian Rogers2d10b202014-05-12 19:15:18 -07001108 const jchar* chars = env_->GetStringCritical(s, nullptr);
Elliott Hughes75770752011-08-24 17:52:38 -07001109 EXPECT_EQ(expected[0], chars[0]);
1110 EXPECT_EQ(expected[1], chars[1]);
1111 EXPECT_EQ(expected[2], chars[2]);
1112 EXPECT_EQ(expected[3], chars[3]);
1113 EXPECT_EQ(expected[4], chars[4]);
1114 env_->ReleaseStringCritical(s, chars);
1115
Fred Shih56890e22014-06-02 11:11:52 -07001116 jboolean is_copy = JNI_TRUE;
Elliott Hughes75770752011-08-24 17:52:38 -07001117 chars = env_->GetStringCritical(s, &is_copy);
Fred Shih56890e22014-06-02 11:11:52 -07001118 EXPECT_EQ(JNI_FALSE, is_copy);
Elliott Hughes75770752011-08-24 17:52:38 -07001119 EXPECT_EQ(expected[0], chars[0]);
1120 EXPECT_EQ(expected[1], chars[1]);
1121 EXPECT_EQ(expected[2], chars[2]);
1122 EXPECT_EQ(expected[3], chars[3]);
1123 EXPECT_EQ(expected[4], chars[4]);
1124 env_->ReleaseStringCritical(s, chars);
1125}
1126
Elliott Hughes814e4032011-08-23 12:07:56 -07001127TEST_F(JniInternalTest, GetObjectArrayElement_SetObjectArrayElement) {
Elliott Hughesb264f082012-04-06 17:10:10 -07001128 jclass java_lang_Class = env_->FindClass("java/lang/Class");
Ian Rogers2d10b202014-05-12 19:15:18 -07001129 ASSERT_TRUE(java_lang_Class != nullptr);
Elliott Hughes289da822011-08-16 10:11:20 -07001130
Ian Rogers2d10b202014-05-12 19:15:18 -07001131 jobjectArray array = env_->NewObjectArray(1, java_lang_Class, nullptr);
1132 EXPECT_NE(array, nullptr);
1133 EXPECT_EQ(env_->GetObjectArrayElement(array, 0), nullptr);
Elliott Hughesb264f082012-04-06 17:10:10 -07001134 env_->SetObjectArrayElement(array, 0, java_lang_Class);
1135 EXPECT_TRUE(env_->IsSameObject(env_->GetObjectArrayElement(array, 0), java_lang_Class));
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001136
1137 // ArrayIndexOutOfBounds for negative index.
Elliott Hughesb264f082012-04-06 17:10:10 -07001138 env_->SetObjectArrayElement(array, -1, java_lang_Class);
Ian Rogers2d10b202014-05-12 19:15:18 -07001139 ExpectException(aioobe_);
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001140
1141 // ArrayIndexOutOfBounds for too-large index.
Elliott Hughesb264f082012-04-06 17:10:10 -07001142 env_->SetObjectArrayElement(array, 1, java_lang_Class);
Ian Rogers2d10b202014-05-12 19:15:18 -07001143 ExpectException(aioobe_);
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001144
Elliott Hughesb264f082012-04-06 17:10:10 -07001145 // ArrayStoreException thrown for bad types.
1146 env_->SetObjectArrayElement(array, 0, env_->NewStringUTF("not a jclass!"));
Ian Rogers2d10b202014-05-12 19:15:18 -07001147 ExpectException(ase_);
1148
1149 // Null as array should fail.
1150 CheckJniAbortCatcher jni_abort_catcher;
1151 EXPECT_EQ(nullptr, env_->GetObjectArrayElement(nullptr, 0));
1152 jni_abort_catcher.Check("java_array == null");
1153 env_->SetObjectArrayElement(nullptr, 0, nullptr);
1154 jni_abort_catcher.Check("java_array == null");
Elliott Hughes289da822011-08-16 10:11:20 -07001155}
1156
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001157#define EXPECT_STATIC_PRIMITIVE_FIELD(type, field_name, sig, value1, value2) \
1158 do { \
1159 jfieldID fid = env_->GetStaticFieldID(c, field_name, sig); \
Ian Rogers2d10b202014-05-12 19:15:18 -07001160 EXPECT_NE(fid, nullptr); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001161 env_->SetStatic ## type ## Field(c, fid, value1); \
Ian Rogers2d10b202014-05-12 19:15:18 -07001162 EXPECT_EQ(value1, env_->GetStatic ## type ## Field(c, fid)); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001163 env_->SetStatic ## type ## Field(c, fid, value2); \
Ian Rogers2d10b202014-05-12 19:15:18 -07001164 EXPECT_EQ(value2, env_->GetStatic ## type ## Field(c, fid)); \
1165 \
1166 CheckJniAbortCatcher jni_abort_catcher; \
1167 env_->GetStatic ## type ## Field(nullptr, fid); \
1168 jni_abort_catcher.Check("received null jclass"); \
1169 env_->SetStatic ## type ## Field(nullptr, fid, value1); \
1170 jni_abort_catcher.Check("received null jclass"); \
1171 env_->GetStatic ## type ## Field(c, nullptr); \
1172 jni_abort_catcher.Check("fid == null"); \
1173 env_->SetStatic ## type ## Field(c, nullptr, value1); \
1174 jni_abort_catcher.Check("fid == null"); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001175 } while (false)
1176
1177#define EXPECT_PRIMITIVE_FIELD(instance, type, field_name, sig, value1, value2) \
1178 do { \
1179 jfieldID fid = env_->GetFieldID(c, field_name, sig); \
Ian Rogers2d10b202014-05-12 19:15:18 -07001180 EXPECT_NE(fid, nullptr); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001181 env_->Set ## type ## Field(instance, fid, value1); \
Ian Rogers2d10b202014-05-12 19:15:18 -07001182 EXPECT_EQ(value1, env_->Get ## type ## Field(instance, fid)); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001183 env_->Set ## type ## Field(instance, fid, value2); \
Ian Rogers2d10b202014-05-12 19:15:18 -07001184 EXPECT_EQ(value2, env_->Get ## type ## Field(instance, fid)); \
1185 \
1186 CheckJniAbortCatcher jni_abort_catcher; \
1187 env_->Get ## type ## Field(nullptr, fid); \
1188 jni_abort_catcher.Check("obj == null"); \
1189 env_->Set ## type ## Field(nullptr, fid, value1); \
1190 jni_abort_catcher.Check("obj == null"); \
1191 env_->Get ## type ## Field(instance, nullptr); \
1192 jni_abort_catcher.Check("fid == null"); \
1193 env_->Set ## type ## Field(instance, nullptr, value1); \
1194 jni_abort_catcher.Check("fid == null"); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001195 } while (false)
1196
1197
1198TEST_F(JniInternalTest, GetPrimitiveField_SetPrimitiveField) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001199 TEST_DISABLED_FOR_PORTABLE();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001200 Thread::Current()->TransitionFromSuspendedToRunnable();
1201 LoadDex("AllFields");
Brian Carlstrombd86bcc2013-03-10 20:26:16 -07001202 bool started = runtime_->Start();
Ian Rogers2d10b202014-05-12 19:15:18 -07001203 ASSERT_TRUE(started);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001204
1205 jclass c = env_->FindClass("AllFields");
Ian Rogers2d10b202014-05-12 19:15:18 -07001206 ASSERT_NE(c, nullptr);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001207 jobject o = env_->AllocObject(c);
Ian Rogers2d10b202014-05-12 19:15:18 -07001208 ASSERT_NE(o, nullptr);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001209
Ian Rogers2d10b202014-05-12 19:15:18 -07001210 EXPECT_STATIC_PRIMITIVE_FIELD(Boolean, "sZ", "Z", JNI_TRUE, JNI_FALSE);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001211 EXPECT_STATIC_PRIMITIVE_FIELD(Byte, "sB", "B", 1, 2);
1212 EXPECT_STATIC_PRIMITIVE_FIELD(Char, "sC", "C", 'a', 'b');
1213 EXPECT_STATIC_PRIMITIVE_FIELD(Double, "sD", "D", 1.0, 2.0);
1214 EXPECT_STATIC_PRIMITIVE_FIELD(Float, "sF", "F", 1.0, 2.0);
1215 EXPECT_STATIC_PRIMITIVE_FIELD(Int, "sI", "I", 1, 2);
1216 EXPECT_STATIC_PRIMITIVE_FIELD(Long, "sJ", "J", 1, 2);
1217 EXPECT_STATIC_PRIMITIVE_FIELD(Short, "sS", "S", 1, 2);
1218
Ian Rogers2d10b202014-05-12 19:15:18 -07001219 EXPECT_PRIMITIVE_FIELD(o, Boolean, "iZ", "Z", JNI_TRUE, JNI_FALSE);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001220 EXPECT_PRIMITIVE_FIELD(o, Byte, "iB", "B", 1, 2);
1221 EXPECT_PRIMITIVE_FIELD(o, Char, "iC", "C", 'a', 'b');
1222 EXPECT_PRIMITIVE_FIELD(o, Double, "iD", "D", 1.0, 2.0);
1223 EXPECT_PRIMITIVE_FIELD(o, Float, "iF", "F", 1.0, 2.0);
1224 EXPECT_PRIMITIVE_FIELD(o, Int, "iI", "I", 1, 2);
1225 EXPECT_PRIMITIVE_FIELD(o, Long, "iJ", "J", 1, 2);
1226 EXPECT_PRIMITIVE_FIELD(o, Short, "iS", "S", 1, 2);
1227}
1228
1229TEST_F(JniInternalTest, GetObjectField_SetObjectField) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001230 TEST_DISABLED_FOR_PORTABLE();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001231 Thread::Current()->TransitionFromSuspendedToRunnable();
1232 LoadDex("AllFields");
Brian Carlstrom25c33252011-09-18 15:58:35 -07001233 runtime_->Start();
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001234
1235 jclass c = env_->FindClass("AllFields");
Ian Rogers2d10b202014-05-12 19:15:18 -07001236 ASSERT_NE(c, nullptr);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001237 jobject o = env_->AllocObject(c);
Ian Rogers2d10b202014-05-12 19:15:18 -07001238 ASSERT_NE(o, nullptr);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001239
1240 jstring s1 = env_->NewStringUTF("hello");
Ian Rogers2d10b202014-05-12 19:15:18 -07001241 ASSERT_NE(s1, nullptr);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001242 jstring s2 = env_->NewStringUTF("world");
Ian Rogers2d10b202014-05-12 19:15:18 -07001243 ASSERT_NE(s2, nullptr);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001244
1245 jfieldID s_fid = env_->GetStaticFieldID(c, "sObject", "Ljava/lang/Object;");
Ian Rogers2d10b202014-05-12 19:15:18 -07001246 ASSERT_NE(s_fid, nullptr);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001247 jfieldID i_fid = env_->GetFieldID(c, "iObject", "Ljava/lang/Object;");
Ian Rogers2d10b202014-05-12 19:15:18 -07001248 ASSERT_NE(i_fid, nullptr);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001249
1250 env_->SetStaticObjectField(c, s_fid, s1);
1251 ASSERT_TRUE(env_->IsSameObject(s1, env_->GetStaticObjectField(c, s_fid)));
1252 env_->SetStaticObjectField(c, s_fid, s2);
1253 ASSERT_TRUE(env_->IsSameObject(s2, env_->GetStaticObjectField(c, s_fid)));
1254
1255 env_->SetObjectField(o, i_fid, s1);
1256 ASSERT_TRUE(env_->IsSameObject(s1, env_->GetObjectField(o, i_fid)));
1257 env_->SetObjectField(o, i_fid, s2);
1258 ASSERT_TRUE(env_->IsSameObject(s2, env_->GetObjectField(o, i_fid)));
1259}
1260
Ian Rogers2d10b202014-05-12 19:15:18 -07001261TEST_F(JniInternalTest, NewLocalRef_nullptr) {
1262 EXPECT_EQ(env_->NewLocalRef(nullptr), nullptr);
Elliott Hughes18c07532011-08-18 15:50:51 -07001263}
1264
1265TEST_F(JniInternalTest, NewLocalRef) {
1266 jstring s = env_->NewStringUTF("");
Ian Rogers2d10b202014-05-12 19:15:18 -07001267 ASSERT_NE(s, nullptr);
Elliott Hughes18c07532011-08-18 15:50:51 -07001268 jobject o = env_->NewLocalRef(s);
Ian Rogers2d10b202014-05-12 19:15:18 -07001269 EXPECT_NE(o, nullptr);
1270 EXPECT_NE(o, s);
Elliott Hughes18c07532011-08-18 15:50:51 -07001271
Elliott Hughes2ced6a52011-10-16 18:44:48 -07001272 EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(o));
Elliott Hughes18c07532011-08-18 15:50:51 -07001273}
1274
Ian Rogers2d10b202014-05-12 19:15:18 -07001275TEST_F(JniInternalTest, DeleteLocalRef_nullptr) {
1276 env_->DeleteLocalRef(nullptr);
Elliott Hughes18c07532011-08-18 15:50:51 -07001277}
1278
1279TEST_F(JniInternalTest, DeleteLocalRef) {
1280 jstring s = env_->NewStringUTF("");
Ian Rogers2d10b202014-05-12 19:15:18 -07001281 ASSERT_NE(s, nullptr);
Elliott Hughes18c07532011-08-18 15:50:51 -07001282 env_->DeleteLocalRef(s);
1283
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001284 // Currently, deleting an already-deleted reference is just a CheckJNI warning.
Elliott Hughesb264f082012-04-06 17:10:10 -07001285 {
Elliott Hughesb264f082012-04-06 17:10:10 -07001286 CheckJniAbortCatcher check_jni_abort_catcher;
1287 env_->DeleteLocalRef(s);
Elliott Hughesa9137c62013-01-09 10:55:21 -08001288
Brian Carlstromea46f952013-07-30 01:26:50 -07001289 std::string expected(StringPrintf("native code passing in reference to "
1290 "invalid local reference: %p", s));
Elliott Hughesa9137c62013-01-09 10:55:21 -08001291 check_jni_abort_catcher.Check(expected.c_str());
Elliott Hughesb264f082012-04-06 17:10:10 -07001292 }
Elliott Hughes18c07532011-08-18 15:50:51 -07001293
1294 s = env_->NewStringUTF("");
Ian Rogers2d10b202014-05-12 19:15:18 -07001295 ASSERT_NE(s, nullptr);
Elliott Hughes18c07532011-08-18 15:50:51 -07001296 jobject o = env_->NewLocalRef(s);
Ian Rogers2d10b202014-05-12 19:15:18 -07001297 ASSERT_NE(o, nullptr);
Elliott Hughes18c07532011-08-18 15:50:51 -07001298
1299 env_->DeleteLocalRef(s);
1300 env_->DeleteLocalRef(o);
1301}
1302
Elliott Hughesaa836f72013-08-20 16:57:23 -07001303TEST_F(JniInternalTest, PushLocalFrame_10395422) {
1304 // The JNI specification is ambiguous about whether the given capacity is to be interpreted as a
1305 // maximum or as a minimum, but it seems like it's supposed to be a minimum, and that's how
1306 // Android historically treated it, and it's how the RI treats it. It's also the more useful
1307 // interpretation!
1308 ASSERT_EQ(JNI_OK, env_->PushLocalFrame(0));
Ian Rogers2d10b202014-05-12 19:15:18 -07001309 env_->PopLocalFrame(nullptr);
Elliott Hughesaa836f72013-08-20 16:57:23 -07001310
1311 // Negative capacities are not allowed.
1312 ASSERT_EQ(JNI_ERR, env_->PushLocalFrame(-1));
1313
1314 // And it's okay to have an upper limit. Ours is currently 512.
1315 ASSERT_EQ(JNI_ERR, env_->PushLocalFrame(8192));
1316}
1317
Elliott Hughes2ced6a52011-10-16 18:44:48 -07001318TEST_F(JniInternalTest, PushLocalFrame_PopLocalFrame) {
1319 jobject original = env_->NewStringUTF("");
Ian Rogers2d10b202014-05-12 19:15:18 -07001320 ASSERT_NE(original, nullptr);
Elliott Hughes2ced6a52011-10-16 18:44:48 -07001321
1322 jobject outer;
1323 jobject inner1, inner2;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001324 ScopedObjectAccess soa(env_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001325 mirror::Object* inner2_direct_pointer;
Elliott Hughes2ced6a52011-10-16 18:44:48 -07001326 {
Elliott Hughesaa836f72013-08-20 16:57:23 -07001327 ASSERT_EQ(JNI_OK, env_->PushLocalFrame(4));
Elliott Hughes2ced6a52011-10-16 18:44:48 -07001328 outer = env_->NewLocalRef(original);
1329
1330 {
Elliott Hughesaa836f72013-08-20 16:57:23 -07001331 ASSERT_EQ(JNI_OK, env_->PushLocalFrame(4));
Elliott Hughes2ced6a52011-10-16 18:44:48 -07001332 inner1 = env_->NewLocalRef(outer);
1333 inner2 = env_->NewStringUTF("survivor");
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001334 inner2_direct_pointer = soa.Decode<mirror::Object*>(inner2);
Elliott Hughes2ced6a52011-10-16 18:44:48 -07001335 env_->PopLocalFrame(inner2);
1336 }
1337
1338 EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(original));
1339 EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(outer));
1340 EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(inner1));
1341
1342 // Our local reference for the survivor is invalid because the survivor
1343 // gets a new local reference...
1344 EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(inner2));
Elliott Hughes2ced6a52011-10-16 18:44:48 -07001345
Ian Rogers2d10b202014-05-12 19:15:18 -07001346 env_->PopLocalFrame(nullptr);
Elliott Hughes2ced6a52011-10-16 18:44:48 -07001347 }
1348 EXPECT_EQ(JNILocalRefType, env_->GetObjectRefType(original));
1349 EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(outer));
1350 EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(inner1));
1351 EXPECT_EQ(JNIInvalidRefType, env_->GetObjectRefType(inner2));
1352}
1353
Ian Rogers2d10b202014-05-12 19:15:18 -07001354TEST_F(JniInternalTest, NewGlobalRef_nullptr) {
1355 EXPECT_EQ(env_->NewGlobalRef(nullptr), nullptr);
Elliott Hughes18c07532011-08-18 15:50:51 -07001356}
1357
1358TEST_F(JniInternalTest, NewGlobalRef) {
1359 jstring s = env_->NewStringUTF("");
Ian Rogers2d10b202014-05-12 19:15:18 -07001360 ASSERT_NE(s, nullptr);
Elliott Hughes18c07532011-08-18 15:50:51 -07001361 jobject o = env_->NewGlobalRef(s);
Ian Rogers2d10b202014-05-12 19:15:18 -07001362 EXPECT_NE(o, nullptr);
1363 EXPECT_NE(o, s);
Elliott Hughes18c07532011-08-18 15:50:51 -07001364
Ian Rogers2d10b202014-05-12 19:15:18 -07001365 EXPECT_EQ(env_->GetObjectRefType(o), JNIGlobalRefType);
Elliott Hughes18c07532011-08-18 15:50:51 -07001366}
1367
Ian Rogers2d10b202014-05-12 19:15:18 -07001368TEST_F(JniInternalTest, DeleteGlobalRef_nullptr) {
1369 env_->DeleteGlobalRef(nullptr);
Elliott Hughes18c07532011-08-18 15:50:51 -07001370}
1371
1372TEST_F(JniInternalTest, DeleteGlobalRef) {
1373 jstring s = env_->NewStringUTF("");
Ian Rogers2d10b202014-05-12 19:15:18 -07001374 ASSERT_NE(s, nullptr);
Elliott Hughes18c07532011-08-18 15:50:51 -07001375
1376 jobject o = env_->NewGlobalRef(s);
Ian Rogers2d10b202014-05-12 19:15:18 -07001377 ASSERT_NE(o, nullptr);
Elliott Hughes18c07532011-08-18 15:50:51 -07001378 env_->DeleteGlobalRef(o);
1379
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001380 // Currently, deleting an already-deleted reference is just a CheckJNI warning.
Elliott Hughesb264f082012-04-06 17:10:10 -07001381 {
Elliott Hughesb264f082012-04-06 17:10:10 -07001382 CheckJniAbortCatcher check_jni_abort_catcher;
1383 env_->DeleteGlobalRef(o);
Elliott Hughesa9137c62013-01-09 10:55:21 -08001384
Brian Carlstromea46f952013-07-30 01:26:50 -07001385 std::string expected(StringPrintf("native code passing in reference to "
1386 "invalid global reference: %p", o));
Elliott Hughesa9137c62013-01-09 10:55:21 -08001387 check_jni_abort_catcher.Check(expected.c_str());
Elliott Hughesb264f082012-04-06 17:10:10 -07001388 }
Elliott Hughes18c07532011-08-18 15:50:51 -07001389
1390 jobject o1 = env_->NewGlobalRef(s);
Ian Rogers2d10b202014-05-12 19:15:18 -07001391 ASSERT_NE(o1, nullptr);
Elliott Hughes18c07532011-08-18 15:50:51 -07001392 jobject o2 = env_->NewGlobalRef(s);
Ian Rogers2d10b202014-05-12 19:15:18 -07001393 ASSERT_NE(o2, nullptr);
Elliott Hughes18c07532011-08-18 15:50:51 -07001394
1395 env_->DeleteGlobalRef(o1);
1396 env_->DeleteGlobalRef(o2);
1397}
1398
Ian Rogers2d10b202014-05-12 19:15:18 -07001399TEST_F(JniInternalTest, NewWeakGlobalRef_nullptr) {
1400 EXPECT_EQ(env_->NewWeakGlobalRef(nullptr), nullptr);
Elliott Hughes18c07532011-08-18 15:50:51 -07001401}
1402
1403TEST_F(JniInternalTest, NewWeakGlobalRef) {
1404 jstring s = env_->NewStringUTF("");
Ian Rogers2d10b202014-05-12 19:15:18 -07001405 ASSERT_NE(s, nullptr);
Elliott Hughes18c07532011-08-18 15:50:51 -07001406 jobject o = env_->NewWeakGlobalRef(s);
Ian Rogers2d10b202014-05-12 19:15:18 -07001407 EXPECT_NE(o, nullptr);
1408 EXPECT_NE(o, s);
Elliott Hughes18c07532011-08-18 15:50:51 -07001409
Ian Rogers2d10b202014-05-12 19:15:18 -07001410 EXPECT_EQ(env_->GetObjectRefType(o), JNIWeakGlobalRefType);
Elliott Hughes18c07532011-08-18 15:50:51 -07001411}
1412
Ian Rogers2d10b202014-05-12 19:15:18 -07001413TEST_F(JniInternalTest, DeleteWeakGlobalRef_nullptr) {
1414 env_->DeleteWeakGlobalRef(nullptr);
Elliott Hughes18c07532011-08-18 15:50:51 -07001415}
1416
1417TEST_F(JniInternalTest, DeleteWeakGlobalRef) {
1418 jstring s = env_->NewStringUTF("");
Ian Rogers2d10b202014-05-12 19:15:18 -07001419 ASSERT_NE(s, nullptr);
Elliott Hughes18c07532011-08-18 15:50:51 -07001420
1421 jobject o = env_->NewWeakGlobalRef(s);
Ian Rogers2d10b202014-05-12 19:15:18 -07001422 ASSERT_NE(o, nullptr);
Elliott Hughes18c07532011-08-18 15:50:51 -07001423 env_->DeleteWeakGlobalRef(o);
1424
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001425 // Currently, deleting an already-deleted reference is just a CheckJNI warning.
Elliott Hughesb264f082012-04-06 17:10:10 -07001426 {
Elliott Hughesb264f082012-04-06 17:10:10 -07001427 CheckJniAbortCatcher check_jni_abort_catcher;
1428 env_->DeleteWeakGlobalRef(o);
Elliott Hughesa9137c62013-01-09 10:55:21 -08001429
Brian Carlstromea46f952013-07-30 01:26:50 -07001430 std::string expected(StringPrintf("native code passing in reference to "
1431 "invalid weak global reference: %p", o));
Elliott Hughesa9137c62013-01-09 10:55:21 -08001432 check_jni_abort_catcher.Check(expected.c_str());
Elliott Hughesb264f082012-04-06 17:10:10 -07001433 }
Elliott Hughes18c07532011-08-18 15:50:51 -07001434
1435 jobject o1 = env_->NewWeakGlobalRef(s);
Ian Rogers2d10b202014-05-12 19:15:18 -07001436 ASSERT_NE(o1, nullptr);
Elliott Hughes18c07532011-08-18 15:50:51 -07001437 jobject o2 = env_->NewWeakGlobalRef(s);
Ian Rogers2d10b202014-05-12 19:15:18 -07001438 ASSERT_NE(o2, nullptr);
Elliott Hughes18c07532011-08-18 15:50:51 -07001439
1440 env_->DeleteWeakGlobalRef(o1);
1441 env_->DeleteWeakGlobalRef(o2);
1442}
1443
Elliott Hughes37f7a402011-08-22 18:56:01 -07001444TEST_F(JniInternalTest, Throw) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001445 EXPECT_EQ(JNI_ERR, env_->Throw(nullptr));
Elliott Hughes37f7a402011-08-22 18:56:01 -07001446
1447 jclass exception_class = env_->FindClass("java/lang/RuntimeException");
Ian Rogers2d10b202014-05-12 19:15:18 -07001448 ASSERT_TRUE(exception_class != nullptr);
Elliott Hughes37f7a402011-08-22 18:56:01 -07001449 jthrowable exception = reinterpret_cast<jthrowable>(env_->AllocObject(exception_class));
Ian Rogers2d10b202014-05-12 19:15:18 -07001450 ASSERT_TRUE(exception != nullptr);
Elliott Hughes37f7a402011-08-22 18:56:01 -07001451
1452 EXPECT_EQ(JNI_OK, env_->Throw(exception));
1453 EXPECT_TRUE(env_->ExceptionCheck());
Elliott Hughesa2501992011-08-26 19:39:54 -07001454 jthrowable thrown_exception = env_->ExceptionOccurred();
Elliott Hughes37f7a402011-08-22 18:56:01 -07001455 env_->ExceptionClear();
Elliott Hughesa2501992011-08-26 19:39:54 -07001456 EXPECT_TRUE(env_->IsSameObject(exception, thrown_exception));
Elliott Hughes37f7a402011-08-22 18:56:01 -07001457}
1458
1459TEST_F(JniInternalTest, ThrowNew) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001460 EXPECT_EQ(JNI_ERR, env_->Throw(nullptr));
Elliott Hughes37f7a402011-08-22 18:56:01 -07001461
1462 jclass exception_class = env_->FindClass("java/lang/RuntimeException");
Ian Rogers2d10b202014-05-12 19:15:18 -07001463 ASSERT_TRUE(exception_class != nullptr);
Elliott Hughes37f7a402011-08-22 18:56:01 -07001464
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001465 jthrowable thrown_exception;
1466
Elliott Hughes37f7a402011-08-22 18:56:01 -07001467 EXPECT_EQ(JNI_OK, env_->ThrowNew(exception_class, "hello world"));
1468 EXPECT_TRUE(env_->ExceptionCheck());
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001469 thrown_exception = env_->ExceptionOccurred();
1470 env_->ExceptionClear();
1471 EXPECT_TRUE(env_->IsInstanceOf(thrown_exception, exception_class));
1472
Ian Rogers2d10b202014-05-12 19:15:18 -07001473 EXPECT_EQ(JNI_OK, env_->ThrowNew(exception_class, nullptr));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001474 EXPECT_TRUE(env_->ExceptionCheck());
1475 thrown_exception = env_->ExceptionOccurred();
Elliott Hughes37f7a402011-08-22 18:56:01 -07001476 env_->ExceptionClear();
Elliott Hughesa2501992011-08-26 19:39:54 -07001477 EXPECT_TRUE(env_->IsInstanceOf(thrown_exception, exception_class));
Elliott Hughes37f7a402011-08-22 18:56:01 -07001478}
1479
Ian Rogers1d99e452014-01-02 17:36:41 -08001480TEST_F(JniInternalTest, NewDirectBuffer_GetDirectBufferAddress_GetDirectBufferCapacity) {
1481 // Start runtime.
1482 Thread* self = Thread::Current();
1483 self->TransitionFromSuspendedToRunnable();
1484 MakeExecutable(nullptr, "java.lang.Class");
1485 MakeExecutable(nullptr, "java.lang.Object");
1486 MakeExecutable(nullptr, "java.nio.DirectByteBuffer");
1487 MakeExecutable(nullptr, "java.nio.MemoryBlock");
1488 MakeExecutable(nullptr, "java.nio.MemoryBlock$UnmanagedBlock");
1489 MakeExecutable(nullptr, "java.nio.MappedByteBuffer");
1490 MakeExecutable(nullptr, "java.nio.ByteBuffer");
1491 MakeExecutable(nullptr, "java.nio.Buffer");
1492 // TODO: we only load a dex file here as starting the runtime relies upon it.
1493 const char* class_name = "StaticLeafMethods";
1494 LoadDex(class_name);
1495 bool started = runtime_->Start();
1496 ASSERT_TRUE(started);
1497
Elliott Hughesb465ab02011-08-24 11:21:21 -07001498 jclass buffer_class = env_->FindClass("java/nio/Buffer");
Ian Rogers2d10b202014-05-12 19:15:18 -07001499 ASSERT_NE(buffer_class, nullptr);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001500
1501 char bytes[1024];
1502 jobject buffer = env_->NewDirectByteBuffer(bytes, sizeof(bytes));
Ian Rogers2d10b202014-05-12 19:15:18 -07001503 ASSERT_NE(buffer, nullptr);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001504 ASSERT_TRUE(env_->IsInstanceOf(buffer, buffer_class));
Ian Rogers2d10b202014-05-12 19:15:18 -07001505 ASSERT_EQ(env_->GetDirectBufferAddress(buffer), bytes);
Ian Rogers1ee99352014-05-14 14:38:16 -07001506 ASSERT_EQ(env_->GetDirectBufferCapacity(buffer), static_cast<jlong>(sizeof(bytes)));
Elliott Hughesb465ab02011-08-24 11:21:21 -07001507}
1508
Ian Rogers6d0b13e2012-02-07 09:25:29 -08001509TEST_F(JniInternalTest, MonitorEnterExit) {
Ian Rogers2d10b202014-05-12 19:15:18 -07001510 // Create an object to torture.
Ian Rogers6d0b13e2012-02-07 09:25:29 -08001511 jclass object_class = env_->FindClass("java/lang/Object");
Ian Rogers2d10b202014-05-12 19:15:18 -07001512 ASSERT_NE(object_class, nullptr);
Ian Rogers6d0b13e2012-02-07 09:25:29 -08001513 jobject object = env_->AllocObject(object_class);
Ian Rogers2d10b202014-05-12 19:15:18 -07001514 ASSERT_NE(object, nullptr);
Ian Rogers6d0b13e2012-02-07 09:25:29 -08001515
1516 // Expected class of exceptions
1517 jclass imse_class = env_->FindClass("java/lang/IllegalMonitorStateException");
Ian Rogers2d10b202014-05-12 19:15:18 -07001518 ASSERT_NE(imse_class, nullptr);
Ian Rogers6d0b13e2012-02-07 09:25:29 -08001519
1520 jthrowable thrown_exception;
1521
1522 // Unlock of unowned monitor
1523 env_->MonitorExit(object);
1524 EXPECT_TRUE(env_->ExceptionCheck());
1525 thrown_exception = env_->ExceptionOccurred();
1526 env_->ExceptionClear();
1527 EXPECT_TRUE(env_->IsInstanceOf(thrown_exception, imse_class));
1528
1529 // Lock of unowned monitor
1530 env_->MonitorEnter(object);
1531 EXPECT_FALSE(env_->ExceptionCheck());
1532 // Regular unlock
1533 env_->MonitorExit(object);
1534 EXPECT_FALSE(env_->ExceptionCheck());
1535
1536 // Recursively lock a lot
1537 size_t max_recursive_lock = 1024;
1538 for (size_t i = 0; i < max_recursive_lock; i++) {
1539 env_->MonitorEnter(object);
1540 EXPECT_FALSE(env_->ExceptionCheck());
1541 }
1542 // Recursively unlock a lot
1543 for (size_t i = 0; i < max_recursive_lock; i++) {
1544 env_->MonitorExit(object);
1545 EXPECT_FALSE(env_->ExceptionCheck());
1546 }
1547
1548 // Unlock of unowned monitor
1549 env_->MonitorExit(object);
1550 EXPECT_TRUE(env_->ExceptionCheck());
1551 thrown_exception = env_->ExceptionOccurred();
1552 env_->ExceptionClear();
1553 EXPECT_TRUE(env_->IsInstanceOf(thrown_exception, imse_class));
Elliott Hughesa92853e2012-02-07 16:09:27 -08001554
Ian Rogers2d10b202014-05-12 19:15:18 -07001555 // It's an error to call MonitorEnter or MonitorExit on nullptr.
Elliott Hughesb264f082012-04-06 17:10:10 -07001556 {
1557 CheckJniAbortCatcher check_jni_abort_catcher;
Ian Rogers2d10b202014-05-12 19:15:18 -07001558 env_->MonitorEnter(nullptr);
Elliott Hughesb264f082012-04-06 17:10:10 -07001559 check_jni_abort_catcher.Check("in call to MonitorEnter");
Elliott Hughesb264f082012-04-06 17:10:10 -07001560
Ian Rogers2d10b202014-05-12 19:15:18 -07001561 env_->MonitorExit(nullptr);
Elliott Hughesb264f082012-04-06 17:10:10 -07001562 check_jni_abort_catcher.Check("in call to MonitorExit");
1563 }
Ian Rogers6d0b13e2012-02-07 09:25:29 -08001564}
1565
Brian Carlstrom4d571432012-05-16 00:21:41 -07001566TEST_F(JniInternalTest, DetachCurrentThread) {
1567 CleanUpJniEnv(); // cleanup now so TearDown won't have junk from wrong JNIEnv
1568 jint ok = vm_->DetachCurrentThread();
1569 EXPECT_EQ(JNI_OK, ok);
1570
1571 jint err = vm_->DetachCurrentThread();
1572 EXPECT_EQ(JNI_ERR, err);
Ian Rogers2d10b202014-05-12 19:15:18 -07001573 vm_->AttachCurrentThread(&env_, nullptr); // need attached thread for CommonRuntimeTest::TearDown
Brian Carlstrom4d571432012-05-16 00:21:41 -07001574}
1575
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001576} // namespace art