From 12ae406671e0ea164b72a257ebe4226175f055d4 Mon Sep 17 00:00:00 2001 From: Postmodern Date: Thu, 1 Feb 2024 16:35:34 -0800 Subject: [PATCH] Improve `Importer` specs (issue #13). * 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. --- spec/importer_spec.rb | 155 ++++++++++++++++++++++++++++++------------ 1 file changed, 111 insertions(+), 44 deletions(-) diff --git a/spec/importer_spec.rb b/spec/importer_spec.rb index 257ff18..7e85054 100644 --- a/spec/importer_spec.rb +++ b/spec/importer_spec.rb @@ -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| @@ -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| @@ -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) @@ -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| @@ -125,45 +163,71 @@ 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 @@ -171,8 +235,16 @@ 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| @@ -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