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 <vector> 18 #include "update_binary_unittest.h" 19 #include "script_context.h" 20 #include "update_image_patch.h" 21 #include "mount.h" 22 23 using namespace Uscript; 24 using namespace Updater; 25 using namespace testing::ext; 26 using namespace Hpackage; 27 28 namespace UpdaterUt { 29 class ImgPatchUnittest : public ::testing::Test { 30 public: ImgPatchUnittest()31 ImgPatchUnittest() {} ~ImgPatchUnittest()32 ~ImgPatchUnittest() {} 33 34 /* GetParam(): 脚本指令image_patch,参数个数为IMAGE_PATCH_CMD_LEN:6,入参为空 */ TestImgPatch01()35 void TestImgPatch01() 36 { 37 UTestBinaryEnv env {&pkgManager_}; 38 UScriptInstructionContext context {}; 39 auto instruction = std::make_unique<USInstrImagePatch>(); 40 EXPECT_EQ(instruction->Execute(env, context), USCRIPT_INVALID_PARAM); 41 std::vector<UScriptValuePtr> output = context.GetOutVar(); 42 EXPECT_EQ(output.size(), 1); 43 } 44 45 /* GetParam(): 指令入参参数格式不对 */ TestImgPatch02()46 void TestImgPatch02() 47 { 48 UTestBinaryEnv env {&pkgManager_}; 49 UScriptInstructionContext context {}; 50 context.AddInputParam(std::make_shared<IntegerValue>(1)); 51 context.AddInputParam(std::make_shared<IntegerValue>(1)); 52 context.AddInputParam(std::make_shared<IntegerValue>(1)); 53 context.AddInputParam(std::make_shared<IntegerValue>(1)); 54 context.AddInputParam(std::make_shared<IntegerValue>(1)); 55 context.AddInputParam(std::make_shared<StringValue>("/data/updater/patchfile")); 56 auto instruction = std::make_unique<USInstrImagePatch>(); 57 EXPECT_EQ(instruction->Execute(env, context), USCRIPT_INVALID_PARAM); 58 std::vector<UScriptValuePtr> output = context.GetOutVar(); 59 EXPECT_EQ(output.size(), 1); 60 } 61 62 /* GetParam(): 解析命令行参数成功 */ TestImgPatch04()63 void TestImgPatch04() 64 { 65 UTestBinaryEnv env {&pkgManager_}; 66 UScriptInstructionContext context {}; 67 context.AddInputParam(std::make_shared<StringValue>("/vendortest")); 68 context.AddInputParam(std::make_shared<StringValue>("srcSize")); 69 context.AddInputParam(std::make_shared<StringValue>("srcHash")); 70 context.AddInputParam(std::make_shared<StringValue>("destSize")); 71 context.AddInputParam(std::make_shared<StringValue>("destHash")); 72 context.AddInputParam(std::make_shared<StringValue>("/data/updater/patchfile")); 73 auto instruction = std::make_unique<USInstrImagePatch>(); 74 EXPECT_EQ(instruction->Execute(env, context), USCRIPT_ERROR_EXECUTE); 75 std::vector<UScriptValuePtr> output = context.GetOutVar(); 76 EXPECT_EQ(output.size(), 1); 77 } 78 79 /* GetPatchFile(): retry为true, 获取文件失败, 创建文件流失败 */ TestImgPatch05()80 void TestImgPatch05() 81 { 82 PkgManager::PkgManagerPtr pkgMgr = nullptr; 83 UTestBinaryEnv env1 {pkgMgr}; 84 env1.SetRetry(true); 85 UScriptInstructionContext context {}; 86 context.AddInputParam(std::make_shared<StringValue>("/vendortest")); 87 context.AddInputParam(std::make_shared<StringValue>("srcSize")); 88 context.AddInputParam(std::make_shared<StringValue>("srcHash")); 89 context.AddInputParam(std::make_shared<StringValue>("destSize")); 90 context.AddInputParam(std::make_shared<StringValue>("destHash")); 91 context.AddInputParam(std::make_shared<StringValue>("/data/updater/patchfile")); 92 auto instruction = std::make_unique<USInstrImagePatch>(); 93 EXPECT_EQ(instruction->Execute(env1, context), USCRIPT_ERROR_EXECUTE); 94 std::vector<UScriptValuePtr> output = context.GetOutVar(); 95 EXPECT_EQ(output.size(), 1); 96 97 UTestBinaryEnv env2(&pkgManager_); 98 EXPECT_EQ(instruction->Execute(env2, context), USCRIPT_ERROR_EXECUTE); 99 100 TestPkgMgrStream1 pkgStream1; 101 UTestBinaryEnv env3(&pkgStream1); 102 EXPECT_EQ(instruction->Execute(env3, context), USCRIPT_ERROR_EXECUTE); 103 104 TestPkgMgrStream2 pkgStream2; 105 UTestBinaryEnv env4(&pkgStream2); 106 EXPECT_EQ(instruction->Execute(env4, context), USCRIPT_ERROR_EXECUTE); 107 108 TestPkgMgrExtract1 pkgExtract1; 109 UTestBinaryEnv env5(&pkgExtract1); 110 EXPECT_EQ(instruction->Execute(env5, context), USCRIPT_ERROR_EXECUTE); 111 } 112 113 /* GetPatchFile(): 获取文件成功,patchfile=/data/udpater/binary */ TestImgPatch06()114 void TestImgPatch06() 115 { 116 UTestBinaryEnv env {&pkgManager_}; 117 UScriptInstructionContext context {}; 118 context.AddInputParam(std::make_shared<StringValue>("binary")); 119 context.AddInputParam(std::make_shared<StringValue>("srcSize")); 120 context.AddInputParam(std::make_shared<StringValue>("srcHash")); 121 context.AddInputParam(std::make_shared<StringValue>("destSize")); 122 context.AddInputParam(std::make_shared<StringValue>("destHash")); 123 context.AddInputParam(std::make_shared<StringValue>("/data/updater/patchfile")); 124 auto instruction = std::make_unique<USInstrImagePatch>(); 125 EXPECT_EQ(instruction->Execute(env, context), USCRIPT_ERROR_EXECUTE); 126 std::vector<UScriptValuePtr> output = context.GetOutVar(); 127 EXPECT_EQ(output.size(), 1); 128 } 129 130 protected: SetUpTestCase()131 static void SetUpTestCase() 132 { 133 LoadSpecificFstab("/data/updater/applypatch/etc/fstab.ut.updater"); 134 } TearDownTestCase()135 static void TearDownTestCase() {} SetUp()136 void SetUp() {} TearDown()137 void TearDown() {} TestBody()138 void TestBody() {} 139 private: 140 TestPkgMgr pkgManager_ {}; 141 }; 142 143 HWTEST_F(ImgPatchUnittest, TestImgPatch01, TestSize.Level1) 144 { 145 ImgPatchUnittest {}.TestImgPatch01(); 146 } 147 148 HWTEST_F(ImgPatchUnittest, TestImgPatch02, TestSize.Level1) 149 { 150 ImgPatchUnittest {}.TestImgPatch02(); 151 } 152 153 HWTEST_F(ImgPatchUnittest, TestImgPatch04, TestSize.Level1) 154 { 155 ImgPatchUnittest {}.TestImgPatch04(); 156 } 157 158 HWTEST_F(ImgPatchUnittest, TestImgPatch05, TestSize.Level1) 159 { 160 ImgPatchUnittest {}.TestImgPatch05(); 161 } 162 163 HWTEST_F(ImgPatchUnittest, TestImgPatch06, TestSize.Level1) 164 { 165 ImgPatchUnittest {}.TestImgPatch06(); 166 } 167 } 168