-
Notifications
You must be signed in to change notification settings - Fork 20
/
PeerAddressTests.swift
81 lines (71 loc) · 3.08 KB
/
PeerAddressTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//
// PeerAddressTests.swift
// BitcoinSwift
//
// Created by Kevin Greene on 12/6/14.
// Copyright (c) 2014 DoubleSha. All rights reserved.
//
import BitcoinSwift
import XCTest
class PeerAddressTests: XCTestCase {
let peerAddressBytesWithTimestamp: [UInt8] = [
0xe2, 0x15, 0x10, 0x4d, // timestamp
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // services
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // IP
0x00, 0x00, 0xff, 0xff, 0x01, 0x02, 0x03, 0x04, // IP
0x20, 0x8d] // port
let peerAddressBytesWithoutTimestamp: [UInt8] = [
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // services
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // IP
0x00, 0x00, 0xff, 0xff, 0x01, 0x02, 0x03, 0x04, // IP
0x20, 0x8d] // port
var peerAddressDataWithTimestamp: NSData!
var peerAddressDataWithoutTimestamp: NSData!
var peerAddressWithTimestamp: PeerAddress!
var peerAddressWithoutTimestamp: PeerAddress!
override func setUp() {
super.setUp()
peerAddressDataWithTimestamp = NSData(bytes: peerAddressBytesWithTimestamp,
length: peerAddressBytesWithTimestamp.count)
peerAddressDataWithoutTimestamp = NSData(bytes: peerAddressBytesWithoutTimestamp,
length: peerAddressBytesWithoutTimestamp.count)
let timestamp = NSDate(timeIntervalSince1970: NSTimeInterval(0x4d1015e2))
let services = PeerServices.NodeNetwork
let IP = IPAddress.IPV4(0x01020304)
let port: UInt16 = 8333
peerAddressWithTimestamp = PeerAddress(services: services,
IP: IP,
port: port,
timestamp: timestamp)
peerAddressWithoutTimestamp = PeerAddress(services: services, IP: IP, port: port)
}
func testPeerAddressEncodingWithTimestamp() {
XCTAssertEqual(peerAddressWithTimestamp.bitcoinData, peerAddressDataWithTimestamp)
}
func testPeerAddressEncodingWithoutTimestamp() {
XCTAssertEqual(peerAddressWithTimestamp.bitcoinDataWithTimestamp(false),
peerAddressDataWithoutTimestamp)
}
func testPeerAddressDecodingWithTimestamp() {
let stream = NSInputStream(data: peerAddressDataWithTimestamp)
stream.open()
if let testPeerAddress = PeerAddress.fromBitcoinStream(stream) {
XCTAssertEqual(testPeerAddress, peerAddressWithTimestamp)
} else {
XCTFail("Failed to parse PeerAddress")
}
XCTAssertFalse(stream.hasBytesAvailable)
stream.close()
}
func testPeerAddressDecodingWithoutTimestamp() {
let stream = NSInputStream(data: peerAddressDataWithoutTimestamp)
stream.open()
if let testPeerAddress = PeerAddress.fromBitcoinStream(stream, includeTimestamp: false) {
XCTAssertEqual(testPeerAddress, peerAddressWithoutTimestamp)
} else {
XCTFail("Failed to parse PeerAddress")
}
XCTAssertFalse(stream.hasBytesAvailable)
stream.close()
}
}