File: test_schema.rb

package info (click to toggle)
ruby-activeldap 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 1,588 kB
  • sloc: ruby: 18,143; sh: 12; makefile: 5
file content (632 lines) | stat: -rw-r--r-- 25,921 bytes parent folder | download | duplicates (4)
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
require 'al-test-utils'

class TestSchema < Test::Unit::TestCase
  priority :must
  def test_directory_operation
    attributes_schema =
      "( 2.5.18.1 NAME 'createTimestamp' " +
      "DESC 'RFC4512: time which ob ject was created' " +
      "EQUALITY generalizedTimeMatch " +
      "ORDERING generalizedTimeOrderingMatch " +
      "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 " +
      "SINGLE-VALUE NO-USER-MODIFICATION USAGE directoryOperation )"

    assert_attribute_type({
                            :read_only => true,
                            :single_value => true,
                            :binary => false,
                            :binary_required => false,
                            :directory_operation => true,
                            :syntax => "1.3.6.1.4.1.1466.115.121.1.24",
                            :syntax_description => nil,
                          },
                          "createTimestamp",
                          [attributes_schema])
  end

  priority :normal
  def test_dit_content_rule
    object_class_schema = "( 2.5.6.6 NAME 'person' DESC " +
      "'RFC2256: a person' SUP top STRUCTURAL MUST sn " +
      "MAY ( userPassword $ telephoneNumber ) )"
    dit_content_rule_schema = "( 2.5.6.6 NAME 'person' MUST cn " +
      "MAY ( seeAlso $ description ) )"
    attributes_schema =
      [
       "( 2.5.4.3 NAME 'cn' SYNTAX '1.3.6.1.4.1.1466.115.121.1.15' SINGLE-VALUE )",
       "( 2.5.4.4 NAME 'sn' SYNTAX '1.3.6.1.4.1.1466.115.121.1.15' SINGLE-VALUE )",
       "( 2.5.4.35 NAME 'userPassword' SYNTAX '1.3.6.1.4.1.1466.115.121.1.40' )",
       "( 2.5.4.20 NAME 'telephoneNumber' SYNTAX '1.3.6.1.4.1.1466.115.121.1.15' SINGLE-VALUE )",
       "( 2.5.4.34 NAME 'seeAlso' SYNTAX '1.3.6.1.4.1.1466.115.121.1.12' )",
       "( 2.5.4.13 NAME 'description' SYNTAX '1.3.6.1.4.1.1466.115.121.1.15' )",
      ]

    entry = {
      "objectClasses" => [object_class_schema],
      "dITContentRules" => [dit_content_rule_schema],
      "attributeTypes" => attributes_schema,
    }

    schema = ActiveLdap::Schema.new(entry)
    object_class = schema.object_class("person")
    assert_equal({
                   :must => ["sn", "cn"],
                   :may => ["userPassword", "telephoneNumber",
                            "seeAlso", "description"],
                 },
                 {
                   :must => object_class.must.collect(&:name),
                   :may => object_class.may.collect(&:name),
                 })
  end

  def test_oid_list_with_just_only_one_oid
    ou_schema = "( 2.5.6.5 NAME 'organizationalUnit' SUP top STRUCTURAL MUST " +
      "(ou ) MAY (c $ l $ st $ street $ searchGuide $ businessCategory $ " +
      "postalAddress $ postalCode $ postOfficeBox $ " +
      "physicalDeliveryOfficeName $ telephoneNumber $ telexNumber $ " +
      "teletexTerminalIdentifier $ facsimileTelephoneNumber $ x121Address $ " +
      "internationalISDNNumber $ registeredAddress $ destinationIndicator $ " +
      "preferredDeliveryMethod $ seeAlso $ userPassword $ co $ countryCode $ " +
      "desktopProfile $ defaultGroup $ managedBy $ uPNSuffixes $ gPLink $ " +
      "gPOptions $ msCOM-UserPartitionSetLink $ thumbnailLogo ) ) "

    expect = {
      :name => ["organizationalUnit"],
      :sup => ["top"],
      :structural => ["TRUE"],
      :must => %w(ou),
      :may => %w(c l st street searchGuide businessCategory
                 postalAddress postalCode postOfficeBox
                 physicalDeliveryOfficeName telephoneNumber telexNumber
                 teletexTerminalIdentifier facsimileTelephoneNumber
                 x121Address internationalISDNNumber registeredAddress
                 destinationIndicator preferredDeliveryMethod seeAlso
                 userPassword co countryCode desktopProfile defaultGroup
                 managedBy uPNSuffixes gPLink gPOptions
                 msCOM-UserPartitionSetLink thumbnailLogo),
    }
    assert_schema(expect, "2.5.6.5", ou_schema)
    assert_schema(expect, "organizationalUnit", ou_schema)
  end

  def test_normalize_attribute_value
    entry = {
      "attributeTypes" =>
      [
       "( 0.9.2342.19200300.100.1.25 NAME ( 'dc' 'domainComponent' ) DESC " +
       "'RFC1274/2247: domain component' EQUALITY caseIgnoreIA5Match SUBSTR " +
       "caseIgnoreIA5SubstringsMatch SYNTAX 1.3.6.1.4.1.1466.115.121.1.26 " +
       "SINGLE-VALUE )",
      ],
      "ldapSyntaxes" =>
      [
       "( 1.3.6.1.4.1.1466.109.114.1 NAME 'caseExactIA5Match' " +
       "SYNTAX 1.3.6.1.4.1.1466.115.121.1.26 )",
      ],
    }

    schema = ActiveLdap::Schema.new(entry)
    dc = schema.attribute("dc")
    assert_equal(["com"], dc.normalize_value("com"))
    assert_equal(["com"], dc.normalize_value(["com"]))
    assert_raise(ActiveLdap::AttributeValueInvalid) do
      dc.normalize_value(["com", "co.jp"])
    end
    assert_equal([{"lang-en" => ["com"]},
                  {"lang-ja" => ["co.jp"]}],
                 dc.normalize_value([{"lang-en" => "com"},
                                     {"lang-ja" => "co.jp"}]))
  end

  def test_syntax_validation
    entry = {
      "attributeTypes" =>
      [
       "( 2.5.4.34 NAME 'seeAlso' DESC 'RFC2256: DN of related object'" +
       "SUP distinguishedName )",
       "( 2.5.4.49 NAME 'distinguishedName' DESC 'RFC2256: common " +
       "supertype of DN attributes' EQUALITY distinguishedNameMatch "+
       "SYNTAX 1.3.6.1.4.1.1466.115.121.1.12 )",
      ],
      "ldapSyntaxes" =>
      [
       "( 1.3.6.1.4.1.1466.115.121.1.12 DESC 'Distinguished Name' )",
      ],
    }

    schema = ActiveLdap::Schema.new(entry)
    see_also = schema.attribute("seeAlso")
    assert(see_also.valid?("cn=test,dc=example,dc=com"))
    assert(!see_also.valid?("test"))
  end

  def test_super_class?
    group = 'objectClasses'
    entry = {
      group => [
                "( 2.5.6.6 NAME 'person' DESC 'RFC2256: a person' SUP " +
                "top STRUCTURAL MUST ( sn $ cn ) MAY ( userPassword $ " +
                "telephoneNumber $ seeAlso $ description ) )",

                "( 2.5.6.7 NAME 'organizationalPerson' DESC 'RFC2256: " +
                "an organizational person' SUP person STRUCTURAL MAY ( " +
                "title $ x121Address $ registeredAddress $ " +
                "destinationIndicator $ preferredDeliveryMethod $ " +
                "telexNumber $ teletexTerminalIdentifier $ telephoneNumber " +
                "$ internationaliSDNNumber $ facsimileTelephoneNumber $ " +
                "street $ postOfficeBox $ postalCode $ postalAddress $ " +
                "physicalDeliveryOfficeName $ ou $ st $ l ) )",

                "( 2.16.840.1.113730.3.2.2 NAME 'inetOrgPerson' DESC " +
                "'RFC2798: Internet Organizational Person' SUP " +
                "organizationalPerson STRUCTURAL MAY ( audio $ " +
                "businessCategory $ carLicense $ departmentNumber $ " +
                "displayName $ employeeNumber $ employeeType $ givenName " +
                "$ homePhone $ homePostalAddress $ initials $ jpegPhoto " +
                "$ labeledURI $ mail $ manager $ mobile $ o $ pager $ " +
                "photo $ roomNumber $ secretary $ uid $ userCertificate $ " +
                "x500UniqueIdentifier $ preferredLanguage $ " +
                "userSMIMECertificate $ userPKCS12 ) )",
               ]
    }
    schema = ActiveLdap::Schema.new(entry)

    person = schema.object_class('person')
    organizational_person = schema.object_class("organizationalPerson")
    inet_org_person = schema.object_class("inetOrgPerson")

    assert_equal([false, false, false],
                 [person.super_class?(person),
                   person.super_class?(organizational_person),
                   person.super_class?(inet_org_person)])

    assert_equal([true, false, false],
                 [organizational_person.super_class?(person),
                  organizational_person.super_class?(organizational_person),
                  organizational_person.super_class?(inet_org_person)])

    assert_equal([true, true, false],
                 [inet_org_person.super_class?(person),
                  inet_org_person.super_class?(organizational_person),
                  inet_org_person.super_class?(inet_org_person)])
  end

  def test_duplicate_schema
    sasNMASProductOptions_schema =
      "( 2.16.840.1.113719.1.39.42.1.0.38 NAME 'sasNMASProductOptions' " +
      "SYNTAX 1.3.6.1.4.1.1466.115.121.1.40{64512} SINGLE-VALUE " +
      "X-NDS_PUBLIC_READ '1' )"
    rADIUSActiveConnections_schema =
      "( 2.16.840.1.113719.1.39.42.1.0.38 NAME 'rADIUSActiveConnections' " +
      "SYNTAX 1.3.6.1.4.1.1466.115.121.1.40{64512} X-NDS_NAME " +
      "'RADIUS:ActiveConnections' X-NDS_NOT_SCHED_SYNC_IMMEDIATE '1' )"

    sasNMASProductOptions = 'sasNMASProductOptions'
    rADIUSActiveConnections = 'rADIUSActiveConnections'
    sasNMASProductOptions_aliases =
      [sasNMASProductOptions, []]
    rADIUSActiveConnections_aliases =
      [rADIUSActiveConnections, []]
    sas_radius_aliases = [sasNMASProductOptions, [rADIUSActiveConnections]]
    radius_sas_aliases = [rADIUSActiveConnections, [sasNMASProductOptions]]

    assert_attribute_aliases([sasNMASProductOptions_aliases],
                             [sasNMASProductOptions],
                             [sasNMASProductOptions_schema],
                             false)
    assert_attribute_aliases([rADIUSActiveConnections_aliases],
                             [rADIUSActiveConnections],
                             [rADIUSActiveConnections_schema],
                             false)

    assert_attribute_aliases([sasNMASProductOptions_aliases,
                              sas_radius_aliases],
                             [sasNMASProductOptions,
                              rADIUSActiveConnections],
                             [sasNMASProductOptions_schema,
                              rADIUSActiveConnections_schema],
                             false)
    assert_attribute_aliases([rADIUSActiveConnections_aliases,
                              radius_sas_aliases],
                             [rADIUSActiveConnections,
                              sasNMASProductOptions],
                             [rADIUSActiveConnections_schema,
                              sasNMASProductOptions_schema],
                             false)

    assert_attribute_aliases([sas_radius_aliases,
                              sas_radius_aliases],
                             [rADIUSActiveConnections,
                              sasNMASProductOptions],
                             [sasNMASProductOptions_schema,
                              rADIUSActiveConnections_schema],
                             false)
    assert_attribute_aliases([radius_sas_aliases,
                              radius_sas_aliases],
                             [sasNMASProductOptions,
                              rADIUSActiveConnections],
                             [rADIUSActiveConnections_schema,
                              sasNMASProductOptions_schema],
                             false)

    assert_attribute_aliases([sas_radius_aliases,
                              sas_radius_aliases],
                             [sasNMASProductOptions,
                              rADIUSActiveConnections],
                             [sasNMASProductOptions_schema,
                              rADIUSActiveConnections_schema],
                             true)
    assert_attribute_aliases([radius_sas_aliases,
                              radius_sas_aliases],
                             [rADIUSActiveConnections,
                              sasNMASProductOptions],
                             [rADIUSActiveConnections_schema,
                              sasNMASProductOptions_schema],
                             true)
  end

  def test_empty_schema
    assert_make_schema_with_empty_entries(nil)
    assert_make_schema_with_empty_entries({})
    assert_make_schema_with_empty_entries({"someValues" => ["aValue"]})
  end

  def test_empty_schema_value
    schema = ActiveLdap::Schema.new({"attributeTypes" => nil})
    assert_equal([], schema["attributeTypes", "cn", "DESC"])
  end

  def test_attribute_name_with_under_score
    top_schema =
      "( 2.5.6.0 NAME 'Top' STRUCTURAL MUST objectClass MAY ( " +
      "cAPublicKey $ cAPrivateKey $ certificateValidityInterval $ " +
      "authorityRevocation $ lastReferencedTime $ " +
      "equivalentToMe $ ACL $ backLink $ binderyProperty $ " +
      "Obituary $ Reference $ revision $ " +
      "ndsCrossCertificatePair $ certificateRevocation $ " +
      "usedBy $ GUID $ otherGUID $ DirXML-Associations $ " +
      "creatorsName $ modifiersName $ unknownBaseClass $ " +
      "unknownAuxiliaryClass $ auditFileLink $ " +
      "masvProposedLabelel $ masvDefaultRange $ " +
      "masvAuthorizedRange $ objectVersion $ " +
      "auxClassCompatibility $ rbsAssignedRoles $ " +
      "rbsOwnedCollections $ rbsAssignedRoles2 $ " +
      "rbsOwnedCollections2 ) X-NDS_NONREMOVABLE '1' " +
      "X-NDS_ACL_TEMPLATES '16#subtree#[Creator]#[Entry Rights]' )"

    expect = {
      :name => ["Top"],
      :structural => ["TRUE"],
      :must => ["objectClass"],
      :x_nds_nonremovable => ["1"],
      :x_nds_acl_templates => ['16#subtree#[Creator]#[Entry Rights]'],
    }
    assert_schema(expect, "Top", top_schema)
  end

  def test_sup_with_oid_start_with_upper_case
    organizational_person_schema =
      "( 2.5.6.7 NAME 'organizationalPerson' SUP Person STRUCTURAL MAY " +
      "( facsimileTelephoneNumber $ l $ eMailAddress $ ou $ " +
      "physicalDeliveryOfficeName $ postalAddress $ postalCode $ " +
      "postOfficeBox $ st $ street $ title $ mailboxLocation $ " +
      "mailboxID $ uid $ mail $ employeeNumber $ destinationIndicator $ " +
      "internationaliSDNNumber $ preferredDeliveryMethod $ " +
      "registeredAddress $ teletexTerminalIdentifier $ telexNumber $ " +
      "x121Address $ businessCategory $ roomNumber $ x500UniqueIdentifier " +
      ") X-NDS_NAMING ( 'cn' 'ou' 'uid' ) X-NDS_CONTAINMENT ( " +
      "'Organization' 'organizationalUnit' 'domain' ) X-NDS_NAME " +
      "'Organizational Person' X-NDS_NOT_CONTAINER '1' " +
      "X-NDS_NONREMOVABLE '1' )"

    expect = {
      :name => ["organizationalPerson"],
      :sup => ["Person"],
      :structural => ["TRUE"],
      :x_nds_naming => ["cn", "ou", "uid"],
      :x_nds_containment => ["Organization", "organizationalUnit", "domain"],
      :x_nds_name => ["Organizational Person"],
      :x_nds_not_container => ["1"],
      :x_nds_nonremovable => ["1"],
    }
    assert_schema(expect, "organizationalPerson",
                  organizational_person_schema)
  end

  def test_text_oid
    text_oid_schema = "( mysite-oid NAME 'mysite' " +
                      "SUP groupofuniquenames STRUCTURAL " +
                      "MUST ( mysitelang $ mysiteurl ) " +
                      "MAY ( mysitealias $ mysitecmsurl ) " +
                      "X-ORIGIN 'user defined' )"
    expect = {
      :name => ["mysite"],
      :sup => ["groupofuniquenames"],
      :structural => ["TRUE"],
      :must => %w(mysitelang mysiteurl),
      :may => %w(mysitealias mysitecmsurl),
      :x_origin => ["user defined"]
    }
    assert_schema(expect, "mysite", text_oid_schema)

    text_oid_attribute_schema = "( mysiteurl-oid NAME 'mysiteurl' " +
                                "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 " +
                                "SINGLE-VALUE X-ORIGIN 'user defined' )"
    expect = {
      :name => ["mysiteurl"],
      :syntax => ["1.3.6.1.4.1.1466.115.121.1.15"],
      :single_value => ["TRUE"],
      :x_origin => ["user defined"]
    }
    assert_schema(expect, "mysiteurl", text_oid_attribute_schema)
  end

  def test_name_as_key
    top_schema = "( 2.5.6.0 NAME 'top' DESC 'top of the superclass chain' " +
                 "ABSTRACT MUST objectClass )"
    expect = {
      :name => ["top"],
      :desc => ['top of the superclass chain'],
      :abstract => ["TRUE"],
      :must => ["objectClass"]
    }
    assert_schema(expect, "2.5.6.0", top_schema)
    assert_schema(expect, "top", top_schema)
  end

  def test_name_as_key_for_multiple_name
    uid_schema = "( 0.9.2342.19200300.100.1.1 NAME ( 'uid' 'userid' ) " +
                 "DESC 'RFC1274: user identifier' EQUALITY caseIgnoreMatch " +
                 "SUBSTR caseIgnoreSubstringsMatch " +
                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15{256} )"

    expect = {
      :name => ["uid", "userid"],
      :desc => ['RFC1274: user identifier'],
      :equality => ["caseIgnoreMatch"],
      :substr => ["caseIgnoreSubstringsMatch"],
      :syntax => ["1.3.6.1.4.1.1466.115.121.1.15{256}"],
    }
    assert_schema(expect, "0.9.2342.19200300.100.1.1", uid_schema)
    assert_schema(expect, "uid", uid_schema)
    assert_schema(expect, "userid", uid_schema)
  end

  def test_dollar
    dn_match_schema = "( 2.5.13.1 NAME 'distinguishedNameMatch' APPLIES " +
                      "( creatorsName $ modifiersName $ subschemaSubentry " +
                      "$ namingContexts $ aliasedObjectName $ " +
                      "distinguishedName $ seeAlso $ olcDefaultSearchBase $ " +
                      "olcRootDN $ olcSchemaDN $ olcSuffix $ olcUpdateDN $ " +
                      "member $ owner $ roleOccupant $ manager $ " +
                      "documentAuthor $ secretary $ associatedName $ " +
                      "dITRedirect ) )"

    expect = {
      :name => ["distinguishedNameMatch"],
      :applies => %w(creatorsName modifiersName subschemaSubentry
                     namingContexts aliasedObjectName
                     distinguishedName seeAlso olcDefaultSearchBase
                     olcRootDN olcSchemaDN olcSuffix olcUpdateDN
                     member owner roleOccupant manager
                     documentAuthor secretary associatedName
                     dITRedirect),
    }
    assert_schema(expect, "2.5.13.1", dn_match_schema)
    assert_schema(expect, "distinguishedNameMatch", dn_match_schema)
  end

  def test_dc_object
    dc_object_schema = "( 1.3.6.1.4.1.1466.344 NAME 'dcObject' DESC " +
                       "'RFC2247: domain component object' SUP top " +
                       "AUXILIARY MUST dc )"

    expect = {
      :name => ["dcObject"],
      :desc => ['RFC2247: domain component object'],
      :auxiliary => ["TRUE"],
      :must => ["dc"],
    }
    assert_schema(expect, "1.3.6.1.4.1.1466.344", dc_object_schema)
    assert_schema(expect, "dcObject", dc_object_schema)
  end

  def test_organization
    organization_schema = "( 2.5.6.4 NAME 'organization' DESC " +
                          "'RFC2256: an organization' SUP top STRUCTURAL " +
                          "MUST o MAY ( userPassword $ searchGuide $ " +
                          "seeAlso $ businessCategory $ x121Address $ " +
                          "registeredAddress $ destinationIndicator $ " +
                          "preferredDeliveryMethod $ telexNumber $ " +
                          "teletexTerminalIdentifier $ telephoneNumber $ " +
                          "internationaliSDNNumber $ " +
                          "facsimileTelephoneNumber $ street $ " +
                          "postOfficeBox $ postalCode $ postalAddress $ " +
                          "physicalDeliveryOfficeName $ st $ l $ " +
                          "description ) )"

    expect = {
      :name => ["organization"],
      :desc => ['RFC2256: an organization'],
      :sup => ["top"],
      :structural => ["TRUE"],
      :must => ["o"],
      :may => %w(userPassword searchGuide seeAlso businessCategory
                 x121Address registeredAddress destinationIndicator
                 preferredDeliveryMethod telexNumber
                 teletexTerminalIdentifier telephoneNumber
                 internationaliSDNNumber
                 facsimileTelephoneNumber street
                 postOfficeBox postalCode postalAddress
                 physicalDeliveryOfficeName st l description),
    }
    assert_schema(expect, "2.5.6.4", organization_schema)
    assert_schema(expect, "organization", organization_schema)
  end

  def test_posix_account
    posix_account_schema = "( 1.3.6.1.1.1.2.0 NAME 'posixAccount' DESC " +
                           "'Abstraction of an account with POSIX " +
                           "attributes' SUP top AUXILIARY MUST ( cn $ " +
                           "uid $ uidNumber $ gidNumber $ homeDirectory " +
                           ") MAY ( userPassword $ loginShell $ gecos $ " +
                           "description ) )"
    expect = {
      :name => ["posixAccount"],
      :desc => ['Abstraction of an account with POSIX attributes'],
      :sup => ["top"],
      :auxiliary => ["TRUE"],
      :must => %w(cn uid uidNumber gidNumber homeDirectory),
      :may => %w(userPassword loginShell gecos description),
    }
    assert_schema(expect, "1.3.6.1.1.1.2.0", posix_account_schema)
    assert_schema(expect, "posixAccount", posix_account_schema)
  end

  def test_jpeg_photo
    jpeg_photo_schema = "( 0.9.2342.19200300.100.1.60 NAME 'jpegPhoto' " +
                        "DESC 'RFC2798: a JPEG image' SYNTAX " +
                        "1.3.6.1.4.1.1466.115.121.1.28 )"
    expect = {
      :name => ["jpegPhoto"],
      :desc => ['RFC2798: a JPEG image'],
      :syntax => ["1.3.6.1.4.1.1466.115.121.1.28"],
    }
    assert_schema(expect, "0.9.2342.19200300.100.1.60", jpeg_photo_schema)
    assert_schema(expect, "jpegPhoto", jpeg_photo_schema)

    jpeg_schema = "( 1.3.6.1.4.1.1466.115.121.1.28 DESC 'JPEG' " +
                  "X-NOT-HUMAN-READABLE 'TRUE' )"

    expect = {
      :desc => ['JPEG'],
      :x_not_human_readable => ["TRUE"],
    }
    assert_schema(expect, "1.3.6.1.4.1.1466.115.121.1.28", jpeg_schema)

    schema = ActiveLdap::Schema.new({"attributeTypes" => [jpeg_photo_schema],
                                     "ldapSyntaxes" => [jpeg_schema]})
    assert(schema.attribute("jpegPhoto").binary?)
  end

  private
  def assert_schema(expect, name, schema)
    sub = "objectClasses"
    entry = {sub => [schema]}
    schema = ActiveLdap::Schema.new(entry)
    actual = {}
    normalized_expect = {}
    expect.each do |key, value|
      normalized_key = key.to_s.gsub(/_/, "-")
      normalized_expect[normalized_key] = value
      actual[normalized_key] = schema[sub, name, normalized_key]
    end
    assert_equal(normalized_expect, actual)
    schema
  end

  def assert_make_schema_with_empty_entries(entries)
    schema = ActiveLdap::Schema.new(entries)
    assert_equal([], schema["attributeTypes", "cn", "DESC"])
    assert_equal([], schema["ldapSyntaxes",
                            "1.3.6.1.4.1.1466.115.121.1.5",
                            "DESC"])
    assert_equal([], schema["objectClasses", "posixAccount", "MUST"])
  end

  def assert_attribute_aliases(expected, keys, schemata, ensure_parse)
    group = 'attributeTypes'
    entry = {group => schemata}
    schema = ActiveLdap::Schema.new(entry)
    schema.send(:ensure_parse, group) if ensure_parse
    result = keys.collect do |key|
      attribute = schema.attribute(key)
      [attribute.name, attribute.aliases]
    end
    assert_equal(expected, result)
  end

  def assert_attribute_type(expected, name, schemata)
    group = 'attributeTypes'
    entry = {group => schemata}
    schema = ActiveLdap::Schema.new(entry)
    attribute_hash = schema.attribute(name).to_hash
    syntax = attribute_hash[:syntax]
    attribute_hash[:syntax] = syntax.id if syntax
    assert_equal(expected, attribute_hash)
  end

  class TestAttribute < self
    class TestApplyEncoding < self
      class TestNotBinary < self
        def setup
          super
          uid_schema =
            "( 0.9.2342.19200300.100.1.1 NAME ( 'uid' 'userid' ) " +
            "DESC 'RFC1274: user identifier' EQUALITY caseIgnoreMatch " +
            "SUBSTR caseIgnoreSubstringsMatch " +
            "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15{256} )"
          entries = {
            "attributeTypes" => [uid_schema],
          }
          schema = ActiveLdap::Schema.new(entries)
          @attribute = schema.attribute("uid")
        end

        def test_do_nothing
          value = ""
          value.force_encoding("UTF-8")
          @attribute.apply_encoding(value)
          assert_equal(Encoding::UTF_8, value.encoding)
        end
      end

      class TestBinary < self
        def setup
          super
          jpeg_schema =
            "( 1.3.6.1.4.1.1466.115.121.1.28 DESC 'JPEG' " +
            "X-NOT-HUMAN-READABLE 'TRUE' )"
          jpeg_photo_schema =
            "( 0.9.2342.19200300.100.1.60 NAME 'jpegPhoto' " +
            "DESC 'RFC2798: a JPEG image' SYNTAX " +
            "1.3.6.1.4.1.1466.115.121.1.28 )"
          entries = {
            "attributeTypes" => [jpeg_photo_schema],
            "ldapSyntaxes" => [jpeg_schema],
          }
          schema = ActiveLdap::Schema.new(entries)
          @attribute = schema.attribute("jpegPhoto")
        end

        def test_string
          value = ""
          value.force_encoding("UTF-8")
          @attribute.apply_encoding(value)
          assert_equal(Encoding::ASCII_8BIT, value.encoding)
        end

        def test_array
          values = [""]
          values.each do |value|
            value.force_encoding("UTF-8")
          end
          @attribute.apply_encoding(values)
          assert_equal([Encoding::ASCII_8BIT],
                       values.collect(&:encoding))
        end

        def test_hash
          values = {:binary => ""}
          values.each_value do |value|
            value.force_encoding("UTF-8")
          end
          @attribute.apply_encoding(values)
          assert_equal([Encoding::ASCII_8BIT],
                       values.each_value.collect(&:encoding))
        end
      end
    end
  end
end