• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "misc_info_unittest.h"
17 #include <iostream>
18 #include <unistd.h>
19 #include "misc_info/misc_info.h"
20 #include "securec.h"
21 
22 using namespace testing::ext;
23 using namespace UpdaterUt;
24 using namespace Updater;
25 using namespace std;
26 
27 namespace UpdaterUt {
28 const std::string MISC_FILE = "/data/updater/misc_ut";
29 
SetUpTestCase(void)30 void MiscInfoUnitTest::SetUpTestCase(void)
31 {
32     cout << "Updater Unit MiscInfoUnitTest Begin!" << endl;
33 }
34 
TearDownTestCase(void)35 void MiscInfoUnitTest::TearDownTestCase(void)
36 {
37     cout << "Updater Unit MiscInfoUnitTest End!" << endl;
38 }
39 
40 HWTEST_F(MiscInfoUnitTest, misc_info_test_001, TestSize.Level1)
41 {
42     auto fp = std::unique_ptr<FILE, decltype(&fclose)>(fopen(MISC_FILE.c_str(), "wb"), fclose);
43     EXPECT_NE(fp, nullptr);
44 
45     UpdateMessage boot {};
46     const std::string command1 = "boot_updater";
47     EXPECT_EQ(strncpy_s(boot.command, sizeof(boot.command) - 1, command1.c_str(), command1.size()), 0);
48     const std::string command2 = "--update_package=./updater/xxx.zip\n--retry_count=1";
49     EXPECT_EQ(strncpy_s(boot.update, sizeof(boot.update) - 1, command2.c_str(), command2.size()), 0);
50     std::string path = "";
51     bool ret = WriteUpdaterMessage(path, boot);
52     EXPECT_EQ(ret, false);
53 
54     path = MISC_FILE;
55     ret = WriteUpdaterMessage(path, boot);
56     EXPECT_EQ(ret, true);
57 
58     path = "";
59     ret = ReadUpdaterMessage(path, boot);
60     EXPECT_EQ(ret, false);
61     unlink(path.c_str());
62 
63     path = MISC_FILE;
64     ret = ReadUpdaterMessage(path, boot);
65     EXPECT_EQ(ret, true);
66 
67     ret = WriteUpdaterMiscMsg(boot);
68     EXPECT_EQ(ret, true);
69 
70     ret = ReadUpdaterMiscMsg(boot);
71     EXPECT_EQ(ret, true);
72 }
73 } // namespace updater_ut
74