Skip to content

Commit

Permalink
Improve Importer specs (issue #13).
Browse files Browse the repository at this point in the history
* Run the `destroy_all` method calls after every spec.
* Try to keep code within 80 columns.
* Test the classes and the record attributes of the imported
  records.
  • Loading branch information
postmodern committed Feb 2, 2024
1 parent e083f44 commit 12ae406
Showing 1 changed file with 111 additions and 44 deletions.
155 changes: 111 additions & 44 deletions spec/importer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
Ronin::DB.connect('sqlite3::memory:')
end

after(:all) do
after do
Ronin::DB::OpenPort.destroy_all
Ronin::DB::IPAddress.destroy_all
Ronin::DB::Port.destroy_all
Ronin::DB::IPAddress.destroy_all
end

describe '.import_file' do
it 'must import records from file' do
it 'must import the IP and open ports from file' do
yielded_values = []

subject.import_file(masscan_path) do |value|
Expand Down Expand Up @@ -51,7 +51,7 @@
end

context 'when a block is given' do
it 'must yield imported records' do
it 'must yield the imported records' do
yielded_values = []

subject.import(output_file) do |value|
Expand All @@ -70,7 +70,7 @@
end

context 'when no block is given' do
it 'must return imported records' do
it 'must return the imported records' do
imported_records = subject.import(output_file)

expect(imported_records.size).to eq(7)
Expand All @@ -86,37 +86,75 @@
end

describe '.import_status' do
let(:status1) { Masscan::Status.new(status: :open, protocol: :icmp, ip: ip_addr, port: 80, timestamp: Time.now) }
let(:status2) { Masscan::Status.new(status: :open, protocol: :tcp, ip: ip_addr, port: 80, timestamp: Time.now) }
let(:ip_addr) { IPAddr.new('1.2.3.4') }
let(:ip) { '1.2.3.4' }
let(:ip_addr) { IPAddr.new(ip) }
let(:port_number) { 80 }

let(:status1) do
Masscan::Status.new(
status: :open,
protocol: :icmp,
ip: ip_addr,
port: port_number,
timestamp: Time.now
)
end

let(:status2) do
Masscan::Status.new(
status: :open,
protocol: :tcp,
ip: ip_addr,
port: port_number,
timestamp: Time.now
)
end

context 'when status is :open' do
context 'when protocol is :icmp' do
let(:expected_ip_address) { Ronin::DB::IPAddress.find_or_create_by(address: '1.2.3.4', version: 4) }
context 'and when protocol is :icmp' do
let(:expected_ip_address) do
Ronin::DB::IPAddress.create(address: ip, version: 4)
end

it 'must return imported ip address' do
expect(subject.import_status(status1)).to eq(expected_ip_address)
it 'must return imported Ronin::DB::IPAddress' do
imported_ip_address = subject.import_status(status1)

expect(imported_ip_address).to be_a(Ronin::DB::IPAddress)
expect(imported_ip_address.address).to eq(ip)
end
end

context 'when protocol is not :icmp' do
let(:port) { Ronin::DB::Port.find_or_create_by(protocol: :tcp, number: 80) }
let(:ip_address) { Ronin::DB::IPAddress.find_or_create_by(address: '1.2.3.4', version: 4) }
let(:expected_open_port) { Ronin::DB::OpenPort.find_or_create_by(ip_address: ip_address, port: port) }
context 'and when protocol is not :icmp' do
it 'must return imported Ronin::DB::OpenPort' do
imported_open_port = subject.import_status(status2)

it 'must return imported open port' do
expect(subject.import_status(status2)).to eq(expected_open_port)
expect(imported_open_port).to be_a(Ronin::DB::OpenPort)
expect(imported_open_port.port).to be_a(Ronin::DB::Port)
expect(imported_open_port.port.number).to eq(port_number)
expect(imported_open_port.ip_address).to be_a(Ronin::DB::IPAddress)
expect(imported_open_port.ip_address.address).to eq(ip)
end
end
end
end

describe '.import_open_port_status' do
let(:ip_addr) { IPAddr.new('1.1.1.1') }
let(:status) { Masscan::Status.new(ip: ip_addr, port: 80, protocol: :tcp, timestamp: Time.now, status: :open) }
let(:ip) { '1.1.1.1' }
let(:ip_addr) { IPAddr.new(ip) }
let(:port_number) { 80 }

let(:status) do
Masscan::Status.new(
ip: ip_addr,
port: port_number,
protocol: :tcp,
timestamp: Time.now,
status: :open
)
end

context 'when block is given' do
it 'must yield imported records' do
it 'must yield the imported records' do
yielded_values = []

subject.import_open_port_status(status) do |value|
Expand All @@ -125,54 +163,88 @@

expect(yielded_values.size).to eq(3)
expect(yielded_values[0]).to be_a(Ronin::DB::IPAddress)
expect(yielded_values[0].address).to eq(ip)
expect(yielded_values[1]).to be_a(Ronin::DB::Port)
expect(yielded_values[1].number).to eq(port_number)
expect(yielded_values[2]).to be_a(Ronin::DB::OpenPort)
end
end

it 'must return open port' do
expect(subject.import_open_port_status(status).class).to be(Ronin::DB::OpenPort)
it 'must return the imported Ronin::DB::OpenPort' do
imported_open_port = subject.import_open_port_status(status)

expect(imported_open_port).to be_a(Ronin::DB::OpenPort)
expect(imported_open_port.port).to be_a(Ronin::DB::Port)
expect(imported_open_port.port.number).to eq(port_number)
expect(imported_open_port.ip_address).to be_a(Ronin::DB::IPAddress)
expect(imported_open_port.ip_address.address).to eq(ip)
end
end

describe '.import_ip_address' do
let(:ipv4_address) { IPAddr.new('192.168.1.1') }
let(:ipv6_address) { IPAddr.new('2001:0db8:85a3:0000:0000:8a2e:0370:7334') }
let(:ip) { '1.1.1.1' }
let(:ip_addr) { IPAddr.new(ip) }

it 'imports an IPv4 address' do
expect(subject.import_ip_address(ipv4_address).address).to eq(ipv4_address.to_s)
end
it 'must return the imported Ronin::DB::IPAddress' do
imported_ip_address = subject.import_ip_address(ip_addr)

it 'imports an IPv6 address' do
expect(subject.import_ip_address(ipv6_address).address).to eq(ipv6_address.to_s)
expect(imported_ip_address).to be_a(Ronin::DB::IPAddress)
expect(imported_ip_address.address).to eq(ip)
end

context 'when a block is given' do
it 'must yield the imported IP address' do
it 'must yield the imported Ronin::DB::IPAddress' do
yielded_ip_address = nil

subject.import_ip_address(ipv4_address) do |ip_address|
subject.import_ip_address(ip_addr) do |ip_address|
yielded_ip_address = ip_address
end

expect(yielded_ip_address.address).to eq(ipv4_address.to_s)
expect(yielded_ip_address.address).to eq(ip)
end
end

it 'must return the imported IP address' do
imported_ip_address = subject.import_ip_address(ipv4_address)
context "when given an IPv4 address" do
let(:ipv4) { '192.168.1.1' }
let(:ipv4_addr) { IPAddr.new(ipv4) }

expect(imported_ip_address).to be_a(Ronin::DB::IPAddress)
expect(imported_ip_address.address).to eq(ipv4_address.to_s)
it 'imports an IPv4 address and return a Ronin::DB::IPAddress record' do
imported_ip_address = subject.import_ip_address(ipv4_addr)

expect(imported_ip_address).to be_a(Ronin::DB::IPAddress)
expect(imported_ip_address.address).to eq(ipv4)
expect(imported_ip_address.version).to eq(4)
end
end

context "when given an IPv6 address" do
let(:ipv6) { '2001:db8:85a3::8a2e:370:7334' }
let(:ipv6_addr) { IPAddr.new(ipv6) }

it 'imports an IPv6 address and return a Ronin::DB::IPAddress record' do
imported_ip_address = subject.import_ip_address(ipv6_addr)

expect(imported_ip_address).to be_a(Ronin::DB::IPAddress)
expect(imported_ip_address.address).to eq(ipv6)
expect(imported_ip_address.version).to eq(6)
end
end
end

describe '.import_port' do
let(:port_number) { 80 }
let(:protocol) { :tcp }

it 'must return the imported Ronin::DB::Port' do
imported_port = subject.import_port(port_number,protocol)

expect(imported_port).to be_a(Ronin::DB::Port)
expect(imported_port.number).to eq(port_number)
expect(imported_port.protocol).to eq(protocol.to_s)
end

context 'when block if given' do
it 'must yeild imported port' do
it 'must yeild the imported Ronin::DB::Port' do
yielded_port = nil

subject.import_port(port_number,protocol) do |port|
Expand All @@ -181,13 +253,8 @@

expect(yielded_port).to be_kind_of(Ronin::DB::Port)
expect(yielded_port.number).to eq(port_number)
expect(yielded_port.protocol).to eq(protocol.to_s)
end
end

it 'must return imported port' do
imported_port = subject.import_port(port_number,protocol)
expect(imported_port).to be_kind_of(Ronin::DB::Port)
expect(imported_port.number).to eq(port_number)
end
end
end

0 comments on commit 12ae406

Please sign in to comment.