|
| 1 | +/* |
| 2 | + * Copyright 2019 Web3 Labs Ltd. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 5 | + * the License. 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 distributed under the License is distributed on |
| 10 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | + * specific language governing permissions and limitations under the License. |
| 12 | + */ |
| 13 | +package org.web3j.abi; |
| 14 | + |
| 15 | +import java.util.Arrays; |
| 16 | +import java.util.List; |
| 17 | + |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | + |
| 20 | +import org.web3j.abi.datatypes.Address; |
| 21 | +import org.web3j.abi.datatypes.CustomError; |
| 22 | +import org.web3j.abi.datatypes.DynamicArray; |
| 23 | +import org.web3j.abi.datatypes.Utf8String; |
| 24 | +import org.web3j.abi.datatypes.generated.Uint256; |
| 25 | + |
| 26 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 27 | +import static org.web3j.abi.Utils.convert; |
| 28 | + |
| 29 | +public class CustomErrorEncoderTest { |
| 30 | + @Test |
| 31 | + public void testCalculateSignatureHash() { |
| 32 | + assertEquals( |
| 33 | + CustomErrorEncoder.calculateSignatureHash("InvalidAccess(address,string,uint256)"), |
| 34 | + ("0xcb5157bf1b439b9573ea7a95f7c00cc33f832ed728345c2bd29146ce58bbab57")); |
| 35 | + |
| 36 | + assertEquals( |
| 37 | + CustomErrorEncoder.calculateSignatureHash("RandomError(address[],bytes)"), |
| 38 | + ("0xbf37b77ddf0fbbf29ee6a3ebda3d177c2d438123b10571806c57958230d9f905")); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void testEncode() { |
| 43 | + CustomError error = |
| 44 | + new CustomError( |
| 45 | + "InvalidAccess", |
| 46 | + Arrays.<TypeReference<?>>asList( |
| 47 | + new TypeReference<Address>() {}, |
| 48 | + new TypeReference<Utf8String>() {}, |
| 49 | + new TypeReference<Uint256>() {})); |
| 50 | + |
| 51 | + assertEquals( |
| 52 | + CustomErrorEncoder.encode(error), |
| 53 | + "0xcb5157bf1b439b9573ea7a95f7c00cc33f832ed728345c2bd29146ce58bbab57"); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void testBuildErrorSignature() { |
| 58 | + List<TypeReference<?>> parameters = |
| 59 | + Arrays.<TypeReference<?>>asList( |
| 60 | + new TypeReference<Address>() {}, |
| 61 | + new TypeReference<Utf8String>() {}, |
| 62 | + new TypeReference<Uint256>() {}); |
| 63 | + |
| 64 | + assertEquals( |
| 65 | + "InvalidAccess(address,string,uint256)", |
| 66 | + CustomErrorEncoder.buildErrorSignature("InvalidAccess", convert(parameters))); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void testBuildErrorSignatureWithDynamicStructs() { |
| 71 | + List<TypeReference<?>> parameters = |
| 72 | + Arrays.asList( |
| 73 | + new TypeReference<AbiV2TestFixture.Nazz>() {}, |
| 74 | + new TypeReference<AbiV2TestFixture.Foo>() {}); |
| 75 | + |
| 76 | + assertEquals( |
| 77 | + "DynamicStructError((((string,string)[])[],uint256),(string,string))", |
| 78 | + CustomErrorEncoder.buildErrorSignature("DynamicStructError", convert(parameters))); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void testBuildErrorSignatureWithDynamicArrays() { |
| 83 | + List<TypeReference<?>> parameters = |
| 84 | + Arrays.asList(new TypeReference<DynamicArray<AbiV2TestFixture.Nazz>>() {}); |
| 85 | + |
| 86 | + assertEquals( |
| 87 | + "DynamicArrayError((((string,string)[])[],uint256)[])", |
| 88 | + EventEncoder.buildMethodSignature("DynamicArrayError", convert(parameters))); |
| 89 | + } |
| 90 | +} |
0 commit comments