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.GetPrintNameCache().empty(), true); 58 EXPECT_EQ(options.GetApplyNameCache().empty(), true); 59 EXPECT_EQ(options.IsPropertyObfEnabled(), true); 60 EXPECT_EQ(options.IsToplevelObfEnabled(), true); 61 EXPECT_EQ(options.IsFileNameObfEnabled(), true); 62 EXPECT_EQ(options.IsReservedNames("xxx"), true); 63 EXPECT_EQ(options.IsReservedProperties("xxx"), true); 64 EXPECT_EQ(options.IsReservedToplevelNames("xxx"), true); 65 EXPECT_EQ(options.IsReservedFileNames("xxx"), true); 66 EXPECT_EQ(options.IsKeepPath("xxx"), true); 67 EXPECT_EQ(options.IsKeepPath("yyy"), false); 68 } 69 70 /** 71 * @tc.name: guard_options_test_002 72 * @tc.desc: test options abc and obf abc is empty and not has obfuscationRules field 73 * @tc.type: FUNC 74 * @tc.require: 75 */ 76 HWTEST(GuardOptionsUnitTest, guard_options_test_002, TestSize.Level4) 77 { 78 guard::GuardOptions options; 79 80 std::string jsonStr = R"({ 81 "abcFilePath": "", 82 "obfAbcFilePath": "", 83 "obfPaFilePath": "", 84 "targetApiVersion": 12, 85 "targetApiSubVersion": "beta3", 86 "compileSdkVersion": "xxx", 87 "entryPackageInfo": "xxx", 88 "defaultNameCachePath": "xxx", 89 "obfuscationRules" : {} 90 })"; 91 guard::FileUtil::WriteFile(TEMP_OPTIONS_TEST_JSON_FILE, jsonStr); 92 EXPECT_DEATH(options.Load(TEMP_OPTIONS_TEST_JSON_FILE), ""); 93 94 guard::TestUtil::RemoveFile(TEMP_OPTIONS_TEST_JSON_FILE); 95 96 jsonStr = R"({ 97 "abcFilePath": "xxx", 98 "obfAbcFilePath": "xxx", 99 "obfPaFilePath": "xxx", 100 "targetApiVersion": 12, 101 "targetApiSubVersion": "beta3", 102 "compileSdkVersion": "xxx", 103 "entryPackageInfo": "xxx", 104 "defaultNameCachePath": "xxx" 105 })"; 106 guard::FileUtil::WriteFile(TEMP_OPTIONS_TEST_JSON_FILE, jsonStr); 107 EXPECT_DEATH(options.Load(TEMP_OPTIONS_TEST_JSON_FILE), ""); 108 109 guard::TestUtil::RemoveFile(TEMP_OPTIONS_TEST_JSON_FILE); 110 } 111 112 /** 113 * @tc.name: guard_options_test_003 114 * @tc.desc: test options obf rules default value 115 * @tc.type: FUNC 116 * @tc.require: 117 */ 118 HWTEST(GuardOptionsUnitTest, guard_options_test_003, TestSize.Level4) 119 { 120 std::string jsonStr = R"({ 121 "abcFilePath": "xxx", 122 "obfAbcFilePath": "xxx", 123 "obfPaFilePath": "xxx", 124 "targetApiVersion": 12, 125 "targetApiSubVersion": "beta3", 126 "compileSdkVersion": "xxx", 127 "entryPackageInfo": "xxx", 128 "defaultNameCachePath": "xxx", 129 "obfuscationRules" : {} 130 })"; 131 guard::FileUtil::WriteFile(TEMP_OPTIONS_TEST_JSON_FILE, jsonStr); 132 guard::GuardOptions options; 133 options.Load(TEMP_OPTIONS_TEST_JSON_FILE); 134 135 EXPECT_EQ(options.DisableObfuscation(), false); 136 EXPECT_EQ(options.IsExportObfEnabled(), false); 137 EXPECT_EQ(options.IsRemoveLogObfEnabled(), false); 138 EXPECT_EQ(options.IsPropertyObfEnabled(), false); 139 EXPECT_EQ(options.IsToplevelObfEnabled(), false); 140 EXPECT_EQ(options.IsFileNameObfEnabled(), false); 141 EXPECT_EQ(options.IsKeepPath("xxx"), false); 142 guard::TestUtil::RemoveFile(TEMP_OPTIONS_TEST_JSON_FILE); 143 } 144 145 /** 146 * @tc.name: guard_options_test_004 147 * @tc.desc: test options parse data not json return failed 148 * @tc.type: FUNC 149 * @tc.require: 150 */ 151 HWTEST(GuardOptionsUnitTest, guard_options_test_004, TestSize.Level4) 152 { 153 std::string jsonStr = R"({ 154 "abcFilePath": "xxx", 155 "obfAbcFilePath": "xxx", 156 "obfPaFilePath": "xxx", 157 "targetApiVersion": 12, 158 "targetApiSubVersion": "beta3", 159 "compileSdkVersion": "xxx", 160 "entryPackageInfo": "xxx", 161 "defaultNameCachePath": "xxx", 162 "obfuscationRules" : { 163 })"; 164 guard::FileUtil::WriteFile(TEMP_OPTIONS_TEST_JSON_FILE, jsonStr); 165 guard::GuardOptions options; 166 EXPECT_DEATH(options.Load(TEMP_OPTIONS_TEST_JSON_FILE), ""); 167 168 guard::TestUtil::RemoveFile(TEMP_OPTIONS_TEST_JSON_FILE); 169 } 170 171 /** 172 * @tc.name: guard_options_test_005 173 * @tc.desc: test options universal reserved names match is right 174 * @tc.type: FUNC 175 * @tc.require: 176 */ 177 HWTEST(GuardOptionsUnitTest, guard_options_test_005, TestSize.Level4) 178 { 179 guard::GuardOptions options; 180 options.Load(OPTIONS_TEST_05_CONFIG_FILE); 181 182 // xx.* 183 EXPECT_EQ(options.IsReservedProperties("xx"), true); 184 EXPECT_EQ(options.IsReservedProperties("xxabc"), true); 185 EXPECT_EQ(options.IsReservedProperties("xy"), false); 186 // yy. 187 EXPECT_EQ(options.IsReservedToplevelNames("yya"), true); 188 EXPECT_EQ(options.IsReservedToplevelNames("yy"), false); 189 EXPECT_EQ(options.IsReservedToplevelNames("xy"), false); 190 // .*zz. 191 EXPECT_EQ(options.IsReservedFileNames("abczzA"), true); 192 EXPECT_EQ(options.IsReservedFileNames("zzA"), true); 193 EXPECT_EQ(options.IsReservedFileNames("abczz"), false); 194 EXPECT_EQ(options.IsReservedFileNames("zz"), false); 195 } 196