-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhardening_test.go
More file actions
225 lines (208 loc) · 7.34 KB
/
Copy pathhardening_test.go
File metadata and controls
225 lines (208 loc) · 7.34 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
package localized_test
import (
"encoding/json"
"errors"
"fmt"
"strings"
"sync"
"testing"
language "github.com/faustbrian/go-international/locale"
localized "github.com/faustbrian/go-localized"
localizedmatch "github.com/faustbrian/go-localized/match"
)
func TestStandardsCanonicalizationMatrix(t *testing.T) {
t.Parallel()
tests := []struct {
class string
raw string
want string
}{
{"language", "FI", "fi"},
{"script", "zh-hant", "zh-Hant"},
{"region", "en-us", "en-US"},
{"variant", "sl-rozaj-biske", "sl-rozaj-biske"},
{"extension", "en-u-ca-gregory", "en-u-ca-gregory"},
{"private use", "x-tenant", "x-tenant"},
{"grandfathered preferred value", "i-klingon", "tlh"},
{"deprecated preferred value", "iw-IL", "he-IL"},
{"undetermined", "und", "und"},
{"multiple", "mul", "mul"},
{"reserved unknown", "qaa", "qaa"},
}
for _, test := range tests {
t.Run(test.class, func(t *testing.T) {
tag, err := language.Parse(test.raw)
if err != nil {
t.Fatalf("language.Parse(%q) error = %v", test.raw, err)
}
value, err := localized.NewText(localized.Entry{Locale: tag, Text: test.class})
if err != nil {
t.Fatalf("NewText(%q) error = %v", test.raw, err)
}
if got := value.Pairs()[0].Locale; got != test.want {
t.Fatalf("canonical locale = %q, want %q", got, test.want)
}
})
}
for _, raw := range []string{"", "en_", "en-", "en us", "-fi"} {
if _, err := localized.TextFromMap(map[string]string{raw: "value"}); !errors.Is(err, localized.ErrInvalidLocale) {
t.Fatalf("TextFromMap(%q) error = %v", raw, err)
}
}
}
func TestStandardsRegistryProvenance(t *testing.T) {
t.Parallel()
provenance := language.DatasetProvenance()
if provenance.Dataset != "iana-language-subtag-registry" ||
provenance.UpstreamVersion != "IANA registry 2026-06-14; x/text v0.40.0" ||
provenance.SHA256 != "be1fad86a99e3a932d07b80c9b3c271ec2381a5909ce22420144e5077ab0a43a" {
t.Fatalf("locale provenance drifted: %+v", provenance)
}
}
func TestStandardsMatchingMatrix(t *testing.T) {
t.Parallel()
value, err := localized.TextFromMap(map[string]string{
"de": "Deutsch", "en-GB": "Hallo", "en-US": "Hello", "mul": "Many",
"und": "Unknown", "x-tenant": "Private", "zh-Hant": "您好",
})
if err != nil {
t.Fatal(err)
}
tests := []struct {
name string
requested string
kind localizedmatch.Kind
selected string
}{
{"script parent", "zh-Hant-TW", localizedmatch.Matched, "zh-Hant"},
{"region choice", "en-CA", localizedmatch.Matched, "en-GB"},
{"variant parent", "de-1996", localizedmatch.Matched, "de"},
{"private exact", "x-tenant", localizedmatch.Exact, "x-tenant"},
{"und exact", "und", localizedmatch.Exact, "und"},
{"mul exact", "mul", localizedmatch.Exact, "mul"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result, matchErr := localizedmatch.Best(value, localizedmatch.Preference{
Locale: mustLocale(t, test.requested), Weight: 1,
})
if matchErr != nil || result.Kind != test.kind || result.Locale.String() != test.selected {
t.Fatalf("Best(%s) = %+v, %v", test.requested, result, matchErr)
}
})
}
}
func TestMergeMissingEmptyConflictMatrix(t *testing.T) {
t.Parallel()
type state struct {
present bool
text string
}
states := []state{{}, {present: true}, {present: true, text: "value"}}
policies := []localized.MergePolicy{localized.LeftWins, localized.RightWins, localized.RejectConflict}
for leftIndex, leftState := range states {
for rightIndex, rightState := range states {
for _, emptyPolicy := range []localized.EmptyPolicy{localized.EmptyIsValue, localized.EmptyIsAbsent} {
for _, policy := range policies {
name := fmt.Sprintf("left-%d/right-%d/empty-%d/policy-%d", leftIndex, rightIndex, emptyPolicy, policy)
t.Run(name, func(t *testing.T) {
left := matrixText(t, leftState.present, leftState.text)
right := matrixText(t, rightState.present, rightState.text)
result, err := left.MergeWithOptions(right, localized.MergeOptions{Conflicts: policy, Empty: emptyPolicy})
leftEffective := leftState.present && (emptyPolicy == localized.EmptyIsValue || leftState.text != "")
rightEffective := rightState.present && (emptyPolicy == localized.EmptyIsValue || rightState.text != "")
if policy == localized.RejectConflict && leftEffective && rightEffective {
if !errors.Is(err, localized.ErrConflict) {
t.Fatalf("error = %v, want ErrConflict", err)
}
return
}
if err != nil {
t.Fatal(err)
}
got, present := result.Get(mustLocale(t, "en"))
want, wantPresent := "", false
switch {
case leftEffective && rightEffective && policy == localized.RightWins:
want, wantPresent = rightState.text, true
case leftEffective:
want, wantPresent = leftState.text, true
case rightEffective:
want, wantPresent = rightState.text, true
}
if got != want || present != wantPresent {
t.Fatalf("Get(en) = %q, %v; want %q, %v", got, present, want, wantPresent)
}
})
}
}
}
}
}
func matrixText(t *testing.T, present bool, text string) localized.Text {
t.Helper()
if !present {
return localized.Text{}
}
value, err := localized.NewText(localized.Entry{Locale: mustLocale(t, "en"), Text: text})
if err != nil {
t.Fatal(err)
}
return value
}
func TestConcurrentImmutableOperationSurface(t *testing.T) {
t.Parallel()
value, _ := localized.TextFromMap(map[string]string{"en-US": "Hello", "fi": "", "zh-Hant": "您好"})
overlay, _ := localized.TextFromMap(map[string]string{"sv": "Hej"})
requested := mustLocale(t, "zh-Hant-TW")
plan, err := localizedmatch.NewPlan([]localizedmatch.Chain{{
From: requested, Candidates: []localizedmatch.Candidate{{Kind: localizedmatch.ParentRange, Locale: requested}},
}}, localizedmatch.PlanOptions{MaxDepth: 2, MaxCandidates: 1})
if err != nil {
t.Fatal(err)
}
var wait sync.WaitGroup
for range 32 {
wait.Add(1)
go func() {
defer wait.Done()
if text, present := value.Get(mustLocale(t, "en-US")); !present || text != "Hello" {
t.Errorf("Get() = %q, %v", text, present)
}
count := 0
for range value.All() {
count++
}
if count != value.Len() {
t.Errorf("iterator count = %d", count)
}
matched, matchErr := localizedmatch.Best(value, localizedmatch.Preference{Locale: mustLocale(t, "en-CA"), Weight: 1})
if matchErr != nil || !matched.Present {
t.Errorf("Best() = %+v, %v", matched, matchErr)
}
if resolved := plan.Resolve(value, requested); !resolved.Present || resolved.Text != "您好" {
t.Errorf("Resolve() = %+v", resolved)
}
merged, mergeErr := value.Merge(overlay, localized.RightWins)
if mergeErr != nil || merged.Len() != value.Len()+1 {
t.Errorf("Merge() len = %d, error = %v", merged.Len(), mergeErr)
}
encoded, encodeErr := json.Marshal(value)
if encodeErr != nil || !strings.Contains(string(encoded), `"fi":""`) {
t.Errorf("Marshal() = %s, %v", encoded, encodeErr)
}
}()
}
wait.Wait()
}
func TestErrorsDoNotDiscloseLocalizedContent(t *testing.T) {
t.Parallel()
secret := "secret-customer-content"
_, err := localized.NewTextWithLimits(
localized.Limits{MaxLocales: 1, MaxTagBytes: 8, MaxTextBytes: 1, MaxTotalBytes: 1},
localized.Entry{Locale: mustLocale(t, "en"), Text: secret},
)
if err == nil || strings.Contains(err.Error(), secret) {
t.Fatalf("error = %q", err)
}
}