~bigbes/sr-ht-spec

ref: 54fac8ddcf7a3232e2335300db465a6244a1b4be sr-ht-spec/core/names_test.go -rw-r--r-- 6.3 KiB
54fac8dd — bigbes docs: resolve bilingual search with per-line routing 27 days ago
                                                                                
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
package core

import (
	"errors"
	"strings"
	"testing"
)

func TestValidateOwner(t *testing.T) {
	tests := []struct {
		in string
		ok bool
	}{
		{"bigbes", true},
		{"user_name", true},
		{"user-name", true},
		{"user.name", true},
		{"u123", true},
		{"a", true},
		{strings.Repeat("a", MaxOwnerLen), true},

		{"", false},
		{strings.Repeat("a", MaxOwnerLen+1), false},
		{"-leading", false},
		{"~bigbes", false}, // caller must strip the '~' first
		{"has/slash", false},
		{"has..dots", false},
		{"..", false},
		{"Upper", false},
		{"has space", false},
		{"tilde~", false},
		{"emojiπŸ˜€", false},
		{"ΠΏΡ€ΠΈΠ²Π΅Ρ‚", false},
		{"nul\x00byte", false},
	}
	for _, tc := range tests {
		err := ValidateOwner(tc.in)
		if (err == nil) != tc.ok {
			t.Errorf("ValidateOwner(%q) = %v, want ok=%v", tc.in, err, tc.ok)
		}
		if err != nil && !errors.Is(err, ErrInvalidName) {
			t.Errorf("ValidateOwner(%q) error %v is not ErrInvalidName", tc.in, err)
		}
	}
}

func TestValidateSpaceName(t *testing.T) {
	tests := []struct {
		in string
		ok bool
	}{
		{"rfcs", true},
		{"tarantool-rfcs", true},
		{"home_ops", true},
		{"v1.0", true},
		{strings.Repeat("a", MaxSpaceNameLen), true},

		{"", false},
		{strings.Repeat("a", MaxSpaceNameLen+1), false},
		{"-dashfirst", false},
		{"a/b", false},
		{"a..b", false},
		{".", false},  // single dot is a legal byte set but "." alone is a directory
		{"..", false}, // traversal
		{"CamelCase", false},
		{"+everything", false}, // project namespace, not a space name
	}
	for _, tc := range tests {
		err := ValidateSpaceName(tc.in)
		if (err == nil) != tc.ok {
			t.Errorf("ValidateSpaceName(%q) = %v, want ok=%v", tc.in, err, tc.ok)
		}
	}
}

func TestValidateSpaceNameRejectsBareDot(t *testing.T) {
	// "." passes the byte-set rule, so it must be caught somewhere: a space
	// literally named "." would resolve to the repos root.
	if err := ValidateSpaceName("."); err == nil {
		t.Fatal(`ValidateSpaceName(".") must fail: it would resolve to the repos root`)
	}
}

func TestParseSpaceRef(t *testing.T) {
	tests := []struct {
		name      string
		in        string
		wantOwner string
		wantName  string
		ok        bool
	}{
		{"tilde form", "~bigbes/rfcs", "bigbes", "rfcs", true},
		{"bare form", "bigbes/rfcs", "bigbes", "rfcs", true},
		{"leading slash", "/~bigbes/rfcs", "bigbes", "rfcs", true},
		{"trailing slash", "~bigbes/rfcs/", "bigbes", "rfcs", true},

		{"empty", "", "", "", false},
		{"slashes only", "///", "", "", false},
		{"one segment", "~bigbes", "", "", false},
		{"three segments", "~bigbes/rfcs/docs", "", "", false},
		{"empty owner", "~/rfcs", "", "", false},
		{"empty name", "~bigbes/", "", "", false},
		{"traversal owner", "~../rfcs", "", "", false},
		{"traversal name", "~bigbes/..", "", "", false},
		{"uppercase owner", "~BigBes/rfcs", "", "", false},
		{"space in name", "~bigbes/my rfcs", "", "", false},
	}
	for _, tc := range tests {
		t.Run(tc.name, func(t *testing.T) {
			ref, err := ParseSpaceRef(tc.in)
			if (err == nil) != tc.ok {
				t.Fatalf("ParseSpaceRef(%q) = %v, %v, want ok=%v", tc.in, ref, err, tc.ok)
			}
			if !tc.ok {
				if !errors.Is(err, ErrInvalidName) {
					t.Fatalf("error %v is not ErrInvalidName", err)
				}
				return
			}
			if ref.Owner != tc.wantOwner || ref.Name != tc.wantName {
				t.Fatalf("ParseSpaceRef(%q) = %+v, want {%q %q}", tc.in, ref, tc.wantOwner, tc.wantName)
			}
			if got, want := ref.String(), "~"+tc.wantOwner+"/"+tc.wantName; got != want {
				t.Fatalf("String() = %q, want %q", got, want)
			}
		})
	}
}

func TestValidatePath(t *testing.T) {
	tests := []struct {
		name string
		in   string
		ok   bool
	}{
		{"simple", "specs/0007-storage.md", true},
		{"root file", "README.md", true},
		{"dotfile", ".spec.yml", true},
		{"deep", "a/b/c/d/e.md", true},
		{"cyrillic", "спСки/Ρ…Ρ€Π°Π½ΠΈΠ»ΠΈΡ‰Π΅.md", true},
		{"attachment", "assets/diagram.png", true},
		{"dot in name", "v1.2.3.md", true},
		{"leading dot component", "notes/.private.md", true},
		{"max length", strings.Repeat("a", MaxPathLen), true},

		{"empty", "", false},
		{"too long", strings.Repeat("a", MaxPathLen+1), false},
		{"absolute", "/etc/passwd", false},
		{"trailing slash", "specs/", false},
		{"parent traversal", "../etc/passwd", false},
		{"embedded traversal", "specs/../../etc/passwd", false},
		{"interior traversal", "specs/../notes/a.md", false},
		{"dot component", "./a.md", false},
		{"interior dot component", "a/./b.md", false},
		{"empty component", "specs//a.md", false},
		{"git dir", ".git/config", false},
		{"nested git dir", "specs/.git/HEAD", false},
		{"backslash", `specs\a.md`, false},
		{"windows traversal", `..\a.md`, false},
		{"nul byte", "specs/a\x00.md", false},
		{"newline", "specs/a\nb.md", false},
		{"tab", "specs/a\tb.md", false},
		{"del", "specs/a\x7f.md", false},
		{"rtl override", "specs/exe\u202egnp.md", false},
		{"rtl mark", "specs/a\u200fb.md", false},
		{"pop directional isolate", "specs/a\u2069b.md", false},
		{"invalid utf-8", "specs/\xff\xfe.md", false},
		{"trailing dot component", "specs/a./b.md", false},
		{"trailing space component", "specs/a /b.md", false},
		{"trailing space at end", "specs/a.md ", false},
	}
	for _, tc := range tests {
		t.Run(tc.name, func(t *testing.T) {
			err := ValidatePath(tc.in)
			if (err == nil) != tc.ok {
				t.Fatalf("ValidatePath(%q) = %v, want ok=%v", tc.in, err, tc.ok)
			}
			if err != nil && !errors.Is(err, ErrInvalidPath) {
				t.Fatalf("error %v is not ErrInvalidPath", err)
			}
		})
	}
}

func TestValidateDocPath(t *testing.T) {
	tests := []struct {
		name string
		in   string
		ok   bool
	}{
		{"spec", "specs/0007-storage.md", true},
		{"root", "README.md", true},
		{"hidden doc", "notes/.draft.md", true},
		{"cyrillic", "спСки/Ρ…Ρ€Π°Π½ΠΈΠ»ΠΈΡ‰Π΅.md", true},

		{"no extension", "specs/0007-storage", false},
		{"wrong extension", "specs/0007-storage.markdown", false},
		{"uppercase extension", "specs/0007-storage.MD", false},
		{"extension only", ".md", false},
		{"extension only in dir", "specs/.md", false},
		{"extension in middle", "specs/a.md.txt", false},
		{"traversal wins", "../a.md", false},
		{"empty", "", false},
	}
	for _, tc := range tests {
		t.Run(tc.name, func(t *testing.T) {
			err := ValidateDocPath(tc.in)
			if (err == nil) != tc.ok {
				t.Fatalf("ValidateDocPath(%q) = %v, want ok=%v", tc.in, err, tc.ok)
			}
			if err != nil && !errors.Is(err, ErrInvalidPath) {
				t.Fatalf("error %v is not ErrInvalidPath", err)
			}
		})
	}
}