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 #include "param_update_test.h"
16 
17 #include "file_util.h"
18 #include "param_manager.h"
19 
20 using namespace testing::ext;
21 using namespace OHOS::HiviewDFX;
22 
23 namespace {
24     const std::string TEST_CONFIG_FILE = "/data/system/hiview/test.txt";
25     const std::string TEST_ANCO_CONFIG_PATH = "/data/system/hiview/anco/";
26     const std::string CERT_CONFIG_FILE_FULL_NAME = "/data/test/test_data/CERT_PRE.config";
27     const std::string CERT_ENC_FILE_FULL_NAME =
28         "/data/service/el1/public/update/param_service/install/system/etc/HIVIEWPARA/DEFAULT/CERT.ENC";
29     const std::string SOURCE_TEST_CONFIG_FILE =
30         "/data/service/el1/public/update/param_service/install/system/etc/HIVIEWPARA/DEFAULT/test.txt";
31     constexpr int CHAR_0 = 48; // '0'
32     constexpr int CHAR_9 = 57; // '9'
33     constexpr int CHAR_A = 65; // 'A'
34     constexpr int CHAR_F = 70; // 'F'
35     constexpr int HEX_BASE = 10;
36 
ToNum(char a)37 int ToNum(char a)
38 {
39     if (a >= CHAR_0 && a <= CHAR_9) {
40         return static_cast<int>(a - '0');
41     }
42 
43     if (a >= CHAR_A && a <= CHAR_F) {
44         return (static_cast<int>(a - 'A') + HEX_BASE);
45     }
46     return 0;
47 }
48 
CreateEncFile()49 void CreateEncFile()
50 {
51     std::string content;
52     FileUtil::LoadStringFromFile(CERT_CONFIG_FILE_FULL_NAME, content);
53     std::vector<char> saveContent;
54     const int hightBit = 4;
55     const int hexLen = 2;
56     const int secondHex = 1;
57     for (int i = 0; i < content.length();) {
58         saveContent.push_back(static_cast<char>(ToNum(content[i]) << hightBit) + (ToNum(content[i + secondHex])));
59         i += hexLen;
60     }
61 
62     FileUtil::SaveBufferToFile(CERT_ENC_FILE_FULL_NAME, saveContent, true);
63 }
64 }
65 
SetUp()66 void ParamUpdateTest::SetUp()
67 {
68     /**
69      * @tc.setup: create an event loop and multiple event handlers
70      */
71     std::string configDir("/data/test/test_data/hiview_platform_config");
72     if (!platform.InitEnvironment(configDir)) {
73         std::cout << "fail to init environment" << std::endl;
74     } else {
75         std::cout << "init environment successful" << std::endl;
76     }
77 }
78 
TearDown()79 void ParamUpdateTest::TearDown()
80 {
81     /**
82      * @tc.teardown: destroy the event loop we have created
83      */
84     FileUtil::RemoveFile(TEST_CONFIG_FILE);
85     FileUtil::ForceRemoveDirectory(TEST_ANCO_CONFIG_PATH);
86 }
87 
88 /**
89  * @tc.name: PlatformConfigParse001
90  * @tc.desc: parse a correct config file and check result
91  * @tc.type: FUNC
92  * @tc.require: AR000DPTT5
93  */
94 HWTEST_F(ParamUpdateTest, ParamUpdateTest001, TestSize.Level1)
95 {
96     ParamManager::InitParam();
97     ASSERT_FALSE(FileUtil::FileExists(TEST_CONFIG_FILE));
98     CreateEncFile();
99     ParamManager::InitParam();
100     if (!FileUtil::FileExists(PUBKEY_PATH)) {
101         ASSERT_FALSE(FileUtil::FileExists(TEST_CONFIG_FILE));
102     } else {
103         ASSERT_TRUE(FileUtil::FileExists(TEST_CONFIG_FILE));
104     }
105     FileUtil::RemoveFile(TEST_CONFIG_FILE);
106     FileUtil::SaveStringToFile(SOURCE_TEST_CONFIG_FILE, "1234");
107     ParamManager::InitParam();
108     ASSERT_FALSE(FileUtil::FileExists(TEST_CONFIG_FILE));
109 }