Permalink
Browse files

Clean up

  • Loading branch information...
sax committed Feb 18, 2011
1 parent 4ecbd16 commit 80189f9dc30ab21796252ea5102f1b936e9d3390
View
1 .rvmrc
@@ -0,0 +1 @@
+rvm use ree@fake_ftp --create
View
@@ -7,6 +7,7 @@ GEM
remote: http://rubygems.org/
specs:
diff-lcs (1.1.2)
+ rake (0.8.7)
rspec (2.5.0)
rspec-core (~> 2.5.0)
rspec-expectations (~> 2.5.0)
@@ -20,5 +21,7 @@ PLATFORMS
ruby
DEPENDENCIES
+ bundler (>= 1.0.10)
fake_ftp!
+ rake (>= 0.8.7)
rspec (> 2)
View
@@ -4,10 +4,7 @@ Bundler::GemHelper.install_tasks
require 'rspec/core'
require 'rspec/core/rake_task'
-RSpec::Core::RakeTask.new(:spec) do |spec|
- spec.pattern = FileList['rspec/**/*_spec.rb']
-end
-#RSpec::Core::RakeTask.new('spec')
+RSpec::Core::RakeTask.new('spec')
# If you want to make this the default task
task :default => :spec
View
@@ -15,7 +15,9 @@ Gem::Specification.new do |s|
s.required_rubygems_version = ">= 1.3.6"
s.rubyforge_project = "fake_ftp"
+ s.add_development_dependency "bundler", '>=1.0.10'
s.add_development_dependency "rspec", '>2'
+ s.add_development_dependency "rake", '>=0.8.7'
s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
View
@@ -2,119 +2,27 @@
module FakeFtp
class Server < TCPServer
- attr_accessor :directory
+ attr_accessor :directory, :status, :port
- COMMANDS = %w[quit type user retr stor port cdup cwd dele rmd pwd list size
- syst site mkd pass]
- LBRK = "\r\n"
+ CMDS = %w[]
+ LNBK = "\r\n"
def initialize(port = 21, *args)
- @status = :started
+ self.port = port
+ @status = :dead
@logger = ::Logger.new(STDERR)
-
- server = super('127.0.0.1', port)
-
- while (@status == :started)
- begin
- socket = server.accept
-
- @current_thread = Thread.new(socket) do |sock|
- current_thread[:socket] = sock
- current_thread[:mode] = :binary
- log "Got connection"
- response "200 FTP server"
- while sock.nil? == false and sock.closed? == false
- request = sock.gets
- response handler(request)
- end
- end
- rescue Interrupt
- @status = :stopped
- rescue Exception => ex
- @status = :stopped
- request ||= 'No request'
- log "#{ex.class}: #{ex.message} - #{request}\n\t#{ex.backtrace[0]}"
- end
- end
end
- def current_thread
- Thread.current
+ def start
+ @status = :started
end
def stop
- current_thread[:status] = :stopped
- end
-
- def log(stuff)
- @logger.puts("[#{Time.new.ctime}] %s" % stuff)
- end
-
- def handler(request)
- return if request.nil? or request.to_s == ''
- begin
- command = request[0, 4].downcase.strip
- rqarray = request.split
- message = rqarray.length > 2 ? rqarray[1..rqarray.length] : rqarray[1]
- log "Request: #{command}(#{message})"
- case command
- when *COMMANDS
- __send__ command, message
- else
- bad_command command, message
- end
- rescue Errno::EACCES, Errno::EPERM
- "553 Permission denied"
- rescue Errno::ENOENT
- "553 File doesn't exist"
- rescue Exception => e
- log "Request: #{request}"
- log "Error: #{e.class} - #{e.message}\n\t#{e.backtrace[0]}"
- exit!
- end
- end
-
- # send a message to the client
- def response(message)
- sock = current_thread[:socket]
- sock.print message << LBRK unless msg.nil? or sock.nil? or sock.closed?
- end
-
- # command not understood
- def bad_command(name, *params)
- arg = (params.is_a? Array) ? params.join(' ') : params
- if @config[:debug]
- "500 I don't understand " << name.to_s << "(" << arg << ")"
- else
- "500 Sorry, I don't understand #{name.to_s}"
- end
- end
-
- def next_state(current)
- @status = case current
- when :connect
- :waiting
- when :waiting
- :complete
- when :complete
- :finished
- end
- end
-
- def user(msg)
- "230 OK, logging in"
- end
-
- def pass(msg)
- "230 Password accepted"
+ @status = :dead
end
- # quit the ftp session
- def quit(msg = false)
- current_thread[:socket].close
- current_thread[:socket] = nil
- log "User #{current_thread[:user]} disconnected."
- "221 Laterz"
+ def is_running?
+ @status != :dead
end
end
end
@@ -1,63 +0,0 @@
-require "../../spec_helper.rb"
-require 'net/ftp'
-
-describe FakeFtp::Server do
- subject { FakeFtp::Server }
-
- before :all do
- @directory = File.expand_path(__FILE__, "../../fixtures/destination")
- @text_filename = File.expand_path(__FILE__, "../../fixtures/text_file.txt")
- end
- before :each do
- FileUtils.rm_rf(@directory+"/*")
- end
-
- it "starts a server on port n" do
- server = FakeFtp::Server.new(21212)
- server.port.should == 21212
- ftp = Net::FTP.new
- proc { ftp.connect('127.0.0.1', 21212) }.should_not raise_error
- proc { ftp.connect('127.0.0.1', 21212) }.should_not raise_error(Net::FTPReplyError)
-
- server.stop
- end
-
- it "should default to port 21" do
- server = FakeFtp::Server.new
- server.port.should == 21
- end
-
- it "should authenticate" do
- server = FakeFtp::Server.new(21212)
- ftp = Net::FTP.new
-
- proc { ftp.connect('127.0.0.1', 21212) }.should_not raise_error
- proc { ftp.login('user', 'password') }.should_not raise_error(Net::FTPReplyError)
-
- server.stop
- end
-
- it "can be configured with a directory store" do
- server = FakeFtp::Server.new(21212)
- server.directory = @directory
- server.directory.should == @directory
- end
-
- context 'file puts' do
- before :each do
- @server = FakeFtp::Server.new(21216)
- end
-
- after :each do
- @server.stop
- end
-
- it "should accept a file" do
- ftp = Net::FTP.new
- ftp.connect('127.0.0.1', 21216)
- proc { ftp.login('user', 'password') }.should_not raise_error
- proc { ftp.put(@text_filename)}.should_not raise_error
- Dir.glob(@directory).should include(@text_filename)
- end
- end
-end
File renamed without changes.
@@ -0,0 +1,89 @@
+require "spec_helper.rb"
+require 'net/ftp'
+
+describe FakeFtp::Server do
+
+ before :each do
+ @directory = File.expand_path(__FILE__, "../../fixtures/destination")
+ @text_filename = File.expand_path(__FILE__, "../../fixtures/text_file.txt")
+ end
+
+ after :each do
+ FileUtils.rm_rf(@directory+"/*")
+ end
+
+ context '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 "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 "can be configured with a directory store" do
+ server = FakeFtp::Server.new
+ server.directory = @directory
+ server.directory.should == @directory
+ end
+
+ it "should clean up directory after itself"
+
+ it "should raise if attempting to delete a directory with contents other than its own"
+ end
+
+ context 'connection' do
+ before :each do
+ @server = FakeFtp::Server.new(21212)
+ @server.start
+ end
+
+ after :each do
+ @server.stop
+ end
+
+ it 'should accept ftp connections' do
+ ftp = Net::FTP.new
+ proc { ftp.connect('127.0.0.1', 21212) }.should_not raise_error
+ proc { ftp.close }.should_not raise_error
+ end
+
+ it "should allow anonymous authentication"
+
+ it "should allow named authentication"
+
+ it "should put files to directory store"
+
+ end
+
+# it "should authenticate" do
+# server = FakeFtp::Server.new(21212)
+# ftp = Net::FTP.new
+#
+# proc { ftp.connect('127.0.0.1', 21212) }.should_not raise_error
+# proc { ftp.login('user', 'password') }.should_not raise_error(Net::FTPReplyError)
+#
+# server.stop
+# end
+#
+# context 'file puts' do
+# it "should accept a file" do
+# ftp = Net::FTP.new
+# ftp.connect('127.0.0.1', 21216)
+# proc { ftp.login('user', 'password') }.should_not raise_error
+# proc { ftp.put(@text_filename)}.should_not raise_error
+# Dir.glob(@directory).should include(@text_filename)
+# end
+# end
+end
@@ -1,5 +1,6 @@
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH.unshift(File.dirname(__FILE__))
+$LOAD_PATH.unshift(File.dirname(__FILE__), '**','*')
require 'rubygems'
require 'bundler/setup'

0 comments on commit 80189f9

Please sign in to comment.