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 <climits> 17 #include <cstring> 18 #include <fcntl.h> 19 #include <gtest/gtest.h> 20 #include <iostream> 21 #include <sys/mman.h> 22 #include <sys/stat.h> 23 #include <unistd.h> 24 #include "log.h" 25 #include "pkg_stream.h" 26 #include "pkg_utils.h" 27 #include "script_instruction.h" 28 #include "script_manager.h" 29 #include "script/script_unittest.h" 30 #include "script_utils.h" 31 #include "unittest_comm.h" 32 #include "utils.h" 33 34 using namespace std; 35 using namespace Hpackage; 36 using namespace Uscript; 37 using namespace Updater; 38 using namespace testing::ext; 39 40 namespace { 41 constexpr int32_t SCRIPT_TEST_PRIORITY_NUM = 3; 42 constexpr int32_t SCRIPT_TEST_LAST_PRIORITY = 2; 43 44 class TestPkgManager : public TestScriptPkgManager { 45 public: ExtractFile(const std::string & fileId,StreamPtr output)46 int32_t ExtractFile(const std::string &fileId, StreamPtr output) override 47 { 48 return 0; 49 } GetFileInfo(const std::string & fileId)50 const FileInfo *GetFileInfo(const std::string &fileId) override 51 { 52 static FileInfo fileInfo {}; 53 static std::vector<std::string> testFileNames = { 54 "loadScript.us", 55 "registerCmd.us", 56 "test_function.us", 57 "test_if.us", 58 "test_logic.us", 59 "test_math.us", 60 "test_native.us", 61 "testscript.us", 62 "Verse-script.us", 63 "test_script.us" 64 }; 65 if (std::find(testFileNames.begin(), testFileNames.end(), fileId) != testFileNames.end()) { 66 return &fileInfo; 67 } 68 return nullptr; 69 } CreatePkgStream(StreamPtr & stream,const std::string & fileName,size_t size,int32_t type)70 int32_t CreatePkgStream(StreamPtr &stream, const std::string &fileName, 71 size_t size, int32_t type) override 72 { 73 FILE *file = nullptr; 74 std::string fileNameReal = fileName; 75 char realPath[PATH_MAX + 1] = {}; 76 if (!Updater::Utils::IsUpdaterMode()) { 77 fileNameReal = fileName.substr(fileName.rfind("/")); 78 } 79 if (realpath((TEST_PATH_FROM + fileNameReal).c_str(), realPath) == nullptr) { 80 LOG(ERROR) << fileNameReal << " realpath failed"; 81 return PKG_INVALID_FILE; 82 } 83 file = fopen(realPath, "rb"); 84 PKG_CHECK(file != nullptr, return PKG_INVALID_FILE, "Fail to open file %s ", fileNameReal.c_str()); 85 stream = new FileStream(this, fileNameReal, file, PkgStream::PkgStreamType_Read); 86 return USCRIPT_SUCCESS; 87 } ClosePkgStream(StreamPtr & stream)88 void ClosePkgStream(StreamPtr &stream) override 89 { 90 delete stream; 91 } 92 }; 93 94 95 class TestScriptInstructionSparseImageWrite : public Uscript::UScriptInstruction { 96 public: TestScriptInstructionSparseImageWrite()97 TestScriptInstructionSparseImageWrite() {} ~TestScriptInstructionSparseImageWrite()98 virtual ~TestScriptInstructionSparseImageWrite() {} Execute(Uscript::UScriptEnv & env,Uscript::UScriptContext & context)99 int32_t Execute(Uscript::UScriptEnv &env, Uscript::UScriptContext &context) override 100 { 101 // 从参数中获取分区信息 102 std::string partitionName; 103 int32_t ret = context.GetParam(0, partitionName); 104 if (ret != USCRIPT_SUCCESS) { 105 LOG(ERROR) << "Error to get param"; 106 return ret; 107 } 108 LOG(INFO) << "UScriptInstructionSparseImageWrite::Execute " << partitionName; 109 if (env.GetPkgManager() == nullptr) { 110 LOG(ERROR) << "Error to get pkg manager"; 111 return USCRIPT_ERROR_EXECUTE; 112 } 113 return ret; 114 } 115 }; 116 117 class TestScriptInstructionFactory : public UScriptInstructionFactory { 118 public: CreateInstructionInstance(UScriptInstructionPtr & instr,const std::string & name)119 virtual int32_t CreateInstructionInstance(UScriptInstructionPtr& instr, const std::string& name) 120 { 121 if (name == "sparse_image_write") { 122 instr = new TestScriptInstructionSparseImageWrite(); 123 } 124 return USCRIPT_SUCCESS; 125 } DestoryInstructionInstance(UScriptInstructionPtr & instr)126 virtual void DestoryInstructionInstance(UScriptInstructionPtr& instr) 127 { 128 delete instr; 129 instr = nullptr; 130 } TestScriptInstructionFactory()131 TestScriptInstructionFactory() {} ~TestScriptInstructionFactory()132 virtual ~TestScriptInstructionFactory() {} 133 }; 134 135 class UTestScriptEnv : public UScriptEnv { 136 public: UTestScriptEnv(Hpackage::PkgManager::PkgManagerPtr pkgManager)137 explicit UTestScriptEnv(Hpackage::PkgManager::PkgManagerPtr pkgManager) : UScriptEnv(pkgManager) {} ~UTestScriptEnv()138 ~UTestScriptEnv() 139 { 140 if (factory_ != nullptr) { 141 delete factory_; 142 factory_ = nullptr; 143 } 144 } 145 PostMessage(const std::string & cmd,std::string content)146 virtual void PostMessage(const std::string &cmd, std::string content) {} 147 GetInstructionFactory()148 virtual UScriptInstructionFactoryPtr GetInstructionFactory() 149 { 150 if (factory_ == nullptr) { 151 factory_ = new TestScriptInstructionFactory(); 152 } 153 return factory_; 154 } 155 GetInstructionNames() const156 virtual const std::vector<std::string> GetInstructionNames() const 157 { 158 static std::vector<std::string> updaterCmds = {"sparse_image_write"}; 159 return updaterCmds; 160 } 161 IsRetry() const162 virtual bool IsRetry() const 163 { 164 return isRetry; 165 } 166 UScriptInstructionFactory *factory_ = nullptr; 167 private: 168 bool isRetry = false; 169 }; 170 171 class UScriptTest : public ::testing::Test { 172 public: UScriptTest()173 UScriptTest() {} 174 ~UScriptTest()175 ~UScriptTest() 176 { 177 ScriptManager::ReleaseScriptManager(); 178 } 179 TestUscriptExecute()180 int TestUscriptExecute() 181 { 182 int32_t ret {}; 183 TestPkgManager packageManager; 184 UTestScriptEnv* env = new UTestScriptEnv(&packageManager); 185 ScriptManager* manager = ScriptManager::GetScriptManager(env); 186 if (manager == nullptr) { 187 USCRIPT_LOGI("create manager fail ret:%d", ret); 188 delete env; 189 return USCRIPT_INVALID_SCRIPT; 190 } 191 int32_t priority = SCRIPT_TEST_PRIORITY_NUM; 192 ret = manager->ExecuteScript(priority); 193 EXPECT_EQ(ret, USCRIPT_ABOART); 194 USCRIPT_LOGI("ExecuteScript ret:%d", ret); 195 priority = 0; 196 ret = manager->ExecuteScript(priority); 197 priority = 1; 198 ret = manager->ExecuteScript(priority); 199 priority = SCRIPT_TEST_LAST_PRIORITY; 200 ret = manager->ExecuteScript(priority); 201 delete env; 202 ScriptManager::ReleaseScriptManager(); 203 return ret; 204 } 205 206 protected: SetUp()207 void SetUp() {} TearDown()208 void TearDown() {} TestBody()209 void TestBody() {} 210 211 private: 212 std::vector<std::string> testFileNames_ = { 213 "loadScript.us", 214 "registerCmd.us", 215 "test_function.us", 216 "test_if.us", 217 "test_logic.us", 218 "test_math.us", 219 "test_native.us", 220 "testscript.us", 221 "Verse-script.us", 222 "test_script.us" 223 }; 224 }; 225 226 HWTEST_F(UScriptTest, TestUscriptExecute, TestSize.Level1) 227 { 228 UScriptTest test; 229 EXPECT_EQ(0, test.TestUscriptExecute()); 230 } 231 } 232