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(): 根据指令入参解析fstab设备文件失败 */ TestImgPatch03()63 void TestImgPatch03() 64 { 65 UTestBinaryEnv env {&pkgManager_}; 66 UScriptInstructionContext context {}; 67 context.AddInputParam(std::make_shared<StringValue>("")); 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 /* GetParam(): 解析命令行参数成功 */ TestImgPatch04()80 void TestImgPatch04() 81 { 82 UTestBinaryEnv env {&pkgManager_}; 83 UScriptInstructionContext context {}; 84 context.AddInputParam(std::make_shared<StringValue>("/vendortest")); 85 context.AddInputParam(std::make_shared<StringValue>("srcSize")); 86 context.AddInputParam(std::make_shared<StringValue>("srcHash")); 87 context.AddInputParam(std::make_shared<StringValue>("destSize")); 88 context.AddInputParam(std::make_shared<StringValue>("destHash")); 89 context.AddInputParam(std::make_shared<StringValue>("/data/updater/patchfile")); 90 auto instruction = std::make_unique<USInstrImagePatch>(); 91 EXPECT_EQ(instruction->Execute(env, context), USCRIPT_ERROR_EXECUTE); 92 std::vector<UScriptValuePtr> output = context.GetOutVar(); 93 EXPECT_EQ(output.size(), 1); 94 } 95 96 /* GetPatchFile(): retry为true, 获取文件失败, 创建文件流失败 */ TestImgPatch05()97 void TestImgPatch05() 98 { 99 PkgManager::PkgManagerPtr pkgMgr = nullptr; 100 UTestBinaryEnv env1 {pkgMgr}; 101 env1.SetRetry(true); 102 UScriptInstructionContext context {}; 103 context.AddInputParam(std::make_shared<StringValue>("/vendortest")); 104 context.AddInputParam(std::make_shared<StringValue>("srcSize")); 105 context.AddInputParam(std::make_shared<StringValue>("srcHash")); 106 context.AddInputParam(std::make_shared<StringValue>("destSize")); 107 context.AddInputParam(std::make_shared<StringValue>("destHash")); 108 context.AddInputParam(std::make_shared<StringValue>("/data/updater/patchfile")); 109 auto instruction = std::make_unique<USInstrImagePatch>(); 110 EXPECT_EQ(instruction->Execute(env1, context), USCRIPT_ERROR_EXECUTE); 111 std::vector<UScriptValuePtr> output = context.GetOutVar(); 112 EXPECT_EQ(output.size(), 1); 113 114 UTestBinaryEnv env2(&pkgManager_); 115 EXPECT_EQ(instruction->Execute(env2, context), USCRIPT_ERROR_EXECUTE); 116 117 TestPkgMgrStream1 pkgStream1; 118 UTestBinaryEnv env3(&pkgStream1); 119 EXPECT_EQ(instruction->Execute(env3, context), USCRIPT_ERROR_EXECUTE); 120 121 TestPkgMgrStream2 pkgStream2; 122 UTestBinaryEnv env4(&pkgStream2); 123 EXPECT_EQ(instruction->Execute(env4, context), USCRIPT_ERROR_EXECUTE); 124 125 TestPkgMgrExtract1 pkgExtract1; 126 UTestBinaryEnv env5(&pkgExtract1); 127 EXPECT_EQ(instruction->Execute(env5, context), USCRIPT_ERROR_EXECUTE); 128 } 129 130 /* GetPatchFile(): 获取文件成功,patchfile=/data/udpater/binary */ TestImgPatch06()131 void TestImgPatch06() 132 { 133 UTestBinaryEnv env {&pkgManager_}; 134 UScriptInstructionContext context {}; 135 context.AddInputParam(std::make_shared<StringValue>("binary")); 136 context.AddInputParam(std::make_shared<StringValue>("srcSize")); 137 context.AddInputParam(std::make_shared<StringValue>("srcHash")); 138 context.AddInputParam(std::make_shared<StringValue>("destSize")); 139 context.AddInputParam(std::make_shared<StringValue>("destHash")); 140 context.AddInputParam(std::make_shared<StringValue>("/data/updater/patchfile")); 141 auto instruction = std::make_unique<USInstrImagePatch>(); 142 EXPECT_EQ(instruction->Execute(env, context), USCRIPT_ERROR_EXECUTE); 143 std::vector<UScriptValuePtr> output = context.GetOutVar(); 144 EXPECT_EQ(output.size(), 1); 145 } 146 147 protected: SetUpTestCase()148 static void SetUpTestCase() 149 { 150 LoadSpecificFstab("/data/updater/applypatch/etc/fstab.ut.updater"); 151 } TearDownTestCase()152 static void TearDownTestCase() {} SetUp()153 void SetUp() {} TearDown()154 void TearDown() {} TestBody()155 void TestBody() {} 156 private: 157 TestPkgMgr pkgManager_ {}; 158 }; 159 160 HWTEST_F(ImgPatchUnittest, TestImgPatch01, TestSize.Level1) 161 { 162 ImgPatchUnittest {}.TestImgPatch01(); 163 } 164 165 HWTEST_F(ImgPatchUnittest, TestImgPatch02, TestSize.Level1) 166 { 167 ImgPatchUnittest {}.TestImgPatch02(); 168 } 169 170 HWTEST_F(ImgPatchUnittest, TestImgPatch03, TestSize.Level1) 171 { 172 ImgPatchUnittest {}.TestImgPatch03(); 173 } 174 175 HWTEST_F(ImgPatchUnittest, TestImgPatch04, TestSize.Level1) 176 { 177 ImgPatchUnittest {}.TestImgPatch04(); 178 } 179 180 HWTEST_F(ImgPatchUnittest, TestImgPatch05, TestSize.Level1) 181 { 182 ImgPatchUnittest {}.TestImgPatch05(); 183 } 184 185 HWTEST_F(ImgPatchUnittest, TestImgPatch06, TestSize.Level1) 186 { 187 ImgPatchUnittest {}.TestImgPatch06(); 188 } 189 } 190