1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <gtest/gtest.h>
17 #include "cj_environment.h"
18 #include "dynamic_loader.h"
19
20 #include <string>
21
22 #include "cj_invoker.h"
23 #ifdef __OHOS__
24 #include <dlfcn.h>
25 #endif
26 #include "dynamic_loader.h"
27 #ifdef WITH_EVENT_HANDLER
28 #include "event_handler.h"
29 #endif
30
31 using namespace OHOS;
32 using namespace testing;
33 using namespace testing::ext;
34
35
36 class CjEnvironmentTest : public testing::Test {
37 public:
CjEnvironmentTest()38 CjEnvironmentTest()
39 {}
~CjEnvironmentTest()40 ~CjEnvironmentTest()
41 {}
42 static void SetUpTestCase(void);
43 static void TearDownTestCase(void);
44 void SetUp() override;
45 void TearDown() override;
46 };
47
SetUpTestCase(void)48 void CjEnvironmentTest::SetUpTestCase(void)
49 {}
50
TearDownTestCase(void)51 void CjEnvironmentTest::TearDownTestCase(void)
52 {}
53
SetUp(void)54 void CjEnvironmentTest::SetUp(void)
55 {}
56
TearDown(void)57 void CjEnvironmentTest::TearDown(void)
58 {}
59
TestFunc()60 void TestFunc()
61 {}
62
RegisterCJUncaughtExceptionHandlerTest(const CJUncaughtExceptionInfo & handle)63 void RegisterCJUncaughtExceptionHandlerTest(const CJUncaughtExceptionInfo &handle)
64 {}
65
66 /**
67 * @tc.name: CjEnvironmentTestPostTask_001
68 * @tc.desc: CjEnvironmentTest test for PostTask.
69 * @tc.type: FUNC
70 */
71 HWTEST_F(CjEnvironmentTest, CjEnvironmentTestPostTask_001, TestSize.Level0)
72 {
73 CJEnvironment::GetInstance()->PostTask(nullptr);
74 void (*func)() = TestFunc;
75 auto ret = CJEnvironment::GetInstance()->PostTask(func);
76 EXPECT_EQ(ret, true);
77 }
78
79 /**
80 * @tc.name: CjEnvironmentTestHasHigherPriorityTask_001
81 * @tc.desc: CjEnvironmentTest test for HasHigherPriorityTask.
82 * @tc.type: FUNC
83 */
84 HWTEST_F(CjEnvironmentTest, CjEnvironmentTestHasHigherPriorityTask_001, TestSize.Level0)
85 {
86 auto ret = CJEnvironment::GetInstance()->HasHigherPriorityTask();
87 EXPECT_EQ(ret, false);
88 }
89
90 /**
91 * @tc.name: CjEnvironmentTestInitCJChipSDKNS_001
92 * @tc.desc: CjEnvironmentTest test for InitCJChipSDKNS.
93 * @tc.type: FUNC
94 */
95 HWTEST_F(CjEnvironmentTest, CjEnvironmentTestInitCJChipSDKNS_001, TestSize.Level0)
96 {
97 CJEnvironment::GetInstance()->InitCJChipSDKNS("path/to/hap");
98 EXPECT_NE(CJEnvironment::GetInstance()->cjAppNSName, nullptr);
99 }
100
101 /**
102 * @tc.name: CjEnvironmentTestInitCJAppNS_001
103 * @tc.desc: CjEnvironmentTest test for InitCJAppNS.
104 * @tc.type: FUNC
105 */
106 HWTEST_F(CjEnvironmentTest, CjEnvironmentTestInitCJAppNS_001, TestSize.Level0)
107 {
108 CJEnvironment::GetInstance()->InitCJAppNS("path/to/hap");
109 EXPECT_NE(CJEnvironment::GetInstance()->cjAppNSName, nullptr);
110 }
111
112 /**
113 * @tc.name: CjEnvironmentTestInitCJSDKNS_001
114 * @tc.desc: CjEnvironmentTest test for InitCJSDKNS.
115 * @tc.type: FUNC
116 */
117 HWTEST_F(CjEnvironmentTest, CjEnvironmentTestInitCJSDKNS_001, TestSize.Level0)
118 {
119 CJEnvironment::GetInstance()->InitCJSDKNS("path/to/hap");
120 EXPECT_NE(CJEnvironment::GetInstance()->cjAppNSName, nullptr);
121 }
122
123 /**
124 * @tc.name: CjEnvironmentTestInitCJSysNS_001
125 * @tc.desc: CjEnvironmentTest test for InitCJSysNS.
126 * @tc.type: FUNC
127 */
128 HWTEST_F(CjEnvironmentTest, CjEnvironmentTestInitCJSysNS_001, TestSize.Level0)
129 {
130 CJEnvironment::GetInstance()->InitCJSysNS("path/to/hap");
131 EXPECT_NE(CJEnvironment::GetInstance()->cjAppNSName, nullptr);
132 }
133
134 /**
135 * @tc.name: CjEnvironmentTestStartRuntime_001
136 * @tc.desc: CjEnvironmentTest test for StartRuntime.
137 * @tc.type: FUNC
138 */
139 HWTEST_F(CjEnvironmentTest, CjEnvironmentTestStartRuntime_001, TestSize.Level0)
140 {
141 CJEnvironment cJEnvironment(CJEnvironment::NSMode::APP);
142 CJUncaughtExceptionInfo handle;
143 handle.hapPath = "/test1/";
__anon5dc7c2ea0102(const char* summary, const CJErrorObject errorObj) 144 handle.uncaughtTask = [](const char* summary, const CJErrorObject errorObj) {};
145
146 CJRuntimeAPI api {
147 .InitCJRuntime = nullptr,
148 .InitUIScheduler = nullptr,
149 .RunUIScheduler = nullptr,
150 .FiniCJRuntime = nullptr,
151 .InitCJLibrary = nullptr,
152 .RegisterEventHandlerCallbacks = nullptr,
153 .RegisterCJUncaughtExceptionHandler = RegisterCJUncaughtExceptionHandlerTest,
154 };
155
156 CJRuntimeAPI* lazyApi = new CJRuntimeAPI(api);
157 cJEnvironment.SetLazyApis(lazyApi);
158 cJEnvironment.RegisterCJUncaughtExceptionHandler(handle);
159 auto ret = cJEnvironment.StartRuntime();
160 EXPECT_EQ(ret, false);
161 }
162
163 /**
164 * @tc.name: CjEnvironmentTestStopRuntime_001
165 * @tc.desc: CjEnvironmentTest test for StopRuntime.
166 * @tc.type: FUNC
167 */
168 HWTEST_F(CjEnvironmentTest, CjEnvironmentTestStopRuntime_001, TestSize.Level0)
169 {
170 CJEnvironment cJEnvironment(CJEnvironment::NSMode::APP);
171 CJUncaughtExceptionInfo handle;
172 handle.hapPath = "/test1/";
__anon5dc7c2ea0202(const char* summary, const CJErrorObject errorObj) 173 handle.uncaughtTask = [](const char* summary, const CJErrorObject errorObj) {};
174
175 CJRuntimeAPI api {
176 .InitCJRuntime = nullptr,
177 .InitUIScheduler = nullptr,
178 .RunUIScheduler = nullptr,
179 .FiniCJRuntime = nullptr,
180 .InitCJLibrary = nullptr,
181 .RegisterEventHandlerCallbacks = nullptr,
182 .RegisterCJUncaughtExceptionHandler = RegisterCJUncaughtExceptionHandlerTest,
183 };
184
185 CJRuntimeAPI* lazyApi = new CJRuntimeAPI(api);
186 cJEnvironment.SetLazyApis(lazyApi);
187 cJEnvironment.RegisterCJUncaughtExceptionHandler(handle);
188 cJEnvironment.StopRuntime();
189 EXPECT_EQ(CJEnvironment::GetInstance(), nullptr);
190 }
191
192 /**
193 * @tc.name: CjEnvironmentTestStopUIScheduler_001
194 * @tc.desc: CjEnvironmentTest test for StopUIScheduler.
195 * @tc.type: FUNC
196 */
197 HWTEST_F(CjEnvironmentTest, CjEnvironmentTestStopUIScheduler_001, TestSize.Level0)
198 {
199 CJEnvironment cJEnvironment(CJEnvironment::NSMode::APP);
200 CJUncaughtExceptionInfo handle;
201 handle.hapPath = "/test1/";
__anon5dc7c2ea0302(const char* summary, const CJErrorObject errorObj) 202 handle.uncaughtTask = [](const char* summary, const CJErrorObject errorObj) {};
203
204 CJRuntimeAPI api {
205 .InitCJRuntime = nullptr,
206 .InitUIScheduler = nullptr,
207 .RunUIScheduler = nullptr,
208 .FiniCJRuntime = nullptr,
209 .InitCJLibrary = nullptr,
210 .RegisterEventHandlerCallbacks = nullptr,
211 .RegisterCJUncaughtExceptionHandler = RegisterCJUncaughtExceptionHandlerTest,
212 };
213
214 CJRuntimeAPI* lazyApi = new CJRuntimeAPI(api);
215 cJEnvironment.SetLazyApis(lazyApi);
216 cJEnvironment.RegisterCJUncaughtExceptionHandler(handle);
217 cJEnvironment.StopUIScheduler();
218 EXPECT_EQ(CJEnvironment::GetInstance(), nullptr);
219 }
220
221 /**
222 * @tc.name: CjEnvironmentTestLoadCJLibrary_001
223 * @tc.desc: CjEnvironmentTest test for LoadCJLibrary.
224 * @tc.type: FUNC
225 */
226 HWTEST_F(CjEnvironmentTest, CjEnvironmentTestLoadCJLibrary_001, TestSize.Level0)
227 {
228 CJEnvironment cJEnvironment(CJEnvironment::NSMode::APP);
229 auto ret = cJEnvironment.LoadCJLibrary("dlName");
230 EXPECT_EQ(ret, nullptr);
231 }
232
233 /**
234 * @tc.name: CjEnvironmentTestLoadCJLibrary_001
235 * @tc.desc: CjEnvironmentTest test for LoadCJLibrary.
236 * @tc.type: FUNC
237 */
238 HWTEST_F(CjEnvironmentTest, CjEnvironmentTestLoadCJLibrary_002, TestSize.Level0)
239 {
240 CJEnvironment cJEnvironment(CJEnvironment::NSMode::APP);
241 cJEnvironment.LoadCJLibrary(CJEnvironment::GetInstance()->LibraryKind::APP, "dlName");
242 cJEnvironment.LoadCJLibrary(CJEnvironment::GetInstance()->LibraryKind::SYSTEM, "dlName");
243 cJEnvironment.LoadCJLibrary(CJEnvironment::GetInstance()->LibraryKind::SDK, "dlName");
244 EXPECT_EQ(CJEnvironment::GetInstance(), nullptr);
245 }
246
247 /**
248 * @tc.name: CjEnvironmentTestStartDebugger_001
249 * @tc.desc: CjEnvironmentTest test for StartDebugger.
250 * @tc.type: FUNC
251 */
252 HWTEST_F(CjEnvironmentTest, CjEnvironmentTestStartDebugger_001, TestSize.Level0)
253 {
254 CJEnvironment cJEnvironment(CJEnvironment::NSMode::APP);
255 auto ret = cJEnvironment.StartDebugger();
256 EXPECT_EQ(ret, false);
257 }
258
259 /**
260 * @tc.name: CjEnvironmentTestGetSymbol_001
261 * @tc.desc: CjEnvironmentTest test for GetSymbol.
262 * @tc.type: FUNC
263 */
264 HWTEST_F(CjEnvironmentTest, CjEnvironmentTestGetSymbol_001, TestSize.Level0)
265 {
266 CJEnvironment cJEnvironment(CJEnvironment::NSMode::APP);
267 auto ret = cJEnvironment.GetSymbol(nullptr, "dlName");
268 EXPECT_EQ(ret, nullptr);
269 }
270