Flush bad write data from the client socket.

If the client makes a write that's out of range, by the time we get to
validate the message at the server end the client has already stuffed
the socket with data we can't use, so we have to flush it.

This patch also fixes a potential problem in the acceptance tests where
the error field was being returned as an array rather than a value.
This commit is contained in:
Alex Young
2012-07-15 23:19:12 +01:00
parent f5850e5aaf
commit d0b39cce08
4 changed files with 78 additions and 6 deletions

View File

@@ -4,6 +4,9 @@
# Connect, read the hello then make a write request with an impossible
# (from,len) pair. We expect an error response, and not to be
# disconnected.
#
# We then expect to be able to issue a successful write: the destination
# has to flush the data in the socket.
require 'flexnbd/fake_source'
include FlexNBD
@@ -11,12 +14,19 @@ include FlexNBD
addr, port = *ARGV
client = FakeSource.new( addr, port, "Timed out connecting" )
client.read_hello
client.write_write_request( 1 << 31, 1 << 31, "myhandle" )
hello = client.read_hello
client.write_write_request( hello[:size]+1, 32, "myhandle" )
client.write_data("1"*32)
response = client.read_response
fail "Not an error" if response[:error] == 0
fail "Wrong handle" unless "myhandle" == response[:handle]
client.write_write_request( 0, 32 )
client.write_data( "2"*32 )
success_response = client.read_response
fail "Second write failed" unless success_response[:error] == 0
client.close
exit(0)

View File

@@ -26,9 +26,17 @@ module FlexNBD
def read_hello()
timing_out( FlexNBD::MS_HELLO_TIME_SECS,
"Timed out waiting for hello." ) do
fail "No hello." unless (hello = @sock.read( 152 )) &&
hello.length==152
hello
fail "No hello." unless (hello = @sock.read( 152 )) &&
hello.length==152
magic_s = hello[0..7]
ignore_s= hello[8..15]
size_s = hello[16..23]
size_h, size_l = size_s.unpack("NN")
size = (size_h << 32) + size_l
return { :magic => magic_s, :size => size }
end
end
@@ -99,7 +107,7 @@ module FlexNBD
{
:magic => magic,
:error => error_s.unpack("N"),
:error => error_s.unpack("N").first,
:handle => handle
}
end