1
2
3
4
5 package x509
6
7 import (
8 "encoding/asn1"
9 "math"
10 "testing"
11 )
12
13 func TestOID(t *testing.T) {
14 var tests = []struct {
15 raw []byte
16 valid bool
17 str string
18 ints []uint64
19 }{
20 {[]byte{}, false, "", nil},
21 {[]byte{0x80, 0x01}, false, "", nil},
22 {[]byte{0x01, 0x80, 0x01}, false, "", nil},
23
24 {[]byte{1, 2, 3}, true, "0.1.2.3", []uint64{0, 1, 2, 3}},
25 {[]byte{41, 2, 3}, true, "1.1.2.3", []uint64{1, 1, 2, 3}},
26 {[]byte{86, 2, 3}, true, "2.6.2.3", []uint64{2, 6, 2, 3}},
27
28 {[]byte{41, 255, 255, 255, 127}, true, "1.1.268435455", []uint64{1, 1, 268435455}},
29 {[]byte{41, 0x87, 255, 255, 255, 127}, true, "1.1.2147483647", []uint64{1, 1, 2147483647}},
30 {[]byte{41, 255, 255, 255, 255, 127}, true, "1.1.34359738367", []uint64{1, 1, 34359738367}},
31 {[]byte{42, 255, 255, 255, 255, 255, 255, 255, 255, 127}, true, "1.2.9223372036854775807", []uint64{1, 2, 9223372036854775807}},
32 {[]byte{43, 0x81, 255, 255, 255, 255, 255, 255, 255, 255, 127}, true, "1.3.18446744073709551615", []uint64{1, 3, 18446744073709551615}},
33 {[]byte{44, 0x83, 255, 255, 255, 255, 255, 255, 255, 255, 127}, true, "1.4.36893488147419103231", nil},
34 {[]byte{85, 255, 255, 255, 255, 255, 255, 255, 255, 255, 127}, true, "2.5.1180591620717411303423", nil},
35 {[]byte{85, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 127}, true, "2.5.19342813113834066795298815", nil},
36
37 {[]byte{255, 255, 255, 127}, true, "2.268435375", []uint64{2, 268435375}},
38 {[]byte{0x87, 255, 255, 255, 127}, true, "2.2147483567", []uint64{2, 2147483567}},
39 {[]byte{255, 127}, true, "2.16303", []uint64{2, 16303}},
40 {[]byte{255, 255, 255, 255, 127}, true, "2.34359738287", []uint64{2, 34359738287}},
41 {[]byte{255, 255, 255, 255, 255, 255, 255, 255, 127}, true, "2.9223372036854775727", []uint64{2, 9223372036854775727}},
42 {[]byte{0x81, 255, 255, 255, 255, 255, 255, 255, 255, 127}, true, "2.18446744073709551535", []uint64{2, 18446744073709551535}},
43 {[]byte{0x83, 255, 255, 255, 255, 255, 255, 255, 255, 127}, true, "2.36893488147419103151", nil},
44 {[]byte{255, 255, 255, 255, 255, 255, 255, 255, 255, 127}, true, "2.1180591620717411303343", nil},
45 {[]byte{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 127}, true, "2.19342813113834066795298735", nil},
46 }
47
48 for _, v := range tests {
49 oid, ok := newOIDFromDER(v.raw)
50 if ok != v.valid {
51 if ok {
52 t.Errorf("%v: unexpected success while parsing: %v", v.raw, oid)
53 } else {
54 t.Errorf("%v: unexpected failure while parsing", v.raw)
55 }
56 continue
57 }
58
59 if !ok {
60 continue
61 }
62
63 if str := oid.String(); str != v.str {
64 t.Errorf("%v: oid.String() = %v, want; %v", v.raw, str, v.str)
65 }
66
67 var asn1OID asn1.ObjectIdentifier
68 for _, v := range v.ints {
69 if v > math.MaxInt32 {
70 asn1OID = nil
71 break
72 }
73 asn1OID = append(asn1OID, int(v))
74 }
75
76 o, ok := oid.toASN1OID()
77 if shouldOk := asn1OID != nil; shouldOk != ok {
78 if ok {
79 t.Errorf("%v: oid.toASN1OID() unexpected success", v.raw)
80 } else {
81 t.Errorf("%v: oid.toASN1OID() unexpected fauilure", v.raw)
82 }
83 continue
84 }
85
86 if asn1OID != nil {
87 if !o.Equal(asn1OID) {
88 t.Errorf("%v: oid.toASN1OID(asn1OID).Equal(oid) = false, want: true", v.raw)
89 }
90 }
91
92 if v.ints != nil {
93 oid2, err := OIDFromInts(v.ints)
94 if err != nil {
95 t.Errorf("%v: OIDFromInts() unexpected error: %v", v.raw, err)
96 }
97 if !oid2.Equal(oid) {
98 t.Errorf("%v: %#v.Equal(%#v) = false, want: true", v.raw, oid2, oid)
99 }
100 }
101 }
102 }
103
104 func mustNewOIDFromInts(t *testing.T, ints []uint64) OID {
105 oid, err := OIDFromInts(ints)
106 if err != nil {
107 t.Fatalf("OIDFromInts(%v) unexpected error: %v", ints, err)
108 }
109 return oid
110 }
111
View as plain text