Skip to content

Commit ba24547

Browse files
committed
Enable tests for Socket::AF_UNIX on Windows
1 parent be4fad6 commit ba24547

File tree

8 files changed

+90
-55
lines changed

8 files changed

+90
-55
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/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: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,17 @@
5454
buffer.encoding.should == Encoding::ISO_8859_1
5555
end
5656

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)
62-
63-
real_data.should == peek_data
64-
peek_data.should == ["foobar", ["AF_UNIX", ""]]
65-
sock.close
57+
platform_is_not :windows do
58+
it "uses different message options" do
59+
@client.send("foobar", Socket::MSG_PEEK)
60+
sock = @server.accept
61+
peek_data = sock.recvfrom(6, Socket::MSG_PEEK) # Does not retrieve the message
62+
real_data = sock.recvfrom(6)
63+
64+
real_data.should == peek_data
65+
peek_data.should == ["foobar", ["AF_UNIX", ""]]
66+
sock.close
67+
end
6668
end
6769
end
6870

@@ -83,35 +85,37 @@
8385
end
8486
end
8587

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)
88+
platform_is_not :windows do
89+
# These specs are taken from the rdoc examples on UNIXSocket#recvfrom.
90+
describe 'using a UNIX socket constructed using UNIXSocket.for_fd' do
91+
before do
92+
@path1 = SocketSpecs.socket_path
93+
@path2 = SocketSpecs.socket_path.chop + '2'
94+
rm_r(@path2)
9295

93-
@client_raw = Socket.new(:UNIX, :DGRAM)
94-
@client_raw.bind(Socket.sockaddr_un(@path1))
96+
@client_raw = Socket.new(:UNIX, :DGRAM)
97+
@client_raw.bind(Socket.sockaddr_un(@path1))
9598

96-
@server_raw = Socket.new(:UNIX, :DGRAM)
97-
@server_raw.bind(Socket.sockaddr_un(@path2))
99+
@server_raw = Socket.new(:UNIX, :DGRAM)
100+
@server_raw.bind(Socket.sockaddr_un(@path2))
98101

99-
@socket = UNIXSocket.for_fd(@server_raw.fileno)
100-
@socket.autoclose = false
101-
end
102+
@socket = UNIXSocket.for_fd(@server_raw.fileno)
103+
@socket.autoclose = false
104+
end
102105

103-
after do
104-
@client_raw.close
105-
@server_raw.close # also closes @socket
106+
after do
107+
@client_raw.close
108+
@server_raw.close # also closes @socket
106109

107-
rm_r @path1
108-
rm_r @path2
109-
end
110+
rm_r @path1
111+
rm_r @path2
112+
end
110113

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

114-
@socket.recvfrom(5).should == ['hello', ['AF_UNIX', @path1]]
117+
@socket.recvfrom(5).should == ['hello', ['AF_UNIX', @path1]]
118+
end
115119
end
116120
end
117121
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)