summaryrefslogtreecommitdiff
path: root/spec/ruby/library/socket/basicsocket/shutdown_spec.rb
blob: c78b32de38c3cde72eeffc01a861661d48f181e6 (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
155
require_relative '../spec_helper'
require_relative '../fixtures/classes'

platform_is_not :windows do # hangs
  describe "Socket::BasicSocket#shutdown" do
    SocketSpecs.each_ip_protocol do |family, ip_address|
      before do
        @server = Socket.new(family, :STREAM)
        @client = 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 an Integer' do
        it 'shuts down a socket for reading' do
          @client.shutdown(Socket::SHUT_RD)

          @client.recv(1).to_s.should be_empty
        end

        it 'shuts down a socket for writing' do
          @client.shutdown(Socket::SHUT_WR)

          -> { @client.write('hello') }.should raise_error(Errno::EPIPE)
        end

        it 'shuts down a socket for reading and writing' do
          @client.shutdown(Socket::SHUT_RDWR)

          @client.recv(1).to_s.should be_empty

          -> { @client.write('hello') }.should raise_error(Errno::EPIPE)
        end

        it 'raises ArgumentError when using an invalid option' do
          -> { @server.shutdown(666) }.should raise_error(ArgumentError)
        end
      end

      describe 'using a Symbol' do
        it 'shuts down a socket for reading using :RD' do
          @client.shutdown(:RD)

          @client.recv(1).to_s.should be_empty
        end

        it 'shuts down a socket for reading using :SHUT_RD' do
          @client.shutdown(:SHUT_RD)

          @client.recv(1).to_s.should be_empty
        end

        it 'shuts down a socket for writing using :WR' do
          @client.shutdown(:WR)

          -> { @client.write('hello') }.should raise_error(Errno::EPIPE)
        end

        it 'shuts down a socket for writing using :SHUT_WR' do
          @client.shutdown(:SHUT_WR)

          -> { @client.write('hello') }.should raise_error(Errno::EPIPE)
        end

        it 'shuts down a socket for reading and writing' do
          @client.shutdown(:RDWR)

          @client.recv(1).to_s.should be_empty

          -> { @client.write('hello') }.should raise_error(Errno::EPIPE)
        end

        it 'raises ArgumentError when using an invalid option' do
          -> { @server.shutdown(:Nope) }.should raise_error(SocketError)
        end
      end

      describe 'using a String' do
        it 'shuts down a socket for reading using "RD"' do
          @client.shutdown('RD')

          @client.recv(1).to_s.should be_empty
        end

        it 'shuts down a socket for reading using "SHUT_RD"' do
          @client.shutdown('SHUT_RD')

          @client.recv(1).to_s.should be_empty
        end

        it 'shuts down a socket for writing using "WR"' do
          @client.shutdown('WR')

          -> { @client.write('hello') }.should raise_error(Errno::EPIPE)
        end

        it 'shuts down a socket for writing using "SHUT_WR"' do
          @client.shutdown('SHUT_WR')

          -> { @client.write('hello') }.should raise_error(Errno::EPIPE)
        end

        it 'raises ArgumentError when using an invalid option' do
          -> { @server.shutdown('Nope') }.should raise_error(SocketError)
        end
      end

      describe 'using an object that responds to #to_str' do
        before do
          @dummy = mock(:dummy)
        end

        it 'shuts down a socket for reading using "RD"' do
          @dummy.stub!(:to_str).and_return('RD')

          @client.shutdown(@dummy)

          @client.recv(1).to_s.should be_empty
        end

        it 'shuts down a socket for reading using "SHUT_RD"' do
          @dummy.stub!(:to_str).and_return('SHUT_RD')

          @client.shutdown(@dummy)

          @client.recv(1).to_s.should be_empty
        end

        it 'shuts down a socket for reading and writing' do
          @dummy.stub!(:to_str).and_return('RDWR')

          @client.shutdown(@dummy)

          @client.recv(1).to_s.should be_empty

          -> { @client.write('hello') }.should raise_error(Errno::EPIPE)
        end
      end

      describe 'using an object that does not respond to #to_str' do
        it 'raises TypeError' do
          -> { @server.shutdown(mock(:dummy)) }.should raise_error(TypeError)
        end
      end
    end
  end
end