validate_test.gno
2.65 Kb · 73 lines
1package host_test
2
3import (
4 "errors"
5 "testing"
6
7 "gno.land/p/aib/ibc/host"
8 "gno.land/p/nt/urequire/v0"
9)
10
11// 195 characters
12var longID = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis eros neque, ultricies vel ligula ac, convallis porttitor elit. Maecenas tincidunt turpis elit, vel faucibus nisl pellentesque sodales"
13
14type testCase struct {
15 msg string
16 id string
17 expErr error
18}
19
20func TestDefaultIdentifierValidator(t *testing.T) {
21 testCases := []testCase{
22 {"valid lowercase", "lowercaseid", nil},
23 {"valid id special chars", "._+-#[]<>._+-#[]<>", nil},
24 {"valid id lower and special chars", "lower._+-#[]<>", nil},
25 {"numeric id", "1234567890", nil},
26 {"uppercase id", "NOTLOWERCASE", nil},
27 {"numeric id", "1234567890", nil},
28 {"blank id", " ", errors.New("the ID is blank - contains only spaces")},
29 {"id length out of range", "1", errors.New("the ID length is too short")},
30 {"id is too long", "this identifier is too long to be used as a valid identifier", errors.New("the ID exceeds the maximum allowed length")},
31 {"path-like id", "lower/case/id", errors.New("the ID contains path-like characters, which are invalid for an ID")},
32 {"invalid id", "(clientid)", errors.New("the ID contains invalid characters")},
33 {"empty string", "", errors.New("the ID cannot be empty")},
34 }
35
36 for _, tc := range testCases {
37 err := host.ClientIdentifierValidator(tc.id)
38 err2 := host.PortIdentifierValidator(tc.id)
39 if tc.expErr == nil {
40 urequire.NoError(t, err, tc.msg)
41 urequire.NoError(t, err2, tc.msg)
42 } else {
43 urequire.Error(t, err, tc.msg)
44 urequire.Error(t, err2, tc.msg)
45 }
46 }
47}
48
49func TestPortIdentifierValidator(t *testing.T) {
50 testCases := []testCase{
51 {"valid lowercase", "transfer", nil},
52 {"valid id special chars", "._+-#[]<>._+-#[]<>", nil},
53 {"valid id lower and special chars", "lower._+-#[]<>", nil},
54 {"numeric id", "1234567890", nil},
55 {"uppercase id", "NOTLOWERCASE", nil},
56 {"numeric id", "1234567890", nil},
57 {"blank id", " ", errors.New("the ID is blank - contains only spaces")},
58 {"id length out of range", "1", errors.New("the ID length is too short")},
59 {"id is too long", longID, errors.New("the ID exceeds the maximum allowed length")},
60 {"path-like id", "lower/case/id", errors.New("the ID contains path-like characters, which are invalid for an ID")},
61 {"invalid id", "(clientid)", errors.New("the ID contains invalid characters")},
62 {"empty string", "", errors.New("the ID cannot be empty")},
63 }
64
65 for _, tc := range testCases {
66 err := host.PortIdentifierValidator(tc.id)
67 if tc.expErr == nil {
68 urequire.NoError(t, err, tc.msg)
69 } else {
70 urequire.Error(t, err, tc.msg)
71 }
72 }
73}