aboutsummaryrefslogtreecommitdiffstats
path: root/Tests.st
blob: eb683deef99dbac812ae306cfc58d870499c4b77 (plain)
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
"======================================================================
|
| Copyright (c) 2004-2009
| Ragnar Hojland Espinosa <ragnar@ragnar-hojland.com>,
|
| Contributions by:
| Göran Krampe
| Andreas Raab
|
| Ported by:
| Stephen Woolerton 
| 
| Permission is hereby granted, free of charge, to any person obtaining
| a copy of this software and associated documentation files (the 
| 'Software'), to deal in the Software without restriction, including 
| without limitation the rights to use, copy, modify, merge, publish, 
| distribute, sublicense, and/or sell copies of the Software, and to 
| permit persons to whom the Software is furnished to do so, subject to 
| the following conditions:
|
| The above copyright notice and this permission notice shall be 
| included in all copies or substantial portions of the Software.
|
| THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 
| EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
| MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
| IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 
| CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 
| TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
| SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  
|
 ======================================================================"
 
TestCase subclass: BERTest [

    BERTest class >> getBooleanTestSet [
	^  {		{ 0 . '010100' }. 
			{ 1 . '0101FF' }.
			{ 255 . '0101FF' }.
			{ 1000 . '0101FF' }.
			{ false . '010100' }.
			{ true . '0101FF' }.
		} 
    ]

    testBooleanEncoding [
	|ber stream tests output |
	ber := BERBoolean new.
	stream := ReadWriteStream on: (String new).

	tests :=  self class getBooleanTestSet.
	tests do: [:test | 
		stream := ReadWriteStream on: (String new).
		ber value: (test at: 1).	
		 Transcript cr; showCr: ('value: %1, BooleanEncoded: %2' bindWith: (test at: 1) with: (test at: 2)).		
		ber writeOn: stream.
		output :=  self stringHex: stream contents asString.
		Transcript show: 'Expected: ', (test at: 2), ' Got: ', output; cr.
		self assert: (output = (test at: 2)) ]
    ]

    stringHex: aString [
	| stream |
	stream := WriteStream on: (String new: self size * 4).
	aString do: [ :ch | stream nextPutAll: (self charHex: ch) ].
	^stream contents
    ]

    charHex: ch [
	| hexVal |
	^(ch value < 16)
	    ifTrue:  ['0',(ch value printString: 16)]
	    ifFalse: [ch value printString: 16]
    ]

    BERTest class >> getIntegerTestSet [
	^  {		{27066 .  '020269BA'}. 
			{-27066 .  '02029646'}.
			{72 .  '020148' }.
			{127  . '02017F'}.
			{-128. '020180'}.
			{128  . '02020080'}.
			{ 0 . '020100' }.
			{ 256  .  '02020100'}.
			{4294967290  . '020500FFFFFFFA'}.
			{ 1  . '020101'}.
			{-1 . '0201FF'}.
			{ -129  . '0202FF7F'}.
		} 
    ]

    testIntegerEncoding [
    | ber byte stream tests output valueStream value |
	
	ber := BERInteger new.
	stream := ReadWriteStream on: (String new).

	tests := self class getIntegerTestSet.


	tests do: [:test | 
		valueStream := ReadStream on: (test at: 2).
		value := test at: 1.
		"made stream a string as notthing in it. Have found asCharacter
		is the problem so TODO is put stream declaration back how it was"
		stream := ReadWriteStream on: (String new).
		 Transcript cr; showCr: 'value: ', value printString, '  IntegerEncoded: ',valueStream contents.		

		[valueStream atEnd] whileFalse: [
			byte := (valueStream next digitValue ) * 16.
			byte := byte + valueStream next digitValue.
			"code below, don't use 'byte asCharacter' since if 
			 value >127 get UnicodeCharacter returned"
			stream nextPut: (Character value: byte) ] .
		stream reset.
		ber := BERInteger newFrom: stream.
		"(ber class = BERInteger) ifTrue: [Transcript showCr: 'isBERInt']."
		 Transcript showCr: 'Expected: ', (value printString),' Got: ', (ber value printString).
		self assert: (ber value = value )
	] 
    ]

testOctetStringEncoding [
	|ber stream tests|
	ber := BEROctetString new.
	stream := ReadWriteStream on: (String new).

	tests := {	{ 'hello' . 5 . '040568656C6C6F' } }.

	tests do: [:test | 
		stream := ReadWriteStream on: (String new).
		ber value: (test at: 1).	
		ber writeOn: stream.
		self assert: ((self stringHex: stream contents asString) = (test at: 3)) ]
]

    testSequenceEncoding [
	|ber0 ber1 ber2 stream|
	ber0 := BERSequence new.
	ber1 := BERInteger new value: 17.
	ber2 := BERInteger new value: 170.
	
	ber0 addElement: ber1.
	ber0 addElement: ber2.
	stream := ReadWriteStream on: (String new).

	ber0 writeOn: stream.
	'' displayNl.'Sequence Encoding Test ...' displayNl. stream contents inspect displayNl.
	"self assert: (stream contents asString asHex = '3007020111020200AA') "
	self assert: ((self stringHex: (stream contents asString))  = '3007020111020200AA')  
    ]




    testIntegerDecoding [
	"changes are
	1. no stream reset command in GST so just reinitialize same as the first time
	2. No asCharacter, use Character value: byte instread
	3. Instead as asString in the Transcript, use printString"

	|ber stream tests value valueStream byte |
	stream := ReadWriteStream on: (String new).

	tests :=  self class getIntegerTestSet.
	'' displayNl.'Integer Decoding Test ...' displayNl. 
	tests do: [:test | 
		valueStream := ReadStream on: (test at: 2).
		value := test at: 1.
		stream := ReadWriteStream on: (String new).
		
		[valueStream atEnd] whileFalse: [
			byte := (valueStream next digitValue * 16).
			byte := byte + valueStream next digitValue.
			"stream nextPut: (byte asCharacter)
			code below, don't use 'byte asCharacter' since if
                         value >127 get UnicodeCharacter returned"
                        stream nextPut: (Character value: byte) ] .
		
		stream reset.
		ber := BERInteger newFrom: stream.
		"self assert: (ber class = BERInteger)."
		
		Transcript show: 'Expected: ', (value printString), ' Got: ', (ber value printString); cr. "stream contents inspect displayNl."
		self assert: (ber value = value ) 
	]
    ]

    testBindRequest [
        | encoded |
	'' displayNl.'Beginning testBindRequest...' displayNl. 

        encoded _ LDAPEncoder bindRequest: 1 username: 'cn=admin,dc=linalco,dc=test' credentials: 'secret' method: nil.
        "encoded _ encoded asString asHex."
	encoded := self stringHex: encoded asString.

        Transcript show: 'testBindRequest got: ', encoded; cr.
        self assert: (encoded = '302D0201016028020103041B636E3D61646D696E2C64633D6C696E616C636F2C64633D746573748006736563726574')
    ]

 

  testBindRequestHere [
    | stream mesg req encoded |
    stream := ReadWriteStream on: String new.
    mesg := BERSequence new.
    mesg addElement: (BERInteger new value: 1).

        req := BERSequence new tagSetApplication.
	req addElement: (BERInteger new value: 3).
        req addElement: (BEROctetString new value: 'cn=admin,dc=linalco,dc=test').
        req addElement: ((BEROctetString new)
                    tagSetContext;
                    value: 'secret')
            withTag: 0.
        mesg addElement: req withTag: 0.
        mesg writeOn: stream.
        encoded := stream contents.
	encoded inspect.
	encoded := self stringHex: encoded asString.

        Transcript show: 'testBindRequest got: ', encoded; cr.
        self assert: (encoded = '302D0201016028020103041B636E3D61646D696E2C64633D6C696E616C636F2C64633D746573748006736563726574')
    ]

   testAddRequest [
       | encoded attrs |
       attrs := Dictionary new.
       attrs at: 'objectClass' put: (OrderedCollection new addLast: 'person'; yourself).                                                                        
       attrs at: 'cn' put: (OrderedCollection new addLast: 'test2'; yourself).  
       attrs at: 'sn' put: (OrderedCollection new addLast: 'test2'; yourself).  

       encoded := LDAPEncoder addRequest: 1 dn: 'cn=test2,dc=linalco,dc=test' attrs: attrs.                                                                     
       encoded := self stringHex: encoded asString ."original code uses an 'asHex' method"                                                                      

       '' displayNl. Transcript show: 'testAddRequest got: ', encoded; cr; cr.
       self assert: (encoded = '305B0201016856041B636E3D74657374322C64633D6C696E616C636F2C64633D7465737430373017040B6F626A656374436C61737331080406706572736F6E300D0402636E310704057465737432300D0402736E310704057465737432')                    
   ]                                                                            

   testDelRequest [
       | encoded | 
       encoded := LDAPEncoder delRequest: 1 dn: 'cn=test2,dc=linalco,dc=test'.
       encoded := self stringHex: encoded asString ."original code uses an 'asHex' method"                                                                      

       Transcript show: 'testDelRequest got: ', encoded; cr; cr.
       self assert: (encoded = '30200201014A1B636E3D74657374322C64633D6C696E616C636F2C64633D74657374')                                                          
   ]                                                                            

   testModifyRequest [
       | encoded ops |
       ops :=  {      
                   LDAPAttrModifier set: 'sn' to: { 'test5sn' . 'foo' . 'bar' } .
                   LDAPAttrModifier addTo: 'description' values: {'rchueo'} }.
       encoded := LDAPEncoder modifyRequest: 1 dn: 'cn=test5,dc=linalco,dc=test' ops: ops.                                                                      
       encoded := self stringHex: encoded asString ."original code uses an 'asHex' method"                                                                      

       Transcript show: 'testModifyRequest got: ', encoded; cr.
       self assert: (encoded = '3062020101665D041B636E3D74657374352C64633D6C696E616C636F2C64633D74657374303E301E0A010230190402736E311304077465737435736E0403666F6F0403626172301C0A01003017040B6465736372697074696F6E3108040672636875656F')      

   ]

   testSearchRequest [
       | encoded |    
       encoded := LDAPEncoder searchRequest: 1 base: 'dc=linalco, dc=test' scope: (LDAPConnection wholeSubtree) deref: (LDAPConnection derefNever) filter: (LDAPFilter with: 'objectclass' ) attrs: (OrderedCollection new) wantAttrsOnly: false.                                                                               
       encoded := self stringHex: encoded asString ."original code uses an 'asHex' method"                                                                      

       '' displayNl. Transcript show: 'testSearchRequest got: ', encoded; cr.
       self assert: (encoded = '30380201016333041364633D6C696E616C636F2C2064633D746573740A01020A0100020100020100010100870B6F626A656374636C6173733000')          
   ]


]

" ------------------------------------- "
| suite tester |
suite := TestSuite named: 'Set Tests'.
suite addTest: (BERTest selector: #testIntegerEncoding).
suite addTest: (BERTest selector: #testBooleanEncoding).
suite addTest: (BERTest selector: #testOctetStringEncoding).
suite addTest: (BERTest selector: #testSequenceEncoding).
suite addTest: (BERTest selector: #testIntegerDecoding).
suite addTest: (BERTest selector: #testBindRequest) .
suite addTest: (BERTest selector: #testAddRequest).
suite addTest: (BERTest selector: #testDelRequest).
suite addTest: (BERTest selector: #testModifyRequest).
suite addTest: (BERTest selector: #testSearchRequest).