Improved fuzz test to find an actual code bug (previous bug was in the test
<g>).
This commit is contained in:
@@ -7,7 +7,7 @@ class FlexNBD
|
|||||||
|
|
||||||
def initialize(bin, ip, port)
|
def initialize(bin, ip, port)
|
||||||
@bin = bin
|
@bin = bin
|
||||||
@debug = `#{@bin} serve --help` =~ /--debug/ ? "--debug" : ""
|
@debug = "--verbose" # tests always run in debug mode!
|
||||||
@valgrind = ENV['VALGRIND'] ? "valgrind " : ""
|
@valgrind = ENV['VALGRIND'] ? "valgrind " : ""
|
||||||
@bin = "#{@valgrind}#{@bin}"
|
@bin = "#{@valgrind}#{@bin}"
|
||||||
raise "#{bin} not executable" unless File.executable?(bin)
|
raise "#{bin} not executable" unless File.executable?(bin)
|
||||||
@@ -25,6 +25,7 @@ class FlexNBD
|
|||||||
"--sock #{ctrl} "\
|
"--sock #{ctrl} "\
|
||||||
"#{@debug} "\
|
"#{@debug} "\
|
||||||
"#{acl.join(' ')}"
|
"#{acl.join(' ')}"
|
||||||
|
#STDERR.print "#{cmd}\n"
|
||||||
@pid = fork do
|
@pid = fork do
|
||||||
exec(cmd)
|
exec(cmd)
|
||||||
end
|
end
|
||||||
@@ -42,15 +43,17 @@ class FlexNBD
|
|||||||
end
|
end
|
||||||
|
|
||||||
def read(offset, length)
|
def read(offset, length)
|
||||||
IO.popen("#{@bin} read "\
|
|
||||||
|
out = IO.popen("#{@bin} read "\
|
||||||
"--addr #{ip} "\
|
"--addr #{ip} "\
|
||||||
"--port #{port} "\
|
"--port #{port} "\
|
||||||
"--from #{offset} "\
|
"--from #{offset} "\
|
||||||
"#{@debug} "\
|
"#{@debug} "\
|
||||||
"--size #{length}","r") do |fh|
|
"--size #{length}","r") do |fh|
|
||||||
return fh.read
|
fh.read
|
||||||
end
|
end
|
||||||
raise "read failed" unless $?.success?
|
raise IOError.new "NBD read failed" unless $?.success?
|
||||||
|
out
|
||||||
end
|
end
|
||||||
|
|
||||||
def write(offset, data)
|
def write(offset, data)
|
||||||
@@ -62,7 +65,7 @@ class FlexNBD
|
|||||||
"--size #{data.length}","w") do |fh|
|
"--size #{data.length}","w") do |fh|
|
||||||
fh.write(data)
|
fh.write(data)
|
||||||
end
|
end
|
||||||
raise "write failed" unless $?.success?
|
raise IOError.new "NBD write failed" unless $?.success?
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
15
tests/fuzz
15
tests/fuzz
@@ -22,7 +22,7 @@ end
|
|||||||
testname_local = "#{$0}.test.#{$$}.local"
|
testname_local = "#{$0}.test.#{$$}.local"
|
||||||
testname_serve = "#{$0}.test.#{$$}.serve"
|
testname_serve = "#{$0}.test.#{$$}.serve"
|
||||||
[testname_local, testname_serve].each do |name|
|
[testname_local, testname_serve].each do |name|
|
||||||
File.open(name, "w+") { |fh| fh.seek(test_size-1, IO::SEEK_SET); fh.write("0") }
|
File.open(name, "w+") { |fh| fh.seek(test_size-1, IO::SEEK_SET); fh.write("\0") }
|
||||||
end
|
end
|
||||||
|
|
||||||
@local = File.open(testname_local, "r+")
|
@local = File.open(testname_local, "r+")
|
||||||
@@ -55,15 +55,22 @@ repetitions.times do |n|
|
|||||||
length &= 0xfffff000 if CHEAT_AND_ROUND_DOWN
|
length &= 0xfffff000 if CHEAT_AND_ROUND_DOWN
|
||||||
offset = rand(test_size - length)
|
offset = rand(test_size - length)
|
||||||
offset &= 0xfffff000 if CHEAT_AND_ROUND_DOWN
|
offset &= 0xfffff000 if CHEAT_AND_ROUND_DOWN
|
||||||
content = (n%2 == 1) ? ("x" * length) : ("\0" * length)
|
|
||||||
|
content = (n%2 == 0) ? ("\0" * length) : ( ((n/2)&255).chr * length)
|
||||||
|
|
||||||
@local.seek(offset, IO::SEEK_SET)
|
@local.seek(offset, IO::SEEK_SET)
|
||||||
@local.write(content)
|
@local.write(content)
|
||||||
|
@local.fsync
|
||||||
|
|
||||||
@serve.write(offset, content)
|
@serve.write(offset, content)
|
||||||
|
|
||||||
if @serve.read(offset, length) != content
|
check_read = @serve.read(offset, length)
|
||||||
STDERR.print "After pass #{n}: Didn't read back what we wrote!"
|
if check_read != content
|
||||||
|
STDERR.print "After pass #{n}: Didn't read back what we wrote!\n"
|
||||||
|
STDERR.print "*** We wrote these #{content.length} bytes...\n"
|
||||||
|
IO.popen("hexdump", "w") { |io| io.print(content) }
|
||||||
|
STDERR.print "*** But we got back these #{check_read.length} bytes...\n"
|
||||||
|
IO.popen("hexdump", "w") { |io| io.print(check_read) }
|
||||||
exit 1
|
exit 1
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user