-
Notifications
You must be signed in to change notification settings - Fork 1
/
oid_test.go
165 lines (146 loc) · 3.89 KB
/
oid_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package objectid
import (
"fmt"
"math/big"
"testing"
)
func TestNewOID(t *testing.T) {
for idx, typ := range []any{
[]string{
`iso(1)`,
`identified-organization(3)`,
`dod(6)`,
`internet(1)`,
`private(4)`,
`enterprise(1)`,
`56521`,
`example(999)`,
},
nil,
`{iso(1) identified-organization(3) dod(6) internet(1) private(4) enterprise(1) 56521 example(999)}`,
float32(1),
} {
_, err := NewOID(typ)
if err != nil && idx%2 == 0 {
t.Errorf("%s failed: %v", t.Name(), err)
return
} else if err == nil && idx%2 != 0 {
t.Errorf("%s failed: no error where one was expected", t.Name())
return
}
}
}
func ExampleOID_Dot() {
raw := `{iso(1) identified-organization(3) dod(6) internet(1) private(4) enterprise(1) 56521 example(999)}`
id, err := NewOID(raw)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("%s", id.Dot())
// Output: 1.3.6.1.4.1.56521.999
}
/*
This example demonstrates a bogus [DotNotation] output due to the presence of
less than two (2) [NameAndNumberForm] instances within the receiver.
[DotNotation] ALWAYS requires two (2) or more elements to be considered valid.
*/
func ExampleOID_Dot_bogus() {
id, err := NewOID(`{iso(1)}`)
if err != nil {
fmt.Println(err)
return
}
dot := id.Dot()
fmt.Println(dot)
// Output:
}
func ExampleOID_Len() {
raw := `{iso(1) identified-organization(3) dod(6) internet(1) private(4) enterprise(1) 56521 example(999)}`
id, err := NewOID(raw)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("%d", id.Len())
// Output: 8
}
func ExampleOID_ASN() {
raw := `{iso(1) identified-organization(3) dod(6) internet(1) private(4) enterprise(1) 56521 example(999)}`
id, err := NewOID(raw)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("%s", id.ASN())
// Output: {iso(1) identified-organization(3) dod(6) internet(1) private(4) enterprise(1) 56521 example(999)}
}
func ExampleOID_IsZero() {
var z OID
fmt.Printf("Zero: %t", z.IsZero())
// Output: Zero: true
}
func ExampleOID_Valid() {
var o OID
fmt.Printf("Valid: %t", o.Valid())
// Output: Valid: false
}
func ExampleOID_Leaf() {
a := `{joint-iso-itu-t(2) uuid(25) ans(987895962269883002155146617097157934)}`
id, err := NewOID(a)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("Leaf node: %s", id.Leaf())
// Output: Leaf node: ans(987895962269883002155146617097157934)
}
func ExampleOID_Parent() {
a := `{joint-iso-itu-t(2) uuid(25) ans(987895962269883002155146617097157934)}`
id, err := NewOID(a)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("Leaf node parent: %s", id.Parent())
// Output: Leaf node parent: uuid(25)
}
func ExampleOID_Root() {
a := `{joint-iso-itu-t(2) uuid(25) ans(987895962269883002155146617097157934)}`
id, err := NewOID(a)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("Root node: %s", id.Root())
// Output: Root node: joint-iso-itu-t(2)
}
func TestOID_bogus(t *testing.T) {
if _, err := NewOID(testASN1Bogus); err == nil {
t.Errorf("%s successfully parsed bogus value; expected an error", t.Name())
return
}
if _, err := NewOID(`iso(3) identified-organization(3)`); err == nil {
t.Errorf("%s successfully parsed bogus value; expected an error", t.Name())
return
}
if _, err := NewOID(`itu-t recommendation(-3)`); err == nil {
t.Errorf("%s successfully parsed bogus value; expected an error", t.Name())
return
}
if _, err := NewOID(`joint-iso-itu-t thing`); err == nil {
t.Errorf("%s successfully parsed bogus value; expected an error", t.Name())
return
}
if _, err := NewOID([]NameAndNumberForm{
{identifier: `iso`, primaryIdentifier: NumberForm(*big.NewInt(1)), parsed: true},
{identifier: `identified-organization`, primaryIdentifier: NumberForm(*big.NewInt(3)), parsed: true},
}); err != nil {
t.Errorf("%s error: %v", t.Name(), err)
return
}
if _, err := NewOID([]NameAndNumberForm{}); err == nil {
t.Errorf("%s error: %v", t.Name(), err)
return
}
}