This repository has been archived by the owner on Dec 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
dc_test.go
469 lines (403 loc) · 13.4 KB
/
dc_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
package schemax
import (
"fmt"
"testing"
)
/*
This example demonstrates the means for marshaling an instance of
[DITContentRule] from a map[string]any instance.
*/
func ExampleDITContentRule_Marshal() {
m := map[string]any{
`NAME`: `exampleRule`,
`DESC`: `This is an example`,
`NUMERICOID`: `2.5.6.6`,
`OBSOLETE`: `FALSE`,
`AUX`: []string{`uidObject`},
`MUST`: []string{`cn`, `sn`},
`MAY`: []string{`description`},
`NOT`: []string{`userPassword`},
`X-ORIGIN`: `RFCXXXX`,
}
var def DITContentRule = mySchema.NewDITContentRule()
if err := def.Marshal(m); err != nil {
fmt.Println(err)
return
}
fmt.Printf("%s\n", def)
// Output: ( 2.5.6.6
// NAME 'exampleRule'
// DESC 'This is an example'
// AUX uidObject
// MUST ( cn
// $ sn )
// MAY description
// NOT userPassword
// X-ORIGIN 'RFCXXXX' )
}
/*
This example demonstrates the means for checking to see if the receiver
is in an error condition.
*/
func ExampleDITContentRule_E() {
def := mySchema.NewDITContentRule()
def.SetNumericOID(`23jklm5.1`) // bogus
if err := def.E(); err != nil {
fmt.Println(err)
}
// Output: Numeric OID is invalid
}
/*
This example demonstrates the means for resolving an error condition.
*/
func ExampleDITContentRule_E_clearError() {
def := mySchema.NewDITContentRule()
def.SetNumericOID(`23jklm5.1`) // bogus
// We realized our mistake.
def.SetNumericOID(mySchema.ObjectClasses().Get(`person`).NumericOID()) // valid
// But when we check again, the error is still there.
if def.E() != nil {
//fmt.Println(... the error ...)
}
// We must clear the error with a
// passing compliance check.
if def.Compliant(); def.E() == nil {
fmt.Println("Error has been resolved")
}
// Output: Error has been resolved
return
}
/*
This example demonstrates a compliancy check of the "account" [ObjectClass].
*/
func ExampleDITContentRule_Compliant() {
dc := mySchema.DITContentRules().Index(0)
fmt.Println(dc.Compliant())
// Output: true
}
/*
This example demonstrates a compliancy check of all [ObjectClasses] members.
Note: this example assumes a legitimate schema variable is defined
in place of the fictional "mySchema" var shown here for simplicity.
*/
func ExampleDITContentRules_Compliant() {
rules := mySchema.DITContentRules()
fmt.Println(rules.Compliant())
// Output: true
}
/*
This example demonstrates use of the [ObjectClass.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 [ObjectClass.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 ExampleDITContentRule_SetStringer() {
opers := mySchema.DITContentRules().Index(0)
opers.SetStringer(func() string {
return "This useless message brought to you by a dumb stringer"
})
msg := fmt.Sprintln(opers)
opers.SetStringer() // return it to its previous state if need be ...
fmt.Printf("Original: %s\nOld: %s", opers, msg)
// Output: Original: ( 1.3.6.1.4.1.56521.101.2.5.3
// NAME 'arcContent'
// DESC 'arc entry content rule'
// AUX ( x660Context
// $ x667Context
// $ x680Context
// $ x690Context )
// MUST ( aSN1Notation
// $ iRI
// $ identifier
// $ n
// $ unicodeValue )
// MAY ( additionalUnicodeValue
// $ nameAndNumberForm
// $ secondaryIdentifier
// $ standardizedNameForm )
// NOT dotNotation
// X-ORIGIN 'draft-coretta-oiddir-schema; unofficial supplement'
// X-WARNING 'UNOFFICIAL' )
// Old: This useless message brought to you by a dumb stringer
}
/*
This example demonstrates use of the [ObjectClasses.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 [ObjectClass] instances.
To avoid impacting other unit tests, we reset the default stringer
via the [ObjectClasses.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 ExampleDITContentRules_SetStringer() {
attrs := mySchema.DITContentRules()
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 means for accessing all [DITContentRule]
instances which bear the specified `X-ORIGIN` extension value.
Note: this example assumes a legitimate schema variable is defined
in place of the fictional "mySchema" var shown here for simplicity.
*/
func ExampleDITContentRules_XOrigin() {
defs := mySchema.DITContentRules()
matches := defs.XOrigin(`RFC4512`) // no content rules in RFC 4512
fmt.Printf("Matched %d of %d %s\n", matches.Len(), defs.Len(), defs.Type())
// Output: Matched 0 of 1 dITContentRules
}
/*
This example demonstrates the assignment of arbitrary data to an instance
of [ObjectClass].
Note: this example assumes a legitimate schema variable is defined
in place of the fictional "mySchema" var shown here for simplicity.
*/
func ExampleDITContentRule_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.`
// Pretend this is a real dITContentRule
dvc := mySchema.NewDITContentRule()
// Set it.
dvc.SetData(documentation)
// Get it and compare to the original.
equal := documentation == dvc.Data().(string)
fmt.Printf("Values are equal: %t", equal)
// Output: Values are equal: true
}
func ExampleDITContentRule_IsIdentifiedAs() {
oc := mySchema.DITContentRules().Get(`arcContent`)
fmt.Println(oc.IsIdentifiedAs(`1.3.6.1.4.1.56521.101.2.5.3`))
// Output: true
}
func ExampleDITContentRule_SetObsolete() {
fake := NewDITContentRule().
SetNumericOID(`1.3.6.1.4.1.56521.999.108.4`).
SetName(`obsoleteClass`).
SetObsolete()
fmt.Println(fake.Obsolete())
// Output: true
}
/*
This example demonstrates a means of checking whether a particular instance
of [ObjectClass] is present within an instance of [ObjectClasses].
Note: this example assumes a legitimate schema variable is defined
in place of the fictional "mySchema" var shown here for simplicity.
*/
func ExampleDITContentRules_Contains() {
rules := mySchema.DITContentRules()
fmt.Println(rules.Contains(`arcContent`)) // or "1.3.6.1.4.1.56521.101.2.5.3"
// Output: true
}
func ExampleDITContentRules_Inventory() {
dc := mySchema.DITContentRules().Inventory()
fmt.Println(dc[`1.3.6.1.4.1.56521.101.2.5.3`][0])
// Output: arcContent
}
func ExampleDITContentRules_Type() {
oc := mySchema.DITContentRules()
fmt.Println(oc.Type())
// Output: dITContentRules
}
func ExampleDITContentRule_Type() {
var def DITContentRule
fmt.Println(def.Type())
// Output: dITContentRule
}
func ExampleDITContentRule_Map() {
def := mySchema.DITContentRules().Index(0)
fmt.Println(def.Map()[`NUMERICOID`][0]) // risky, just for simplicity
// Output: 1.3.6.1.4.1.56521.101.2.5.3
}
/*
This example demonstrates use of the [ObjectClasses.Maps] method, which
produces slices of [DefinitionMap] instances born of the [ObjectClasses]
stack in which they reside. We (quite recklessly) call index three (3)
and reference index zero (0) of its `SYNTAX` key to obtain the relevant
[LDAPSyntax] OID string value.
*/
func ExampleDITContentRules_Maps() {
defs := mySchema.DITContentRules().Maps()
fmt.Println(defs[0][`NUMERICOID`][0]) // risky, just for simplicity
// Output: 1.3.6.1.4.1.56521.101.2.5.3
}
/*
This example demonstrates the manual (non-parsed) assembly of a new
[ObjectClass] instance.
Note: this example assumes a legitimate schema variable is defined
in place of the fictional "mySchema" var shown here for simplicity.
*/
func ExampleNewDITContentRule() {
oc := NewDITContentRule() // Important! Initializes internal stacks
// Conveniently input values in fluent form ...
oc.SetSchema(mySchema).
SetName(`engineeringPersonnel`).
SetDescription(`EP-46: Engineering employee`).
SetNumericOID(`0.9.2342.19200300.100.4.5`).
SetMust(`uid`).
SetMay(`description`, `seeAlso`, `l`, `o`, `ou`).
SetNot(`host`).
SetExtension(`X-ORIGIN`, `NOWHERE`).
SetStringer() // use default stringer
fmt.Println(oc)
// Output: ( 0.9.2342.19200300.100.4.5
// NAME 'engineeringPersonnel'
// DESC 'EP-46: Engineering employee'
// MUST uid
// MAY ( description
// $ seeAlso
// $ l
// $ o
// $ ou )
// NOT host
// X-ORIGIN 'NOWHERE' )
}
/*
Do stupid things to make schemax panic, gain additional
coverage in the process.
*/
func TestDITContentRule_codecov(t *testing.T) {
_ = mySchema.DITContentRules().SetStringer().Contains(``)
mySchema.DITContentRules().Push(rune(10))
mySchema.DITContentRules().IsZero()
_ = mySchema.DITContentRules().String()
cim := mySchema.DITContentRules().Get(`account`)
mySchema.DITContentRules().canPush()
mySchema.DITContentRules().canPush(``, ``, ``, ``, cim)
mySchema.DITContentRules().canPush(cim, cim)
bmr := newCollection(``)
bma := newCollection(``)
ObjectClasses(bmr.cast()).Push(NewDITContentRule().SetSchema(mySchema))
ObjectClasses(bmr.cast()).Push(NewDITContentRule().SetSchema(mySchema).SetNumericOID(`1.2.3.4.5`))
bmr.cast().Push(NewDITContentRule().SetSchema(mySchema))
bmr.cast().Push(NewDITContentRule().SetSchema(mySchema).SetNumericOID(`1.2.3.4.5`))
var bad ObjectClass
bmr.cast().Push(bad)
ObjectClasses(bmr.cast()).oIDsStringerPretty(0)
ObjectClasses(bmr.cast()).oIDsStringerStd()
ObjectClasses(bmr.cast()).canPush()
ObjectClasses(bmr.cast()).canPush(`things`)
var ocs ObjectClasses
ocs.oIDsStringerPretty(0)
ocs.oIDsStringerStd()
ocs.canPush(`forks`)
ocs.Push(NewDITContentRule().SetSchema(mySchema))
bmr.cast().Push(AttributeType{&attributeType{OID: `1.2.3.4.5`, Collective: true, Single: true}})
bma.cast().Push(AttributeType{&attributeType{OID: ``, Collective: true, Single: true}})
xoc := ObjectClass{&objectClass{
Must: AttributeTypes(bmr),
}}
yoc := ObjectClass{&objectClass{
May: AttributeTypes(bma),
}}
xoc.Compliant()
yoc.Compliant()
ocs.Push(bad)
ObjectClasses(bmr).Push(NewDITContentRule().SetSchema(mySchema))
ObjectClasses(bmr).Push(NewDITContentRule().SetSchema(mySchema).SetNumericOID(`1.2.3.4.5`))
ObjectClasses(bmr).Compliant()
mySchema.DITContentRules().Compliant()
var def DITContentRule
_ = def.String()
_ = def.SetStringer()
_ = def.Description()
_ = def.Name()
_ = def.Names()
_ = def.Extensions()
_ = def.Must()
_ = def.May()
_ = def.Not()
_ = def.Schema()
_ = def.Map()
_ = def.Compliant()
_ = def.macro()
_ = def.Obsolete()
def.setOID(`4.3.2.1`)
var raw string = `( 1.3.6.1.4.1.56521.101.2.5.3
NAME 'arcContent'
DESC 'root arc entry content rule'
AUX ( x660Context $ x667Context $ x680Context $ x690Context )
MUST ( aSN1Notation
$ iRI
$ identifier
$ n
$ unicodeValue )
MAY ( additionalUnicodeValue
$ nameAndNumberForm
$ secondaryIdentifier
$ standardizedNameForm )
NOT dotNotation
X-ORIGIN 'draft-coretta-oiddir-schema; unofficial supplement'
X-WARNING 'UNOFFICIAL' )`
if err := def.Parse(raw); err != ErrNilReceiver {
t.Errorf("%s failed: expected ErrNilReceiver, got %v", t.Name(), err)
return
}
def = NewDITContentRule()
def.SetDescription(`'a`)
def.SetDescription(`'Unnecessary quoted value to be overwritten'`)
oo := new(dITContentRule)
oo.OID = mySchema.ObjectClasses().Get(`device`)
def.replace(DITContentRule{oo})
if err := def.Parse(raw); err != ErrNilSchemaRef {
t.Errorf("%s failed: expected ErrNilSchemaRef, got %v", t.Name(), err)
return
}
// Try again. Properly.
def.SetSchema(mySchema)
if def.Schema().IsZero() {
t.Errorf("%s failed: no schema reference!", t.Name())
return
}
def.setStringer(func() string {
return "blarg"
})
def.SetAux(mySchema.ObjectClasses().Get(`dcObject`))
def.SetAux(`dcObject`)
def.SetAux(rune(8))
def.SetMust(mySchema.AttributeTypes().Get(`cn`))
def.SetMust(rune(11))
def.SetMay(mySchema.AttributeTypes().Get(`sn`))
def.SetMay(rune(11))
def.SetNot(rune(8))
def.SetNot(`l`)
def.SetNot(mySchema.AttributeTypes().Get(`l`))
def.Map()
mySchema.DITContentRules().canPush(DITContentRule{}, DITContentRule{new(dITContentRule)})
if err := def.Parse(raw); err != ErrDuplicateDef {
t.Errorf("%s failed: expected duplicate err, got %v", t.Name(), err)
return
}
def.StructuralClass().EnforcedBy()
_ = def.macro()
def.setOID(`2.5.6.2`)
def.SetData(`fake`)
def.SetData(nil)
def.Data()
auxs := NewObjectClassOIDList()
auxs.Push(mySchema.ObjectClasses().Get(`dcObject`))
dcrs := NewDITContentRules()
dcrs.oIDsStringer()
dcrs.Push(mySchema.DITContentRules().Index(0))
dcrs.oIDsStringer()
dcrs.Push(DITContentRule{&dITContentRule{
OID: mySchema.ObjectClasses().Get(`2.5.6.2`),
Aux: auxs,
}})
dcrs.oIDsStringer()
dcrs.cast().NoPadding(true)
dcrs.oIDsStringer()
def.Map()
var def2 DITContentRule
_ = def2.Replace(def) // will fail
}