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 "updater_unittest.h" 17fb299fa2Sopenharmony_ci#include <cerrno> 18fb299fa2Sopenharmony_ci#include <cstdio> 19fb299fa2Sopenharmony_ci#include <iostream> 20fb299fa2Sopenharmony_ci#include <sys/mount.h> 21fb299fa2Sopenharmony_ci#include <unistd.h> 22fb299fa2Sopenharmony_ci#include "fs_manager/mount.h" 23fb299fa2Sopenharmony_ci#include "log/log.h" 24fb299fa2Sopenharmony_ci#include "package/pkg_manager.h" 25fb299fa2Sopenharmony_ci#include "unittest_comm.h" 26fb299fa2Sopenharmony_ci#include "updater/updater.h" 27fb299fa2Sopenharmony_ci#include "updater_main.h" 28fb299fa2Sopenharmony_ci#include "updater_ui.h" 29fb299fa2Sopenharmony_ci#include "utils.h" 30fb299fa2Sopenharmony_ci 31fb299fa2Sopenharmony_cinamespace UpdaterUt { 32fb299fa2Sopenharmony_ciusing namespace testing::ext; 33fb299fa2Sopenharmony_ciusing namespace UpdaterUt; 34fb299fa2Sopenharmony_ciusing namespace Updater; 35fb299fa2Sopenharmony_ciusing namespace std; 36fb299fa2Sopenharmony_ciusing namespace Hpackage; 37fb299fa2Sopenharmony_ciusing namespace testing; 38fb299fa2Sopenharmony_ci 39fb299fa2Sopenharmony_civoid UpdaterUnitTest::SetUp() 40fb299fa2Sopenharmony_ci{ 41fb299fa2Sopenharmony_ci unsigned long mountFlag = MS_REMOUNT; 42fb299fa2Sopenharmony_ci std::string tmpPath = "/tmp"; 43fb299fa2Sopenharmony_ci // mount rootfs to read-write. 44fb299fa2Sopenharmony_ci std::string rootSource = "/dev/root"; 45fb299fa2Sopenharmony_ci if (mount(rootSource.c_str(), "/", "ext4", mountFlag, nullptr) != 0) { 46fb299fa2Sopenharmony_ci std::cout << "Cannot re-mount rootfs\n"; 47fb299fa2Sopenharmony_ci } 48fb299fa2Sopenharmony_ci mode_t mode = (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); 49fb299fa2Sopenharmony_ci auto ret = mkdir(tmpPath.c_str(), mode); 50fb299fa2Sopenharmony_ci if (ret != 0 && errno != EEXIST) { 51fb299fa2Sopenharmony_ci std::cout << "Cannot create \"/tmp\" directory: " << errno << "\n"; 52fb299fa2Sopenharmony_ci } 53fb299fa2Sopenharmony_ci 54fb299fa2Sopenharmony_ci // Load specific fstab for testing. 55fb299fa2Sopenharmony_ci LoadSpecificFstab("/data/updater/updater/etc/fstab.ut.updater"); 56fb299fa2Sopenharmony_ci} 57fb299fa2Sopenharmony_ci 58fb299fa2Sopenharmony_civoid UpdaterUnitTest::TearDown() {} 59fb299fa2Sopenharmony_ci 60fb299fa2Sopenharmony_civoid UpdaterUnitTest::SetUpTestCase() 61fb299fa2Sopenharmony_ci{ 62fb299fa2Sopenharmony_ci UpdaterUiInit(); 63fb299fa2Sopenharmony_ci} 64fb299fa2Sopenharmony_ci 65fb299fa2Sopenharmony_civoid UpdaterUnitTest::TearDownTestCase() 66fb299fa2Sopenharmony_ci{ 67fb299fa2Sopenharmony_ci DeleteView(); 68fb299fa2Sopenharmony_ci} 69fb299fa2Sopenharmony_ci 70fb299fa2Sopenharmony_ciHWTEST_F(UpdaterUnitTest, updater_StartUpdaterProc, TestSize.Level1) 71fb299fa2Sopenharmony_ci{ 72fb299fa2Sopenharmony_ci std::string packagePath = "/data/updater/updater/updater_without_updater_binary.zip"; 73fb299fa2Sopenharmony_ci PkgManager::PkgManagerPtr pkgManager = PkgManager::CreatePackageInstance(); 74fb299fa2Sopenharmony_ci UpdaterStatus status; 75fb299fa2Sopenharmony_ci status = StartUpdaterProc(pkgManager, packagePath, 0); 76fb299fa2Sopenharmony_ci EXPECT_EQ(status, UPDATE_CORRUPT); 77fb299fa2Sopenharmony_ci 78fb299fa2Sopenharmony_ci packagePath = "/data/updater/updater/updater_with_incorrect_binary.zip"; 79fb299fa2Sopenharmony_ci status = StartUpdaterProc(pkgManager, packagePath, 0); 80fb299fa2Sopenharmony_ci EXPECT_EQ(status, UPDATE_CORRUPT); 81fb299fa2Sopenharmony_ci 82fb299fa2Sopenharmony_ci packagePath = "/data/updater/updater/updater.zip"; 83fb299fa2Sopenharmony_ci std::vector<std::string> components; 84fb299fa2Sopenharmony_ci int32_t ret = pkgManager->LoadPackage(packagePath, GetTestCertName(), components); 85fb299fa2Sopenharmony_ci EXPECT_EQ(ret, 0); 86fb299fa2Sopenharmony_ci status = StartUpdaterProc(pkgManager, packagePath, 0); 87fb299fa2Sopenharmony_ci EXPECT_EQ(status, UPDATE_SUCCESS); 88fb299fa2Sopenharmony_ci 89fb299fa2Sopenharmony_ci // retrycount is greater than 0. 90fb299fa2Sopenharmony_ci status = StartUpdaterProc(pkgManager, packagePath, 1); 91fb299fa2Sopenharmony_ci EXPECT_EQ(status, UPDATE_RETRY); 92fb299fa2Sopenharmony_ci 93fb299fa2Sopenharmony_ci packagePath = "/data/updater/updater/updater_binary_abnormal.zip"; 94fb299fa2Sopenharmony_ci status = StartUpdaterProc(pkgManager, packagePath, 1); 95fb299fa2Sopenharmony_ci PkgManager::ReleasePackageInstance(pkgManager); 96fb299fa2Sopenharmony_ci EXPECT_EQ(status, UPDATE_ERROR); 97fb299fa2Sopenharmony_ci} 98fb299fa2Sopenharmony_ci 99fb299fa2Sopenharmony_ciHWTEST_F(UpdaterUnitTest, updater_GetUpdatePackageInfo, TestSize.Level1) 100fb299fa2Sopenharmony_ci{ 101fb299fa2Sopenharmony_ci // Non-exist file. 102fb299fa2Sopenharmony_ci PkgManager::PkgManagerPtr pkgManager = PkgManager::CreatePackageInstance(); 103fb299fa2Sopenharmony_ci std::string nonExistPackagePath = "/data/non_exist"; 104fb299fa2Sopenharmony_ci int ret = GetUpdatePackageInfo(pkgManager, nonExistPackagePath); 105fb299fa2Sopenharmony_ci EXPECT_EQ(ret, static_cast<int>(PKG_INVALID_FILE)); 106fb299fa2Sopenharmony_ci 107fb299fa2Sopenharmony_ci // valid updater package. 108fb299fa2Sopenharmony_ci std::string validUpdaterPackage = "/data/updater/updater/updater.zip"; 109fb299fa2Sopenharmony_ci 110fb299fa2Sopenharmony_ci ret = GetUpdatePackageInfo(pkgManager, validUpdaterPackage); 111fb299fa2Sopenharmony_ci PkgManager::ReleasePackageInstance(pkgManager); 112fb299fa2Sopenharmony_ci EXPECT_EQ(ret, static_cast<int>(PKG_SUCCESS)); 113fb299fa2Sopenharmony_ci} 114fb299fa2Sopenharmony_ci 115fb299fa2Sopenharmony_ciHWTEST_F(UpdaterUnitTest, updater_UpdateSdcard, TestSize.Level1) 116fb299fa2Sopenharmony_ci{ 117fb299fa2Sopenharmony_ci UpdaterStatus status; 118fb299fa2Sopenharmony_ci UpdaterParams upParams; 119fb299fa2Sopenharmony_ci upParams.updateMode = SDCARD_UPDATE; 120fb299fa2Sopenharmony_ci status = UpdaterFromSdcard(upParams); 121fb299fa2Sopenharmony_ci EXPECT_EQ(status, UPDATE_SUCCESS); 122fb299fa2Sopenharmony_ci} 123fb299fa2Sopenharmony_ci} // namespace updater_ut 124