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

vharseko
12.41.2023 0de6858e9548e84aebc148747be92a811ba33636
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
/*
 * The contents of this file are subject to the terms of the Common Development and
 * Distribution License (the License). You may not use this file except in compliance with the
 * License.
 *
 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
 * specific language governing permission and limitations under the License.
 *
 * When distributing Covered Software, include this CDDL Header Notice in each file and include
 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
 * Header, with the fields enclosed by brackets [] replaced by your own identifying
 * information: "Portions Copyright [year] [name of copyright owner]".
 *
 * Copyright 2013-2014 Manuel Gaupp
 * Portions Copyright 2014-2016 ForgeRock AS.
 */
package org.opends.server.protocols.asn1;
 
import static org.testng.Assert.*;
 
import org.opends.server.DirectoryServerTestCase;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
 
/** This class tests the GSERParser. */
public class GSERParserTestCase extends DirectoryServerTestCase
{
  /** Try to create a GSER Parser with <CODE>null</CODE> as parameter. */
  @Test(expectedExceptions = NullPointerException.class)
  public void testGSERParserInitWithNull () throws Exception
  {
    new GSERParser(null);
  }
 
  /** Test the <CODE>hasNext</CODE> method. */
  @Test
  public void testHasNext() throws Exception
  {
    GSERParser parser = new GSERParser("0");
    assertTrue(parser.hasNext());
    assertEquals(parser.nextInteger(),0);
    assertFalse(parser.hasNext());
  }
 
  /** Test the <CODE>skipSP</CODE> method. */
  @Test
  public void testSkipSP() throws Exception
  {
    String[] values = {" 42","  42","42"};
    for (String value : values)
    {
      GSERParser parser = new GSERParser(value);
      assertEquals(parser.skipSP().nextInteger(),42);
      assertFalse(parser.hasNext());
    }
  }
 
  /** Test the <CODE>skipMSP</CODE> method. */
  @Test
  public void testSkipMSP() throws Exception
  {
    String[] values = {" 42","  42","           42"};
    for (String value : values)
    {
      GSERParser parser = new GSERParser(value);
      assertEquals(parser.skipMSP().nextInteger(),42);
      assertFalse(parser.hasNext());
    }
  }
 
  /** Verify that <CODE>skipMSP</CODE> requires at least one space. */
  @Test(expectedExceptions = GSERException.class)
  public void testSkipMSPwithZeroSpaces() throws Exception
  {
    GSERParser parser = new GSERParser("42");
    parser.skipMSP();
  }
 
  /** Create data for the <CODE>testSequence</CODE> test case. */
  @DataProvider(name="sequenceValues")
  public Object[][] createSequenceValues()
  {
    return new Object[][] {
      {"{123,122}", true},
      {"{ 123,1}", true },
      {"{ 123   ,   1   }", true },
      {"{0123,}", false},
      {"{0123 42 }", false},
      {"{123  , 11 ", false},
      {" {123  , 11 ", false},
      {" 123  , 11}", false}
    };
  }
 
  /** Test sequence parsing. */
  @Test(dataProvider="sequenceValues")
  public void testSequence(String value, boolean expectedResult) throws Exception
  {
    GSERParser parser = new GSERParser(value);
    boolean result = true;
    try
    {
      parser.readStartSequence();
      parser.nextInteger();
      parser.skipSP().skipSeparator();
      parser.nextInteger();
      parser.readEndSequence();
      if (parser.hasNext())
      {
        result = false;
      }
    }
    catch (GSERException e)
    {
      result = false;
    }
    assertEquals(expectedResult,result);
  }
 
  /** Create data for the <CODE>testString</CODE> test case. */
  @DataProvider(name="stringValues")
  public Object[][] createStringValues()
  {
    return new Object[][] {
      {"\"\"", true},
      {"\"escaped\"\"dquotes\"", true },
      {"\"valid Unicode \u00D6\u00C4\"", true },
      {"\"only one \" \"", false},
      {"invalid without dquotes", false},
      {"\"missing end", false},
      {"\"valid string\" with extra trailing characters", false}
    };
  }
 
  /** Test the parsing of String values. */
  @Test(dataProvider="stringValues")
  public void testString(String value, boolean expectedResult) throws Exception
  {
    GSERParser parser = new GSERParser(value);
    boolean result = true;
    try
    {
      assertNotNull(parser.nextString());
      if (parser.hasNext())
      {
        result = false;
      }
    }
    catch (GSERException e)
    {
      result = false;
    }
    assertEquals(expectedResult,result);
  }
 
  /** Create data for the <CODE>testInteger</CODE> test case. */
  @DataProvider(name="integerValues")
  public Object[][] createIntegerValues()
  {
    return new Object[][] {
      {"0123456", true},
      {"42", true},
      {"0", true },
      {"", false},
      {"0xFF", false},
      {"NULL", false},
      {"Not a Number", false}
    };
  }
 
  /** Create data for the <CODE>testBigInteger</CODE> test case. */
  @DataProvider(name="bigIntegerValues")
  public Object[][] createBigIntegerValues()
  {
    return new Object[][] {
      {"0123456", true},
      {"42", true},
      {"0", true },
      {"", false},
      {"0xFF", false},
      {"NULL", false},
      {"Not a Number", false},
      {"2147483648",true}
    };
  }
 
  /** Test the parsing of Integer values. */
  @Test(dataProvider="integerValues")
  public void testInteger(String value, boolean expectedResult) throws Exception
  {
    GSERParser parser = new GSERParser(value);
    boolean result = true;
    try
    {
      parser.nextInteger();
      if (parser.hasNext())
      {
        result = false;
      }
    }
    catch (GSERException e)
    {
      result = false;
    }
    assertEquals(expectedResult,result);
  }
 
  /** Test the parsing of BigInteger values. */
  @Test(dataProvider="bigIntegerValues")
  public void testBigInteger(String value, boolean expectedResult) throws Exception
  {
    GSERParser parser = new GSERParser(value);
    boolean result = true;
    try
    {
      parser.nextBigInteger();
      if (parser.hasNext())
      {
        result = false;
      }
    }
    catch (GSERException e)
    {
      result = false;
    }
    assertEquals(expectedResult,result);
  }
 
  /** Create data for the <CODE>testNamedValueIdentifier</CODE> test case. */
  @DataProvider(name="namedValueIdentifierValues")
  public Object[][] createNamedValueIdentifierValues()
  {
    return new Object[][] {
      {"serialNumber ", true},
      {"issuer ", true},
      {"Serialnumber ", false},
      {"0serialnumber ", false},
      {"serial Number ", false},
      {"missingSpace",false}
    };
  }
 
  /** Test the parsing of NamedValue identifiers. */
  @Test(dataProvider="namedValueIdentifierValues")
  public void testNamedValueIdentifier(String value, boolean expectedResult) throws Exception
  {
    GSERParser parser = new GSERParser(value);
    boolean result = true;
    try
    {
      assertNotNull(parser.nextNamedValueIdentifier());
      if (parser.hasNext())
      {
        result = false;
      }
    }
    catch (GSERException e)
    {
      result = false;
    }
    assertEquals(expectedResult,result);
  }
 
  /** Create data for the <CODE>testIdentifiedChoiceIdentifier</CODE> test case. */
  @DataProvider(name="identifiedChoicdeIdentifierValues")
  public Object[][] createIdentifiedChoicdeIdentifierValues()
  {
    return new Object[][] {
      {"serialNumber:", true},
      {"issuer1:", true},
      {"Serialnumber:", false},
      {"0serialnumber:", false},
      {"serial Number:", false},
      {"missingColon",false}
    };
  }
 
  /** Test the parsing of IdentifiedChoice identifiers. */
  @Test(dataProvider="identifiedChoicdeIdentifierValues")
  public void testIdentifiedChoicdeIdentifier(String value, boolean expectedResult) throws Exception
  {
    GSERParser parser = new GSERParser(value);
    boolean result = true;
    try
    {
      assertNotNull(parser.nextChoiceValueIdentifier());
      if (parser.hasNext())
      {
        result = false;
      }
    }
    catch (GSERException e)
    {
      result = false;
    }
    assertEquals(expectedResult,result);
  }
}