Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix XContentTypeTests where version was a negative byte #63382

Merged
merged 3 commits into from
Oct 8, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -97,7 +97,7 @@ public void testFromRubbish() throws Exception {
}

public void testVersionedMediaType() {
String version = String.valueOf(Math.abs(randomByte()));
String version = String.valueOf(randomNonNegativeByte());
assertThat(XContentType.fromMediaType("application/vnd.elasticsearch+json;compatible-with=" + version),
equalTo(XContentType.JSON));
assertThat(XContentType.fromMediaType("application/vnd.elasticsearch+cbor;compatible-with=" + version),
Expand All @@ -119,7 +119,7 @@ public void testVersionedMediaType() {
}

public void testVersionParsing() {
byte version = (byte) Math.abs(randomByte());
byte version = randomNonNegativeByte();
assertThat(XContentType.parseVersion("application/vnd.elasticsearch+json;compatible-with=" + version),
equalTo(version));
assertThat(XContentType.parseVersion("application/vnd.elasticsearch+cbor;compatible-with=" + version),
Expand All @@ -146,12 +146,12 @@ public void testUnrecognizedParameter() {
is(nullValue())); }

public void testMediaTypeWithoutESSubtype() {
String version = String.valueOf(Math.abs(randomByte()));
String version = String.valueOf(randomNonNegativeByte());
assertThat(XContentType.fromMediaType("application/json;compatible-with=" + version), nullValue());
}

public void testAnchoring() {
String version = String.valueOf(Math.abs(randomByte()));
String version = String.valueOf(randomNonNegativeByte());
assertThat(XContentType.fromMediaType("sth_application/json;compatible-with=" + version + ".0"), nullValue());
assertThat(XContentType.fromMediaType("sth_application/json;compatible-with=" + version + "_sth"), nullValue());
assertThat(XContentType.fromMediaType("application/json;compatible-with=" + version + "_sth"), nullValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,11 @@ public static byte randomByte() {
return (byte) random().nextInt();
}

public static byte randomNonNegativeByte() {
byte randomByte = (byte) random().nextInt();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: just use randomByte()?

return (byte) (randomByte == Byte.MIN_VALUE ? 0 : Math.abs(randomByte));
}

/**
* Helper method to create a byte array of a given length populated with random byte values
*
Expand Down