1 /*
2 * Copyright (c) 2024-2025 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 <cstdarg>
18 #include <string>
19
20 #include "extractor.h"
21 #include "file_mapper.h"
22 #include "js_environment_impl.h"
23 #define private public
24 #define protected public
25 #include "js_worker.h"
26 #undef private
27 #undef protected
28 #include "native_engine.h"
29 #include "worker_info.h"
30
31 using namespace testing;
32 using namespace testing::ext;
33
34 namespace OHOS {
35 namespace AbilityRuntime {
36 class JsWorkerTest : public testing::Test {
37 public:
38 static void SetUpTestCase();
39 static void TearDownTestCase();
40 void SetUp() override;
41 void TearDown() override;
42 void TestSetGetAssetFunc(GetAssetFunc func);
43 GetAssetFunc TestGetGetAssetFunc() const;
44
45 private:
46 GetAssetFunc getAssetFunc_ = nullptr;
47 };
48
SetUpTestCase()49 void JsWorkerTest::SetUpTestCase()
50 {}
51
TearDownTestCase()52 void JsWorkerTest::TearDownTestCase()
53 {}
54
SetUp()55 void JsWorkerTest::SetUp()
56 {}
57
TearDown()58 void JsWorkerTest::TearDown()
59 {}
60
TestSetGetAssetFunc(GetAssetFunc func)61 void JsWorkerTest::TestSetGetAssetFunc(GetAssetFunc func)
62 {
63 getAssetFunc_ = func;
64 }
65
TestGetGetAssetFunc() const66 GetAssetFunc JsWorkerTest::TestGetGetAssetFunc() const
67 {
68 return getAssetFunc_;
69 }
70
71 /**
72 * @tc.name: AssetHelper_0100
73 * @tc.desc: Asset helper.
74 * @tc.type: FUNC
75 * @tc.require: issue#I948D4
76 */
77 HWTEST_F(JsWorkerTest, AssetHelper_0100, TestSize.Level1)
78 {
79 std::shared_ptr<JsEnv::WorkerInfo> workerInfo = std::make_shared<JsEnv::WorkerInfo>();
80 workerInfo->codePath = panda::panda_file::StringPacProtect("/data/test/codePath");
81 workerInfo->packagePathStr = "/data/test/packagePath";
82 workerInfo->hapPath = panda::panda_file::StringPacProtect("/data/test/hapPath");
83 workerInfo->moduleName = "moduleName";
84 TestSetGetAssetFunc(AssetHelper(workerInfo));
85
86 std::string uri = "/data";
87 uint8_t *buff = nullptr;
88 size_t buffSize;
89 std::vector<uint8_t> content;
90 std::string ami;
91 bool useSecureMem;
92 bool isRestricted = false;
93 auto func = TestGetGetAssetFunc();
94 std::unique_ptr<AbilityBase::FileMapper> fileMapper = std::make_unique<AbilityBase::FileMapper>();
95 void* mapper = static_cast<void*>(fileMapper.get());
96 func("/data", &buff, &buffSize, content, ami, useSecureMem, &mapper, isRestricted);
97 EXPECT_EQ(useSecureMem, false);
98 }
99
100 /**
101 * @tc.name: AssetHelper_0200
102 * @tc.desc: Asset helper GetSafeData.
103 * @tc.type: FUNC
104 * @tc.require: issue#I948D4
105 */
106 HWTEST_F(JsWorkerTest, AssetHelper_0200, TestSize.Level1)
107 {
108 std::shared_ptr<JsEnv::WorkerInfo> workerInfo = std::make_shared<JsEnv::WorkerInfo>();
109 workerInfo->codePath = panda::panda_file::StringPacProtect("/data/test/codePath");
110 workerInfo->packagePathStr = "/data/test/packagePath";
111 workerInfo->hapPath = panda::panda_file::StringPacProtect("/data/test/hapPath");
112 workerInfo->moduleName = "moduleName";
113 AssetHelper helper = AssetHelper(workerInfo);
114
115 FILE *fp = nullptr;
116 fp = fopen("test.txt", "w+");
117 ASSERT_NE(fp, nullptr);
118 fclose(fp);
119
120 uint8_t *buff = nullptr;
121 size_t buffSize;
122 std::unique_ptr<AbilityBase::FileMapper> fileMapper = std::make_unique<AbilityBase::FileMapper>();
123 void* mapper = static_cast<void*>(fileMapper.get());
124 auto ret = helper.GetSafeData("test.txt", &buff, &buffSize, &mapper);
125 EXPECT_EQ(ret, false);
126 }
127 } // namespace AbilityRuntime
128 } // namespace OHOS
129