This repository has been archived by the owner on Mar 3, 2021. It is now read-only.
/
server_spec.rb
553 lines (452 loc) · 16.1 KB
/
server_spec.rb
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
require "spec_helper.rb"
require 'net/ftp'
describe FakeFtp::Server, 'setup' do
it "starts a server on port n" do
server = FakeFtp::Server.new(21212)
server.port.should == 21212
end
it "should defaults to port 21" do
server = FakeFtp::Server.new
server.port.should == 21
end
it "starts a passive server on port p" do
server = FakeFtp::Server.new(21212, 21213)
server.passive_port.should == 21213
end
it "should start and stop" do
server = FakeFtp::Server.new(21212)
server.is_running?.should be_false
server.start
server.is_running?.should be_true
server.stop
server.is_running?.should be_false
end
it "should default :mode to :active" do
server = FakeFtp::Server.new(21212, 21213)
server.mode.should == :active
end
it "should start and stop passive port" do
server = FakeFtp::Server.new(21212, 21213)
server.is_running?(21213).should be_false
server.start
server.is_running?(21213).should be_true
server.stop
server.is_running?(21213).should be_false
end
it "should raise if attempting to use a bound port" do
server = FakeFtp::Server.new(21212)
server.start
proc { FakeFtp::Server.new(21212) }.should raise_error(Errno::EADDRINUSE, "Address already in use - 21212")
server.stop
end
it "should raise if attempting to use a bound passive_port" do
server = FakeFtp::Server.new(21212, 21213)
server.start
proc { FakeFtp::Server.new(21214, 21213) }.should raise_error(Errno::EADDRINUSE, "Address already in use - 21213")
server.stop
end
end
describe FakeFtp::Server, 'files' do
let(:file) { FakeFtp::File.new('filename', 34) }
let(:server) { FakeFtp::Server.new(21212) }
before { server.instance_variable_set(:@files, [file]) }
it "returns filenames from :files" do
server.files.should include('filename')
end
it "can be accessed with :file" do
server.file('filename').should == file
end
it "can reset files" do
server.reset
server.files.should == []
end
end
describe FakeFtp::Server, 'commands' do
let(:server) { FakeFtp::Server.new(21212, 21213) }
let(:client) { TCPSocket.open('127.0.0.1', 21212) }
before { server.start }
after {
client.close
server.stop
}
context 'general' do
it "should accept connections" do
client.gets.should == "220 Can has FTP?\r\n"
end
it "should get unknown command response when nothing is sent" do
client.gets
client.puts
client.gets.should == "500 Unknown command\r\n"
end
it "accepts QUIT" do
client.gets
client.puts "QUIT"
client.gets.should == "221 OMG bye!\r\n"
end
it "should accept multiple commands in one session" do
client.gets
client.puts "USER thing"
client.gets
client.puts "PASS thing"
client.gets
client.puts "ACCT thing"
client.gets
client.puts "USER thing"
end
end
context 'passive' do
it "accepts PASV" do
server.mode.should == :active
client.gets
client.puts "PASV"
client.gets.should == "227 Entering Passive Mode (127,0,0,1,82,221)\r\n"
server.mode.should == :passive
end
it "responds with correct PASV port" do
server.stop
server.passive_port = 21111
server.start
client.gets
client.puts "PASV"
client.gets.should == "227 Entering Passive Mode (127,0,0,1,82,119)\r\n"
end
it "does not accept PASV if no port set" do
server.stop
server.passive_port = nil
server.start
client.gets
client.puts "PASV"
client.gets.should == "502 Aww hell no, use Active\r\n"
end
end
context 'active' do
before :each do
client.gets
@data_server = ::TCPServer.new('127.0.0.1', 21216)
@data_connection = Thread.new do
@server_client = @data_server.accept
@server_client.should_not be_nil
end
end
after :each do
@data_server.close
@data_server = nil
@data_connection = nil
end
it "accepts PORT and connects to port" do
client.puts "PORT 127,0,0,1,82,224"
client.gets.should == "200 Okay\r\n"
@data_connection.join
end
it "should switch to :active on port command" do
server.mode.should == :active
client.puts 'PASV'
client.gets
server.mode.should == :passive
client.puts "PORT 127,0,0,1,82,224"
client.gets.should == "200 Okay\r\n"
@data_connection.join
server.mode.should == :active
end
end
context 'authentication commands' do
before :each do
client.gets ## connection successful response
end
it "accepts USER" do
client.puts "USER some_dude"
client.gets.should == "331 send your password\r\n"
end
it "accepts anonymous USER" do
client.puts "USER anonymous"
client.gets.should == "230 logged in\r\n"
end
it "accepts PASS" do
client.puts "PASS password"
client.gets.should == "230 logged in\r\n"
end
it "accepts ACCT" do
client.puts "ACCT"
client.gets.should == "230 WHATEVER!\r\n"
end
end
context 'directory commands' do
before :each do
client.gets ## connection successful response
end
it "returns directory on PWD" do
client.puts "PWD"
client.gets.should == "257 \"/pub\" is current directory\r\n"
end
it "says OK to any CWD, CDUP, without doing anything" do
client.puts "CWD somewhere/else"
client.gets.should == "250 OK!\r\n"
client.puts "CDUP"
client.gets.should == "250 OK!\r\n"
end
end
context 'file commands' do
before :each do
client.gets ## connection successful response
end
it "accepts TYPE ascii" do
client.puts "TYPE A"
client.gets.should == "200 Type set to A.\r\n"
end
it "accepts TYPE image" do
client.puts "TYPE I"
client.gets.should == "200 Type set to I.\r\n"
end
it "does not accept TYPEs other than ascii or image" do
client.puts "TYPE E"
client.gets.should == "504 We don't allow those\r\n"
client.puts "TYPE N"
client.gets.should == "504 We don't allow those\r\n"
client.puts "TYPE T"
client.gets.should == "504 We don't allow those\r\n"
client.puts "TYPE C"
client.gets.should == "504 We don't allow those\r\n"
client.puts "TYPE L"
client.gets.should == "504 We don't allow those\r\n"
end
context 'passive' do
let(:data_client) { TCPSocket.open('127.0.0.1', 21213) }
before :each do
client.puts 'PASV'
client.gets.should == "227 Entering Passive Mode (127,0,0,1,82,221)\r\n"
end
it "accepts STOR with filename" do
client.puts "STOR some_file"
client.gets.should == "125 Do it!\r\n"
data_client.puts "1234567890"
data_client.close
client.gets.should == "226 Did it!\r\n"
server.files.should include('some_file')
# server.file('some_file').bytes.should == 10
end
it "accepts STOR with filename and long file" do
client.puts "STOR some_file"
client.gets.should == "125 Do it!\r\n"
data_client.puts("1234567890" * 10_000)
data_client.close
client.gets.should == "226 Did it!\r\n"
server.files.should include('some_file')
end
it "does not accept RETR without a filename" do
client.puts "RETR"
client.gets.should == "501 No filename given\r\n"
end
it "does not serve files that do not exist" do
client.puts "RETR some_file"
client.gets.should == "550 File not found\r\n"
end
it "accepts RETR with a filename" do
server.add_file('some_file', '1234567890')
client.puts "RETR some_file"
client.gets.should == "150 File status ok, about to open data connection\r\n"
data = data_client.read(1024)
data_client.close
data.should == '1234567890'
client.gets.should == "226 File transferred\r\n"
end
it "accepts DELE with a filename" do
server.add_file('some_file', '1234567890')
client.puts "DELE some_file"
client.gets.should == "250 Delete operation successful.\r\n"
server.files.should_not include('some_file')
end
it "gives error message when trying to delete a file that does not exist" do
client.puts "DELE non_existing_file"
client.gets.should == "550 Delete operation failed.\r\n"
end
it "accepts a LIST command" do
server.add_file('some_file', '1234567890')
server.add_file('another_file', '1234567890')
client.puts "LIST"
client.gets.should == "150 Listing status ok, about to open data connection\r\n"
data = data_client.read(2048)
data_client.close
data.should == [
"-rw-r--r--\t1\towner\tgroup\t10\t#{server.file('some_file').created.strftime('%b %d %H:%M')}\tsome_file",
"-rw-r--r--\t1\towner\tgroup\t10\t#{server.file('another_file').created.strftime('%b %d %H:%M')}\tanother_file",
].join("\n")
client.gets.should == "226 List information transferred\r\n"
end
it "accepts a LIST command with a wildcard argument" do
files = ['test.jpg', 'test-2.jpg', 'test.txt']
files.each do |file|
server.add_file(file, '1234567890')
end
client.puts "LIST *.jpg"
client.gets.should == "150 Listing status ok, about to open data connection\r\n"
data = data_client.read(2048)
data_client.close
data.should == files[0,2].map do |file|
"-rw-r--r--\t1\towner\tgroup\t10\t#{server.file(file).created.strftime('%b %d %H:%M')}\t#{file}"
end.join("\n")
client.gets.should == "226 List information transferred\r\n"
end
it "accepts a LIST command with multiple wildcard arguments" do
files = ['test.jpg', 'test.gif', 'test.txt']
files.each do |file|
server.add_file(file, '1234567890')
end
client.puts "LIST *.jpg *.gif"
client.gets.should == "150 Listing status ok, about to open data connection\r\n"
data = data_client.read(2048)
data_client.close
data.should == files[0,2].map do |file|
"-rw-r--r--\t1\towner\tgroup\t10\t#{server.file(file).created.strftime('%b %d %H:%M')}\t#{file}"
end.join("\n")
client.gets.should == "226 List information transferred\r\n"
end
it "accepts an NLST command" do
server.add_file('some_file', '1234567890')
server.add_file('another_file', '1234567890')
client.puts "NLST"
client.gets.should == "150 Listing status ok, about to open data connection\r\n"
data = data_client.read(1024)
data_client.close
data.should == "some_file\nanother_file"
client.gets.should == "226 List information transferred\r\n"
end
end
context 'active' do
before :each do
@data_server = ::TCPServer.new('127.0.0.1', 21216)
@data_connection = Thread.new do
@server_client = @data_server.accept
end
end
after :each do
@data_server.close
@data_connection = nil
@data_server = nil
end
it 'creates a directory on MKD' do
client.puts "MKD some_dir"
client.gets.should == "257 OK!\r\n"
end
it 'should save the directory after you CWD' do
client.puts "CWD /somewhere/else"
client.gets.should == "250 OK!\r\n"
client.puts "PWD"
client.gets.should == "257 \"/somewhere/else\" is current directory\r\n"
end
it 'CWD should add a / to the beginning of the directory' do
client.puts "CWD somewhere/else"
client.gets.should == "250 OK!\r\n"
client.puts "PWD"
client.gets.should == "257 \"/somewhere/else\" is current directory\r\n"
end
it 'should not change the directory on CDUP' do
client.puts "CDUP"
client.gets.should == "250 OK!\r\n"
client.puts "PWD"
client.gets.should == "257 \"/pub\" is current directory\r\n"
end
it "sends error message if no PORT received" do
client.puts "STOR some_file"
client.gets.should == "425 Ain't no data port!\r\n"
end
it "accepts STOR with filename" do
client.puts "PORT 127,0,0,1,82,224"
client.gets.should == "200 Okay\r\n"
client.puts "STOR some_other_file"
client.gets.should == "125 Do it!\r\n"
@data_connection.join
@server_client.print "12345"
@server_client.close
client.gets.should == "226 Did it!\r\n"
server.files.should include('some_other_file')
server.file('some_other_file').bytes.should == 5
end
it "accepts RETR with a filename" do
client.puts "PORT 127,0,0,1,82,224"
client.gets.should == "200 Okay\r\n"
server.add_file('some_file', '1234567890')
client.puts "RETR some_file"
client.gets.should == "150 File status ok, about to open data connection\r\n"
@data_connection.join
data = @server_client.read(1024)
@server_client.close
data.should == '1234567890'
client.gets.should == "226 File transferred\r\n"
end
it 'can rename with RNFR and RNTO' do
client.puts "PORT 127,0,0,1,82,224"
client.gets.should == "200 Okay\r\n"
server.add_file('some_file', '1234567890')
server.add_file('another_file', '1234567890')
client.puts "RNFR some_file"
client.gets.should == "350 Waiting for rnto\r\n"
client.puts "RNTO some_file_renamed"
client.gets.should == "250 OK!\r\n"
server.files.should == ["some_file_renamed", "another_file"]
end
it "accepts an NLST command" do
client.puts "PORT 127,0,0,1,82,224"
client.gets.should == "200 Okay\r\n"
server.add_file('some_file', '1234567890')
server.add_file('another_file', '1234567890')
client.puts "NLST"
client.gets.should == "150 Listing status ok, about to open data connection\r\n"
@data_connection.join
data = @server_client.read(1024)
@server_client.close
data.should == "some_file\nanother_file"
client.gets.should == "226 List information transferred\r\n"
end
end
end
end
describe FakeFtp::Server, 'with ftp client' do
let(:server) { FakeFtp::Server.new(21212, 21213) }
let(:client) { Net::FTP.new }
let(:text_filename) { File.expand_path("../../fixtures/text_file.txt", File.dirname(__FILE__)) }
before { server.start }
after :each do
client.close
server.stop
end
it 'should accept connections' do
proc { client.connect('127.0.0.1', 21212) }.should_not raise_error
end
context "" do
before { client.connect("127.0.0.1", 21212) }
it "should allow anonymous authentication" do
proc { client.login }.should_not raise_error
end
it "should allow named authentication" do
proc { client.login('someone', 'password') }.should_not raise_error
end
it "should allow client to quit" do
proc { client.login('someone', 'password') }.should_not raise_error
proc { client.quit }.should_not raise_error
end
it "should put files using PASV" do
File.stat(text_filename).size.should == 20
client.passive = true
proc { client.put(text_filename) }.should_not raise_error
server.files.should include('text_file.txt')
server.file('text_file.txt').bytes.should == 20
server.file('text_file.txt').should be_passive
server.file('text_file.txt').should_not be_active
end
it "should put files using active" do
File.stat(text_filename).size.should == 20
client.passive = false
proc { client.put(text_filename) }.should_not raise_error
server.files.should include('text_file.txt')
server.file('text_file.txt').bytes.should == 20
server.file('text_file.txt').should_not be_passive
server.file('text_file.txt').should be_active
end
xit "should disconnect clients on close" do
# TODO: when this succeeds, we can care less about manually closing clients
# otherwise we get a CLOSE_WAIT process hanging around that blocks our port
server.stop
client.closed?.should be_true
end
end
end