Skip to content

Commit

Permalink
Add spec for Nmap.scan (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
AI-Mozi committed Apr 29, 2024
1 parent 9045c19 commit b397b43
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions spec/nmap_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
require 'spec_helper'
require 'ronin/nmap'

RSpec.describe Ronin::Nmap do
describe '.scan' do
let(:targets) { '192.168.1.*' }

context 'if tartets is empty' do
it 'must raise an ArgumentError' do
expect {
subject.scan
}.to raise_error(ArgumentError, 'must specify at least one target')
end
end

context 'if sudo is not a Hash, true, false, nor nil' do
it 'must raise an ArgumentError' do
expect {
subject.scan(targets, sudo: 'sudo')
}.to raise_error(ArgumentError, 'sudo keyword must be a Hash, true, false, or nil')
end
end

context 'when nmap command was successfull' do
let(:expected_output_filename) { %r{#{Ronin::Nmap::CACHE_DIR}\/nmap[^.]+\.xml} }

before do
allow(Kernel).to receive(:system).with({}, 'nmap', '-oX', match(expected_output_filename), targets).and_return(true)
end

it 'must return a Nmap::XML' do
expect(subject.scan(targets)).to be_kind_of(Nmap::XML)
end
end

context 'when nmap command fails' do
before do
allow(Kernel).to receive(:system).with({}, 'nmap', '-oX', anything, targets).and_return(false)
end

it 'must return false' do
expect(subject.scan(targets)).to be(false)
end
end

context 'when nmap command is not installed' do
before do
allow(Kernel).to receive(:system).with({}, 'nmap', '-oX', anything, targets).and_return(nil)
end

it 'must return nil' do
expect(subject.scan(targets)).to be(nil)
end
end
end
end

0 comments on commit b397b43

Please sign in to comment.