Skip to content

Commit

Permalink
Merge pull request #3 from QuiltMC/v2-reader-validate-version
Browse files Browse the repository at this point in the history
validate version header in V2 reader
  • Loading branch information
ix0rai authored Oct 26, 2024
2 parents 95accea + bbe7cd9 commit 1c1501d
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 2 deletions.
3 changes: 3 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[versions]
asm = "9.7"
log4j = "2.19.0"
junit = "5.9.3"

[libraries]
asm = { module = "org.ow2.asm:asm", version.ref = "asm" }
Expand All @@ -10,3 +11,5 @@ asm_util = { module = "org.ow2.asm:asm-util", version.ref = "asm" }

log4j_api = { module = "org.apache.logging.log4j:log4j-api", version.ref = "log4j" }
log4j_core = { module = "org.apache.logging.log4j:log4j-core", version.ref = "log4j" }

junit = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit" }
3 changes: 3 additions & 0 deletions unpick-format-utils/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dependencies {
testImplementation(libs.junit)
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,13 @@ public UnpickV2Reader(InputStream definitionsStream)
*/
public void accept(Visitor visitor)
{
try(LineNumberReader reader = new LineNumberReader(new InputStreamReader(definitionsStream)))
try(InputStreamReader streamReader = new InputStreamReader(definitionsStream))
{
validateVersion(streamReader);
LineNumberReader reader = new LineNumberReader(streamReader);

Iterator<String[]> lineTokensIter = reader.lines()
.skip(1) //Skip version
.skip(1) // we already checked the version
.map(s -> stripComment(s.trim()))
.filter(s -> !s.isEmpty()) //Discard empty lines & lines that are empty once comments are stripped
.map(l -> Arrays.stream(WHITESPACE_SPLITTER.split(l)).filter(s -> !s.isEmpty()).toArray(String[]::new)) //Tokenise lines
Expand Down Expand Up @@ -98,6 +101,27 @@ public void accept(Visitor visitor)
}
}

private void validateVersion(InputStreamReader definitionStream) throws IOException {
// only consume chars for the version
char[] versionChars = new char [2];
if (definitionStream.read(versionChars) == 2 && versionChars[0] == 'v')
{
switch (versionChars[1])
{
case '1':
throw new UnpickSyntaxException(1, "Incompatible version! (v1 data passed to v2 reader)");

case '2':
return;

default :
throw new UnpickSyntaxException(1, "Unknown version " + versionChars[1]);
}
}
else
throw new UnpickSyntaxException(1, "Missing or invalid version");
}

private void visitParameterConstantGroupDefinition(Visitor visitor, String[] tokens, int lineNumber)
{
if (lastTargetMethodVisitor == null)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package daomephsta.unpick.tests;

import daomephsta.unpick.constantmappers.datadriven.parser.UnpickSyntaxException;
import daomephsta.unpick.constantmappers.datadriven.parser.v2.UnpickV2Reader;
import daomephsta.unpick.constantmappers.datadriven.parser.v2.UnpickV2Writer;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import static org.junit.jupiter.api.Assertions.assertThrows;

public class TestV2Reader {
private final Path missingHeader;
private final Path validSyntax;
private final Path invalidKeyword;

public TestV2Reader() throws URISyntaxException {
Path dir = getResource("/v2_test_files");
missingHeader = dir.resolve("missing_header.unpick");
validSyntax = dir.resolve("valid_syntax.unpick");
invalidKeyword = dir.resolve("invalid_keyword.unpick");
}

private static Path getResource(String name) throws URISyntaxException {
return Paths.get(TestV2Reader.class.getResource(name).toURI());
}

@Test
void testValidRead() throws IOException {
File file = validSyntax.toFile();

try (UnpickV2Reader reader = new UnpickV2Reader(Files.newInputStream(file.toPath()))) {
reader.accept(new UnpickV2Writer());
}
}

@Test
void testMissingHeader() throws IOException {
File file = missingHeader.toFile();

try (UnpickV2Reader reader = new UnpickV2Reader(Files.newInputStream(file.toPath()))) {
assertThrows(UnpickSyntaxException.class, () -> reader.accept(new UnpickV2Writer()));
}
}

@Test
void testInvalidKeyword() throws IOException {
File file = invalidKeyword.toFile();

try (UnpickV2Reader reader = new UnpickV2Reader(Files.newInputStream(file.toPath()))) {
assertThrows(UnpickSyntaxException.class, () -> reader.accept(new UnpickV2Writer()));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
v2

gaming flag org/quiltmc/Gaming GAMING
gaming flag org/quiltmc/Gaming NOT_GAMING

target_method org/quiltmc/Gaming getFlag (I)Z
param 0 flag
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
constant flag org/quiltmc/Gaming GAMING
constant flag org/quiltmc/Gaming NOT_GAMING

target_method org/quiltmc/Gaming getFlag (I)Z
param 0 flag
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
v2

constant flag org/quiltmc/Gaming GAMING
constant flag org/quiltmc/Gaming NOT_GAMING

target_method org/quiltmc/Gaming getFlag (I)Z
param 0 flag

0 comments on commit 1c1501d

Please sign in to comment.