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 <cstring> 17 #include <fcntl.h> 18 #include <gtest/gtest.h> 19 #include <gmock/gmock.h> 20 #include <iostream> 21 #include <memory> 22 #include <sys/mman.h> 23 #include <sys/stat.h> 24 #include <type_traits> 25 #include <unistd.h> 26 #include "log.h" 27 #include "script_context.h" 28 #include "script_basicinstruction.h" 29 #include "script_instruction.h" 30 #include "script_manager.h" 31 #include "script/script_unittest.h" 32 #include "script_utils.h" 33 #include "thread_pool.h" 34 35 using namespace std; 36 using namespace Hpackage; 37 using namespace Uscript; 38 using namespace BasicInstruction; 39 using namespace Updater; 40 using namespace testing::ext; 41 42 namespace { 43 class MockUScriptEnv : public UScriptEnv { 44 public: MockUScriptEnv(Hpackage::PkgManager::PkgManagerPtr pkgManager)45 explicit MockUScriptEnv(Hpackage::PkgManager::PkgManagerPtr pkgManager) : UScriptEnv(pkgManager) {} 46 MOCK_CONST_METHOD0(IsRetry, bool()); 47 MOCK_METHOD2(PostMessage, void(const std::string &cmd, std::string content)); 48 MOCK_METHOD0(GetInstructionFactory, UScriptInstructionFactoryPtr()); 49 MOCK_CONST_METHOD0(GetInstructionNames, const std::vector<std::string>()); 50 }; 51 52 class BasicInstructionUnittest : public ::testing::Test { 53 public: BasicInstructionUnittest()54 BasicInstructionUnittest() {} ~BasicInstructionUnittest()55 ~BasicInstructionUnittest() {} 56 template<typename ... Args> AddInputParam(UScriptInstructionContext & ctx,Args &&...args)57 static void AddInputParam(UScriptInstructionContext &ctx, Args && ... args) 58 { 59 [[maybe_unused]] auto li = { AddInputParamImpl(ctx, args)...}; 60 } AddInputParamImpl(UScriptInstructionContext & ctx,const std::string & v)61 static int32_t AddInputParamImpl(UScriptInstructionContext &ctx, const std::string &v) 62 { 63 return ctx.AddInputParam(std::make_shared<StringValue>(v)); 64 } 65 template<typename T, std::enable_if_t<std::is_floating_point_v<T>, void *> = nullptr> AddInputParamImpl(UScriptInstructionContext & ctx,T v)66 static int32_t AddInputParamImpl(UScriptInstructionContext &ctx, T v) 67 { 68 return ctx.AddInputParam(std::make_shared<FloatValue>(v)); 69 } AddInputParamImpl(UScriptInstructionContext & ctx,int32_t v)70 static int32_t AddInputParamImpl(UScriptInstructionContext &ctx, int32_t v) 71 { 72 return ctx.AddInputParam(std::make_shared<IntegerValue>(v)); 73 } TestBasicInstructionIsSubString01() const74 void TestBasicInstructionIsSubString01() const 75 { 76 { 77 MockUScriptEnv env {nullptr}; 78 UScriptInstructionContext context {}; 79 AddInputParam(context, "this is a test", "this is a test for is sub string"); 80 auto instruction = std::make_unique<UScriptInstructionIsSubString>(); 81 EXPECT_EQ(instruction->Execute(env, context), USCRIPT_SUCCESS); 82 std::vector<UScriptValuePtr> output = context.GetOutVar(); 83 ASSERT_EQ(output.size(), 1); 84 EXPECT_EQ(output[0]->GetValueType(), UScriptValue::VALUE_TYPE_INTEGER); 85 EXPECT_EQ(output[0]->ToString(), "0"); 86 } 87 { 88 MockUScriptEnv env {nullptr}; 89 UScriptInstructionContext context {}; 90 AddInputParam(context, "this is a test for is sub string", "this is a test"); 91 auto instruction = std::make_unique<UScriptInstructionIsSubString>(); 92 EXPECT_EQ(instruction->Execute(env, context), USCRIPT_SUCCESS); 93 std::vector<UScriptValuePtr> output = context.GetOutVar(); 94 ASSERT_EQ(output.size(), 1); 95 EXPECT_EQ(output[0]->GetValueType(), UScriptValue::VALUE_TYPE_INTEGER); 96 EXPECT_EQ(output[0]->ToString(), "1"); 97 } 98 } TestBasicInstructionIsSubString02() const99 void TestBasicInstructionIsSubString02() const 100 { 101 { 102 MockUScriptEnv env {nullptr}; 103 UScriptInstructionContext context {}; 104 context.AddInputParam(std::make_shared<StringValue>("this is a test")); 105 auto instruction = std::make_unique<UScriptInstructionIsSubString>(); 106 EXPECT_EQ(instruction->Execute(env, context), UScriptContext::PARAM_TYPE_INVALID); 107 std::vector<UScriptValuePtr> output = context.GetOutVar(); 108 EXPECT_EQ(output.size(), 0); 109 } 110 { 111 MockUScriptEnv env {nullptr}; 112 UScriptInstructionContext context {}; 113 AddInputParam(context, "this is a test", 1); 114 auto instruction = std::make_unique<UScriptInstructionIsSubString>(); 115 EXPECT_EQ(instruction->Execute(env, context), USCRIPT_INVALID_PARAM); 116 std::vector<UScriptValuePtr> output = context.GetOutVar(); 117 EXPECT_EQ(output.size(), 0); 118 } 119 } TestBasicInstructionStdout() const120 void TestBasicInstructionStdout() const 121 { 122 MockUScriptEnv env {nullptr}; 123 UScriptInstructionContext context {}; 124 auto input = std::make_tuple("string1", 1, 1.0, 1.0001, 1.00000001); 125 auto instruction = std::make_unique<UScriptInstructionStdout>(); 126 std::stringstream buffer; 127 std::streambuf* old = std::cout.rdbuf(buffer.rdbuf()); 128 std::stringstream tgt; 129 std::apply([&tgt] (auto && ... args) { 130 ((tgt << args << " "), ...); 131 tgt << std::endl; 132 }, input); 133 std::apply([&context] (auto && ... args) { 134 AddInputParam(context, args...); 135 }, input); 136 EXPECT_EQ(instruction->Execute(env, context), USCRIPT_SUCCESS); 137 std::vector<UScriptValuePtr> output = context.GetOutVar(); 138 EXPECT_EQ(output.size(), 0); 139 EXPECT_EQ(buffer.str(), tgt.str()); 140 std::cout.rdbuf(old); 141 } TestBasicInstructionConcat() const142 void TestBasicInstructionConcat() const 143 { 144 MockUScriptEnv env {nullptr}; 145 { 146 UScriptInstructionContext context {}; 147 auto input = std::make_tuple("this is a test", "test2", 1, 1.0, 1.0001, 1.00000001); 148 auto instruction = std::make_unique<UScriptInstructionConcat>(); 149 std::stringstream tgt; 150 auto toString = [] (auto &&arg) -> std::string { 151 using T = std::remove_cv_t<std::remove_reference_t<decltype(arg)>>; 152 if constexpr (std::is_same_v<std::string, T> || 153 std::is_same_v<const char *, T> || 154 std::is_same_v<char *, T>) { 155 return arg; 156 } else { 157 return std::to_string(arg); 158 } 159 }; 160 std::apply([&toString, &tgt] (auto && ... args) { 161 ((tgt << toString(args)), ...); 162 }, input); 163 std::apply([&context] (auto && ... args) { 164 AddInputParam(context, args...); 165 }, input); 166 EXPECT_EQ(instruction->Execute(env, context), USCRIPT_SUCCESS); 167 std::vector<UScriptValuePtr> output = context.GetOutVar(); 168 ASSERT_EQ(output.size(), 1); 169 EXPECT_EQ(output[0]->GetValueType(), UScriptValue::VALUE_TYPE_STRING); 170 EXPECT_EQ(output[0]->ToString(), tgt.str()); 171 } 172 { 173 UScriptInstructionContext context {}; 174 auto instruction = std::make_unique<UScriptInstructionConcat>(); 175 EXPECT_EQ(instruction->Execute(env, context), UScriptContext::PARAM_TYPE_INVALID); 176 } 177 { 178 UScriptInstructionContext context1 {}; 179 AddInputParam(context1, 1); 180 auto instruction1 = std::make_unique<UScriptInstructionConcat>(); 181 EXPECT_EQ(instruction1->Execute(env, context1), USCRIPT_INVALID_PARAM); 182 UScriptInstructionContext context2 {}; 183 AddInputParam(context2, "test"); 184 auto instruction2 = std::make_unique<UScriptInstructionConcat>(); 185 EXPECT_EQ(instruction2->Execute(env, context2), USCRIPT_SUCCESS); 186 std::vector<UScriptValuePtr> output = context2.GetOutVar(); 187 ASSERT_EQ(output.size(), 1); 188 EXPECT_EQ(output[0]->GetValueType(), UScriptValue::VALUE_TYPE_STRING); 189 EXPECT_EQ(output[0]->ToString(), "test"); 190 } 191 } TestBasicInstructionAbort() const192 void TestBasicInstructionAbort() const 193 { 194 MockUScriptEnv env {nullptr}; 195 { 196 UScriptInstructionContext context {}; 197 auto instruction = std::make_unique<UScriptInstructionAbort>(); 198 EXPECT_EQ(instruction->Execute(env, context), UScriptContext::PARAM_TYPE_INVALID); 199 } 200 { 201 UScriptInstructionContext context {}; 202 AddInputParam(context, 1.0); 203 auto instruction = std::make_unique<UScriptInstructionSleep>(); 204 EXPECT_EQ(instruction->Execute(env, context), USCRIPT_INVALID_PARAM); 205 } 206 { 207 UScriptInstructionContext context {}; 208 AddInputParam(context, 1); 209 auto instruction = std::make_unique<UScriptInstructionAbort>(); 210 EXPECT_EQ(instruction->Execute(env, context), USCRIPT_SUCCESS); 211 } 212 { 213 UScriptInstructionContext context {}; 214 AddInputParam(context, 0); 215 auto instruction = std::make_unique<UScriptInstructionAbort>(); 216 EXPECT_EQ(instruction->Execute(env, context), USCRIPT_ABOART); 217 } 218 } TestBasicInstructionAssert() const219 void TestBasicInstructionAssert() const 220 { 221 MockUScriptEnv env {nullptr}; 222 { 223 UScriptInstructionContext context {}; 224 auto instruction = std::make_unique<UScriptInstructionAssert>(); 225 EXPECT_EQ(instruction->Execute(env, context), UScriptContext::PARAM_TYPE_INVALID); 226 } 227 { 228 UScriptInstructionContext context {}; 229 AddInputParam(context, 1); 230 auto instruction = std::make_unique<UScriptInstructionAssert>(); 231 EXPECT_EQ(instruction->Execute(env, context), USCRIPT_SUCCESS); 232 } 233 { 234 UScriptInstructionContext context {}; 235 AddInputParam(context, 0); 236 auto instruction = std::make_unique<UScriptInstructionAssert>(); 237 EXPECT_EQ(instruction->Execute(env, context), USCRIPT_ASSERT); 238 } 239 } TestBasicInstructionSleep() const240 void TestBasicInstructionSleep() const 241 { 242 MockUScriptEnv env {nullptr}; 243 { 244 UScriptInstructionContext context {}; 245 auto instruction = std::make_unique<UScriptInstructionSleep>(); 246 EXPECT_EQ(instruction->Execute(env, context), UScriptContext::PARAM_TYPE_INVALID); 247 } 248 { 249 UScriptInstructionContext context {}; 250 AddInputParam(context, 0); 251 auto instruction = std::make_unique<UScriptInstructionSleep>(); 252 EXPECT_EQ(instruction->Execute(env, context), USCRIPT_SUCCESS); 253 } 254 } 255 protected: SetUp()256 void SetUp() {} TearDown()257 void TearDown() {} TestBody()258 void TestBody() {} 259 }; 260 261 HWTEST_F(BasicInstructionUnittest, TestBasicInstructionIsSubString01, TestSize.Level1) 262 { 263 BasicInstructionUnittest test; 264 test.TestBasicInstructionIsSubString01(); 265 } 266 267 HWTEST_F(BasicInstructionUnittest, TestBasicInstructionIsSubString02, TestSize.Level1) 268 { 269 BasicInstructionUnittest test; 270 test.TestBasicInstructionIsSubString02(); 271 } 272 273 HWTEST_F(BasicInstructionUnittest, TestBasicInstructionStdout, TestSize.Level1) 274 { 275 BasicInstructionUnittest test; 276 test.TestBasicInstructionStdout(); 277 } 278 279 HWTEST_F(BasicInstructionUnittest, TestBasicInstructionConcat, TestSize.Level1) 280 { 281 BasicInstructionUnittest test; 282 test.TestBasicInstructionConcat(); 283 } 284 285 HWTEST_F(BasicInstructionUnittest, TestBasicInstructionAbort, TestSize.Level1) 286 { 287 BasicInstructionUnittest test; 288 test.TestBasicInstructionAbort(); 289 } 290 291 HWTEST_F(BasicInstructionUnittest, TestBasicInstructionAssert, TestSize.Level1) 292 { 293 BasicInstructionUnittest test; 294 test.TestBasicInstructionAssert(); 295 } 296 297 HWTEST_F(BasicInstructionUnittest, TestBasicInstructionSleep, TestSize.Level1) 298 { 299 BasicInstructionUnittest test; 300 test.TestBasicInstructionSleep(); 301 } 302 } 303