1 /**
2 * Copyright (c) 2021-2024 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
18 #include "runtime/include/runtime.h"
19 #include "runtime/include/runtime_options.h"
20 #include "plugins/ets/runtime/types/ets_method_signature.h"
21
22 namespace ark::ets::test {
23
24 namespace {
MethodSignaturePrologue()25 void MethodSignaturePrologue()
26 {
27 EtsMethodSignature minimal(":V");
28 EXPECT_EQ(minimal.GetProto(), Method::Proto(
29 Method::Proto::ShortyVector {
30 panda_file::Type {panda_file::Type::TypeId::VOID},
31 },
32 Method::Proto::RefTypeVector {}));
33 EtsMethodSignature minimalint("I:V");
34 EXPECT_EQ(minimalint.GetProto(), Method::Proto(
35 Method::Proto::ShortyVector {
36 panda_file::Type {panda_file::Type::TypeId::VOID},
37 panda_file::Type {panda_file::Type::TypeId::I32},
38 },
39 Method::Proto::RefTypeVector {}));
40
41 EtsMethodSignature simple("BCSIJFD:Z");
42 EXPECT_EQ(simple.GetProto(), Method::Proto(
43 Method::Proto::ShortyVector {
44 panda_file::Type {panda_file::Type::TypeId::U1},
45 panda_file::Type {panda_file::Type::TypeId::I8},
46 panda_file::Type {panda_file::Type::TypeId::U16},
47 panda_file::Type {panda_file::Type::TypeId::I16},
48 panda_file::Type {panda_file::Type::TypeId::I32},
49 panda_file::Type {panda_file::Type::TypeId::I64},
50 panda_file::Type {panda_file::Type::TypeId::F32},
51 panda_file::Type {panda_file::Type::TypeId::F64},
52 },
53 Method::Proto::RefTypeVector {}));
54 }
55
56 } // namespace
57
58 // NOTE(a.urakov): move initialization to a common internal objects testing base class
59 class EtsMethodSignatureTest : public testing::Test {
60 public:
EtsMethodSignatureTest()61 EtsMethodSignatureTest()
62 {
63 RuntimeOptions options;
64 options.SetShouldLoadBootPandaFiles(false);
65 options.SetShouldInitializeIntrinsics(false);
66 options.SetCompilerEnableJit(false);
67 options.SetGcType("epsilon");
68 options.SetLoadRuntimes({"ets"});
69
70 Runtime::Create(options);
71 }
72
~EtsMethodSignatureTest()73 ~EtsMethodSignatureTest() override
74 {
75 Runtime::Destroy();
76 }
77
78 NO_COPY_SEMANTIC(EtsMethodSignatureTest);
79 NO_MOVE_SEMANTIC(EtsMethodSignatureTest);
80 };
81
TEST_F(EtsMethodSignatureTest,MethodSignature)82 TEST_F(EtsMethodSignatureTest, MethodSignature)
83 {
84 MethodSignaturePrologue();
85
86 EtsMethodSignature arrays("SB[J[Lstd/core/String;I:LT;");
87 EXPECT_EQ(arrays.GetProto(), Method::Proto(
88 Method::Proto::ShortyVector {
89 panda_file::Type {panda_file::Type::TypeId::REFERENCE},
90 panda_file::Type {panda_file::Type::TypeId::I16},
91 panda_file::Type {panda_file::Type::TypeId::I8},
92 panda_file::Type {panda_file::Type::TypeId::REFERENCE},
93 panda_file::Type {panda_file::Type::TypeId::REFERENCE},
94 panda_file::Type {panda_file::Type::TypeId::I32},
95 },
96 Method::Proto::RefTypeVector {
97 std::string_view {"LT;"},
98 std::string_view {"[J"},
99 std::string_view {"[Lstd/core/String;"},
100 }));
101
102 EtsMethodSignature multiarr("[[[LT;:[[I");
103 EXPECT_EQ(multiarr.GetProto(), Method::Proto(
104 Method::Proto::ShortyVector {
105 panda_file::Type {panda_file::Type::TypeId::REFERENCE},
106 panda_file::Type {panda_file::Type::TypeId::REFERENCE},
107 },
108 Method::Proto::RefTypeVector {
109 std::string_view {"[[I"},
110 std::string_view {"[[[LT;"},
111 }));
112 EtsMethodSignature big("J[BI[CI[IZ:I");
113 EXPECT_EQ(big.GetProto(), Method::Proto(
114 Method::Proto::ShortyVector {
115 panda_file::Type {panda_file::Type::TypeId::I32},
116 panda_file::Type {panda_file::Type::TypeId::I64},
117 panda_file::Type {panda_file::Type::TypeId::REFERENCE},
118 panda_file::Type {panda_file::Type::TypeId::I32},
119 panda_file::Type {panda_file::Type::TypeId::REFERENCE},
120 panda_file::Type {panda_file::Type::TypeId::I32},
121 panda_file::Type {panda_file::Type::TypeId::REFERENCE},
122 panda_file::Type {panda_file::Type::TypeId::U1},
123 },
124 Method::Proto::RefTypeVector {
125 std::string_view {"[B"},
126 std::string_view {"[C"},
127 std::string_view {"[I"},
128 }));
129 }
130
TEST_F(EtsMethodSignatureTest,InvalidMethodSignature)131 TEST_F(EtsMethodSignatureTest, InvalidMethodSignature)
132 {
133 EtsMethodSignature invalid1("");
134 EXPECT_FALSE(invalid1.IsValid());
135
136 EtsMethodSignature invalid2(":");
137 EXPECT_FALSE(invalid2.IsValid());
138
139 EtsMethodSignature invalid3(":L");
140 EXPECT_FALSE(invalid3.IsValid());
141
142 EtsMethodSignature invalid4("[]");
143 EXPECT_FALSE(invalid4.IsValid());
144
145 EtsMethodSignature invalid5("IL:J");
146 EXPECT_FALSE(invalid5.IsValid());
147
148 EtsMethodSignature invalid6("[:S");
149 EXPECT_FALSE(invalid6.IsValid());
150
151 EtsMethodSignature invalid7("V:Lg;");
152 EXPECT_FALSE(invalid7.IsValid());
153
154 EtsMethodSignature invalid8("L;:[");
155 EXPECT_FALSE(invalid8.IsValid());
156
157 EtsMethodSignature invalid9("::");
158 EXPECT_FALSE(invalid9.IsValid());
159
160 EtsMethodSignature invalid10("I:I:Z");
161 EXPECT_FALSE(invalid10.IsValid());
162 }
163
164 } // namespace ark::ets::test