forked from mavricknz/ldap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ldif_test.go
190 lines (166 loc) · 4.06 KB
/
ldif_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
package ldap
import (
//"bufio"
//"errors"
"fmt"
//"io"
//"os"
"strings"
"testing"
)
var simpleLDIF string = `
version: 1
# comment
dn: cn=bob,ou=people,o=example.com
cn: bob
# comment in entry
description: a multi-line
attribute value
-
dn: cn=joe,ou=people,o=example.com
cn: joe
description:: VGhpcyB0ZXh0IHdhcyBvcmlnaW5hbGx5IGJhc2U2NCBlbmNvZGVkLg==
dn: cn=joe,ou=people,o=example.com
changetype: modify
replace: cn
cn: joe blogs
-
add: sn
sn: clogs
-
dn: cn=joe2,ou=people,o=example.com
changetype: add
cn: joe blogs
dn: cn=joe2,ou=people,o=example.com
changetype: delete
dn: cn=joe3,ou=people,o=example.com
cn: joe3
description: space at end of sn
sn: space at end
dn: cn=joe4,ou=people,o=example.com
cn: joe4
description: space at start of sn
sn: space at start
dn: cn=joe5,ou=people,o=example.com
cn: joe5
description: less than "<" at start of sn
sn: <blogs
dn: cn=joe6,ou=people,o=example.com
cn: joe6
description: utf8 sn Hello World?
sn: 世界
dn: cn=joe7,ou=people,o=example.com
cn: joe7
description: A longish attibute value that should end up being wrapped around if that is enabled.
sn: joe7
dn: cn=joe8,ou=people,o=example.com
cn: joe8
description: <A longish attibute value that should end up being wrapped around if that is enabled, plus base64'ed
sn: joe8
`
func TestLDIFOpenAndRead(t *testing.T) {
fmt.Printf("TestLDIFOpenAndRead: starting...\n")
reader := strings.NewReader(simpleLDIF)
lr, err := NewLDIFReader(reader)
if err != nil {
t.Error(err)
}
// record 0
fmt.Printf("Reading record 0\n")
record, err := lr.ReadLDIFEntry()
if err != nil {
t.Error(err)
}
if record.RecordType() != EntryRecord {
t.Errorf("record 0: record.RecordType() mismatch")
}
entry := record.(*Entry)
if entry.GetAttributeValues("description")[0] != "a multi-line attribute value" {
t.Errorf("record 0: description mismatch")
}
fmt.Printf("0 (entry): DN: %s\n", entry.DN)
// record 1
fmt.Printf("Reading record 1\n")
//LDIFDebug = true
record, err = lr.ReadLDIFEntry()
if err != nil {
t.Error(err)
}
if record.RecordType() != EntryRecord {
t.Errorf("record 1: record.RecordType() mismatch")
}
entry = record.(*Entry)
if entry.GetAttributeValues("description")[0] != "This text was originally base64 encoded." {
t.Errorf("record 1: description mismatch")
}
fmt.Printf("1 (entry): DN: %s\n", entry.DN)
//LDIFDebug = false
// record 2
fmt.Printf("Reading record 2\n")
record, err = lr.ReadLDIFEntry()
if err != nil {
fmt.Println(err)
}
if record.RecordType() != ModifyRecord {
fmt.Errorf("record 2: record.RecordType() mismatch")
}
modRequest := record.(*ModifyRequest)
fmt.Printf("2 (ModifyRequest): DN: %s\n", modRequest.DN)
fmt.Println(modRequest)
// record 3
fmt.Printf("Reading record 3\n")
record, err = lr.ReadLDIFEntry()
if err != nil {
fmt.Println(err)
}
if record.RecordType() != AddRecord {
t.Errorf("record 3: record.RecordType() mismatch")
}
addRequest := record.(*AddRequest)
fmt.Printf("3 (addRequest): DN: %s\n", addRequest.Entry.DN)
// record 4
fmt.Printf("Reading record 4\n")
record, err = lr.ReadLDIFEntry()
if err != nil {
fmt.Println(err)
}
if record.RecordType() != DeleteRecord {
t.Errorf("record 4: record.RecordType() mismatch")
}
deleteRequest := record.(*DeleteRequest)
fmt.Printf("3 (deleteRequest): DN: %s\n", deleteRequest.DN)
// nil record
//fmt.Printf("Reading record 5 (nil)\n")
//record, err = lr.ReadLDIFEntry()
//if err != nil {
// t.Error(err)
//}
//if record != nil {
//t.Errorf("record nil: record was not nil!")
//}
// reading 250K entries ~ 15sec on 4+ year old desktop.
// ldif generated from OpenDJ install
//file, nerr := os.Open("e:/temp/250k.ldif")
//if nerr != nil {
// t.Errorf(nerr)
// return
//}
//defer file.Close()
//bufReader := bufio.NewReader(file)
//lr, err = NewLDIFReader(bufReader)
//if err != nil {
// t.Error(err)
//}
//for {
// record, err = lr.ReadLDIFEntry()
// if err != nil {
// t.Error(err)
// }
// if record == nil {
// break
// }
// entry := record.(*Entry)
// fmt.Println(entry.DN)
// fmt.Println(entry.GetAttributeValue("entryUUID"))
//}
}