summaryrefslogtreecommitdiff
path: root/spec/ruby/library/socket/udpsocket/send_spec.rb
blob: 5d5de684afb8c37efef2ff426f554d609aa1acff (plain)
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
require_relative '../spec_helper'
require_relative '../fixtures/classes'

describe "UDPSocket#send" do
  before :each do
    @port = nil
    @server_thread = Thread.new do
      @server = UDPSocket.open
      begin
        @server.bind(nil, 0)
        @port = @server.addr[1]
        begin
          @msg = @server.recvfrom_nonblock(64)
        rescue IO::WaitReadable
          IO.select([@server])
          retry
        end
      ensure
        @server.close if !@server.closed?
      end
    end
    Thread.pass while @server_thread.status and !@port
  end

  after :each do
    @server_thread.join
  end

  it "sends data in ad hoc mode" do
    @socket = UDPSocket.open
    @socket.send("ad hoc", 0, SocketSpecs.hostname, @port)
    @socket.close
    @server_thread.join

    @msg[0].should == "ad hoc"
    @msg[1][0].should == "AF_INET"
    @msg[1][1].should be_kind_of(Integer)
    @msg[1][3].should == "127.0.0.1"
  end

  it "sends data in ad hoc mode (with port given as a String)" do
    @socket = UDPSocket.open
    @socket.send("ad hoc", 0, SocketSpecs.hostname, @port.to_s)
    @socket.close
    @server_thread.join

    @msg[0].should == "ad hoc"
    @msg[1][0].should == "AF_INET"
    @msg[1][1].should be_kind_of(Integer)
    @msg[1][3].should == "127.0.0.1"
  end

  it "sends data in connection mode" do
    @socket = UDPSocket.open
    @socket.connect(SocketSpecs.hostname, @port)
    @socket.send("connection-based", 0)
    @socket.close
    @server_thread.join

    @msg[0].should == "connection-based"
    @msg[1][0].should == "AF_INET"
    @msg[1][1].should be_kind_of(Integer)
    @msg[1][3].should == "127.0.0.1"
  end

  it "raises EMSGSIZE if data is too too big" do
    @socket = UDPSocket.open
    begin
      -> do
        @socket.send('1' * 100_000, 0, SocketSpecs.hostname, @port.to_s)
      end.should raise_error(Errno::EMSGSIZE)
    ensure
      @socket.send("ad hoc", 0, SocketSpecs.hostname, @port)
      @socket.close
      @server_thread.join
    end
  end
end

describe 'UDPSocket#send' do
  SocketSpecs.each_ip_protocol do |family, ip_address|
    before do
      @server = UDPSocket.new(family)
      @client = UDPSocket.new(family)

      @server.bind(ip_address, 0)

      @addr = @server.connect_address
    end

    after do
      @server.close
      @client.close
    end

    describe 'using a disconnected socket' do
      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 separate arguments' do
        it 'returns the amount of sent bytes' do
          @client.send('hello', 0, @addr.ip_address, @addr.ip_port).should == 5
        end

        it 'does not persist the connection after sending data' do
          @client.send('hello', 0, @addr.ip_address, @addr.ip_port)

          -> { @client.send('hello', 0) }.should raise_error(SocketSpecs.dest_addr_req_error)
        end
      end

      describe 'with a destination address as a single String argument' do
        it 'returns the amount of sent bytes' do
          @client.send('hello', 0, @server.getsockname).should == 5
        end
      end
    end

    describe 'using a connected socket' do
      describe 'without an explicit destination address' do
        before do
          @client.connect(@addr.ip_address, @addr.ip_port)
        end

        it 'returns the amount of bytes written' do
          @client.send('hello', 0).should == 5
        end
      end

      describe 'with an explicit destination address' do
        before do
          @alt_server = UDPSocket.new(family)

          @alt_server.bind(ip_address, 0)
        end

        after do
          @alt_server.close
        end

        it 'sends the data 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
      end
    end
  end
end