1fb299fa2Sopenharmony_ci/*
2fb299fa2Sopenharmony_ci * Copyright (c) 2022 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 <gtest/gtest.h>
17fb299fa2Sopenharmony_ci#include "ptable_manager.h"
18fb299fa2Sopenharmony_ci#include "ptable.h"
19fb299fa2Sopenharmony_ci
20fb299fa2Sopenharmony_ciusing namespace Updater;
21fb299fa2Sopenharmony_ciusing namespace testing;
22fb299fa2Sopenharmony_ciusing namespace testing::ext;
23fb299fa2Sopenharmony_ci
24fb299fa2Sopenharmony_cinamespace {
25fb299fa2Sopenharmony_ciclass PtableTest : public Ptable {
26fb299fa2Sopenharmony_cipublic:
27fb299fa2Sopenharmony_ci    PtableTest() {}
28fb299fa2Sopenharmony_ci
29fb299fa2Sopenharmony_ci    ~PtableTest() {}
30fb299fa2Sopenharmony_ci
31fb299fa2Sopenharmony_ci    bool ParsePartitionFromBuffer(uint8_t *ptbImgBuffer, const uint32_t imgBufSize) override
32fb299fa2Sopenharmony_ci    {
33fb299fa2Sopenharmony_ci        return true;
34fb299fa2Sopenharmony_ci    }
35fb299fa2Sopenharmony_ci
36fb299fa2Sopenharmony_ci    bool LoadPtableFromDevice() override
37fb299fa2Sopenharmony_ci    {
38fb299fa2Sopenharmony_ci        return true;
39fb299fa2Sopenharmony_ci    }
40fb299fa2Sopenharmony_ci
41fb299fa2Sopenharmony_ci    bool WritePartitionTable() override
42fb299fa2Sopenharmony_ci    {
43fb299fa2Sopenharmony_ci        return true;
44fb299fa2Sopenharmony_ci    }
45fb299fa2Sopenharmony_ci
46fb299fa2Sopenharmony_ci    bool TestGetPartionInfoByName(const std::string &partitionName, PtnInfo &ptnInfo, int32_t &index)
47fb299fa2Sopenharmony_ci    {
48fb299fa2Sopenharmony_ci        return GetPartionInfoByName(partitionName, ptnInfo, index);
49fb299fa2Sopenharmony_ci    }
50fb299fa2Sopenharmony_ci
51fb299fa2Sopenharmony_ci    int TestCalculateCrc32(const uint8_t *buffer, const uint32_t len)
52fb299fa2Sopenharmony_ci    {
53fb299fa2Sopenharmony_ci        return static_cast<int>(CalculateCrc32(buffer, len));
54fb299fa2Sopenharmony_ci    }
55fb299fa2Sopenharmony_ci
56fb299fa2Sopenharmony_ci    void ChangePartitionInfo(std::vector<PtnInfo> partitionInfo)
57fb299fa2Sopenharmony_ci    {
58fb299fa2Sopenharmony_ci        partitionInfo_ = partitionInfo;
59fb299fa2Sopenharmony_ci    }
60fb299fa2Sopenharmony_ci};
61fb299fa2Sopenharmony_ci
62fb299fa2Sopenharmony_ciclass UTestPtable : public ::testing::Test {
63fb299fa2Sopenharmony_cipublic:
64fb299fa2Sopenharmony_ci    UTestPtable() {}
65fb299fa2Sopenharmony_ci
66fb299fa2Sopenharmony_ci    ~UTestPtable() = default;
67fb299fa2Sopenharmony_ci
68fb299fa2Sopenharmony_ci    void TestGetPartionInfoByName()
69fb299fa2Sopenharmony_ci    {
70fb299fa2Sopenharmony_ci        PtableTest ptableTest {};
71fb299fa2Sopenharmony_ci        std::string partionName = "";
72fb299fa2Sopenharmony_ci        int32_t index = 0;
73fb299fa2Sopenharmony_ci        PtableTest::PtnInfo ptnInfo;
74fb299fa2Sopenharmony_ci        bool ret = ptableTest.TestGetPartionInfoByName(partionName, ptnInfo, index);
75fb299fa2Sopenharmony_ci        ASSERT_EQ(ret, false);
76fb299fa2Sopenharmony_ci        std::vector<PtableTest::PtnInfo> partionInfo;
77fb299fa2Sopenharmony_ci        ptnInfo.dispName = "updater";
78fb299fa2Sopenharmony_ci        partionInfo.push_back(ptnInfo);
79fb299fa2Sopenharmony_ci        ptableTest.ChangePartitionInfo(partionInfo);
80fb299fa2Sopenharmony_ci        ret = ptableTest.TestGetPartionInfoByName(partionName, ptnInfo, index);
81fb299fa2Sopenharmony_ci        ASSERT_EQ(ret, false);
82fb299fa2Sopenharmony_ci        partionName = "updater";
83fb299fa2Sopenharmony_ci        ret = ptableTest.TestGetPartionInfoByName(partionName, ptnInfo, index);
84fb299fa2Sopenharmony_ci        ASSERT_NE(ret, false);
85fb299fa2Sopenharmony_ci    }
86fb299fa2Sopenharmony_ci
87fb299fa2Sopenharmony_ci    void TestCalculateCrc32()
88fb299fa2Sopenharmony_ci    {
89fb299fa2Sopenharmony_ci        PtableTest ptableTest {};
90fb299fa2Sopenharmony_ci        uint8_t buffer[128] = {1}; // 128 : set buffer size
91fb299fa2Sopenharmony_ci        uint8_t *nullBuffer = nullptr;
92fb299fa2Sopenharmony_ci        uint32_t len = 0;
93fb299fa2Sopenharmony_ci        int ret = ptableTest.TestCalculateCrc32(buffer, len);
94fb299fa2Sopenharmony_ci        EXPECT_EQ(ret, 0);
95fb299fa2Sopenharmony_ci        ret = ptableTest.TestCalculateCrc32(nullBuffer, len);
96fb299fa2Sopenharmony_ci        EXPECT_EQ(ret, 0);
97fb299fa2Sopenharmony_ci        len = 1;
98fb299fa2Sopenharmony_ci        ret = ptableTest.TestCalculateCrc32(nullBuffer, len);
99fb299fa2Sopenharmony_ci        EXPECT_EQ(ret, 0);
100fb299fa2Sopenharmony_ci        ret = ptableTest.TestCalculateCrc32(buffer, len);
101fb299fa2Sopenharmony_ci        EXPECT_EQ(ret, -1526341861); // -1526341861 : CalculateCrc32's result;
102fb299fa2Sopenharmony_ci    }
103fb299fa2Sopenharmony_ciprotected:
104fb299fa2Sopenharmony_ci    void SetUp() {}
105fb299fa2Sopenharmony_ci    void TearDown() {}
106fb299fa2Sopenharmony_ci    void TestBody() {}
107fb299fa2Sopenharmony_ci};
108fb299fa2Sopenharmony_ci
109fb299fa2Sopenharmony_ciHWTEST_F(UTestPtable, TestGetPartionInfoByName, TestSize.Level1)
110fb299fa2Sopenharmony_ci{
111fb299fa2Sopenharmony_ci    UTestPtable {}.TestGetPartionInfoByName();
112fb299fa2Sopenharmony_ci}
113fb299fa2Sopenharmony_ci
114fb299fa2Sopenharmony_ciHWTEST_F(UTestPtable, TestCalculateCrc32, TestSize.Level1)
115fb299fa2Sopenharmony_ci{
116fb299fa2Sopenharmony_ci    UTestPtable {}.TestCalculateCrc32();
117fb299fa2Sopenharmony_ci}
118fb299fa2Sopenharmony_ci}