Do not allow for System exit

Change-Id: I268b12dfab2e6021ad057a4dc9716a01efbe375d
This commit is contained in:
Dominik Adam Przybysz 2015-09-19 20:14:09 +02:00
parent f03618873e
commit 5545b67ebd
2 changed files with 31 additions and 1 deletions

View file

@ -839,4 +839,31 @@ class MockServerIntegrationTest extends Specification {
false | 1 false | 1
true | 0 true | 0
} }
@Unroll
def "should reject mock when it has System.exit in closure"() {
when:
remoteMockServer.addMock(new AddMockRequestData(
name: 'testRest',
path: 'testEndpoint',
port: 9999,
predicate: predicate,
response: '''{req -> "<goodResponseRest-${req.xml.name()}/>"}''',
soap: false
))
then:
thrown(InvalidMockDefinition)
expect:
remoteMockServer.listMocks() == []
where:
predicate << [
'''{req -> System.exit(-1); req.xml.name() == 'request'}''',
'''{req -> System .exit(-1); req.xml.name() == 'request'}''',
'''{req -> System
.exit(-1); req.xml.name() == 'request'}''',
'''{req -> System. exit(-1); req.xml.name() == 'request'}''',
'''{req -> System.exit (-1); req.xml.name() == 'request'}'''
]
}
} }

View file

@ -64,6 +64,9 @@ class Mock implements Comparable<Mock> {
} }
private Closure toClosure(String predicate) { private Closure toClosure(String predicate) {
if (predicate ==~ /(?m).*System\s*\.\s*exit\s*\(.*/) {
throw new RuntimeException('System.exit is forbidden')
}
GroovyShell sh = new GroovyShell(this.class.classLoader); GroovyShell sh = new GroovyShell(this.class.classLoader);
return sh.evaluate(predicate) as Closure return sh.evaluate(predicate) as Closure
} }