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