-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathparse_test.go
140 lines (132 loc) · 4.78 KB
/
parse_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
package dsref
import (
"testing"
)
func TestParseFull(t *testing.T) {
goodCases := []struct {
description string
text string
expect Ref
}{
{"human friendly", "abc/my_dataset", Ref{Username: "abc", Name: "my_dataset"}},
{"full reference", "abc/my_dataset@base32identifier/ipfs/QmSecond", Ref{Username: "abc", Name: "my_dataset", InitID: "base32identifier", Path: "/ipfs/QmSecond"}},
{"right hand side", "@base32identifier/ipfs/QmSecond", Ref{InitID: "base32identifier", Path: "/ipfs/QmSecond"}},
{"just path", "@/ipfs/QmSecond", Ref{Path: "/ipfs/QmSecond"}},
{"long name", "peer/some_name@/mem/QmXATayrFgsS3tpCi2ykfpNJ8uiCWT74dttnvJvVo1J7Rn", Ref{Username: "peer", Name: "some_name", Path: "/mem/QmXATayrFgsS3tpCi2ykfpNJ8uiCWT74dttnvJvVo1J7Rn"}},
{"name-has-dash", "abc/my-dataset", Ref{Username: "abc", Name: "my-dataset"}},
{"dash-in-username", "some-user/my_dataset", Ref{Username: "some-user", Name: "my_dataset"}},
{"legacy profileID", "@QmFirst/ipfs/QmSecond", Ref{ProfileID: "QmFirst", Path: "/ipfs/QmSecond"}},
{"legacy profileID for ED key", "abc/my_dataset@12D3KooWDbd4L1UzsmxH7T7nufQBL3jC9MpS6syvXZjRdk4XqoK4/ipfs/QmSecond", Ref{Username: "abc", Name: "my_dataset", ProfileID: "12D3KooWDbd4L1UzsmxH7T7nufQBL3jC9MpS6syvXZjRdk4XqoK4", Path: "/ipfs/QmSecond"}},
}
for i, c := range goodCases {
ref, err := Parse(c.text)
if err != nil {
t.Errorf("case %d %q error: %s", i, c.description, err)
continue
}
if !ref.Equals(c.expect) {
t.Errorf("case %d %q mismatch: expect %q, got %q", i, c.description, c.expect, ref)
}
}
badCases := []struct {
description string
text string
expectErr string
}{
{"missing at", "/ipfs/QmThis", "unexpected character at position 0: '/'"},
{"invalid base58", "@/ipfs/QmOne", "path contains invalid base58 characters"},
{"no slash", "foo", "need username separated by '/' from dataset name"},
{"http url", "https://apple.com", "unexpected character at position 5: ':'"},
{"domain name", "apple.com", "unexpected character at position 5: '.'"},
{"local filename", "foo.json", "unexpected character at position 3: '.'"},
{"absolute filepath", "/usr/local/bin/file.cbor", "unexpected character at position 0: '/'"},
{"absolute dirname", "/usr/local/bin", "unexpected character at position 0: '/'"},
{"dot in dataset", "abc/data.set", "unexpected character at position 8: '.'"},
{"equals in dataset", "abc/my+ds", "unexpected character at position 6: '+'"},
}
for i, c := range badCases {
_, err := Parse(c.text)
if err == nil || err.Error() != c.expectErr {
t.Errorf("case %d %q expected error: %q, got %q", i, c.description, c.expectErr, err)
continue
}
}
}
func TestParseBadUpperCase(t *testing.T) {
ref, err := Parse("test_peer_bad_upper_case/a_New_Dataset")
if err != ErrBadCaseName {
t.Errorf("expected to get error %s, but got %s", ErrBadCaseName, err)
}
expect := Ref{Username: "test_peer_bad_upper_case", Name: "a_New_Dataset"}
if !ref.Equals(expect) {
t.Errorf("mismatch: expect %s, got %s", expect, ref)
}
}
func TestParseHumanFriendly(t *testing.T) {
goodCases := []struct {
description string
text string
expect Ref
}{
{"human friendly", "abc/my_dataset", Ref{Username: "abc", Name: "my_dataset"}},
}
for i, c := range goodCases {
ref, err := ParseHumanFriendly(c.text)
if err != nil {
t.Errorf("case %d %q error: %s", i, c.description, err)
continue
}
if !ref.Equals(c.expect) {
t.Errorf("case %d %q mismatch: expect %s, got %s", i, c.description, c.expect, ref)
}
}
badCases := []struct {
description string
text string
expectErr string
}{
{"full reference", "abc/my_dataset@QmFirst/ipfs/QmSecond", ErrNotHumanFriendly.Error()},
{"only name", "my_dataset", "need username separated by '/' from dataset name"},
{"right hand side", "@QmFirst/ipfs/QmSecond", ErrNotHumanFriendly.Error()},
{"just path", "@/ipfs/QmSecond", ErrNotHumanFriendly.Error()},
{"missing at", "/ipfs/QmThis", "unexpected character at position 0: '/'"},
{"invalid base58", "@/ipfs/QmOne", ErrNotHumanFriendly.Error()},
}
for i, c := range badCases {
_, err := ParseHumanFriendly(c.text)
if err == nil || err.Error() != c.expectErr {
t.Errorf("case %d %q expected error: %q, got %q", i, c.description, c.expectErr, err)
continue
}
}
}
func TestIsValidName(t *testing.T) {
goodCases := []struct {
text string
}{
{"abc"},
{"aDataset"},
{"a1234"},
{"a_dataset_name"},
{"DatasetName"},
}
for i, c := range goodCases {
if !IsValidName(c.text) {
t.Errorf("case %d %q should be valid", i, c.text)
continue
}
}
badCases := []struct {
text string
}{
{"_bad"},
{"1dataset"},
{"dataset!"},
}
for i, c := range badCases {
if IsValidName(c.text) {
t.Errorf("case %d %q should not be considered valid", i, c.text)
continue
}
}
}