-
Notifications
You must be signed in to change notification settings - Fork 331
Expand file tree
/
Copy pathGrpcRequestMessageHandlerTest.groovy
More file actions
143 lines (114 loc) · 4.25 KB
/
GrpcRequestMessageHandlerTest.groovy
File metadata and controls
143 lines (114 loc) · 4.25 KB
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
package com.datadog.iast
import com.datadog.iast.propagation.PropagationModuleImpl
import com.datadog.iast.protobuf.Test2
import com.datadog.iast.protobuf.Test3
import com.datadog.iast.util.ObjectVisitor
import datadog.trace.api.iast.InstrumentationBridge
import datadog.trace.api.iast.SourceTypes
import datadog.trace.api.iast.propagation.PropagationModule
import datadog.trace.api.iast.telemetry.IastMetric
import datadog.trace.api.iast.telemetry.IastMetricCollector
import foo.bar.VisitableClass
import java.util.function.Predicate
import static com.datadog.iast.util.ObjectVisitor.State.CONTINUE
class GrpcRequestMessageHandlerTest extends IastModuleImplTestBase {
private PropagationModule propagation
private IastMetricCollector collector
void setup() {
propagation = Spy(new PropagationModuleImpl())
InstrumentationBridge.registerIastModule(propagation)
collector = Spy(new IastMetricCollector())
ctx.collector = collector
}
void 'the handler does nothing without propagation'() {
given:
final handler = new GrpcRequestMessageHandler()
InstrumentationBridge.clearIastModules()
when:
handler.apply(reqCtx, [:])
then:
0 * _
}
void 'the handler does nothing with null values'() {
given:
final handler = new GrpcRequestMessageHandler()
when:
handler.apply(reqCtx, null)
then:
0 * _
}
void 'the handler forwards objects to the propagation module'() {
given:
final target = [:]
final handler = new GrpcRequestMessageHandler()
when:
handler.apply(reqCtx, target)
then:
1 * propagation.taintObjectDeeply(ctx, target, SourceTypes.GRPC_BODY, _ as Predicate<Class<?>>)
}
void 'the handler only takes into account protobuf v.#protobufVersion related messages'() {
given:
final visitor = Mock(ObjectVisitor.Visitor) {
visit(_ as String, _ as Object) >> {
return CONTINUE
}
}
final nonProtobufMessage = new VisitableClass(name: 'test')
final filter = GrpcRequestMessageHandler::visitProtobufArtifact
when: 'the message is not a protobuf instance'
ObjectVisitor.visit(nonProtobufMessage, visitor, filter)
then: 'only the root object is visited'
1 * visitor.visit('root', nonProtobufMessage) >> CONTINUE
0 * visitor._
when: 'the message is a protobuf message'
ObjectVisitor.visit(protobufMessage, visitor, filter)
then: 'all the properties are visited'
1 * visitor.visit('root', protobufMessage) >> CONTINUE
1 * visitor.visit('root.child_.optional_', 'optional') >> CONTINUE
1 * visitor.visit('root.child_.required_', 'required') >> CONTINUE
1 * visitor.visit('root.child_.repeated_[0]', 'repeated0') >> CONTINUE
1 * visitor.visit('root.child_.repeated_[1]', 'repeated1') >> CONTINUE
// for maps we go inside com.google.protobuf.MapField and extract properties
1 * visitor.visit('root.child_.map_.mapData[]', 'key') >> CONTINUE
1 * visitor.visit('root.child_.map_.mapData[key]', 'value') >> CONTINUE
where:
protobufVersion << ['2', '3']
protobufMessage << [buildProto2Message(), buildProto3Message()]
}
void 'test that metrics are properly generated'() {
given:
final handler = new GrpcRequestMessageHandler()
when:
handler.apply(reqCtx, message)
then:
1 * collector.addMetric(IastMetric.EXECUTED_SOURCE, SourceTypes.GRPC_BODY, 6)
where:
message | _
buildProto2Message() | _
buildProto3Message() | _
}
void 'visitProtobufArtifact handles classes without superclass'() {
when:
boolean result = GrpcRequestMessageHandler.visitProtobufArtifact(Object)
then:
!result
}
private static def buildProto2Message() {
final child = Test2.Proto2Child.newBuilder()
.setOptional("optional")
.setRequired("required")
.addAllRepeated(Arrays.asList('repeated0', 'repeated1'))
.putMap('key', 'value')
.build()
return Test2.Proto2Parent.newBuilder().setChild(child).build()
}
private static def buildProto3Message() {
final child = Test3.Proto3Child.newBuilder()
.setOptional("optional")
.setRequired("required")
.addAllRepeated(Arrays.asList('repeated0', 'repeated1'))
.putMap('key', 'value')
.build()
return Test3.Proto3Parent.newBuilder().setChild(child).build()
}
}