Skip to content

Commit

Permalink
junit: Generate temporary seeds with deterministic names
Browse files Browse the repository at this point in the history
This removes a potential source of non-deterministic behavior while
fuzzing with a fixed `-seed`.
  • Loading branch information
fmeum committed May 24, 2023
1 parent be1a7e5 commit 0ec9b46
Showing 1 changed file with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -201,8 +205,21 @@ static FuzzTestExecutor fromContext(ExtensionContext extensionContext) {
}

public void addSeed(byte[] bytes) throws IOException {
Path seed = Files.createTempFile(javaSeedsDir, "seed", null);
Files.write(seed, bytes);
Path tmpSeed = Files.createTempFile(javaSeedsDir, "tmp-seed-", null);
Files.write(tmpSeed, bytes);

byte[] hash;
try {
hash = MessageDigest.getInstance("SHA-256").digest(bytes);
} catch (NoSuchAlgorithmException e) {
// Always available.
throw new IllegalStateException(e);
}
// Case-insensitive file systems lose at most one bit of entropy per character, that is, the
// resulting file name still encodes more than 200 bits of entropy.
String basename = "seed-" + Base64.getUrlEncoder().withoutPadding().encodeToString(hash);
Path seed = javaSeedsDir.resolve(basename);
Files.move(tmpSeed, seed, StandardCopyOption.REPLACE_EXISTING);
}

@SuppressWarnings("OptionalGetWithoutIsPresent")
Expand Down

0 comments on commit 0ec9b46

Please sign in to comment.