Skip to content

Commit 5c3dbf8

Browse files
Improve assertions on json related tests
1 parent 3fca23d commit 5c3dbf8

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

dd-java-agent/appsec/src/test/groovy/com/datadog/appsec/event/data/ObjectIntrospectionSpecification.groovy

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -388,10 +388,12 @@ class ObjectIntrospectionSpecification extends DDSpecification {
388388
final jsonInput = '{"long": "' + longString + '"}'
389389

390390
when:
391-
convert(MAPPER.readTree(jsonInput), ctx)
391+
final result = convert(MAPPER.readTree(jsonInput), ctx)
392392

393393
then:
394394
1 * ctx.setWafTruncated()
395+
1 * wafMetricCollector.wafInputTruncated(true, false, false)
396+
result["long"].length() <= ObjectIntrospection.MAX_STRING_LENGTH
395397
}
396398

397399
void 'jackson with deep nesting triggers depth limit'() {
@@ -402,11 +404,13 @@ class ObjectIntrospectionSpecification extends DDSpecification {
402404
)
403405

404406
when:
405-
convert(MAPPER.readTree(json), ctx)
407+
final result = convert(MAPPER.readTree(json), ctx)
406408

407409
then:
408410
// Should truncate at max depth and set truncation flag
409411
1 * ctx.setWafTruncated()
412+
1 * wafMetricCollector.wafInputTruncated(false, false, true)
413+
countNesting(result as Map, 0) <= ObjectIntrospection.MAX_DEPTH
410414
}
411415

412416
void 'jackson with large arrays triggers element limit'() {
@@ -416,11 +420,13 @@ class ObjectIntrospectionSpecification extends DDSpecification {
416420
final json = new JsonBuilder(largeArray).toString()
417421

418422
when:
419-
convert(MAPPER.readTree(json), ctx)
423+
final result = convert(MAPPER.readTree(json), ctx) as List
420424

421425
then:
422426
// Should truncate and set truncation flag
423427
1 * ctx.setWafTruncated()
428+
1 * wafMetricCollector.wafInputTruncated(false, true, false)
429+
result.size() <= ObjectIntrospection.MAX_ELEMENTS
424430
}
425431

426432
void 'jackson number type variations'() {
@@ -458,4 +464,15 @@ class ObjectIntrospectionSpecification extends DDSpecification {
458464
MAPPER.readTree('"\\"quotes\\""') || '"quotes"'
459465
MAPPER.readTree('"unicode: \\u0041"') || 'unicode: A'
460466
}
467+
468+
private static int countNesting(final Map<String, Object>object, final int levels) {
469+
if (object.isEmpty()) {
470+
return levels
471+
}
472+
final child = object.values().first()
473+
if (child == null) {
474+
return levels
475+
}
476+
return countNesting(object.values().first() as Map, levels + 1)
477+
}
461478
}

0 commit comments

Comments
 (0)