• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "assembler/assembly-emitter.h"
17 #include "assembler/assembly-parser.h"
18 #include "libpandafile/class_data_accessor-inl.h"
19 #include "libziparchive/zip_archive.h"
20 
21 #include "ecmascript/global_env.h"
22 #include "ecmascript/jspandafile/abc_buffer_cache.h"
23 #include "ecmascript/jspandafile/js_pandafile.h"
24 #include "ecmascript/jspandafile/js_pandafile_manager.h"
25 #include "ecmascript/jspandafile/program_object.h"
26 #include "ecmascript/tests/test_helper.h"
27 
28 using namespace panda::ecmascript;
29 using namespace panda::panda_file;
30 using namespace panda::pandasm;
31 
32 namespace panda::test {
33 class JSPandaFileManagerTest : public testing::Test {
34 public:
SetUpTestCase()35     static void SetUpTestCase()
36     {
37         GTEST_LOG_(INFO) << "SetUpTestCase";
38     }
39 
TearDownTestCase()40     static void TearDownTestCase()
41     {
42         GTEST_LOG_(INFO) << "TearDownCase";
43     }
44 
SetUp()45     void SetUp() override
46     {
47         TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
48     }
49 
TearDown()50     void TearDown() override
51     {
52         TestHelper::DestroyEcmaVMWithScope(instance, scope);
53     }
54 
FakeReleaseSecureMemCallback(void * mapper)55     static void FakeReleaseSecureMemCallback(void* mapper)
56     {
57         if (mapper != nullptr) {
58             *reinterpret_cast<uint8_t *>(mapper) = 10; // 10: random number
59         }
60     }
61 
62     EcmaVM *instance {nullptr};
63     EcmaHandleScope *scope {nullptr};
64     JSThread *thread {nullptr};
65 };
66 
HWTEST_F_L0(JSPandaFileManagerTest,GetInstance)67 HWTEST_F_L0(JSPandaFileManagerTest, GetInstance)
68 {
69     JSPandaFileManager *manager = JSPandaFileManager::GetInstance();
70     EXPECT_TRUE(manager != nullptr);
71 }
72 
HWTEST_F_L0(JSPandaFileManagerTest,NewJSPandaFile)73 HWTEST_F_L0(JSPandaFileManagerTest, NewJSPandaFile)
74 {
75     Parser parser;
76     std::string fileName = "__JSPandaFileManagerTest.pa";
77     const char *source = R"(
78         .function void foo() {}
79     )";
80     auto res = parser.Parse(source, fileName);
81     EXPECT_EQ(parser.ShowError().err, Error::ErrorType::ERR_NONE);
82 
83     std::unique_ptr<const File> pfPtr = pandasm::AsmEmitter::Emit(res.Value());
84     JSPandaFileManager *pfManager = JSPandaFileManager::GetInstance();
85     std::shared_ptr<JSPandaFile> pf = pfManager->NewJSPandaFile(pfPtr.release(), CString(fileName.c_str()));
86     EXPECT_TRUE(pf != nullptr);
87 
88     auto expectFileName = pf->GetJSPandaFileDesc();
89     EXPECT_STREQ(expectFileName.c_str(), "__JSPandaFileManagerTest.pa");
90     remove(fileName.c_str());
91     pfManager->RemoveJSPandaFile(pf.get());
92 }
93 
HWTEST_F_L0(JSPandaFileManagerTest,OpenJSPandaFile)94 HWTEST_F_L0(JSPandaFileManagerTest, OpenJSPandaFile)
95 {
96     const char *filename = "__JSPandaFileManagerTest.pa";
97     const char *data = R"(
98         .function void foo() {}
99     )";
100     JSPandaFileManager *pfManager = JSPandaFileManager::GetInstance();
101     std::shared_ptr<JSPandaFile> ojspf = pfManager->OpenJSPandaFile(filename);
102     EXPECT_TRUE(ojspf == nullptr);
103 
104     Parser parser;
105     auto res = parser.Parse(data);
106     EXPECT_TRUE(pandasm::AsmEmitter::Emit(filename, res.Value()));
107 
108     ojspf = pfManager->OpenJSPandaFile(filename);
109     EXPECT_TRUE(ojspf != nullptr);
110     EXPECT_STREQ(ojspf->GetJSPandaFileDesc().c_str(), "__JSPandaFileManagerTest.pa");
111     pfManager->RemoveJSPandaFile(ojspf.get());
112 
113     remove(filename);
114     ojspf = pfManager->OpenJSPandaFile(filename);
115     EXPECT_TRUE(ojspf == nullptr);
116 }
117 
HWTEST_F_L0(JSPandaFileManagerTest,Add_Find_Remove_JSPandaFile)118 HWTEST_F_L0(JSPandaFileManagerTest, Add_Find_Remove_JSPandaFile)
119 {
120     const char *filename1 = "__JSPandaFileManagerTest1.pa";
121     const char *filename2 = "__JSPandaFileManagerTest2.pa";
122     const char *data = R"(
123         .function void foo() {}
124     )";
125     JSPandaFileManager *pfManager = JSPandaFileManager::GetInstance();
126     Parser parser;
127     auto res = parser.Parse(data);
128     std::unique_ptr<const File> pfPtr1 = pandasm::AsmEmitter::Emit(res.Value());
129     std::unique_ptr<const File> pfPtr2 = pandasm::AsmEmitter::Emit(res.Value());
130     std::shared_ptr<JSPandaFile> pf1 = pfManager->NewJSPandaFile(pfPtr1.release(), CString(filename1));
131     std::shared_ptr<JSPandaFile> pf2 = pfManager->NewJSPandaFile(pfPtr2.release(), CString(filename2));
132     pfManager->AddJSPandaFile(pf1);
133     pfManager->AddJSPandaFile(pf2);
134     std::shared_ptr<JSPandaFile> foundPf1 = pfManager->FindJSPandaFile(filename1);
135     std::shared_ptr<JSPandaFile> foundPf2 = pfManager->FindJSPandaFile(filename2);
136     EXPECT_STREQ(foundPf1->GetJSPandaFileDesc().c_str(), "__JSPandaFileManagerTest1.pa");
137     EXPECT_STREQ(foundPf2->GetJSPandaFileDesc().c_str(), "__JSPandaFileManagerTest2.pa");
138 
139     pfManager->RemoveJSPandaFile(pf1.get());
140     pfManager->RemoveJSPandaFile(pf2.get());
141     std::shared_ptr<JSPandaFile> afterRemovePf1 = pfManager->FindJSPandaFile(filename1);
142     std::shared_ptr<JSPandaFile> afterRemovePf2 = pfManager->FindJSPandaFile(filename2);
143     EXPECT_EQ(afterRemovePf1, nullptr);
144     EXPECT_EQ(afterRemovePf2, nullptr);
145 }
146 
HWTEST_F_L0(JSPandaFileManagerTest,MultiEcmaVM_Add_Find_Remove_JSPandaFile)147 HWTEST_F_L0(JSPandaFileManagerTest, MultiEcmaVM_Add_Find_Remove_JSPandaFile)
148 {
149     const char *filename1 = "__JSPandaFileManagerTest1.pa";
150     const char *filename2 = "__JSPandaFileManagerTest2.pa";
151     const char *data = R"(
152         .function void foo() {}
153     )";
154     JSPandaFileManager *pfManager = JSPandaFileManager::GetInstance();
155     Parser parser;
156     auto res = parser.Parse(data);
157     std::unique_ptr<const File> pfPtr1 = pandasm::AsmEmitter::Emit(res.Value());
158     std::unique_ptr<const File> pfPtr2 = pandasm::AsmEmitter::Emit(res.Value());
159     std::shared_ptr<JSPandaFile> pf1 = pfManager->NewJSPandaFile(pfPtr1.release(), CString(filename1));
160     std::shared_ptr<JSPandaFile> pf2 = pfManager->NewJSPandaFile(pfPtr2.release(), CString(filename2));
161     pfManager->AddJSPandaFile(pf1);
162     pfManager->AddJSPandaFile(pf2);
163 
164     EcmaVM *vm = instance->GetJSThread()->GetEcmaVM();
165     JSHandle<ConstantPool> constpool1 = instance->GetFactory()->NewSConstantPool(1);
166     JSHandle<ConstantPool> constpool2 = instance->GetFactory()->NewSConstantPool(2);
167     constpool1 = vm->AddOrUpdateConstpool(pf1.get(), constpool1, 0);
168     constpool2 = vm->AddOrUpdateConstpool(pf2.get(), constpool2, 0);
169 
170     std::thread t1([&]() {
171         EcmaVM *instance1;
172         EcmaHandleScope *scope1;
173         JSThread *thread1;
174         TestHelper::CreateEcmaVMWithScope(instance1, thread1, scope1);
175         std::shared_ptr<JSPandaFile> loadedPf1 = pfManager->LoadJSPandaFile(
176             thread1, filename1, JSPandaFile::ENTRY_MAIN_FUNCTION, false, ExecuteTypes::STATIC);
177         EXPECT_TRUE(pf1 == loadedPf1);
178         EXPECT_TRUE(instance1->GetJSThread()->GetEcmaVM()->HasCachedConstpool(pf1.get()));
179         TestHelper::DestroyEcmaVMWithScope(instance1, scope1); // Remove 'instance1' when ecmaVM destruct.
180     });
181     {
182         ecmascript::ThreadSuspensionScope suspensionScope(thread);
183         t1.join();
184     }
185 
186     std::shared_ptr<JSPandaFile> foundPf1 = pfManager->FindJSPandaFile(filename1);
187     EXPECT_TRUE(foundPf1 != nullptr);
188 
189     pfManager->RemoveJSPandaFile(pf1.get());
190     pfManager->RemoveJSPandaFile(pf2.get());
191     std::shared_ptr<JSPandaFile> afterRemovePf1 = pfManager->FindJSPandaFile(filename1);
192     std::shared_ptr<JSPandaFile> afterRemovePf2 = pfManager->FindJSPandaFile(filename2);
193     EXPECT_EQ(afterRemovePf1, nullptr);
194     EXPECT_EQ(afterRemovePf2, nullptr);
195 
196     // panda file would be managed by gc, readd to panda file manager after check
197     pfManager->AddJSPandaFile(pf1);
198     pfManager->AddJSPandaFile(pf2);
199 }
200 
CreateJSPandaFileAndConstpool(EcmaVM * vm)201 void CreateJSPandaFileAndConstpool(EcmaVM *vm)
202 {
203     const char *filename = "__JSPandaFileManagerTest1.pa";
204     const char *data = R"(
205         .function void foo() {}
206     )";
207     JSPandaFileManager *pfManager = JSPandaFileManager::GetInstance();
208     Parser parser;
209     auto res = parser.Parse(data);
210     std::unique_ptr<const File> pfPtr = pandasm::AsmEmitter::Emit(res.Value());
211     std::shared_ptr<JSPandaFile> pf = pfManager->NewJSPandaFile(pfPtr.release(), CString(filename));
212     pfManager->AddJSPandaFile(pf);
213 
214     [[maybe_unused]] EcmaHandleScope handleScope(vm->GetJSThread());
215     JSHandle<ConstantPool> constpool = vm->GetFactory()->NewSConstantPool(1);
216     constpool = vm->AddOrUpdateConstpool(pf.get(), constpool, 0);
217     JSHandle<ConstantPool> newConstpool = vm->GetFactory()->NewConstantPool(1);
218     vm->SetUnsharedConstpool(constpool, newConstpool.GetTaggedValue());
219 }
220 
HWTEST_F_L0(JSPandaFileManagerTest,GC_Add_Find_Remove_JSPandaFile)221 HWTEST_F_L0(JSPandaFileManagerTest, GC_Add_Find_Remove_JSPandaFile)
222 {
223     const char *filename = "__JSPandaFileManagerTest1.pa";
224     JSPandaFileManager *pfManager = JSPandaFileManager::GetInstance();
225 
226     CreateJSPandaFileAndConstpool(instance);
227     // Remove 'instance' and JSPandafile when trigger GC.
228     JSThread *thread = instance->GetJSThread();
229     SharedHeap::GetInstance()->CollectGarbage<TriggerGCType::SHARED_GC, GCReason::ALLOCATION_FAILED>(thread);
230     SharedHeap::GetInstance()->CollectGarbage<TriggerGCType::SHARED_GC, GCReason::ALLOCATION_FAILED>(thread);
231     std::shared_ptr<JSPandaFile> afterRemovePf = pfManager->FindJSPandaFile(filename);
232     EXPECT_EQ(afterRemovePf, nullptr);
233 }
234 
HWTEST_F_L0(JSPandaFileManagerTest,LoadJSPandaFile)235 HWTEST_F_L0(JSPandaFileManagerTest, LoadJSPandaFile)
236 {
237     const char *filename1 = "__JSPandaFileManagerTest1.pa";
238     const char *filename2 = "__JSPandaFileManagerTest2.pa";
239     const char *filename3 = "__JSPandaFileManagerTest3.abc";
240     const char *data = R"(
241         .function void foo() {}
242     )";
243     JSPandaFileManager *pfManager = JSPandaFileManager::GetInstance();
244     Parser parser;
245     auto res = parser.Parse(data);
246     std::unique_ptr<const File> pfPtr1 = pandasm::AsmEmitter::Emit(res.Value());
247     std::unique_ptr<const File> pfPtr2 = pandasm::AsmEmitter::Emit(res.Value());
248     std::unique_ptr<const File> pfPtr3 = pandasm::AsmEmitter::Emit(res.Value());
249     std::shared_ptr<JSPandaFile> pf1 = pfManager->NewJSPandaFile(pfPtr1.release(), CString(filename1));
250     std::shared_ptr<JSPandaFile> pf2 = pfManager->NewJSPandaFile(pfPtr2.release(), CString(filename2));
251     std::shared_ptr<JSPandaFile> pf3 = pfManager->NewJSPandaFile(pfPtr3.release(), CString(filename3));
252     pfManager->AddJSPandaFile(pf1);
253     pfManager->AddJSPandaFile(pf2);
254     pfManager->AddJSPandaFile(pf3);
255     std::shared_ptr<JSPandaFile> loadedPf1 =
256         pfManager->LoadJSPandaFile(thread, filename1, JSPandaFile::ENTRY_MAIN_FUNCTION);
257     std::shared_ptr<JSPandaFile> loadedPf2 =
258         pfManager->LoadJSPandaFile(thread, filename2, JSPandaFile::ENTRY_MAIN_FUNCTION, (void *)data, sizeof(data));
259     std::shared_ptr<JSPandaFile> loadedPf3 =
260         pfManager->LoadJSPandaFile(thread, filename3, JSPandaFile::ENTRY_MAIN_FUNCTION, (void *)data, sizeof(data));
261     EXPECT_TRUE(loadedPf1 != nullptr);
262     EXPECT_TRUE(loadedPf2 != nullptr);
263     EXPECT_TRUE(loadedPf3 != nullptr);
264     EXPECT_TRUE(pf1 == loadedPf1);
265     EXPECT_TRUE(pf2 == loadedPf2);
266     EXPECT_TRUE(pf3 == loadedPf3);
267     EXPECT_STREQ(loadedPf1->GetJSPandaFileDesc().c_str(), "__JSPandaFileManagerTest1.pa");
268     EXPECT_STREQ(loadedPf2->GetJSPandaFileDesc().c_str(), "__JSPandaFileManagerTest2.pa");
269     EXPECT_STREQ(loadedPf3->GetJSPandaFileDesc().c_str(), "__JSPandaFileManagerTest3.abc");
270 
271     pfManager->RemoveJSPandaFile(pf1.get());
272     pfManager->RemoveJSPandaFile(pf2.get());
273     pfManager->RemoveJSPandaFile(pf3.get());
274 }
275 
HWTEST_F_L0(JSPandaFileManagerTest,GenerateProgram)276 HWTEST_F_L0(JSPandaFileManagerTest, GenerateProgram)
277 {
278     Parser parser;
279     auto vm = thread->GetEcmaVM();
280     const char *filename = "__JSPandaFileManagerTest.pa";
281     const uint8_t *typeDesc = utf::CStringAsMutf8("L_GLOBAL;");
282     const char *data = R"(
283         .function void foo() {}
284     )";
285     auto res = parser.Parse(data);
286     JSPandaFileManager *pfManager = JSPandaFileManager::GetInstance();
287     std::unique_ptr<const File> pfPtr = pandasm::AsmEmitter::Emit(res.Value());
288     std::shared_ptr<JSPandaFile> pf = pfManager->NewJSPandaFile(pfPtr.release(), CString(filename));
289     const File *file = pf->GetPandaFile();
290     File::EntityId classId = file->GetClassId(typeDesc);
291     ClassDataAccessor cda(*file, classId);
292     std::vector<File::EntityId> methodId {};
293     cda.EnumerateMethods([&](panda_file::MethodDataAccessor &mda) {
294         methodId.push_back(mda.GetMethodId());
295     });
296     pf->UpdateMainMethodIndex(methodId[0].GetOffset());
297     MethodLiteral *method = new MethodLiteral(methodId[0]);
298     pf->SetMethodLiteralToMap(method);
299     pfManager->AddJSPandaFile(pf);
300 
301     JSHandle<ecmascript::Program> program = pfManager->GenerateProgram(vm, pf.get(), JSPandaFile::ENTRY_FUNCTION_NAME);
302     JSHandle<JSFunction> mainFunc(thread, program->GetMainFunction(thread));
303     JSHandle<JSTaggedValue> funcName = JSFunction::GetFunctionName(thread, JSHandle<JSFunctionBase>(mainFunc));
304     EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(funcName)).ToCString(thread).c_str(), "foo");
305 }
306 
HWTEST_F_L0(JSPandaFileManagerTest,GetJSPtExtractor)307 HWTEST_F_L0(JSPandaFileManagerTest, GetJSPtExtractor)
308 {
309     const char *filename = "__JSPandaFileManagerTest.pa";
310     const char *data = R"(
311         .function void foo() {}
312     )";
313     Parser parser;
314     auto res = parser.Parse(data);
315     JSPandaFileManager *pfManager = JSPandaFileManager::GetInstance();
316     std::unique_ptr<const File> pfPtr = pandasm::AsmEmitter::Emit(res.Value());
317     std::shared_ptr<JSPandaFile> pf = pfManager->NewJSPandaFile(pfPtr.release(), CString(filename));
318     pfManager->AddJSPandaFile(pf);
319     DebugInfoExtractor *extractor = pfManager->GetJSPtExtractor(pf.get());
320     EXPECT_TRUE(extractor != nullptr);
321 
322     pfManager->RemoveJSPandaFile(pf.get());
323 }
324 
HWTEST_F_L0(JSPandaFileManagerTest,EnumerateJSPandaFiles)325 HWTEST_F_L0(JSPandaFileManagerTest, EnumerateJSPandaFiles)
326 {
327     const char *filename1 = "__JSPandaFileManagerTest3.pa";
328     const char *filename2 = "__JSPandaFileManagerTest4.pa";
329     const char *data = R"(
330         .function void foo() {}
331     )";
332     Parser parser;
333     auto res = parser.Parse(data);
334     JSPandaFileManager *pfManager = JSPandaFileManager::GetInstance();
335     std::unique_ptr<const File> pfPtr1 = pandasm::AsmEmitter::Emit(res.Value());
336     std::unique_ptr<const File> pfPtr2 = pandasm::AsmEmitter::Emit(res.Value());
337     std::shared_ptr<JSPandaFile> pf1 = pfManager->NewJSPandaFile(pfPtr1.release(), CString(filename1));
338     std::shared_ptr<JSPandaFile> pf2 = pfManager->NewJSPandaFile(pfPtr2.release(), CString(filename2));
339     pfManager->AddJSPandaFile(pf1);
340     pfManager->AddJSPandaFile(pf2);
341     std::vector<CString> descList{};
342     int count = 0;
343     pfManager->EnumerateJSPandaFiles([&](const std::shared_ptr<JSPandaFile> &file) -> bool {
344         auto desc = file->GetJSPandaFileDesc();
345         std::cout << "desc:" << desc << std::endl;
346         descList.push_back(desc);
347         count++;
348         return true;
349     });
350     EXPECT_EQ(count, 2); // 2 : test number of files
351     // Sort by the hash value of the element, the output is unordered
352     EXPECT_STREQ(descList[0].c_str(), "__JSPandaFileManagerTest4.pa");
353     EXPECT_STREQ(descList[1].c_str(), "__JSPandaFileManagerTest3.pa");
354 
355     pfManager->RemoveJSPandaFile(pf1.get());
356     pfManager->RemoveJSPandaFile(pf2.get());
357 }
358 
HWTEST_F_L0(JSPandaFileManagerTest,CheckFilePath)359 HWTEST_F_L0(JSPandaFileManagerTest, CheckFilePath)
360 {
361     JSPandaFileManager *pfManager = JSPandaFileManager::GetInstance();
362     const char *fileName = "__JSPandaFileManagerTest3.abc";
363     const char *data = R"(
364         .function void foo() {}
365     )";
366     Parser parser;
367     auto res = parser.Parse(data);
368     std::unique_ptr<const File> pfPtr = pandasm::AsmEmitter::Emit(res.Value());
369     std::shared_ptr<JSPandaFile> pf = pfManager->NewJSPandaFile(pfPtr.release(), CString(fileName));
370     pfManager->AddJSPandaFile(pf);
371     bool result = pfManager->CheckFilePath(thread, fileName);
372     EXPECT_TRUE(result);
373     pfManager->RemoveJSPandaFile(pf.get());
374 }
375 
HWTEST_F_L0(JSPandaFileManagerTest,GetJSPandaFileByBufferFiles)376 HWTEST_F_L0(JSPandaFileManagerTest, GetJSPandaFileByBufferFiles)
377 {
378     JSPandaFileManager *pfManager = JSPandaFileManager::GetInstance();
379     const char *fileName = "__JSPandaFileManagerTest3.abc";
380     const char *data = R"(
381         .function void foo() {}
382     )";
383     Parser parser;
384     auto res = parser.Parse(data);
385     std::unique_ptr<const File> pfPtr = pandasm::AsmEmitter::Emit(res.Value());
386     std::shared_ptr<JSPandaFile> pf = pfManager->NewJSPandaFile(pfPtr.release(), CString(fileName));
387     std::shared_ptr<JSPandaFile> jsPandaFile;
388     pfManager->AddJSPandaFile(pf);
389     AbcBufferCache *abcBufferCache = thread->GetEcmaVM()->GetAbcBufferCache();
390     abcBufferCache->AddAbcBufferToCache(CString(fileName), (void *)data, sizeof(data), AbcBufferType::NORMAL_BUFFER);
391     AbcBufferInfo bufferInfo = abcBufferCache->FindJSPandaFileInAbcBufferCache(CString(fileName));
392     EXPECT_TRUE(bufferInfo.buffer_ != nullptr);
393     abcBufferCache->DeleteAbcBufferFromCache(CString(fileName));
394     jsPandaFile = pfManager->LoadJSPandaFile(thread, CString(fileName), "");
395     EXPECT_TRUE(jsPandaFile != nullptr);
396 }
397 
HWTEST_F_L0(JSPandaFileManagerTest,ReleaseSecureMem)398 HWTEST_F_L0(JSPandaFileManagerTest, ReleaseSecureMem)
399 {
400     JSNApi::SetReleaseSecureMemCallback(FakeReleaseSecureMemCallback);
401 
402     JSPandaFileManager *pfManager = JSPandaFileManager::GetInstance();
403     const char *fileName = "__JSPandaFileManagerTest3.abc";
404     const char *data = R"(
405         .function void foo() {}
406     )";
407     Parser parser;
408     auto res = parser.Parse(data);
409     std::unique_ptr<const File> pfPtr = pandasm::AsmEmitter::Emit(res.Value());
410     std::shared_ptr<JSPandaFile> pf = pfManager->NewJSPandaFile(pfPtr.release(), CString(fileName));
411     uint8_t *fileMapper = new uint8_t[1];
412     *fileMapper = 1;
413     uint8_t *secondFileMapper = new uint8_t[1];
414     uint8_t *buffer = new uint8_t[1];
415     pf->SetFileMapper(fileMapper);
416     {
417         // first fileMapper doesn't need to release
418         AbcBufferCacheScope bufferScope(
419             thread, CString(fileName), buffer, 3, pf.get(), reinterpret_cast<void *>(fileMapper)); // 3: random number
420     }
421     EXPECT_EQ(*fileMapper, 1);
422     {
423         // second secondFileMapper need to release
424         AbcBufferCacheScope bufferScope(
425             thread, CString(fileName), buffer, 3, pf.get(), reinterpret_cast<void *>(secondFileMapper));
426     }
427     EXPECT_EQ(*secondFileMapper, 10);
428     // when pandafile released, release corresponding fileMapper.
429     EXPECT_EQ(*fileMapper, 1);
430     pf.reset();
431     EXPECT_EQ(*fileMapper, 10);
432 
433     delete[] buffer;
434     delete[] fileMapper;
435     delete[] secondFileMapper;
436 }
437 
HWTEST_F_L0(JSPandaFileManagerTest,LoadInsecureJSPandaFile_1)438 HWTEST_F_L0(JSPandaFileManagerTest, LoadInsecureJSPandaFile_1)
439 {
440     CString fileName = QUICKFIX_ABC_PATH "multi_file/base/merge.abc";
441     JSPandaFileManager *pfManager = JSPandaFileManager::GetInstance();
442     auto jsPandaFile = pfManager->LoadInsecureJSPandaFile(thread, fileName, "");
443     EXPECT_TRUE(jsPandaFile != nullptr);
444     pfManager->RemoveJSPandaFile(jsPandaFile.get());
445 }
446 
HWTEST_F_L0(JSPandaFileManagerTest,LoadInsecureJSPandaFile_2)447 HWTEST_F_L0(JSPandaFileManagerTest, LoadInsecureJSPandaFile_2)
448 {
449     CString fileName = QUICKFIX_ABC_PATH "multi_file/base/module.abc";
450     JSPandaFileManager *pfManager = JSPandaFileManager::GetInstance();
451     auto jsPandaFile = pfManager->LoadInsecureJSPandaFile(thread, fileName, "");
452     EXPECT_TRUE(jsPandaFile == nullptr);
453     pfManager->RemoveJSPandaFile(jsPandaFile.get());
454 }
455 }  // namespace panda::test
456