-
Notifications
You must be signed in to change notification settings - Fork 17
/
codec_test.go
500 lines (483 loc) · 21.4 KB
/
codec_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
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
package lnurl
import (
"fmt"
"strings"
"testing"
)
// Starting tests with validation
// TestLNURLDecodeStrict will test if LNURLDecodeStrict returns the expected string
func TestLNURLDecodeStrict(t *testing.T) {
type args struct {
code string
}
tests := []struct {
name string
desc string
args args
want string
wantErr bool
}{
{desc: "LUD17_CHANGE_SCHEMA",
args: args{code: "lnurlp://lnurl.fiatjaf.com"}, // change scheme
want: "https://lnurl.fiatjaf.com"},
{desc: "ONION_SCHEMA_CHANGE_SCHEMA_ERROR",
args: args{code: "onion://lnurl.fiatjaf.onion"}, // change scheme
want: "http://lnurl.fiatjaf.onion", wantErr: true},
{desc: "LUD17_BECH32_ENCODED_CHANGE_SCHEMA_ERROR",
args: args{code: "lnurl1d3h82unvwqaz7tmpwp5juenfv96x5ctx9e3k7mf0wccj7mrww4exctmsv9usxr8j98"}, // lnurlp://api.fiatjaf.com/v1/lnurl/pay
want: "https://api.fiatjaf.com/v1/lnurl/pay", wantErr: true},
{desc: "LUD17_BECH32_ENCODED_CHANGE_SCHEMA_IP_ERROR",
args: args{code: "lnurl1dp68gup69uhnzt339ccjuvf0wccj7mrww4exctmsv9usux0rql"}, // lnurlp://api.fiatjaf.com/v1/lnurl/pay
want: "https://1.1.1.1/v1/lnurl/pay", wantErr: true},
{desc: "LUD1_ENCODED_CHANGE_SCHEMA",
args: args{code: "lnurl1dp68gurn8ghj7vfwxyhrzt339amrztmvde6hymp0wpshjr50pch"}, // https://1.1.1.1/v1/lnurl/pay
want: "https://1.1.1.1/v1/lnurl/pay"},
{desc: "LUD1_PUNY_SAME_SCHEMA",
args: args{code: "lnurl1dp68gurn8ghj77rw95khx7r3wc6kwv3nv3uhyvmpxserscfwvdhk6tmkxyhkcmn4wfkz7urp0y6jmn9j"}, // https://xn--sxqv5g23dyr3a428a.com/v1/lnurl/pay
want: "https://xn--sxqv5g23dyr3a428a.com/v1/lnurl/pay"}, // check puny code
{desc: "LUD1_IDN_SAME_SCHEMA",
args: args{code: "lnurl1dp68gurn8ghjle443052l909sxr7t8ulukgg6tnrdakj7a339akxuatjdshhqctea4v3jm"},
want: "https://测试假域名.com/v1/lnurl/pay"}, // check puny code
{desc: "LUD17_BECH32_IDN_CHANGE_SCHEMA_ERROR",
args: args{code: "lnurl1d3h82unvwuaz7tlxkk973tu4ukqc0evlnljeprfwvdhk6tmkxyhkcmn4wfkz7urp0y93ltxj"},
want: "https://测试假域名.com/v1/lnurl/pay", wantErr: true}, // check puny code
{desc: "LUD1_BECH32_ONION_SAME_SCHEMA",
args: args{code: "lnurl1dp68gup69uh7ddvtazhetevpsljel8l9jzxjummwd9hkutmkxyhkcmn4wfkz7urp0ygc52rr"}, // http://测试假域名.onion/v1/lnurl/pay
want: "http://测试假域名.onion/v1/lnurl/pay"}, // check puny onion (not sure know if this is possible)
{desc: "LUD1_BECH32_ONION_CHANGE_SCHEMA_ERROR",
args: args{code: "lnurl1dp68gurn8ghj7er0d4skjm3wdahxjmmw9amrztmvde6hymp0wpshjcw5kvw"},
want: "http://domain.onion/v1/lnurl/pay", wantErr: true}, // check puny onion (not sure know if this is possible)
{desc: "RANDOM_STRING_ERROR",
args: args{code: "lnurl1d3h82unvjhypn2"},
want: "lnurl", wantErr: true}, // invalid domain name. returns error and decoded input
{desc: "RANDOM_STRING_UPPERCASE_ERROR",
args: args{code: strings.ToUpper("lnurl1d3h82unvjhypn2")},
want: "lnurl", wantErr: true},
{desc: "HTTPS",
args: args{code: "https://lnurl.fiatjaf.com"}, // do noting
want: "https://lnurl.fiatjaf.com"},
{desc: "ONION_SCHEMA_CHANGE_SCHEMA_ERROR",
args: args{code: "lnurl1dahxjmmw8ghj7mrww4exctnxd9shg6npvchx7mnfdahquxueex"}, // change scheme
want: "http://lnurl.fiatjaf.onion", wantErr: true},
{desc: "ONION_HTTPS_CHANGE_SCHEMA_ERROR",
args: args{code: "lnurl1dp68gurn8ghj7mrww4exctnxd9shg6npvchx7mnfdahq874q6e"}, // change scheme
want: "http://lnurl.fiatjaf.onion", wantErr: true},
}
for _, tt := range tests {
tt.name = tt.args.code
t.Run(fmt.Sprintf("%s", tt.desc), func(t *testing.T) {
got, err := LNURLDecodeStrict(tt.args.code)
if (err != nil) != tt.wantErr {
t.Errorf("LNURLDecodeStrict() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("LNURLDecodeStrict() got = %v, want %v", got, tt.want)
}
})
}
}
// TestLNURLEncodeStrict will test if LNURLEncodeStrict returns the expected string
func TestLNURLEncodeStrict(t *testing.T) {
type args struct {
actualurl string
}
tests := []struct {
name string
desc string
args args
want string
wantErr bool
}{{desc: "INVALID_IP_ERROR",
args: args{actualurl: "onion://10.2.0.10:8888"},
want: strings.ToUpper("LNURL1DP68GURN8GHJ7VFS9CEZUVPWXYCR5WPC8QUQAPPZPR"), wantErr: false},
{desc: "INVALID_IP_ERROR",
args: args{actualurl: "onion://111.2.999.2:88"},
want: strings.ToUpper("lnurl1dahxjmmw8ghj7vf3xyhryt3e8yujuv368quq39nwhr"), wantErr: true},
{desc: "ONION_SCHEME_ERROR",
args: args{actualurl: "onion://lnurl.com"},
want: strings.ToUpper("LNURL1DP68GURN8GHJ7MRWW4EXCTNRDAKS6NPC70"), wantErr: true},
{desc: "INVALID_DOMAIN_NAME_2_ERROR",
args: args{actualurl: "lnur%%%l"},
want: strings.ToUpper("lnurl1d3h82u39y5jkc45g4zl"), wantErr: true},
{desc: "IDN_CHANGE_SCHEMA_ERROR",
args: args{actualurl: "http://看.com"},
want: strings.ToUpper("lnurl1dp68gurn8ghjleuu3vhxxmmdrmldr8"), wantErr: true}, // https://看.com
{desc: "NO_SCHEMA_INVALID_TLD_ERROR",
args: args{actualurl: "lnurl.com.btc"}, //invalid tld
want: strings.ToUpper("LNURL1D3H82UNV9E3K7MFWVF6XXSVAUH5"), wantErr: true}, // lnurl.com.btc
{desc: "INVALID_DOMAIN_NAME_ERROR",
args: args{actualurl: "lnurl"},
want: strings.ToUpper("lnurl1d3h82unvjhypn2"), wantErr: true},
{desc: "INVALID_TLD_ERROR",
args: args{actualurl: "lnurl.msfmsdkfns&dfandfasdf"}, // invalid tld
want: strings.ToUpper("LNURL1D3H82UNV9EKHXENDWDJXKENWWVNXGENPDEJXVCTNV3NQXM32ED"), wantErr: true}, // lnurl.msfmsdkfns&dfandfasdf
{desc: "ADD_SCHEMA_COM_ORG",
args: args{actualurl: "lnurl.com.org"},
want: strings.ToUpper("LNURL1DP68GURN8GHJ7MRWW4EXCTNRDAKJUMMJVUK5QJ46"), wantErr: true}, // https://lnurl.com.org,
{desc: "ADD_SCHEMA_COM",
args: args{actualurl: "lnurl.com"},
want: "LNURL1DP68GURN8GHJ7MRWW4EXCTNRDAKS6NPC70", wantErr: true}, // https://lnurl.com
{desc: "COM_ORG_ADD_SCHEMA",
args: args{actualurl: "lnurl.com.org"},
want: strings.ToUpper("LNURL1DP68GURN8GHJ7MRWW4EXCTNRDAKJUMMJVUK5QJ46"), wantErr: true}, // https://lnurl.com.org,
{desc: "LUD17_P_ONION",
args: args{actualurl: "lnurlp://api.fiatjaf.onion/v2/lnurl/pay"},
want: "lnurlp://api.fiatjaf.onion/v2/lnurl/pay"}, // http://api.fiatjaf.onion/v2/lnurl/pay
{desc: "LUD17_A_ONION",
args: args{actualurl: "lnurla://api.fiatjaf.onion/v2/lnurl/pay"},
want: "lnurla://api.fiatjaf.onion/v2/lnurl/pay"}, // http://api.fiatjaf.onion/v2/lnurl/pay
{desc: "LUD17_W_ONION",
args: args{actualurl: "lnurlw://api.fiatjaf.onion/v2/lnurl/pay"},
want: "lnurlw://api.fiatjaf.onion/v2/lnurl/pay"}, // https://api.fiatjaf.com/v2/lnurl/pay
{desc: "LUD17_IP_AND_PORT",
args: args{actualurl: "lnurlp://1.1.1.1:8080/v1/lnurl/pay"},
want: "lnurlp://1.1.1.1:8080/v1/lnurl/pay"}, // https://1.1.1.1:8080/v1/lnurl/pay
{desc: "LUD17_IP",
args: args{actualurl: "lnurlp://1.1.1.1/v1/lnurl/pay"},
want: "lnurlp://1.1.1.1/v1/lnurl/pay"}, // https://1.1.1.1/v1/lnurl/pay
{desc: "LUD1_ONION",
args: args{actualurl: "http://juhaavcaxhxlp77nkq76byazcldy2hlmovfu2epvl5ankdibsot4csyd.onion/"},
want: strings.ToUpper("lnurl1dp68gup69uhk5atgv9shvcmp0p58smrsxumku6m3xumxy7tp0f3kcerexf5xcmt0wen82vn9wpmxcdtpde4kg6tzwdhhgdrrwdukgtn0de5k7m30p4k848"), wantErr: false},
{desc: "HTTPS",
args: args{actualurl: "https://api.fiatjaf.com/v2/lnurl/pay"},
want: strings.ToUpper("lnurl1dp68gurn8ghj7ctsdyhxv6tpw34xze3wvdhk6tmkxghkcmn4wfkz7urp0y0q3peg")}, // https://api.fiatjaf.com/v2/lnurl/pay
}
for _, tt := range tests {
tt.name = tt.args.actualurl
t.Run(fmt.Sprintf("%s", tt.desc), func(t *testing.T) {
got, err := LNURLEncodeStrict(tt.args.actualurl)
if (err != nil) != tt.wantErr {
t.Errorf("LNURLDecodeStrict() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("LNURLDecodeStrict() got = %v, want %v", got, tt.want)
}
})
}
}
// Start test without validation
// TestLNURLDecodeStrict will test if LNURLDecodeStrict returns the expected string
func TestLNURLEncode(t *testing.T) {
type args struct {
actualurl string
}
tests := []struct {
name string
desc string
args args
want string
wantErr bool
}{{desc: "INVALID_IP_NO_ERROR",
args: args{actualurl: "onion://111.2.999.2:88"},
want: strings.ToUpper("lnurl1dahxjmmw8ghj7vf3xyhryt3e8yujuv368quq39nwhr"), wantErr: false},
{desc: "ONION_SCHEME_NO_CHANGE",
args: args{actualurl: "onion://lnurl.com"},
want: strings.ToUpper("LNURL1DAHXJMMW8GHJ7MRWW4EXCTNRDAKSU44R9H"), wantErr: false},
{desc: "INVALID_DOMAIN_NAME_2_ERROR",
args: args{actualurl: "lnur%%%l"},
want: strings.ToUpper("lnurl1d3h82u39y5jkc45g4zl"), wantErr: false},
{desc: "IDN_NO_CHANGE_SCHEMA_ERROR",
args: args{actualurl: "http://看.com"},
want: strings.ToUpper("LNURL1DP68GUP69UH708YT9E3K7MGL572TH"), wantErr: false}, // http://看.com
{desc: "NO_SCHEMA_INVALID_TLD_NO_ERROR",
args: args{actualurl: "lnurl.com.btc"}, //invalid tld
want: strings.ToUpper("LNURL1D3H82UNV9E3K7MFWVF6XXSVAUH5"), wantErr: false}, // lnurl.com.btc
{desc: "NO_SCHEMA_INVALID_TLD_NO_ERROR_2",
args: args{actualurl: "https://lnurl.com.btc"}, //invalid tld
want: strings.ToUpper("LNURL1DP68GURN8GHJ7MRWW4EXCTNRDAKJUCN5VVXPFXPM"), wantErr: false}, // lnurl.com.btc
{desc: "INVALID_DOMAIN_NAME_ERROR",
args: args{actualurl: "lnurl"},
want: strings.ToUpper("lnurl1d3h82unvjhypn2"), wantErr: false},
{desc: "INVALID_TLD_ERROR",
args: args{actualurl: "lnurl.msfmsdkfns&dfandfasdf"}, // invalid tld
want: strings.ToUpper("LNURL1D3H82UNV9EKHXENDWDJXKENWWVNXGENPDEJXVCTNV3NQXM32ED"), wantErr: false}, // lnurl.msfmsdkfns&dfandfasdf
{desc: "NO_SCHEMA_ERROR",
args: args{actualurl: "lnurl.com.org"},
want: strings.ToUpper("LNURL1D3H82UNV9E3K7MFWDAEXWSE0TLM"), wantErr: false}, // https://lnurl.com.org,
{desc: "NO_SCHEMA_TLD_COM",
args: args{actualurl: "lnurl.com"},
want: "LNURL1D3H82UNV9E3K7MG347503", wantErr: false}, // https://lnurl.com
{desc: "COM_ORG_NO_SCHEMA",
args: args{actualurl: "lnurl.com.org"},
want: strings.ToUpper("LNURL1D3H82UNV9E3K7MFWDAEXWSE0TLM"), wantErr: false}, // https://lnurl.com.org,
{desc: "LUD17_P_ONION_BECH",
args: args{actualurl: "lnurlp://api.fiatjaf.onion/v2/lnurl/pay"},
want: "LNURL1D3H82UNVWQAZ7TMPWP5JUENFV96X5CTX9EHKU6T0DCHHVV30D3H82UNV9ACXZ7GLKMARH"}, // http://api.fiatjaf.onion/v2/lnurl/pay
{desc: "LUD17_A_ONION_BECH",
args: args{actualurl: "lnurla://api.fiatjaf.onion/v2/lnurl/pay"},
want: "LNURL1D3H82UNVVYAZ7TMPWP5JUENFV96X5CTX9EHKU6T0DCHHVV30D3H82UNV9ACXZ7GEDZQYL"}, // http://api.fiatjaf.onion/v2/lnurl/pay
{desc: "LUD17_W_ONION_BECH",
args: args{actualurl: "lnurlw://api.fiatjaf.onion/v2/lnurl/pay"},
want: "LNURL1D3H82UNVWUAZ7TMPWP5JUENFV96X5CTX9EHKU6T0DCHHVV30D3H82UNV9ACXZ7GRWJJDV"}, // lnurlp://api.fiatjaf.onion/v2/lnurl/pay
{desc: "LUD17_IP_AND_PORT_BECH",
args: args{actualurl: "lnurlp://1.1.1.1:8080/v1/lnurl/pay"},
want: "LNURL1D3H82UNVWQAZ7TE39CCJUVFWXYARSVPCXQHHVVF0D3H82UNV9ACXZ7G5A0M2Q"}, // https://1.1.1.1:8080/v1/lnurl/pay
{desc: "LUD17_IP_BECH",
args: args{actualurl: "lnurlp://1.1.1.1/v1/lnurl/pay"},
want: "LNURL1D3H82UNVWQAZ7TE39CCJUVFWXYHHVVF0D3H82UNV9ACXZ7GTCNKJU"}, // https://1.1.1.1/v1/lnurl/pay
{desc: "LUD1_ONION",
args: args{actualurl: "http://juhaavcaxhxlp77nkq76byazcldy2hlmovfu2epvl5ankdibsot4csyd.onion/"},
want: strings.ToUpper("lnurl1dp68gup69uhk5atgv9shvcmp0p58smrsxumku6m3xumxy7tp0f3kcerexf5xcmt0wen82vn9wpmxcdtpde4kg6tzwdhhgdrrwdukgtn0de5k7m30p4k848"), wantErr: false},
{desc: "HTTPS",
args: args{actualurl: "https://api.fiatjaf.com/v2/lnurl/pay"},
want: strings.ToUpper("lnurl1dp68gurn8ghj7ctsdyhxv6tpw34xze3wvdhk6tmkxghkcmn4wfkz7urp0y0q3peg")}}
for _, tt := range tests {
tt.name = tt.args.actualurl
t.Run(fmt.Sprintf("%s", tt.desc), func(t *testing.T) {
got, err := LNURLEncode(tt.args.actualurl)
if (err != nil) != tt.wantErr {
t.Errorf("LNURLDecodeStrict() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("LNURLDecodeStrict() got = %v, want %v", got, tt.want)
}
})
}
}
// TestLNURLDecodeStrict will test if LNURLDecodeStrict returns the expected string
func TestLNURLDecode(t *testing.T) {
type args struct {
code string
}
tests := []struct {
name string
desc string
args args
want string
wantErr bool
}{{desc: "INVALID_IP_NO_ERROR",
args: args{code: "lnurl1dahxjmmw8ghj7vf3xyhryt3e8yujuv368quq39nwhr"},
want: "onion://111.2.999.2:88", wantErr: false},
{desc: "LUD17_CHANGE_SCHEMA",
args: args{code: "lnurlp://lnurl.fiatjaf.com"}, // change scheme
want: "https://lnurl.fiatjaf.com"},
{desc: "ONION_SCHEMA_CHANGE_SCHEMA_ERROR",
args: args{code: "onion://lnurl.fiatjaf.onion"}, // change scheme
want: "", wantErr: true},
{desc: "LUD17_BECH32_ENCODED_CHANGE_SCHEMA_ERROR",
args: args{code: "lnurl1d3h82unvwqaz7tmpwp5juenfv96x5ctx9e3k7mf0wccj7mrww4exctmsv9usxr8j98"}, // lnurlp://api.fiatjaf.com/v1/lnurl/pay
want: "lnurlp://api.fiatjaf.com/v1/lnurl/pay", wantErr: false},
{desc: "LUD17_BECH32_ENCODED_CHANGE_SCHEMA_IP_ERROR",
args: args{code: "lnurl1dp68gup69uhnzt339ccjuvf0wccj7mrww4exctmsv9usux0rql"}, // lnurlp://api.fiatjaf.com/v1/lnurl/pay
want: "http://1.1.1.1/v1/lnurl/pay", wantErr: false},
{desc: "LUD1_ENCODED_CHANGE_SCHEMA",
args: args{code: "lnurl1dp68gurn8ghj7vfwxyhrzt339amrztmvde6hymp0wpshjr50pch"}, // https://1.1.1.1/v1/lnurl/pay
want: "https://1.1.1.1/v1/lnurl/pay"},
{desc: "LUD1_PUNY_SAME_SCHEMA",
args: args{code: "lnurl1dp68gurn8ghj77rw95khx7r3wc6kwv3nv3uhyvmpxserscfwvdhk6tmkxyhkcmn4wfkz7urp0y6jmn9j"}, // https://xn--sxqv5g23dyr3a428a.com/v1/lnurl/pay
want: "https://xn--sxqv5g23dyr3a428a.com/v1/lnurl/pay"}, // check puny code
{desc: "LUD1_IDN_SAME_SCHEMA",
args: args{code: "lnurl1dp68gurn8ghjle443052l909sxr7t8ulukgg6tnrdakj7a339akxuatjdshhqctea4v3jm"},
want: "https://测试假域名.com/v1/lnurl/pay"}, // check puny code
{desc: "LUD17_BECH32_IDN_CHANGE_SCHEMA_ERROR",
args: args{code: "lnurl1d3h82unvwuaz7tlxkk973tu4ukqc0evlnljeprfwvdhk6tmkxyhkcmn4wfkz7urp0y93ltxj"},
want: "lnurlw://测试假域名.com/v1/lnurl/pay", wantErr: false}, // check puny code
{desc: "LUD1_BECH32_ONION_SAME_SCHEMA",
args: args{code: "lnurl1dp68gup69uh7ddvtazhetevpsljel8l9jzxjummwd9hkutmkxyhkcmn4wfkz7urp0ygc52rr"}, // http://测试假域名.onion/v1/lnurl/pay
want: "http://测试假域名.onion/v1/lnurl/pay"}, // check puny onion (not sure know if this is possible)
{desc: "LUD1_BECH32_ONION_CHANGE_SCHEMA_ERROR",
args: args{code: "lnurl1dp68gurn8ghj7er0d4skjm3wdahxjmmw9amrztmvde6hymp0wpshjcw5kvw"},
want: "https://domain.onion/v1/lnurl/pay", wantErr: false}, // check puny onion (not sure know if this is possible)
{desc: "RANDOM_STRING_ERROR",
args: args{code: "lnurl1d3h82unvjhypn2"},
want: "lnurl", wantErr: false}, // invalid domain name. returns error and decoded input
{desc: "RANDOM_STRING_UPPERCASE_ERROR",
args: args{code: strings.ToUpper("lnurl1d3h82unvjhypn2")},
want: "lnurl", wantErr: false},
{desc: "HTTPS",
args: args{code: "https://lnurl.fiatjaf.com"}, // do noting
want: "https://lnurl.fiatjaf.com"},
{desc: "ONION_SCHEMA_CHANGE_SCHEMA_ERROR",
args: args{code: "lnurl1dahxjmmw8ghj7mrww4exctnxd9shg6npvchx7mnfdahquxueex"}, // change scheme
want: "onion://lnurl.fiatjaf.onion", wantErr: false},
{desc: "ONION_HTTPS_CHANGE_SCHEMA_ERROR",
args: args{code: "lnurl1dp68gurn8ghj7mrww4exctnxd9shg6npvchx7mnfdahq874q6e"}, // change scheme
want: "https://lnurl.fiatjaf.onion", wantErr: false},
}
for _, tt := range tests {
tt.name = tt.args.code
t.Run(fmt.Sprintf("%s", tt.desc), func(t *testing.T) {
got, err := LNURLDecode(tt.args.code)
if (err != nil) != tt.wantErr {
t.Errorf("LNURLDecode() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("LNURLDecode() got = %v, want %v", got, tt.want)
}
})
}
}
// TestLNURLEncodeDecode will test if encoding and decoding multiple times returns the expected string
// this test will fail without validation
func TestLNURLEncodeDecode(t *testing.T) {
type args struct {
input string
}
tests := []struct {
desc string
name string
args args
wantErr bool
want string
}{
{desc: "no_scheme_onion",
args: args{input: "hellosir.onion"}, wantErr: true, want: "http://hellosir.onion"}, // will be encoded to http://
{desc: "https",
args: args{input: "https://api.fiatjaf.com/v1/lnurl/pay"}, want: "https://api.fiatjaf.com/v1/lnurl/pay"},
{desc: "https_scheme",
args: args{input: "https://lnurl.fiatjaf.com"}, want: "https://lnurl.fiatjaf.com"},
{desc: "lnurlp",
args: args{input: "lnurlp://lnurl.fiatjaf.com"}, want: "https://lnurl.fiatjaf.com"}, // will be encoded to https://
{desc: "no_scheme_com",
args: args{input: "lnurl.fiatjaf.com"}, want: "https://lnurl.fiatjaf.com", wantErr: true},
{desc: "no_domain_string",
args: args{input: "lnurl"}, want: "lnurl", wantErr: true},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("%s", tt.desc), func(t *testing.T) {
// encode string first
enc, err := LNURLEncodeStrict(tt.args.input)
if err != nil {
if !tt.wantErr {
t.Errorf("LNURLDecodeStrict() error = %v, wantErr %v", err, tt.wantErr)
}
return
}
// decode string
dec, err := LNURLDecodeStrict(enc)
if err != nil {
if !tt.wantErr {
t.Errorf("LNURLDecodeStrict() error = %v, wantErr %v", err, tt.wantErr)
}
return
}
// decode string
dec, err = LNURLDecodeStrict(enc)
if err != nil {
if !tt.wantErr {
t.Errorf("LNURLDecodeStrict() error = %v, wantErr %v", err, tt.wantErr)
}
return
}
enc, err = LNURLEncodeStrict(tt.args.input)
if err != nil {
if !tt.wantErr {
t.Errorf("LNURLDecodeStrict() error = %v, wantErr %v", err, tt.wantErr)
}
return
}
// decode string
dec, err = LNURLDecodeStrict(enc)
if err != nil {
if !tt.wantErr {
t.Errorf("LNURLDecodeStrict() error = %v, wantErr %v", err, tt.wantErr)
}
return
}
// check if decoded string == actualurl
if dec != tt.want {
t.Errorf("LNURLDecodeStrict() enc = %v, dec %v", enc, dec)
}
})
}
}
// TestLNURLEncodeDecode will test if encoding strict and decoding default returns the expected string
func TestLNURLEncodeStrictDecode(t *testing.T) {
type args struct {
input string
}
tests := []struct {
desc string
name string
args args
wantErr bool
want string
}{
{desc: "ONION_ADD_SCHEME",
args: args{input: "hellosir.onion"}, wantErr: true, want: "http://hellosir.onion"}, // will be encoded to http://
{desc: "HTTPS",
args: args{input: "https://api.fiatjaf.com/v1/lnurl/pay"}, want: "https://api.fiatjaf.com/v1/lnurl/pay"},
{desc: "ONION_SCHEME",
args: args{input: "onion://lnurl.fiatjaf.com"}, want: "https://lnurl.fiatjaf.com", wantErr: true},
{desc: "lnurlp",
args: args{input: "lnurlp://lnurl.fiatjaf.com"}, want: "https://lnurl.fiatjaf.com", wantErr: false}, // will be encoded to https://
{desc: "NO_SCHEME",
args: args{input: "lnurl.fiatjaf.com"}, want: "https://lnurl.fiatjaf.com", wantErr: true},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("%s", tt.desc), func(t *testing.T) {
// encode string first
enc, err := LNURLEncodeStrict(tt.args.input)
if err != nil {
if !tt.wantErr {
t.Errorf("LNURLDecodeStrict() error = %v, wantErr %v", err, tt.wantErr)
return
}
}
// decode string
dec, err := LNURLDecode(enc)
if err != nil {
if !tt.wantErr {
t.Errorf("LNURLDecodeStrict() error = %v, wantErr %v", err, tt.wantErr)
return
}
}
// check if decoded string == actualurl
if dec != tt.want {
t.Errorf("LNURLDecodeStrict() enc = %v, dec %v", enc, dec)
}
})
}
}
// TestLNURLEncodeDecodeStrict will test if encoding default and decoding strict returns the expected string
func TestLNURLEncodeDecodeStrict(t *testing.T) {
type args struct {
input string
}
tests := []struct {
desc string
name string
args args
wantErr bool
want string
}{
{desc: "ONION_ADD_SCHEME",
args: args{input: "hellosir.onion"}, wantErr: true, want: "http://hellosir.onion"}, // will be encoded to http://
{desc: "HTTPS",
args: args{input: "https://api.fiatjaf.com/v1/lnurl/pay"}, want: "https://api.fiatjaf.com/v1/lnurl/pay"},
{desc: "ONION_SCHEME",
args: args{input: "onion://lnurl.fiatjaf.com"}, want: "https://lnurl.fiatjaf.com", wantErr: true},
{desc: "lnurlp",
args: args{input: "lnurlp://lnurl.fiatjaf.com"}, want: "https://lnurl.fiatjaf.com", wantErr: true}, // will be encoded to https://
{desc: "NO_SCHEME",
args: args{input: "lnurl.fiatjaf.com"}, want: "https://lnurl.fiatjaf.com", wantErr: true},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("%s", tt.desc), func(t *testing.T) {
// encode string first
enc, err := LNURLEncode(tt.args.input)
if err != nil {
if !tt.wantErr {
t.Errorf("LNURLDecodeStrict() error = %v, wantErr %v", err, tt.wantErr)
return
}
}
// decode string
dec, err := LNURLDecodeStrict(enc)
if err != nil {
if !tt.wantErr {
t.Errorf("LNURLDecodeStrict() error = %v, wantErr %v", err, tt.wantErr)
return
}
}
// check if decoded string == actualurl
if dec != tt.want {
t.Errorf("LNURLDecodeStrict() enc = %v, dec %v", enc, dec)
}
})
}
}