Skip to content

Commit dceeddb

Browse files
committed
Enable tests for Socket::AF_UNIX on Windows
1 parent 84bd439 commit dceeddb

File tree

9 files changed

+170
-69
lines changed

9 files changed

+170
-69
lines changed

library/socket/basicsocket/getpeereid_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
require_relative '../fixtures/classes'
33

44
describe 'BasicSocket#getpeereid' do
5-
with_feature :unix_socket do
5+
platform_is_not :windows do
66
describe 'using a UNIXSocket' do
77
before do
88
@path = SocketSpecs.socket_path

library/socket/shared/address.rb

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,23 @@
164164
end
165165
end
166166

167-
it 'equals address of peer socket' do
168-
if @method == :local_address
169-
@addr.to_s.should == @b.remote_address.to_s
170-
else
171-
@addr.to_s.should == @b.local_address.to_s
167+
platform_is_not :windows do
168+
it 'equals address of peer socket' do
169+
if @method == :local_address
170+
@addr.to_s.should == @b.remote_address.to_s
171+
else
172+
@addr.to_s.should == @b.local_address.to_s
173+
end
174+
end
175+
end
176+
177+
guard -> { platform_is :windows and ruby_bug "#21702", ""..."4.0" } do
178+
it 'equals address of peer socket' do
179+
if @method == :local_address
180+
@addr.to_s.should == @b.remote_address.to_s
181+
else
182+
@addr.to_s.should == @b.local_address.to_s
183+
end
172184
end
173185
end
174186

library/socket/spec_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
require 'socket'
33

44
MSpec.enable_feature :sock_packet if Socket.const_defined?(:SOCK_PACKET)
5-
MSpec.enable_feature :unix_socket unless PlatformGuard.windows?
5+
MSpec.enable_feature :unix_socket if Socket.const_defined?(:AF_UNIX)
66
MSpec.enable_feature :udp_cork if Socket.const_defined?(:UDP_CORK)
77
MSpec.enable_feature :tcp_cork if Socket.const_defined?(:TCP_CORK)
88
MSpec.enable_feature :pktinfo if Socket.const_defined?(:IP_PKTINFO)

library/socket/unixserver/accept_spec.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,12 @@
111111
@socket.recv(5).should == 'hello'
112112
end
113113

114-
it "is set to nonblocking" do
115-
require 'io/nonblock'
116-
@socket = @server.accept
117-
@socket.should.nonblock?
114+
platform_is_not :windows do
115+
it "is set to nonblocking" do
116+
require 'io/nonblock'
117+
@socket = @server.accept
118+
@socket.should.nonblock?
119+
end
118120
end
119121

120122
it "is set to close on exec" do

library/socket/unixsocket/initialize_spec.rb

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,17 @@
44
with_feature :unix_socket do
55
describe 'UNIXSocket#initialize' do
66
describe 'using a non existing path' do
7-
it 'raises Errno::ENOENT' do
8-
-> { UNIXSocket.new(SocketSpecs.socket_path) }.should raise_error(Errno::ENOENT)
7+
platform_is_not :windows do
8+
it 'raises Errno::ENOENT' do
9+
-> { UNIXSocket.new(SocketSpecs.socket_path) }.should raise_error(Errno::ENOENT)
10+
end
11+
end
12+
13+
platform_is :windows do
14+
# Why, Windows, why?
15+
it 'raises Errno::ECONNREFUSED' do
16+
-> { UNIXSocket.new(SocketSpecs.socket_path) }.should raise_error(Errno::ECONNREFUSED)
17+
end
918
end
1019
end
1120

@@ -34,15 +43,16 @@
3443
@socket.binmode?.should be_true
3544
end
3645

37-
it 'sets the socket to nonblock' do
38-
require 'io/nonblock'
39-
@socket.should.nonblock?
46+
platform_is_not :windows do
47+
it 'sets the socket to nonblock' do
48+
require 'io/nonblock'
49+
@socket.should.nonblock?
50+
end
4051
end
4152

4253
it 'sets the socket to close on exec' do
4354
@socket.should.close_on_exec?
4455
end
45-
4656
end
4757
end
4858
end

library/socket/unixsocket/recv_io_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require_relative '../spec_helper'
22
require_relative '../fixtures/classes'
33

4-
with_feature :unix_socket do
4+
platform_is_not :windows do
55
describe "UNIXSocket#recv_io" do
66
before :each do
77
@path = SocketSpecs.socket_path

library/socket/unixsocket/recvfrom_spec.rb

Lines changed: 98 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,66 @@
1515
SocketSpecs.rm_socket @path
1616
end
1717

18-
it "receives len bytes from sock" do
18+
it "receives len bytes from sock, returning an array containing sent data as first element" do
1919
@client.send("foobar", 0)
2020
sock = @server.accept
2121
sock.recvfrom(6).first.should == "foobar"
2222
sock.close
2323
end
2424

25-
it "returns an array with data and information on the sender" do
26-
@client.send("foobar", 0)
27-
sock = @server.accept
28-
data = sock.recvfrom(6)
29-
data.first.should == "foobar"
30-
data.last.should == ["AF_UNIX", ""]
31-
sock.close
25+
context "when called on a server's socket" do
26+
platform_is_not :windows do
27+
it "returns an array containing basic information on the client as second element" do
28+
@client.send("foobar", 0)
29+
sock = @server.accept
30+
data = sock.recvfrom(6)
31+
data.last.should == ["AF_UNIX", ""]
32+
sock.close
33+
end
34+
end
35+
36+
guard -> { platform_is :windows and ruby_bug "#21702", ""..."4.0" } do
37+
it "returns an array containing basic information on the client as second element" do
38+
@client.send("foobar", 0)
39+
sock = @server.accept
40+
data = sock.recvfrom(6)
41+
data.last.should == ["AF_UNIX", ""]
42+
sock.close
43+
end
44+
end
45+
end
46+
47+
context "when called on a client's socket" do
48+
platform_is_not :windows, :darwin do
49+
it "returns an array containing server's address as second element" do
50+
@client.send("", 0)
51+
sock = @server.accept
52+
sock.send("barfoo", 0)
53+
@client.recvfrom(6).last.should == ["AF_UNIX", @server.local_address.unix_path]
54+
sock.close
55+
end
56+
end
57+
58+
guard -> { platform_is :windows and ruby_bug "#21702", ""..."4.0" } do
59+
it "returns an array containing server's address as second element" do
60+
@client.send("", 0)
61+
sock = @server.accept
62+
sock.send("barfoo", 0)
63+
# This may not be correct, depends on what underlying recvfrom actually returns.
64+
@client.recvfrom(6).last.should == ["AF_UNIX", @server.local_address.unix_path]
65+
sock.close
66+
end
67+
end
68+
69+
platform_is :darwin do
70+
it "returns an array containing basic information on the server as second element" do
71+
@client.send("", 0)
72+
sock = @server.accept
73+
sock.send("barfoo", 0)
74+
@client.recvfrom(6).last.should == ["AF_UNIX", ""]
75+
sock.close
76+
end
77+
end
3278
end
3379

3480
it "allows an output buffer as third argument" do
@@ -54,15 +100,17 @@
54100
buffer.encoding.should == Encoding::ISO_8859_1
55101
end
56102

57-
it "uses different message options" do
58-
@client.send("foobar", Socket::MSG_PEEK)
59-
sock = @server.accept
60-
peek_data = sock.recvfrom(6, Socket::MSG_PEEK) # Does not retrieve the message
61-
real_data = sock.recvfrom(6)
103+
platform_is_not :windows do
104+
it "uses different message options" do
105+
@client.send("foobar", Socket::MSG_PEEK)
106+
sock = @server.accept
107+
peek_data = sock.recvfrom(6, Socket::MSG_PEEK) # Does not retrieve the message
108+
real_data = sock.recvfrom(6)
62109

63-
real_data.should == peek_data
64-
peek_data.should == ["foobar", ["AF_UNIX", ""]]
65-
sock.close
110+
real_data.should == peek_data
111+
peek_data.should == ["foobar", ["AF_UNIX", ""]]
112+
sock.close
113+
end
66114
end
67115
end
68116

@@ -78,40 +126,50 @@
78126
@server.close
79127
end
80128

81-
it 'returns an Array containing the data and address information' do
82-
@server.recvfrom(5).should == ['hello', ['AF_UNIX', '']]
129+
platform_is_not :windows do
130+
it 'returns an Array containing the data and address information' do
131+
@server.recvfrom(5).should == ['hello', ['AF_UNIX', '']]
132+
end
133+
end
134+
135+
guard -> { platform_is :windows and ruby_bug "#21702", ""..."4.0" } do
136+
it 'returns an Array containing the data and address information' do
137+
@server.recvfrom(5).should == ['hello', ['AF_UNIX', '']]
138+
end
83139
end
84140
end
85141

86-
# These specs are taken from the rdoc examples on UNIXSocket#recvfrom.
87-
describe 'using a UNIX socket constructed using UNIXSocket.for_fd' do
88-
before do
89-
@path1 = SocketSpecs.socket_path
90-
@path2 = SocketSpecs.socket_path.chop + '2'
91-
rm_r(@path2)
142+
platform_is_not :windows do
143+
# These specs are taken from the rdoc examples on UNIXSocket#recvfrom.
144+
describe 'using a UNIX socket constructed using UNIXSocket.for_fd' do
145+
before do
146+
@path1 = SocketSpecs.socket_path
147+
@path2 = SocketSpecs.socket_path.chop + '2'
148+
rm_r(@path2)
92149

93-
@client_raw = Socket.new(:UNIX, :DGRAM)
94-
@client_raw.bind(Socket.sockaddr_un(@path1))
150+
@client_raw = Socket.new(:UNIX, :DGRAM)
151+
@client_raw.bind(Socket.sockaddr_un(@path1))
95152

96-
@server_raw = Socket.new(:UNIX, :DGRAM)
97-
@server_raw.bind(Socket.sockaddr_un(@path2))
153+
@server_raw = Socket.new(:UNIX, :DGRAM)
154+
@server_raw.bind(Socket.sockaddr_un(@path2))
98155

99-
@socket = UNIXSocket.for_fd(@server_raw.fileno)
100-
@socket.autoclose = false
101-
end
156+
@socket = UNIXSocket.for_fd(@server_raw.fileno)
157+
@socket.autoclose = false
158+
end
102159

103-
after do
104-
@client_raw.close
105-
@server_raw.close # also closes @socket
160+
after do
161+
@client_raw.close
162+
@server_raw.close # also closes @socket
106163

107-
rm_r @path1
108-
rm_r @path2
109-
end
164+
rm_r @path1
165+
rm_r @path2
166+
end
110167

111-
it 'returns an Array containing the data and address information' do
112-
@client_raw.send('hello', 0, Socket.sockaddr_un(@path2))
168+
it 'returns an Array containing the data and address information' do
169+
@client_raw.send('hello', 0, Socket.sockaddr_un(@path2))
113170

114-
@socket.recvfrom(5).should == ['hello', ['AF_UNIX', @path1]]
171+
@socket.recvfrom(5).should == ['hello', ['AF_UNIX', @path1]]
172+
end
115173
end
116174
end
117175
end

library/socket/unixsocket/send_io_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require_relative '../spec_helper'
22
require_relative '../fixtures/classes'
33

4-
with_feature :unix_socket do
4+
platform_is_not :windows do
55
describe "UNIXSocket#send_io" do
66
before :each do
77
@path = SocketSpecs.socket_path

library/socket/unixsocket/shared/pair.rb

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,37 @@
1212
@s2.gets.should == "foo\n"
1313
end
1414

15-
it "sets the socket paths to empty Strings" do
16-
@s1.path.should == ""
17-
@s2.path.should == ""
18-
end
15+
platform_is_not :windows do
16+
it "sets the socket paths to empty Strings" do
17+
@s1.path.should == ""
18+
@s2.path.should == ""
19+
end
20+
21+
it "sets the socket addresses to empty Strings" do
22+
@s1.addr.should == ["AF_UNIX", ""]
23+
@s2.addr.should == ["AF_UNIX", ""]
24+
end
1925

20-
it "sets the socket addresses to empty Strings" do
21-
@s1.addr.should == ["AF_UNIX", ""]
22-
@s2.addr.should == ["AF_UNIX", ""]
26+
it "sets the socket peer addresses to empty Strings" do
27+
@s1.peeraddr.should == ["AF_UNIX", ""]
28+
@s2.peeraddr.should == ["AF_UNIX", ""]
29+
end
2330
end
2431

25-
it "sets the socket peer addresses to empty Strings" do
26-
@s1.peeraddr.should == ["AF_UNIX", ""]
27-
@s2.peeraddr.should == ["AF_UNIX", ""]
32+
platform_is :windows do
33+
it "emulates unnamed sockets with a temporary file with a path" do
34+
@s1.path.match?(/\\AppData\\Local\\Temp\\\d+-\d+\.\(\$\)\z/).should be_true
35+
@s1.addr.should == ["AF_UNIX", @s1.path]
36+
@s2.peeraddr.should == ["AF_UNIX", @s1.path]
37+
end
38+
39+
it "sets the peer address of first socket to an empty string" do
40+
@s1.peeraddr.should == ["AF_UNIX", ""]
41+
end
42+
43+
it "sets the address and path of second socket to an empty string" do
44+
@s2.addr.should == ["AF_UNIX", ""]
45+
@s2.path.should == ""
46+
end
2847
end
2948
end

0 commit comments

Comments
 (0)