[TEST] fixed ScrollIdSigningTests to never use the signed scroll ids as tampered one

`randomInt` includes 0, thus the tampered id could stay the same as the signed scroll ids in some cases which would make everything work and the test fail.

Also cleared the scroll from a finally block, otherwise when the test fails the scroll stays around which might make after test checks fail.

Original commit: elastic/x-pack-elasticsearch@6f6b0d844d
This commit is contained in:
javanna 2014-09-19 14:45:50 +02:00 committed by Luca Cavanna
parent de893c544a
commit d5d4be018d
1 changed files with 22 additions and 33 deletions

View File

@ -52,23 +52,21 @@ public class ScrollIdSigningTests extends ShieldIntegrationTest {
SearchResponse response = client().prepareSearch()
.setQuery(matchAllQuery())
.setScroll(TimeValue.timeValueMinutes(2))
.setSize(5)
.execute().get();
String scrollId = response.getScrollId();
assertSigned(scrollId);
.setSize(randomIntBetween(1, 10)).get();
while (true) {
response = client().prepareSearchScroll(scrollId)
.setScroll(TimeValue.timeValueMinutes(2))
.execute().get();
scrollId = response.getScrollId();
assertSigned(scrollId);
if (response.getHits().getHits().length == 0) {
break;
try {
assertSigned(response.getScrollId());
while (true) {
response = client().prepareSearchScroll(response.getScrollId())
.setScroll(TimeValue.timeValueMinutes(2)).get();
assertSigned(response.getScrollId());
if (response.getHits().getHits().length == 0) {
break;
}
}
} finally {
clearScroll(response.getScrollId());
}
client().prepareClearScroll().addScrollId(scrollId).execute().get();
}
@Test
@ -81,23 +79,17 @@ public class ScrollIdSigningTests extends ShieldIntegrationTest {
SearchResponse response = client().prepareSearch()
.setQuery(matchAllQuery())
.setScroll(TimeValue.timeValueMinutes(2))
.setSize(5)
.execute().get();
.setSize(randomIntBetween(1, 10)).get();
String scrollId = response.getScrollId();
scrollId = randomBoolean() ? scrollId.substring(randomInt(10)) : scrollId + randomAsciiOfLength(randomIntBetween(3, 10));
String tamperedScrollId = randomBoolean() ? scrollId.substring(randomIntBetween(1, 10)) : scrollId + randomAsciiOfLength(randomIntBetween(3, 10));
try {
response = client().prepareSearchScroll(scrollId)
.setScroll(TimeValue.timeValueMinutes(2))
.execute().get();
client().prepareSearchScroll(tamperedScrollId).setScroll(TimeValue.timeValueMinutes(2)).get();
fail("Expected an authorization exception to be thrown when scroll id is tampered");
} catch (Exception e) {
assertThat(ExceptionsHelper.unwrap(e, AuthorizationException.class), notNullValue());
} finally {
clearScroll(scrollId);
}
client().prepareClearScroll().addScrollId(response.getScrollId()).execute().get();
}
@Test
@ -110,20 +102,17 @@ public class ScrollIdSigningTests extends ShieldIntegrationTest {
SearchResponse response = client().prepareSearch()
.setQuery(matchAllQuery())
.setScroll(TimeValue.timeValueMinutes(2))
.setSize(5)
.execute().get();
.setSize(5).get();
String scrollId = response.getScrollId();
String tamperedScrollId = randomBoolean() ? scrollId.substring(randomInt(10)) : scrollId + randomAsciiOfLength(randomIntBetween(3, 10));
String tamperedScrollId = randomBoolean() ? scrollId.substring(randomIntBetween(1, 10)) : scrollId + randomAsciiOfLength(randomIntBetween(3, 10));
try {
client().prepareClearScroll().addScrollId(tamperedScrollId).execute().get();
client().prepareClearScroll().addScrollId(tamperedScrollId).get();
fail("Expected an authorization exception to be thrown when scroll id is tampered");
} catch (Exception e) {
assertThat(ExceptionsHelper.unwrap(e, AuthorizationException.class), notNullValue());
} finally {
clearScroll(scrollId);
}
client().prepareClearScroll().addScrollId(response.getScrollId()).execute().get();
}
private void assertSigned(String scrollId) {