Search in sources :

Example 46 with JSONField

use of com.unboundid.util.json.JSONField in project ldapsdk by pingidentity.

the class JSONLogMessageTestCase method testGetStringList.

/**
 * Tests the methods for getting a string list from a JSON object.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testGetStringList() throws Exception {
    // Test with a field whose value is an array of strings.
    JSONObject o = createMinimalMessageObject(CONNECT, null, createField(REQUEST_CONTROL_OIDS, "1.2.3.4", "5.6.7.8"));
    JSONAccessLogMessage m = new JSONConnectAccessLogMessage(o);
    assertEquals(m.getStringList(REQUEST_CONTROL_OIDS), Arrays.asList("1.2.3.4", "5.6.7.8"));
    // Test with a field whose value is a single string.
    o = createMinimalMessageObject(CONNECT, null, createField(REQUEST_CONTROL_OIDS, "1.2.3.4"));
    m = new JSONConnectAccessLogMessage(o);
    assertEquals(m.getStringList(REQUEST_CONTROL_OIDS), Collections.singletonList("1.2.3.4"));
    // Test with a field whose value is a single number.
    o = createMinimalMessageObject(CONNECT, null, createField(OPERATION_ID, 1L));
    m = new JSONConnectAccessLogMessage(o);
    assertEquals(m.getStringList(OPERATION_ID), Collections.emptyList());
    // Test with a field whose value is an array of non-strings.
    final LogField testField = new LogField("testField", new StringLogFieldSyntax(100));
    o = createMinimalMessageObject(CONNECT, null, new JSONField(testField.getFieldName(), new JSONArray(JSONBoolean.TRUE, new JSONNumber("1234"))));
    m = new JSONConnectAccessLogMessage(o);
    assertEquals(m.getStringList(testField), Collections.emptyList());
    // Test with a nonexistent field.
    o = createMinimalMessageObject(CONNECT, null);
    m = new JSONConnectAccessLogMessage(o);
    assertEquals(m.getStringList(REQUEST_CONTROL_OIDS), Collections.emptyList());
}
Also used : StringLogFieldSyntax(com.unboundid.ldap.sdk.unboundidds.logs.v2.syntax.StringLogFieldSyntax) JSONObject(com.unboundid.util.json.JSONObject) JSONArray(com.unboundid.util.json.JSONArray) JSONField(com.unboundid.util.json.JSONField) JSONNumber(com.unboundid.util.json.JSONNumber) LogField(com.unboundid.ldap.sdk.unboundidds.logs.v2.LogField) Test(org.testng.annotations.Test)

Example 47 with JSONField

use of com.unboundid.util.json.JSONField in project ldapsdk by pingidentity.

the class JSONLogFieldSyntaxTestCase method testBasicSyntax.

/**
 * Tests the basic functionality of the syntax.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testBasicSyntax() throws Exception {
    final JSONLogFieldSyntax syntax = new JSONLogFieldSyntax(10, null, null);
    assertEquals(syntax.getMaxStringLengthCharacters(), 10);
    assertNotNull(syntax.getIncludedSensitiveFields());
    assertTrue(syntax.getIncludedSensitiveFields().isEmpty());
    assertNotNull(syntax.getExcludedSensitiveFields());
    assertTrue(syntax.getExcludedSensitiveFields().isEmpty());
    assertNotNull(syntax.getSyntaxName());
    assertEquals(syntax.getSyntaxName(), "json");
    assertNotNull(syntax.valueToSanitizedString(JSONObject.EMPTY_OBJECT));
    assertEquals(syntax.valueToSanitizedString(JSONObject.EMPTY_OBJECT), "{ }");
    final JSONObject singleFieldObject = new JSONObject(new JSONField("foo", "bar"));
    assertNotNull(syntax.valueToSanitizedString(singleFieldObject));
    assertEquals(syntax.valueToSanitizedString(singleFieldObject), "{ \"foo\":\"bar\" }");
    final JSONObject multiFieldObject = new JSONObject(new JSONField("a", "b"), new JSONField("c", "ThisIsALongerValue"), new JSONField("d", 123), new JSONField("e", 4.5), new JSONField("f", true), new JSONField("g", JSONNull.NULL), new JSONField("h", new JSONArray(new JSONString("i"), JSONBoolean.FALSE, new JSONNumber(678), JSONNull.NULL, new JSONObject(new JSONField("j", "k")))), new JSONField("l", new JSONObject(new JSONField("m", "n"))));
    assertNotNull(syntax.valueToSanitizedString(multiFieldObject));
    assertEquals(syntax.valueToSanitizedString(multiFieldObject), "{ \"a\":\"b\", " + "\"c\":\"ThisIsALon{8 more characters}\", " + "\"d\":123, " + "\"e\":4.5, " + "\"f\":true, " + "\"g\":null, " + "\"h\":[ " + "\"i\", " + "false, " + "678, " + "null, " + "{ \"j\":\"k\" } ], " + "\"l\":{ \"m\":\"n\" } }");
    assertNotNull(syntax.parseValue(singleFieldObject.toSingleLineString()));
    assertEquals(syntax.parseValue(singleFieldObject.toSingleLineString()), singleFieldObject);
    try {
        syntax.parseValue("{REDACTED}");
        fail("Expected an exception when trying to parse a redacted value.");
    } catch (final RedactedValueException e) {
    // This was expected.
    }
    try {
        syntax.parseValue("{TOKENIZED:1234567890ABCDEF}");
        fail("Expected an exception when trying to parse a tokenized value.");
    } catch (final TokenizedValueException e) {
    // This was expected
    }
    try {
        syntax.parseValue("malformed");
        fail("Expected an exception when trying to parse a malformed value.");
    } catch (final LogSyntaxException e) {
        // This was expected.
        assertFalse((e instanceof RedactedValueException) || (e instanceof TokenizedValueException));
    }
    assertFalse(syntax.valueStringIsCompletelyRedacted(singleFieldObject.toSingleLineString()));
    assertTrue(syntax.valueStringIsCompletelyRedacted("{ \"redacted\":\"{REDACTED}\" }"));
    assertTrue(syntax.valueStringIsCompletelyRedacted("{REDACTED}"));
    assertTrue(syntax.completelyRedactedValueConformsToSyntax());
    assertNotNull(syntax.redactEntireValue());
    assertEquals(syntax.redactEntireValue(), "{ \"redacted\":\"{REDACTED}\" }");
    JSONObject tokenizedObject = new JSONObject(syntax.redactEntireValue());
    assertTrue(syntax.supportsRedactedComponents());
    assertTrue(syntax.valueWithRedactedComponentsConformsToSyntax());
    assertNotNull(syntax.redactComponents(singleFieldObject));
    assertEquals(syntax.redactComponents(singleFieldObject), "{ \"foo\":\"{REDACTED}\" }");
    tokenizedObject = new JSONObject(syntax.redactComponents(singleFieldObject));
    assertNotNull(syntax.redactComponents(multiFieldObject));
    assertEquals(syntax.redactComponents(multiFieldObject), "{ \"a\":\"{REDACTED}\", " + "\"c\":\"{REDACTED}\", " + "\"d\":\"{REDACTED}\", " + "\"e\":\"{REDACTED}\", " + "\"f\":\"{REDACTED}\", " + "\"g\":\"{REDACTED}\", " + "\"h\":\"{REDACTED}\", " + "\"l\":\"{REDACTED}\" }");
    tokenizedObject = new JSONObject(syntax.redactComponents(multiFieldObject));
    assertFalse(syntax.valueStringIsCompletelyTokenized(singleFieldObject.toSingleLineString()));
    assertTrue(syntax.valueStringIsCompletelyTokenized("{ \"tokenized\":\"{TOKENIZED:abcdef}\" }"));
    assertTrue(syntax.valueStringIsCompletelyTokenized("{TOKENIZED:abcdef}"));
    assertTrue(syntax.completelyTokenizedValueConformsToSyntax());
    final byte[] pepper = StaticUtils.randomBytes(8, false);
    String tokenizedString = syntax.tokenizeEntireValue(singleFieldObject, pepper);
    assertNotNull(tokenizedString);
    assertTrue(tokenizedString.startsWith("{ \"tokenized\":\"{TOKENIZED:"));
    assertTrue(tokenizedString.endsWith("}\" }"));
    tokenizedObject = new JSONObject(tokenizedString);
    assertTrue(syntax.supportsTokenizedComponents());
    assertTrue(syntax.valueWithTokenizedComponentsConformsToSyntax());
    tokenizedString = syntax.tokenizeComponents(singleFieldObject, pepper);
    tokenizedObject = new JSONObject(tokenizedString);
    assertEquals(tokenizedObject.getFields().size(), 1);
    assertNotNull(tokenizedObject.getFieldAsString("foo"));
    assertTrue(tokenizedObject.getFieldAsString("foo").startsWith("{TOKENIZED:"));
    assertTrue(tokenizedObject.getFieldAsString("foo").endsWith("}"));
    tokenizedString = syntax.tokenizeComponents(multiFieldObject, pepper);
    tokenizedObject = new JSONObject(tokenizedString);
    assertEquals(tokenizedObject.getFields().size(), 8);
    assertNotNull(tokenizedObject.getFieldAsString("a"));
    assertTrue(tokenizedObject.getFieldAsString("a").startsWith("{TOKENIZED:"));
    assertTrue(tokenizedObject.getFieldAsString("a").endsWith("}"));
    assertNotNull(tokenizedObject.getFieldAsString("c"));
    assertTrue(tokenizedObject.getFieldAsString("c").startsWith("{TOKENIZED:"));
    assertTrue(tokenizedObject.getFieldAsString("c").endsWith("}"));
    assertNotNull(tokenizedObject.getFieldAsString("d"));
    assertTrue(tokenizedObject.getFieldAsString("d").startsWith("{TOKENIZED:"));
    assertTrue(tokenizedObject.getFieldAsString("d").endsWith("}"));
    assertNotNull(tokenizedObject.getFieldAsString("e"));
    assertTrue(tokenizedObject.getFieldAsString("e").startsWith("{TOKENIZED:"));
    assertTrue(tokenizedObject.getFieldAsString("e").endsWith("}"));
    assertNotNull(tokenizedObject.getFieldAsString("f"));
    assertTrue(tokenizedObject.getFieldAsString("f").startsWith("{TOKENIZED:"));
    assertTrue(tokenizedObject.getFieldAsString("f").endsWith("}"));
    assertNotNull(tokenizedObject.getFieldAsString("g"));
    assertTrue(tokenizedObject.getFieldAsString("g").startsWith("{TOKENIZED:"));
    assertTrue(tokenizedObject.getFieldAsString("g").endsWith("}"));
    assertNotNull(tokenizedObject.getFieldAsString("h"));
    assertTrue(tokenizedObject.getFieldAsString("h").startsWith("{TOKENIZED:"));
    assertTrue(tokenizedObject.getFieldAsString("h").endsWith("}"));
    assertNotNull(tokenizedObject.getFieldAsString("l"));
    assertTrue(tokenizedObject.getFieldAsString("l").startsWith("{TOKENIZED:"));
    assertTrue(tokenizedObject.getFieldAsString("l").endsWith("}"));
}
Also used : JSONObject(com.unboundid.util.json.JSONObject) JSONArray(com.unboundid.util.json.JSONArray) JSONField(com.unboundid.util.json.JSONField) JSONNumber(com.unboundid.util.json.JSONNumber) JSONString(com.unboundid.util.json.JSONString) JSONString(com.unboundid.util.json.JSONString) Test(org.testng.annotations.Test)

Example 48 with JSONField

use of com.unboundid.util.json.JSONField in project ldapsdk by pingidentity.

the class JSONLogFieldSyntaxTestCase method testTextLogMethods.

/**
 * Tests  the methods that may be used for logging text-formatted messages.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testTextLogMethods() throws Exception {
    final Set<String> includeFields = StaticUtils.setOf("a");
    final JSONLogFieldSyntax syntax = new JSONLogFieldSyntax(10, includeFields, null);
    final JSONObject o = new JSONObject(new JSONField("a", "foo"), new JSONField("b", "ThisIsALongerValue"));
    final ByteStringBuffer buffer = new ByteStringBuffer();
    syntax.logSanitizedFieldToTextFormattedLog("abc", o, buffer);
    assertEquals(buffer.toString(), " abc=\"{ 'a':'foo', 'b':'ThisIsALon{8 more characters}' }\"");
    buffer.clear();
    syntax.logCompletelyRedactedFieldToTextFormattedLog("def", buffer);
    assertEquals(buffer.toString(), " def=\"{ 'redacted':'{REDACTED}' }\"");
    buffer.clear();
    syntax.logRedactedComponentsFieldToTextFormattedLog("ghi", o, buffer);
    assertEquals(buffer.toString(), " ghi=\"{ 'a':'{REDACTED}', 'b':'ThisIsALon{8 more characters}' }\"");
    buffer.clear();
    final byte[] pepper = StaticUtils.randomBytes(8, false);
    syntax.logCompletelyTokenizedFieldToTextFormattedLog("jkl", o, pepper, buffer);
    final String completelyTokenizedString = buffer.toString();
    assertTrue(completelyTokenizedString.startsWith(" jkl=\"{ 'tokenized':'{TOKENIZED:"));
    assertTrue(completelyTokenizedString.endsWith("}' }\""));
    buffer.clear();
    syntax.logTokenizedComponentsFieldToTextFormattedLog("mno", o, pepper, buffer);
    final String tokenizedComponentsString = buffer.toString();
    assertTrue(tokenizedComponentsString.startsWith(" mno=\"{ 'a':'{TOKENIZED:"));
    assertTrue(tokenizedComponentsString.endsWith("}', 'b':'ThisIsALon{8 more characters}' }\""));
}
Also used : JSONObject(com.unboundid.util.json.JSONObject) JSONField(com.unboundid.util.json.JSONField) JSONString(com.unboundid.util.json.JSONString) ByteStringBuffer(com.unboundid.util.ByteStringBuffer) Test(org.testng.annotations.Test)

Example 49 with JSONField

use of com.unboundid.util.json.JSONField in project ldapsdk by pingidentity.

the class JSONLogFieldSyntaxTestCase method testJSONValueLogMethods.

/**
 * Tests the methods that may be used for logging JSON-formatted values
 * (without field names).
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testJSONValueLogMethods() throws Exception {
    final Set<String> includeFields = StaticUtils.setOf("a");
    final JSONLogFieldSyntax syntax = new JSONLogFieldSyntax(10, includeFields, null);
    final JSONObject o = new JSONObject(new JSONField("a", "foo"), new JSONField("b", "ThisIsALongerValue"));
    final JSONBuffer buffer = new JSONBuffer();
    syntax.logSanitizedValueToJSONFormattedLog(o, buffer);
    assertEquals(buffer.toString(), "{ \"a\":\"foo\", " + "\"b\":\"ThisIsALon{8 more characters}\" }");
    buffer.clear();
    syntax.logCompletelyRedactedValueToJSONFormattedLog(buffer);
    assertEquals(buffer.toString(), "{ \"redacted\":\"{REDACTED}\" }");
    buffer.clear();
    syntax.logRedactedComponentsValueToJSONFormattedLog(o, buffer);
    assertEquals(buffer.toString(), "{ \"a\":\"{REDACTED}\", " + "\"b\":\"ThisIsALon{8 more characters}\" }");
    buffer.clear();
    final byte[] pepper = StaticUtils.randomBytes(8, false);
    syntax.logCompletelyTokenizedValueToJSONFormattedLog(o, pepper, buffer);
    final String completelyTokenizedString = buffer.toString();
    assertTrue(completelyTokenizedString.startsWith("{ \"tokenized\":\"{TOKENIZED:"));
    assertTrue(completelyTokenizedString.endsWith("}\" }"));
    buffer.clear();
    syntax.logTokenizedComponentsValueToJSONFormattedLog(o, pepper, buffer);
    final String tokenizedComponentsString = buffer.toString();
    assertTrue(tokenizedComponentsString.startsWith("{ \"a\":\"{TOKENIZED:"));
    assertTrue(tokenizedComponentsString.endsWith("}\", \"b\":\"ThisIsALon{8 more characters}\" }"));
}
Also used : JSONObject(com.unboundid.util.json.JSONObject) JSONBuffer(com.unboundid.util.json.JSONBuffer) JSONField(com.unboundid.util.json.JSONField) JSONString(com.unboundid.util.json.JSONString) Test(org.testng.annotations.Test)

Example 50 with JSONField

use of com.unboundid.util.json.JSONField in project ldapsdk by pingidentity.

the class JSONLogFieldSyntaxTestCase method testExcludeFields.

/**
 * Tests the behavior for the syntax when it is configured to exclude a
 * specified set of fields as sensitive, and therefore only those fields will
 * not be redacted or tokenized when operating on components.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testExcludeFields() throws Exception {
    final Set<String> excludeFields = StaticUtils.setOf("a", "b", "c");
    final JSONLogFieldSyntax syntax = new JSONLogFieldSyntax(10, null, excludeFields);
    assertNotNull(syntax.getIncludedSensitiveFields());
    assertTrue(syntax.getIncludedSensitiveFields().isEmpty());
    assertNotNull(syntax.getExcludedSensitiveFields());
    assertEquals(syntax.getExcludedSensitiveFields(), excludeFields);
    final JSONObject o = new JSONObject(new JSONField("a", "foo"), new JSONField("b", 5), new JSONField("c", true), new JSONField("d", "e"), new JSONField("f", new JSONObject(new JSONField("a", "g"), new JSONField("h", "i"))), new JSONField("j", new JSONArray(new JSONString("k"), new JSONObject(new JSONField("a", "l"), new JSONField("m", "n")))));
    assertNotNull(syntax.valueToSanitizedString(o));
    assertEquals(syntax.valueToSanitizedString(o), "{ \"a\":\"foo\", " + "\"b\":5, " + "\"c\":true, " + "\"d\":\"e\", " + "\"f\":{ \"a\":\"g\", \"h\":\"i\" }, " + "\"j\":[ \"k\", { \"a\":\"l\", \"m\":\"n\" } ] }");
    assertNotNull(syntax.redactComponents(o));
    assertEquals(syntax.redactComponents(o), "{ \"a\":\"foo\", " + "\"b\":5, " + "\"c\":true, " + "\"d\":\"{REDACTED}\", " + "\"f\":\"{REDACTED}\", " + "\"j\":\"{REDACTED}\" }");
    final byte[] pepper = StaticUtils.randomBytes(8, false);
    assertNotNull(syntax.tokenizeComponents(o, pepper));
    final JSONObject tokenizedObject = new JSONObject(syntax.tokenizeComponents(o, pepper));
    assertEquals(tokenizedObject.getFields().size(), 6);
    assertNotNull(tokenizedObject.getFieldAsString("a"));
    assertEquals(tokenizedObject.getFieldAsString("a"), "foo");
    assertNotNull(tokenizedObject.getFieldAsInteger("b"));
    assertEquals(tokenizedObject.getFieldAsInteger("b").intValue(), 5);
    assertNotNull(tokenizedObject.getFieldAsBoolean("c"));
    assertTrue(tokenizedObject.getFieldAsBoolean("c").booleanValue());
    assertNotNull(tokenizedObject.getFieldAsString("d"));
    assertTrue(tokenizedObject.getFieldAsString("d").startsWith("{TOKENIZED:"));
    assertNotNull(tokenizedObject.getFieldAsString("f"));
    assertTrue(tokenizedObject.getFieldAsString("f").startsWith("{TOKENIZED:"));
    assertNotNull(tokenizedObject.getFieldAsString("j"));
    assertTrue(tokenizedObject.getFieldAsString("j").startsWith("{TOKENIZED:"));
}
Also used : JSONObject(com.unboundid.util.json.JSONObject) JSONArray(com.unboundid.util.json.JSONArray) JSONField(com.unboundid.util.json.JSONField) JSONString(com.unboundid.util.json.JSONString) JSONString(com.unboundid.util.json.JSONString) Test(org.testng.annotations.Test)

Aggregations

JSONField (com.unboundid.util.json.JSONField)97 JSONObject (com.unboundid.util.json.JSONObject)97 Test (org.testng.annotations.Test)91 JSONArray (com.unboundid.util.json.JSONArray)68 JSONString (com.unboundid.util.json.JSONString)66 JSONNumber (com.unboundid.util.json.JSONNumber)20 PasswordPolicyStateJSONField (com.unboundid.ldap.sdk.unboundidds.PasswordPolicyStateJSONField)11 LDAPSDKUsageException (com.unboundid.util.LDAPSDKUsageException)8 ModifyRequest (com.unboundid.ldap.sdk.ModifyRequest)7 Date (java.util.Date)7 Entry (com.unboundid.ldap.sdk.Entry)5 JSONException (com.unboundid.util.json.JSONException)5 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)4 JSONBoolean (com.unboundid.util.json.JSONBoolean)4 JSONValue (com.unboundid.util.json.JSONValue)4 CompareRequest (com.unboundid.ldap.sdk.CompareRequest)3 LDAPResult (com.unboundid.ldap.sdk.LDAPResult)3 PasswordQualityRequirement (com.unboundid.ldap.sdk.unboundidds.extensions.PasswordQualityRequirement)3 LogField (com.unboundid.ldap.sdk.unboundidds.logs.v2.LogField)3 ArrayList (java.util.ArrayList)3