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 * distribZuted 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 <cstdio>
17#include <iostream>
18
19#include "ecmascript/extractortool/src/extractor.h"
20#include "ecmascript/tests/test_helper.h"
21
22using namespace panda::ecmascript;
23
24namespace panda::ecmascript {
25class ExtractorFriend {
26public:
27    explicit ExtractorFriend(const std::string &source) : extractor_(source)
28    {
29    }
30
31    bool GetInitial() const
32    {
33        return extractor_.initial_;
34    }
35
36    void SetInitial(bool initialValue)
37    {
38        extractor_.initial_ = initialValue;
39    }
40
41    const std::string &GetHapPath() const
42    {
43        return extractor_.hapPath_;
44    }
45
46    void SetHapPath(const std::string &newHapPath)
47    {
48        extractor_.hapPath_ = newHapPath;
49    }
50
51    std::optional<bool> GetIsStageModel() const
52    {
53        return extractor_.isStageModel_;
54    }
55
56    void SetIsStageModel(bool model)
57    {
58        extractor_.isStageModel_ = model;
59    }
60
61    Extractor extractor_;
62};
63
64class ExtractorUtilFriend {
65public:
66    void DeleteExtractor(const std::string &hapPath)
67    {
68        return extractorUtil_.DeleteExtractor(hapPath);
69    }
70
71    std::unordered_map<std::string, std::shared_ptr<Extractor>> GetExtractorMap()
72    {
73        return extractorUtil_.extractorMap_;
74    }
75
76private:
77    ExtractorUtil extractorUtil_;
78};
79}
80
81namespace panda::test {
82class ExtractorTest : public testing::Test {
83public:
84    static void SetUpTestCase()
85    {
86        GTEST_LOG_(INFO) << "SetUpTestCase";
87    }
88
89    static void TearDownTestCase()
90    {
91        GTEST_LOG_(INFO) << "TearDownCase";
92    }
93
94    void SetUp() override
95    {
96        TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
97        instance->SetEnableForceGC(false);
98    }
99
100    void TearDown() override
101    {
102        TestHelper::DestroyEcmaVMWithScope(instance, scope);
103    }
104
105    EcmaVM *instance{nullptr};
106    EcmaHandleScope *scope{nullptr};
107    JSThread *thread{nullptr};
108};
109
110HWTEST_F_L0(ExtractorTest, InitTest)
111{
112    ExtractorFriend extractorFriend("TestFile");
113    EXPECT_FALSE(extractorFriend.extractor_.Init());
114}
115
116HWTEST_F_L0(ExtractorTest, GetFileBufferTest)
117{
118    ExtractorFriend extractorFriend("TestFile");
119    std::ostringstream dest;
120    EXPECT_FALSE(extractorFriend.extractor_.GetFileBuffer("Test", dest));
121    extractorFriend.SetInitial(true);
122    EXPECT_FALSE(extractorFriend.extractor_.GetFileBuffer("", dest));
123    EXPECT_FALSE(extractorFriend.extractor_.GetFileBuffer("Test", dest));
124}
125
126HWTEST_F_L0(ExtractorTest, GetFileListTest)
127{
128    ExtractorFriend extractorFriend("TestFile");
129    std::vector<std::string> assetList;
130    EXPECT_FALSE(extractorFriend.extractor_.GetFileList("TestSrcPath", assetList));
131    extractorFriend.SetInitial(true);
132    EXPECT_FALSE(extractorFriend.extractor_.GetFileList("", assetList));
133    EXPECT_TRUE(extractorFriend.extractor_.GetFileList("/", assetList));
134    std::set<std::string> fileSet;
135    EXPECT_TRUE(extractorFriend.extractor_.GetFileList("TestSrcPath", fileSet));
136}
137
138HWTEST_F_L0(ExtractorTest, HasEntryTest)
139{
140    ExtractorFriend extractorFriend("TestFile");
141    EXPECT_FALSE(extractorFriend.extractor_.HasEntry("TestFileName"));
142    extractorFriend.SetInitial(true);
143    EXPECT_FALSE(extractorFriend.extractor_.HasEntry("TestFileName"));
144}
145
146HWTEST_F_L0(ExtractorTest, IsDirExistTest)
147{
148    ExtractorFriend extractorFriend("TestFile");
149    EXPECT_FALSE(extractorFriend.extractor_.IsDirExist("TestFileName"));
150    extractorFriend.SetInitial(true);
151    EXPECT_FALSE(extractorFriend.extractor_.IsDirExist(""));
152    EXPECT_TRUE(extractorFriend.extractor_.IsDirExist("/"));
153}
154
155HWTEST_F_L0(ExtractorTest, ExtractByNameTest)
156{
157    ExtractorFriend extractorFriend("TestFile");
158    std::ostringstream dest;
159    EXPECT_FALSE(extractorFriend.extractor_.ExtractByName("TestFileName", dest));
160    extractorFriend.SetInitial(true);
161}
162
163HWTEST_F_L0(ExtractorTest, ExtractFileTest)
164{
165    ExtractorFriend extractorFriend("TestFile");
166    EXPECT_FALSE(extractorFriend.extractor_.ExtractFile("TestFileName", "TestPath"));
167}
168
169HWTEST_F_L0(ExtractorTest, GetSafeDataTest)
170{
171    ExtractorFriend extractorFriend("TestFile");
172    auto result = extractorFriend.extractor_.GetSafeData("TestFileName.abc");
173    EXPECT_EQ(result, nullptr);
174
175    result = extractorFriend.extractor_.GetSafeData("TestFileName");
176    EXPECT_EQ(result, nullptr);
177}
178
179HWTEST_F_L0(ExtractorTest, IsStageModelTest)
180{
181    ExtractorFriend extractorFriend("TestFile");
182    EXPECT_TRUE(extractorFriend.extractor_.IsStageModel());
183    EXPECT_TRUE(extractorFriend.extractor_.IsStageModel());
184}
185
186HWTEST_F_L0(ExtractorTest, ExtractToBufByNameTest)
187{
188    ExtractorFriend extractorFriend("TestFile");
189    auto byteArray = std::make_unique<uint8_t[]>(1024);
190    size_t len;
191    EXPECT_FALSE(extractorFriend.extractor_.ExtractToBufByName("TestFileName", byteArray, len));
192}
193
194HWTEST_F_L0(ExtractorTest, GetFileInfoTest)
195{
196    ExtractorFriend extractorFriend("TestFile");
197    FileInfo fileInfo;
198    EXPECT_FALSE(extractorFriend.extractor_.GetFileInfo("TestFileName", fileInfo));
199}
200
201HWTEST_F_L0(ExtractorTest, GetFileListTest2)
202{
203    ExtractorFriend extractorFriend("TestFile");
204    std::set<std::string> fileSet;
205    EXPECT_FALSE(extractorFriend.extractor_.GetFileList("TestSrcPath", fileSet));
206
207    extractorFriend.SetInitial(true);
208    EXPECT_FALSE(extractorFriend.extractor_.GetFileList("", fileSet));
209}
210
211HWTEST_F_L0(ExtractorTest, IsHapCompressTest)
212{
213    ExtractorFriend extractorFriend("TestFile");
214    EXPECT_FALSE(extractorFriend.extractor_.IsHapCompress("TestFileName"));
215}
216
217HWTEST_F_L0(ExtractorTest, GetLoadFilePathTest)
218{
219    ExtractorUtil util;
220    EXPECT_EQ(util.GetLoadFilePath("/data/app/el1/bundle/public/Test.hap"), "/data/storage/el1/bundle/Test.hap");
221    EXPECT_EQ(util.GetLoadFilePath("Test.hap"), "Test.hap");
222}
223
224HWTEST_F_L0(ExtractorTest, GetExtractorTest)
225{
226    ExtractorUtil util;
227    bool newCreate = false;
228    bool cache = false;
229    auto result = util.GetExtractor("", newCreate, cache);
230    EXPECT_EQ(result, nullptr);
231
232    result = util.GetExtractor("TestHapPath.hap", newCreate, cache);
233    EXPECT_EQ(result, nullptr);
234}
235
236HWTEST_F_L0(ExtractorTest, DeleteExtractorTest)
237{
238    ExtractorUtilFriend util;
239    util.DeleteExtractor("");
240    util.DeleteExtractor("TestHapPath.hap");
241    std::unordered_map<std::string, std::shared_ptr<Extractor>> emptyMap;
242    auto map = util.GetExtractorMap();
243    EXPECT_EQ(map, emptyMap);
244}
245}