-
Notifications
You must be signed in to change notification settings - Fork 279
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
check literal length in listener and add tests
- Loading branch information
Showing
3 changed files
with
64 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
.../instrumentation/iast-instrumenter/src/test/groovy/IastHardcodedSecretListenerTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import datadog.trace.agent.test.AgentTestRunner | ||
import datadog.trace.agent.tooling.bytebuddy.csi.ConstantPool | ||
import datadog.trace.api.iast.InstrumentationBridge | ||
import datadog.trace.api.iast.sink.HardcodedSecretModule | ||
import datadog.trace.instrumentation.iastinstrumenter.IastHardcodedSecretListener | ||
import net.bytebuddy.description.type.TypeDescription | ||
import org.apache.commons.io.IOUtils | ||
|
||
class IastHardcodedSecretListenerTest extends AgentTestRunner{ | ||
|
||
|
||
void 'test'(){ | ||
given: | ||
final module = Mock(HardcodedSecretModule) | ||
InstrumentationBridge.registerIastModule(module) | ||
final classFile = readClassBytes(clazz) | ||
final pool = new ConstantPool(classFile) | ||
final type = Mock(TypeDescription) | ||
final instance = IastHardcodedSecretListener.INSTANCE | ||
|
||
when: | ||
instance.onConstantPool(type, pool, classFile) | ||
|
||
then: | ||
expected * module.onStringLiteral(_, _, _) | ||
|
||
where: | ||
clazz | expected | ||
HardcodedSecretTestClass | 1 | ||
HardcodedSecretTestClass2 | 0 | ||
} | ||
|
||
byte [] readClassBytes(Class<?> clazz){ | ||
final String classResourceName = clazz.getName().replace('.', '/') + ".class" | ||
try (InputStream is = clazz.getClassLoader().getResourceAsStream(classResourceName)) { | ||
if(is == null) { | ||
throw new IllegalStateException("Could not find class resource: " + classResourceName) | ||
} | ||
return IOUtils.toByteArray(is) | ||
} | ||
} | ||
|
||
class HardcodedSecretTestClass { | ||
|
||
public static final String FOO = "foo" | ||
public static final String LITERAL_LONGER_THAN_10_CHARS = "12345678901" | ||
} | ||
|
||
class HardcodedSecretTestClass2 { | ||
|
||
public static final String FOO = "foo" | ||
} | ||
} | ||
|