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 "applypatch_unittest.h" 17fb299fa2Sopenharmony_ci#include <cerrno> 18fb299fa2Sopenharmony_ci#include <cstdio> 19fb299fa2Sopenharmony_ci#include <fcntl.h> 20fb299fa2Sopenharmony_ci#include <iostream> 21fb299fa2Sopenharmony_ci#include <libgen.h> 22fb299fa2Sopenharmony_ci#include <sys/mman.h> 23fb299fa2Sopenharmony_ci#include <sys/mount.h> 24fb299fa2Sopenharmony_ci#include <sys/stat.h> 25fb299fa2Sopenharmony_ci#include <unistd.h> 26fb299fa2Sopenharmony_ci#include <vector> 27fb299fa2Sopenharmony_ci#include "applypatch/data_writer.h" 28fb299fa2Sopenharmony_ci#include "fs_manager/mount.h" 29fb299fa2Sopenharmony_ci#include "log/log.h" 30fb299fa2Sopenharmony_ci#include "securec.h" 31fb299fa2Sopenharmony_ci#include "unittest_comm.h" 32fb299fa2Sopenharmony_ci#include "utils.h" 33fb299fa2Sopenharmony_ci 34fb299fa2Sopenharmony_cinamespace UpdaterUt { 35fb299fa2Sopenharmony_ciusing namespace testing::ext; 36fb299fa2Sopenharmony_ciusing namespace Updater; 37fb299fa2Sopenharmony_ciusing namespace std; 38fb299fa2Sopenharmony_ciusing namespace testing; 39fb299fa2Sopenharmony_ciconstexpr unsigned int BUFFER_LEN = 12; 40fb299fa2Sopenharmony_ci 41fb299fa2Sopenharmony_civoid ApplyPatchUnitTest::SetUp() 42fb299fa2Sopenharmony_ci{ 43fb299fa2Sopenharmony_ci unsigned long mountFlag = MS_REMOUNT; 44fb299fa2Sopenharmony_ci std::string tmpPath = "/tmp"; 45fb299fa2Sopenharmony_ci // mount rootfs to read-write. 46fb299fa2Sopenharmony_ci std::string rootSource = "/dev/root"; 47fb299fa2Sopenharmony_ci if (mount(rootSource.c_str(), "/", "ext4", mountFlag, nullptr) != 0) { 48fb299fa2Sopenharmony_ci std::cout << "Cannot re-mount rootfs\n"; 49fb299fa2Sopenharmony_ci } 50fb299fa2Sopenharmony_ci auto ret = mkdir(tmpPath.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); 51fb299fa2Sopenharmony_ci if (ret != 0) { 52fb299fa2Sopenharmony_ci std::cout << "Cannot create \"/tmp\" directory: " << errno << "\n"; 53fb299fa2Sopenharmony_ci } 54fb299fa2Sopenharmony_ci 55fb299fa2Sopenharmony_ci // Load specific fstab for testing. 56fb299fa2Sopenharmony_ci LoadSpecificFstab("/data/updater/applypatch/etc/fstab.ut.updater"); 57fb299fa2Sopenharmony_ci} 58fb299fa2Sopenharmony_ci 59fb299fa2Sopenharmony_civoid ApplyPatchUnitTest::TearDown() 60fb299fa2Sopenharmony_ci{ 61fb299fa2Sopenharmony_ci std::string partitionName = "/rawwriter"; 62fb299fa2Sopenharmony_ci auto devPath = GetBlockDeviceByMountPoint(partitionName); 63fb299fa2Sopenharmony_ci unlink(devPath.c_str()); 64fb299fa2Sopenharmony_ci} 65fb299fa2Sopenharmony_ci 66fb299fa2Sopenharmony_ciHWTEST_F(ApplyPatchUnitTest, updater_RawWriter, TestSize.Level1) 67fb299fa2Sopenharmony_ci{ 68fb299fa2Sopenharmony_ci WriteMode mode = WRITE_RAW; 69fb299fa2Sopenharmony_ci uint8_t *addr = nullptr; 70fb299fa2Sopenharmony_ci uint8_t buf[BUFFER_LEN + 1] = {0}; 71fb299fa2Sopenharmony_ci 72fb299fa2Sopenharmony_ci std::string partitionName = "/rawwriter"; 73fb299fa2Sopenharmony_ci std::string filePath = "/data/updater/ut/datawriter/rawwrite"; 74fb299fa2Sopenharmony_ci std::unique_ptr<DataWriter> writer = DataWriter::CreateDataWriter(mode, filePath); 75fb299fa2Sopenharmony_ci EXPECT_NE(writer, nullptr); 76fb299fa2Sopenharmony_ci bool ret = writer->Write(addr, 0, nullptr); 77fb299fa2Sopenharmony_ci EXPECT_FALSE(ret); 78fb299fa2Sopenharmony_ci 79fb299fa2Sopenharmony_ci addr = buf; 80fb299fa2Sopenharmony_ci ret = writer->Write(addr, 0, nullptr); 81fb299fa2Sopenharmony_ci EXPECT_FALSE(ret); 82fb299fa2Sopenharmony_ci 83fb299fa2Sopenharmony_ci ret = writer->Write(buf, BUFFER_LEN, nullptr); 84fb299fa2Sopenharmony_ci EXPECT_FALSE(ret); 85fb299fa2Sopenharmony_ci 86fb299fa2Sopenharmony_ci int mRet = memcpy_s(buf, BUFFER_LEN, "hello, world", BUFFER_LEN); 87fb299fa2Sopenharmony_ci EXPECT_EQ(mRet, 0); 88fb299fa2Sopenharmony_ci auto devPath = GetBlockDeviceByMountPoint(partitionName); 89fb299fa2Sopenharmony_ci const std::string devDir = "/data/updater/ut/datawriter"; 90fb299fa2Sopenharmony_ci Updater::Utils::MkdirRecursive(devDir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); 91fb299fa2Sopenharmony_ci close(open(devPath.c_str(), O_CREAT | O_WRONLY | O_EXCL, 0664)); 92fb299fa2Sopenharmony_ci ret = writer->Write(buf, BUFFER_LEN, nullptr); 93fb299fa2Sopenharmony_ci EXPECT_TRUE(ret); 94fb299fa2Sopenharmony_ci 95fb299fa2Sopenharmony_ci int fd = open(devPath.c_str(), O_RDONLY); 96fb299fa2Sopenharmony_ci EXPECT_GE(fd, 0); 97fb299fa2Sopenharmony_ci 98fb299fa2Sopenharmony_ci uint8_t buffer[BUFFER_LEN + 1] = {0}; 99fb299fa2Sopenharmony_ci size_t n = read(fd, buffer, BUFFER_LEN); 100fb299fa2Sopenharmony_ci EXPECT_EQ(n, BUFFER_LEN); 101fb299fa2Sopenharmony_ci 102fb299fa2Sopenharmony_ci auto result = memcmp(buf, buffer, BUFFER_LEN); 103fb299fa2Sopenharmony_ci EXPECT_EQ(result, 0); 104fb299fa2Sopenharmony_ci DataWriter::ReleaseDataWriter(writer); 105fb299fa2Sopenharmony_ci} 106fb299fa2Sopenharmony_ci 107fb299fa2Sopenharmony_ciHWTEST_F(ApplyPatchUnitTest, updater_CreateDataWriter, TestSize.Level1) 108fb299fa2Sopenharmony_ci{ 109fb299fa2Sopenharmony_ci std::vector<WriteMode> modes = { WRITE_RAW, WRITE_DECRYPT }; 110fb299fa2Sopenharmony_ci std::unique_ptr<DataWriter> writer = nullptr; 111fb299fa2Sopenharmony_ci for (auto mode : modes) { 112fb299fa2Sopenharmony_ci if (mode == WRITE_DECRYPT) { 113fb299fa2Sopenharmony_ci EXPECT_EQ(writer, nullptr); 114fb299fa2Sopenharmony_ci continue; 115fb299fa2Sopenharmony_ci } 116fb299fa2Sopenharmony_ci writer = DataWriter::CreateDataWriter(mode, "", DataWriter::GetUpdaterEnv()); 117fb299fa2Sopenharmony_ci EXPECT_NE(writer, nullptr); 118fb299fa2Sopenharmony_ci DataWriter::ReleaseDataWriter(writer); 119fb299fa2Sopenharmony_ci writer = nullptr; 120fb299fa2Sopenharmony_ci } 121fb299fa2Sopenharmony_ci} 122fb299fa2Sopenharmony_ci} // namespace updater_ut 123