1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
require File.expand_path('../../../../spec_helper', __FILE__)
require File.expand_path('../../fixtures/classes', __FILE__)
include Socket::Constants
describe "Socket#bind on SOCK_DGRAM socket" do
before :each do
@sock = Socket.new(AF_INET, SOCK_DGRAM, 0);
@sockaddr = Socket.pack_sockaddr_in(SocketSpecs.port, "127.0.0.1");
end
after :each do
@sock.closed?.should be_false
@sock.close
end
it "binds to a port" do
lambda { @sock.bind(@sockaddr) }.should_not raise_error
end
it "returns 0 if successful" do
@sock.bind(@sockaddr).should == 0
end
it "raises Errno::EINVAL when binding to an already bound port" do
@sock.bind(@sockaddr);
lambda { @sock.bind(@sockaddr); }.should raise_error(Errno::EINVAL);
end
it "raises Errno::EADDRNOTAVAIL when the specified sockaddr is not available from the local machine" do
sockaddr1 = Socket.pack_sockaddr_in(SocketSpecs.port, "4.3.2.1");
lambda { @sock.bind(sockaddr1); }.should raise_error(Errno::EADDRNOTAVAIL)
end
platform_is_not :windows, :cygwin do
it "raises Errno::EACCES when the current user does not have permission to bind" do
sockaddr1 = Socket.pack_sockaddr_in(1, "127.0.0.1");
lambda { @sock.bind(sockaddr1); }.should raise_error(Errno::EACCES)
end
end
end
describe "Socket#bind on SOCK_STREAM socket" do
before :each do
@sock = Socket.new(AF_INET, SOCK_STREAM, 0);
@sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, true)
@sockaddr = Socket.pack_sockaddr_in(SocketSpecs.port, "127.0.0.1");
end
after :each do
@sock.closed?.should be_false
@sock.close
end
it "binds to a port" do
lambda { @sock.bind(@sockaddr) }.should_not raise_error
end
it "returns 0 if successful" do
@sock.bind(@sockaddr).should == 0
end
it "raises Errno::EINVAL when binding to an already bound port" do
@sock.bind(@sockaddr);
lambda { @sock.bind(@sockaddr); }.should raise_error(Errno::EINVAL);
end
it "raises Errno::EADDRNOTAVAIL when the specified sockaddr is not available from the local machine" do
sockaddr1 = Socket.pack_sockaddr_in(SocketSpecs.port, "4.3.2.1");
lambda { @sock.bind(sockaddr1); }.should raise_error(Errno::EADDRNOTAVAIL)
end
platform_is_not :windows, :cygwin do
it "raises Errno::EACCES when the current user does not have permission to bind" do
sockaddr1 = Socket.pack_sockaddr_in(1, "127.0.0.1");
lambda { @sock.bind(sockaddr1); }.should raise_error(Errno::EACCES)
end
end
end
|