Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
248 views
in Technique[技术] by (71.8m points)

java - CRC32 checksum different during runtime and JUnit test

I currently have a strange problem. I read a file using the following function, create an object from it and store the file content as a byte array in the object as well.

public static Iodd readFile(File file) throws IOException {
    if (!file.getName().endsWith(".xml"))
        throw new InvalidObjectException("Not a valid IODD XML file");

    String s = FileUtils.readFileToString(file, StandardCharsets.UTF_8);
    Iodd i = new XmlMapper().readValue(s, Iodd.class);

    i.setFile(Files.readAllBytes(file.toPath()));

    return i;
}

In the file there is a CRC32 checksum. During the runtime of the program I can reproduce the checksum without problems using the java.util.zip.CRC32 class. But if I want to test the same way using a JUnit test now, I get a completely different checksum.

The test file you can find here. Please use the the XML-file without country code.

Am I missing something?

I have already checked if the byte arrays are different in the JUnit test and during runtime but everything matches. For testing purpose I created a MD5 checksum from the file content and check it with the MD5 from the JUnit test but they are equal.

Function for interest

This is the function which calculates the CRC32 checksum. The method removeStamp removes the crc-value from the stamp tag.

public static boolean isValidInternal(final byte[] data, final long checkCrc) {
    CRC32 c = new CRC32();
    c.update(removeStamp(data));

    return (c.getValue() == checkCrc);
}

Update 1

I add a new class with a main and add an output to the crc calculation method.

public class App {
    private final static String TESTFILE = "testfile-iodd-tv7105.xml";

    public static void main(String[] args) throws IOException {
        File f = new File(Iodd.class.getClassLoader().getResource(TESTFILE).getFile());
        Iodd i = Iodd.readFile(f);
        System.out.println(i.isValid());
    }
}

Output from the IntelliJ Run:
checkCrc: 1195433981
CRC32: 1195433981
true

Output from the Maven test command in IntelliJ:
checkCrc: 1195433981
CRC32: 2019633554

question from:https://stackoverflow.com/questions/65901674/crc32-checksum-different-during-runtime-and-junit-test

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Thank you. I hope the problem is solved now. mvn test runs without problems from IntelliJ and Bash.

The solution was that I set the following configurations for the maven-surefire-plugin in the pom.xml:

  • reuseForks: false
  • forkCount: 0
  • useSystemClassLoader: false (surpress warning that this option is not used when reuseForks option is false)
<plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.22.1</version>
    <configuration>
        <reuseForks>false</reuseForks>
        <forkCount>0</forkCount>
        <useSystemClassLoader>false</useSystemClassLoader>
    </configuration>
</plugin>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...