Skip to content
This repository has been archived by the owner on Jun 27, 2020. It is now read-only.

Commit

Permalink
Don't install gems in development and production groups on Travis.
Browse files Browse the repository at this point in the history
Cache gems on Travis.
Don't run Antivirus spec tests if Antivirus is not installed.
  • Loading branch information
dchandekstark committed May 22, 2014
1 parent 4a0b75c commit 92a88e0
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 62 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ rvm:
- 2.0.0
before_script:
- "cp config/log4r.yml.sample config/log4r.yml"
bundler_args: --without development production --deployment
cache: bundler
script: "bundle exec rake dul_hydra:ci:build"
notifications:
email:
Expand Down
5 changes: 4 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ gem 'grouper-rest-client'
gem 'hydra-editor', '0.2.2'
gem 'hydra-derivatives'
gem 'deprecation'
gem 'clamav'

group :production, :development do
gem 'clamav'
end

gem 'sqlite3'
gem 'log4r'
Expand Down
131 changes: 71 additions & 60 deletions lib/dul_hydra/services/antivirus.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require 'clamav'

module DulHydra
module Services
class Antivirus
Expand All @@ -8,79 +6,60 @@ class AntivirusError < DulHydra::Error; end
class VirusFoundError < AntivirusError; end
class AntivirusEngineError < AntivirusError; end

class ScanResult

attr_reader :raw, :file, :scanned_at, :version

def initialize(raw, file, opts={})
@raw = raw
@file = file
@scanned_at = opts.fetch(:scanned_at, DateTime.now)
@version = opts.fetch(:version, DulHydra::Services::Antivirus.version)
end

def virus_found
raw if raw.is_a? String
end

def has_virus?
!virus_found.nil?
end

def status
if has_virus?
"FOUND #{virus_found}"
else
"OK"
end
end

def error?
raw == 1
end

def to_s
"Virus scan: #{status} - #{file} (#{version})"
end

end

class << self

def init
engine.loaddb
end

def scan(file)
result = scan_one file
if result.has_virus?
raise VirusFoundError, result
end
raise VirusFoundError, result if result.has_virus?
raise AntivirusEngineError, result.version if result.error?
logger.info result
result
end

def scan_one(file)
load! unless loaded?
path = get_file_path file
raw_result = engine.scanfile path
ScanResult.new raw_result, path
end

def load!
raise DulHydra::Error, "Antivirus is not installed." unless installed?
load
end

def version
# ClamAV sigtool may be installed on system,
# but we require that clamav gem is installed.
return unless installed?
# Engine and database versions
# E.g., ClamAV 0.98.3/19010/Tue May 20 21:46:01 2014
`sigtool --version`.strip
end

def reload
# Reload virus database if changed
# 1 - reload successful
# 0 - reload unnecessary
engine.reload == 1
def installed?
@installed ||= begin
require 'clamav'
rescue LoadError
false
else
true
end
end

def scan_one(file)
path = get_file_path file
raw_result = engine.scanfile path
ScanResult.new raw_result, path
private

def loaded!
@loaded = true
end

private
def loaded?
!@loaded.nil?
end

def load
engine.loaddb
loaded!
end

def get_file_path(file)
path = if file.is_a? String
Expand All @@ -96,11 +75,43 @@ def get_file_path(file)
def engine
ClamAV.instance
end

end

class ScanResult
attr_reader :raw, :file, :scanned_at, :version

def initialize(raw, file, opts={})
@raw = raw
@file = file
@scanned_at = opts.fetch(:scanned_at, DateTime.now)
@version = opts.fetch(:version, DulHydra::Services::Antivirus.version)
end

def virus_found
raw if raw.is_a? String
end

def has_virus?
!virus_found.nil?
end

def status
if has_virus?
"FOUND #{virus_found}"
else
"OK"
end
end

def error?
raw == 1
end

def to_s
"Virus scan: #{status} - #{file} (#{version})"
end
end

end
end
end

DulHydra::Services::Antivirus.init
6 changes: 5 additions & 1 deletion spec/services/antivirus_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
require 'spec_helper'

describe DulHydra::Services::Antivirus do
def installed?
DulHydra::Services::Antivirus.installed?
end

describe DulHydra::Services::Antivirus, if: installed? do
let(:file) { fixture_file_upload "library-devil.tiff" }
it "should report whether a file is infected" do
expect(described_class.scan(file)).not_to have_virus
Expand Down

0 comments on commit 92a88e0

Please sign in to comment.