Skip to content

Commit f3ec3c8

Browse files
committed
Enable tests for Socket::AF_UNIX on Windows
1 parent 26d6c8a commit f3ec3c8

File tree

9 files changed

+142
-68
lines changed

9 files changed

+142
-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: 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+
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+
@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: 70 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,31 @@
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 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.send("barfoo", 0)
32+
data = @client.recvfrom(6)
33+
data.should == ["barfoo", ["AF_UNIX", ""]]
34+
sock.close
35+
end
36+
end
37+
38+
platform_is :windows do
39+
it "returns an Array containing the data and address information" do
40+
# The second part of the address is a memory dump on Windows!
41+
@client.send("foobar", 0)
42+
sock = @server.accept
43+
data = sock.recvfrom(6)
44+
(data in ["foobar", ["AF_UNIX", String]]).should be_true
45+
sock.send("barfoo", 0)
46+
data = @client.recvfrom(6)
47+
data.should == ["barfoo", ["AF_UNIX", ""]]
48+
sock.close
49+
end
3250
end
3351

3452
it "allows an output buffer as third argument" do
@@ -54,15 +72,17 @@
5472
buffer.encoding.should == Encoding::ISO_8859_1
5573
end
5674

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)
75+
platform_is_not :windows do
76+
it "uses different message options" do
77+
@client.send("foobar", Socket::MSG_PEEK)
78+
sock = @server.accept
79+
peek_data = sock.recvfrom(6, Socket::MSG_PEEK) # Does not retrieve the message
80+
real_data = sock.recvfrom(6)
6281

63-
real_data.should == peek_data
64-
peek_data.should == ["foobar", ["AF_UNIX", ""]]
65-
sock.close
82+
real_data.should == peek_data
83+
peek_data.should == ["foobar", ["AF_UNIX", ""]]
84+
sock.close
85+
end
6686
end
6787
end
6888

@@ -78,40 +98,51 @@
7898
@server.close
7999
end
80100

81-
it 'returns an Array containing the data and address information' do
82-
@server.recvfrom(5).should == ['hello', ['AF_UNIX', '']]
101+
platform_is_not :windows do
102+
it 'returns an Array containing the data and address information' do
103+
@server.recvfrom(5).should == ['hello', ['AF_UNIX', '']]
104+
end
105+
end
106+
107+
platform_is :windows do
108+
it 'returns an Array containing the data and address information' do
109+
# The second part of the address is a memory dump on Windows!
110+
(@server.recvfrom(5) in ['hello', ['AF_UNIX', String]]).should be_true
111+
end
83112
end
84113
end
85114

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)
115+
platform_is_not :windows do
116+
# These specs are taken from the rdoc examples on UNIXSocket#recvfrom.
117+
describe 'using a UNIX socket constructed using UNIXSocket.for_fd' do
118+
before do
119+
@path1 = SocketSpecs.socket_path
120+
@path2 = SocketSpecs.socket_path.chop + '2'
121+
rm_r(@path2)
92122

93-
@client_raw = Socket.new(:UNIX, :DGRAM)
94-
@client_raw.bind(Socket.sockaddr_un(@path1))
123+
@client_raw = Socket.new(:UNIX, :DGRAM)
124+
@client_raw.bind(Socket.sockaddr_un(@path1))
95125

96-
@server_raw = Socket.new(:UNIX, :DGRAM)
97-
@server_raw.bind(Socket.sockaddr_un(@path2))
126+
@server_raw = Socket.new(:UNIX, :DGRAM)
127+
@server_raw.bind(Socket.sockaddr_un(@path2))
98128

99-
@socket = UNIXSocket.for_fd(@server_raw.fileno)
100-
@socket.autoclose = false
101-
end
129+
@socket = UNIXSocket.for_fd(@server_raw.fileno)
130+
@socket.autoclose = false
131+
end
102132

103-
after do
104-
@client_raw.close
105-
@server_raw.close # also closes @socket
133+
after do
134+
@client_raw.close
135+
@server_raw.close # also closes @socket
106136

107-
rm_r @path1
108-
rm_r @path2
109-
end
137+
rm_r @path1
138+
rm_r @path2
139+
end
110140

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

114-
@socket.recvfrom(5).should == ['hello', ['AF_UNIX', @path1]]
144+
@socket.recvfrom(5).should == ['hello', ['AF_UNIX', @path1]]
145+
end
115146
end
116147
end
117148
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)