Skip to content

Commit 8092a25

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

File tree

9 files changed

+156
-68
lines changed

9 files changed

+156
-68
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: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,24 @@
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+
platform_is :windows 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+
# see https://bugs.ruby-lang.org/issues/21702
183+
@addr.unix_path.should == @b.local_address.unix_path
184+
end
172185
end
173186
end
174187

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: 83 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,43 @@
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+
platform_is_not :windows, :linux do
26+
it "returns an array with data and information on the sender" do
27+
@client.send("foobar", 0)
28+
sock = @server.accept
29+
data = sock.recvfrom(6)
30+
data.should == ["foobar", ["AF_UNIX", ""]]
31+
sock.close
32+
end
33+
end
34+
35+
platform_is :linux do
36+
# It isn't clear which platforms support remote addresses here, so only Linux tests this.
37+
it "returns an array with data and information on the sender" do
38+
@client.send("foobar", 0)
39+
sock = @server.accept
40+
data = sock.recvfrom(6)
41+
data.should == ["foobar", ["AF_UNIX", ""]]
42+
sock.send("barfoo", 0)
43+
data = @client.recvfrom(6)
44+
data.should == ["barfoo", ["AF_UNIX", @server.local_address.unix_path]]
45+
sock.close
46+
end
47+
end
48+
49+
platform_is :windows do
50+
it "returns an Array containing the data and address information" do
51+
# The second part of the address is a memory dump on Windows!
52+
# See https://bugs.ruby-lang.org/issues/21702
53+
@client.send("foobar", 0)
54+
sock = @server.accept
55+
data = sock.recvfrom(6)
56+
(data in ["foobar", ["AF_UNIX", String]]).should be_true
57+
sock.send("barfoo", 0)
58+
data = @client.recvfrom(6)
59+
(data in ["barfoo", ["AF_UNIX", String]]).should be_true
60+
sock.close
61+
end
3262
end
3363

3464
it "allows an output buffer as third argument" do
@@ -54,15 +84,17 @@
5484
buffer.encoding.should == Encoding::ISO_8859_1
5585
end
5686

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)
87+
platform_is_not :windows do
88+
it "uses different message options" do
89+
@client.send("foobar", Socket::MSG_PEEK)
90+
sock = @server.accept
91+
peek_data = sock.recvfrom(6, Socket::MSG_PEEK) # Does not retrieve the message
92+
real_data = sock.recvfrom(6)
6293

63-
real_data.should == peek_data
64-
peek_data.should == ["foobar", ["AF_UNIX", ""]]
65-
sock.close
94+
real_data.should == peek_data
95+
peek_data.should == ["foobar", ["AF_UNIX", ""]]
96+
sock.close
97+
end
6698
end
6799
end
68100

@@ -78,40 +110,52 @@
78110
@server.close
79111
end
80112

81-
it 'returns an Array containing the data and address information' do
82-
@server.recvfrom(5).should == ['hello', ['AF_UNIX', '']]
113+
platform_is_not :windows do
114+
it 'returns an Array containing the data and address information' do
115+
@server.recvfrom(5).should == ['hello', ['AF_UNIX', '']]
116+
end
117+
end
118+
119+
platform_is :windows do
120+
it 'returns an Array containing the data and address information' do
121+
# The second part of the address is a memory dump on Windows!
122+
# See https://bugs.ruby-lang.org/issues/21702
123+
(@server.recvfrom(5) in ['hello', ['AF_UNIX', String]]).should be_true
124+
end
83125
end
84126
end
85127

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)
128+
platform_is_not :windows do
129+
# These specs are taken from the rdoc examples on UNIXSocket#recvfrom.
130+
describe 'using a UNIX socket constructed using UNIXSocket.for_fd' do
131+
before do
132+
@path1 = SocketSpecs.socket_path
133+
@path2 = SocketSpecs.socket_path.chop + '2'
134+
rm_r(@path2)
92135

93-
@client_raw = Socket.new(:UNIX, :DGRAM)
94-
@client_raw.bind(Socket.sockaddr_un(@path1))
136+
@client_raw = Socket.new(:UNIX, :DGRAM)
137+
@client_raw.bind(Socket.sockaddr_un(@path1))
95138

96-
@server_raw = Socket.new(:UNIX, :DGRAM)
97-
@server_raw.bind(Socket.sockaddr_un(@path2))
139+
@server_raw = Socket.new(:UNIX, :DGRAM)
140+
@server_raw.bind(Socket.sockaddr_un(@path2))
98141

99-
@socket = UNIXSocket.for_fd(@server_raw.fileno)
100-
@socket.autoclose = false
101-
end
142+
@socket = UNIXSocket.for_fd(@server_raw.fileno)
143+
@socket.autoclose = false
144+
end
102145

103-
after do
104-
@client_raw.close
105-
@server_raw.close # also closes @socket
146+
after do
147+
@client_raw.close
148+
@server_raw.close # also closes @socket
106149

107-
rm_r @path1
108-
rm_r @path2
109-
end
150+
rm_r @path1
151+
rm_r @path2
152+
end
110153

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

114-
@socket.recvfrom(5).should == ['hello', ['AF_UNIX', @path1]]
157+
@socket.recvfrom(5).should == ['hello', ['AF_UNIX', @path1]]
158+
end
115159
end
116160
end
117161
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)