• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
18 #include "configs/guard_options.h"
19 #include "util/file_util.h"
20 
21 #include "util/test_util.h"
22 
23 using namespace testing::ext;
24 using namespace panda;
25 
26 namespace {
27 const std::string OPTIONS_TEST_01_CONFIG_FILE = PANDA_GUARD_UNIT_TEST_DIR "configs/options_test_01_config.json";
28 const std::string OPTIONS_TEST_05_CONFIG_FILE = PANDA_GUARD_UNIT_TEST_DIR "configs/options_test_05_config.json";
29 const std::string TEMP_OPTIONS_TEST_JSON_FILE = PANDA_GUARD_UNIT_TEST_DIR "configs/temp_options_test.json";
30 }  // namespace
31 
32 /**
33  * @tc.name: guard_options_test_001
34  * @tc.desc: test options parse is right
35  * @tc.type: FUNC
36  * @tc.require:
37  */
38 HWTEST(GuardOptionsUnitTest, guard_options_test_001, TestSize.Level4)
39 {
40     guard::GuardOptions options;
41     options.Load(OPTIONS_TEST_01_CONFIG_FILE);
42 
43     EXPECT_EQ(options.GetAbcFilePath(), "xxx");
44     EXPECT_EQ(options.GetObfAbcFilePath(), "xxx");
45     EXPECT_EQ(options.GetObfPaFilePath(), "xxx");
46     EXPECT_EQ(options.GetCompileSdkVersion(), "xxx");
47     EXPECT_EQ(options.GetTargetApiVersion(), 12);
48     EXPECT_EQ(options.GetTargetApiSubVersion(), "beta3");
49     EXPECT_EQ(options.GetSourceName("xxx"), "xxx");
50     EXPECT_EQ(options.GetEntryPackageInfo(), "xxx");
51     EXPECT_EQ(options.GetDefaultNameCachePath(), "xxx");
52     EXPECT_EQ(options.IsSkippedRemoteHar("xxx"), true);
53     EXPECT_EQ(options.IsUseNormalizedOhmUrl(), true);
54     EXPECT_EQ(options.DisableObfuscation(), false);
55     EXPECT_EQ(options.IsExportObfEnabled(), true);
56     EXPECT_EQ(options.IsRemoveLogObfEnabled(), true);
57     EXPECT_EQ(options.IsDecoratorObfEnabled(), true);
58     EXPECT_EQ(options.IsCompactObfEnabled(), true);
59     EXPECT_EQ(options.GetPrintNameCache().empty(), true);
60     EXPECT_EQ(options.GetApplyNameCache().empty(), true);
61     EXPECT_EQ(options.IsPropertyObfEnabled(), true);
62     EXPECT_EQ(options.IsToplevelObfEnabled(), true);
63     EXPECT_EQ(options.IsFileNameObfEnabled(), true);
64     EXPECT_EQ(options.IsReservedNames("xxx"), true);
65     EXPECT_EQ(options.IsReservedProperties("xxx"), true);
66     EXPECT_EQ(options.IsReservedToplevelNames("xxx"), true);
67     EXPECT_EQ(options.IsReservedFileNames("xxx"), true);
68     EXPECT_EQ(options.IsReservedRemoteHarPkgNames("xxx"), true);
69     EXPECT_EQ(options.IsKeepPath("xxx"), true);
70     EXPECT_EQ(options.IsKeepPath("yyy"), false);
71 }
72 
73 /**
74  * @tc.name: guard_options_test_002
75  * @tc.desc: test options abc and obf abc is empty and not has obfuscationRules field
76  * @tc.type: FUNC
77  * @tc.require:
78  */
79 HWTEST(GuardOptionsUnitTest, guard_options_test_002, TestSize.Level4)
80 {
81     guard::GuardOptions options;
82 
83     std::string jsonStr = R"({
84             "abcFilePath": "",
85             "obfAbcFilePath": "",
86             "obfPaFilePath": "",
87             "targetApiVersion": 12,
88             "targetApiSubVersion": "beta3",
89             "compileSdkVersion": "xxx",
90             "entryPackageInfo": "xxx",
91             "defaultNameCachePath": "xxx",
92             "obfuscationRules" : {}
93         })";
94     guard::FileUtil::WriteFile(TEMP_OPTIONS_TEST_JSON_FILE, jsonStr);
95     EXPECT_DEATH(options.Load(TEMP_OPTIONS_TEST_JSON_FILE), "");
96 
97     guard::TestUtil::RemoveFile(TEMP_OPTIONS_TEST_JSON_FILE);
98 
99     jsonStr = R"({
100             "abcFilePath": "xxx",
101             "obfAbcFilePath": "xxx",
102             "obfPaFilePath": "xxx",
103             "targetApiVersion": 12,
104             "targetApiSubVersion": "beta3",
105             "compileSdkVersion": "xxx",
106             "entryPackageInfo": "xxx",
107             "defaultNameCachePath": "xxx"
108         })";
109     guard::FileUtil::WriteFile(TEMP_OPTIONS_TEST_JSON_FILE, jsonStr);
110     EXPECT_DEATH(options.Load(TEMP_OPTIONS_TEST_JSON_FILE), "");
111 
112     guard::TestUtil::RemoveFile(TEMP_OPTIONS_TEST_JSON_FILE);
113 }
114 
115 /**
116  * @tc.name: guard_options_test_003
117  * @tc.desc: test options obf rules default value
118  * @tc.type: FUNC
119  * @tc.require:
120  */
121 HWTEST(GuardOptionsUnitTest, guard_options_test_003, TestSize.Level4)
122 {
123     std::string jsonStr = R"({
124             "abcFilePath": "xxx",
125             "obfAbcFilePath": "xxx",
126             "obfPaFilePath": "xxx",
127             "targetApiVersion": 12,
128             "targetApiSubVersion": "beta3",
129             "compileSdkVersion": "xxx",
130             "entryPackageInfo": "xxx",
131             "defaultNameCachePath": "xxx",
132             "obfuscationRules" : {}
133         })";
134     guard::FileUtil::WriteFile(TEMP_OPTIONS_TEST_JSON_FILE, jsonStr);
135     guard::GuardOptions options;
136     options.Load(TEMP_OPTIONS_TEST_JSON_FILE);
137 
138     EXPECT_EQ(options.DisableObfuscation(), false);
139     EXPECT_EQ(options.IsExportObfEnabled(), false);
140     EXPECT_EQ(options.IsRemoveLogObfEnabled(), false);
141     EXPECT_EQ(options.IsPropertyObfEnabled(), false);
142     EXPECT_EQ(options.IsToplevelObfEnabled(), false);
143     EXPECT_EQ(options.IsFileNameObfEnabled(), false);
144     EXPECT_EQ(options.IsKeepPath("xxx"), false);
145     guard::TestUtil::RemoveFile(TEMP_OPTIONS_TEST_JSON_FILE);
146 }
147 
148 /**
149  * @tc.name: guard_options_test_004
150  * @tc.desc: test options parse data not json return failed
151  * @tc.type: FUNC
152  * @tc.require:
153  */
154 HWTEST(GuardOptionsUnitTest, guard_options_test_004, TestSize.Level4)
155 {
156     std::string jsonStr = R"({
157             "abcFilePath": "xxx",
158             "obfAbcFilePath": "xxx",
159             "obfPaFilePath": "xxx",
160             "targetApiVersion": 12,
161             "targetApiSubVersion": "beta3",
162             "compileSdkVersion": "xxx",
163             "entryPackageInfo": "xxx",
164             "defaultNameCachePath": "xxx",
165             "obfuscationRules" : {
166         })";
167     guard::FileUtil::WriteFile(TEMP_OPTIONS_TEST_JSON_FILE, jsonStr);
168     guard::GuardOptions options;
169     EXPECT_DEATH(options.Load(TEMP_OPTIONS_TEST_JSON_FILE), "");
170 
171     guard::TestUtil::RemoveFile(TEMP_OPTIONS_TEST_JSON_FILE);
172 }
173 
174 /**
175  * @tc.name: guard_options_test_005
176  * @tc.desc: test options universal reserved names match is right
177  * @tc.type: FUNC
178  * @tc.require:
179  */
180 HWTEST(GuardOptionsUnitTest, guard_options_test_005, TestSize.Level4)
181 {
182     guard::GuardOptions options;
183     options.Load(OPTIONS_TEST_05_CONFIG_FILE);
184 
185     // xx.*
186     EXPECT_EQ(options.IsReservedProperties("xx"), true);
187     EXPECT_EQ(options.IsReservedProperties("xxabc"), true);
188     EXPECT_EQ(options.IsReservedProperties("xy"), false);
189     // yy.
190     EXPECT_EQ(options.IsReservedToplevelNames("yya"), true);
191     EXPECT_EQ(options.IsReservedToplevelNames("yy"), false);
192     EXPECT_EQ(options.IsReservedToplevelNames("xy"), false);
193     // .*zz.
194     EXPECT_EQ(options.IsReservedFileNames("abczzA"), true);
195     EXPECT_EQ(options.IsReservedFileNames("zzA"), true);
196     EXPECT_EQ(options.IsReservedFileNames("abczz"), false);
197     EXPECT_EQ(options.IsReservedFileNames("zz"), false);
198 }
199