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
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
/*
 * 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 2009-2010 Sun Microsystems, Inc.
 * Portions Copyright 2010-2016 ForgeRock AS.
 */
package org.opends.server.schema;
 
import static org.assertj.core.api.Assertions.*;
import static org.opends.server.TestCaseUtils.*;
import static org.opends.server.schema.GeneralizedTimeSyntax.format;
import static org.opends.server.schema.SchemaConstants.*;
import static org.testng.Assert.*;
 
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Set;
import java.util.TimeZone;
 
import org.forgerock.opendj.ldap.Assertion;
import org.forgerock.opendj.ldap.AttributeDescription;
import org.forgerock.opendj.ldap.ByteString;
import org.forgerock.opendj.ldap.ConditionResult;
import org.forgerock.opendj.ldap.DN;
import org.forgerock.opendj.ldap.DecodeException;
import org.forgerock.opendj.ldap.schema.AttributeType;
import org.forgerock.opendj.ldap.schema.MatchingRule;
import org.opends.server.TestCaseUtils;
import org.opends.server.types.Attribute;
import org.opends.server.types.Entry;
import org.opends.server.types.FilterType;
import org.opends.server.types.SearchFilter;
import org.opends.server.util.TimeThread;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
 
/** This class tests various time-based matching rules. */
@SuppressWarnings("javadoc")
public final class TimeBasedMatchingRuleTest
        extends SchemaTestCase
{
  /** User DNs to be used in tests. */
  private DN user1;
  private DN user2;
  private DN user3;
  private DN user4;
  private DN user5;
 
  private static final String TIME_ATTR = "test-time";
  private static final String DATE_ATTR = "test-date";
 
  private static final String TEST_TIME_DEF = "( test-time-oid NAME 'test-time' DESC 'Test time attribute'  EQUALITY "
      + "generalizedTimeMatch ORDERING  generalizedTimeOrderingMatch "
      + "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 SINGLE-VALUE )";
  private static final String TEST_DATE_DEF = "( test-date-oid NAME 'test-date' DESC 'Test date attribute'  EQUALITY "
      + "generalizedTimeMatch ORDERING  generalizedTimeOrderingMatch "
      + "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 SINGLE-VALUE )";
  private static final String TEST_OC_DEF = "( testoc-oid NAME 'testOC' SUP top AUXILIARY MUST test-time)";
  private static final String TEST_OC2_DEF = "( testoc2-oid NAME 'testOC2' SUP top AUXILIARY MUST test-date)";
 
 
  /**
   * Ensures that the Directory Server is running before executing the
   * testcases.
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @BeforeClass
  public void startServer()
         throws Exception
  {
    user1 = DN.valueOf("cn=user1,dc=example,dc=com");
    user2 = DN.valueOf("cn=user2,dc=example,dc=com");
    user3 = DN.valueOf("cn=user3,dc=example,dc=com");
    user4 = DN.valueOf("cn=user4,dc=example,dc=com");
    user5 = DN.valueOf("cn=user5,dc=example,dc=com");
 
    /*
    Extend the schema and add an attribute which is based on
    generalizedTimeSyntax. Since all the existing attributes based
    on that syntax are read-only, let us create a new attribute and
    add it.*/
   int resultCode = TestCaseUtils.applyModifications(true,
    "dn: cn=schema",
    "changetype: modify",
    "add: attributeTypes",
    "attributeTypes: " + TEST_TIME_DEF,
    "attributeTypes: " + TEST_DATE_DEF,
    "-",
    "add: objectclasses",
    "objectclasses: " + TEST_OC_DEF,
    "objectclasses: " + TEST_OC2_DEF
    );
    assertEquals(0, resultCode);
  }
 
  @AfterClass
  public void cleanup() throws Exception
  {
    int resultCode = TestCaseUtils.applyModifications(true,
        "dn: cn=schema",
        "changetype: modify",
        "delete: objectclasses",
        "objectclasses: " + TEST_OC_DEF,
        "objectclasses: " + TEST_OC2_DEF,
        "-",
        "delete: attributetypes",
        "attributeTypes: " + TEST_TIME_DEF,
        "attributeTypes: " + TEST_DATE_DEF
      );
      assertThat(resultCode).isEqualTo(0);
  }
 
  @DataProvider
  public Object[][] relativeTime()
  {
    return new Object[][] {
      // relativeTime less than expired events
      { TIME_ATTR + ":" + EXT_OMR_RELATIVE_TIME_LT_OID + ":=-60m", new DN[] { user1, user2, } },
      // relativeTime less than future events
      { TIME_ATTR + ":" + EXT_OMR_RELATIVE_TIME_LT_OID + ":=1d", new DN[] { user1, user2, user3, user5, } },
      // relativeTime greater than expired events
      { TIME_ATTR + ":" + EXT_OMR_RELATIVE_TIME_GT_OID + ":=-1h", new DN[] { user3, user4, user5, } },
      // relativeTime greater than future events
      { TIME_ATTR + ":" + EXT_OMR_RELATIVE_TIME_GT_OID + ":=0s", new DN[] { user3, user4, } },
    };
  }
 
  @Test(dataProvider = "relativeTime")
  public void testRelativeTimeUsingAssertion(String filterString, DN[] expectedDNs) throws Exception
  {
    SearchFilter filter = SearchFilter.createFilterFromString(filterString);
    assertThat(getMatchingEntryDNs(filter)).containsOnly(expectedDNs);
  }
 
  private Collection<DN> getMatchingEntryDNs(SearchFilter filter) throws Exception
  {
    AttributeType attrType = filter.getAttributeType();
    MatchingRule rule = getServerContext().getSchema().getMatchingRule(filter.getMatchingRuleID());
    Assertion assertion = rule.getAssertion(filter.getAssertionValue());
 
    Collection<DN> results = new ArrayList<>();
    for (Entry entry : makeEntries())
    {
      Attribute attribute = entry.getAttribute(AttributeDescription.create(attrType));
      if (attribute != null)
      {
        ByteString attrValue = rule.normalizeAttributeValue(attribute.iterator().next());
        if (assertion.matches(attrValue).toBoolean())
        {
          results.add(entry.getName());
        }
      }
    }
    return results;
  }
 
  /** Test to search using the relative time matching rule with index. */
  @Test(dataProvider = "relativeTime")
  public void testRelativeTimeWithIndex(String filterString, DN[] expectedDNs) throws Exception
  {
    FakeEntryIndex index = new FakeEntryIndex(TIME_ATTR);
    index.addAll(makeEntries());
    Collection<Entry> entries = index.evaluateFilter(filterString);
    assertThat(toNames(entries)).containsOnly(expectedDNs);
  }
 
  private List<DN> toNames(Collection<? extends Entry> entries)
  {
    List<DN> results = new ArrayList<>();
    for (Entry entry : entries)
    {
      results.add(entry.getName());
    }
    return results;
  }
 
  /**
   * Test to match the attribute and the assertion values using a partial date and time
   * matching rule.
   */
  @Test(dataProvider="partialDateTimeValues")
  public void testPartialDateNTimeMatch(long timeInMillis, String generalizedTime, String assertionValue)
      throws Exception
  {
    MatchingRule partialTimeRule = getServerContext().getSchema().getMatchingRule(EXT_PARTIAL_DATE_TIME_NAME);
    Assertion assertion = partialTimeRule.getAssertion(ByteString.valueOfUtf8(assertionValue));
    assertEquals(assertion.matches(ByteString.valueOfLong(timeInMillis)), ConditionResult.TRUE);
  }
 
  @Test(dataProvider="partialDateTimeValues")
  public void testPartialDateNTimeMatchViaIndex(long timeInMillis, String generalizedTime, String assertionValue)
      throws Exception
  {
    ByteString attrValue = ByteString.valueOfUtf8(generalizedTime);
    ByteString assertValue = ByteString.valueOfUtf8(assertionValue);
 
    FakeByteStringIndex fakeIndex = new FakeByteStringIndex(EXT_PARTIAL_DATE_TIME_NAME);
    fakeIndex.add(attrValue);
    Set<ByteString> attrValues = fakeIndex.evaluateAssertionValue(assertValue, FilterType.EXTENSIBLE_MATCH);
    assertThat(attrValues).containsOnly(attrValue);
  }
 
 
 
  /** Tests the assertion syntax of the relative time matching rules. */
  @Test(dataProvider= "relativeTimeValues")
  public void testRelativeTimeMatchingRuleAssertionSyntax(String assertion,boolean isValid)
  {
    MatchingRule relativeTimeLTRule = getServerContext().getSchema().getMatchingRule(EXT_OMR_RELATIVE_TIME_LT_ALT_NAME);
    try
    {
      relativeTimeLTRule.getAssertion(ByteString.valueOfUtf8(assertion));
      // An invalid value can't get away without throwing exception.
      assertTrue(isValid);
    }
    catch (DecodeException e)
    {
      //invalid values will throw an exception.
      assertFalse(isValid);
    }
  }
 
 
 
  /** Tests the assertion syntax of the partial date and time matching rules. */
  @Test(dataProvider= "partialDateTimeSyntaxes")
  public void testPartialDateTimeMatchingRuleAssertionSyntax(String assertion,boolean isValid)
  {
    MatchingRule partialDTRule = getServerContext().getSchema().getMatchingRule(EXT_PARTIAL_DATE_TIME_OID);
    try
    {
      partialDTRule.getAssertion(ByteString.valueOfUtf8(assertion));
      assertTrue(isValid);
    }
    catch (DecodeException e)
    {
      //invalid values will throw an exception.
      assertFalse(isValid);
    }
  }
 
 
 
  /** Generates data for testing relative time matching rule assertion syntax. */
  @DataProvider
  public Object[][] relativeTimeValues()
  {
    return new Object[][] {
      {"1s",true},
      {"1s0d",false},
      {"-1d",true},
      {"2h",true},
      {"+2w",true},
      {"0",true},
      {"0s",true},
      {"0d",true},
      {"xyz",false},
      {"12w-2d",false},
      {"1s2s",false},
      {"1d4s5d",false}
    };
  }
 
 
  /** Generates the data for testing partial time date and time values. */
  @DataProvider
  public Object[][] partialDateTimeValues()
  {
    SimpleDateFormat sdf = new SimpleDateFormat("YYYYMMddHHmmssZ");
    GregorianCalendar c = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
    c.setLenient(false);
    c.clear();
    sdf.setCalendar(c);
    c.set(Calendar.HOUR_OF_DAY,23);
    c.set(Calendar.MINUTE,0);
    c.set(Calendar.SECOND,0);
    long time1 = c.getTimeInMillis();
    String format1 = sdf.format(c.getTime());
    c.set(Calendar.HOUR_OF_DAY,00);
    c.set(Calendar.MINUTE,59);
    c.set(Calendar.SECOND,59);
    long time2 = c.getTimeInMillis();
    String format2 = sdf.format(c.getTime());
 
    return new Object[][] {
      { time1, format1, "0s" },
      { time1, format1, "0m" },
      { time1, format1, "23h" },
      { time2, format2, "59m59s" },
      { time2, format2, "0h59m59s" },
      { time2, format2, "01D01M" },
    };
  }
 
 
 
  /** Generates data for testing partial date and time assertion syntax. */
  @DataProvider
  public Object[][] partialDateTimeSyntaxes()
  {
   //Get the current time.
   GregorianCalendar cal =
           new GregorianCalendar(TimeZone.getTimeZone("UTC"));
   cal.setLenient(false);
 
    //Get the date today.
    int second = cal.get(Calendar.SECOND);
    int minute = cal.get(Calendar.MINUTE);
    int hour = cal.get(Calendar.HOUR);
    int date = cal.get(Calendar.DATE);
    int month = cal.get(Calendar.MONTH) + 1;
    int year = cal.get(Calendar.YEAR);
 
    return new Object[][] {
      {"20MM30DD1978YY",false},
      {"02MM29DD2009YY",false},
      {"02MM31DD2010YY",false},
      {"-1s",false},
      {"02M29D2008Y",true},
      {"DDYY",false},
      {"02D",true},
      {"12M",true},
      {"1978Y",true},
      {"0MM",false},
      {"20MM03DD10MM",false},
      {"00s12m13h",true},
      {"00s12m14h1M3D1978Y",true},
      {"1s",true},
      {"12m",true},
      {"23h",true},
      {"61s",false},
      {"60m",false},
      {"24h",false},
      {second+"s",true},
      {minute+"m",true},
      {hour+"h",true},
      {date+"D",true},
      {month+"M",true},
      {year+"Y",true},
      {month+"M"+date+"D",true},
      {year+"Y"+date+"D",true},
      {month+"M"+year+"Y"+date+"D",true}
    };
  }
 
  private List<Entry> makeEntries() throws Exception
  {
    // Get the current time from the TimeThread. Using the current time from new
    // calendar may fail if the time thread using a stale time.
    long currentTime = TimeThread.getTime();
 
    return TestCaseUtils.makeEntries(
      "dn: cn=user1,dc=example,dc=com",
      "objectclass: person",
      "objectclass: testoc",
      "cn: user1",
      "sn: user1",
      TIME_ATTR + ": "+ format(currentTime-4000*1000), //more than 1 hour old.
      "",
      "dn: cn=user2,dc=example,dc=com",
      "objectclass: person",
      "objectclass: testoc",
      "cn: user2",
      "sn: user2",
      TIME_ATTR + ": " + format(currentTime-25*3600*1000), //more than  a day old.
      "",
      "dn: cn=user3,dc=example,dc=com",
      "objectclass: person",
      "objectclass: testoc",
      "cn: user3",
      "sn: user3",
      TIME_ATTR + ": " + format(currentTime+4000*1000),  //more than 1 hour in advance.
      "",
      "dn: cn=user4,dc=example,dc=com",
      "objectclass: person",
      "objectclass: testoc",
      "cn: user4",
      "sn: user4",
      TIME_ATTR + ": " + format(currentTime+25*3600*1000),  // more than 1 day in advance
      "",
      "dn: cn=user5,dc=example,dc=com",
      "objectclass: person",
      "objectclass: testoc",
      "cn: user5",
      "sn: user5",
      TIME_ATTR + ": " + format(currentTime), // now.
      "",
      "dn: cn=user6,dc=example,dc=com",
      "objectclass: person",
      "objectclass: testoc2",
      "cn: user6",
      "sn: user6",
      DATE_ATTR + ": 19651101000000Z", // Nov 1st 1965
      "",
      "dn: cn=user7,dc=example,dc=com",
      "objectclass: person",
      "objectclass: testoc2",
      "cn: user7",
      "sn: user7",
      DATE_ATTR + ": 20101104000000Z", // Nov 4th 2010
      "",
      "dn: cn=user8,dc=example,dc=com",
      "objectclass: person",
      "objectclass: testoc2",
      "cn: user8",
      "sn: user8",
      DATE_ATTR + ": 20000101000000Z" // Jan 1st 2000
    );
  }
}