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 "script_context.h"
19 #include "script_instruction.h"
20 #include "script_instruction_unittest.h"
21 #include "script_instructionhelper.h"
22 #include "script_loadscript.h"
23 #include "script_manager_impl.h"
24 #include "script/script_unittest.h"
25 
26 using namespace Uscript;
27 using namespace BasicInstruction;
28 using namespace Hpackage;
29 using namespace testing::ext;
30 
31 class TestPkgManager : public TestScriptPkgManager {
32 public:
ExtractFile(const std::string & fileId,StreamPtr output)33     int32_t ExtractFile(const std::string &fileId, StreamPtr output) override
34     {
35         return 0;
36     }
GetFileInfo(const std::string & fileId)37     const FileInfo *GetFileInfo(const std::string &fileId) override
38     {
39         static FileInfo fileInfo {};
40         if (fileId == "script") {
41             return &fileInfo;
42         }
43         return nullptr;
44     }
45 };
46 
47 class LoadScriptInstructionUnittest : public ::testing::Test {
48 public:
LoadScriptInstructionUnittest()49     LoadScriptInstructionUnittest() {}
~LoadScriptInstructionUnittest()50     ~LoadScriptInstructionUnittest() {}
TestLoadScript01() const51     void TestLoadScript01() const
52     {
53         TestPkgManager pkgManager;
54         UTestScriptEnv env {&pkgManager};
55         ScriptManagerImpl scriptManager {&env};
56         ScriptInstructionHelper::GetBasicInstructionHelper(&scriptManager);
57         UScriptInstructionContext context {};
58         context.AddInputParam(std::make_shared<IntegerValue>(1));
59         auto instruction = std::make_unique<ScriptLoadScript>();
60         EXPECT_EQ(instruction->Execute(env, context), USCRIPT_INVALID_PARAM);
61         std::vector<UScriptValuePtr> output = context.GetOutVar();
62         EXPECT_EQ(output.size(), 0);
63         ScriptInstructionHelper::ReleaseBasicInstructionHelper();
64     }
TestLoadScript02() const65     void TestLoadScript02() const
66     {
67         TestPkgManager pkgManager;
68         UTestScriptEnv env {&pkgManager};
69         ScriptManagerImpl scriptManager {&env};
70         ScriptInstructionHelper::GetBasicInstructionHelper(&scriptManager);
71         UScriptInstructionContext context {};
72         context.AddInputParam(std::make_shared<StringValue>("script1"));
73         context.AddInputParam(std::make_shared<StringValue>("invalid param"));
74         auto instruction = std::make_unique<ScriptLoadScript>();
75         EXPECT_EQ(instruction->Execute(env, context), USCRIPT_INVALID_PARAM);
76         std::vector<UScriptValuePtr> output = context.GetOutVar();
77         EXPECT_EQ(output.size(), 0);
78         ScriptInstructionHelper::ReleaseBasicInstructionHelper();
79     }
TestLoadScript03() const80     void TestLoadScript03() const
81     {
82         TestPkgManager pkgManager;
83         UTestScriptEnv env {&pkgManager};
84         ScriptManagerImpl scriptManager {&env};
85         ScriptInstructionHelper::GetBasicInstructionHelper(&scriptManager);
86         UScriptInstructionContext context {};
87         context.AddInputParam(std::make_shared<StringValue>("script"));
88         context.AddInputParam(std::make_shared<IntegerValue>(1));
89         auto instruction = std::make_unique<ScriptLoadScript>();
90         EXPECT_EQ(instruction->Execute(env, context), USCRIPT_SUCCESS);
91         std::vector<UScriptValuePtr> output = context.GetOutVar();
92         EXPECT_EQ(output.size(), 0);
93         ScriptInstructionHelper::ReleaseBasicInstructionHelper();
94     }
95 protected:
SetUp()96     void SetUp() {}
TearDown()97     void TearDown() {}
TestBody()98     void TestBody() {}
99 };
100 
101 HWTEST_F(LoadScriptInstructionUnittest, TestLoadScript01, TestSize.Level1)
102 {
103     LoadScriptInstructionUnittest {}.TestLoadScript01();
104 }
105 
106 HWTEST_F(LoadScriptInstructionUnittest, TestLoadScript02, TestSize.Level1)
107 {
108     LoadScriptInstructionUnittest {}.TestLoadScript02();
109 }
110 
111 HWTEST_F(LoadScriptInstructionUnittest, TestLoadScript03, TestSize.Level1)
112 {
113     LoadScriptInstructionUnittest {}.TestLoadScript03();
114 }