Skip to content

Commit 8d2490b

Browse files
chore: tests for errors decode
1 parent 3151678 commit 8d2490b

File tree

2 files changed

+187
-56
lines changed

2 files changed

+187
-56
lines changed
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
//
2+
// ABIElementErrorDecodingTest.swift
3+
//
4+
// Created by JeneaVranceanu on 28.11.2022.
5+
//
6+
7+
import Foundation
8+
import XCTest
9+
import Core
10+
11+
class ABIElementErrorDecodingTest: XCTestCase {
12+
typealias EthError = ABI.Element.EthError
13+
14+
/// Function with any parameters should be able to decode `require` and `revert` calls in soliditiy.
15+
/// Note: `require(expression)` and `revert()` without a message return 0 bytes thus we cannot guarantee
16+
/// that 0 bytes response will be interpreted correctly.
17+
private let emptyFunction = ABI.Element.Function(name: "any",
18+
inputs: [],
19+
outputs: [],
20+
constant: false,
21+
payable: false)
22+
private let oneOutputFunction = ABI.Element.Function(name: "any",
23+
inputs: [],
24+
outputs: [.init(name: "", type: .bool)],
25+
constant: false,
26+
payable: false)
27+
28+
func testErrorRepresentation() {
29+
XCTAssertEqual(EthError(name: "Error", inputs: []).errorDeclaration, "Error()")
30+
XCTAssertEqual(EthError(name: "Error", inputs: [.init(name: "", type: .address)]).errorDeclaration, "Error(address)")
31+
XCTAssertEqual(EthError(name: "Error", inputs: [.init(name: " ", type: .address)]).errorDeclaration, "Error(address)")
32+
XCTAssertEqual(EthError(name: "Error", inputs: [.init(name: " ", type: .address), .init(name: "", type: .uint(bits: 256))]).errorDeclaration, "Error(address,uint256)")
33+
XCTAssertEqual(EthError(name: "Error", inputs: [.init(name: "sender", type: .address), .init(name: " ", type: .uint(bits: 256))]).errorDeclaration, "Error(address sender,uint256)")
34+
// Not all types are supported in errors, e.g. tuples and functions are not supported
35+
let allTypesNamedAndNot: [ABI.Element.InOut] = [
36+
.init(name: "sender", type: .address),
37+
.init(name: "", type: .address),
38+
.init(name: "", type: .uint(bits: 8)),
39+
.init(name: "", type: .uint(bits: 16)),
40+
.init(name: "", type: .uint(bits: 32)),
41+
.init(name: "", type: .uint(bits: 64)),
42+
.init(name: "", type: .uint(bits: 128)),
43+
.init(name: "", type: .uint(bits: 256)),
44+
.init(name: "my_int_8", type: .int(bits: 8)),
45+
.init(name: "my_int_16", type: .int(bits: 16)),
46+
.init(name: "my_int_32", type: .int(bits: 32)),
47+
.init(name: "my_int_64", type: .int(bits: 64)),
48+
.init(name: "my_int_128", type: .int(bits: 128)),
49+
.init(name: "my_int_256", type: .int(bits: 256)),
50+
.init(name: "someFlag", type: .bool),
51+
.init(name: "rand_bytes", type: .bytes(length: 123)),
52+
.init(name: "", type: .dynamicBytes),
53+
.init(name: "arrarrarray123", type: .array(type: .bool, length: 0)),
54+
.init(name: "error_message_maybe", type: .string),
55+
]
56+
XCTAssertEqual(EthError(name: "VeryCustomErrorName",
57+
inputs: allTypesNamedAndNot).errorDeclaration,
58+
"VeryCustomErrorName(address sender,address,uint8,uint16,uint32,uint64,uint128,uint256,int8 my_int_8,int16 my_int_16,int32 my_int_32,int64 my_int_64,int128 my_int_128,int256 my_int_256,bool someFlag,bytes123 rand_bytes,bytes,bool[] arrarrarray123,string error_message_maybe)")
59+
}
60+
61+
/// Empty Data is not decoded as a call of `revert` or `require` if function has no outputs.
62+
/// If a function that has no outputs attempts to decode empty `revert` or `require` must return `nil`
63+
/// because we don't know just based on the output if the call was successful or reverted.
64+
func testDecodeEmptyErrorOnNoOutputFunction() {
65+
XCTAssertTrue(emptyFunction.decodeErrorResponse(Data()) == nil)
66+
}
67+
68+
func testDecodeEmptyErrorOnOneOutputFunction() {
69+
guard let errorData = oneOutputFunction.decodeErrorResponse(Data()) else {
70+
XCTFail("Empty Data must be decoded as a `revert()` or `require(false)` call if function used to decode it has at least one output parameter.")
71+
return
72+
}
73+
74+
XCTAssertEqual(errorData["_success"] as? Bool, false)
75+
XCTAssertNotNil(errorData["_failureReason"] as? String)
76+
77+
let decodedOutput = oneOutputFunction.decodeReturnData(Data())
78+
79+
XCTAssertEqual(errorData["_success"] as? Bool, decodedOutput["_success"] as? Bool)
80+
XCTAssertEqual(errorData["_failureReason"] as? String, decodedOutput["_failureReason"] as? String)
81+
}
82+
83+
/// Data is decoded as a call of `revert` or `require` with a message no matter the number of outputs configured in the ``ABI/Element/Function``.
84+
/// `revert(message)` and `require(false,message)`return at least 128 bytes. We cannot differentiate between `require` or `revert`.
85+
func testDecodeDefaultErrorWithMessage() {
86+
/// 08c379a0 - Error(string) function selector
87+
/// 0000000000000000000000000000000000000000000000000000000000000020 - Data offset
88+
/// 000000000000000000000000000000000000000000000000000000000000001a - Message length
89+
/// 4e6f7420656e6f7567682045746865722070726f76696465642e000000000000 - Message + 0 bytes padding
90+
/// 0000... - some more 0 bytes padding to make the number of bytes match 32 bytes chunks
91+
let errorResponse = Data.fromHex("08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001a4e6f7420656e6f7567682045746865722070726f76696465642e00000000000000000000000000000000000000000000000000000000000000000000")!
92+
guard let errorData = emptyFunction.decodeErrorResponse(errorResponse) else {
93+
XCTFail("Data must be decoded as a `revert(\"Not enough Ether provided.\")` or `require(false, \"Not enough Ether provided.\")` but decoding failed completely.")
94+
return
95+
}
96+
97+
XCTAssertEqual(errorData["_success"] as? Bool, false)
98+
XCTAssertEqual(errorData["_abortedByRevertOrRequire"] as? Bool, true)
99+
XCTAssertEqual(errorData["_errorMessage"] as? String, "Not enough Ether provided.")
100+
XCTAssertNotNil(errorData["_failureReason"] as? String)
101+
102+
let decodedOutput = oneOutputFunction.decodeReturnData(errorResponse)
103+
104+
XCTAssertEqual(errorData["_success"] as? Bool, decodedOutput["_success"] as? Bool)
105+
XCTAssertEqual(errorData["_failureReason"] as? String, decodedOutput["_failureReason"] as? String)
106+
XCTAssertEqual(errorData["_abortedByRevertOrRequire"] as? Bool, decodedOutput["_abortedByRevertOrRequire"] as? Bool)
107+
XCTAssertEqual(errorData["_errorMessage"] as? String, decodedOutput["_errorMessage"] as? String)
108+
XCTAssertEqual(decodedOutput["_errorMessage"] as? String, "Not enough Ether provided.")
109+
}
110+
111+
/// Data is decoded as a call of `revert Unauthorized()`. Decoded only if custom error ABI is given.
112+
func testDecodeRevertWithCustomError() {
113+
/// 82b42900 - Unauthorized() function selector
114+
/// 00000000000000000000000000000000000000000000000000000000 - padding bytes
115+
let errorResponse = Data.fromHex("82b4290000000000000000000000000000000000000000000000000000000000")!
116+
let errors: [String: EthError] = ["82b42900" : .init(name: "Unauthorized", inputs: [])]
117+
guard let errorData = emptyFunction.decodeErrorResponse(errorResponse, errors: errors) else {
118+
XCTFail("Data must be decoded as a `revert(\"Not enough Ether provided.\")` or `require(false, \"Not enough Ether provided.\")` but decoding failed completely.")
119+
return
120+
}
121+
122+
XCTAssertEqual(errorData["_success"] as? Bool, false)
123+
XCTAssertEqual(errorData["_abortedByRevertOrRequire"] as? Bool, true)
124+
XCTAssertEqual(errorData["_error"] as? String, "Unauthorized()")
125+
126+
let decodedOutput = oneOutputFunction.decodeReturnData(errorResponse, errors: errors)
127+
128+
XCTAssertEqual(errorData["_success"] as? Bool, decodedOutput["_success"] as? Bool)
129+
XCTAssertEqual(errorData["_abortedByRevertOrRequire"] as? Bool, decodedOutput["_abortedByRevertOrRequire"] as? Bool)
130+
XCTAssertEqual(errorData["_error"] as? String, decodedOutput["_error"] as? String)
131+
}
132+
133+
/// Data is decoded as a call of `revert Unauthorized()`. Decoded only if custom error ABI is given.
134+
/// Trying to decode as `Unauthorized(string)`. Must fail.
135+
func testDecodeRevertWithCustomErrorFailed() {
136+
/// 82b42900 - Unauthorized() function selector
137+
/// 00000000000000000000000000000000000000000000000000000000 - padding bytes
138+
let errorResponse = Data.fromHex("82b4290000000000000000000000000000000000000000000000000000000000")!
139+
let errors: [String: EthError] = ["82b42900" : .init(name: "Unauthorized", inputs: [.init(name: "", type: .string)])]
140+
guard let errorData = emptyFunction.decodeErrorResponse(errorResponse, errors: errors) else {
141+
XCTFail("Data must be decoded as a `revert(\"Not enough Ether provided.\")` or `require(false, \"Not enough Ether provided.\")` but decoding failed completely.")
142+
return
143+
}
144+
145+
XCTAssertEqual(errorData["_success"] as? Bool, false)
146+
XCTAssertEqual(errorData["_abortedByRevertOrRequire"] as? Bool, true)
147+
XCTAssertEqual(errorData["_error"] as? String, "Unauthorized(string)")
148+
XCTAssertEqual(errorData["_parsingError"] as? String, "Data matches Unauthorized(string) but failed to be decoded.")
149+
150+
let decodedOutput = oneOutputFunction.decodeReturnData(errorResponse, errors: errors)
151+
152+
XCTAssertEqual(errorData["_success"] as? Bool, decodedOutput["_success"] as? Bool)
153+
XCTAssertEqual(errorData["_abortedByRevertOrRequire"] as? Bool, decodedOutput["_abortedByRevertOrRequire"] as? Bool)
154+
XCTAssertEqual(errorData["_error"] as? String, decodedOutput["_error"] as? String)
155+
XCTAssertEqual(errorData["_parsingError"] as? String, decodedOutput["_parsingError"] as? String)
156+
}
157+
158+
/// Data is decoded as a call of `revert Unauthorized("Reason")`. Decoded only if custom error ABI is given.
159+
/// The custom error argument must be extractable by index and name if the name is available.
160+
func testDecodeRevertWithCustomErrorWithArguments() {
161+
/// 973d02cb - `Unauthorized(string)` function selector
162+
/// 0000000000000000000000000000000000000000000000000000000000000020 - data offset
163+
/// 0000000000000000000000000000000000000000000000000000000000000006 - first custom argument length
164+
/// 526561736f6e0000000000000000000000000000000000000000000000000000 - first custom argument bytes + 0 bytes padding
165+
/// 0000... - some more 0 bytes padding to make the number of bytes match 32 bytes chunks
166+
let errorResponse = Data.fromHex("973d02cb00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000006526561736f6e000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")!
167+
let errors: [String: EthError] = ["973d02cb" : .init(name: "Unauthorized", inputs: [.init(name: "message_arg", type: .string)])]
168+
guard let errorData = emptyFunction.decodeErrorResponse(errorResponse, errors: errors) else {
169+
XCTFail("Data must be decoded as a `revert(\"Not enough Ether provided.\")` or `require(false, \"Not enough Ether provided.\")` but decoding failed completely.")
170+
return
171+
}
172+
173+
XCTAssertEqual(errorData["_success"] as? Bool, false)
174+
XCTAssertEqual(errorData["_abortedByRevertOrRequire"] as? Bool, true)
175+
XCTAssertEqual(errorData["_error"] as? String, "Unauthorized(string message_arg)")
176+
XCTAssertEqual(errorData["0"] as? String, "Reason")
177+
XCTAssertEqual(errorData["0"] as? String, errorData["message_arg"] as? String)
178+
179+
let decodedOutput = oneOutputFunction.decodeReturnData(errorResponse, errors: errors)
180+
181+
XCTAssertEqual(errorData["_success"] as? Bool, decodedOutput["_success"] as? Bool)
182+
XCTAssertEqual(errorData["_abortedByRevertOrRequire"] as? Bool, decodedOutput["_abortedByRevertOrRequire"] as? Bool)
183+
XCTAssertEqual(errorData["_error"] as? String, decodedOutput["_error"] as? String)
184+
XCTAssertEqual(errorData["0"] as? String, decodedOutput["0"] as? String)
185+
XCTAssertEqual(errorData["message_arg"] as? String, decodedOutput["message_arg"] as? String)
186+
}
187+
}

Tests/web3swiftTests/localTests/ABIElementsTests.swift

Lines changed: 0 additions & 56 deletions
This file was deleted.

0 commit comments

Comments
 (0)