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 <fcntl.h>
17 #include <gmock/gmock.h>
18 #include <gtest/gtest.h>
19 #include <iostream>
20 #include <string>
21 #include "applypatch/partition_record.h"
22 #include "fs_manager/mount.h"
23 #include "log/log.h"
24 #include "misc_info/misc_info.h"
25 #include "package/pkg_manager.h"
26 #include "updater/updater.h"
27 #include "utils.h"
28
29 using namespace testing::ext;
30 using namespace Updater;
31 using namespace std;
32 namespace UpdaterUt {
33 class PartitionUpdateRecordUnitTest : public testing::Test {
34 public:
35 static void SetUpTestCase(void);
TearDownTestCase(void)36 static void TearDownTestCase(void) {};
37 void SetUp();
38 void TearDown();
39 };
40
SetUpTestCase()41 void PartitionUpdateRecordUnitTest::SetUpTestCase()
42 {
43 LoadSpecificFstab("/data/updater/applypatch/etc/partition.tab");
44 }
45
SetUp()46 void PartitionUpdateRecordUnitTest::SetUp()
47 {
48 PartitionRecord::GetInstance().ClearRecordPartitionOffset();
49 }
50
TearDown()51 void PartitionUpdateRecordUnitTest::TearDown()
52 {
53 PartitionRecord::GetInstance().ClearRecordPartitionOffset();
54 }
55
56 HWTEST_F(PartitionUpdateRecordUnitTest, partition_record_test_001, TestSize.Level1)
57 {
58 mode_t dirMode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
59 const int bufferLen = 4096;
60 std::vector<uint8_t> buffer(bufferLen);
61 const std::string miscDir = "/data/updater/applypatch";
62 Updater::Utils::MkdirRecursive(miscDir, dirMode);
63 std::string filePath = "/data/updater/applypatch/misc_test";
64 int fd = open(filePath.c_str(), O_RDWR | O_CREAT, dirMode);
65 if (fd < 0) {
66 printf("Open file failed");
67 return;
68 }
69 const std::string partitionName = "ut_partition";
70 std::fill(buffer.begin(), buffer.end(), 0);
71 write(fd, buffer.data(), bufferLen);
72 close(fd);
73 bool ret = PartitionRecord::GetInstance().RecordPartitionUpdateStatus(partitionName, true);
74 EXPECT_EQ(ret, true);
75
76 ret = PartitionRecord::GetInstance().IsPartitionUpdated(partitionName);
77 EXPECT_EQ(ret, true);
78 }
79
80 HWTEST_F(PartitionUpdateRecordUnitTest, partition_record_test_002, TestSize.Level1)
81 {
82 const std::string partitionName = "ut_partition1";
83 bool ret = PartitionRecord::GetInstance().IsPartitionUpdated(partitionName);
84 EXPECT_EQ(ret, false);
85 }
86
87 HWTEST_F(PartitionUpdateRecordUnitTest, partition_record_test_003, TestSize.Level1)
88 {
89 string partitionName = "partitionName";
90 for (int i = 0; i < MAX_PARTITION_NUM; i++) {
91 bool ret = PartitionRecord::GetInstance().RecordPartitionUpdateStatus(partitionName, true);
92 EXPECT_EQ(ret, true);
93 ret = PartitionRecord::GetInstance().IsPartitionUpdated(partitionName);
94 EXPECT_EQ(ret, true);
95 partitionName += "a";
96 }
97 bool ret = PartitionRecord::GetInstance().RecordPartitionUpdateStatus(partitionName, true);
98 EXPECT_EQ(ret, false);
99 }
100 } // updater_ut
101