Created
July 11, 2017 12:33
-
-
Save 166MMX/b74379fd6bc58257e5033765aab769a7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.nio.ByteBuffer | |
import java.nio.channels.SeekableByteChannel | |
import java.nio.file.Files | |
import java.nio.file.Path | |
import java.nio.file.Paths | |
import java.security.SecureRandom | |
class FileFuzzer { | |
private static final Random random = new SecureRandom() | |
static void main(String[] args) { | |
iterate(Paths.get(args[0]), args[1] as int, args[2] as int) | |
} | |
static void iterate(final Path sourceFile, final int count, final int start = 1) { | |
for (int i = start; i < count; i++) { | |
final resultName = "fuzz_${i}_${sourceFile.fileName.toString()}" | |
final resultFile = sourceFile.resolveSibling(resultName) | |
Files.copy(sourceFile, resultFile) | |
mutate(resultFile) | |
} | |
} | |
static void mutate(final Path file, final int chunkCapacity = 1024 * 4) { | |
final long size = Files.size(file) | |
final long start = size * random.nextDouble() | |
final long length = (size - start) * random.nextDouble() | |
final int count = Math.floorDiv(length, chunkCapacity) | |
final int remaining = Math.floorMod(length, chunkCapacity) | |
final channel = Files.newByteChannel(file) | |
channel.position(start) | |
write(channel, remaining) | |
write(channel, chunkCapacity, count) | |
channel.close() | |
} | |
static void write(SeekableByteChannel channel, int size, int count = 1) { | |
final buffer = ByteBuffer.allocateDirect(size) | |
for (int i = 0; i < count; i++) { | |
random.nextBytes(buffer.array()) | |
channel.write(buffer) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment