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

dugan
23.57.2006 1c72a61779b04f2d5e2f281085ddc36a1778e754
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
/*
 * 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 Sun Microsystems, Inc.
 */
package org.opends.server.protocols.ldap ;
 
import org.opends.server.DirectoryServerTestCase;
import org.opends.server.protocols.asn1.ASN1Boolean;
import org.opends.server.protocols.asn1.ASN1Element;
import org.opends.server.protocols.asn1.ASN1Long;
import org.opends.server.protocols.asn1.ASN1Sequence;
import org.testng.annotations.Test;
 
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.ListIterator;
 
/**
 * An abstract class that all types  unit test should extend.
 */
 
@Test(groups = { "precommit", "ldap" })
public abstract class LdapTestCase extends DirectoryServerTestCase
{
  /**
   * Determine whether one LDAPAttribute is equal to another.
   * The values of the attribute must be identical and in the same order.
   * @param a1 The first LDAPAttribute.
   * @param a2 The second LDAPAttribute.
   * @return true if the first LDAPAttribute is equal to the second.
   */
  static boolean testEqual(LDAPAttribute a1, LDAPAttribute a2)
  {
    if (!a1.getAttributeType().equals(a2.getAttributeType()))
    {
      return false;
    }
    return a1.getValues().equals(a2.getValues());
  }
 
  /**
   * Determine whether one list of LDAPAttribute is equal to another.
   * @param list1 The first list of LDAPAttribute.
   * @param list2 The second list of LDAPAttribute.
   * @return true if the first list of LDAPAttribute is equal to the second.
   */
  static boolean testEqual(LinkedList<LDAPAttribute> list1,
                           LinkedList<LDAPAttribute> list2)
  {
    ListIterator<LDAPAttribute> e1 = list1.listIterator();
    ListIterator<LDAPAttribute> e2 = list2.listIterator();
    while(e1.hasNext() && e2.hasNext()) {
      LDAPAttribute o1 = e1.next();
      LDAPAttribute o2 = e2.next();
      if (!(o1==null ? o2==null : testEqual(o1, o2)))
        return false;
    }
    return !(e1.hasNext() || e2.hasNext());
  }
 
  /**
   * Generate an exception by writing a long into a integer element.
   * @param op The op.
   * @param type The type of sequence.
   * @param index The index into the element to write to.
   * @throws Exception If the protocol op decode can't write the sequence.
   */
  static void 
  badIntegerElement(ProtocolOp op, byte type, int index) throws Exception {
      ASN1Element element = op.encode();
      ArrayList<ASN1Element> elements = ((ASN1Sequence)element).elements();
      elements.set(index, new ASN1Long(Long.MAX_VALUE));
      ProtocolOp.decode(new ASN1Sequence(type, elements));
  }
 
  /**
   * Generate an exception by adding an element.
   * @param op The op.
   * @param type The type of sequence.
   * @throws Exception If the protocol op decode has too many elements.
   */
  static void 
  tooManyElements(ProtocolOp op, byte type) throws Exception
  {
      ASN1Element element = op.encode();
      ArrayList<ASN1Element> elements = ((ASN1Sequence)element).elements();
      elements.add(new ASN1Boolean(true));
      ProtocolOp.decode(new ASN1Sequence(type, elements));
  }
 
  /**
   * Generate an exception by removing an element.
   * @param op The op.
   * @param type The type of sequence.
   * @throws Exception If the protocol op decode has too few elements.
   */
  static void 
  tooFewElements(ProtocolOp op, byte type) throws Exception
  {
      ASN1Element element = op.encode();
      ArrayList<ASN1Element> elements = ((ASN1Sequence)element).elements();
      elements.remove(0);
      ProtocolOp.decode(new ASN1Sequence(type, elements));
  }
 
  /**
   * Test toString methods.
   * @param op The op.
   * @throws Exception If the toString method fails.
   */
  static void 
  toString(ProtocolOp op) throws Exception {
      StringBuilder sb = new StringBuilder();
      op.toString(sb);
      op.toString(sb, 1);
  }
}