mirror of
https://github.com/luxfi/safe-ios.git
synced 2026-10-30 15:19:20 +00:00
378 lines
25 KiB
Swift
378 lines
25 KiB
Swift
//
|
|
// File.swift
|
|
//
|
|
//
|
|
// Created by Dmitry Bespalov on 03.01.22.
|
|
//
|
|
|
|
import Foundation
|
|
import Solidity
|
|
import XCTest
|
|
|
|
class TupleEncodingTests: XCTestCase {
|
|
|
|
func testEncodeTwoElementary() {
|
|
// test data from the Solidity docs
|
|
let tuple = Sol.Tuple(elements: [Sol.UInt32(69), Sol.Bool(storage: true)])
|
|
let expected = Data([
|
|
// 0000000000000000000000000000000000000000000000000000000000000045
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x45,
|
|
// 0000000000000000000000000000000000000000000000000000000000000001
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
|
|
])
|
|
let data = tuple.encode()
|
|
XCTAssertEqual(Array(data), Array(expected))
|
|
}
|
|
|
|
func testDecodeTwoElementary() throws {
|
|
// test data from the Solidity docs
|
|
// (uint32,bool) = (69, true)
|
|
let data = Data([
|
|
// 0000000000000000000000000000000000000000000000000000000000000045
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x45,
|
|
// 0000000000000000000000000000000000000000000000000000000000000001
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
|
|
])
|
|
var tuple = Sol.Tuple(elements: [Sol.UInt32(), Sol.Bool()])
|
|
var offset = 0
|
|
try tuple.decode(from: data, offset: &offset)
|
|
continueAfterFailure = false
|
|
XCTAssertEqual(tuple.elements.count, 2)
|
|
XCTAssertEqual(tuple.elements[0] as? Sol.UInt32, 69)
|
|
XCTAssertEqual(tuple.elements[1] as? Sol.Bool, .init(storage: true))
|
|
XCTAssertEqual(offset, 64)
|
|
}
|
|
|
|
func testDecodeBytesBoolUIntArray() throws {
|
|
// test data from the Solidity docs
|
|
// (bytes, bool, uint256[])
|
|
let data = Data([
|
|
// 0000000000000000000000000000000000000000000000000000000000000060
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60,
|
|
|
|
// 0000000000000000000000000000000000000000000000000000000000000001
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
|
|
|
|
// 00000000000000000000000000000000000000000000000000000000000000a0
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa0,
|
|
|
|
// 0000000000000000000000000000000000000000000000000000000000000004
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
|
|
|
|
// 6461766500000000000000000000000000000000000000000000000000000000
|
|
0x64, 0x61, 0x76, 0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
// 0000000000000000000000000000000000000000000000000000000000000003
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
|
|
|
|
// 0000000000000000000000000000000000000000000000000000000000000001
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
|
|
|
|
// 0000000000000000000000000000000000000000000000000000000000000002
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
|
|
|
|
// 0000000000000000000000000000000000000000000000000000000000000003
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
|
|
])
|
|
var tuple = Sol.Tuple(elements: [Sol.Bytes(), Sol.Bool(), Sol.Array<Sol.UInt256>()])
|
|
var offset = 0
|
|
try tuple.decode(from: data, offset: &offset)
|
|
continueAfterFailure = false
|
|
XCTAssertEqual(tuple.elements.count, 3)
|
|
XCTAssertEqual(tuple.elements[0] as? Sol.Bytes, Sol.Bytes(storage: Data([0x64, 0x61, 0x76, 0x65])))
|
|
XCTAssertEqual(tuple.elements[1] as? Sol.Bool, .init(storage: true))
|
|
XCTAssertEqual(tuple.elements[2] as? Sol.Array<Sol.UInt256>, Sol.Array(elements: [1, 2, 3]))
|
|
XCTAssertEqual(offset, 288)
|
|
}
|
|
|
|
func testDecodeExampleWithDynamicParts() throws {
|
|
// (uint,uint32[],bytes10,bytes)
|
|
let data = Data([
|
|
// 0000000000000000000000000000000000000000000000000000000000000123
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x23,
|
|
|
|
// 0000000000000000000000000000000000000000000000000000000000000080
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,
|
|
|
|
// 3132333435363738393000000000000000000000000000000000000000000000
|
|
0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
// 00000000000000000000000000000000000000000000000000000000000000e0
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0,
|
|
|
|
// 0000000000000000000000000000000000000000000000000000000000000002
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
|
|
|
|
// 0000000000000000000000000000000000000000000000000000000000000456
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x56,
|
|
|
|
// 0000000000000000000000000000000000000000000000000000000000000789
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x89,
|
|
|
|
// 000000000000000000000000000000000000000000000000000000000000000d
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0d,
|
|
|
|
// 48656c6c6f2c20776f726c642100000000000000000000000000000000000000
|
|
0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
])
|
|
var tuple = Sol.Tuple(elements: [Sol.UInt256(), Sol.Array<Sol.UInt32>(), Sol.Bytes10(), Sol.Bytes()])
|
|
var offset = 0
|
|
try tuple.decode(from: data, offset: &offset)
|
|
continueAfterFailure = false
|
|
XCTAssertEqual(tuple.elements.count, 4)
|
|
XCTAssertEqual(tuple.elements[0] as? Sol.UInt256, 0x123)
|
|
XCTAssertEqual(tuple.elements[1] as? Sol.Array<Sol.UInt32>, Sol.Array(elements: [0x456, 0x789]))
|
|
XCTAssertEqual(tuple.elements[2] as? Sol.Bytes10, Sol.Bytes10(storage: Data([
|
|
// "0123456789"
|
|
0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30
|
|
])))
|
|
XCTAssertEqual(tuple.elements[3] as? Sol.Bytes, Sol.Bytes(storage: Data([
|
|
// "Hello, world!
|
|
0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21
|
|
])))
|
|
XCTAssertEqual(offset, 288)
|
|
}
|
|
|
|
func testEncodeAnotherExampleWithDynamicParts() {
|
|
// (uint[][],string[]) with values ([[1, 2], [3]], ["one", "two", "three"])
|
|
let tuple = Sol.Tuple(elements: [
|
|
Sol.Array<Sol.Array<Sol.UInt256>>(elements: [
|
|
Sol.Array<Sol.UInt256>(elements: [1, 2]),
|
|
Sol.Array<Sol.UInt256>(elements: [3])
|
|
]),
|
|
Sol.Array<Sol.String>(elements: [
|
|
Sol.String(storage: "one"),
|
|
Sol.String(storage: "two"),
|
|
Sol.String(storage: "three"),
|
|
])
|
|
])
|
|
let expected = Data([
|
|
// 0 - 000000000000000000000000000000000000000000000000000000000000000f - offset of [[1, 2], [3]]
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40,
|
|
// 1 - 000000000000000000000000000000000000000000000000000000000000000g - offset of ["one", "two", "three"]
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x40,
|
|
// 2 - 0000000000000000000000000000000000000000000000000000000000000002 - count for [[1, 2], [3]]
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
|
|
// 3 - 0000000000000000000000000000000000000000000000000000000000000040 - offset of [1, 2]
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40,
|
|
// 4 - 00000000000000000000000000000000000000000000000000000000000000a0 - offset of [3]
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa0,
|
|
// 5 - 0000000000000000000000000000000000000000000000000000000000000002 - count for [1, 2]
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
|
|
// 6 - 0000000000000000000000000000000000000000000000000000000000000001 - encoding of 1
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
|
|
// 7 - 0000000000000000000000000000000000000000000000000000000000000002 - encoding of 2
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
|
|
// 8 - 0000000000000000000000000000000000000000000000000000000000000001 - count for [3]
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
|
|
// 9 - 0000000000000000000000000000000000000000000000000000000000000003 - encoding of 3
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
|
|
// 10 - 0000000000000000000000000000000000000000000000000000000000000003 - count for ["one", "two", "three"]
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
|
|
// 11 - 0000000000000000000000000000000000000000000000000000000000000060 - offset for "one"
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60,
|
|
// 12 - 00000000000000000000000000000000000000000000000000000000000000a0 - offset for "two"
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa0,
|
|
// 13 - 00000000000000000000000000000000000000000000000000000000000000e0 - offset for "three"
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0,
|
|
// 14 - 0000000000000000000000000000000000000000000000000000000000000003 - count for "one"
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
|
|
// 15 - 6f6e650000000000000000000000000000000000000000000000000000000000 - encoding of "one"
|
|
0x6f, 0x6e, 0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
// 16 - 0000000000000000000000000000000000000000000000000000000000000003 - count for "two"
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
|
|
// 17 - 74776f0000000000000000000000000000000000000000000000000000000000 - encoding of "two"
|
|
0x74, 0x77, 0x6f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
// 18 - 0000000000000000000000000000000000000000000000000000000000000005 - count for "three"
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05,
|
|
// 19 - 7468726565000000000000000000000000000000000000000000000000000000 - encoding of "three"
|
|
0x74, 0x68, 0x72, 0x65, 0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
])
|
|
let data = tuple.encode()
|
|
XCTAssertEqual(Array(data), Array(expected))
|
|
}
|
|
|
|
func data(hex str: String) -> Data {
|
|
let value = stride(from: 0, to: str.count, by: 2).compactMap { offset in
|
|
UInt8(str[str.index(str.startIndex, offsetBy: offset)..<str.index(str.startIndex, offsetBy: offset + 2)], radix: 16)
|
|
}
|
|
return Data(value)
|
|
}
|
|
|
|
func testEncodeTupleOfElementaryTuple() {
|
|
// ((uint32, bool)) = ((7, false))
|
|
let tuple = Sol.Tuple(elements: [Sol.Tuple(elements: [Sol.UInt32(7), Sol.Bool(storage: false)])])
|
|
// since all tuple types are elementary, then tuple is static and encoded without additional 'head' elements
|
|
let expected = Data([
|
|
// uint32
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 16 bytes
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, // 32 bytes
|
|
// bool
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 16 bytes
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 32 bytes
|
|
])
|
|
let data = tuple.encode()
|
|
XCTAssertEqual(Array(data), Array(expected))
|
|
}
|
|
|
|
func testDecodeTupleOfElementaryTuple() throws {
|
|
// ((uint32, bool)) = ((7, false))
|
|
let data = Data([
|
|
// uint32
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 16 bytes
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, // 32 bytes
|
|
// bool
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 16 bytes
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 32 bytes
|
|
])
|
|
var tuple = Sol.Tuple(elements: [Sol.Tuple(elements: [Sol.UInt32(), Sol.Bool()])])
|
|
var offset = 0
|
|
try tuple.decode(from: data, offset: &offset)
|
|
continueAfterFailure = false
|
|
XCTAssertEqual(tuple.elements.count, 1)
|
|
XCTAssertEqual((tuple.elements[0] as? Sol.Tuple)?.elements.count, 2)
|
|
XCTAssertEqual((tuple.elements[0] as? Sol.Tuple)?.elements[0] as? Sol.UInt32, 7)
|
|
XCTAssertEqual((tuple.elements[0] as? Sol.Tuple)?.elements[1] as? Sol.Bool, .init(storage: false))
|
|
XCTAssertEqual(offset, 64)
|
|
}
|
|
|
|
func testEncodeTupleBytes() {
|
|
// (bytes) = heads tails = offset_to_tail size_of_bytes bytes_data
|
|
let value = Sol.Tuple(elements: [Sol.Bytes(storage: Data([
|
|
0xef
|
|
]))])
|
|
let expected = Data([
|
|
// head = offset to tail from the beginning = 32 bytes = 0x20
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 16 bytes
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, // 32 bytes
|
|
// size of bytes = 1
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 16 bytes
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, // 32 bytes
|
|
// bytes data
|
|
0xef, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 16 bytes
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 32 bytes
|
|
])
|
|
let data = value.encode()
|
|
XCTAssertEqual(Array(data), Array(expected))
|
|
}
|
|
|
|
func testDecodeTupleBytes() throws {
|
|
let data = Data([
|
|
// head = offset to tail from the beginning = 32 bytes = 0x20
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 16 bytes
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, // 32 bytes
|
|
// size of bytes = 1
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 16 bytes
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, // 32 bytes
|
|
// bytes data
|
|
0xef, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 16 bytes
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 32 bytes
|
|
])
|
|
var value = Sol.Tuple(elements: [Sol.Bytes()])
|
|
var offset = 0
|
|
try value.decode(from: data, offset: &offset)
|
|
continueAfterFailure = false
|
|
XCTAssertEqual(value.elements.count, 1)
|
|
XCTAssertEqual((value.elements[0] as? Sol.Bytes), Sol.Bytes(storage: Data([0xef])))
|
|
XCTAssertEqual(offset, 96)
|
|
}
|
|
|
|
func testEncodeMixedDynamicStatic() {
|
|
let value = Sol.Tuple(elements: [Sol.UInt32(7), Sol.Bytes(storage: Data([0xef])), Sol.UInt64(9)])
|
|
let expected = Data([
|
|
// head[0] = uint32(7)
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 16 bytes
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, // 32 bytes
|
|
// head[1] = offset to tail from the beginning = all heads sizes + tails up to this index = 96 = 0x60
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 16 bytes
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, // 32 bytes
|
|
// head[2] = uint64(9)
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 16 bytes
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, // 32 bytes
|
|
// tail[0] = ""
|
|
// tail[1] = size of bytes = 1
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 16 bytes
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, // 32 bytes
|
|
// tail[1] = bytes data
|
|
0xef, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 16 bytes
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 32 bytes
|
|
// tail[2] = ""
|
|
])
|
|
let data = value.encode()
|
|
XCTAssertEqual(Array(data), Array(expected))
|
|
}
|
|
|
|
func testDecodeMixedDynamicStatic() throws {
|
|
let data = Data([
|
|
// head[0] = uint32(7)
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 16 bytes
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, // 32 bytes
|
|
// head[1] = offset to tail from the beginning = all heads sizes + tails up to this index = 96 = 0x60
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 16 bytes
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, // 32 bytes
|
|
// head[2] = uint64(9)
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 16 bytes
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, // 32 bytes
|
|
// tail[0] = ""
|
|
// tail[1] = size of bytes = 1
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 16 bytes
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, // 32 bytes
|
|
// tail[1] = bytes data
|
|
0xef, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 16 bytes
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 32 bytes
|
|
// tail[2] = ""
|
|
])
|
|
var value = Sol.Tuple(elements: [Sol.UInt32(), Sol.Bytes(), Sol.UInt64()])
|
|
var offset = 0
|
|
try value.decode(from: data, offset: &offset)
|
|
continueAfterFailure = false
|
|
XCTAssertEqual(value.elements.count, 3)
|
|
XCTAssertEqual((value.elements[0] as? Sol.UInt32), 7)
|
|
XCTAssertEqual((value.elements[1] as? Sol.Bytes)?.storage.map(\.magnitude), [0xef])
|
|
XCTAssertEqual((value.elements[2] as? Sol.UInt64), 9)
|
|
XCTAssertEqual(offset, 160)
|
|
}
|
|
}
|