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 "updaterkits_unittest.h"
17#include <iostream>
18#include <unistd.h>
19#include "securec.h"
20#include "updaterkits/updaterkits.h"
21
22using namespace testing::ext;
23using namespace UpdaterUt;
24using namespace std;
25
26namespace UpdaterUt {
27const std::string MISC_FILE = "/data/updater/misc_ut";
28
29void UpdaterKitsUnitTest::SetUpTestCase(void)
30{
31    cout << "Updater Unit UpdaterKitsUnitTest Begin!" << endl;
32}
33
34void UpdaterKitsUnitTest::TearDownTestCase(void)
35{
36    cout << "Updater Unit UpdaterKitsUnitTest End!" << endl;
37}
38
39HWTEST_F(UpdaterKitsUnitTest, updater_kits_test01, TestSize.Level1)
40{
41    const std::vector<std::string> packageName1 = {""};
42    int ret = RebootAndInstallUpgradePackage(MISC_FILE, packageName1);
43    EXPECT_EQ(ret, 2); // 2 : path not exit
44
45    const std::vector<std::string> packageName2 = {"/data/updater/updater/updater_without_updater_binary.zip"};
46    auto fp = std::unique_ptr<FILE, decltype(&fclose)>(fopen(MISC_FILE.c_str(), "wb+"), fclose);
47    EXPECT_NE(fp, nullptr);
48    ret = RebootAndInstallUpgradePackage(MISC_FILE, packageName2);
49    EXPECT_EQ(ret, 0);
50    ret = RebootAndInstallUpgradePackage(MISC_FILE, packageName2, UPGRADE_TYPE_SD);
51    EXPECT_EQ(ret, 0);
52    ret = RebootAndInstallUpgradePackage(MISC_FILE, packageName2, UPGRADE_TYPE_SD_INTRAL);
53    EXPECT_EQ(ret, 0);
54    unlink(MISC_FILE.c_str());
55}
56
57HWTEST_F(UpdaterKitsUnitTest, updater_kits_test02, TestSize.Level1)
58{
59    const std::string cmd1 = "";
60    bool ret = RebootAndCleanUserData(MISC_FILE, cmd1);
61    EXPECT_EQ(ret, false);
62
63    std::vector<std::string> pkgPath {};
64    ret = RebootAndInstallSdcardPackage(MISC_FILE, pkgPath);
65    EXPECT_EQ(ret, true);
66
67    std::string path = "data/sdcard/updater/updater.zip";
68    pkgPath.push_back(path);
69    ret = RebootAndInstallSdcardPackage(MISC_FILE, pkgPath);
70    EXPECT_EQ(ret, true);
71
72    const std::string cmd2 = "--user_wipe_data";
73    auto fp = std::unique_ptr<FILE, decltype(&fclose)>(fopen(MISC_FILE.c_str(), "wb+"), fclose);
74    EXPECT_NE(fp, nullptr);
75    ret = RebootAndCleanUserData(MISC_FILE, cmd2);
76    EXPECT_EQ(ret, true);
77    unlink(MISC_FILE.c_str());
78}
79} // namespace updater_ut
80