mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

davidely
02.00.2007 a5ce1b53bf9304c08bb51639b48bb77085cd62b3
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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE
 * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
 * add the following below this CDDL HEADER, with the fields enclosed
 * by brackets "[]" replaced with your own identifying information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *
 *      Portions Copyright 2006-2007 Sun Microsystems, Inc.
 */
package org.opends.server.protocols.asn1;
 
 
 
import java.util.ArrayList;
 
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
 
import static org.testng.Assert.*;
 
 
 
/**
 * This class defines a set of tests for the
 * org.opends.server.protocols.asn1.ASN1Set class.
 */
public class TestASN1Set
       extends ASN1TestCase
{
  // The array of element arrays to use for the testing.  Each element is a
  // single-element array containing a byte[] with the encoded elements.
  private Object[][] elementArrays;
 
  // The array of elements to use for the testing.  Each element is a
  // single-element array containing an ArrayList<ASN1Element>.
  private Object[][] elementLists;
 
 
 
  /**
   * Constructs the element lists that will be used to perform the testing.
   */
  @BeforeClass()
  public void populateElementLists()
  {
    ArrayList<ArrayList<ASN1Element>> lists =
         new ArrayList<ArrayList<ASN1Element>>();
 
    // Add a null element.
    lists.add(null);
 
    // Add an empty list.
    lists.add(new ArrayList<ASN1Element>());
 
    // Create an array of single elements, and add each of them in their own
    // lists.
    ASN1Element[] elementArray =
    {
      new ASN1OctetString(),
      new ASN1OctetString((byte) 0x50),
      new ASN1OctetString(new byte[50]),
      new ASN1OctetString("Hello"),
      new ASN1Element((byte) 0x50),
      new ASN1Element((byte) 0x50, new byte[50]),
      new ASN1Boolean(false),
      new ASN1Boolean(true),
      new ASN1Enumerated(0),
      new ASN1Enumerated(1),
      new ASN1Enumerated(127),
      new ASN1Enumerated(128),
      new ASN1Enumerated(255),
      new ASN1Enumerated(256),
      new ASN1Integer(0),
      new ASN1Integer(1),
      new ASN1Integer(127),
      new ASN1Integer(128),
      new ASN1Integer(255),
      new ASN1Integer(256),
      new ASN1Long(0),
      new ASN1Long(1),
      new ASN1Long(127),
      new ASN1Long(128),
      new ASN1Long(255),
      new ASN1Long(256),
      new ASN1Null(),
      new ASN1Sequence(),
      new ASN1Set()
    };
 
    // Add lists of single elements.
    for (ASN1Element e : elementArray)
    {
      ArrayList<ASN1Element> list = new ArrayList<ASN1Element>(1);
      list.add(e);
      lists.add(list);
    }
 
 
    // Create multi-element lists based on the single-element lists.
    for (int i=0; i < elementArray.length; i++)
    {
      ArrayList<ASN1Element> list = new ArrayList<ASN1Element>(i+1);
      for (int j=0; j <=i; j++)
      {
        list.add(elementArray[j]);
      }
      lists.add(list);
    }
 
 
    // Convert the lists into object arrays.
    elementLists = new Object[lists.size()][1];
    for (int i=0; i < elementLists.length; i++)
    {
      elementLists[i] = new Object[] { lists.get(i) };
    }
 
    lists.remove(null);
    elementArrays = new Object[lists.size()][1];
    for (int i=0; i < elementArrays.length; i++)
    {
      elementArrays[i] = new Object[] { ASN1Element.encodeValue(lists.get(i)) };
    }
  }
 
 
 
  /**
   * Retrieves lists of byte arrays that can be used to construct sets.
   *
   * @return  Lists of byte arrays that can be used to construct sets.
   */
  @DataProvider(name = "elementArrays")
  public Object[][] getElementArrays()
  {
    return elementArrays;
  }
 
 
 
  /**
   * Retrieves lists of ASN.1 elements that can be used to construct sets.
   *
   * @return  Lists of ASN.1 elements that can be used to construct sets.
   */
  @DataProvider(name = "elementLists")
  public Object[][] getElementLists()
  {
    return elementLists;
  }
 
 
 
  /**
   * Tests the first constructor, which doesn't take any arguments.
   */
  @Test()
  public void testConstructor1()
  {
    new ASN1Set();
  }
 
 
 
  /**
   * Create the values that can be used for testing BER types.
   *
   * @return  The values that can be used for testing BER types.
   */
  @DataProvider(name = "types")
  public Object[][] getTypes()
  {
    // Create an array with all of the valid single-byte types.  We don't
    // support multi-byte types, so this should be a comprehensive data set.
    Object[][] testTypes = new Object[0xFF][1];
    for (int i=0x00; i < 0xFF; i++)
    {
      testTypes[i] = new Object[] { (byte) (i & 0xFF) };
    }
 
    return testTypes;
  }
 
 
 
  /**
   * Tests the second constructor, which takes a byte argument.
   *
   * @param  b  The BER type to use for the set.
   */
  @Test(dataProvider = "types")
  public void testConstructor2(byte b)
  {
    new ASN1Set(b);
  }
 
 
 
  /**
   * Tests the third constructor, which takes a list of elements.
   *
   * @param  elements  The list of elements to use to create the set.
   */
  @Test(dataProvider = "elementLists")
  public void testConstructor3(ArrayList<ASN1Element> elements)
  {
    new ASN1Set(elements);
  }
 
 
 
  /**
   * Tests the third constructor, which takes a byte and a list of elements.
   *
   * @param  elements  The list of elements to use to create the set.
   */
  @Test(dataProvider = "elementLists")
  public void testConstructor4(ArrayList<ASN1Element> elements)
  {
    for (int i=0; i < 255; i++)
    {
      new ASN1Set((byte) (i & 0xFF), elements);
    }
  }
 
 
 
  /**
   * Tests the <CODE>elements</CODE> method.
   *
   * @param  elements  The list of elements to use to create the set.
   */
  @Test(dataProvider = "elementLists")
  public void testGetElements(ArrayList<ASN1Element> elements)
  {
    ASN1Set s = new ASN1Set(elements);
    if (elements == null)
    {
      assertEquals(s.elements(), new ArrayList<ASN1Element>());
    }
    else
    {
      assertEquals(s.elements(), elements);
    }
  }
 
 
 
  /**
   * Tests the <CODE>setElements</CODE> method.
   *
   * @param  elements  The list of elements to use to create the set.
   */
  @Test(dataProvider = "elementLists")
  public void testSetElements(ArrayList<ASN1Element> elements)
  {
    ASN1Set s = new ASN1Set();
    s.setElements(elements);
    if (elements == null)
    {
      assertEquals(s.elements(), new ArrayList<ASN1Element>());
    }
    else
    {
      assertEquals(s.elements(), elements);
    }
  }
 
 
 
  /**
   * Tests the <CODE>setValue</CODE> method with valid values.
   *
   * @param  encodedElements  The byte array containing the encoded elements to
   *                          use in the value.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(dataProvider = "elementArrays")
  public void testSetValueValid(byte[] encodedElements)
         throws Exception
  {
    ASN1Set s = new ASN1Set();
    s.setValue(encodedElements);
  }
 
 
 
  /**
   * Tests the <CODE>setValue</CODE> method with a null array.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testSetValueNull()
         throws Exception
  {
    ASN1Set s = new ASN1Set();
    s.setValue(null);
  }
 
 
 
  /**
   * Retrieves a set of byte arrays containing invalid element value encodings.
   *
   * @return  A set of byte arrays containing invalid element value encodings.
   */
  @DataProvider(name = "invalidElementArrays")
  public Object[][] getInvalidArrays()
  {
    return new Object[][]
    {
      new Object[] { new byte[] { 0x05 } },
      new Object[] { new byte[] { 0x05, 0x01 } },
      new Object[] { new byte[] { 0x05, (byte) 0x85, 0x00, 0x00, 0x00, 0x00,
                                  0x00 } },
      new Object[] { new byte[] { 0x05, (byte) 0x82, 0x00 } },
      new Object[] { new byte[] { 0x05, 0x00, 0x05 } },
      new Object[] { new byte[] { 0x05, 0x00, 0x05, 0x01 } },
    };
  }
 
 
 
  /**
   * Tests the <CODE>setValue</CODE> method with valid values.
   *
   * @param  encodedElements  The byte array containing the encoded elements to
   *                          use in the value.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(dataProvider = "invalidElementArrays",
        expectedExceptions = { ASN1Exception.class })
  public void testSetValueInvalid(byte[] invalidElements)
         throws Exception
  {
    ASN1Set s = new ASN1Set();
    s.setValue(invalidElements);
  }
 
 
 
  /**
   * Tests the <CODE>decodeAsSet</CODE> method that takes an ASN1Element
   * argument with valid elements.
   *
   * @param  encodedElements  Byte arrays that may be used as valid values for
   *                          encoded elements.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(dataProvider = "elementArrays")
  public void testDecodeValidElementAsSet(byte[] encodedElements)
         throws Exception
  {
    ASN1Element e = new ASN1Element(ASN1Constants.UNIVERSAL_SET_TYPE,
                                    encodedElements);
    ASN1Set.decodeAsSet(e);
  }
 
 
 
  /**
   * Tests the <CODE>decodeAsSet</CODE> method that takes an ASN1Element
   * argument with valid elements.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testDecodeNullElementAsSet()
         throws Exception
  {
    ASN1Element e = null;
    ASN1Set.decodeAsSet(e);
  }
 
 
 
  /**
   * Tests the <CODE>decodeAsSet</CODE> method that takes a byte array argument
   * with valid arrays.
   *
   * @param  encodedElements  Byte arrays that may be used as valid values for
   *                          encoded elements.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(dataProvider = "elementArrays")
  public void testDecodeValidArrayAsSet(byte[] encodedElements)
         throws Exception
  {
    byte[] encodedLength = ASN1Element.encodeLength(encodedElements.length);
    byte[] elementBytes  =
         new byte[1 + encodedLength.length + encodedElements.length];
    elementBytes[0] = ASN1Constants.UNIVERSAL_SET_TYPE;
    System.arraycopy(encodedLength, 0, elementBytes, 1, encodedLength.length);
    System.arraycopy(encodedElements, 0, elementBytes, 1+encodedLength.length,
                     encodedElements.length);
    ASN1Set.decodeAsSet(elementBytes);
  }
 
 
 
  /**
   * Tests the <CODE>decodeAsSet</CODE> method that takes a byte array argument
   * with a null array.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testDecodeNullArrayAsSet()
         throws Exception
  {
    byte[] b = null;
    ASN1Set.decodeAsSet(b);
  }
 
 
 
  /**
   * Tests the <CODE>decodeAsSet</CODE> method that takes a byte array argument
   * with a short array.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testDecodeShortArrayAsSet()
         throws Exception
  {
    byte[] b = new byte[1];
    ASN1Set.decodeAsSet(b);
  }
 
 
 
  /**
   * Tests the <CODE>decodeAsSet</CODE> method that takes a byte array argument
   * with an array that takes too many bytes to encode the length.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testDecodeLongLengthArrayAsSet()
         throws Exception
  {
    byte[] b = { 0x30, (byte) 0x85, 0x00, 0x00, 0x00, 0x00, 0x00 };
    ASN1Set.decodeAsSet(b);
  }
 
 
 
  /**
   * Tests the <CODE>decodeAsSet</CODE> method that takes a byte array argument
   * with an array that doesn't fully describe the length.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testDecodeTruncatedLengthArrayAsSet()
         throws Exception
  {
    byte[] b = { 0x30, (byte) 0x82, 0x00 };
    ASN1Set.decodeAsSet(b);
  }
 
 
 
  /**
   * Tests the <CODE>decodeAsSet</CODE> method that takes a byte array argument
   * with an array whose decoded length doesn't match the real length.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(expectedExceptions = { ASN1Exception.class })
  public void testDecodeLengthMismatchArrayAsSet()
         throws Exception
  {
    byte[] b = { 0x30, 0x01 };
    ASN1Set.decodeAsSet(b);
  }
 
 
 
  /**
   * Tests the <CODE>toString</CODE> method that takes a string builder
   * argument.
   *
   * @param  elements  The list of elements to use to create the set.
   */
  @Test(dataProvider = "elementLists")
  public void testToString1(ArrayList<ASN1Element> elements)
  {
    new ASN1Set(elements).toString(new StringBuilder());
  }
 
 
 
  /**
   * Tests the <CODE>toString</CODE> method that takes string builder and
   * integer arguments.
   *
   * @param  elements  The list of elements to use to create the set.
   */
  @Test(dataProvider = "elementLists")
  public void testToString2(ArrayList<ASN1Element> elements)
  {
    new ASN1Set(elements).toString(new StringBuilder(), 1);
  }
}