File: tc_ldif.rb

package info (click to toggle)
ruby-ldap 0.9.20-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 452 kB
  • sloc: ansic: 4,097; ruby: 1,525; makefile: 5
file content (174 lines) | stat: -rwxr-xr-x 5,856 bytes parent folder | download | duplicates (2)
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
# $Id: tc_ldif.rb,v 1.5 2005/02/26 01:42:27 ianmacd Exp $
#
# A suite of unit tests for testing Ruby/LDAP LDIF functionality. 

require 'ldap'
require 'ldap/ldif'
require 'ldap/control'
require 'test/unit'

class TC_LDIFTest < Test::Unit::TestCase

  include LDAP::LDIF

  def test_version_entry
    ldif = File.open( 'data/ldif1.txt' ) { |f| f.readlines }
    entry = nil
    assert_nothing_raised { entry = LDAP::LDIF.parse_entry( ldif ) }
    assert_instance_of( LDAP::Record, entry )
    assert_instance_of( String, entry.dn )
    assert_instance_of( Array, entry.attrs['objectclass'] )
    assert( entry.attrs['objectclass'].length > 1 )
  end

  def test_bad_version_entry
    ldif = File.open( 'data/ldif2.txt' ) { |f| f.readlines }
    assert_raise( LDIFError ) { LDAP::LDIF.parse_entry( ldif ) }
  end

  def test_no_version_entry
    ldif = File.open( 'data/ldif3.txt' ) { |f| f.readlines }
    assert_nothing_raised { LDAP::LDIF.parse_entry( ldif ) }
  end

  def test_file
    entries = LDAP::LDIF.parse_file( 'data/ldif4.txt' )
    assert_instance_of( Array, entries )
    assert_instance_of( Hash, entries[0].attrs )
    assert_not_equal( {}, entries[0].attrs )
  end

  def test_folded_attribute_entry
    ldif = File.open( 'data/ldif5.txt' ) { |f| f.readlines }
    entry = nil
    assert_nothing_raised { entry = LDAP::LDIF.parse_entry( ldif ) }
    assert_no_match( /\n/, entry.attrs['description'][0] )
    assert( entry.attrs['description'][0].length > LDAP::LDIF::LINE_LENGTH )
  end

  def test_base64_value_entry
    ldif = File.open( 'data/ldif6.txt' ) { |f| f.readlines }
    entry = LDAP::LDIF.parse_entry( ldif )
    assert_match( /\r/, entry.attrs['description'][0] )
  end

  def test_utf8_file
    entries = LDAP::LDIF.parse_file( 'data/ldif7.txt' )
    assert_instance_of( Array, entries )
    assert_instance_of( Hash, entries[0].attrs )
    assert_not_equal( {}, entries[0].attrs )
  end

  def test_external_file_entry
    ldif = File.open( 'data/ldif8.txt' ) { |f| f.readlines }
    entry = LDAP::LDIF.parse_entry( ldif )
    assert( entry.attrs['jpegphoto'][0].size > 1024 )
  end

  def test_change_records_file
    entries = LDAP::LDIF.parse_file( 'data/ldif9.txt' )
    assert_instance_of( Array, entries )
    assert_instance_of( Hash, entries[0].attrs )
    assert_not_equal( {}, entries[0].attrs )
  end

  def test_bad_line_entry
    ldif = File.open( 'data/ldif10.txt' ) { |f| f.readlines }
    assert_raise( LDIFError ) { LDAP::LDIF.parse_entry( ldif ) }
  end

  def test_bad_attr_entry
    ldif = File.open( 'data/ldif11.txt' ) { |f| f.readlines }
    assert_raise( LDIFError ) { LDAP::LDIF.parse_entry( ldif ) }
  end

  def test_change_record_binary_replace_entry
    ldif = File.open( 'data/ldif12.txt' ) { |f| f.readlines }
    entry = LDAP::LDIF.parse_entry( ldif )
    assert( entry.mods.keys.include?( LDAP::LDAP_MOD_REPLACE |
                                      LDAP::LDAP_MOD_BVALUES ) )
  end

  def test_change_record_control
    ldif = File.open( 'data/ldif13.txt' ) { |f| f.readlines }
    entry = LDAP::LDIF.parse_entry( ldif )
    assert_instance_of( LDAP::Control, entry.controls[0] )
    assert_equal( entry.controls[0].oid, '1.2.840.113556.1.4.805' )
    assert( entry.controls[0].iscritical )
    assert_nil( entry.controls[0].value )
  end

  def test_change_record_control2
    ldif = File.open( 'data/ldif14.txt' ) { |f| f.readlines }
    entry = LDAP::LDIF.parse_entry( ldif )
    assert_instance_of( LDAP::Control, entry.controls[0] )
    assert_equal( entry.controls[0].oid, '1.2.3.4' )
    assert_equal( entry.controls[0].iscritical, false )
    assert_not_nil( entry.controls[0].value )
  end

  def test_mod_to_ldif
    mod = LDAP.mod( LDAP::LDAP_MOD_ADD | LDAP::LDAP_MOD_BVALUES,
                    'mailRoutingAddress', ['a', 'b'] )
    assert_instance_of( LDAP::LDIF::Mod,
                        mod.to_ldif( 'uid=foo,dc=example,dc=com' ) )
    assert_equal( <<LDIF, mod.to_ldif( 'uid=foo,dc=example,dc=com' ) )
dn: uid=foo,dc=example,dc=com
changetype: add
mailRoutingAddress: a
mailRoutingAddress: b
LDIF

    mod = LDAP.mod( LDAP::LDAP_MOD_REPLACE | LDAP::LDAP_MOD_BVALUES,
                    'mailRoutingAddress', ['a', 'b'] )
    assert_equal( <<LDIF, mod.to_ldif( 'uid=foo,dc=example,dc=com' ) )
dn: uid=foo,dc=example,dc=com
changetype: modify
replace: mailRoutingAddress
mailRoutingAddress: a
mailRoutingAddress: b
LDIF

    mod = LDAP.mod( LDAP::LDAP_MOD_DELETE, 'mailRoutingAddress', ['a', 'b'] )
    assert_equal( <<LDIF, mod.to_ldif( 'uid=foo,dc=example,dc=com' ) )
dn: uid=foo,dc=example,dc=com
changetype: delete
mailRoutingAddress: a
mailRoutingAddress: b
LDIF
  end

  def test_mods_to_ldif

    # Try passing an array of mods to LDAP::LDIF.mods_to_ldif.

    # This must be a single line for the heredoc to work.
    assert_equal( <<LDIF, LDAP::LDIF.mods_to_ldif( 'uid=ianmacd,dc=foo', [ LDAP.mod( LDAP::LDAP_MOD_ADD | LDAP::LDAP_MOD_BVALUES, 'mailRoutingAddress', ['a', 'b'] ), LDAP.mod( LDAP::LDAP_MOD_DELETE, 'location', [ 'amsterdam'] ), LDAP.mod( LDAP::LDAP_MOD_REPLACE, 'telephonenumber', [ '+1 408 555 1234', '+1 408 555 5678' ] ), LDAP.mod( LDAP::LDAP_MOD_DELETE, 'office', [] ) ] ) )
dn: uid=ianmacd,dc=foo
changetype: modify
add: mailRoutingAddress
mailRoutingAddress: a
mailRoutingAddress: b
-
delete: location
location: amsterdam
-
replace: telephonenumber
telephonenumber: +1 408 555 1234
telephonenumber: +1 408 555 5678
-
delete: office
LDIF

  # Try passing a single mod to LDAP::LDIF.mods_to_ldif.
  assert_equal( <<LDIF, LDAP::LDIF.mods_to_ldif( 'uid=ianmacd,dc=google,dc=com', LDAP.mod( LDAP::LDAP_MOD_REPLACE, 'telephonenumber', [ '+1 408 555 1234', '+1 408 555 5678' ] ) ) )
dn: uid=ianmacd,dc=google,dc=com
changetype: modify
replace: telephonenumber
telephonenumber: +1 408 555 1234
telephonenumber: +1 408 555 5678
LDIF

  end

end