1 /*
2 This file is part of solidity.
3
4 solidity is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 solidity is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with solidity. If not, see <http://www.gnu.org/licenses/>.
16 */
17 // SPDX-License-Identifier: GPL-3.0
18 /**
19 * @author Christian <c@ethdev.com>
20 * @date 2015
21 * Unit tests for the type system of Solidity.
22 */
23
24 #include <libsolidity/ast/Types.h>
25 #include <libsolidity/ast/TypeProvider.h>
26 #include <libsolidity/ast/AST.h>
27 #include <libsolutil/Keccak256.h>
28 #include <boost/test/unit_test.hpp>
29
30 using namespace std;
31 using namespace solidity::langutil;
32
33 namespace solidity::frontend::test
34 {
35
36 BOOST_AUTO_TEST_SUITE(SolidityTypes)
37
BOOST_AUTO_TEST_CASE(int_types)38 BOOST_AUTO_TEST_CASE(int_types)
39 {
40 BOOST_CHECK(*TypeProvider::fromElementaryTypeName(ElementaryTypeNameToken(Token::Int, 0, 0)) == *TypeProvider::integer(256, IntegerType::Modifier::Signed));
41 for (unsigned i = 8; i <= 256; i += 8)
42 BOOST_CHECK(*TypeProvider::fromElementaryTypeName(ElementaryTypeNameToken(Token::IntM, i, 0)) == *TypeProvider::integer(i, IntegerType::Modifier::Signed));
43 }
44
BOOST_AUTO_TEST_CASE(uint_types)45 BOOST_AUTO_TEST_CASE(uint_types)
46 {
47 BOOST_CHECK(*TypeProvider::fromElementaryTypeName(ElementaryTypeNameToken(Token::UInt, 0, 0)) == *TypeProvider::integer(256, IntegerType::Modifier::Unsigned));
48 for (unsigned i = 8; i <= 256; i += 8)
49 BOOST_CHECK(*TypeProvider::fromElementaryTypeName(ElementaryTypeNameToken(Token::UIntM, i, 0)) == *TypeProvider::integer(i, IntegerType::Modifier::Unsigned));
50 }
51
BOOST_AUTO_TEST_CASE(byte_types)52 BOOST_AUTO_TEST_CASE(byte_types)
53 {
54 for (unsigned i = 1; i <= 32; i++)
55 BOOST_CHECK(*TypeProvider::fromElementaryTypeName(ElementaryTypeNameToken(Token::BytesM, i, 0)) == *TypeProvider::fixedBytes(i));
56 }
57
BOOST_AUTO_TEST_CASE(fixed_types)58 BOOST_AUTO_TEST_CASE(fixed_types)
59 {
60 BOOST_CHECK(*TypeProvider::fromElementaryTypeName(ElementaryTypeNameToken(Token::Fixed, 0, 0)) == *TypeProvider::fixedPoint(128, 18, FixedPointType::Modifier::Signed));
61 for (unsigned i = 8; i <= 256; i += 8)
62 {
63 BOOST_CHECK(*TypeProvider::fromElementaryTypeName(ElementaryTypeNameToken(Token::FixedMxN, i, 0)) == *TypeProvider::fixedPoint(i, 0, FixedPointType::Modifier::Signed));
64 BOOST_CHECK(*TypeProvider::fromElementaryTypeName(ElementaryTypeNameToken(Token::FixedMxN, i, 2)) == *TypeProvider::fixedPoint(i, 2, FixedPointType::Modifier::Signed));
65 }
66 }
67
BOOST_AUTO_TEST_CASE(ufixed_types)68 BOOST_AUTO_TEST_CASE(ufixed_types)
69 {
70 BOOST_CHECK(*TypeProvider::fromElementaryTypeName(ElementaryTypeNameToken(Token::UFixed, 0, 0)) == *TypeProvider::fixedPoint(128, 18, FixedPointType::Modifier::Unsigned));
71 for (unsigned i = 8; i <= 256; i += 8)
72 {
73 BOOST_CHECK(*TypeProvider::fromElementaryTypeName(ElementaryTypeNameToken(Token::UFixedMxN, i, 0)) == *TypeProvider::fixedPoint(i, 0, FixedPointType::Modifier::Unsigned));
74 BOOST_CHECK(*TypeProvider::fromElementaryTypeName(ElementaryTypeNameToken(Token::UFixedMxN, i, 2)) == *TypeProvider::fixedPoint(i, 2, FixedPointType::Modifier::Unsigned));
75 }
76 }
77
BOOST_AUTO_TEST_CASE(storage_layout_simple)78 BOOST_AUTO_TEST_CASE(storage_layout_simple)
79 {
80 MemberList members(MemberList::MemberMap({
81 {"first", TypeProvider::fromElementaryTypeName("uint128")},
82 {"second", TypeProvider::fromElementaryTypeName("uint120")},
83 {"wraps", TypeProvider::fromElementaryTypeName("uint16")}
84 }));
85 BOOST_REQUIRE_EQUAL(u256(2), members.storageSize());
86 BOOST_REQUIRE(members.memberStorageOffset("first") != nullptr);
87 BOOST_REQUIRE(members.memberStorageOffset("second") != nullptr);
88 BOOST_REQUIRE(members.memberStorageOffset("wraps") != nullptr);
89 BOOST_CHECK(*members.memberStorageOffset("first") == make_pair(u256(0), unsigned(0)));
90 BOOST_CHECK(*members.memberStorageOffset("second") == make_pair(u256(0), unsigned(16)));
91 BOOST_CHECK(*members.memberStorageOffset("wraps") == make_pair(u256(1), unsigned(0)));
92 }
93
BOOST_AUTO_TEST_CASE(storage_layout_mapping)94 BOOST_AUTO_TEST_CASE(storage_layout_mapping)
95 {
96 MemberList members(MemberList::MemberMap({
97 {"first", TypeProvider::fromElementaryTypeName("uint128")},
98 {"second", TypeProvider::mapping(
99 TypeProvider::fromElementaryTypeName("uint8"),
100 TypeProvider::fromElementaryTypeName("uint8")
101 )},
102 {"third", TypeProvider::fromElementaryTypeName("uint16")},
103 {"final", TypeProvider::mapping(
104 TypeProvider::fromElementaryTypeName("uint8"),
105 TypeProvider::fromElementaryTypeName("uint8")
106 )},
107 }));
108 BOOST_REQUIRE_EQUAL(u256(4), members.storageSize());
109 BOOST_REQUIRE(members.memberStorageOffset("first") != nullptr);
110 BOOST_REQUIRE(members.memberStorageOffset("second") != nullptr);
111 BOOST_REQUIRE(members.memberStorageOffset("third") != nullptr);
112 BOOST_REQUIRE(members.memberStorageOffset("final") != nullptr);
113 BOOST_CHECK(*members.memberStorageOffset("first") == make_pair(u256(0), unsigned(0)));
114 BOOST_CHECK(*members.memberStorageOffset("second") == make_pair(u256(1), unsigned(0)));
115 BOOST_CHECK(*members.memberStorageOffset("third") == make_pair(u256(2), unsigned(0)));
116 BOOST_CHECK(*members.memberStorageOffset("final") == make_pair(u256(3), unsigned(0)));
117 }
118
BOOST_AUTO_TEST_CASE(storage_layout_arrays)119 BOOST_AUTO_TEST_CASE(storage_layout_arrays)
120 {
121 BOOST_CHECK(ArrayType(DataLocation::Storage, TypeProvider::fixedBytes(1), 32).storageSize() == 1);
122 BOOST_CHECK(ArrayType(DataLocation::Storage, TypeProvider::fixedBytes(1), 33).storageSize() == 2);
123 BOOST_CHECK(ArrayType(DataLocation::Storage, TypeProvider::fixedBytes(2), 31).storageSize() == 2);
124 BOOST_CHECK(ArrayType(DataLocation::Storage, TypeProvider::fixedBytes(7), 8).storageSize() == 2);
125 BOOST_CHECK(ArrayType(DataLocation::Storage, TypeProvider::fixedBytes(7), 9).storageSize() == 3);
126 BOOST_CHECK(ArrayType(DataLocation::Storage, TypeProvider::fixedBytes(31), 9).storageSize() == 9);
127 BOOST_CHECK(ArrayType(DataLocation::Storage, TypeProvider::fixedBytes(32), 9).storageSize() == 9);
128 }
129
BOOST_AUTO_TEST_CASE(type_identifier_escaping)130 BOOST_AUTO_TEST_CASE(type_identifier_escaping)
131 {
132 BOOST_CHECK_EQUAL(Type::escapeIdentifier("("), "$_");
133 BOOST_CHECK_EQUAL(Type::escapeIdentifier(")"), "_$");
134 BOOST_CHECK_EQUAL(Type::escapeIdentifier(","), "_$_");
135 BOOST_CHECK_EQUAL(Type::escapeIdentifier("$"), "$$$");
136 BOOST_CHECK_EQUAL(Type::escapeIdentifier(")$("), "_$$$$$_");
137 BOOST_CHECK_EQUAL(Type::escapeIdentifier("()"), "$__$");
138 BOOST_CHECK_EQUAL(Type::escapeIdentifier("(,)"), "$__$__$");
139 BOOST_CHECK_EQUAL(Type::escapeIdentifier("(,$,)"), "$__$_$$$_$__$");
140 BOOST_CHECK_EQUAL(
141 Type::escapeIdentifier("((__(_$_$$,__($$,,,$$),$,,,)))$$,$$"),
142 "$_$___$__$$$_$$$$$$_$___$_$$$$$$_$__$__$_$$$$$$_$_$_$$$_$__$__$__$_$_$$$$$$$_$_$$$$$$"
143 );
144 }
145
BOOST_AUTO_TEST_CASE(type_identifiers)146 BOOST_AUTO_TEST_CASE(type_identifiers)
147 {
148 int64_t id = 0;
149
150 BOOST_CHECK_EQUAL(TypeProvider::fromElementaryTypeName("uint128")->identifier(), "t_uint128");
151 BOOST_CHECK_EQUAL(TypeProvider::fromElementaryTypeName("int128")->identifier(), "t_int128");
152 BOOST_CHECK_EQUAL(TypeProvider::fromElementaryTypeName("address")->identifier(), "t_address");
153 BOOST_CHECK_EQUAL(TypeProvider::fromElementaryTypeName("uint8")->identifier(), "t_uint8");
154 BOOST_CHECK_EQUAL(TypeProvider::fromElementaryTypeName("ufixed64x2")->identifier(), "t_ufixed64x2");
155 BOOST_CHECK_EQUAL(TypeProvider::fromElementaryTypeName("fixed128x8")->identifier(), "t_fixed128x8");
156 BOOST_CHECK_EQUAL(RationalNumberType(rational(7, 1)).identifier(), "t_rational_7_by_1");
157 BOOST_CHECK_EQUAL(RationalNumberType(rational(200, 77)).identifier(), "t_rational_200_by_77");
158 BOOST_CHECK_EQUAL(RationalNumberType(rational(2 * 200, 2 * 77)).identifier(), "t_rational_200_by_77");
159 BOOST_CHECK_EQUAL(RationalNumberType(rational(-2 * 200, 2 * 77)).identifier(), "t_rational_minus_200_by_77");
160 BOOST_CHECK_EQUAL(
161 StringLiteralType(Literal(++id, SourceLocation{}, Token::StringLiteral, make_shared<string>("abc - def"))).identifier(),
162 "t_stringliteral_196a9142ee0d40e274a6482393c762b16dd8315713207365e1e13d8d85b74fc4"
163 );
164 BOOST_CHECK_EQUAL(TypeProvider::fromElementaryTypeName("bytes1")->identifier(), "t_bytes1");
165 BOOST_CHECK_EQUAL(TypeProvider::fromElementaryTypeName("bytes8")->identifier(), "t_bytes8");
166 BOOST_CHECK_EQUAL(TypeProvider::fromElementaryTypeName("bytes32")->identifier(), "t_bytes32");
167 BOOST_CHECK_EQUAL(TypeProvider::fromElementaryTypeName("bool")->identifier(), "t_bool");
168 BOOST_CHECK_EQUAL(TypeProvider::fromElementaryTypeName("bytes")->identifier(), "t_bytes_storage_ptr");
169 BOOST_CHECK_EQUAL(TypeProvider::fromElementaryTypeName("bytes memory")->identifier(), "t_bytes_memory_ptr");
170 BOOST_CHECK_EQUAL(TypeProvider::fromElementaryTypeName("bytes storage")->identifier(), "t_bytes_storage_ptr");
171 BOOST_CHECK_EQUAL(TypeProvider::fromElementaryTypeName("bytes calldata")->identifier(), "t_bytes_calldata_ptr");
172 BOOST_CHECK_EQUAL(TypeProvider::fromElementaryTypeName("string")->identifier(), "t_string_storage_ptr");
173 BOOST_CHECK_EQUAL(TypeProvider::fromElementaryTypeName("string memory")->identifier(), "t_string_memory_ptr");
174 BOOST_CHECK_EQUAL(TypeProvider::fromElementaryTypeName("string storage")->identifier(), "t_string_storage_ptr");
175 BOOST_CHECK_EQUAL(TypeProvider::fromElementaryTypeName("string calldata")->identifier(), "t_string_calldata_ptr");
176 ArrayType largeintArray(DataLocation::Memory, TypeProvider::fromElementaryTypeName("int128"), u256("2535301200456458802993406410752"));
177 BOOST_CHECK_EQUAL(largeintArray.identifier(), "t_array$_t_int128_$2535301200456458802993406410752_memory_ptr");
178 Type const* stringArray = TypeProvider::array(DataLocation::Storage, TypeProvider::fromElementaryTypeName("string"), u256("20"));
179 Type const* multiArray = TypeProvider::array(DataLocation::Storage, stringArray);
180 BOOST_CHECK_EQUAL(multiArray->identifier(), "t_array$_t_array$_t_string_storage_$20_storage_$dyn_storage_ptr");
181
182 ContractDefinition c(++id, SourceLocation{}, make_shared<string>("MyContract$"), SourceLocation{}, {}, {}, {}, ContractKind::Contract);
183 BOOST_CHECK_EQUAL(c.type()->identifier(), "t_type$_t_contract$_MyContract$$$_$2_$");
184 BOOST_CHECK_EQUAL(ContractType(c, true).identifier(), "t_super$_MyContract$$$_$2");
185
186 StructDefinition s(++id, {}, make_shared<string>("Struct"), {}, {});
187 s.annotation().recursive = false;
188 BOOST_CHECK_EQUAL(s.type()->identifier(), "t_type$_t_struct$_Struct_$3_storage_ptr_$");
189
190 EnumDefinition e(++id, {}, make_shared<string>("Enum"), {}, {});
191 BOOST_CHECK_EQUAL(e.type()->identifier(), "t_type$_t_enum$_Enum_$4_$");
192
193 TupleType t({e.type(), s.type(), stringArray, nullptr});
194 BOOST_CHECK_EQUAL(t.identifier(), "t_tuple$_t_type$_t_enum$_Enum_$4_$_$_t_type$_t_struct$_Struct_$3_storage_ptr_$_$_t_array$_t_string_storage_$20_storage_ptr_$__$");
195
196 Type const* keccak256fun = TypeProvider::function(strings{}, strings{}, FunctionType::Kind::KECCAK256);
197 BOOST_CHECK_EQUAL(keccak256fun->identifier(), "t_function_keccak256_nonpayable$__$returns$__$");
198
199 FunctionType metaFun(TypePointers{keccak256fun}, TypePointers{s.type()}, strings{""}, strings{""});
200 BOOST_CHECK_EQUAL(metaFun.identifier(), "t_function_internal_nonpayable$_t_function_keccak256_nonpayable$__$returns$__$_$returns$_t_type$_t_struct$_Struct_$3_storage_ptr_$_$");
201
202 Type const* m = TypeProvider::mapping(TypeProvider::fromElementaryTypeName("bytes32"), s.type());
203 MappingType m2(TypeProvider::fromElementaryTypeName("uint64"), m);
204 BOOST_CHECK_EQUAL(m2.identifier(), "t_mapping$_t_uint64_$_t_mapping$_t_bytes32_$_t_type$_t_struct$_Struct_$3_storage_ptr_$_$_$");
205
206 // TypeType is tested with contract
207
208 auto emptyParams = make_shared<ParameterList>(++id, SourceLocation(), std::vector<ASTPointer<VariableDeclaration>>());
209 ModifierDefinition mod(++id, SourceLocation{}, make_shared<string>("modif"), SourceLocation{}, {}, emptyParams, {}, {}, {});
210 BOOST_CHECK_EQUAL(ModifierType(mod).identifier(), "t_modifier$__$");
211
212 SourceUnit su(++id, {}, {}, {});
213 BOOST_CHECK_EQUAL(ModuleType(su).identifier(), "t_module_7");
214 BOOST_CHECK_EQUAL(MagicType(MagicType::Kind::Block).identifier(), "t_magic_block");
215 BOOST_CHECK_EQUAL(MagicType(MagicType::Kind::Message).identifier(), "t_magic_message");
216 BOOST_CHECK_EQUAL(MagicType(MagicType::Kind::Transaction).identifier(), "t_magic_transaction");
217
218 BOOST_CHECK_EQUAL(InaccessibleDynamicType().identifier(), "t_inaccessible");
219 }
220
BOOST_AUTO_TEST_CASE(encoded_sizes)221 BOOST_AUTO_TEST_CASE(encoded_sizes)
222 {
223 BOOST_CHECK_EQUAL(IntegerType(16).calldataEncodedSize(true), 32);
224 BOOST_CHECK_EQUAL(IntegerType(16).calldataEncodedSize(false), 2);
225
226 BOOST_CHECK_EQUAL(FixedBytesType(16).calldataEncodedSize(true), 32);
227 BOOST_CHECK_EQUAL(FixedBytesType(16).calldataEncodedSize(false), 16);
228
229 BOOST_CHECK_EQUAL(BoolType().calldataEncodedSize(true), 32);
230 BOOST_CHECK_EQUAL(BoolType().calldataEncodedSize(false), 1);
231
232 ArrayType const* uint24Array = TypeProvider::array(
233 DataLocation::Memory,
234 TypeProvider::uint(24),
235 9
236 );
237 BOOST_CHECK_EQUAL(uint24Array->calldataEncodedSize(true), 9 * 32);
238 BOOST_CHECK_EQUAL(uint24Array->calldataEncodedSize(false), 9 * 32);
239
240 ArrayType twoDimArray(DataLocation::Memory, uint24Array, 3);
241 BOOST_CHECK_EQUAL(twoDimArray.calldataEncodedSize(true), 9 * 3 * 32);
242 BOOST_CHECK_EQUAL(twoDimArray.calldataEncodedSize(false), 9 * 3 * 32);
243 }
244
BOOST_AUTO_TEST_CASE(helper_bool_result)245 BOOST_AUTO_TEST_CASE(helper_bool_result)
246 {
247 BoolResult r1{true};
248 BoolResult r2 = BoolResult::err("Failure.");
249 r1.merge(r2, logical_and<bool>());
250 BOOST_REQUIRE_EQUAL(r1.get(), false);
251 BOOST_REQUIRE_EQUAL(r1.message(), "Failure.");
252
253 BoolResult r3{false};
254 BoolResult r4{true};
255 r3.merge(r4, logical_and<bool>());
256 BOOST_REQUIRE_EQUAL(r3.get(), false);
257 BOOST_REQUIRE_EQUAL(r3.message(), "");
258
259 BoolResult r5{true};
260 BoolResult r6{true};
261 r5.merge(r6, logical_and<bool>());
262 BOOST_REQUIRE_EQUAL(r5.get(), true);
263 BOOST_REQUIRE_EQUAL(r5.message(), "");
264 }
265
BOOST_AUTO_TEST_CASE(helper_string_result)266 BOOST_AUTO_TEST_CASE(helper_string_result)
267 {
268 using StringResult = util::Result<string>;
269
270 StringResult r1{string{"Success"}};
271 StringResult r2 = StringResult::err("Failure");
272
273 BOOST_REQUIRE_EQUAL(r1.get(), "Success");
274 BOOST_REQUIRE_EQUAL(r2.get(), "");
275
276 r1.merge(r2, [](string const&, string const& _rhs) { return _rhs; });
277
278 BOOST_REQUIRE_EQUAL(r1.get(), "");
279 BOOST_REQUIRE_EQUAL(r1.message(), "Failure");
280 }
281
282 BOOST_AUTO_TEST_SUITE_END()
283
284 }
285