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

junit: Generate temporary seeds with deterministic names #744

Merged
merged 1 commit into from
Jun 19, 2023
Merged
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 @@ -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 @@ -212,8 +216,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);
fmeum marked this conversation as resolved.
Show resolved Hide resolved
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