Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add specs and .travis.yml #111

Merged
merged 6 commits into from
Feb 11, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
language: ruby
rvm:
- 1.9.3
- 2.0.0
- 2.1.0
- 2.2.0
script: bundle exec rspec --color --format=documentation
1 change: 1 addition & 0 deletions nexpose.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ Gem::Specification.new do |s|
s.add_runtime_dependency('rex', '~> 2.0.5', '>= 2.0.5')

s.add_development_dependency('bundler', '~> 1.3')
s.add_development_dependency('rspec', '~> 3.2')
end
32 changes: 32 additions & 0 deletions spec/nexpose/connection_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'spec_helper'

describe Nexpose::Connection do
describe '.from_uri' do
let(:username) { nil }
let(:password) { nil }
let(:silo_id) { nil }
subject(:connection) { Nexpose::Connection.from_uri(uri, username, password, silo_id) }

context 'with the default port' do
let(:uri) { 'https://nexpose.local:3780/' }

it 'initializes a new Connection' do
expect(connection.host).to eq('nexpose.local')
expect(connection.password).to eq(password)
expect(connection.port).to eq(3780)
expect(connection.username).to eq(username)
end
end

context 'with a non-default port' do
let(:uri) { 'https://nexpose.local:1234/' }

it 'initializes a new Connection' do
expect(connection.host).to eq('nexpose.local')
expect(connection.password).to eq(password)
expect(connection.port).to eq(1234)
expect(connection.username).to eq(username)
end
end
end
end
61 changes: 61 additions & 0 deletions spec/nexpose/site_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
require 'spec_helper'

describe Nexpose::Site do
let(:scan_template) { 'full-audit-without-webspider' }
let(:site_name) { 'joe blow site' }
subject { Nexpose::Site.new(site_name, scan_template) }

describe '#add_asset' do
context 'with a hostname' do
it 'adds a new HostName to the assets list' do
subject.add_asset('nexpose.local')

expect(subject.assets).to include(Nexpose::HostName.new('nexpose.local'))
end
end

context 'with a single IP address' do
it 'adds a new IPRange to the assets list' do
subject.add_asset('192.168.1.1')

expect(subject.assets).to include(Nexpose::IPRange.new('192.168.1.1'))
end
end

context 'with a multiple IP address' do
# TODO: The IPRange class apparently doesn't handle CIDR notation on the client side
xit 'adds a new IPRange to the assets list' do
subject.add_asset('192.168.1.0/24')

expect(subject.assets).to include(Nexpose::IPRange.new('192.168.1.0', '192.168.1.255'))
end
end
end

describe '#add_host' do
it 'adds a new HostName to the assets list' do
subject.add_asset('nexpose.local')

expect(subject.assets).to include(Nexpose::HostName.new('nexpose.local'))
end
end

describe '#add_ip' do
context 'with a single IP address' do
it 'adds a new IPRange to the assets list' do
subject.add_asset('192.168.1.1')

expect(subject.assets).to include(Nexpose::IPRange.new('192.168.1.1'))
end
end

context 'with a multiple IP address' do
# TODO: The IPRange class apparently doesn't handle CIDR notation on the client side
xit 'adds a new IPRange to the assets list' do
subject.add_asset('192.168.1.0/24')

expect(subject.assets).to include(Nexpose::IPRange.new('192.168.1.0', '192.168.1.255'))
end
end
end
end
18 changes: 18 additions & 0 deletions spec/nexpose/util/attributes_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'spec_helper'

describe Nexpose::Attributes do
subject { Nexpose::Attributes }

describe '.to_hash' do
it 'converts an array into a XML compatible format' do
attributes = [
{ awesome: true },
{ boring: false }
]
observed = subject.to_hash(attributes)

expect(observed).to include(a_hash_including('key' => 'awesome', 'value' => 'true'))
.and include(a_hash_including('key' => 'boring', 'value' => 'false'))
end
end
end
97 changes: 97 additions & 0 deletions spec/nexpose/util/host_or_ip_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
require 'spec_helper'

describe Nexpose::HostOrIP do
subject { Nexpose::HostOrIP }

describe '.convert' do
context 'with a fully qualified domain name' do
let(:asset) { asset = 'nexpose.local' }

it 'returns a HostName' do
observed = subject.convert(asset)
expect(observed).to be_a(Nexpose::HostName)
end
end

context 'with a hostname' do
let(:asset) { asset = 'target-host' }

it 'returns a HostName' do
observed = subject.convert(asset)
expect(observed).to be_a(Nexpose::HostName)
end
end

context 'with an IP address' do
let(:asset) { asset = '192.168.1.1' }


it 'returns an IPRange' do
observed = subject.convert(asset)
expect(observed).to be_a(Nexpose::IPRange)
end
end

context 'with an IP address range' do
let(:asset) { asset = '192.168.1.0/24' }


it 'returns an IPRange' do
observed = subject.convert(asset)
expect(observed).to be_a(Nexpose::IPRange)
end
end
end

# TODO: Since HostOrIP.parse deals with API responses consider adding
# integration tests for the parse method.
describe '.parse' do
let(:multiple_address_range_xml) { '<range from="192.168.1.1" to="192.168.1.254"/>' }
let(:single_address_range_xml) { '<range from="192.168.2.1"/>' }
let(:host_xml) { '<host>nexpose.local</host>' }
let(:xml_format) { '<GlobalSettings><ExcludedHosts>%{nested_xml}</ExcludedHosts></GlobalSettings>' }

context 'with a host element' do
let(:xml) { REXML::Document.new(format(xml_format, nested_xml: host_xml)) }

it 'returns a valid HostName object' do
observed = subject.parse(xml)
expect(observed).to include(Nexpose::HostName.new('nexpose.local'))
end
end

context 'with a single IP range element' do
let(:xml) { REXML::Document.new(format(xml_format, nested_xml: single_address_range_xml)) }

it 'returns a valid IPRange object' do
observed = subject.parse(xml)
expect(observed).to include(Nexpose::IPRange.new('192.168.2.1'))
end
end

context 'with a multiple IP range element' do
let(:xml) { REXML::Document.new(format(xml_format, nested_xml: multiple_address_range_xml)) }

it 'returns a valid IPRange object' do
observed = subject.parse(xml)
expect(observed).to include(Nexpose::IPRange.new('192.168.1.1', '192.168.1.254'))
end
end

context 'with host, IP address, and range elements' do
let(:xml) do
nodes = [host_xml, single_address_range_xml, multiple_address_range_xml]
REXML::Document.new(format(xml_format, nested_xml: nodes.join))
end

it 'returns valid HostName and IPRange objects' do
observed = subject.parse(xml)
expect(observed).to include(
Nexpose::IPRange.new('192.168.2.1'),
Nexpose::HostName.new('nexpose.local'),
Nexpose::IPRange.new('192.168.1.1', '192.168.1.254')
)
end
end
end
end
27 changes: 27 additions & 0 deletions spec/nexpose/util/iso_8601_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'spec_helper'

describe Nexpose::ISO8601 do
subject { Nexpose::ISO8601 }
let(:time_iso8601_string) { '20141210T165822.412Z' }
let(:time) {
# 412,000 (10^3 * 412) microseconds is equivalent to 412
# nanoseconds which is used in "time_iso8601_string".
microseconds = 412_000
seconds = Time.new(2014, 12, 10, 16, 58, 22, 0).to_i
Time.at(seconds, microseconds)
}

describe '.to_string' do
it 'converts a Time object into an ISO 8601 string' do
observed = subject.to_string(time)
expect(observed).to eq(time_iso8601_string)
end
end

describe '.to_time' do
it 'converts an ISO 8601 string into a Time object' do
observed = subject.to_time(time_iso8601_string)
expect(observed).to eq(time)
end
end
end
36 changes: 36 additions & 0 deletions spec/nexpose/util/sanititze_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require 'spec_helper'

describe Nexpose::Sanitize do
subject do
# Create a dummy class which has included the Sanitize module
dummy_class = Class.new { include Nexpose::Sanitize }
dummy_class.new
end

describe '#replace_entities' do
it 'replaces all instances of the ampersand character with &amp;' do
observed = subject.replace_entities('one & two & three')
expect(observed).to eq('one &amp; two &amp; three')
end

it 'replaces all instances of the double quote character with &quot;' do
observed = subject.replace_entities('Lorem "ipsum"')
expect(observed).to eq('Lorem &quot;ipsum&quot;')
end

it 'replaces all instances of the single quote character with &apos;' do
observed = subject.replace_entities("Lorem 'ipsum'")
expect(observed).to eq('Lorem &apos;ipsum&apos;')
end

it 'replaces all instances of the "greater than" character' do
observed = subject.replace_entities("n_bits >> m_bits")
expect(observed).to eq('n_bits &gt;&gt; m_bits')
end

it 'replaces all instances of the "less than" character' do
observed = subject.replace_entities("array << Time.now")
expect(observed).to eq('array &lt;&lt; Time.now')
end
end
end
34 changes: 34 additions & 0 deletions spec/nexpose/util/xml_utils_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'spec_helper'

describe Nexpose::XMLUtils do
describe '.success?' do
subject { Nexpose::XMLUtils }

context 'with a successful response' do
let(:response_string) { '<status success="1"/>' }

it 'returns true' do
observed = subject.success?(response_string)
expect(observed).to be(true)
end
end

context 'with a failed response' do
let(:response_string) { '<status success="0"/>' }

it 'returns false' do
observed = subject.success?(response_string)
expect(observed).to be(false)
end
end

context 'with a response that did not define "success"' do
let(:response_string) { '<status other-attr="ignored"/>' }

it 'returns false' do
observed = subject.success?(response_string)
expect(observed).to be(false)
end
end
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require 'nexpose'