forked from JesseCoretta/go-objectid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
doc_test.go
193 lines (165 loc) · 4.67 KB
/
doc_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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package objectid
import (
"fmt"
)
func ExampleNewNumberForm() {
// single UUID integer parse example
arc, err := NewNumberForm(`987895962269883002155146617097157934`)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("%s", arc)
// Output: 987895962269883002155146617097157934
}
func ExampleNewOID() {
// X.667 example
a := `{joint-iso-itu-t(2) uuid(25) ans(987895962269883002155146617097157934)}`
id, err := NewOID(a)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("ASN.1 Notation: %s", id.ASN())
// Output: ASN.1 Notation: {joint-iso-itu-t(2) uuid(25) ans(987895962269883002155146617097157934)}
}
func ExampleNewDotNotation() {
a := `2.25.987895962269883002155146617097157934`
id, err := NewDotNotation(a)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("dotNotation: %s", id)
// Output: dotNotation: 2.25.987895962269883002155146617097157934
}
func ExampleDotNotation_Leaf() {
a := `2.25.987895962269883002155146617097157934`
id, err := NewDotNotation(a)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("Leaf node: %s", id.Leaf())
// Output: Leaf node: 987895962269883002155146617097157934
}
func ExampleDotNotation_Parent() {
a := `2.25.987895962269883002155146617097157934`
id, err := NewDotNotation(a)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("Leaf node parent: %s", id.Parent())
// Output: Leaf node parent: 25
}
func ExampleDotNotation_Root() {
a := `2.25.987895962269883002155146617097157934`
id, err := NewDotNotation(a)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("Root node: %s", id.Root())
// Output: Root node: 2
}
func ExampleNewASN1Notation() {
a := `{iso identified-organization(3) dod(6)}`
id, err := NewASN1Notation(a)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("Root node: %s", id.Root())
// Output: Root node: iso(1)
}
func ExampleDotNotation_IntSlice() {
a := `1.3.6.1.4.1.56521.999.5`
dot, _ := NewDotNotation(a)
// If needed, slice instance can be
// cast as asn1.ObjectIdentifier.
slice, err := dot.IntSlice()
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("%v", slice)
// Output: [1 3 6 1 4 1 56521 999 5]
}
func ExampleDotNotation_Uint64Slice() {
a := `1.3.6.1.4.1.56521.9999999999999999999.5` // overflows int, but not uint64
dot, _ := NewDotNotation(a)
// If needed, slice instance can be
// fed to crypto/x509.OIDFromInts
slice, err := dot.Uint64Slice()
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("%v", slice)
// Output: [1 3 6 1 4 1 56521 9999999999999999999 5]
}
func ExampleDotNotation_IntSlice_overflow() {
a := `2.25.987895962269883002155146617097157934`
dot, _ := NewDotNotation(a)
if _, err := dot.IntSlice(); err != nil {
fmt.Println(err)
return
}
// Output: strconv.Atoi: parsing "987895962269883002155146617097157934": value out of range
}
func ExampleDotNotation_Uint64Slice_overflow() {
a := `2.25.987895962269883002155146617097157934`
dot, _ := NewDotNotation(a)
if _, err := dot.Uint64Slice(); err != nil {
fmt.Println(err)
return
}
// Output: strconv.ParseUint: parsing "987895962269883002155146617097157934": value out of range
}
func ExampleDotNotation_Ancestry() {
dot, err := NewDotNotation(`1.3.6.1.4.1.56521`)
if err != nil {
fmt.Println(err)
}
anc := dot.Ancestry()
fmt.Printf("%s", anc[len(anc)-2])
// Output: 1.3
}
func ExampleASN1Notation_Ancestry() {
asn, err := NewASN1Notation(`{iso(1) identified-organization(3) dod(6) internet(1) private(4) enterprise(1) 56521 example(999)}`)
if err != nil {
fmt.Println(err)
}
anc := asn.Ancestry()
fmt.Printf("%s", anc[len(anc)-2])
// Output: {iso(1) identified-organization(3)}
}
func ExampleASN1Notation_NewSubordinate() {
asn, err := NewASN1Notation(`{iso(1) identified-organization(3) dod(6) internet(1) private(4) enterprise(1) 56521 example(999)}`)
if err != nil {
fmt.Println(err)
}
fmt.Printf("%s", asn.NewSubordinate(`friedChicken(5)`))
// Output: {iso(1) identified-organization(3) dod(6) internet(1) private(4) enterprise(1) 56521 example(999) friedChicken(5)}
}
func ExampleDotNotation_NewSubordinate() {
dot, err := NewDotNotation(`1.3.6.1.4.1.56521.999`)
if err != nil {
fmt.Println(err)
}
fmt.Printf("%s", dot.NewSubordinate(5))
// Output: 1.3.6.1.4.1.56521.999.5
}
func ExampleASN1Notation_AncestorOf() {
asn, _ := NewASN1Notation(`{iso(1) identified-organization(3) dod(6) internet(1)}`)
child, _ := NewASN1Notation(`{iso(1) identified-organization(3) dod(6) internet(1) private(4) enterprise(1)}`)
fmt.Printf("%t", asn.AncestorOf(child))
// Output: true
}
func ExampleDotNotation_AncestorOf() {
dot, _ := NewDotNotation(`1.3.6`)
child, _ := NewDotNotation(`2.1.0.1`)
fmt.Printf("%t", dot.AncestorOf(child))
// Output: false
}