forked from JesseCoretta/go-schemax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
at_test.go
1100 lines (920 loc) · 31.9 KB
/
at_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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package schemax
import (
"fmt"
"regexp"
"strings"
"testing"
"time"
"unicode/utf8"
)
/*
This example demonstrates the means of gathering references to every
superior [AttributeType] in the relevant super type chain.
Note: this example assumes a legitimate schema variable is defined
in place of the fictional "mySchema" var shown here for simplicity.
*/
func ExampleAttributeType_SuperChain() {
cn := mySchema.AttributeTypes().Get(`nisMapName`)
chain := cn.SuperChain()
for i := 0; i < chain.Len(); i++ {
fmt.Println(chain.Index(i).OID())
}
// Output: name
}
/*
This example demonstrates the means of accessing all subordinate type
instances of the receiver instance.
In essence, this method is the opposite of the [AttributeType.SuperType]
method and may return zero (0) or more [AttributeType] instances within
the return [AttributeTypes] instance.
*/
func ExampleAttributeType_SubTypes() {
def := mySchema.AttributeTypes().Get(`name`)
fmt.Printf("%d subordinate types found", def.SubTypes().Len())
// Output: 15 subordinate types found
}
/*
This example demonstrates a compliancy check of the "name" [AttributeType].
Note: this example assumes a legitimate schema variable is defined
in place of the fictional "mySchema" var shown here for simplicity.
*/
func ExampleAttributeType_Compliant() {
name := mySchema.AttributeTypes().Get(`name`)
fmt.Println(name.Compliant())
// Output: true
}
/*
This example demonstrates a compliancy check of the "name" [AttributeType].
Note: this example assumes a legitimate schema variable is defined
in place of the fictional "mySchema" var shown here for simplicity.
*/
func ExampleAttributeTypes_Compliant() {
attrs := mySchema.AttributeTypes()
fmt.Println(attrs.Compliant())
// Output: true
}
/*
This example demonstrates determining the USAGE of an [AttributeType].
Note: this example assumes a legitimate schema variable is defined
in place of the fictional "mySchema" var shown here for simplicity.
*/
func ExampleAttributeType_Usage() {
ctime := mySchema.AttributeTypes().Get(`createTimestamp`)
fmt.Println(ctime.Usage())
// Output: directoryOperation
}
/*
This example demonstrates the means of walking the super type chain to
determine the effective [LDAPSyntax] instance held by an [AttributeType]
instance, whether direct or indirect.
Note: this example assumes a legitimate schema variable is defined
in place of the fictional "mySchema" var shown here for simplicity.
*/
func ExampleAttributeType_EffectiveSyntax() {
at := mySchema.AttributeTypes().Get(`roleOccupant`)
fmt.Println(at.EffectiveSyntax().Description())
// Output: DN
}
/*
This example demonstrates the means of walking the super type chain to
determine the effective EQUALITY [MatchingRule] instance held by an
[AttributeType] instance, whether direct or indirect.
*/
func ExampleAttributeType_EffectiveEquality() {
at := mySchema.AttributeTypes().Get(`registeredAddress`)
fmt.Println(at.EffectiveEquality().OID())
// Output: caseIgnoreListMatch
}
/*
This example demonstrates the means of walking the super type chain
to determine the effective SUBSTR [MatchingRule] instance held by an
[AttributeType] instance, whether direct or indirect.
*/
func ExampleAttributeType_EffectiveSubstring() {
at := mySchema.AttributeTypes().Get(`registeredAddress`)
fmt.Println(at.EffectiveSubstring().OID())
// Output: caseIgnoreListSubstringsMatch
}
/*
This example demonstrates the means of walking the super type chain
to determine the effective ORDERING [MatchingRule] instance held by
an [AttributeType] instance, whether direct or indirect.
*/
func ExampleAttributeType_EffectiveOrdering() {
at := mySchema.AttributeTypes().Get(`createTimestamp`)
fmt.Println(at.EffectiveOrdering().OID())
// Output: generalizedTimeOrderingMatch
}
/*
This example demonstrates a conventional means of checking a given
value under the terms of a specific [AttributeType]'s assigned
[ValueQualifier].
Naturally this example is overly simplified, with support extended
for nil value states purely for educational purposes only. A real
life implementation would likely be more stringent.
Note: this example assumes a legitimate schema variable is defined
in place of the fictional "mySchema" var shown here for simplicity.
*/
func ExampleAttributeType_QualifyValue_withSet() {
// Let's use "hasSubordinates" due to its common
// use within multiple popular directory products.
hS := mySchema.AttributeTypes().Get(`hasSubordinates`)
if hS.IsZero() {
fmt.Println("hasSubordinates not found!")
return
}
// hasSubordinates is a BOOLEAN type, so let's make
// a very "forgiving" handler for such values, both
// for string and (Go) bool/*bool values.
hS.SetValueQualifier(func(x any) error {
var err error
switch tv := x.(type) {
case string:
switch strings.ToLower(tv) {
case `true`, `false`, `undefined`, ``:
// OK: Match all valid string values in one shot
// in a manner compliant with the caseIgnoreMatch
// equality matching rule.
default:
// BAD: No other string value is applicable here.
err = ErrInvalidSyntax
}
case bool, *bool, nil:
// OK: Guaranteed to be valid, with a nil instance
// equivalent to the LDAP "Undefined" BOOLEAN state.
default:
// BAD: no other type is applicable here.
err = ErrInvalidType
}
return err
})
// Let's subject our newly-assigned SyntaxQualifier to
// a series of valid values of supported types.
for _, possibleValue := range []any{
`True`,
false,
`False`,
true,
`FALSE`,
`fALse`,
``,
`undefineD`,
nil,
} {
// None of these should return errors.
if err := hS.QualifyValue(possibleValue); err != nil {
fmt.Println(err)
return
}
}
// Let's pass a known bogus value just to
// make sure this thing is indeed working.
err := hS.QualifyValue(`falsch`) // Entschuldigung, kein deutscher support :(
fmt.Println(err)
// Output: Value does not meet the prescribed syntax qualifications
}
/*
This example demonstrates a means of parsing a raw definition into a new
instance of [AttributeType].
Note: this example assumes a legitimate schema variable is defined in place
of the fictional "mySchema" var shown here for simplicity.
*/
func ExampleAttributeType_Parse() {
nattr := mySchema.NewAttributeType()
// feed the parser a subtly bogus definition ...
err := nattr.Parse(`( 1.3.6.1.4.1.56521.999.14.56.1
NAME 'fakeAttribute'
DESC 'It\'s not real'
SINGLE-VALUE
COLLECTIVE
X-ORIGIN 'YOUR FACE'
)`)
fmt.Println(err)
// Output: AttributeType is both single-valued and collective; aborting (1.3.6.1.4.1.56521.999.14.56.1)
}
/*
This example demonstrates the preferred means of initializing a new instance
of [AttributeType]. This strategy will automatically associate the receiver
instance of [Schema] with the return value.
Note: this example assumes a legitimate schema variable is defined in place
of the fictional "mySchema" var shown here for simplicity.
*/
func ExampleSchema_NewAttributeType() {
nattr := mySchema.NewAttributeType()
nattr.SetNumericOID(`1.3.6.1.4.1.56521.999.14.56.1`)
fmt.Println(nattr.NumericOID())
// Output: 1.3.6.1.4.1.56521.999.14.56.1
}
/*
This example demonstrates an alternative to [Schema.NewAttributeType].
The return value must be manually configured and must also be manually
associated with the relevant [Schema] instance. Use of this function
is only meaningful when dealing with multiple [Schema] instances.
Note: this example assumes a legitimate schema variable is defined
in place of the fictional "mySchema" var shown here for simplicity.
*/
func ExampleNewAttributeType() {
// lookup and get the Directory String syntax
dStr := mySchema.LDAPSyntaxes().Get(`1.3.6.1.4.1.1466.115.121.1.15`)
if dStr.IsZero() {
return
}
// lookup and get the caseIgnoreMatch equality matching rule
cIM := mySchema.MatchingRules().Get(`caseIgnoreMatch`)
if cIM.IsZero() {
return
}
// prepare new var instance
var def AttributeType = NewAttributeType()
// set values in fluent form
def.SetSchema(mySchema).
SetNumericOID(`1.3.6.1.4.1.56521.999.5`).
SetName(`cb`).
SetDescription(`Celestial Body`).
SetMinimumUpperBounds(64).
SetSyntax(dStr).
SetEquality(cIM).
SetSingleValue(true).
SetExtension(`X-ORIGIN`, `NOWHERE`).
SetStringer() // use default stringer
fmt.Printf("%s", def)
// Output: ( 1.3.6.1.4.1.56521.999.5
// NAME 'cb'
// DESC 'Celestial Body'
// EQUALITY caseIgnoreMatch
// SYNTAX 1.3.6.1.4.1.1466.115.121.1.15{64}
// SINGLE-VALUE
// X-ORIGIN 'NOWHERE' )
}
/*
This example demonstrates the replacement process of an [AttributeType]
instance within an instance of [AttributeTypes].
For reasons of oversight, we've added a custom extension X-WARNING to
remind users and admin alike of the modification.
Note: this example assumes a legitimate schema variable is defined
in place of the fictional "mySchema" var shown here for simplicity.
*/
func ExampleAttributeType_Replace() {
// Lets create a new attributeType: coolattr
attr := mySchema.NewAttributeType()
goodraw := `( 1.3.6.1.4.1.56521.999.14.56.1 NAME 'coolattr' SUP cn )`
if err := attr.Parse(goodraw); err != nil {
fmt.Println(err)
return
}
// Parsing says its valid, so let's push this
// new type into our official type stack.
mySchema.AttributeTypes().Push(attr)
// Oh no! We realized we used the wrong supertype.
// We wanted name, not cn :(
// Retrieve the type
attr = mySchema.AttributeTypes().Get(`coolattr`)
// Craft a near identical type instance, changing
// the supertype to name. Also, for good measure,
// lets make a note of this modification using
// an "X-WARNING" extension...
nattr := mySchema.NewAttributeType().
SetName(attr.Name()).
SetNumericOID(attr.NumericOID()).
SetSuperType(`name`).
SetExtension(`X-WARNING`, `MODIFIED`). // optional
SetStringer()
// Replace attr with nattr, while preserving its pointer
// address so that references within stacks do not fail.
attr.Replace(nattr)
// call the new one (just to be sure)
fmt.Println(mySchema.AttributeTypes().Get(`coolattr`))
// Output: ( 1.3.6.1.4.1.56521.999.14.56.1
// NAME 'coolattr'
// SUP name
// X-WARNING 'MODIFIED' )
}
/*
This example demonstrates the assignment of arbitrary data to an instance
of [AttributeType].
Note: this example assumes a legitimate schema variable is defined
in place of the fictional "mySchema" var shown here for simplicity.
*/
func ExampleAttributeType_SetData() {
// The value can be any type, but we'll
// use a string for simplicity.
documentation := `Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.`
// Obtain the target attribute type to bear
// the assigned value.
drink := mySchema.AttributeTypes().Get(`drink`)
// Set it.
drink.SetData(documentation)
// Get it and compare to the original.
equal := documentation == drink.Data().(string)
fmt.Printf("Values are equal: %t", equal)
// Output: Values are equal: true
}
func ExampleAttributeType_QualifySyntax() {
// Obtain the syntax of interest
dstr := mySchema.LDAPSyntaxes().Get(`directoryString`)
// Assign a new syntax qualifier to our syntax
// to perform a naïve assessment of x in order
// to determine whether it is UTF8.
dstr.SetSyntaxQualifier(func(x any) (err error) {
// Type assert, allowing string or
// byte values to be processed.
switch tv := x.(type) {
case string:
if !utf8.ValidString(tv) {
err = ErrInvalidSyntax
}
case []byte:
if !utf8.ValidString(string(tv)) {
err = ErrInvalidSyntax
}
default:
err = ErrInvalidType
}
return
})
// Check an attribute that is known to use the above syntax
cn := mySchema.AttributeTypes().Get(`2.5.4.3`) // or "cn"
// Test a value against the qualifier function
ok := cn.QualifySyntax(`Coolie McLoach`) == nil
fmt.Printf("Syntax ok: %t", ok)
// Output: Syntax ok: true
}
/*
This example demonstrates the means of performing a substring match
assertion between two values by way of an [AssertionMatcher] closure
assigned to the relevant [MatchingRule] instance in use by one or
more [AttributeType] instances.
For this example, we'll use the [regexp] package for brevity.
Note: this example assumes a legitimate schema variable is defined
in place of the fictional "mySchema" var shown here for simplicity.
*/
func ExampleAttributeType_SubstringAssertion() {
// Obtain the syntax of interest
cism := mySchema.MatchingRules().Get(`caseIgnoreSubstringsMatch`)
// Assign a new assertion matcher to our matching rule to
// allow caseless substring matching between two values.
cism.SetAssertionMatcher(func(val, substr any) (err error) {
// Type assert x, allowing string or
// byte values to be processed.
var value, substring string
switch tv := val.(type) {
case string:
value = tv
case []byte:
value = string(tv)
default:
err = ErrInvalidType
return
}
// Now type assert y similarly.
switch tv := substr.(type) {
case string:
substring = tv
case []byte:
substring = string(tv)
default:
err = ErrInvalidType
return
}
// create expression, altering wildcards
// to conform to regexp.
pat := strings.ReplaceAll(substring, "*", ".*")
// Compile the expression.
re, _ := regexp.Compile("(?i)" + pat)
if match := re.MatchString(value); !match {
err = ErrNoMatch
}
return
})
// Check an attribute that is known to use the above
// matching rule.
cn := mySchema.AttributeTypes().Get(`2.5.4.3`) // or "cn"
// Compare two values via the SubstringAssertion method.
// In the context of an assertion check via LDAP, the
// first value (Kenny) could represent a value within
// the database being compared, while the second value
// (k*NN*) is the substring statement input by the user,
// ostensibly within an LDAP Search Filter.
ok := cn.SubstringAssertion(`Kenny`, `k*NN*`) == nil
fmt.Printf("Values match: %t", ok)
// Output: Values match: true
}
/*
This example demonstrates the means of performing an ordering match
assertion between two values by way of an [AssertionMatcher] closure
assigned to the relevant [MatchingRule] instance in use by one or
more [AttributeType] instances.
For this example, we'll be comparing two string-based timestamps in
"YYYYMMDDHHmmss" timestamp format. The values are marshaled into
proper [time.Time] instances and then compared ordering-wise.
The first input value is the higher order value, while the second value
is the lower order value. A comparison error returned indicates that the
first value is NOT greater or equal to the second.
Note: this example assumes a legitimate schema variable is defined
in place of the fictional "mySchema" var shown here for simplicity.
*/
func ExampleAttributeType_OrderingAssertion() {
// Obtain the syntax of interest
cism := mySchema.MatchingRules().Get(`generalizedTimeOrderingMatch`)
// Assign a new assertion matcher to our matching rule to allow
// time ordering matching (e.g.: is timeA later than timeB).
cism.SetAssertionMatcher(func(after, before any) (err error) {
// Type assert x, allowing string or
// byte values to be processed.
var A, B string
switch tv := after.(type) {
case string:
A = tv
case []byte:
A = string(tv)
default:
err = ErrInvalidType
return
}
// Now type assert y similarly.
switch tv := before.(type) {
case string:
B = tv
case []byte:
B = string(tv)
default:
err = ErrInvalidType
return
}
format := `20060102150405`
After, _ := time.Parse(format, A)
Before, _ := time.Parse(format, B)
if !(After.After(Before) || (After.Equal(Before))) {
err = ErrNoMatch
}
return
})
// Check an attribute that is known to use the above matching rule.
modTime := mySchema.AttributeTypes().Get(`modifyTimestamp`)
// Compare two values via the SubstringAssertion method.
// In the context of an assertion check via LDAP, the
// first value (Kenny) could represent a value within
// the database being compared, while the second value
// (k*NN*) is the substring statement input by the user,
// ostensibly within an LDAP Search Filter.
timeA := `20150107145309`
timeB := `20090417110844`
ok := modTime.OrderingAssertion(timeA, timeB) == nil
fmt.Printf("Values match: %t", ok)
// Output: Values match: true
}
/*
This example demonstrates the means of performing an equality match
assertion between two values by way of an [AssertionMatcher] closure
assigned to the relevant [MatchingRule] instance in use by one or
more [AttributeType] instances.
Note: this example assumes a legitimate schema variable is defined
in place of the fictional "mySchema" var shown here for simplicity.
*/
func ExampleAttributeType_EqualityAssertion() {
// Obtain the syntax of interest
cim := mySchema.MatchingRules().Get(`caseIgnoreMatch`)
// Assign a new assertion matcher to our matching rule to
// allow caseless equality matching between two values.
cim.SetAssertionMatcher(func(x, y any) (err error) {
// Type assert x, allowing string or
// byte values to be processed.
var X, Y string
switch tv := x.(type) {
case string:
X = tv
case []byte:
X = string(tv)
default:
err = ErrInvalidType
return
}
// Now type assert y similarly.
switch tv := y.(type) {
case string:
Y = tv
case []byte:
Y = string(tv)
default:
err = ErrInvalidType
return
}
if !strings.EqualFold(X, Y) {
err = ErrNoMatch
}
return
})
// Check an attribute that is known to use the above
// matching rule.
cn := mySchema.AttributeTypes().Get(`2.5.4.3`) // or "cn"
// Compare two values via the EqualityAssertion method.
ok := cn.EqualityAssertion(`kenny`, `Kenny`) == nil
fmt.Printf("Values match: %t", ok)
// Output: Values match: true
}
/*
This example demonstrates the creation of an [Inventory] instance based
upon the current contents of an [AttributeTypes] stack instance. Use
of an [Inventory] instance is convenient in cases where a receiver of
schema information may not be able to directly receive working stack
instances and requires a more portable and generalized type.
Note: this example assumes a legitimate schema variable is defined
in place of the fictional "mySchema" var shown here for simplicity.
*/
func ExampleAttributeTypes_Inventory() {
at := mySchema.AttributeTypes().Inventory()
fmt.Println(at[`2.5.4.3`][0])
// Output: cn
}
func ExampleAttributeTypes_Type() {
at := mySchema.AttributeTypes()
fmt.Println(at.Type())
// Output: attributeTypes
}
func ExampleAttributeType_Type() {
var def AttributeType
fmt.Println(def.Type())
// Output: attributeType
}
/*
This example demonstrates the means of transferring an [AttributeType]
into an instance of [DefinitionMap].
Note: this example assumes a legitimate schema variable is defined
in place of the fictional "mySchema" var shown here for simplicity.
*/
func ExampleAttributeType_Map() {
def := mySchema.AttributeTypes().Get(`cn`)
fmt.Println(def.Map()[`NUMERICOID`][0]) // risky, just for simplicity
// Output: 2.5.4.3
}
/*
This example demonstrates use of the [AttributeTypes.Maps] method, which
produces slices of [DefinitionMap] instances containing [AttributeType]
derived values
Here, we (quite recklessly) call index three (3) and reference index zero
(0) of its `SYNTAX` key to obtain the relevant [LDAPSyntax] OID string value.
Note: this example assumes a legitimate schema variable is defined
in place of the fictional "mySchema" var shown here for simplicity.
*/
func ExampleAttributeTypes_Maps() {
defs := mySchema.AttributeTypes().Maps()
fmt.Println(defs[3][`SYNTAX`][0]) // risky, just for simplicity
// Output: 1.3.6.1.4.1.1466.115.121.1.24
}
/*
This example demonstrates a means of checking whether a particular instance
of [AttributeType] is present within an instance of [AttributeTypes].
Note: this example assumes a legitimate schema variable is defined
in place of the fictional "mySchema" var shown here for simplicity.
*/
func ExampleAttributeTypes_Contains() {
attrs := mySchema.AttributeTypes()
fmt.Println(attrs.Contains(`cn`)) // or "2.5.4.3"
// Output: true
}
/*
This example demonstrates a means of determining whether an [AttributeType]
instance is known by the numeric OID or descriptor input.
Note: this example assumes a legitimate schema variable is defined
in place of the fictional "mySchema" var shown here for simplicity.
*/
func ExampleAttributeType_IsIdentifiedAs() {
surprise := mySchema.AttributeTypes().Get(`0.9.2342.19200300.100.1.5`)
knownBy := surprise.IsIdentifiedAs(`drink`)
fmt.Printf("Definition is known by 'drink': %t", knownBy)
// Output: Definition is known by 'drink': true
}
func ExampleAttributeType_NoUserModification() {
modTime := mySchema.AttributeTypes().Get(`modifyTimestamp`)
fmt.Printf("Definition is immutable: %t", modTime.NoUserModification())
// Output: Definition is immutable: true
}
func ExampleAttributeType_Obsolete() {
modTime := mySchema.AttributeTypes().Get(`modifyTimestamp`)
fmt.Printf("Definition is obsolete: %t", modTime.Obsolete())
// Output: Definition is obsolete: false
}
func ExampleAttributeType_Names() {
cn := mySchema.AttributeTypes().Get(`2.5.4.3`)
fmt.Println(cn.Names())
// Output: ( 'cn' 'commonName' )
}
/*
This example demonstrates a means of accessing the underlying [Extensions]
stack instance within the receiver instance.
Note: this example assumes a legitimate schema variable is defined
in place of the fictional "mySchema" var shown here for simplicity.
*/
func ExampleAttributeType_Extensions() {
cn := mySchema.AttributeTypes().Get(`cn`)
fmt.Println(cn.Extensions())
// Output: X-ORIGIN 'RFC4519'
}
func ExampleAttributeType_Description() {
cn := mySchema.AttributeTypes().Get(`cn`)
fmt.Println(cn.Description())
// Output: RFC4519: common name(s) for which the entity is known by
}
/*
This example demonstrates use of the [AttributeType.SetStringer] method
to impose a custom [Stringer] closure over the default instance.
Naturally the end-user would opt for a more useful stringer, such as one
that produces singular CSV rows per instance.
To avoid impacting other unit tests, we reset the default stringer
via the [AttributeType.SetStringer] method again, with no arguments.
Note: this example assumes a legitimate schema variable is defined
in place of the fictional "mySchema" var shown here for simplicity.
*/
func ExampleAttributeType_SetStringer() {
cn := mySchema.AttributeTypes().Get(`cn`)
cn.SetStringer(func() string {
return "This useless message brought to you by a dumb stringer"
})
msg := fmt.Sprintln(cn)
cn.SetStringer() // return it to its previous state if need be ...
fmt.Printf("Original: %s\nOld: %s", cn, msg)
// Output: Original: ( 2.5.4.3
// NAME ( 'cn' 'commonName' )
// DESC 'RFC4519: common name(s) for which the entity is known by'
// SUP name
// X-ORIGIN 'RFC4519' )
// Old: This useless message brought to you by a dumb stringer
}
/*
This example demonstrates use of the [AttributeTypes.SetStringer] method
to impose a custom [Stringer] closure upon all stack members.
Naturally the end-user would opt for a more useful stringer, such as one
that produces a CSV file containing all [AttributeType] instances.
To avoid impacting other unit tests, we reset the default stringer
via the [AttributeTypes.SetStringer] method again, with no arguments.
Note: this example assumes a legitimate schema variable is defined
in place of the fictional "mySchema" var shown here for simplicity.
*/
func ExampleAttributeTypes_SetStringer() {
attrs := mySchema.AttributeTypes()
attrs.SetStringer(func() string {
return "" // make a null stringer
})
output := attrs.String()
attrs.SetStringer() // return to default
fmt.Println(output)
// Output:
}
/*
This example demonstrates the assignment of a minimum upper bounds value,
meant to declare the maximum limit for a value of this [AttributeType].
Note: this example assumes a legitimate schema variable is defined
in place of the fictional "mySchema" var shown here for simplicity.
*/
func ExampleAttributeType_SetMinimumUpperBounds() {
// First we'll craft a fake attribute
raw := `( 1.3.6.1.4.1.56521.999.14.56.1
NAME 'coolattr'
SYNTAX 1.3.6.1.4.1.1466.115.121.1.26 )`
var attr AttributeType = mySchema.NewAttributeType()
if err := attr.Parse(raw); err != nil {
fmt.Println(err)
return
}
// Oh no! We forgot to specify the min. upper bounds!
// No worries, it can be done after the fact:
attr.SetMinimumUpperBounds(128)
fmt.Println(attr.MinimumUpperBounds())
// Output: 128
}
/*
This example demonstrates the assignment of an [LDAPSyntax] instance to
an [AttributeType] instance during assembly.
Note: this example assumes a legitimate schema variable is defined
in place of the fictional "mySchema" var shown here for simplicity.
*/
func ExampleAttributeType_SetSyntax() {
// First we'll craft a fake attribute
raw := `( 1.3.6.1.4.1.56521.999.14.56.1
NAME 'coolattr'
EQUALITY caseIgnoreMatch )`
var attr AttributeType = mySchema.NewAttributeType()
if err := attr.Parse(raw); err != nil {
fmt.Println(err)
return
}
// Oh no! We forgot to specify the desired syntax!
// No worries, it can be done after the fact:
attr.SetSyntax(`1.3.6.1.4.1.1466.115.121.1.26`)
fmt.Println(attr.Syntax().Description())
// Output: IA5 String
}
/*
This example demonstrates the assignment of an EQUALITY [MatchingRule]
instance to an [AttributeType] instance during assembly.
Note: this example assumes a legitimate schema variable is defined
in place of the fictional "mySchema" var shown here for simplicity.
*/
func ExampleAttributeType_SetEquality() {
// First we'll craft a fake attribute
raw := `( 1.3.6.1.4.1.56521.999.14.56.1
NAME 'coolattr'
SYNTAX 1.3.6.1.4.1.1466.115.121.1.26 )`
var attr AttributeType = mySchema.NewAttributeType()
if err := attr.Parse(raw); err != nil {
fmt.Println(err)
return
}
// Oh no! We forgot to specify the desired equality matching
// rule! No worries, it can be done after the fact:
attr.SetEquality(`caseIgnoreMatch`)
fmt.Println(attr.Equality().NumericOID())
// Output: 2.5.13.2
}
/*
This example demonstrates the assignment of a SUBSTR [MatchingRule]
instance to an [AttributeType] instance during assembly.
Note: this example assumes a legitimate schema variable is defined
in place of the fictional "mySchema" var shown here for simplicity.
*/
func ExampleAttributeType_SetSubstring() {
// First we'll craft a fake attribute
raw := `( 1.3.6.1.4.1.56521.999.14.56.1
NAME 'coolattr'
SYNTAX 1.3.6.1.4.1.1466.115.121.1.26 )`
var attr AttributeType = mySchema.NewAttributeType()
if err := attr.Parse(raw); err != nil {
fmt.Println(err)
return
}
// Oh no! We forgot to specify the desired substring matching
// rule! No worries, it can be done after the fact:
attr.SetSubstring(`caseIgnoreSubstringsMatch`)
fmt.Println(attr.Substring().NumericOID())
// Output: 2.5.13.4
}
/*
This example demonstrates the assignment of an ORDERING [MatchingRule]
instance to an [AttributeType] instance during assembly.
Note: this example assumes a legitimate schema variable is defined
in place of the fictional "mySchema" var shown here for simplicity.
*/
func ExampleAttributeType_SetOrdering() {
// First we'll craft a fake attribute
raw := `( 1.3.6.1.4.1.56521.999.14.56.1
NAME 'coolattr'
SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 )`
var attr AttributeType = mySchema.NewAttributeType()
if err := attr.Parse(raw); err != nil {
fmt.Println(err)
return
}
// Oh no! We forgot to specify the desired ordering matching
// rule! No worries, it can be done after the fact:
attr.SetOrdering(`integerOrderingMatch`)
fmt.Println(attr.Ordering().NumericOID())
// Output: 2.5.13.15
}
/*
This example demonstrates accessing the minimum upper bounds of an instance
of [AttributeType], if set.
Note: this example assumes a legitimate schema variable is defined
in place of the fictional "mySchema" var shown here for simplicity.
*/
func ExampleAttributeType_MinimumUpperBounds() {
ip := mySchema.AttributeTypes().Get(`ipHostNumber`)
fmt.Println(ip.MinimumUpperBounds())
// Output: 128
}
/*
Do stupid things to make schemax panic, gain additional
coverage in the process.
*/
func TestAttributeType_codecov(t *testing.T) {
attr := AttributeType{}
if err := attr.Parse(`(garbage)`); err == nil {
t.Errorf("%s failed: expected error, got nothing", t.Name())
return
}
attr = AttributeType{&attributeType{OID: `1.2.3.4.5`}}
if err := attr.Parse(`(garbage)`); err == nil {
t.Errorf("%s failed: expected error, got nothing", t.Name())
return
}
attr = mySchema.NewAttributeType()
goodraw := `( 1.3.6.1.4.1.56521.999.14.56.1 NAME 'coolattr' SUP cn )`
if err := attr.Parse(goodraw); err != nil {
t.Errorf("%s failed: %v", t.Name(), err)
return
}
var oo *attributeType
zz := AttributeType{oo}
_ = zz.Compliant()
_ = zz.Map()
_ = zz.QualifyValue(``)
_ = zz.QualifySyntax(``)
_ = zz.EqualityAssertion(nil, rune(33))
_ = zz.SubstringAssertion(nil, rune(33))
_ = zz.OrderingAssertion(nil, rune(33))
oo = new(attributeType)
oo.OID = ``
attr.replace(AttributeType{oo})
oo.OID = `freakz`
attr.replace(AttributeType{oo})
zz = AttributeType{oo}
zz.SetSchema(mySchema)
_ = zz.Compliant()
zz.setOID(`1.2.3.4.5.6.7`)
zz.macro()
goodraw = `( 1.3.6.1.4.1.56521.999.14.56.1 NAME 'coolattr' )`
if err := zz.Parse(goodraw); err != nil {
t.Errorf("%s failed: %v", t.Name(), err)
return
}
_ = zz.QualifySyntax(nil)
_ = zz.QualifyValue(nil)
_ = zz.EqualityAssertion(nil, rune(33))
_ = zz.SubstringAssertion(nil, rune(33))
_ = zz.OrderingAssertion(nil, rune(33))