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