You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
77 lines
2.3 KiB
77 lines
2.3 KiB
package der
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"testing"
|
|
)
|
|
|
|
func TestCheck(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name, hex string
|
|
ok bool
|
|
}{
|
|
{"sequence of an integer and a null", "3005020101" + "0500", true},
|
|
{"a long length", "04" + "8180" + zeros(128), true},
|
|
{"a long form under 128", "0481" + "01" + "00", false},
|
|
{"a length with a leading zero", "04820001" + "00", false},
|
|
{"an indefinite length", "30800000", false},
|
|
{"a high tag number", "1f0100", false},
|
|
{"truncated", "0402aa", false},
|
|
{"trailing bytes", "0500" + "00", false},
|
|
{"BOOLEAN 01", "010101", false},
|
|
{"BOOLEAN FF", "0101ff", true},
|
|
{"INTEGER with a leading zero", "02020001", false},
|
|
{"INTEGER 0x80 with its zero", "02020080", true},
|
|
{"INTEGER with a leading FF", "0202ff80", false},
|
|
{"empty INTEGER", "0200", false},
|
|
{"NULL with content", "050100", false},
|
|
{"constructed OCTET STRING", "2404" + "0402aabb", false},
|
|
{"BIT STRING with unused bits set", "03020701", false},
|
|
{"BIT STRING with unused bits clear", "03020780", true},
|
|
{"BIT STRING of 4 bits", "030204f0", true},
|
|
{"OID", "06032a0304", true},
|
|
{"OID with a leading 0x80", "06038001" + "02", false},
|
|
{"OID that does not end", "06022a83", false},
|
|
{"context tag, constructed", "a003020101", true},
|
|
{"SET in primitive form", "1100", false},
|
|
{"the end of contents", "0000", false},
|
|
{"a reserved universal tag", "0e0141", false},
|
|
{"a SEQUENCE of the end of contents", "30020000", false},
|
|
{"UTF8String", "0c026162", true},
|
|
} {
|
|
b, err := hex.DecodeString(tc.hex)
|
|
if err != nil {
|
|
t.Fatal(tc.name, err)
|
|
}
|
|
if err := Check(b); (err == nil) != tc.ok {
|
|
t.Errorf("%s: %v", tc.name, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func zeros(n int) string {
|
|
b := make([]byte, n*2)
|
|
for i := range b {
|
|
b[i] = '0'
|
|
}
|
|
return string(b)
|
|
}
|
|
|
|
func TestSetOfSorted(t *testing.T) {
|
|
a, b := []byte{0x02, 0x01, 0x01}, []byte{0x02, 0x01, 0x02}
|
|
if !SetOfSorted([][]byte{a, b}) || SetOfSorted([][]byte{b, a}) || SetOfSorted([][]byte{a, a}) {
|
|
t.Error("SetOfSorted")
|
|
}
|
|
}
|
|
|
|
func TestSplit(t *testing.T) {
|
|
b, _ := hex.DecodeString("30050201010500")
|
|
if err := Check(b); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
id, kids, err := Split(b)
|
|
if err != nil || id != 0x30 || len(kids) != 2 || len(kids[0]) != 3 || len(kids[1]) != 2 {
|
|
t.Errorf("%x %v %v", id, kids, err)
|
|
}
|
|
}
|