1 /* 2 * Copyright (c) 2023 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 <thread> 18 19 #include "bin_chunk_update.h" 20 #include "log.h" 21 22 using namespace testing::ext; 23 using namespace Hpackage; 24 using namespace Updater; 25 26 namespace OHOS { 27 constexpr const char *PKG_PATH = "/data/updater/package/update_stream.bin"; 28 constexpr uint32_t BUFFER_SIZE = 50 * 1024; 29 class BinChunkUpdateTest : public testing::Test { 30 public: SetUpTestCase(void)31 static void SetUpTestCase(void) {} TearDownTestCase(void)32 static void TearDownTestCase(void) {} SetUp()33 void SetUp() {} TearDown()34 void TearDown() {} 35 }; 36 37 HWTEST_F(BinChunkUpdateTest, binChunkUpdateTest01, TestSize.Level0) 38 { 39 LOG(INFO) << "binChunkUpdateTest01 start"; 40 uint32_t dealLen = 0; 41 //错误数据 42 uint8_t buffer[BUFFER_SIZE] = {0, 'a', 1, 'b', 2}; 43 BinChunkUpdate binChunkUpdate(2 * BUFFER_SIZE); 44 UpdateResultCode ret = binChunkUpdate.StartBinChunkUpdate(buffer, 5, dealLen); 45 EXPECT_EQ(ret, STREAM_UPDATE_FAILURE); 46 } 47 48 HWTEST_F(BinChunkUpdateTest, binChunkUpdateTest02, TestSize.Level0) 49 { 50 LOG(INFO) << "binChunkUpdateTest02 start"; 51 uint32_t dealLen = 0; 52 FILE* fp = fopen(PKG_PATH, "rb"); 53 if (fp == nullptr) { 54 std::cout << "fopen /data/updater/package/update_stream.bin failed" << " : " << strerror(errno); 55 ASSERT_NE(fp, nullptr) << "Failed to open file: " << PKG_PATH; 56 } 57 EXPECT_NE(fp, nullptr); 58 59 uint8_t buffer[BUFFER_SIZE]{0}; 60 size_t len; 61 int ret = 0; 62 BinChunkUpdate binChunkUpdate(2 * BUFFER_SIZE); 63 while ((len = fread(buffer, 1, sizeof(buffer), fp)) != 0) { 64 ret = binChunkUpdate.StartBinChunkUpdate(buffer, len, dealLen); 65 66 if (ret != STREAM_UPDATE_SUCCESS) { 67 break; 68 } 69 } 70 71 EXPECT_EQ(ret, STREAM_UPDATE_COMPLETE); 72 } 73 } // namespace OHOS 74