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 ASN tests and pry. Update .gitignore. #192

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

cmhobbs
Copy link
Contributor

@cmhobbs cmhobbs commented May 5, 2024

No description provided.

@cmhobbs
Copy link
Contributor Author

cmhobbs commented May 5, 2024

Attempting to address #165 for ASN. This is my bleary-eyed nearly 3am commit. I'm sure a lot needs to be done to clean this up but I wanted to get it out there.

@cmhobbs
Copy link
Contributor Author

cmhobbs commented May 5, 2024

earlier tonight i noticed that i was calling the wrong methods in some of my tests due to copying and pasting a couple too many lines on little to no sleep. i think i've resolved most of those issues but it's something to look for.

@cmhobbs
Copy link
Contributor Author

cmhobbs commented May 5, 2024

Well, I guess I've managed to anger CI. I'll try to fix that later. Naturally, the tests all pass on my machine 😞 ...

@postmodern
Copy link
Member

postmodern commented May 5, 2024

@cmhobbs I'm going to guess the reason why the specs are failing is because they're running in a clean environment where the file hasn't even been downloaded yet, so .download is getting called instead of .update.

In the ronin-support specs we used stub_const() to ensure it was using the fixtures file:

      stub_const("#{described_class}::List::PATH",list_file)
      allow(described_class::List).to receive(:update)

Copy link
Member

@postmodern postmodern left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Massive review:

  • Move the example TSV data out into a fixutres/ file.
  • Use stub_const("Ronin::Support::Network::ASN::List::PATH",...) instead of options[:file] to force it to use the fixtures file.
  • For methods that print anything, we should test the output().
  • For methods that return or yield any records, we should test those as well.

2001:200:900::\t2001:200:9ff:ffff:ffff:ffff:ffff:ffff\t7660\tJP\tAPAN-JP Asia Pacific Advanced Network - Japan
2001:200:a00::\t2001:200:dff:ffff:ffff:ffff:ffff:ffff\t2500\tJP\tWIDE-BB WIDE Project
TSV
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably a good idea to move this out into a file in spec/cli/commands/fixtures/.


describe Ronin::CLI::Commands::Asn do
include_examples "man_page"

subject { described_class.new }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually the default value of subject.


it "calls #print_asn_record" do
expect(subject).to receive(:print_asn_record)
subject.run
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should actually test the output of the command here. It might be OK to let it call query_ip, which will perform a DNS query. Any tests that hit the network should also be tagged with :network.

end
end

# FIXME: (cmhobbs) the use of #exit has caused this to be difficult to test
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can do:

        expect {
          ...
        }.to raise_error(SystemExit) do |error|
          expect(error.status).to eq(1)
        end

describe '#download' do
context "with default parameters" do
let(:url) { 'https://iptoasn.com/data/ip2asn-combined.tsv.gz' }
let(:file) { "#{Dir.home}/.cache/ronin/ronin-support/ip2asn-combined.tsv.gz" }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably better to use Ronin::Support::Network::ASN::List::PATH here.

2001:200:a00::\t2001:200:dff:ffff:ffff:ffff:ffff:ffff\t2500\tJP\tWIDE-BB WIDE Project
TSV
end

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also maybe add a top-level before { stub_const("Ronin::Support::Network::ASN::List::PATH",asn_list_file) } at the top instead of setting options[:file] in all of the other specs.

ip2asn_file.unlink
end

describe '#run?' do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra ? got in there.

end
end

context "when options[:update] is true" do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mention "--update option" in the spec description.

record = subject.search_asn_records.first

expect { subject.print_asn_record(record) }.to output(
"223.255.233.0 - 223.255.233.255 AS140694 (AU) ARAFURA-AS-AP Northern Technology Solutions\n"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use #{$/} instead of \n, in case we ever want to run the specs on Windows; it's unlikely that we'll do this anytime soon, but generally is a good practice to use $/.

end

it "prints the record in verbose format" do
output(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing the expect() bit.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that got chopped out on accident when i was fixing my branch... good catch.

@cmhobbs
Copy link
Contributor Author

cmhobbs commented May 7, 2024

thanks for the thorough review! i'll get to work on edits soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants