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 spec for Converters::JSON #18

Merged
merged 3 commits into from
Feb 6, 2024
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
150 changes: 150 additions & 0 deletions spec/converters/json_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
require 'spec_helper'
require 'ronin/masscan/converters/json'
require 'tempfile'

RSpec.describe Ronin::Masscan::Converters::JSON do
let(:fixtures_path) { File.expand_path(File.join(__dir__, '..', 'fixtures')) }
postmodern marked this conversation as resolved.
Show resolved Hide resolved
let(:masscan_path) { File.join(fixtures_path, 'converters', 'input.json') }
let(:masscan_file) { Masscan::OutputFile.new(masscan_path) }
let(:ip_addr) { IPAddr.new('93.184.216.34/32') }
let(:timestamp) { Time.at(1629960621) }
let(:expected_status) do
{
status: :open,
protocol: :tcp,
port: 80,
reason: [:syn, :ack],
ttl: 54,
ip: ip_addr,
timestamp: timestamp
}
end
let(:expected_banner) do
{
protocol: :tcp,
port: 80,
ip: ip_addr,
timestamp: timestamp,
app_protocol: :html_title,
payload: '404 - Not Found'
}
end

describe '.convert' do
let(:expected_result) { JSON.dump([expected_status, expected_banner]) }

it 'must convert masscan file to json and write it into an output' do
tempfile = Tempfile.new(['ronin-masscan', '.json'])

subject.convert(masscan_file, tempfile)

tempfile.rewind
json = tempfile.read
expect(json).to eq(expected_result)
end
end

describe '.masscan_file_to_json' do
let(:expected_result) { JSON.dump([expected_status, expected_banner]) }

it 'must convert masscan file to json and write it into an output' do
tempfile = Tempfile.new(['ronin-masscan', '.json'])

subject.convert(masscan_file, tempfile)

tempfile.rewind
json = tempfile.read
expect(json).to eq(expected_result)
end
end

describe '.masscan_file_as_json' do
let(:expected_result) { [expected_status, expected_banner] }

it 'must convert masscan file to json representation' do
expect(subject.masscan_file_as_json(masscan_file)).to eq(expected_result)
end
end

describe '.record_as_json' do
context 'for Masscan::Status' do
let(:record) do
Masscan::Status.new(
ip: ip_addr,
protocol: :tcp,
port: 80,
reason: [:syn, :ack],
status: :open,
timestamp: Time.at(1629960621),
ttl: 54
)
end

it 'must return status as json' do
expect(subject.record_as_json(record)).to eq(expected_status)
end
end

context 'for Masscan::Banner' do
let(:record) do
Masscan::Banner.new(
protocol: :tcp,
port: 80,
ip: ip_addr,
timestamp: timestamp,
app_protocol: :html_title,
payload: '404 - Not Found'
)
end

it 'must return banner as json' do
expect(subject.record_as_json(record)).to eq(expected_banner)
end
end

context 'for unknown record' do
let(:record) { 'unknown' }

it 'must raise a NotImplementedError' do
expect {
subject.record_as_json(record)
}.to raise_error(NotImplementedError, "unable to convert masscan record: #{record.inspect}")
end
end
end

describe '.status_as_json' do
let(:status) do
Masscan::Status.new(
ip: ip_addr,
protocol: :tcp,
port: 80,
reason: [:syn, :ack],
status: :open,
timestamp: Time.at(1629960621),
ttl: 54
)
end

it 'must convert Masscan::Status to a Hash' do
expect(subject.status_as_json(status)).to eq(expected_status)
end
end

describe '.banner_as_json' do
let(:banner) do
Masscan::Banner.new(
protocol: :tcp,
port: 80,
ip: ip_addr,
timestamp: timestamp,
app_protocol: :html_title,
payload: '404 - Not Found'
)
end

it 'must convert Masscan::Banner to a Hash' do
expect(subject.banner_as_json(banner)).to eq(expected_banner)
end
end
end
5 changes: 5 additions & 0 deletions spec/fixtures/converters/input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
{ "ip": "93.184.216.34", "timestamp": "1629960621", "ports": [ {"port": 80, "proto": "tcp", "status": "open", "reason": "syn-ack", "ttl": 54} ] }
,
{ "ip": "93.184.216.34", "timestamp": "1629960621", "ports": [ {"port": 80, "proto": "tcp", "service": {"name": "title", "banner": "404 - Not Found"} } ] }
]