Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/rb/lib/thrift/transport/ssl_socket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ def initialize(host = "localhost", port = 9090, timeout = nil, ssl_context = nil
attr_accessor :server_hostname

def open
raise TransportException.new(TransportException::ALREADY_OPEN, "Socket already open") if open?

deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + @timeout unless @timeout.nil? || @timeout == 0
socket = connect_socket(deadline)

Expand Down
10 changes: 7 additions & 3 deletions lib/rb/lib/thrift/transport/unix_socket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@ def initialize(path, timeout = nil)
end

def open
@handle = ::UNIXSocket.new(@path)
rescue StandardError
raise TransportException.new(TransportException::NOT_OPEN, "Could not open UNIX socket at #{@path}")
raise TransportException.new(TransportException::ALREADY_OPEN, "Socket already open") if open?

begin
@handle = ::UNIXSocket.new(@path)
rescue StandardError
raise TransportException.new(TransportException::NOT_OPEN, "Could not open UNIX socket at #{@path}")
end
end

def to_s
Expand Down
35 changes: 11 additions & 24 deletions lib/rb/spec/socket_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,30 +51,6 @@
@socket.open
end

it "should reject a second open without orphaning the live connection" do
allow(Addrinfo).to receive(:foreach).and_call_original
server = TCPServer.new("127.0.0.1", 0)
socket = Thrift::Socket.new("127.0.0.1", server.addr[1])
peer = nil

socket.open
peer = server.accept
first_handle = socket.handle

expect { socket.open }.to raise_error(Thrift::TransportException, /already open/) do |error|
expect(error.type).to eq(Thrift::TransportException::ALREADY_OPEN)
end
expect(socket.handle).to equal(first_handle)

socket.close
expect(peer.read).to eq("")
ensure
socket&.close
first_handle&.close unless first_handle&.closed?
peer&.close
server&.close
end

it "should accept host/port options" do
handle = double("Handle", closed?: false)
allow(handle).to receive(:close)
Expand Down Expand Up @@ -215,4 +191,15 @@
peer&.close
end
end

describe "with a live connection" do
let(:listener) { TCPServer.new("127.0.0.1", 0) }
let(:transport) { Thrift::Socket.new("127.0.0.1", listener.local_address.ip_port, 1) }

def accept_peer
listener.accept
end

it_should_behave_like "a socket rejecting duplicate opens"
end
end
26 changes: 26 additions & 0 deletions lib/rb/spec/socket_spec_shared.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,32 @@

require "spec_helper"

# Expects the including group to define +listener+ (a listening ::TCPServer or
# ::UNIXServer), +transport+ (an unopened client transport connecting to it),
# and +accept_peer+ (the server side of the transport's first connection).
shared_examples_for "a socket rejecting duplicate opens" do
it "should reject a second open without connecting again or orphaning the live connection" do
transport.open
peer = accept_peer
first_handle = transport.handle

expect { transport.open }.to raise_error(Thrift::TransportException, "Socket already open") do |error|
expect(error.type).to eq(Thrift::TransportException::ALREADY_OPEN)
end
expect(transport.handle).to equal(first_handle)
expect(IO.select([listener], nil, nil, 0)).to be_nil

transport.close
expect(first_handle).to be_closed
expect(peer.read).to eq("")
ensure
transport.close
first_handle.close unless first_handle.nil? || first_handle.closed?
peer&.close
listener.close
end
end

shared_examples_for "a socket" do
it "should open a socket" do
expect(@socket.open).to eq(@handle)
Expand Down
44 changes: 38 additions & 6 deletions lib/rb/spec/ssl_socket_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -372,16 +372,48 @@ def build_client(context = nil)
server_hostname: "localhost",
)
end
end

def server_context
OpenSSL::SSL::SSLContext.new.tap do |context|
context.cert = OpenSSL::X509::Certificate.new(File.read(File.join(ssl_keys_dir, "server.crt")))
context.key = OpenSSL::PKey::RSA.new(File.read(File.join(ssl_keys_dir, "server.key")))
describe "with a live connection" do
before(:each) do
server = listener
context = server_context
@server_thread = Thread.new do
peer = OpenSSL::SSL::SSLSocket.new(server.accept, context)
peer.sync_close = true
peer.accept
end
@server_thread.report_on_exception = false
end

after(:each) do
@server_thread.join(1)
@server_thread.kill if @server_thread.alive?
end

let(:listener) { TCPServer.new("127.0.0.1", 0) }
let(:transport) do
context = OpenSSL::SSL::SSLContext.new
context.verify_mode = OpenSSL::SSL::VERIFY_PEER
context.ca_file = File.join(ssl_keys_dir, "server.crt")
Thrift::SSLSocket.new("127.0.0.1", listener.local_address.ip_port, 1, context, server_hostname: "localhost")
end

def accept_peer
@server_thread.value
end

def ssl_keys_dir
File.expand_path("../../../test/keys", __dir__)
it_should_behave_like "a socket rejecting duplicate opens"
end

def server_context
OpenSSL::SSL::SSLContext.new.tap do |context|
context.cert = OpenSSL::X509::Certificate.new(File.read(File.join(ssl_keys_dir, "server.crt")))
context.key = OpenSSL::PKey::RSA.new(File.read(File.join(ssl_keys_dir, "server.key")))
end
end

def ssl_keys_dir
File.expand_path("../../../test/keys", __dir__)
end
end
19 changes: 19 additions & 0 deletions lib/rb/spec/unix_socket_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,25 @@
end
end

describe "with a live connection" do
before(:each) do
@dir = Dir.mktmpdir("thrift_unix_spec")
end

after(:each) do
FileUtils.remove_entry(@dir)
end

let(:listener) { ::UNIXServer.new(File.join(@dir, "server.sock")) }
let(:transport) { Thrift::UNIXSocket.new(listener.path, 1) }

def accept_peer
listener.accept
end

it_should_behave_like "a socket rejecting duplicate opens"
end

describe Thrift::UNIXServerSocket do
before(:each) do
@path = "/tmp/thrift_spec_socket"
Expand Down
Loading