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 <array> 17 #include <cstring> 18 #include <dlfcn.h> 19 #include <fcntl.h> 20 #include <gtest/gtest.h> 21 #include <iostream> 22 #include <sys/mman.h> 23 #include <sys/stat.h> 24 #include <unistd.h> 25 #include "log.h" 26 #include "script_basicinstruction.h" 27 #include "script_instruction_unittest.h" 28 #include "script_instructionhelper.h" 29 #include "script_manager.h" 30 #include "script/script_unittest.h" 31 #include "script_utils.h" 32 #include "thread_pool.h" 33 #include "unittest_comm.h" 34 35 using namespace std; 36 using namespace Hpackage; 37 using namespace Uscript; 38 using namespace Updater; 39 using namespace testing::ext; 40 41 namespace { 42 43 class TestInstruction1 : public UScriptInstruction { 44 public: Execute(UScriptEnv & env,UScriptContext & context)45 int32_t Execute(UScriptEnv &env, UScriptContext &context) override 46 { 47 return USCRIPT_SUCCESS; 48 } 49 }; 50 51 class TestInstruction2 : public UScriptInstruction { 52 public: Execute(UScriptEnv & env,UScriptContext & context)53 int32_t Execute(UScriptEnv &env, UScriptContext &context) override 54 { 55 return USCRIPT_SUCCESS; 56 } 57 }; 58 59 class TestInstruction3 : public UScriptInstruction { 60 public: Execute(UScriptEnv & env,UScriptContext & context)61 int32_t Execute(UScriptEnv &env, UScriptContext &context) override 62 { 63 return USCRIPT_SUCCESS; 64 } 65 }; 66 67 class TestInstructionFactory : public UScriptInstructionFactory { 68 public: CreateInstructionInstance(UScriptInstructionPtr & instr,const std::string & name)69 int32_t CreateInstructionInstance(UScriptInstructionPtr& instr, const std::string& name) override 70 { 71 if (name == "test1") { 72 instr = new (std::nothrow) TestInstruction1(); 73 } else if (name == "test2") { 74 instr = new (std::nothrow) TestInstruction2(); 75 } else if (name == "test3") { 76 instr = nullptr; // mock new memory for Instruction failed scene 77 } else if (name == "abort") { 78 instr = new (std::nothrow) BasicInstruction::UScriptInstructionAbort(); // mock reserved error 79 } else { 80 return USCRIPT_NOTEXIST_INSTRUCTION; 81 } 82 return USCRIPT_SUCCESS; 83 } TestInstructionFactory()84 TestInstructionFactory() {} ~TestInstructionFactory()85 virtual ~TestInstructionFactory() {} 86 }; 87 88 class TestPkgManager : public TestScriptPkgManager { 89 public: ExtractFile(const std::string & fileId,StreamPtr output)90 int32_t ExtractFile(const std::string &fileId, StreamPtr output) override 91 { 92 return 0; 93 } GetFileInfo(const std::string & fileId)94 const FileInfo *GetFileInfo(const std::string &fileId) override 95 { 96 const static std::unordered_map<std::string, FileInfo> fileMap { 97 {"script1", FileInfo {}}, 98 {"script2", FileInfo {}}, 99 }; 100 if (auto it = fileMap.find(fileId); it != fileMap.end()) { 101 return &it->second; 102 } 103 return nullptr; 104 } 105 }; 106 107 class ScriptInstructionHelperUnitTest : public ::testing::Test { 108 public: ScriptInstructionHelperUnitTest()109 ScriptInstructionHelperUnitTest() 110 { 111 factory_ = std::make_unique<TestInstructionFactory>(); 112 } ~ScriptInstructionHelperUnitTest()113 ~ScriptInstructionHelperUnitTest() {} TestGetBasicInstructionHelper() const114 void TestGetBasicInstructionHelper() const 115 { 116 EXPECT_EQ(ScriptInstructionHelper::GetBasicInstructionHelper(nullptr), nullptr); 117 UTestScriptEnv env {nullptr}; 118 ScriptInstructionHelper *instructionHelper = nullptr; 119 auto impl = std::make_unique<ScriptManagerImpl>(&env); 120 { 121 instructionHelper = ScriptInstructionHelper::GetBasicInstructionHelper(impl.get()); 122 EXPECT_NE(instructionHelper, nullptr); 123 EXPECT_EQ(ScriptInstructionHelper::GetBasicInstructionHelper(impl.get()), instructionHelper); 124 EXPECT_EQ(ScriptInstructionHelper::GetBasicInstructionHelper(nullptr), instructionHelper); 125 } 126 ScriptInstructionHelper::ReleaseBasicInstructionHelper(); 127 EXPECT_EQ(ScriptInstructionHelper::GetBasicInstructionHelper(nullptr), nullptr); 128 } TestRegisterInstructions() const129 void TestRegisterInstructions() const 130 { 131 UTestScriptEnv env {nullptr}; 132 auto impl = std::make_unique<ScriptManagerImpl>(&env); 133 ScriptInstructionHelper helper(impl.get()); 134 EXPECT_EQ(helper.RegisterInstructions(), USCRIPT_SUCCESS); 135 } TestIsReservedInstruction() const136 void TestIsReservedInstruction() const 137 { 138 UTestScriptEnv env {nullptr}; 139 auto impl = std::make_unique<ScriptManagerImpl>(&env); 140 auto *helper = ScriptInstructionHelper::GetBasicInstructionHelper(impl.get()); 141 std::array reservedInstructions {"LoadScript", "RegisterCmd", "abort", "assert", "concat", 142 "is_substring", "stdout", "sleep", "set_progress", "ui_print", 143 "show_progress"}; 144 for (auto instruction : reservedInstructions) { 145 EXPECT_TRUE(helper->IsReservedInstruction(instruction)); 146 } 147 EXPECT_FALSE(helper->IsReservedInstruction("non reserved")); 148 } TestAddInstruction() const149 void TestAddInstruction() const 150 { 151 UTestScriptEnv env {nullptr}; 152 auto impl = std::make_unique<ScriptManagerImpl>(&env); 153 auto *helper = ScriptInstructionHelper::GetBasicInstructionHelper(impl.get()); 154 std::array reservedInstructions {"LoadScript", "RegisterCmd", "abort", "assert", "concat", 155 "is_substring", "stdout", "sleep", "set_progress", "ui_print", 156 "show_progress"}; 157 for (auto instruction : reservedInstructions) { 158 EXPECT_EQ(helper->AddInstruction(instruction, nullptr), USCRIPT_ERROR_REVERED); 159 } 160 UScriptInstructionPtr instr1 = nullptr; 161 EXPECT_EQ(factory_->CreateInstructionInstance(instr1, "test1"), USCRIPT_SUCCESS); 162 EXPECT_EQ(helper->AddInstruction("instruction1", instr1), USCRIPT_SUCCESS); 163 UScriptInstructionPtr instr2 = nullptr; 164 EXPECT_EQ(factory_->CreateInstructionInstance(instr2, "test2"), USCRIPT_SUCCESS); 165 EXPECT_EQ(helper->AddInstruction("instruction1", instr2), USCRIPT_SUCCESS); 166 } TestAddScript() const167 void TestAddScript() const 168 { 169 // no pkg manager in env 170 { 171 UTestScriptEnv env {nullptr}; 172 ScriptManagerImpl scriptManager(&env); 173 auto *helper = ScriptInstructionHelper::GetBasicInstructionHelper(&scriptManager); 174 EXPECT_EQ(helper->AddScript("", 0), USCRIPT_INVALID_PARAM); 175 } 176 // priority invalid 177 TestPkgManager pkgManager; 178 { 179 UTestScriptEnv env {&pkgManager}; 180 ScriptManagerImpl scriptManager(&env); 181 auto *helper = ScriptInstructionHelper::GetBasicInstructionHelper(&scriptManager); 182 EXPECT_EQ(helper->AddScript("", -1), USCRIPT_INVALID_PRIORITY); 183 EXPECT_EQ(helper->AddScript("", 4), USCRIPT_INVALID_PRIORITY); 184 } 185 // successfully add script 186 { 187 UTestScriptEnv env {&pkgManager}; 188 ScriptManagerImpl scriptManager(&env); 189 auto *helper = ScriptInstructionHelper::GetBasicInstructionHelper(&scriptManager); 190 EXPECT_EQ(helper->AddScript("script0", 0), USCRIPT_INVALID_SCRIPT); 191 EXPECT_EQ(helper->AddScript("script1", 1), USCRIPT_SUCCESS); 192 EXPECT_EQ(helper->AddScript("", 1), USCRIPT_INVALID_SCRIPT); 193 } 194 } TestRegisterUserInstruction01() const195 void TestRegisterUserInstruction01() const 196 { 197 // empty factory 198 { 199 UTestScriptEnv env {nullptr}; 200 ScriptManagerImpl scriptManager(&env); 201 auto *helper = ScriptInstructionHelper::GetBasicInstructionHelper(&scriptManager); 202 EXPECT_EQ(helper->RegisterUserInstruction("", nullptr), USCRIPT_INVALID_PARAM); 203 } 204 // invalid instruction "" 205 { 206 UTestScriptEnv env {nullptr}; 207 ScriptManagerImpl scriptManager(&env); 208 auto *helper = ScriptInstructionHelper::GetBasicInstructionHelper(&scriptManager); 209 EXPECT_EQ(helper->RegisterUserInstruction("", factory_.get()), USCRIPT_NOTEXIST_INSTRUCTION); 210 } 211 // test new failed, register reserved instr, register non-exist instr scene 212 { 213 UTestScriptEnv env {nullptr}; 214 ScriptManagerImpl scriptManager(&env); 215 auto *helper = ScriptInstructionHelper::GetBasicInstructionHelper(&scriptManager); 216 EXPECT_EQ(helper->RegisterUserInstruction("test3", factory_.get()), USCRIPT_ERROR_CREATE_OBJ); 217 EXPECT_EQ(helper->RegisterUserInstruction("test4", factory_.get()), USCRIPT_NOTEXIST_INSTRUCTION); 218 EXPECT_EQ(helper->RegisterUserInstruction("abort", factory_.get()), USCRIPT_ERROR_REVERED); 219 EXPECT_EQ(helper->RegisterUserInstruction("test2", factory_.get()), USCRIPT_SUCCESS); 220 ScriptInstructionHelper::ReleaseBasicInstructionHelper(); 221 } 222 } TestRegisterUserInstruction02() const223 void TestRegisterUserInstruction02() const 224 { 225 { 226 UTestScriptEnv env {nullptr}; 227 ScriptManagerImpl scriptManager(&env); 228 auto *helper = ScriptInstructionHelper::GetBasicInstructionHelper(&scriptManager); 229 EXPECT_EQ(helper->RegisterUserInstruction("", ""), USCRIPT_INVALID_PARAM); 230 } 231 { 232 UTestScriptEnv env {nullptr}; 233 ScriptManagerImpl scriptManager(&env); 234 auto *helper = ScriptInstructionHelper::GetBasicInstructionHelper(&scriptManager); 235 EXPECT_EQ(helper->RegisterUserInstruction("noexist", ""), USCRIPT_INVALID_PARAM); 236 } 237 { 238 UTestScriptEnv env {nullptr};{ 239 ScriptManagerImpl scriptManager(&env); 240 auto *helper = ScriptInstructionHelper::GetBasicInstructionHelper(&scriptManager); 241 EXPECT_EQ(helper->RegisterUserInstruction(TEST_INVALID_LIB_PATH, ""), USCRIPT_ERROR_CREATE_OBJ);} 242 } 243 { 244 UTestScriptEnv env {nullptr};{ 245 ScriptManagerImpl scriptManager(&env); 246 auto *helper = ScriptInstructionHelper::GetBasicInstructionHelper(&scriptManager); 247 EXPECT_EQ(helper->RegisterUserInstruction(TEST_VALID_LIB_PATH, ""), USCRIPT_NOTEXIST_INSTRUCTION); 248 EXPECT_EQ(helper->RegisterUserInstruction(TEST_NONEXIST_LIB_PATH, ""), USCRIPT_INVALID_PARAM); 249 EXPECT_EQ(helper->RegisterUserInstruction(TEST_INVALID_LIB_PATH, ""), USCRIPT_INVALID_PARAM); 250 EXPECT_EQ(helper->RegisterUserInstruction(TEST_VALID_LIB_PATH, "uInstruction1"), USCRIPT_SUCCESS);} 251 } 252 { 253 UTestScriptEnv env {nullptr};{ 254 ScriptManagerImpl scriptManager(&env); 255 auto *helper = ScriptInstructionHelper::GetBasicInstructionHelper(&scriptManager); 256 EXPECT_EQ(helper->RegisterUserInstruction(TEST_VALID_LIB_PATH, ""), USCRIPT_NOTEXIST_INSTRUCTION); 257 EXPECT_EQ(helper->RegisterUserInstruction(TEST_NONEXIST_LIB_PATH, ""), USCRIPT_INVALID_PARAM); 258 EXPECT_EQ(helper->RegisterUserInstruction(TEST_VALID_LIB_PATH, "uInstruction1"), USCRIPT_SUCCESS); 259 EXPECT_EQ(helper->RegisterUserInstruction(TEST_VALID_LIB_PATH, "abort"), USCRIPT_ERROR_REVERED);} 260 } 261 } 262 protected: 263 std::unique_ptr<TestInstructionFactory> factory_; SetUp()264 void SetUp() {} TearDown()265 void TearDown() {} TestBody()266 void TestBody() {} 267 }; 268 269 HWTEST_F(ScriptInstructionHelperUnitTest, TestGetBasicInstructionHelper, TestSize.Level1) 270 { 271 ScriptInstructionHelperUnitTest test; 272 test.TestGetBasicInstructionHelper(); 273 } 274 275 HWTEST_F(ScriptInstructionHelperUnitTest, TestRegisterInstructions, TestSize.Level1) 276 { 277 ScriptInstructionHelperUnitTest test; 278 test.TestRegisterInstructions(); 279 } 280 281 HWTEST_F(ScriptInstructionHelperUnitTest, TestIsReservedInstruction, TestSize.Level1) 282 { 283 ScriptInstructionHelperUnitTest test; 284 test.TestIsReservedInstruction(); 285 } 286 287 HWTEST_F(ScriptInstructionHelperUnitTest, TestAddInstruction, TestSize.Level1) 288 { 289 ScriptInstructionHelperUnitTest test; 290 test.TestAddInstruction(); 291 } 292 293 HWTEST_F(ScriptInstructionHelperUnitTest, TestAddScript, TestSize.Level1) 294 { 295 ScriptInstructionHelperUnitTest test; 296 test.TestAddScript(); 297 } 298 299 HWTEST_F(ScriptInstructionHelperUnitTest, TestRegisterUserInstruction01, TestSize.Level1) 300 { 301 ScriptInstructionHelperUnitTest test; 302 test.TestRegisterUserInstruction01(); 303 } 304 305 HWTEST_F(ScriptInstructionHelperUnitTest, TestRegisterUserInstruction02, TestSize.Level1) 306 { 307 ScriptInstructionHelperUnitTest test; 308 test.TestRegisterUserInstruction02(); 309 } 310 } 311