diff options
Diffstat (limited to 'spec/rubyspec/library/socket/unixsocket')
12 files changed, 365 insertions, 0 deletions
diff --git a/spec/rubyspec/library/socket/unixsocket/addr_spec.rb b/spec/rubyspec/library/socket/unixsocket/addr_spec.rb new file mode 100644 index 0000000000..893a910e92 --- /dev/null +++ b/spec/rubyspec/library/socket/unixsocket/addr_spec.rb @@ -0,0 +1,38 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../../fixtures/classes', __FILE__) + +describe "UNIXSocket#addr" do + + platform_is_not :windows do + before :each do + @path = SocketSpecs.socket_path + rm_r @path + + @server = UNIXServer.open(@path) + @client = UNIXSocket.open(@path) + end + + after :each do + @client.close + @server.close + rm_r @path + end + + it "returns the address family of this socket in an array" do + @client.addr[0].should == "AF_UNIX" + end + + it "returns the path of the socket in an array if it's a server" do + @server.addr[1].should == @path + end + + it "returns an empty string for path if it's a client" do + @client.addr[1].should == "" + end + + it "returns an array" do + @client.addr.should be_kind_of(Array) + end + end + +end diff --git a/spec/rubyspec/library/socket/unixsocket/inspect_spec.rb b/spec/rubyspec/library/socket/unixsocket/inspect_spec.rb new file mode 100644 index 0000000000..8ea25ec1e9 --- /dev/null +++ b/spec/rubyspec/library/socket/unixsocket/inspect_spec.rb @@ -0,0 +1,17 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../../fixtures/classes', __FILE__) + +describe "UNIXSocket#inspect" do + platform_is_not :windows do + it "returns sockets fd for unnamed sockets" do + begin + s1, s2 = UNIXSocket.socketpair + s1.inspect.should == "#<UNIXSocket:fd #{s1.fileno}>" + s2.inspect.should == "#<UNIXSocket:fd #{s2.fileno}>" + ensure + s1.close + s2.close + end + end + end +end diff --git a/spec/rubyspec/library/socket/unixsocket/new_spec.rb b/spec/rubyspec/library/socket/unixsocket/new_spec.rb new file mode 100644 index 0000000000..7db8613b96 --- /dev/null +++ b/spec/rubyspec/library/socket/unixsocket/new_spec.rb @@ -0,0 +1,6 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../shared/new', __FILE__) + +describe "UNIXSocket.new" do + it_behaves_like :unixsocket_new, :new +end diff --git a/spec/rubyspec/library/socket/unixsocket/open_spec.rb b/spec/rubyspec/library/socket/unixsocket/open_spec.rb new file mode 100644 index 0000000000..eb8ffbaf22 --- /dev/null +++ b/spec/rubyspec/library/socket/unixsocket/open_spec.rb @@ -0,0 +1,27 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../shared/new', __FILE__) + +describe "UNIXSocket.open" do + it_behaves_like :unixsocket_new, :open +end + +describe "UNIXSocket.open" do + platform_is_not :windows do + before :each do + @path = SocketSpecs.socket_path + @server = UNIXServer.open(@path) + end + + after :each do + @server.close + rm_r @path + end + + it "opens a unix socket on the specified file and yields it to the block" do + UNIXSocket.send(@method, @path) do |client| + client.addr[0].should == "AF_UNIX" + client.closed?.should == false + end + end + end +end diff --git a/spec/rubyspec/library/socket/unixsocket/pair_spec.rb b/spec/rubyspec/library/socket/unixsocket/pair_spec.rb new file mode 100644 index 0000000000..5cd75e2906 --- /dev/null +++ b/spec/rubyspec/library/socket/unixsocket/pair_spec.rb @@ -0,0 +1,39 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../../fixtures/classes', __FILE__) +require File.expand_path('../../shared/partially_closable_sockets', __FILE__) + +describe "UNIXSocket#pair" do + platform_is_not :windows do + + it_should_behave_like "partially closable sockets" + + before :each do + @s1, @s2 = UNIXSocket.pair + end + + after :each do + @s1.close + @s2.close + end + + it "returns a pair of connected sockets" do + @s1.puts "foo" + @s2.gets.should == "foo\n" + end + + it "returns sockets with no name" do + @s1.path.should == @s2.path + @s1.path.should == "" + end + + it "returns sockets with no address" do + @s1.addr.should == ["AF_UNIX", ""] + @s2.addr.should == ["AF_UNIX", ""] + end + + it "returns sockets with no peeraddr" do + @s1.peeraddr.should == ["AF_UNIX", ""] + @s2.peeraddr.should == ["AF_UNIX", ""] + end + end +end diff --git a/spec/rubyspec/library/socket/unixsocket/partially_closable_spec.rb b/spec/rubyspec/library/socket/unixsocket/partially_closable_spec.rb new file mode 100644 index 0000000000..1123a23541 --- /dev/null +++ b/spec/rubyspec/library/socket/unixsocket/partially_closable_spec.rb @@ -0,0 +1,26 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../../fixtures/classes', __FILE__) +require File.expand_path('../../shared/partially_closable_sockets', __FILE__) + +platform_is_not :windows do + describe "UNIXSocket partial closability" do + + before :each do + @path = SocketSpecs.socket_path + rm_r @path + @server = UNIXServer.open(@path) + @s1 = UNIXSocket.new(@path) + @s2 = @server.accept + end + + after :each do + @server.close + @s1.close + @s2.close + rm_r @path + end + + it_should_behave_like "partially closable sockets" + + end +end diff --git a/spec/rubyspec/library/socket/unixsocket/path_spec.rb b/spec/rubyspec/library/socket/unixsocket/path_spec.rb new file mode 100644 index 0000000000..a9186854da --- /dev/null +++ b/spec/rubyspec/library/socket/unixsocket/path_spec.rb @@ -0,0 +1,30 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../../fixtures/classes', __FILE__) + +describe "UNIXSocket#path" do + + platform_is_not :windows do + before :each do + @path = SocketSpecs.socket_path + rm_r @path + + @server = UNIXServer.open(@path) + @client = UNIXSocket.open(@path) + end + + after :each do + @client.close + @server.close + rm_r @path + end + + it "returns the path of the socket if it's a server" do + @server.path.should == @path + end + + it "returns an empty string for path if it's a client" do + @client.path.should == "" + end + end + +end diff --git a/spec/rubyspec/library/socket/unixsocket/peeraddr_spec.rb b/spec/rubyspec/library/socket/unixsocket/peeraddr_spec.rb new file mode 100644 index 0000000000..cd224540ef --- /dev/null +++ b/spec/rubyspec/library/socket/unixsocket/peeraddr_spec.rb @@ -0,0 +1,30 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../../fixtures/classes', __FILE__) + +describe "UNIXSocket#peeraddr" do + + platform_is_not :windows do + before :each do + @path = SocketSpecs.socket_path + rm_r @path + + @server = UNIXServer.open(@path) + @client = UNIXSocket.open(@path) + end + + after :each do + @client.close + @server.close + rm_r @path + end + + it "returns the address familly and path of the server end of the connection" do + @client.peeraddr.should == ["AF_UNIX", @path] + end + + it "raises an error in server sockets" do + lambda { @server.peeraddr }.should raise_error + end + end + +end diff --git a/spec/rubyspec/library/socket/unixsocket/recv_io_spec.rb b/spec/rubyspec/library/socket/unixsocket/recv_io_spec.rb new file mode 100644 index 0000000000..9fc4470572 --- /dev/null +++ b/spec/rubyspec/library/socket/unixsocket/recv_io_spec.rb @@ -0,0 +1,44 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../../fixtures/classes', __FILE__) + +describe "UNIXSocket#recv_io" do + + platform_is_not :windows do + before :each do + @path = SocketSpecs.socket_path + @server = UNIXServer.open(@path) + @client = UNIXSocket.open(@path) + + @send_io_path = File.expand_path('../../fixtures/send_io.txt', __FILE__) + @file = File.open(@send_io_path) + end + + after :each do + @io.close if @io + @socket.close if @socket + + @file.close + @client.close + @server.close + rm_r @path + end + + it "reads an IO object across the socket" do + @client.send_io(@file) + + @socket = @server.accept + @io = @socket.recv_io + + @io.read.should == File.read(@send_io_path) + end + + it "takes an optional class to use" do + @client.send_io(@file) + + @socket = @server.accept + @io = @socket.recv_io(File) + + @io.should be_kind_of(File) + end + end +end diff --git a/spec/rubyspec/library/socket/unixsocket/recvfrom_spec.rb b/spec/rubyspec/library/socket/unixsocket/recvfrom_spec.rb new file mode 100644 index 0000000000..7ac002607c --- /dev/null +++ b/spec/rubyspec/library/socket/unixsocket/recvfrom_spec.rb @@ -0,0 +1,49 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../../fixtures/classes', __FILE__) + +describe "UNIXSocket#recvfrom" do + + platform_is_not :windows do + before :each do + @path = SocketSpecs.socket_path + rm_r @path + + @server = UNIXServer.open(@path) + @client = UNIXSocket.open(@path) + end + + after :each do + @client.close + @server.close + rm_r @path + end + + it "receives len bytes from sock" do + @client.send("foobar", 0) + sock = @server.accept + sock.recvfrom(6).first.should == "foobar" + sock.close + end + + it "returns an array with data and information on the sender" do + @client.send("foobar", 0) + sock = @server.accept + data = sock.recvfrom(6) + data.first.should == "foobar" + data.last.should == ["AF_UNIX", ""] + sock.close + end + + it "uses different message options" do + @client.send("foobar", Socket::MSG_PEEK) + sock = @server.accept + peek_data = sock.recvfrom(6, Socket::MSG_PEEK) # Does not retrieve the message + real_data = sock.recvfrom(6) + + real_data.should == peek_data + peek_data.should == ["foobar", ["AF_UNIX", ""]] + sock.close + end + end + +end diff --git a/spec/rubyspec/library/socket/unixsocket/send_io_spec.rb b/spec/rubyspec/library/socket/unixsocket/send_io_spec.rb new file mode 100644 index 0000000000..fb6ce9ba17 --- /dev/null +++ b/spec/rubyspec/library/socket/unixsocket/send_io_spec.rb @@ -0,0 +1,35 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../../fixtures/classes', __FILE__) + +describe "UNIXSocket#send_io" do + + platform_is_not :windows do + before :each do + @path = SocketSpecs.socket_path + @server = UNIXServer.open(@path) + @client = UNIXSocket.open(@path) + + @send_io_path = File.expand_path('../../fixtures/send_io.txt', __FILE__) + @file = File.open(@send_io_path) + end + + after :each do + @io.close if @io + @socket.close if @socket + + @file.close + @client.close + @server.close + rm_r @path + end + + it "sends the fd for an IO object across the socket" do + @client.send_io(@file) + + @socket = @server.accept + @io = @socket.recv_io + + @io.read.should == File.read(@send_io_path) + end + end +end diff --git a/spec/rubyspec/library/socket/unixsocket/shared/new.rb b/spec/rubyspec/library/socket/unixsocket/shared/new.rb new file mode 100644 index 0000000000..9d8fb809d2 --- /dev/null +++ b/spec/rubyspec/library/socket/unixsocket/shared/new.rb @@ -0,0 +1,24 @@ +require File.expand_path('../../../../../spec_helper', __FILE__) +require File.expand_path('../../../fixtures/classes', __FILE__) + +describe :unixsocket_new, shared: true do + platform_is_not :windows do + before :each do + @path = SocketSpecs.socket_path + @server = UNIXServer.open(@path) + end + + after :each do + @client.close if @client + @server.close + rm_r @path + end + + it "opens a unix socket on the specified file" do + @client = UNIXSocket.send(@method, @path) + + @client.addr[0].should == "AF_UNIX" + @client.closed?.should == false + end + end +end |