-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapi_test.go
More file actions
386 lines (313 loc) · 8.92 KB
/
Copy pathapi_test.go
File metadata and controls
386 lines (313 loc) · 8.92 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package lightning
import (
"context"
"encoding/json"
"errors"
"os"
"strings"
"testing"
"time"
entities "github.com/bolt-observer/go_common/entities"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/stretchr/testify/assert"
)
func TestApiSelection(t *testing.T) {
dummyMac := "0201036c6e640224030a10f1c3ac8f073a46b6474e24b780a96c3f1201301a0c0a04696e666f12047265616400022974696d652d6265666f726520323032322d30382d30385430383a31303a30342e38383933303336335a00020e69706164647220312e322e332e34000006201495fe7fe048b47ff26abd66a56393869aec2dcb249594ebea44d398f58f26ec"
ignore := 4
data := entities.Data{
PubKey: "030f7b46defcec976ed516de5e7841bdcb7a19bb388b679ec9dba4bb526e93efb0",
MacaroonHex: dummyMac,
Endpoint: "bolt.observer:443",
CertVerificationType: &ignore,
}
// ApiType in NewApi() is a preference that can be overriden through data
// Invalid API type
api, err := NewAPI(APIType(500), func() (*entities.Data, error) {
return &data, nil
})
assert.Error(t, err)
if api != nil {
t.Fatalf("API should be nil")
}
// Use CLN
name, closer := ClnSocketServer(t, nil)
defer closer()
temp := data.Endpoint
data.Endpoint = name
api, err = NewAPI(ClnSocket, func() (*entities.Data, error) {
return &data, nil
})
assert.NoError(t, err)
data.Endpoint = temp
if api == nil {
t.Fatalf("API should not be nil")
}
_, ok := api.(*ClnSocketLightningAPI)
if !ok {
t.Fatalf("Should be CLN_SOCKET")
}
// Use gRPC
api, err = NewAPI(LndGrpc, func() (*entities.Data, error) {
return &data, nil
})
assert.NoError(t, err)
if api == nil {
t.Fatalf("API should not be nil")
}
_, ok = api.(*LndGrpcLightningAPI)
if !ok {
t.Fatalf("Should be LND_GRPC")
}
// Use REST
api, err = NewAPI(LndRest, func() (*entities.Data, error) {
return &data, nil
})
assert.NoError(t, err)
if api == nil {
t.Fatalf("API should not be nil")
}
_, ok = api.(*LndRestLightningAPI)
if !ok {
t.Fatalf("Should be LND_REST")
}
v := int(LndGrpc)
data.ApiType = &v
api, err = NewAPI(LndRest, func() (*entities.Data, error) {
return &data, nil
})
assert.NoError(t, err)
if api == nil {
t.Fatalf("API should not be nil")
}
_, ok = api.(*LndGrpcLightningAPI)
if !ok {
t.Fatalf("Should be LND_GRPC")
}
v = int(LndRest)
data.ApiType = &v
api, err = NewAPI(LndGrpc, func() (*entities.Data, error) {
return &data, nil
})
assert.NoError(t, err)
if api == nil {
t.Fatalf("API should not be nil")
}
_, ok = api.(*LndRestLightningAPI)
if !ok {
t.Fatalf("Should be LND_REST")
}
// Invalid type
v = 500
data.ApiType = &v
api, err = NewAPI(LndGrpc, func() (*entities.Data, error) {
return &data, nil
})
assert.NoError(t, err)
if api == nil {
t.Fatalf("API should not be nil")
}
_, ok = api.(*LndGrpcLightningAPI)
if !ok {
t.Fatalf("Should be LND_GRPC")
}
}
func TestGrpcDoesNotOpenConnection(t *testing.T) {
dummyMac := "0201036c6e640224030a10f1c3ac8f073a46b6474e24b780a96c3f1201301a0c0a04696e666f12047265616400022974696d652d6265666f726520323032322d30382d30385430383a31303a30342e38383933303336335a00020e69706164647220312e322e332e34000006201495fe7fe048b47ff26abd66a56393869aec2dcb249594ebea44d398f58f26ec"
ignore := 4
data := entities.Data{
PubKey: "030f7b46defcec976ed516de5e7841bdcb7a19bb388b679ec9dba4bb526e93efb0",
MacaroonHex: dummyMac,
Endpoint: "bolt.observer:443",
CertVerificationType: &ignore,
}
api, err := NewAPI(LndGrpc, func() (*entities.Data, error) {
return &data, nil
})
assert.NoError(t, err)
if api == nil {
t.Fatalf("API should not be nil")
}
data.Endpoint = "burek:444"
api, err = NewAPI(LndGrpc, func() (*entities.Data, error) {
return &data, nil
})
assert.NoError(t, err)
if api == nil {
t.Fatalf("API should not be nil")
}
}
func TestRestDoesNotOpenConnection(t *testing.T) {
dummyMac := "0201036c6e640224030a10f1c3ac8f073a46b6474e24b780a96c3f1201301a0c0a04696e666f12047265616400022974696d652d6265666f726520323032322d30382d30385430383a31303a30342e38383933303336335a00020e69706164647220312e322e332e34000006201495fe7fe048b47ff26abd66a56393869aec2dcb249594ebea44d398f58f26ec"
ignore := 4
data := entities.Data{
PubKey: "030f7b46defcec976ed516de5e7841bdcb7a19bb388b679ec9dba4bb526e93efb0",
MacaroonHex: dummyMac,
Endpoint: "bolt.observer:443",
CertVerificationType: &ignore,
}
api, err := NewAPI(LndRest, func() (*entities.Data, error) {
return &data, nil
})
assert.NoError(t, err)
if api == nil {
t.Fatalf("API should not be nil")
}
data.Endpoint = "burek:444"
api, err = NewAPI(LndRest, func() (*entities.Data, error) {
return &data, nil
})
assert.NoError(t, err)
if api == nil {
t.Fatalf("API should not be nil")
}
}
func TestNodeInfoFull(t *testing.T) {
mock := &MockLightningAPI{}
resp, err := getNodeInfoFullTemplate(context.Background(), mock, 100, true, true)
if err != nil {
t.Fatalf("Error getting node info: %v", err)
return
}
if resp.Node.PubKey != "fake" || resp.NumChannels != 2 || resp.TotalCapacity != 3 {
t.Fatalf("Wrong data returned")
return
}
if resp.Channels[0].Private || !resp.Channels[1].Private {
t.Fatalf("Wrong private data returned")
return
}
if strings.Contains(mock.Trace, "wrong") || strings.Contains(mock.Trace, "false") || strings.Contains(mock.Trace, "describegraph") {
t.Fatalf("Invoked wrong method")
return
}
if !strings.Contains(mock.Trace, "getchaninfo") {
t.Fatalf("GetChanInfo was not invoked")
return
}
clone := &NodeInfoAPIExtended{
NodeInfoAPI: resp.NodeInfoAPI,
Channels: make([]NodeChannelAPIExtended, 0),
}
if clone.NumChannels != 2 || clone.TotalCapacity != 3 {
t.Fatalf("Wrong data returned from clone")
}
}
func TestNodeInfoFullPublic(t *testing.T) {
mock := &MockLightningAPI{}
resp, err := getNodeInfoFullTemplate(context.Background(), mock, 100, true, false)
if err != nil {
t.Fatalf("Error getting node info: %v", err)
return
}
if resp.Node.PubKey != "fake" || resp.NumChannels != 1 || resp.TotalCapacity != 1 {
t.Fatalf("Wrong data returned")
return
}
if int(resp.NumChannels) != len(resp.Channels) {
t.Fatalf("Not all channels reported")
return
}
}
func TestNodeInfoFullWithDescribeGraph(t *testing.T) {
mock := &MockLightningAPI{}
resp, err := getNodeInfoFullTemplate(context.Background(), mock, 1, true, true)
if err != nil {
t.Fatalf("Error getting node info: %v", err)
return
}
if resp.Node.PubKey != "fake" || resp.NumChannels != 2 || resp.TotalCapacity != 3 {
t.Fatalf("Wrong data returned")
return
}
if strings.Contains(mock.Trace, "wrong") || strings.Contains(mock.Trace, "false") {
t.Fatalf("Invoked wrong method")
return
}
if !strings.Contains(mock.Trace, "describegraph") {
t.Fatalf("DescribeGraph was not invoked")
return
}
if strings.Contains(mock.Trace, "getchaninfo") {
t.Fatalf("GetChanInfo was invoked")
return
}
if resp.Node.PubKey != "fake" || resp.NumChannels != 2 || resp.TotalCapacity != 3 {
t.Fatalf("Wrong data returned")
return
}
if resp.Channels[0].Private || !resp.Channels[1].Private {
t.Fatalf("Wrong private data returned")
return
}
clone := &NodeInfoAPIExtended{
NodeInfoAPI: resp.NodeInfoAPI,
Channels: make([]NodeChannelAPIExtended, 0),
}
if clone.NumChannels != 2 || clone.TotalCapacity != 3 {
t.Fatalf("Wrong data returned from clone")
}
}
func TestRawMessageSerialization(t *testing.T) {
var (
err error
data entities.Data
)
const FixtureSecret = "fixture-grpc.secret"
if _, err := os.Stat(FixtureSecret); errors.Is(err, os.ErrNotExist) {
// If file with credentials does not exist succeed
return
}
content, err := os.ReadFile(FixtureSecret)
if err != nil {
t.Fatalf("Error when opening file: %v", err)
return
}
err = json.Unmarshal(content, &data)
if err != nil {
t.Fatalf("Error during Unmarshal(): %v", err)
return
}
client, _, cleanup, err := GetClient(func() (*entities.Data, error) {
return &data, nil
})
if err != nil {
t.Fatalf("GetClient failed: %v\n", err)
}
defer cleanup()
resp, err := client.ListPayments(context.Background(), &lnrpc.ListPaymentsRequest{MaxPayments: 10, IncludeIncomplete: true})
if err != nil {
t.Fatalf("ListPayments failed: %v\n", err)
}
for _, one := range resp.Payments {
raw := RawMessage{}
raw.Timestamp = time.Unix(0, one.CreationTimeNs)
raw.Implementation = "lnd"
raw.Message, err = json.Marshal(one)
if err != nil {
t.Fatalf("Message marshal error: %v\n", err)
}
_, err := json.Marshal(raw)
if err != nil {
t.Fatalf("Wrapped message marshal error: %v\n", err)
}
}
//t.Fail()
}
func TestCtxError(t *testing.T) {
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(8*time.Second))
done := false
go func() {
for i := 0; i < 100; i++ {
if ctx.Err() != nil {
//fmt.Printf("Error: %v\n", ctx.Err())
break
}
time.Sleep(100 * time.Millisecond)
}
done = true
}()
cancel()
time.Sleep(200 * time.Millisecond)
assert.Equal(t, true, done)
//t.Fail()
}