This is mostly equivalent to email without IP addresses. Signed-off-by: Yonah <contrib@gensokyo.uk>
84 lines
1.9 KiB
Go
84 lines
1.9 KiB
Go
package ident_test
|
|
|
|
import (
|
|
"bytes"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.gensokyo.uk/cofront/cof-spec/ident"
|
|
)
|
|
|
|
// mustNewRemote validates remote and returns its corresponding [ident.Remote].
|
|
// If remote is invalid, mustNewRemote panics.
|
|
func mustNewRemote(remote string) (r ident.Remote) {
|
|
if err := r.UnmarshalText([]byte(remote)); err != nil {
|
|
panic(err)
|
|
}
|
|
return
|
|
}
|
|
|
|
func TestRemote(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
testCases := []struct {
|
|
name string
|
|
remote string
|
|
err error
|
|
}{
|
|
{"short", "", ident.ErrShortRemote},
|
|
{"long", strings.Repeat("\x00", 256), ident.ErrLongRemote},
|
|
|
|
{"short label", "test.label.short..empty", &ident.InvalidLabelError{
|
|
Data: []byte{},
|
|
Label: 3,
|
|
Index: -1,
|
|
Reason: ident.InvalidLabelShort,
|
|
}},
|
|
|
|
{"long label", "test.label.long." + strings.Repeat("\x00", 64), &ident.InvalidLabelError{
|
|
Data: bytes.Repeat([]byte{0}, 64),
|
|
Label: 3,
|
|
Index: 63,
|
|
Reason: ident.InvalidLabelLong,
|
|
}},
|
|
|
|
{"ldh prefix", "test.prefix.-invalid-", &ident.InvalidLabelError{
|
|
Data: []byte("-invalid-"),
|
|
Label: 2,
|
|
Index: 0,
|
|
Reason: ident.InvalidLabelLDH,
|
|
}},
|
|
|
|
{"ldh suffix", "test.suffix.invalid--", &ident.InvalidLabelError{
|
|
Data: []byte("invalid--"),
|
|
Label: 2,
|
|
Index: 8,
|
|
Reason: ident.InvalidLabelLDH,
|
|
}},
|
|
|
|
{"ldh", "test.invalid:byte", &ident.InvalidLabelError{
|
|
Data: []byte("invalid:byte"),
|
|
Label: 1,
|
|
Index: 7,
|
|
Reason: ident.InvalidLabelLDH,
|
|
}},
|
|
|
|
{"simple", "localhost", nil},
|
|
{"valid", "gensokyo.uk", nil},
|
|
{"full", "ABCDEFGHIJKLMNOPQRSTUVWXYZ." +
|
|
"abcdefghijklmnopqrstuvwxyz." +
|
|
"0123456789-9876543210", nil},
|
|
}
|
|
|
|
rtc := make(rTestCases[ident.Remote, *ident.Remote], len(testCases))
|
|
for i, tc := range testCases {
|
|
rtc[i].name = tc.name
|
|
if tc.err == nil {
|
|
rtc[i].ident = mustNewRemote(tc.remote)
|
|
}
|
|
rtc[i].want = []byte(tc.remote)
|
|
rtc[i].err = tc.err
|
|
}
|
|
rtc.run(t)
|
|
}
|