Permalink
Browse files

test case for wss

  • Loading branch information...
1 parent f37e083 commit 072e64dc600228bac0b305481b74a0ec33a48c59 @mplatov committed May 15, 2011
Showing with 4,043 additions and 11 deletions.
  1. +3,987 −0 lib/cacert.pem
  2. +31 −11 lib/ws_cli.rb
  3. +1 −0 lib/ws_proto_hybi07.rb
  4. +24 −0 spec/wss_test_spec.rb
View
3,987 lib/cacert.pem
3,987 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
View
42 lib/ws_cli.rb
@@ -10,6 +10,7 @@
end
require 'socket'
+require 'openssl'
require 'logger'
require 'ws_proto_hybi07'
require 'pp'
@@ -22,6 +23,7 @@ def initialize(logger, options)
@port = options.delete(:port)
@timeout = options.delete(:timeout) || 40
@version = options.delete(:proto) || :hybi07
+ @secure = options[:secure] if options.has_key?(:secure)
@proto = ProtoHybi07.new(@logger, options)
@extensions = []
@@ -92,18 +94,32 @@ def with_sock
def connect_to(host, port, timeout=nil)
puts
@logger.debug("host #{host} port #{port}")
- addr = Socket.getaddrinfo(host, nil)
- sock = Socket.new(Socket.const_get(addr[0][0]), Socket::SOCK_STREAM, 0)
- if timeout
- secs = Integer(timeout)
- usecs = Integer((timeout - secs) * 1_000_000)
- optval = [secs, usecs].pack("l_2")
- sock.setsockopt Socket::SOL_SOCKET, Socket::SO_RCVTIMEO, optval
- sock.setsockopt Socket::SOL_SOCKET, Socket::SO_SNDTIMEO, optval
- end
- sock.connect(Socket.pack_sockaddr_in(port, addr[0][3]))
- sock
+ if @secure
+ sock = TCPSocket.new(host, port)
+ ssl_context = OpenSSL::SSL::SSLContext.new()
+ puts "2DO add certificate verification"
+# ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE
+ ssl_context.verify_mode = OpenSSL::SSL::VERIFY_PEER
+ ssl_context.ca_file = File.join(File.dirname(__FILE__), "cacert.pem")
+ sslsocket = OpenSSL::SSL::SSLSocket.new(sock, ssl_context)
+ sslsocket.sync_close = true
+ sslsocket.connect
+ @old_socket = sock
+ return sslsocket
+ else
+ addr = Socket.getaddrinfo(host, nil)
+ sock = Socket.new(Socket.const_get(addr[0][0]), Socket::SOCK_STREAM, 0)
+ if timeout
+ secs = Integer(timeout)
+ usecs = Integer((timeout - secs) * 1_000_000)
+ optval = [secs, usecs].pack("l_2")
+ sock.setsockopt Socket::SOL_SOCKET, Socket::SO_RCVTIMEO, optval
+ sock.setsockopt Socket::SOL_SOCKET, Socket::SO_SNDTIMEO, optval
+ end
+ sock.connect(Socket.pack_sockaddr_in(port, addr[0][3]))
+ return sock
+ end
end
def connect_i
@@ -123,6 +139,10 @@ def write_to_socket(cmd)
rescue Exception => e
@logger.error("exception #{e.message}")
@sock.close if @sock
+ if @old_socket
+ @old_socket.close
+ @old_socket = nil
+ end
@sock = nil
return nil
end
View
1 lib/ws_proto_hybi07.rb
@@ -10,6 +10,7 @@ def initialize(logger, options)
@compression = options.delete(:frame_compression)
@origin = options.delete(:origin) || "localhost"
@masking_disabled = options.delete(:masking_disabled)
+ @secure = options.delete(:secure)
@big_endian = true if [1].pack("I") == [1].pack("N")
end
View
24 spec/wss_test_spec.rb
@@ -0,0 +1,24 @@
+
+require 'ws_cli'
+
+host = 'wss.websocketstest.com'
+port = 443
+proto = :hybi07
+
+describe WSClient do
+
+ before :each do
+ log = Logger.new(STDOUT)
+# log.level = Logger::DEBUG
+ log.level = Logger::ERROR
+ @c = WSClient.new(log, {:host => host, :port => port, :proto => proto, :frame_compression => true, :secure => true})
+ end
+
+ it "should connect with SSL" do
+ @c.connect('/test', ['org.ietf.websocket.test-echo-fragment'])
+ str ="test 1234567890 test 1234567890"
+ @c.write_data(str)
+ (@c.read_data == str).should be_true
+ end
+
+end

0 comments on commit 072e64d

Please sign in to comment.