1 /*
2 * Copyright (c) 2021 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 "updater_main_unittest.h"
17 #include <dirent.h>
18 #include <fstream>
19 #include <iostream>
20 #include <string>
21 #include "fs_manager/mount.h"
22 #include "log/log.h"
23 #include "misc_info/misc_info.h"
24 #include "securec.h"
25 #include "updater_main.h"
26 #include "updater_ui.h"
27 #include "updater/updater.h"
28 #include "utils.h"
29
30 using namespace Updater;
31 using namespace testing::ext;
32 using namespace std;
33 using namespace Updater::Utils;
34
35 namespace UpdaterUt {
36 constexpr uint32_t MAX_ARG_SIZE = 10;
37 const std::string MISC_FILE = "/dev/block/platform/soc/10100000.himci.eMMC/by-name/misc";
38 // do something at the each function begining
SetUp(void)39 void UpdaterMainUnitTest::SetUp(void)
40 {
41 cout << "Updater Unit UpdaterMainUnitTest Begin!" << endl;
42 }
43
44 // do something at the each function end
TearDown(void)45 void UpdaterMainUnitTest::TearDown(void)
46 {
47 cout << "Updater Unit UpdaterMainUnitTest End!" << endl;
48 }
49
50 // init
SetUpTestCase(void)51 void UpdaterMainUnitTest::SetUpTestCase(void)
52 {
53 cout << "SetUpTestCase" << endl;
54 Updater::Utils::MkdirRecursive("/data/sdcard/updater", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
55 LoadSpecificFstab("/data/updater/main_data/updater.tab");
56 if (MountForPath("/data") != 0) {
57 cout << "MountForPath failed" << endl;
58 }
59 PostUpdater(true);
60 }
61
62 // end
TearDownTestCase(void)63 void UpdaterMainUnitTest::TearDownTestCase(void)
64 {
65 cout << "TearDownTestCase" << endl;
66 unlink("/data/updater.log");
67 unlink("/data/updater_stage.log");
68 }
69
70 HWTEST_F(UpdaterMainUnitTest, updater_main_test_001, TestSize.Level1)
71 {
72 UpdateMessage boot {};
73 if (access("/data/updater/", 0)) {
74 int ret = mkdir("/data/updater/", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
75 ASSERT_EQ(ret, 0);
76 }
77 const std::string commandFile = "/data/updater/command";
78 auto fp = std::unique_ptr<FILE, decltype(&fclose)>(fopen(commandFile.c_str(), "wb"), fclose);
79 EXPECT_NE(fp, nullptr);
80 const std::string commandMsg = "boot_updater";
81 EXPECT_EQ(strncpy_s(boot.command, sizeof(boot.command) - 1, commandMsg.c_str(), commandMsg.size()), 0);
82 EXPECT_EQ(strncpy_s(boot.update, sizeof(boot.update), "", sizeof(boot.update)), 0);
83 bool bRet = WriteUpdaterMessage(commandFile, boot);
84 EXPECT_EQ(bRet, true);
85 char **argv = new char*[1];
86 argv[0] = new char[MAX_ARG_SIZE];
87 EXPECT_EQ(strncpy_s(argv[0], MAX_ARG_SIZE, "./main", MAX_ARG_SIZE), 0);
88 int argc = 1;
89 std::vector<std::string> args = ParseParams(argc, argv);
90
91 EXPECT_EQ(args.size(), static_cast<unsigned int>(argc));
92 PostUpdater(true);
93 delete argv[0];
94 delete []argv;
95 }
96
97 HWTEST_F(UpdaterMainUnitTest, updater_main_test_002, TestSize.Level1)
98 {
99 UpdateMessage boot {};
100 const std::string command1 = "boot_updater";
101 EXPECT_EQ(strncpy_s(boot.command, sizeof(boot.command) - 1, command1.c_str(), command1.size()), 0);
102 const std::string command2 = "--user_wipe_data";
103 EXPECT_EQ(strncpy_s(boot.update, sizeof(boot.update) - 1, command2.c_str(), command2.size()), 0);
104 bool ret = WriteUpdaterMessage(MISC_FILE, boot);
105 EXPECT_EQ(ret, true);
106
107 int fRet = FactoryReset(USER_WIPE_DATA, "/misc/factory_test");
108 EXPECT_EQ(fRet, 0);
109 PostUpdater(true);
110 }
111
112 HWTEST_F(UpdaterMainUnitTest, updater_main_test_003, TestSize.Level1)
113 {
114 const std::string sLog = "/data/updater/main_data/updater.tab";
115 const std::string dLog = "/data/updater/main_data/ut_dLog.txt";
116 bool ret = CopyUpdaterLogs(sLog, dLog);
117 EXPECT_EQ(ret, true);
118 unlink(dLog.c_str());
119 }
120
121 HWTEST_F(UpdaterMainUnitTest, updater_main_test_004, TestSize.Level1)
122 {
123 UpdaterUiInit();
124 auto fp = std::unique_ptr<FILE, decltype(&fclose)>(fopen("/data/updater/updater.zip", "wb"), fclose);
125 EXPECT_NE(fp, nullptr);
126
127 UpdateMessage boot {};
128 const std::string command1 = "boot_updater";
129 EXPECT_EQ(strncpy_s(boot.command, sizeof(boot.command) - 1, command1.c_str(), command1.size()), 0);
130 const std::string command2 = "--update_package=/data/updater/updater.zip\n--retry_count=0";
131 EXPECT_EQ(strncpy_s(boot.update, sizeof(boot.update) - 1, command2.c_str(), command2.size()), 0);
132 bool ret = WriteUpdaterMessage(MISC_FILE, boot);
133 EXPECT_EQ(ret, true);
134
135 char **argv = new char*[MAX_ARG_SIZE];
136 argv[0] = new char[10];
137 EXPECT_EQ(strncpy_s(argv[0], MAX_ARG_SIZE, "./main", MAX_ARG_SIZE), 0);
138 int argc = 1;
139 int lRet = UpdaterMain(argc, argv);
140 EXPECT_EQ(lRet, 0);
141
142 EXPECT_EQ(memset_s(boot.update, sizeof(boot.update), 0, sizeof(boot.update)), 0);
143 const std::string command3 = "--user_wipe_data";
144 EXPECT_EQ(strncpy_s(boot.update, sizeof(boot.update) - 1, command3.c_str(), command3.size()), 0);
145 ret = WriteUpdaterMessage(MISC_FILE, boot);
146 EXPECT_EQ(ret, true);
147 lRet = UpdaterMain(argc, argv);
148 EXPECT_EQ(lRet, 0);
149
150 EXPECT_EQ(memset_s(boot.update, sizeof(boot.update), 0, sizeof(boot.update)), 0);
151 const std::string command4 = "--factory_wipe_data";
152 EXPECT_EQ(strncpy_s(boot.update, sizeof(boot.update) - 1, command4.c_str(), command4.size()), 0);
153 ret = WriteUpdaterMessage(MISC_FILE, boot);
154 EXPECT_EQ(ret, true);
155 lRet = UpdaterMain(argc, argv);
156 EXPECT_EQ(lRet, 0);
157
158 ret = ReadUpdaterMessage(MISC_FILE, boot);
159 EXPECT_EQ(ret, true);
160 EXPECT_STREQ(boot.update, "");
161 delete argv[0];
162 delete []argv;
163 DeleteView();
164 }
165
166 HWTEST_F(UpdaterMainUnitTest, updater_main_test_compress, TestSize.Level1)
167 {
168 const std::string testFile = "/data/sdcard/updater/test_compress.txt";
169 FILE *fp = fopen(testFile.c_str(), "w");
170 ASSERT_NE(fp, nullptr);
171 fclose(fp);
172 CompressLogs(testFile);
173 }
174 } // namespace updater_ut
175