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 "update_bin/bin_flow_update.h"
20 #include "update_bin/bin_process.h"
21 #include "log.h"
22 
23 using namespace testing::ext;
24 using namespace Hpackage;
25 using namespace Updater;
26 
27 namespace OHOS {
28 constexpr const char *PKG_PATH = "/data/updater/package/update.bin";
29 constexpr  int MAX_LOG_BUF_SIZE = 1 * 1024 * 1024;
30 constexpr  int BUF_SIZE = 4 * 1024 * 1024;
31 class BinFlowUpdateTest : public testing::Test {
32 public:
SetUpTestCase(void)33     static void SetUpTestCase(void) {}
TearDownTestCase(void)34     static void TearDownTestCase(void) {}
SetUp()35     void SetUp()
36     {
37         InitUpdaterLogger("UPDATER", "updater_log.log", "updater_status.log", "error_code.log");
38     }
TearDown()39     void TearDown() {}
TestBody()40     void TestBody() {}
41 
TestBinFlowUpdater()42     int TestBinFlowUpdater()
43     {
44         LOG(INFO) << "TestBinFlowUpdater start";
45         std::string packagePath = "/data/updater/package/updater_flow.zip";
46         PkgManager::PkgManagerPtr pkgManager = PkgManager::CreatePackageInstance();
47         if (pkgManager == nullptr) {
48             LOG(ERROR) << "pkgManager is nullptr";
49             return -1;
50         }
51 
52         std::vector<std::string> components;
53         int32_t ret = pkgManager->LoadPackage(packagePath, Utils::GetCertName(), components);
54         if (ret != PKG_SUCCESS) {
55             LOG(ERROR) << "Fail to load package";
56             PkgManager::ReleasePackageInstance(pkgManager);
57             return -1;
58         }
59 
60         ret = Updater::ExecUpdate(pkgManager, false, packagePath,
61             [](const char *cmd, const char *content) {
62                 LOG(INFO) << "pip msg, " << cmd << ":" << content;
63             });
64         PkgManager::ReleasePackageInstance(pkgManager);
65         return ret;
66     }
67 };
68 
69 HWTEST_F(BinFlowUpdateTest, binFlowUpdateTest01, TestSize.Level0)
70 {
71     std::cout << "binFlowUpdateTest01 start\n";
72     FILE* fp = fopen(PKG_PATH, "rb");
73     if (fp == nullptr) {
74         std::cout << "fopen /data/updater/package/update.bin failed" << " : " << strerror(errno);
75     }
76     EXPECT_NE(fp, nullptr);
77 
78     uint8_t buf[MAX_LOG_BUF_SIZE] {};
79     size_t len;
80     int ret = 0;
81     BinFlowUpdate binFlowUpdate(BUF_SIZE);
82     while ((len = fread(buf, 1, sizeof(buf), fp)) != 0) {
83         ret = binFlowUpdate.StartBinFlowUpdate(buf, len);
84         if (ret != 0) {
85             break;
86         }
87     }
88 
89     EXPECT_EQ(ret, 0);
90     std::cout << "binFlowUpdateTest01 end\n";
91 }
92 
93 HWTEST_F(BinFlowUpdateTest, TestBinFlowUpdater, TestSize.Level0)
94 {
95     BinFlowUpdateTest test;
96     EXPECT_EQ(0, test.TestBinFlowUpdater());
97 }
98 }  // namespace OHOS
99