This repository has been archived by the owner on Oct 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
oid_test.go
562 lines (489 loc) · 16.1 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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
package aci
import (
"fmt"
"testing"
)
func TestObjectIdentifiers_codecov(t *testing.T) {
_ = isDotNot(``)
_ = isDotNot(`this is not dot not`)
for keyword, Oidsfn := range map[TargetKeyword]func(...any) ObjectIdentifiers{
TargetCtrl: Ctrls,
TargetExtOp: ExtOps,
} {
var Oids ObjectIdentifiers
_ = Oids.Ne()
_ = Oids.Eq()
_ = objectIdentifiersPushPolicy(Oids, ``, keyword)
_ = objectIdentifiersPushPolicy(Oids, nil, keyword)
_ = objectIdentifiersPushPolicy(Oids, float64(1), keyword)
_ = objectIdentifiersPushPolicy(Oids, BindUDN, keyword)
_ = objectIdentifiersPushPolicy(Oids, keyword, keyword)
_ = objectIdentifiersPushPolicy(Oids, `hello`, keyword)
_ = Oids.Len()
Oids.reset()
Oids.resetKeyword(keyword)
Oids.resetKeyword(keyword.String())
Oids.Push(keyword)
_ = Oids.Keyword()
_ = Oids.Kind()
_ = Oids.Push()
_ = Oids.Push(nil)
_ = Oids.Push(``)
_ = Oids.Push('a')
_ = Oids.Ne()
_ = Oids.Eq()
_ = Oids.Valid()
_ = Oids.setQuoteStyle(0)
_ = Oids.setQuoteStyle(1)
Oids.isObjectIdentifierContext()
Oids = Oidsfn() // init
_ = Oids.Len()
_ = objectIdentifiersPushPolicy(Oids, ``, keyword)
_ = objectIdentifiersPushPolicy(Oids, nil, keyword)
_ = objectIdentifiersPushPolicy(Oids, BindUDN, keyword)
_ = objectIdentifiersPushPolicy(Oids, keyword, keyword)
_ = objectIdentifiersPushPolicy(Oids, float64(1), keyword)
_ = objectIdentifiersPushPolicy(Oids, `hello`, keyword)
Oids.reset()
Oids.resetKeyword(keyword)
Oids.resetKeyword(keyword.String())
Oids.Push(keyword)
_ = Oids.Keyword()
_ = Oids.Kind()
_ = Oids.Push()
_ = Oids.Push(nil)
_ = Oids.Push(``)
_ = Oids.Push('a')
_ = Oids.Ne()
_ = Oids.Eq()
_ = Oids.Valid()
_ = Oids.setQuoteStyle(0)
_ = Oids.setQuoteStyle(1)
Oids.isObjectIdentifierContext()
for _, oid := range []string{
`1.3.6.1.4.1.56521.999.83`,
`1.3.6.1.4.1.56521.999.84`,
`1.3.6.1.4.1.56521.999.85`,
`1.3.6.1.4.1.56521.999.86`,
`1.3.6.1.4.1.56521.999.87`,
`1.3.6.1.4.1.56521.999.88`,
`1.3.6.1.4.1.56521.999.89`,
`1.3.6.1.4.1.56521.999.90`,
`1.3.6.1.4.1.56521.999.91`,
} {
var (
Ol int = Oids.Len()
ofn func(...any) ObjectIdentifier = Oids.F()
Oid ObjectIdentifier
octx ObjectIdentifierContext
)
Oid.isObjectIdentifierContext()
_ = Oid.Len()
_ = Oid.Kind()
_ = Oid.Valid()
_ = Oid.Keyword()
if err := testEmptyOidContext(t, keyword, Oid, Oids, Ol); err != nil {
t.Errorf(err.Error())
return
}
// process OID
Oid = ofn(oid)
Oid = ofn(Oid)
badOid := ofn(5465734.3, '𝝅', '🤮', '🤮', '🤮', '🤮', '🤮', '🤮', '🤮', '☜', 'Ҩ', 'ↂ', '⼼', 'ↂ', 'Ҩ', '☞')
_ = badOid.String()
_ = badOid.Valid()
_ = badOid.Eq()
Ol = Oids.Len()
_ = Oid.Len()
_ = Oid.Kind()
_ = Oid.Keyword()
_ = Oid.Ne()
_ = Oid.Eq()
_ = Oid.Valid()
if Oids.Push(Oid); !Oids.Contains(oid) {
t.Errorf("%s [%s] multival failed: valid %T[%s] instance (%s) not pushed into %T[%s; len:%d]",
t.Name(), keyword, Oid, Oid.Keyword(), Oid, Oids, Oids.Keyword(), Ol)
return
}
_ = Oids.Contains('𝝅')
_ = Oids.Contains(43785)
_ = Oids.Contains(nil)
_ = Oids.Contains(``)
popped := Oids.Pop()
Oids.Push(popped)
Oids.Push(popped.String())
Oids.Push(3444444.445)
Oids.Push()
Oids.Push(``) // crashes go-objectid
Oids.Push('𝝅')
_ = Oids.Keyword()
_ = Oids.Kind()
_ = Oids.setQuoteStyle(0)
_ = Oids.setQuoteStyle(1)
for sop, trmn := range []func() TargetRuleMethods{
Oid.TRM,
Oids.TRM,
} {
octx = testMakeOidContext(sop, Oid, Oids)
trm := trmn()
for i := 0; i < trm.Len(); i++ {
cop, meth := trm.Index(i + 1)
if meth == nil {
t.Errorf("%s [%s] multival failed: expected %s method (%T), got nil",
t.Name(), keyword, cop.Context(), meth)
return
}
wcop := sprintf("( %s %s %q )", octx.Keyword(), cop, octx.String())
if T := meth(); T.String() != wcop {
err := unexpectedStringResult(octx.String(), wcop, T.String())
t.Errorf("%s [%s] multival failed [%s rule]: %v",
t.Name(), keyword, octx.Keyword(), err)
return
}
}
}
}
}
}
func testMakeOidContext(phase int, oid ObjectIdentifier, oids ObjectIdentifiers) (octx ObjectIdentifierContext) {
if phase == 0 {
octx = oid
return
}
octx = oids
return
}
func testEmptyOidContext(t *testing.T, kw Keyword, oid ObjectIdentifier, oids ObjectIdentifiers, ol int) (err error) {
_ = oid.Keyword()
_ = oid.Kind()
_ = oid.Valid()
_ = oids.Keyword()
_ = oids.Kind()
_ = oids.Valid()
err = oid.Valid()
if err != nil {
if err.Error() != `aci.ObjectIdentifier instance is nil` {
err = errorf("%s [%s] multival failed: invalid %T returned no validity error",
t.Name(), kw, oid)
} else {
err = nil
}
} else {
err = errorf("%s [%s] multival failed: invalid %T returned no validity error",
t.Name(), kw, oid)
}
if oid.String() != badDotNot {
err = errorf("%s [%s] multival failed: unexpected string result; want '%s', got '%s'",
t.Name(), kw, badDotNot, oid)
}
if oids.Push(oid); oids.Len() > ol {
err = errorf("%s [%s] multival failed (len): invalid %T (%s) pushed into %T without error",
t.Name(), kw, oid, oid, oids)
}
if oids.Contains(oid) {
err = errorf("%s [%s] multival failed (contains): invalid %T instance pushed into %T without error",
t.Name(), kw, oid, oids)
}
return
}
/*
This example demonstrates the creation of a single LDAP Extended Operation [ObjectIdentifier], intended for use in the creation of [TargetExtOp] [TargetRule] expressions.
*/
func ExampleExtOp() {
o := ExtOp(`1.3.6.1.4.1.56521.999.5`)
fmt.Printf("%s", o)
// Output: 1.3.6.1.4.1.56521.999.5
}
/*
This example demonstrates the creation of a single LDAP Control Object Identifier,
intended for use in the creation of targetcontrol TargetRule expressions.
*/
func ExampleCtrl() {
o := Ctrl(`1.3.6.1.4.1.56521.999.5`)
fmt.Printf("%s", o)
// Output: 1.3.6.1.4.1.56521.999.5
}
func ExampleObjectIdentifier_Compare() {
o1 := Ctrl(`1.3.6.1.4.1.56521.999.5`)
o2 := ExtOp(`1.3.6.1.4.1.56521.999.5`)
fmt.Printf("%t", o1.Compare(o2))
// Output: true
}
func ExampleObjectIdentifiers_Compare() {
o1 := Ctrls(`1.3.6.1.4.1.56521.999.5`, `1.3.6.1.4.1.56521.999.6`)
o2 := Ctrls(`1.3.6.1.4.1.56521.999.7`, `1.3.6.1.4.1.56521.999.6`)
fmt.Printf("%t", o1.Compare(o2))
// Output: false
}
/*
This example demonstrates the creation of a multi-valued targetcontrol (LDAP Control) [TargetRule] expression.
*/
func ExampleCtrls() {
// note: these are phony OIDs
o1 := Ctrl(`1.3.6.1.4.1.56521.999.5`)
o2 := Ctrl(`1.3.6.1.4.1.56521.999.7`)
// Initialize the stack (Ctrls) and
// immediately push o1 and o2.
ctrls := Ctrls().Push(o1, o2)
fmt.Printf("%s", ctrls.Eq())
// Output: ( targetcontrol = "1.3.6.1.4.1.56521.999.5 || 1.3.6.1.4.1.56521.999.7" )
}
/*
This example demonstrates the creation of a multi-valued extop (LDAP Extended Operation) [TargetRule] expression.
*/
func ExampleExtOps() {
// note: these are phony OIDs
o1 := ExtOp(`1.3.6.1.4.1.56521.999.5`)
o2 := ExtOp(`1.3.6.1.4.1.56521.999.7`)
// Initialize the stack (Ctrls) and
// immediately push o1 and o2.
exop := ExtOps().Push(o1, o2)
fmt.Printf("%s", exop.Eq())
// Output: ( extop = "1.3.6.1.4.1.56521.999.5 || 1.3.6.1.4.1.56521.999.7" )
}
/*
This example demonstrates the manual creation of an [ObjectIdentifier] instance using the string representation of an ASN.1 Object Identifier in dot notation, and a valid [TargetKeyword] context for a [TargetRule].
*/
func ExampleOID() {
o1 := `1.3.6.1.4.1.56521.999.5` // note: phony OID
o := OID(o1, TargetExtOp)
//o := OID(o1, TargetCtrl) // alternative
fmt.Printf("OID:%s, Type:%s", o, o.Keyword())
// Output: OID:1.3.6.1.4.1.56521.999.5, Type:extop
}
/*
This example demonstrates the creation of a multi-valued extop (LDAP Extended Operation) equality [TargetRule] expression. [ObjectIdentifiers.Push] is used to submit the instances of [ObjectIdentifier] to the stack.
*/
func ExampleObjectIdentifiers_Push() {
// note: these are phony OIDs
o1 := ExtOp(`1.3.6.1.4.1.56521.999.5`)
o2 := ExtOp(`1.3.6.1.4.1.56521.999.7`)
// Initialize the stack (Ctrls) and
// immediately push o1 and o2.
exop := ExtOps().Push(o1, o2)
fmt.Printf("%s", exop.Eq())
// Output: ( extop = "1.3.6.1.4.1.56521.999.5 || 1.3.6.1.4.1.56521.999.7" )
}
/*
This is an example of a LIFO stack slice removal using the [ObjectIdentifiers.Pop] method.
*/
func ExampleObjectIdentifiers_Pop() {
// note: these are phony OIDs
o1 := ExtOp(`1.3.6.1.4.1.56521.999.5`)
o2 := ExtOp(`1.3.6.1.4.1.56521.999.7`)
o3 := ExtOp(`1.3.6.1.4.1.56521.999.9`)
// Initialize the stack (Ctrls) and
// immediately push o1 and o2.
exop := ExtOps().Push(o1, o2, o3)
popped := exop.Pop()
fmt.Printf("Removed %T (%s), stack length now %d", popped, popped, exop.Len())
// Output: Removed aci.ObjectIdentifier (1.3.6.1.4.1.56521.999.9), stack length now 2
}
/*
This example demonstrates the creation of a multi-valued extop (LDAP Extended Operation) equality [TargetRule] expression.
*/
func ExampleObjectIdentifiers_Eq() {
// note: these are phony OIDs
o1 := ExtOp(`1.3.6.1.4.1.56521.999.5`)
o2 := ExtOp(`1.3.6.1.4.1.56521.999.7`)
// Initialize the stack (Ctrls) and
// immediately push o1 and o2.
exop := ExtOps().Push(o1, o2)
fmt.Printf("%s", exop.Eq())
// Output: ( extop = "1.3.6.1.4.1.56521.999.5 || 1.3.6.1.4.1.56521.999.7" )
}
/*
This example demonstrates the creation of a multi-valued extop (LDAP Extended Operation) equality [TargetRule] expression.
*/
func ExampleObjectIdentifier_Eq() {
fmt.Printf("%s", ExtOp(`1.3.6.1.4.1.56521.999.5`).Eq())
// Output: ( extop = "1.3.6.1.4.1.56521.999.5" )
}
/*
This example demonstrates the creation of a multi-valued targetcontrol (LDAP Controls) negated equality [TargetRule] expression.
*/
func ExampleObjectIdentifiers_Ne() {
// note: these are phony OIDs
o1 := Ctrl(`1.3.6.1.4.1.56521.999.5`)
o2 := Ctrl(`1.3.6.1.4.1.56521.999.7`)
// Initialize the stack (Ctrls) and
// immediately push o1 and o2.
ctrls := Ctrls().Push(o1, o2)
fmt.Printf("%s", ctrls.Ne())
// Output: ( targetcontrol != "1.3.6.1.4.1.56521.999.5 || 1.3.6.1.4.1.56521.999.7" )
}
/*
This example demonstrates the creation of a multi-valued extop (LDAP Extended Operation) negated equality [TargetRule] expression.
*/
func ExampleObjectIdentifier_Ne() {
fmt.Printf("%s", ExtOp(`1.3.6.1.4.1.56521.999.5`).Ne())
// Output: ( extop != "1.3.6.1.4.1.56521.999.5" )
}
/*
This example demonstrates the string representation of the receiver.
*/
func ExampleObjectIdentifier_String() {
fmt.Printf("%s", ExtOp(`1.3.6.1.4.1.56521.999.5`))
// Output: 1.3.6.1.4.1.56521.999.5
}
/*
This example demonstrates the (mostly) useless nature of the [ObjectIdentifier.Len] method, which only exists to satisfy Go's interface signature requirements.
*/
func ExampleObjectIdentifier_Len() {
fmt.Printf("%d", ExtOp(`1.3.6.1.4.1.56521.999.5`).Len())
// Output: 1
}
/*
This example demonstrates use of the [ObjectIdentifiers.Len] method to return the number of slices present within the receiver as an integer.
*/
func ExampleObjectIdentifiers_Len() {
ctrls := Ctrls().Push(
// note: these are phony OIDs
Ctrl(`1.3.6.1.4.1.56521.999.5`),
Ctrl(`1.3.6.1.4.1.56521.999.7`),
)
fmt.Printf("%d", ctrls.Len())
// Output: 2
}
/*
This example demonstrates use of the [ObjectIdentifiers.Keyword] method to obtain the current [TargetKeyword] context from the receiver.
*/
func ExampleObjectIdentifiers_Keyword() {
ctrls := Ctrls().Push(
// note: these are phony OIDs
Ctrl(`1.3.6.1.4.1.56521.999.5`),
Ctrl(`1.3.6.1.4.1.56521.999.7`),
)
fmt.Printf("%s", ctrls.Keyword())
// Output: targetcontrol
}
/*
This example demonstrates use of the [ObjectIdentifiers.Kind] method to obtain the string form of the current [ObjectIdentifiers.Keyword] context from the receiver.
*/
func ExampleObjectIdentifiers_Kind() {
ctrls := Ctrls().Push(
// note: these are phony OIDs
Ctrl(`1.3.6.1.4.1.56521.999.5`),
Ctrl(`1.3.6.1.4.1.56521.999.7`),
)
fmt.Printf("%s", ctrls.Kind())
// Output: targetcontrol
}
/*
This example demonstrates use of the [ObjectIdentifier.Keyword] method to obtain the current [TargetKeyword] context from the receiver.
*/
func ExampleObjectIdentifier_Keyword() {
fmt.Printf("%s", ExtOp(`1.3.6.1.4.1.56521.999.5`).Keyword())
// Output: extop
}
/*
This example demonstrates use of the [ObjectIdentifier.Kind] method to obtain the string form of the current [TargetKeyword] context from the receiver.
*/
func ExampleObjectIdentifier_Kind() {
fmt.Printf("%s", ExtOp(`1.3.6.1.4.1.56521.999.5`).Kind())
// Output: extop
}
/*
This example demonstrates the string representation of the receiver.
*/
func ExampleObjectIdentifiers_String() {
// Initialize the stack (Ctrls) and
// immediately push into it.
ctrls := Ctrls().Push(
// note: these are phony OIDs
Ctrl(`1.3.6.1.4.1.56521.999.5`),
Ctrl(`1.3.6.1.4.1.56521.999.7`),
)
fmt.Printf("%s", ctrls)
// Output: 1.3.6.1.4.1.56521.999.5 || 1.3.6.1.4.1.56521.999.7
}
/*
This example demonstrates the use of the [ObjectIdentifier.IsZero] method upon a nil receiver.
*/
func ExampleObjectIdentifier_IsZero() {
var oid ObjectIdentifier
fmt.Printf("Zero: %t", oid.IsZero())
// Output: Zero: true
}
/*
This example demonstrates the use of the [ObjectIdentifiers.IsZero] method upon a nil receiver.
*/
func ExampleObjectIdentifiers_IsZero() {
var oids ObjectIdentifiers
fmt.Printf("Zero: %t", oids.IsZero())
// Output: Zero: true
}
/*
This example demonstrates the use of the [ObjectIdentifiers.ID] method.
*/
func ExampleObjectIdentifiers_ID() {
var oids ObjectIdentifiers = ExtOps() // must be initialized, as there are two (2) types of OIDs here
fmt.Printf("ID: %s", oids.ID())
// Output: ID: target
}
/*
This example demonstrates the use of the [ObjectIdentifier.Valid] method upon a nil receiver.
*/
func ExampleObjectIdentifier_Valid() {
var oid ObjectIdentifier
fmt.Printf("Valid: %t", oid.Valid() == nil)
// Output: Valid: false
}
/*
This example demonstrates the use of the [ObjectIdentifier.Valid] method upon a nil receiver.
*/
func ExampleObjectIdentifiers_Valid() {
var oids ObjectIdentifiers
fmt.Printf("Valid: %t", oids.Valid() == nil)
// Output: Valid: false
}
/*
This example demonstrates the population of an object identifier stack and a subsequent presence check of one of its members.
*/
func ExampleObjectIdentifiers_Contains() {
ctrls := Ctrls(
`1.3.6.1.4.1.56521.999.5`,
`1.3.6.1.4.1.56521.999.6`,
`1.3.6.1.4.1.56521.999.7`,
)
fmt.Printf("Contains OID: %t", ctrls.Contains(`1.3.6.1.4.1.56521.999.5`))
// Output: Contains OID: true
}
/*
This example demonstrates the use of the Index method to obtain a single slice and print its [ObjectIdentifier.Keyword] value.
*/
func ExampleObjectIdentifiers_Index() {
ctrls := Ctrls(
`1.3.6.1.4.1.56521.999.5`,
`1.3.6.1.4.1.56521.999.6`,
`1.3.6.1.4.1.56521.999.7`,
)
fmt.Printf("Slice keyword: %s", ctrls.Index(1).Keyword())
// Output: Slice keyword: targetcontrol
}
/*
This example demonstrates the use of the [ObjectIdentifiers.TRM] method to obtain a list of available [ComparisonOperator] identifiers and methods, and a subsequent call of [TargetRuleMethods.Contains] to determine whether Greater Than (Gt) is among them.
*/
func ExampleObjectIdentifiers_TRM() {
var oids ObjectIdentifiers
fmt.Printf("Allows greater-than: %t", oids.TRM().Contains(Gt))
// Output: Allows greater-than: false
}
/*
This example demonstrates the use of the [ObjectIdentifiers.F] method to obtain the appropriate package level function to be used to craft additional slices for push into the receiver.
*/
func ExampleObjectIdentifiers_F() {
var oids ObjectIdentifiers = Ctrls()
funk := oids.F()
newval := funk(`1.3.6.1.4.1.56521.999.5`)
fmt.Printf("New %T: %s", newval, newval)
// Output: New aci.ObjectIdentifier: 1.3.6.1.4.1.56521.999.5
}
/*
This example demonstrates the use of the [ObjectIdentifier.TRM] method to obtain a list of available [ComparisonOperator] identifiers and methods, and a subsequent call of [TargetRuleMethods.Contains] to determine whether Greater Than (Gt) is among them.
*/
func ExampleObjectIdentifier_TRM() {
var oid ObjectIdentifier
fmt.Printf("Allows greater-than: %t", oid.TRM().Contains(Gt))
// Output: Allows greater-than: false
}