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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
require_relative '../spec_helper'
require_relative '../fixtures/classes'
describe "BasicSocket#send" do
before :each do
@server = TCPServer.new('127.0.0.1', 0)
@port = @server.addr[1]
@socket = TCPSocket.new('127.0.0.1', @port)
end
after :each do
@server.closed?.should be_false
@socket.closed?.should be_false
@server.close
@socket.close
end
it "sends a message to another socket and returns the number of bytes sent" do
data = ""
t = Thread.new do
client = @server.accept
loop do
got = client.recv(5)
break if got.empty?
data << got
end
client.close
end
Thread.pass while t.status and t.status != "sleep"
t.status.should_not be_nil
@socket.send('hello', 0).should == 5
@socket.shutdown(1) # indicate, that we are done sending
@socket.recv(10)
t.join
data.should == 'hello'
end
platform_is_not :solaris, :windows do
it "accepts flags to specify unusual sending behaviour" do
data = nil
peek_data = nil
t = Thread.new do
client = @server.accept
peek_data = client.recv(6, Socket::MSG_PEEK)
data = client.recv(6)
client.recv(10) # this recv is important
client.close
end
Thread.pass while t.status and t.status != "sleep"
t.status.should_not be_nil
@socket.send('helloU', Socket::MSG_PEEK | Socket::MSG_OOB).should == 6
@socket.shutdown # indicate, that we are done sending
t.join
peek_data.should == "hello"
data.should == 'hello'
end
end
it "accepts a sockaddr as recipient address" do
data = ""
t = Thread.new do
client = @server.accept
loop do
got = client.recv(5)
break if got.empty?
data << got
end
client.close
end
Thread.pass while t.status and t.status != "sleep"
t.status.should_not be_nil
sockaddr = Socket.pack_sockaddr_in(@port, "127.0.0.1")
@socket.send('hello', 0, sockaddr).should == 5
@socket.shutdown # indicate, that we are done sending
t.join
data.should == 'hello'
end
end
describe 'BasicSocket#send' do
SocketSpecs.each_ip_protocol do |family, ip_address|
describe 'using a disconnected socket' do
before do
@client = Socket.new(family, :DGRAM)
@server = Socket.new(family, :DGRAM)
@server.bind(Socket.sockaddr_in(0, ip_address))
end
after do
@client.close
@server.close
end
describe 'with an object implementing #to_str' do
it 'returns the amount of sent bytes' do
data = mock('message')
data.should_receive(:to_str).and_return('hello')
@client.send(data, 0, @server.getsockname).should == 5
end
end
describe 'without a destination address' do
it "raises #{SocketSpecs.dest_addr_req_error}" do
-> { @client.send('hello', 0) }.should raise_error(SocketSpecs.dest_addr_req_error)
end
end
describe 'with a destination address as a String' do
it 'returns the amount of sent bytes' do
@client.send('hello', 0, @server.getsockname).should == 5
end
it 'does not persist the connection after writing to the socket' do
@client.send('hello', 0, @server.getsockname)
-> { @client.send('hello', 0) }.should raise_error(SocketSpecs.dest_addr_req_error)
end
end
describe 'with a destination address as an Addrinfo' do
it 'returns the amount of sent bytes' do
@client.send('hello', 0, @server.connect_address).should == 5
end
end
end
describe 'using a connected UDP socket' do
before do
@client = Socket.new(family, :DGRAM)
@server = Socket.new(family, :DGRAM)
@server.bind(Socket.sockaddr_in(0, ip_address))
end
after do
@client.close
@server.close
end
describe 'without a destination address argument' do
before do
@client.connect(@server.getsockname)
end
it 'returns the amount of bytes written' do
@client.send('hello', 0).should == 5
end
end
describe 'with a destination address argument' do
before do
@alt_server = Socket.new(family, :DGRAM)
@alt_server.bind(Socket.sockaddr_in(0, ip_address))
end
after do
@alt_server.close
end
it 'sends the message to the given address instead' do
@client.send('hello', 0, @alt_server.getsockname).should == 5
-> { @server.recv(5) }.should block_caller
@alt_server.recv(5).should == 'hello'
end
it 'does not persist the alternative connection after writing to the socket' do
@client.send('hello', 0, @alt_server.getsockname)
@client.connect(@server.getsockname)
@client.send('world', 0)
@server.recv(5).should == 'world'
end
end
end
platform_is_not :darwin, :windows do
describe 'using a connected TCP socket' do
before do
@client = Socket.new(family, :STREAM)
@server = Socket.new(family, :STREAM)
@server.bind(Socket.sockaddr_in(0, ip_address))
@server.listen(1)
@client.connect(@server.getsockname)
end
after do
@client.close
@server.close
end
describe 'using the MSG_OOB flag' do
it 'sends an out-of-band message' do
socket, _ = @server.accept
socket.setsockopt(:SOCKET, :OOBINLINE, true)
@client.send('a', Socket::MSG_OOB).should == 1
begin
socket.recv(10).should == 'a'
ensure
socket.close
end
end
end
end
end
end
end
|