Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.io.StringReader;
import org.apache.commons.io.output.NullOutputStream;
import org.junit.jupiter.api.Test;
import static com.rapid7.recog.TestGenerators.anyString;
import static com.rapid7.recog.TestGenerators.anyUTF8String;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class RecogVerifierTest {
Expand All @@ -22,7 +22,7 @@ public void verifyNoExampleNoParamsWarnCount() throws ParseException {

// when
RecogParser recogParser = new RecogParser(true);
RecogMatchers matchers = recogParser.parse(new StringReader(xml), anyString());
RecogMatchers matchers = recogParser.parse(new StringReader(xml), anyUTF8String());
VerifierOptions verifierOpts = new VerifierOptions();
RecogVerifier verifier = RecogVerifier.create(verifierOpts, matchers, NullOutputStream.NULL_OUTPUT_STREAM);
verifier.verify();
Expand All @@ -47,7 +47,7 @@ public void verifyNoExampleZeroPositionParamsWarnCount() throws ParseException {

// when
RecogParser recogParser = new RecogParser(true);
RecogMatchers matchers = recogParser.parse(new StringReader(xml), anyString());
RecogMatchers matchers = recogParser.parse(new StringReader(xml), anyUTF8String());
VerifierOptions verifierOpts = new VerifierOptions();
RecogVerifier verifier = RecogVerifier.create(verifierOpts, matchers, NullOutputStream.NULL_OUTPUT_STREAM);
verifier.verify();
Expand Down Expand Up @@ -75,7 +75,7 @@ public void verifyNoExampleNonZeroPositionParamsWarnCount() throws ParseExceptio

// when
RecogParser recogParser = new RecogParser(true);
RecogMatchers matchers = recogParser.parse(new StringReader(xml), anyString());
RecogMatchers matchers = recogParser.parse(new StringReader(xml), anyUTF8String());
VerifierOptions verifierOpts = new VerifierOptions();
RecogVerifier verifier = RecogVerifier.create(verifierOpts, matchers, NullOutputStream.NULL_OUTPUT_STREAM);
verifier.verify();
Expand Down Expand Up @@ -104,7 +104,7 @@ public void verifySuccessfulExample() throws ParseException {

// when
RecogParser recogParser = new RecogParser(true);
RecogMatchers matchers = recogParser.parse(new StringReader(xml), anyString());
RecogMatchers matchers = recogParser.parse(new StringReader(xml), anyUTF8String());
VerifierOptions verifierOpts = new VerifierOptions();
RecogVerifier verifier = RecogVerifier.create(verifierOpts, matchers, NullOutputStream.NULL_OUTPUT_STREAM);
verifier.verify();
Expand Down Expand Up @@ -134,7 +134,7 @@ public void verify1FailureAnd1SuccessfulExamples() throws ParseException {

// when
RecogParser recogParser = new RecogParser(true);
RecogMatchers matchers = recogParser.parse(new StringReader(xml), anyString());
RecogMatchers matchers = recogParser.parse(new StringReader(xml), anyUTF8String());
VerifierOptions verifierOpts = new VerifierOptions();
RecogVerifier verifier = RecogVerifier.create(verifierOpts, matchers, NullOutputStream.NULL_OUTPUT_STREAM);
verifier.verify();
Expand Down Expand Up @@ -163,7 +163,7 @@ public void verifySuccessfulExampleUntestedParamsFailCount() throws ParseExcepti

// when
RecogParser recogParser = new RecogParser(true);
RecogMatchers matchers = recogParser.parse(new StringReader(xml), anyString());
RecogMatchers matchers = recogParser.parse(new StringReader(xml), anyUTF8String());
VerifierOptions verifierOpts = new VerifierOptions();
RecogVerifier verifier = RecogVerifier.create(verifierOpts, matchers, NullOutputStream.NULL_OUTPUT_STREAM);
verifier.verify();
Expand Down Expand Up @@ -193,7 +193,7 @@ public void verify2FailureExamples() throws ParseException {

// when
RecogParser recogParser = new RecogParser(true);
RecogMatchers matchers = recogParser.parse(new StringReader(xml), anyString());
RecogMatchers matchers = recogParser.parse(new StringReader(xml), anyUTF8String());
VerifierOptions verifierOpts = new VerifierOptions();
RecogVerifier verifier = RecogVerifier.create(verifierOpts, matchers, NullOutputStream.NULL_OUTPUT_STREAM);
verifier.verify();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.util.regex.Pattern;
import org.junit.jupiter.api.Test;
import static com.rapid7.recog.RecogMatcher.pattern;
import static com.rapid7.recog.TestGenerators.anyString;
import static com.rapid7.recog.TestGenerators.anyUTF8String;
import static java.util.regex.Pattern.CASE_INSENSITIVE;
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
import static org.apache.commons.lang3.RandomUtils.nextInt;
Expand All @@ -25,12 +25,12 @@ public void matcherWithNullPattern() {

@Test
public void valueNameMustNotBeNull() {
assertThrows(NullPointerException.class, () -> new RecogMatcher(anyPattern()).addValue(null, anyString()));
assertThrows(NullPointerException.class, () -> new RecogMatcher(anyPattern()).addValue(null, anyUTF8String()));
}

@Test
public void parameterPositionMustBeGreaterThanZero() {
assertThrows(IllegalArgumentException.class, () -> new RecogMatcher(anyPattern()).addParam(nextInt(Integer.MIN_VALUE, 0), anyString()));
assertThrows(IllegalArgumentException.class, () -> new RecogMatcher(anyPattern()).addParam(nextInt(Integer.MIN_VALUE, 0), anyUTF8String()));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.util.List;
import org.junit.jupiter.api.Test;
import static com.rapid7.recog.RecogMatcher.pattern;
import static com.rapid7.recog.TestGenerators.anyString;
import static com.rapid7.recog.TestGenerators.anyUTF8String;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
Expand All @@ -31,7 +31,7 @@ public void matchesNullInput() {
@Test
public void matchesNoMatchers() {
// given
String fingerprint = anyString();
String fingerprint = anyUTF8String();
RecogMatchers matchers = new RecogMatchers();

// when
Expand All @@ -44,7 +44,7 @@ public void matchesNoMatchers() {
@Test
public void matchesNoMatches() {
// given
String fingerprint = anyString();
String fingerprint = anyUTF8String();
RecogMatchers matchers = new RecogMatchers();
matchers.add(new RecogMatcher(pattern("foo")));
matchers.add(new RecogMatcher(pattern("bar")));
Expand Down
7 changes: 5 additions & 2 deletions recog/src/test/java/com/rapid7/recog/TestGenerators.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ public class TestGenerators {

private static Random random = new Random();

public static String anyString() {
return RandomStringUtils.random(16);
public static String anyUTF8String() {
return anyUTF8String(16);
}
public static String anyUTF8String(int length) {
return RandomStringUtils.random(length, 0, 0x10ffff, true, true);
}

public static <E extends Enum<E>> E anyEnum(Class<E> enumType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import static com.rapid7.recog.RecogMatcher.pattern;
import static com.rapid7.recog.TestGenerators.anyString;
import static com.rapid7.recog.TestGenerators.anyUTF8String;
import static java.util.regex.Pattern.CASE_INSENSITIVE;
import static java.util.regex.Pattern.DOTALL;
import static java.util.regex.Pattern.MULTILINE;
Expand All @@ -34,7 +34,7 @@ public void noFingerprintsAreReadWhenInputIsEmpty() throws ParseException {
String xml = "<?xml version=\"1.0\"?><fingerprints/>";

// when
RecogMatchers patterns = new RecogParser().parse(new StringReader(xml), anyString());
RecogMatchers patterns = new RecogParser().parse(new StringReader(xml), anyUTF8String());

// then
assertThat(patterns.size(), is(0));
Expand All @@ -46,7 +46,7 @@ public void invalidXmlInputCausesExceptionToBeThrown() throws ParseException {
String xml = "<?xml version=\"1.0\"?>foo";

// when
assertThrows(ParseException.class, () -> new RecogParser().parse(new StringReader(xml), anyString()));
assertThrows(ParseException.class, () -> new RecogParser().parse(new StringReader(xml), anyUTF8String()));

// then - throws exception
}
Expand All @@ -68,7 +68,7 @@ public void validFingerprint() throws ParseException {
+ "</fingerprints>";

// when
RecogMatchers patterns = new RecogParser().parse(new StringReader(xml), anyString());
RecogMatchers patterns = new RecogParser().parse(new StringReader(xml), anyUTF8String());

// then
assertThat(patterns.size(), is(1));
Expand Down Expand Up @@ -99,7 +99,7 @@ public void twoValidFingerprints() throws ParseException {
+ "</fingerprints>";

// when
RecogMatchers patterns = new RecogParser().parse(new StringReader(xml), anyString());
RecogMatchers patterns = new RecogParser().parse(new StringReader(xml), anyUTF8String());

// then
assertThat(patterns.size(), is(2));
Expand Down Expand Up @@ -127,7 +127,7 @@ public void validFingerprintBase64EncodedExample() throws ParseException {
+ "</fingerprints>", exServiceVersion, exBase64);

// when
RecogMatchers patterns = new RecogParser().parse(new StringReader(xml), anyString());
RecogMatchers patterns = new RecogParser().parse(new StringReader(xml), anyUTF8String());

// then
assertThat(patterns.size(), is(1));
Expand All @@ -141,7 +141,7 @@ public void validFingerprintExternalExampleFile() throws ParseException {
// given
int exServiceVersion = 1;
String exText = String.format("Apache %d", exServiceVersion);
String exFilename = anyString();
String exFilename = anyUTF8String();
String xml = String.format("<?xml version=\"1.0\"?>\n"
+ "<fingerprints matches=\"http_header.server\">"
+ " <fingerprint pattern=\"^Apache (\\d)$\">\n"
Expand All @@ -159,7 +159,7 @@ public void validFingerprintExternalExampleFile() throws ParseException {
.thenReturn(exText.getBytes(StandardCharsets.US_ASCII));

// when
RecogMatchers patterns = new RecogParser().parse(new StringReader(xml), anyString());
RecogMatchers patterns = new RecogParser().parse(new StringReader(xml), anyUTF8String());

// then
assertThat(patterns.size(), is(1));
Expand Down Expand Up @@ -199,7 +199,7 @@ public void invalidFingerprintsIgnored() throws ParseException {
+ "</fingerprints>";

// when
RecogMatchers patterns = new RecogParser().parse(new StringReader(xml), anyString());
RecogMatchers patterns = new RecogParser().parse(new StringReader(xml), anyUTF8String());

// then
assertThat(patterns.size(), is(2));
Expand All @@ -222,7 +222,7 @@ public void patternIsRequired() throws ParseException {
+ "</fingerprints>";

// when
RecogMatchers patterns = new RecogParser().parse(new StringReader(xml), anyString());
RecogMatchers patterns = new RecogParser().parse(new StringReader(xml), anyUTF8String());

// then
assertThat(patterns.size(), is(0));
Expand All @@ -245,7 +245,7 @@ public void patternMustBeValid() throws ParseException {
+ "</fingerprints>";

// when
RecogMatchers patterns = new RecogParser().parse(new StringReader(xml), anyString());
RecogMatchers patterns = new RecogParser().parse(new StringReader(xml), anyUTF8String());

// then
assertThat(patterns.size(), is(0));
Expand All @@ -268,7 +268,7 @@ public void emptyFlagsIgnored() throws ParseException {
+ "</fingerprints>";

// when
RecogMatchers patterns = new RecogParser().parse(new StringReader(xml), anyString());
RecogMatchers patterns = new RecogParser().parse(new StringReader(xml), anyUTF8String());

// then
assertThat(patterns.size(), is(1));
Expand All @@ -292,7 +292,7 @@ public void unknownFlagIgnored() throws ParseException {
+ "</fingerprints>";

// when
RecogMatchers patterns = new RecogParser().parse(new StringReader(xml), anyString());
RecogMatchers patterns = new RecogParser().parse(new StringReader(xml), anyUTF8String());

// then
assertThat(patterns.size(), is(1));
Expand All @@ -318,7 +318,7 @@ public void paramZeroPositionWithNoValueFailsWhenStrict() {

// when
Exception exception = assertThrows(ParseException.class, () -> {
new RecogParser(true).parse(new StringReader(xml), anyString());
new RecogParser(true).parse(new StringReader(xml), anyUTF8String());
}, expectedMessage);

// then
Expand Down Expand Up @@ -346,7 +346,7 @@ public void paramNonZeroPositionWithValueFailsWhenStrict() {

// when
Exception exception = assertThrows(ParseException.class, () -> {
new RecogParser(true).parse(new StringReader(xml), anyString());
new RecogParser(true).parse(new StringReader(xml), anyUTF8String());
}, expectedMessage);

// then
Expand Down Expand Up @@ -375,7 +375,7 @@ public void invalidFingerprintBase64EncodedExampleFailsWhenStrict() {

// when
Exception exception = assertThrows(IllegalArgumentException.class, () -> {
new RecogParser(true).parse(new StringReader(xml), anyString());
new RecogParser(true).parse(new StringReader(xml), anyUTF8String());
});

// then
Expand All @@ -385,7 +385,7 @@ public void invalidFingerprintBase64EncodedExampleFailsWhenStrict() {
@Test
public void missingExternalExampleFileFailsWhenStrict() {
// given
String exFilename = anyString();
String exFilename = anyUTF8String();
String xml = String.format("<?xml version=\"1.0\"?>\n"
+ "<fingerprints matches=\"http_header.server\">"
+ " <fingerprint pattern=\"^Apache (\\d)$\">\n"
Expand All @@ -397,7 +397,7 @@ public void missingExternalExampleFileFailsWhenStrict() {
+ " <param pos=\"1\" name=\"service.version\"/>\n"
+ " </fingerprint>\n"
+ "</fingerprints>", exFilename);
String name = anyString();
String name = anyUTF8String();
String expectedMessage = String.format("Unable to process fingerprint example file '%s'", Paths.get(name, exFilename));

// when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.rapid7.recog.RecogType;
import org.junit.jupiter.api.Test;
import static com.rapid7.recog.TestGenerators.anyEnum;
import static com.rapid7.recog.TestGenerators.anyString;
import static com.rapid7.recog.TestGenerators.anyUTF8String;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
Expand All @@ -18,7 +18,7 @@ public class CompositeFingerprintMatchersProviderTest {
@Test
public void getMatchersNoProviders() {
// given
String name = anyString();
String name = anyUTF8String();
RecogType type = anyEnum(RecogType.class);
CompositeRecogMatchersProvider provider = new CompositeRecogMatchersProvider();

Expand All @@ -32,7 +32,7 @@ public void getMatchersNoProviders() {
@Test
public void getMatchersSingleProviderNoMatch() {
// given
String name = anyString();
String name = anyUTF8String();
RecogType type = anyEnum(RecogType.class);
IRecogMatchersProvider provider = mock(IRecogMatchersProvider.class);
given(provider.getMatchers(name, type)).willReturn(null);
Expand All @@ -48,7 +48,7 @@ public void getMatchersSingleProviderNoMatch() {
@Test
public void getMatchersSingleProviderMatch() {
// given
String name = anyString();
String name = anyUTF8String();
RecogType type = anyEnum(RecogType.class);
IRecogMatchersProvider provider = mock(IRecogMatchersProvider.class);
RecogMatchers result = mock(RecogMatchers.class);
Expand All @@ -65,7 +65,7 @@ public void getMatchersSingleProviderMatch() {
@Test
public void getMatchersMultipleProviderNoMatch() {
// given
String name = anyString();
String name = anyUTF8String();
RecogType type = anyEnum(RecogType.class);
IRecogMatchersProvider provider1 = mock(IRecogMatchersProvider.class);
IRecogMatchersProvider provider2 = mock(IRecogMatchersProvider.class);
Expand All @@ -85,7 +85,7 @@ public void getMatchersMultipleProviderNoMatch() {
@Test
public void getMatchersMultipleProviderSingleMatchFirst() {
// given
String name = anyString();
String name = anyUTF8String();
RecogType type = anyEnum(RecogType.class);
IRecogMatchersProvider provider1 = mock(IRecogMatchersProvider.class);
IRecogMatchersProvider provider2 = mock(IRecogMatchersProvider.class);
Expand All @@ -106,7 +106,7 @@ public void getMatchersMultipleProviderSingleMatchFirst() {
@Test
public void getMatchersMultipleProviderSingleMatchLast() {
// given
String name = anyString();
String name = anyUTF8String();
RecogType type = anyEnum(RecogType.class);
IRecogMatchersProvider provider1 = mock(IRecogMatchersProvider.class);
IRecogMatchersProvider provider2 = mock(IRecogMatchersProvider.class);
Expand All @@ -127,7 +127,7 @@ public void getMatchersMultipleProviderSingleMatchLast() {
@Test
public void getMatchersMultipleProviderMultipleMatchesFirstIsReturned() {
// given
String name = anyString();
String name = anyUTF8String();
RecogType type = anyEnum(RecogType.class);
IRecogMatchersProvider provider1 = mock(IRecogMatchersProvider.class);
IRecogMatchersProvider provider2 = mock(IRecogMatchersProvider.class);
Expand Down Expand Up @@ -156,7 +156,7 @@ public void getMatchersMultipleProviderMultipleMatchesFirstIsReturned() {
@Test
public void getMatchersMultipleProviderMultipleMatchesFirstIsReturnedWhenNotFirst() {
// given
String name = anyString();
String name = anyUTF8String();
RecogType type = anyEnum(RecogType.class);
IRecogMatchersProvider provider1 = mock(IRecogMatchersProvider.class);
IRecogMatchersProvider provider2 = mock(IRecogMatchersProvider.class);
Expand Down