1fb299fa2Sopenharmony_ci/*
2fb299fa2Sopenharmony_ci* Copyright (c) 2021 Huawei Device Co., Ltd.
3fb299fa2Sopenharmony_ci* Licensed under the Apache License, Version 2.0 (the "License");
4fb299fa2Sopenharmony_ci* you may not use this file except in compliance with the License.
5fb299fa2Sopenharmony_ci* You may obtain a copy of the License at
6fb299fa2Sopenharmony_ci*
7fb299fa2Sopenharmony_ci*     http://www.apache.org/licenses/LICENSE-2.0
8fb299fa2Sopenharmony_ci*
9fb299fa2Sopenharmony_ci* Unless required by applicable law or agreed to in writing, software
10fb299fa2Sopenharmony_ci* distributed under the License is distributed on an "AS IS" BASIS,
11fb299fa2Sopenharmony_ci* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fb299fa2Sopenharmony_ci* See the License for the specific language governing permissions and
13fb299fa2Sopenharmony_ci* limitations under the License.
14fb299fa2Sopenharmony_ci*/
15fb299fa2Sopenharmony_ci
16fb299fa2Sopenharmony_ci#include "updaterkits_unittest.h"
17fb299fa2Sopenharmony_ci#include <iostream>
18fb299fa2Sopenharmony_ci#include <unistd.h>
19fb299fa2Sopenharmony_ci#include "securec.h"
20fb299fa2Sopenharmony_ci#include "updaterkits/updaterkits.h"
21fb299fa2Sopenharmony_ci
22fb299fa2Sopenharmony_ciusing namespace testing::ext;
23fb299fa2Sopenharmony_ciusing namespace UpdaterUt;
24fb299fa2Sopenharmony_ciusing namespace std;
25fb299fa2Sopenharmony_ci
26fb299fa2Sopenharmony_cinamespace UpdaterUt {
27fb299fa2Sopenharmony_ciconst std::string MISC_FILE = "/data/updater/misc_ut";
28fb299fa2Sopenharmony_ci
29fb299fa2Sopenharmony_civoid UpdaterKitsUnitTest::SetUpTestCase(void)
30fb299fa2Sopenharmony_ci{
31fb299fa2Sopenharmony_ci    cout << "Updater Unit UpdaterKitsUnitTest Begin!" << endl;
32fb299fa2Sopenharmony_ci}
33fb299fa2Sopenharmony_ci
34fb299fa2Sopenharmony_civoid UpdaterKitsUnitTest::TearDownTestCase(void)
35fb299fa2Sopenharmony_ci{
36fb299fa2Sopenharmony_ci    cout << "Updater Unit UpdaterKitsUnitTest End!" << endl;
37fb299fa2Sopenharmony_ci}
38fb299fa2Sopenharmony_ci
39fb299fa2Sopenharmony_ciHWTEST_F(UpdaterKitsUnitTest, updater_kits_test01, TestSize.Level1)
40fb299fa2Sopenharmony_ci{
41fb299fa2Sopenharmony_ci    const std::vector<std::string> packageName1 = {""};
42fb299fa2Sopenharmony_ci    int ret = RebootAndInstallUpgradePackage(MISC_FILE, packageName1);
43fb299fa2Sopenharmony_ci    EXPECT_EQ(ret, 2); // 2 : path not exit
44fb299fa2Sopenharmony_ci
45fb299fa2Sopenharmony_ci    const std::vector<std::string> packageName2 = {"/data/updater/updater/updater_without_updater_binary.zip"};
46fb299fa2Sopenharmony_ci    auto fp = std::unique_ptr<FILE, decltype(&fclose)>(fopen(MISC_FILE.c_str(), "wb+"), fclose);
47fb299fa2Sopenharmony_ci    EXPECT_NE(fp, nullptr);
48fb299fa2Sopenharmony_ci    ret = RebootAndInstallUpgradePackage(MISC_FILE, packageName2);
49fb299fa2Sopenharmony_ci    EXPECT_EQ(ret, 0);
50fb299fa2Sopenharmony_ci    ret = RebootAndInstallUpgradePackage(MISC_FILE, packageName2, UPGRADE_TYPE_SD);
51fb299fa2Sopenharmony_ci    EXPECT_EQ(ret, 0);
52fb299fa2Sopenharmony_ci    ret = RebootAndInstallUpgradePackage(MISC_FILE, packageName2, UPGRADE_TYPE_SD_INTRAL);
53fb299fa2Sopenharmony_ci    EXPECT_EQ(ret, 0);
54fb299fa2Sopenharmony_ci    unlink(MISC_FILE.c_str());
55fb299fa2Sopenharmony_ci}
56fb299fa2Sopenharmony_ci
57fb299fa2Sopenharmony_ciHWTEST_F(UpdaterKitsUnitTest, updater_kits_test02, TestSize.Level1)
58fb299fa2Sopenharmony_ci{
59fb299fa2Sopenharmony_ci    const std::string cmd1 = "";
60fb299fa2Sopenharmony_ci    bool ret = RebootAndCleanUserData(MISC_FILE, cmd1);
61fb299fa2Sopenharmony_ci    EXPECT_EQ(ret, false);
62fb299fa2Sopenharmony_ci
63fb299fa2Sopenharmony_ci    std::vector<std::string> pkgPath {};
64fb299fa2Sopenharmony_ci    ret = RebootAndInstallSdcardPackage(MISC_FILE, pkgPath);
65fb299fa2Sopenharmony_ci    EXPECT_EQ(ret, true);
66fb299fa2Sopenharmony_ci
67fb299fa2Sopenharmony_ci    std::string path = "data/sdcard/updater/updater.zip";
68fb299fa2Sopenharmony_ci    pkgPath.push_back(path);
69fb299fa2Sopenharmony_ci    ret = RebootAndInstallSdcardPackage(MISC_FILE, pkgPath);
70fb299fa2Sopenharmony_ci    EXPECT_EQ(ret, true);
71fb299fa2Sopenharmony_ci
72fb299fa2Sopenharmony_ci    const std::string cmd2 = "--user_wipe_data";
73fb299fa2Sopenharmony_ci    auto fp = std::unique_ptr<FILE, decltype(&fclose)>(fopen(MISC_FILE.c_str(), "wb+"), fclose);
74fb299fa2Sopenharmony_ci    EXPECT_NE(fp, nullptr);
75fb299fa2Sopenharmony_ci    ret = RebootAndCleanUserData(MISC_FILE, cmd2);
76fb299fa2Sopenharmony_ci    EXPECT_EQ(ret, true);
77fb299fa2Sopenharmony_ci    unlink(MISC_FILE.c_str());
78fb299fa2Sopenharmony_ci}
79fb299fa2Sopenharmony_ci} // namespace updater_ut
80