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 <iostream>
20 #include <sys/mman.h>
21 #include <sys/stat.h>
22 #include <unistd.h>
23 #include "log.h"
24 #include "script_context.h"
25 #include "script_expression.h"
26 #include "script_instruction.h"
27 #include "script_manager.h"
28 #include "unittest_comm.h"
29 
30 using namespace std;
31 using namespace Hpackage;
32 using namespace Uscript;
33 using namespace Updater;
34 using namespace testing::ext;
35 
36 namespace {
37 class ScriptInterpreterUnitTest : public ::testing::Test {
38 public:
ScriptInterpreterUnitTest()39     ScriptInterpreterUnitTest() {}
~ScriptInterpreterUnitTest()40     ~ScriptInterpreterUnitTest() {}
41 
TestScriptInterpreterScriptValue() const42     int TestScriptInterpreterScriptValue() const
43     {
44         int32_t action = 0;
45         UScriptValuePtr rightValue = std::make_shared<UScriptValue>(UScriptValue::VALUE_TYPE_RETURN);
46         std::unique_ptr<UScriptValue> value = std::make_unique<UScriptValue>(UScriptValue::VALUE_TYPE_RETURN);
47         if (value == nullptr) {
48             return -1;
49         }
50         value->Computer(action, rightValue);
51         value->IsTrue();
52         std::string tmpStr = value->ToString();
53         EXPECT_STREQ("null", tmpStr.c_str());
54         value.reset();
55 
56         int32_t intValue = 10;
57         std::unique_ptr<IntegerValue> iv = std::make_unique<IntegerValue>(intValue);
58         if (iv == nullptr) {
59             return -1;
60         }
61         iv->IsTrue();
62         iv.reset();
63 
64         float floatValue = 10.0;
65         std::unique_ptr<FloatValue> fv = std::make_unique<FloatValue>(floatValue);
66         if (fv == nullptr) {
67             return -1;
68         }
69         fv->IsTrue();
70         fv.reset();
71 
72         std::unique_ptr<StringValue> sv = std::make_unique<StringValue>("2222222222222");
73         if (sv == nullptr) {
74             return -1;
75         }
76         sv->IsTrue();
77         sv.reset();
78 
79         std::unique_ptr<ReturnValue> rv = std::make_unique<ReturnValue>();
80         if (rv == nullptr) {
81             return -1;
82         }
83         rv->IsTrue();
84         rv->Computer(action, rightValue);
85         rv->ToString();
86         rv.reset();
87 
88         constexpr int32_t retCode = 100;
89         std::unique_ptr<ErrorValue> ev = std::make_unique<ErrorValue>(retCode);
90         if (ev == nullptr) {
91             return -1;
92         }
93         ev->IsTrue();
94         ev->ToString();
95         ev->Computer(action, rightValue);
96         ev.reset();
97         return 0;
98     }
99 
TestScriptInstructionContext() const100     int TestScriptInstructionContext() const
101     {
102         std::unique_ptr<UScriptInstructionContext> funcContext = std::make_unique<UScriptInstructionContext>();
103         int intValue = 100;
104         int ret1 = funcContext->PushParam(intValue);
105         float floatValue = 100.0;
106         int ret2 = funcContext->PushParam(floatValue);
107         std::string str = std::string("333333333");
108         int ret3 = funcContext->PushParam(str);
109         EXPECT_EQ(0, ret1 || ret2 || ret3);
110 
111         int32_t outOfIndex = 3;
112         int ret = funcContext->GetParamType(outOfIndex);
113         EXPECT_EQ(UScriptContext::PARAM_TYPE_INVALID, ret);
114         return 0;
115     }
116 
TestIntegerValueComputer() const117     int TestIntegerValueComputer() const
118     {
119         int intValue = 100;
120         UScriptValuePtr rightValue = std::make_shared<IntegerValue>(0);
121         UScriptValuePtr value = std::make_shared<IntegerValue>(intValue);
122         if (value == nullptr || rightValue == nullptr) {
123             return -1;
124         }
125         value->Computer(UScriptExpression::DIV_OPERATOR, rightValue);
126         // 除浮点数
127         float floatValue = 100.0;
128         UScriptValuePtr rightValue2 = std::make_shared<FloatValue>(floatValue);
129         value->Computer(UScriptExpression::DIV_OPERATOR, rightValue2);
130 
131         // 除浮点数
132         UScriptValuePtr rightValue3 = std::make_shared<FloatValue>(0);
133         value->Computer(UScriptExpression::DIV_OPERATOR, rightValue3);
134 
135         int32_t action = 100;
136         value->Computer(action, rightValue3);
137         return 0;
138     }
139 
TestFloatValueComputer() const140     int TestFloatValueComputer() const
141     {
142         float floatValue = 100;
143         UScriptValuePtr rightValue = std::make_shared<IntegerValue>(0);
144         UScriptValuePtr value = std::make_shared<FloatValue>(floatValue);
145         if (value == nullptr || rightValue == nullptr) {
146             return -1;
147         }
148         value->Computer(UScriptExpression::DIV_OPERATOR, rightValue);
149         // 除浮点数
150         UScriptValuePtr rightValue2 = std::make_shared<FloatValue>(floatValue);
151         value->Computer(UScriptExpression::DIV_OPERATOR, rightValue2);
152 
153         // 除浮点数
154         UScriptValuePtr rightValue3 = std::make_shared<FloatValue>(0);
155         value->Computer(UScriptExpression::DIV_OPERATOR, rightValue3);
156 
157         value->Computer(UScriptExpression::EQ_OPERATOR, rightValue3);
158         return 0;
159     }
160 
TestStringValueComputer() const161     int TestStringValueComputer() const
162     {
163         UScriptValuePtr rightValue = std::make_shared<IntegerValue>(0);
164         UScriptValuePtr value = std::make_shared<StringValue>("100");
165         if (value == nullptr || rightValue == nullptr) {
166             return -1;
167         }
168         value->Computer(UScriptExpression::ADD_OPERATOR, rightValue);
169 
170         float floatValue = 100.0;
171         UScriptValuePtr rightValue2 = std::make_shared<FloatValue>(floatValue);
172         value->Computer(UScriptExpression::ADD_OPERATOR, rightValue2);
173 
174         UScriptValuePtr rightValue3 = std::make_shared<StringValue>("666666");
175         value->Computer(UScriptExpression::ADD_OPERATOR, rightValue3);
176 
177         // string 比较
178         value->Computer(UScriptExpression::EQ_OPERATOR, rightValue2);
179 
180         value->Computer(UScriptExpression::GT_OPERATOR, rightValue3);
181         value->Computer(UScriptExpression::GE_OPERATOR, rightValue3);
182         value->Computer(UScriptExpression::LT_OPERATOR, rightValue3);
183         value->Computer(UScriptExpression::LE_OPERATOR, rightValue3);
184         value->Computer(UScriptExpression::EQ_OPERATOR, rightValue3);
185         value->Computer(UScriptExpression::NE_OPERATOR, rightValue3);
186         return 0;
187     }
188 
189 protected:
SetUp()190     void SetUp() {}
TearDown()191     void TearDown() {}
TestBody()192     void TestBody() {}
193 };
194 
195 HWTEST_F(ScriptInterpreterUnitTest, TestScriptInterpreterScriptValue, TestSize.Level0)
196 {
197     ScriptInterpreterUnitTest test;
198     EXPECT_EQ(0, test.TestScriptInterpreterScriptValue());
199 }
200 
201 HWTEST_F(ScriptInterpreterUnitTest, TestScriptInstructionContext, TestSize.Level0)
202 {
203     ScriptInterpreterUnitTest test;
204     EXPECT_EQ(0, test.TestScriptInstructionContext());
205 }
206 
207 HWTEST_F(ScriptInterpreterUnitTest, TestIntegerValueComputer, TestSize.Level0)
208 {
209     ScriptInterpreterUnitTest test;
210     EXPECT_EQ(0, test.TestIntegerValueComputer());
211 }
212 
213 HWTEST_F(ScriptInterpreterUnitTest, TestFloatValueComputer, TestSize.Level0)
214 {
215     ScriptInterpreterUnitTest test;
216     EXPECT_EQ(0, test.TestFloatValueComputer());
217 }
218 
219 HWTEST_F(ScriptInterpreterUnitTest, TestStringValueComputer, TestSize.Level0)
220 {
221     ScriptInterpreterUnitTest test;
222     EXPECT_EQ(0, test.TestStringValueComputer());
223 }
224 
225 HWTEST_F(ScriptInterpreterUnitTest, SomeDestructor, TestSize.Level0)
226 {
227     IntegerValue a1(0);
228     FloatValue a2(0.0);
229     ErrorValue a3(0);
230     char a5[1000] = {1};
231     UScriptContextPtr local;
232     UScriptExpression a4(UScriptExpression::EXPRESSION_TYPE_INTERGER);
233     a4.Execute(*(ScriptInterpreter*)a5, local);
234     // just for executing these destructor
235     IntegerValue *a6 = new IntegerValue(1);
236     bool b1 = a6->IsTrue();
237     delete a6;
238     FloatValue *a7 = new FloatValue(0.0);
239     delete a7;
240     ErrorValue *a8 = new ErrorValue(0);
241     delete a8;
242     EXPECT_TRUE(b1);
243 }
244 }
245