summaryrefslogtreecommitdiff
path: root/spec/ruby/library/net/ftp/initialize_spec.rb
blob: 507320e494fe561294102045c6a1586dde6eba7a (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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
require_relative '../../../spec_helper'
require_relative 'spec_helper'

describe "Net::FTP#initialize" do
  before :each do
    @ftp = Net::FTP.allocate
    @ftp.stub!(:connect)
    @port_args = []
    ruby_version_is "2.5" do
      @port_args << 21
    end
  end

  it "is private" do
    Net::FTP.should have_private_instance_method(:initialize)
  end

  it "sets self into binary mode" do
    @ftp.binary.should be_nil
    @ftp.send(:initialize)
    @ftp.binary.should be_true
  end

  it "sets self into active mode" do
    @ftp.passive.should be_nil
    @ftp.send(:initialize)
    @ftp.passive.should be_false
  end

  it "sets self into non-debug mode" do
    @ftp.debug_mode.should be_nil
    @ftp.send(:initialize)
    @ftp.debug_mode.should be_false
  end

  it "sets self to not resume file uploads/downloads" do
    @ftp.resume.should be_nil
    @ftp.send(:initialize)
    @ftp.resume.should be_false
  end

  describe "when passed no arguments" do
    it "does not try to connect" do
      @ftp.should_not_receive(:connect)
      @ftp.send(:initialize)
    end
  end

  describe "when passed host" do
    it "tries to connect to the passed host" do
      @ftp.should_receive(:connect).with("localhost", *@port_args)
      @ftp.send(:initialize, "localhost")
    end
  end

  describe "when passed host, user" do
    it "tries to connect to the passed host" do
      @ftp.should_receive(:connect).with("localhost", *@port_args)
      @ftp.send(:initialize, "localhost")
    end

    it "tries to login with the passed username" do
      @ftp.should_receive(:login).with("rubyspec", nil, nil)
      @ftp.send(:initialize, "localhost", "rubyspec")
    end
  end

  describe "when passed host, user, password" do
    it "tries to connect to the passed host" do
      @ftp.should_receive(:connect).with("localhost", *@port_args)
      @ftp.send(:initialize, "localhost")
    end

    it "tries to login with the passed username and password" do
      @ftp.should_receive(:login).with("rubyspec", "rocks", nil)
      @ftp.send(:initialize, "localhost", "rubyspec", "rocks")
    end
  end

  describe "when passed host, user" do
    it "tries to connect to the passed host" do
      @ftp.should_receive(:connect).with("localhost", *@port_args)
      @ftp.send(:initialize, "localhost")
    end

    it "tries to login with the passed username, password and account" do
      @ftp.should_receive(:login).with("rubyspec", "rocks", "account")
      @ftp.send(:initialize, "localhost", "rubyspec", "rocks", "account")
    end
  end

  before :each do
    @ftp.stub!(:login)
  end

  describe 'when the host' do
    describe 'is set' do
      describe 'and port option' do
        describe 'is set' do
          it 'tries to connect to the host on the specified port' do
            options = mock('ftp initialize options')
            options.should_receive(:to_hash).and_return({ port: 8080 })
            @ftp.should_receive(:connect).with('localhost', 8080)

            @ftp.send(:initialize, 'localhost', options)
          end
        end

        describe 'is not set' do
          it 'tries to connect to the host without a port' do
            @ftp.should_receive(:connect).with("localhost", *@port_args)

            @ftp.send(:initialize, 'localhost')
          end
        end
      end

      describe 'when the username option' do
        describe 'is set' do
          describe 'and the password option' do
            describe 'is set' do
              describe 'and the account option' do
                describe 'is set' do
                  it 'tries to log in with the supplied parameters' do
                    options = mock('ftp initialize options')
                    options.should_receive(:to_hash).and_return({ username: 'a', password: 'topsecret', account: 'b' })
                    @ftp.should_receive(:login).with('a', 'topsecret', 'b')

                    @ftp.send(:initialize, 'localhost', options)
                  end
                end

                describe 'is unset' do
                  it 'tries to log in with the supplied parameters' do
                    options = mock('ftp initialize options')
                    options.should_receive(:to_hash).and_return({ username: 'a', password: 'topsecret' })
                    @ftp.should_receive(:login).with('a', 'topsecret', nil)

                    @ftp.send(:initialize, 'localhost', options)
                  end
                end
              end
            end

            describe 'is unset' do
              describe 'and the account option' do
                describe 'is set' do
                  it 'tries to log in with the supplied parameters' do
                    options = mock('ftp initialize options')
                    options.should_receive(:to_hash).and_return({ username: 'a', account: 'b' })
                    @ftp.should_receive(:login).with('a', nil, 'b')

                    @ftp.send(:initialize, 'localhost', options)
                  end
                end

                describe 'is unset' do
                  it 'tries to log in with the supplied parameters' do
                    options = mock('ftp initialize options')
                    options.should_receive(:to_hash).and_return({ username: 'a'})
                    @ftp.should_receive(:login).with('a', nil, nil)

                    @ftp.send(:initialize, 'localhost', options)
                  end
                end
              end
            end
          end
        end

        describe 'is not set' do
          it 'does not try to log in' do
            options = mock('ftp initialize options')
            options.should_receive(:to_hash).and_return({})
            @ftp.should_not_receive(:login)

            @ftp.send(:initialize, 'localhost', options)
          end
        end
      end
    end

    describe 'is unset' do
      it 'does not try to connect' do
        @ftp.should_not_receive(:connect)

        @ftp.send(:initialize)
      end

      it 'does not try to log in' do
        @ftp.should_not_receive(:login)

        @ftp.send(:initialize)
      end
    end
  end

  describe 'when the passive option' do
    describe 'is set' do
      describe 'to true' do
        it 'sets passive to true' do
          options = mock('ftp initialize options')
          options.should_receive(:to_hash).and_return({ passive: true })

          @ftp.send(:initialize, nil, options)
          @ftp.passive.should == true
        end
      end

      describe 'to false' do
        it 'sets passive to false' do
          options = mock('ftp initialize options')
          options.should_receive(:to_hash).and_return({ passive: false })

          @ftp.send(:initialize, nil, options)
          @ftp.passive.should == false
        end
      end
    end

    describe 'is unset' do
      it 'sets passive to false' do
        @ftp.send(:initialize)
        @ftp.passive.should == false
      end
    end
  end

  describe 'when the debug_mode option' do
    describe 'is set' do
      describe 'to true' do
        it 'sets debug_mode to true' do
          options = mock('ftp initialize options')
          options.should_receive(:to_hash).and_return({ debug_mode: true })

          @ftp.send(:initialize, nil, options)
          @ftp.debug_mode.should == true
        end
      end

      describe 'to false' do
        it 'sets debug_mode to false' do
          options = mock('ftp initialize options')
          options.should_receive(:to_hash).and_return({ debug_mode: false })

          @ftp.send(:initialize, nil, options)
          @ftp.debug_mode.should == false
        end
      end
    end

    describe 'is unset' do
      it 'sets debug_mode to false' do
        @ftp.send(:initialize)
        @ftp.debug_mode.should == false
      end
    end
  end

  describe 'when the open_timeout option' do
    describe 'is set' do
      it 'sets open_timeout to the specified value' do
        options = mock('ftp initialize options')
        options.should_receive(:to_hash).and_return({ open_timeout: 42 })

        @ftp.send(:initialize, nil, options)
        @ftp.open_timeout.should == 42
      end
    end

    describe 'is not set' do
      it 'sets open_timeout to nil' do
        @ftp.send(:initialize)
        @ftp.open_timeout.should == nil
      end
    end
  end

  describe 'when the read_timeout option' do
    describe 'is set' do
      it 'sets read_timeout to the specified value' do
        options = mock('ftp initialize options')
        options.should_receive(:to_hash).and_return({ read_timeout: 100 })

        @ftp.send(:initialize, nil, options)
        @ftp.read_timeout.should == 100
      end
    end

    describe 'is not set' do
      it 'sets read_timeout to the default value' do
        @ftp.send(:initialize)
        @ftp.read_timeout.should == 60
      end
    end
  end

  describe 'when the ssl_handshake_timeout option' do
    describe 'is set' do
      it 'sets ssl_handshake_timeout to the specified value' do
        options = mock('ftp initialize options')
        options.should_receive(:to_hash).and_return({ ssl_handshake_timeout: 23 })

        @ftp.send(:initialize, nil, options)
        @ftp.ssl_handshake_timeout.should == 23
      end
    end

    describe 'is not set' do
      it 'sets ssl_handshake_timeout to nil' do
        @ftp.send(:initialize)
        @ftp.ssl_handshake_timeout.should == nil
      end
    end
  end

  describe 'when the ssl option' do
    describe 'is set' do
      describe "and the ssl option's value is true" do
        it 'initializes ssl_context to a blank SSLContext object' do
          options = mock('ftp initialize options')
          options.should_receive(:to_hash).and_return({ ssl: true })

          ssl_context = OpenSSL::SSL::SSLContext.allocate
          ssl_context.stub!(:set_params)

          OpenSSL::SSL::SSLContext.should_receive(:new).and_return(ssl_context)
          ssl_context.should_receive(:set_params).with({})

          @ftp.send(:initialize, nil, options)
          @ftp.instance_variable_get(:@ssl_context).should == ssl_context
        end
      end

      describe "and the ssl option's value is a hash" do
        it 'initializes ssl_context to a configured SSLContext object' do
          options = mock('ftp initialize options')
          options.should_receive(:to_hash).and_return({ ssl: {key: 'value'} })

          ssl_context = OpenSSL::SSL::SSLContext.allocate
          ssl_context.stub!(:set_params)

          OpenSSL::SSL::SSLContext.should_receive(:new).and_return(ssl_context)
          ssl_context.should_receive(:set_params).with({key: 'value'})

          @ftp.send(:initialize, nil, options)
          @ftp.instance_variable_get(:@ssl_context).should == ssl_context
        end
      end

      describe 'and private_data_connection' do
        describe 'is set' do
          it 'sets private_data_connection to that value' do
            options = mock('ftp initialize options')
            options.should_receive(:to_hash).and_return({ ssl: true, private_data_connection: 'true' })

            @ftp.send(:initialize, nil, options)
            @ftp.instance_variable_get(:@private_data_connection).should == 'true'
          end
        end

        describe 'is not set' do
          it 'sets private_data_connection to nil' do
            options = mock('ftp initialize options')
            options.should_receive(:to_hash).and_return({ ssl: true })

            @ftp.send(:initialize, nil, options)
            @ftp.instance_variable_get(:@private_data_connection).should == true
          end
        end
      end
    end

    describe 'is not set' do
      it 'sets ssl_context to nil' do
        options = mock('ftp initialize options')
        options.should_receive(:to_hash).and_return({})

        @ftp.send(:initialize, nil, options)
        @ftp.instance_variable_get(:@ssl_context).should == nil
      end

      describe 'private_data_connection' do
        describe 'is set' do
          it 'raises an ArgumentError' do
            options = mock('ftp initialize options')
            options.should_receive(:to_hash).and_return({ private_data_connection: true })

            -> {
              @ftp.send(:initialize, nil, options)
            }.should raise_error(ArgumentError, /private_data_connection can be set to true only when ssl is enabled/)
          end
        end

        describe 'is not set' do
          it 'sets private_data_connection to false' do
            options = mock('ftp initialize options')
            options.should_receive(:to_hash).and_return({})

            @ftp.send(:initialize, nil, options)
            @ftp.instance_variable_get(:@private_data_connection).should == false
          end
        end
      end
    end
  end
end