@ 2dbe47969d09d4856796bbf662b4e11c74a7f4fe | history
package ast
import (
"crypto/x509"
"strings"
"testing"
)
func gov(s string) string { return strings.Replace(s, "GOV", Root, 1) }
func mustParse(t *testing.T, s string) x509.OID {
t.Helper()
oid, err := x509.ParseOID(gov(s))
if err != nil {
t.Fatalf("ParseOID(%s): %v", s, err)
}
return oid
}
func TestNamespaceTable(t *testing.T) {
tests := []struct {
ns Namespace
id uint64
text string
oid string
}{
{NSBuiltin, 7, "+", "GOV.0.7.21"},
{NSSymbol, 8, "foo", "GOV.1.8.102.111.111"},
{NSInt, 9, "-42", "GOV.2.9.45.52.50"},
{NSFloat, 10, "3.14", "GOV.3.10.51.46.49.52"},
{NSString, 11, "hi", "GOV.4.11.104.105"},
{NSString, 12, "", "GOV.4.12"},
{NSBuiltin, 13, ListNode, "GOV.0.13.1"},
{NSBuiltin, 14, Empty, "GOV.0.14.5"},
{NSSymbol, 15, "😀", "GOV.1.15.128512"},
{NSSymbol, 1 << 40, "x", "GOV.1.1099511627776.120"},
}
for _, tt := range tests {
oid, err := EncodeOID(tt.ns, tt.id, tt.text)
if err != nil {
t.Errorf("EncodeOID(%s, %d, %q): %v", tt.ns, tt.id, tt.text, err)
continue
}
if got, want := oid.String(), gov(tt.oid); got != want {
t.Errorf("EncodeOID(%s, %d, %q) = %s, want %s", tt.ns, tt.id, tt.text, got, tt.oid)
}
ns, id, text, err := DecodeOID(mustParse(t, tt.oid))
if err != nil {
t.Errorf("DecodeOID(%s): %v", tt.oid, err)
continue
}
if ns != tt.ns || id != tt.id || text != tt.text {
t.Errorf("DecodeOID(%s) = %s, %d, %q", tt.oid, ns, id, text)
}
}
}
func TestEncodeRejects(t *testing.T) {
tests := []struct {
ns Namespace
id uint64
text string
want string
}{
{NSSymbol, 0, "x", "positive"},
{NSBuiltin, 1, "foo", "not a builtin"},
{NSSymbol, 1, "", "empty symbol"},
{NSSymbol, 1, "+", "builtin name"},
{NSSymbol, 1, "T", "builtin name"},
{NSSymbol, 1, "12", "reads as a number"},
{NSSymbol, 1, "1.", "reads as a number"},
{NSInt, 1, "007", "malformed"},
{NSInt, 1, "1.5", "not an integer"},
{NSInt, 1, "9223372036854775808", "64 bits"},
{NSFloat, 1, "1.", "malformed"},
{NSFloat, 1, "1e5", "malformed"},
{NSFloat, 1, "42", "not a float"},
{NSFloat, 1, "1.0e999", "out of range"},
{NSString, 1, "\xff", "UTF-8"},
{5, 1, "x", "unknown namespace 5"},
}
for _, tt := range tests {
_, err := EncodeOID(tt.ns, tt.id, tt.text)
if err == nil || !strings.Contains(err.Error(), tt.want) {
t.Errorf("EncodeOID(%s, %d, %q) error = %v, want %q", tt.ns, tt.id, tt.text, err, tt.want)
}
}
}
func TestDecodeRejects(t *testing.T) {
tests := []struct {
oid string
want string
}{
{"2.5.29.32.0", "not under"},
{"2.25.96655360458544534101705795377599264041.1.1.120", "not under"}, // Root's arc with a digit added
{"GOV", "no node id"},
{"GOV.1", "no node id"},
{"GOV.5.1.120", "unknown namespace 5"},
{"GOV.1.0.120", "positive"},
{"GOV.0.1", "exactly one payload arc, got 0"},
{"GOV.0.1.21.21", "exactly one payload arc, got 2"},
{"GOV.0.1.6", "unknown builtin code 6"},
{"GOV.4.1.55296", "not a Unicode code point"}, // U+D800, a surrogate
{"GOV.4.1.1114112", "not a Unicode code point"}, // one past U+10FFFF
{"GOV.1.1", "empty symbol"},
{"GOV.1.1.49.46", "reads as a number"}, // "1."
{"GOV.3.1.49.46", "malformed"},
{"GOV.1.18446744073709551616.120", "64 bits"},
}
for _, tt := range tests {
_, _, _, err := DecodeOID(mustParse(t, tt.oid))
if err == nil || !strings.Contains(err.Error(), tt.want) {
t.Errorf("DecodeOID(%s) error = %v, want %q", tt.oid, err, tt.want)
}
}
}