-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreamrproxyclient_test.go
200 lines (191 loc) · 7.84 KB
/
streamrproxyclient_test.go
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package streamrproxyclient
import (
"fmt"
"testing"
)
const (
invalidEthereumAddress = "INVALID_ETHEREUM_ADDRESS"
goodEthereumAddress = "0x123456789012345678901234567890123456789a"
validEthereumAddress = "0x1234567890123456789012345678901234567890"
validEthereumAddress2 = "0x1234567890123456789012345678901234567892"
validEthereumAddress3 = "0x1234567890123456789012345678901234567893"
invalidStreamPartId = "INVALID_STREAM_PART_ID"
validStreamPartId = "0xa000000000000000000000000000000000000000#01"
ownEthereumAddress = "0x1234567890123456789012345678901234567890"
streamPartId = "0xd2078dc2d780029473a39ce873fc182587be69db/low-level-client#0"
invalidProxyUrl = "poiejrg039utg240"
validProxyUrl = "ws://valid.com"
nonExistentProxyUrl0 = "ws://localhost:0"
nonExistentProxyUrl1 = "ws://localhost:1"
nonExistentProxyUrl2 = "ws://localhost:2"
invalidClientHandle uint64 = 0
)
func TestLibStreamrProxyClientCreation(t *testing.T) {
lib := NewLibStreamrProxyClient()
if lib == nil {
t.Fatalf("Failed to create LibStreamrProxyClient")
}
defer lib.Close()
client, err := NewProxyClient(ownEthereumAddress, streamPartId)
if err != nil {
t.Fatalf("Failed to create ProxyClient: %v", err)
}
defer client.Close()
}
func TestInvalidEthereumAddress(t *testing.T) {
lib := NewLibStreamrProxyClient()
defer lib.Close()
client, err := NewProxyClient(invalidEthereumAddress, validStreamPartId)
if err == nil {
t.Fatalf("Expected error for invalid Ethereum address")
} else if err.Code != ERROR_INVALID_ETHEREUM_ADDRESS {
t.Fatalf("Expected error code %s, got %s", ERROR_INVALID_ETHEREUM_ADDRESS, err.Code)
}
if client != nil {
defer client.Close()
t.Fatalf("Expected nil client for invalid Ethereum address")
}
}
func TestInvalidStreamPartId(t *testing.T) {
lib := NewLibStreamrProxyClient()
defer lib.Close()
client, err := NewProxyClient(validEthereumAddress, invalidStreamPartId)
if err == nil {
t.Fatalf("Expected error for invalid stream part id")
} else if err.Code != ERROR_INVALID_STREAM_PART_ID {
t.Fatalf("Expected error code %s, got %s", ERROR_INVALID_STREAM_PART_ID, err.Code)
}
if client != nil {
defer client.Close()
t.Fatalf("Expected nil client for invalid stream part id")
}
}
func TestNoProxiesDefined(t *testing.T) {
lib := NewLibStreamrProxyClient()
defer lib.Close()
client, err := NewProxyClient(validEthereumAddress, validStreamPartId)
if err != nil {
t.Fatalf("Failed to create ProxyClient: %v", err)
}
defer client.Close()
result := client.Connect([]Proxy {})
if result.Errors == nil {
t.Fatalf("Expected error code %s, got nil", ERROR_NO_PROXIES_DEFINED)
} else if result.Errors[0].Code != ERROR_NO_PROXIES_DEFINED {
t.Fatalf("Expected error code %s, got %s", ERROR_NO_PROXIES_DEFINED, result.Errors[0].Code)
}
if len(result.Successful) != 0 {
t.Fatalf("Expected nil successful proxies for no proxies defined")
}
}
func TestInvalidProxyUrl(t *testing.T) {
lib := NewLibStreamrProxyClient()
defer lib.Close()
client, err := NewProxyClient(validEthereumAddress, validStreamPartId)
if err != nil {
t.Fatalf("Failed to create ProxyClient: %v", err)
}
defer client.Close()
proxies := []Proxy{
{WebsocketUrl: invalidProxyUrl, EthereumAddress: validEthereumAddress},
}
fmt.Println("calling connect")
result := client.Connect(proxies)
fmt.Println("Testing invalid proxy URL")
if len(result.Errors) != 1 {
t.Fatalf("Expected 1 error for invalid proxy url, got %d", len(result.Errors))
} else if result.Errors[0].Code != ERROR_INVALID_PROXY_URL {
t.Fatalf("Expected error code %s, got %s", ERROR_INVALID_PROXY_URL, result.Errors[0].Code)
}
if len(result.Successful) != 0 {
t.Fatalf("Expected nil successful proxies for invalid proxy url")
}
}
func TestInvalidProxyEthereumAddress(t *testing.T) {
lib := NewLibStreamrProxyClient()
defer lib.Close()
client, err := NewProxyClient(validEthereumAddress, validStreamPartId)
if err != nil {
t.Fatalf("Failed to create ProxyClient: %v", err)
}
defer client.Close()
proxies := []Proxy{
{WebsocketUrl: validProxyUrl, EthereumAddress: invalidEthereumAddress},
}
result := client.Connect(proxies)
if len(result.Errors) != 1 {
t.Fatalf("Expected 1 error for invalid proxy ethereum address, got %d", len(result.Errors))
} else if result.Errors[0].Code != ERROR_INVALID_ETHEREUM_ADDRESS {
t.Fatalf("Expected error code %s, got %s", ERROR_INVALID_ETHEREUM_ADDRESS, result.Errors[0].Code)
}
if len(result.Successful) != 0 {
t.Fatalf("Expected nil successful proxies for invalid proxy ethereum address")
}
}
func TestProxyConnectionFailed(t *testing.T) {
lib := NewLibStreamrProxyClient()
defer lib.Close()
client, err := NewProxyClient(validEthereumAddress, validStreamPartId)
if err != nil {
t.Fatalf("Failed to create ProxyClient: %v", err)
}
defer client.Close()
proxies := []Proxy{
{WebsocketUrl: nonExistentProxyUrl0, EthereumAddress: validEthereumAddress},
}
result := client.Connect(proxies)
if len(result.Errors) != 1 {
t.Fatalf("Expected 1 error for proxy connection failed, got %d", len(result.Errors))
} else if result.Errors[0].Code != ERROR_PROXY_CONNECTION_FAILED {
t.Fatalf("Expected error code %s, got %s", ERROR_PROXY_CONNECTION_FAILED, result.Errors[0].Code)
}
if len(result.Successful) != 0 {
t.Fatalf("Expected nil successful proxies for proxy connection failed")
}
}
func TestThreeProxyConnectionsFailed(t *testing.T) {
lib := NewLibStreamrProxyClient()
defer lib.Close()
client, err := NewProxyClient(validEthereumAddress, validStreamPartId)
if err != nil {
t.Fatalf("Failed to create ProxyClient: %v", err)
}
defer client.Close()
proxies := []Proxy{
{WebsocketUrl: nonExistentProxyUrl0, EthereumAddress: validEthereumAddress},
{WebsocketUrl: nonExistentProxyUrl1, EthereumAddress: validEthereumAddress2},
{WebsocketUrl: nonExistentProxyUrl2, EthereumAddress: validEthereumAddress3},
}
result := client.Connect(proxies)
if len(result.Errors) != 3 {
t.Fatalf("Expected 3 errors for three proxy connections failed, got %d", len(result.Errors))
} else {
for i, err := range result.Errors {
if err.Code != ERROR_PROXY_CONNECTION_FAILED {
t.Fatalf("Expected error code %s for error %d, got %s", ERROR_PROXY_CONNECTION_FAILED, i, err.Code)
}
expectedWebsocketUrl := ""
expectedEthereumAddress := ""
switch i {
case 0:
expectedWebsocketUrl = nonExistentProxyUrl0
expectedEthereumAddress = validEthereumAddress
case 1:
expectedWebsocketUrl = nonExistentProxyUrl1
expectedEthereumAddress = validEthereumAddress2
case 2:
expectedWebsocketUrl = nonExistentProxyUrl2
expectedEthereumAddress = validEthereumAddress3
}
if err.Proxy.WebsocketUrl != expectedWebsocketUrl {
t.Fatalf("Expected websocket URL %s for error %d, got %s", expectedWebsocketUrl, i, err.Proxy.WebsocketUrl)
}
if err.Proxy.EthereumAddress != expectedEthereumAddress {
t.Fatalf("Expected ethereum address %s for error %d, got %s", expectedEthereumAddress, i, err.Proxy.EthereumAddress)
}
}
}
if len(result.Successful) != 0 {
t.Fatalf("Expected nil successful proxies for three proxy connections failed")
}
}