Skip to content

Commit

Permalink
Correct matching condition when finding server key in the test framework
Browse files Browse the repository at this point in the history
  • Loading branch information
ono-max committed Oct 24, 2022
1 parent 418148b commit 253e73c
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions test/support/protocol_test_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -800,18 +800,10 @@ def initialize s
def handshake port, path
key = SecureRandom.hex(11)
@sock.print "GET #{path} HTTP/1.1\r\nHost: 127.0.0.1:#{port}\r\nConnection: Upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: #{key}==\r\n\r\n"
res = nil
loop do
res = @sock.readpartial 4092
break unless res.match?(/out|input/)
end
server_key = get_server_key

if res.match(/^Sec-WebSocket-Accept: (.*)\r\n/)
correct_key = Base64.strict_encode64 Digest::SHA1.digest "#{key}==258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
raise "The Sec-WebSocket-Accept value: #{$1} is not valid" unless $1 == correct_key
else
raise "Unknown response: #{res}"
end
correct_key = Base64.strict_encode64 Digest::SHA1.digest "#{key}==258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
raise "The Sec-WebSocket-Accept value: #{$1} is not valid" unless server_key == correct_key
end

def send msg
Expand Down Expand Up @@ -882,6 +874,19 @@ def send_close_connection
def close
@sock.close
end

private

def get_server_key
Timeout.timeout(ProtocolTestCase::TIMEOUT_SEC) do
loop do
res = @sock.readpartial 4092
if res.match(/^Sec-WebSocket-Accept: (.*)\r\n/)
return $1
end
end
end
end
end

# When constant variables are referred from modules, they have to be defined outside the class.
Expand Down

0 comments on commit 253e73c

Please sign in to comment.