blob: 60c6a5c23adda7d11503b6a51335a14e95f28fa6 [file] [log] [blame]
Ian Rogersf4d4da12014-11-11 16:10:33 -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 */
16
17#include "jni_internal.h"
18
19#include <pthread.h>
20
21#include "common_runtime_test.h"
22#include "java_vm_ext.h"
23#include "runtime.h"
24
25namespace art {
26
27class JavaVmExtTest : public CommonRuntimeTest {
28 protected:
29 virtual void SetUp() {
30 CommonRuntimeTest::SetUp();
31
32 vm_ = Runtime::Current()->GetJavaVM();
33 }
34
35
36 virtual void TearDown() OVERRIDE {
37 CommonRuntimeTest::TearDown();
38 }
39
40 JavaVMExt* vm_;
41};
42
43TEST_F(JavaVmExtTest, JNI_GetDefaultJavaVMInitArgs) {
44 jint err = JNI_GetDefaultJavaVMInitArgs(nullptr);
45 EXPECT_EQ(JNI_ERR, err);
46}
47
48TEST_F(JavaVmExtTest, JNI_GetCreatedJavaVMs) {
49 JavaVM* vms_buf[1];
50 jsize num_vms;
51 jint ok = JNI_GetCreatedJavaVMs(vms_buf, arraysize(vms_buf), &num_vms);
52 EXPECT_EQ(JNI_OK, ok);
53 EXPECT_EQ(1, num_vms);
54 EXPECT_EQ(vms_buf[0], vm_);
55}
56
57static bool gSmallStack = false;
58static bool gAsDaemon = false;
59
60static void* attach_current_thread_callback(void* arg ATTRIBUTE_UNUSED) {
61 JavaVM* vms_buf[1];
62 jsize num_vms;
63 JNIEnv* env;
64 jint ok = JNI_GetCreatedJavaVMs(vms_buf, arraysize(vms_buf), &num_vms);
65 EXPECT_EQ(JNI_OK, ok);
66 if (ok == JNI_OK) {
67 if (!gAsDaemon) {
68 ok = vms_buf[0]->AttachCurrentThread(&env, nullptr);
69 } else {
70 ok = vms_buf[0]->AttachCurrentThreadAsDaemon(&env, nullptr);
71 }
72 EXPECT_EQ(gSmallStack ? JNI_ERR : JNI_OK, ok);
73 if (ok == JNI_OK) {
74 ok = vms_buf[0]->DetachCurrentThread();
75 EXPECT_EQ(JNI_OK, ok);
76 }
77 }
78 return nullptr;
79}
80
81TEST_F(JavaVmExtTest, AttachCurrentThread) {
82 pthread_t pthread;
83 const char* reason = __PRETTY_FUNCTION__;
84 gSmallStack = false;
85 gAsDaemon = false;
86 CHECK_PTHREAD_CALL(pthread_create, (&pthread, nullptr, attach_current_thread_callback,
87 nullptr), reason);
88 void* ret_val;
89 CHECK_PTHREAD_CALL(pthread_join, (pthread, &ret_val), reason);
90 EXPECT_EQ(ret_val, nullptr);
91}
92
93TEST_F(JavaVmExtTest, AttachCurrentThreadAsDaemon) {
94 pthread_t pthread;
95 const char* reason = __PRETTY_FUNCTION__;
96 gSmallStack = false;
97 gAsDaemon = true;
98 CHECK_PTHREAD_CALL(pthread_create, (&pthread, nullptr, attach_current_thread_callback,
99 nullptr), reason);
100 void* ret_val;
101 CHECK_PTHREAD_CALL(pthread_join, (pthread, &ret_val), reason);
102 EXPECT_EQ(ret_val, nullptr);
103}
104
105TEST_F(JavaVmExtTest, AttachCurrentThread_SmallStack) {
106 pthread_t pthread;
107 pthread_attr_t attr;
108 const char* reason = __PRETTY_FUNCTION__;
109 gSmallStack = true;
110 gAsDaemon = false;
111 CHECK_PTHREAD_CALL(pthread_attr_init, (&attr), reason);
112 CHECK_PTHREAD_CALL(pthread_attr_setstacksize, (&attr, PTHREAD_STACK_MIN), reason);
113 CHECK_PTHREAD_CALL(pthread_create, (&pthread, &attr, attach_current_thread_callback,
114 nullptr), reason);
115 CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attr), reason);
116 void* ret_val;
117 CHECK_PTHREAD_CALL(pthread_join, (pthread, &ret_val), reason);
118 EXPECT_EQ(ret_val, nullptr);
119}
120
121TEST_F(JavaVmExtTest, DetachCurrentThread) {
122 JNIEnv* env;
123 jint ok = vm_->AttachCurrentThread(&env, nullptr);
124 ASSERT_EQ(JNI_OK, ok);
125 ok = vm_->DetachCurrentThread();
126 EXPECT_EQ(JNI_OK, ok);
127
128 jint err = vm_->DetachCurrentThread();
129 EXPECT_EQ(JNI_ERR, err);
130}
131
132} // namespace art