Skip to content

Commit 51d1c32

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

File tree

9 files changed

+153
-72
lines changed

9 files changed

+153
-72
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 & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,6 @@
109109
end
110110
end
111111

112-
it 'returns an Addrinfo' do
113-
@addr.should be_an_instance_of(Addrinfo)
114-
end
115-
116112
it 'uses 0 as the protocol' do
117113
@addr.protocol.should == 0
118114
end
@@ -164,11 +160,23 @@
164160
end
165161
end
166162

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
163+
platform_is_not :windows do
164+
it 'equals address of peer socket' do
165+
if @method == :local_address
166+
@addr.to_s.should == @b.remote_address.to_s
167+
else
168+
@addr.to_s.should == @b.local_address.to_s
169+
end
170+
end
171+
end
172+
173+
platform_is :windows do
174+
it 'includes address of peer socket' do
175+
if @method == :local_address
176+
@addr.to_s.should == @b.remote_address.to_s
177+
else
178+
@addr.unix_path.should == @b.local_address.unix_path
179+
end
172180
end
173181
end
174182

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: 81 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,42 @@
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+
@client.send("foobar", 0)
53+
sock = @server.accept
54+
data = sock.recvfrom(6)
55+
(data in ["foobar", ["AF_UNIX", String]]).should be_true
56+
sock.send("barfoo", 0)
57+
data = @client.recvfrom(6)
58+
(data in ["barfoo", ["AF_UNIX", String]]).should be_true
59+
sock.close
60+
end
3261
end
3362

3463
it "allows an output buffer as third argument" do
@@ -54,15 +83,17 @@
5483
buffer.encoding.should == Encoding::ISO_8859_1
5584
end
5685

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

63-
real_data.should == peek_data
64-
peek_data.should == ["foobar", ["AF_UNIX", ""]]
65-
sock.close
93+
real_data.should == peek_data
94+
peek_data.should == ["foobar", ["AF_UNIX", ""]]
95+
sock.close
96+
end
6697
end
6798
end
6899

@@ -78,40 +109,51 @@
78109
@server.close
79110
end
80111

81-
it 'returns an Array containing the data and address information' do
82-
@server.recvfrom(5).should == ['hello', ['AF_UNIX', '']]
112+
platform_is_not :windows do
113+
it 'returns an Array containing the data and address information' do
114+
@server.recvfrom(5).should == ['hello', ['AF_UNIX', '']]
115+
end
116+
end
117+
118+
platform_is :windows do
119+
it 'returns an Array containing the data and address information' do
120+
# The second part of the address is a memory dump on Windows!
121+
(@server.recvfrom(5) in ['hello', ['AF_UNIX', String]]).should be_true
122+
end
83123
end
84124
end
85125

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

93-
@client_raw = Socket.new(:UNIX, :DGRAM)
94-
@client_raw.bind(Socket.sockaddr_un(@path1))
134+
@client_raw = Socket.new(:UNIX, :DGRAM)
135+
@client_raw.bind(Socket.sockaddr_un(@path1))
95136

96-
@server_raw = Socket.new(:UNIX, :DGRAM)
97-
@server_raw.bind(Socket.sockaddr_un(@path2))
137+
@server_raw = Socket.new(:UNIX, :DGRAM)
138+
@server_raw.bind(Socket.sockaddr_un(@path2))
98139

99-
@socket = UNIXSocket.for_fd(@server_raw.fileno)
100-
@socket.autoclose = false
101-
end
140+
@socket = UNIXSocket.for_fd(@server_raw.fileno)
141+
@socket.autoclose = false
142+
end
102143

103-
after do
104-
@client_raw.close
105-
@server_raw.close # also closes @socket
144+
after do
145+
@client_raw.close
146+
@server_raw.close # also closes @socket
106147

107-
rm_r @path1
108-
rm_r @path2
109-
end
148+
rm_r @path1
149+
rm_r @path2
150+
end
110151

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

114-
@socket.recvfrom(5).should == ['hello', ['AF_UNIX', @path1]]
155+
@socket.recvfrom(5).should == ['hello', ['AF_UNIX', @path1]]
156+
end
115157
end
116158
end
117159
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)