government / ast / oid_test.go

@ de62e8f8076f6010f59527991dbc61d7625a603e | 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)
		}
	}
}

func TestBuiltinCodesArePinned(t *testing.T) {
	// Published codes must never change; this list is a second copy of the
	// DESIGN.md table so an accidental edit to builtinCodes fails here.
	want := map[string]uint64{
		ListNode: 1, Dot: 2, True: 3, False: 4, Empty: 5,
		"quote": 10, "if": 11, "define": 12, "lambda": 13, "and": 14, "or": 15,
		"not": 20, "+": 21, "-": 22, "*": 23, "/": 24, "<": 25, ">": 26,
		"<=": 27, ">=": 28, "==": 29, "!=": 30, "cons": 31, "car": 32,
		"cdr": 33, "strlen": 34, "strcat": 35, "str?": 36,
	}
	if len(builtinCodes) != len(want) {
		t.Errorf("%d builtins, want %d", len(builtinCodes), len(want))
	}
	for _, b := range builtinCodes {
		if want[b.name] != b.code {
			t.Errorf("%s has code %d, want %d", b.name, b.code, want[b.name])
		}
	}
	if len(nameByCode) != len(builtinCodes) {
		t.Error("two builtins share a code")
	}
}