diff options
Diffstat (limited to 'spec/ruby/library/socket/unixsocket')
-rw-r--r-- | spec/ruby/library/socket/unixsocket/addr_spec.rb | 36 | ||||
-rw-r--r-- | spec/ruby/library/socket/unixsocket/inspect_spec.rb | 17 | ||||
-rw-r--r-- | spec/ruby/library/socket/unixsocket/new_spec.rb | 6 | ||||
-rw-r--r-- | spec/ruby/library/socket/unixsocket/open_spec.rb | 27 | ||||
-rw-r--r-- | spec/ruby/library/socket/unixsocket/pair_spec.rb | 39 | ||||
-rw-r--r-- | spec/ruby/library/socket/unixsocket/partially_closable_spec.rb | 25 | ||||
-rw-r--r-- | spec/ruby/library/socket/unixsocket/path_spec.rb | 28 | ||||
-rw-r--r-- | spec/ruby/library/socket/unixsocket/peeraddr_spec.rb | 28 | ||||
-rw-r--r-- | spec/ruby/library/socket/unixsocket/recv_io_spec.rb | 44 | ||||
-rw-r--r-- | spec/ruby/library/socket/unixsocket/recvfrom_spec.rb | 47 | ||||
-rw-r--r-- | spec/ruby/library/socket/unixsocket/send_io_spec.rb | 35 | ||||
-rw-r--r-- | spec/ruby/library/socket/unixsocket/shared/new.rb | 24 |
12 files changed, 356 insertions, 0 deletions
diff --git a/spec/ruby/library/socket/unixsocket/addr_spec.rb b/spec/ruby/library/socket/unixsocket/addr_spec.rb new file mode 100644 index 0000000000..34c62e083f --- /dev/null +++ b/spec/ruby/library/socket/unixsocket/addr_spec.rb @@ -0,0 +1,36 @@ +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 + @server = UNIXServer.open(@path) + @client = UNIXSocket.open(@path) + end + + after :each do + @client.close + @server.close + SocketSpecs.rm_socket @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/ruby/library/socket/unixsocket/inspect_spec.rb b/spec/ruby/library/socket/unixsocket/inspect_spec.rb new file mode 100644 index 0000000000..8ea25ec1e9 --- /dev/null +++ b/spec/ruby/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/ruby/library/socket/unixsocket/new_spec.rb b/spec/ruby/library/socket/unixsocket/new_spec.rb new file mode 100644 index 0000000000..7db8613b96 --- /dev/null +++ b/spec/ruby/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/ruby/library/socket/unixsocket/open_spec.rb b/spec/ruby/library/socket/unixsocket/open_spec.rb new file mode 100644 index 0000000000..5b9b19ee33 --- /dev/null +++ b/spec/ruby/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 + SocketSpecs.rm_socket @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/ruby/library/socket/unixsocket/pair_spec.rb b/spec/ruby/library/socket/unixsocket/pair_spec.rb new file mode 100644 index 0000000000..5cd75e2906 --- /dev/null +++ b/spec/ruby/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/ruby/library/socket/unixsocket/partially_closable_spec.rb b/spec/ruby/library/socket/unixsocket/partially_closable_spec.rb new file mode 100644 index 0000000000..f43274db2e --- /dev/null +++ b/spec/ruby/library/socket/unixsocket/partially_closable_spec.rb @@ -0,0 +1,25 @@ +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 + @server = UNIXServer.open(@path) + @s1 = UNIXSocket.new(@path) + @s2 = @server.accept + end + + after :each do + @server.close + @s1.close + @s2.close + SocketSpecs.rm_socket @path + end + + it_should_behave_like "partially closable sockets" + + end +end diff --git a/spec/ruby/library/socket/unixsocket/path_spec.rb b/spec/ruby/library/socket/unixsocket/path_spec.rb new file mode 100644 index 0000000000..a2beaffeea --- /dev/null +++ b/spec/ruby/library/socket/unixsocket/path_spec.rb @@ -0,0 +1,28 @@ +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 + @server = UNIXServer.open(@path) + @client = UNIXSocket.open(@path) + end + + after :each do + @client.close + @server.close + SocketSpecs.rm_socket @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/ruby/library/socket/unixsocket/peeraddr_spec.rb b/spec/ruby/library/socket/unixsocket/peeraddr_spec.rb new file mode 100644 index 0000000000..14dccf7917 --- /dev/null +++ b/spec/ruby/library/socket/unixsocket/peeraddr_spec.rb @@ -0,0 +1,28 @@ +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 + @server = UNIXServer.open(@path) + @client = UNIXSocket.open(@path) + end + + after :each do + @client.close + @server.close + SocketSpecs.rm_socket @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/ruby/library/socket/unixsocket/recv_io_spec.rb b/spec/ruby/library/socket/unixsocket/recv_io_spec.rb new file mode 100644 index 0000000000..c7a8946ceb --- /dev/null +++ b/spec/ruby/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 + SocketSpecs.rm_socket @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/ruby/library/socket/unixsocket/recvfrom_spec.rb b/spec/ruby/library/socket/unixsocket/recvfrom_spec.rb new file mode 100644 index 0000000000..78e272bfe2 --- /dev/null +++ b/spec/ruby/library/socket/unixsocket/recvfrom_spec.rb @@ -0,0 +1,47 @@ +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 + @server = UNIXServer.open(@path) + @client = UNIXSocket.open(@path) + end + + after :each do + @client.close + @server.close + SocketSpecs.rm_socket @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/ruby/library/socket/unixsocket/send_io_spec.rb b/spec/ruby/library/socket/unixsocket/send_io_spec.rb new file mode 100644 index 0000000000..ec8d6b2314 --- /dev/null +++ b/spec/ruby/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 + SocketSpecs.rm_socket @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/ruby/library/socket/unixsocket/shared/new.rb b/spec/ruby/library/socket/unixsocket/shared/new.rb new file mode 100644 index 0000000000..76c0d07b83 --- /dev/null +++ b/spec/ruby/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 + SocketSpecs.rm_socket @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 |