Skip to content

Commit 7e1a139

Browse files
committed
Added InputFile (closes #41).
1 parent 78f60da commit 7e1a139

File tree

3 files changed

+143
-0
lines changed

3 files changed

+143
-0
lines changed

lib/ronin/recon/input_file.rb

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# frozen_string_literal: true
2+
#
3+
# ronin-recon - A micro-framework and tool for performing reconnaissance.
4+
#
5+
# Copyright (c) 2023 Hal Brodigan ([email protected])
6+
#
7+
# ronin-recon is free software: you can redistribute it and/or modify
8+
# it under the terms of the GNU Lesser General Public License as published
9+
# by the Free Software Foundation, either version 3 of the License, or
10+
# (at your option) any later version.
11+
#
12+
# ronin-recon is distributed in the hope that it will be useful,
13+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
# GNU Lesser General Public License for more details.
16+
#
17+
# You should have received a copy of the GNU Lesser General Public License
18+
# along with ronin-recon. If not, see <https://www.gnu.org/licenses/>.
19+
#
20+
21+
require 'ronin/recon/value/parser'
22+
23+
module Ronin
24+
module Recon
25+
#
26+
# Represents an input file of values to recon.
27+
#
28+
class InputFile
29+
30+
# The path to the input file.
31+
#
32+
# @return [String]
33+
attr_reader :path
34+
35+
#
36+
# Initializes the input file.
37+
#
38+
# @param [String] path
39+
# The input file path.
40+
#
41+
def initialize(path)
42+
@path = path
43+
end
44+
45+
#
46+
# Opens the input file.
47+
#
48+
# @param [String] path
49+
# The input file path.
50+
#
51+
# @see #initialize
52+
#
53+
def self.open(path)
54+
new(path)
55+
end
56+
57+
#
58+
# Enumerates over every value in the input file.
59+
#
60+
# @yield [value]
61+
# If a block is given, it will be passed each parsed value.
62+
#
63+
# @yieldparam [Values::Domain, Values::Host, Values::IP, Values::IPRange, Values::Website] value
64+
# A parsed value from the input file.
65+
#
66+
# @return [Enumerator]
67+
# If no block is given, then an Enumerator will be returned.
68+
#
69+
def each
70+
return enum_for unless block_given?
71+
72+
File.open(@path) do |file|
73+
file.each_line(chomp: true) do |line|
74+
yield Value.parse(line)
75+
end
76+
end
77+
end
78+
79+
end
80+
end
81+
end

spec/fixtures/input_file.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
1.2.3.4
2+
1.2.3.4/24
3+
example.com
4+
www.example.com
5+
*.example.com
6+
https://example.com

spec/input_file_spec.rb

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
require 'spec_helper'
2+
require 'ronin/recon/input_file'
3+
4+
describe Ronin::Recon::InputFile do
5+
let(:fixtures_dir) { File.join(__dir__,'fixtures') }
6+
let(:path) { File.join(fixtures_dir,'input_file.txt') }
7+
8+
subject { described_class.new(path) }
9+
10+
describe "#initialize" do
11+
it "must set #path" do
12+
expect(subject.path).to eq(path)
13+
end
14+
end
15+
16+
describe ".open" do
17+
subject { described_class.open(path) }
18+
19+
it "must create a new InputFile with the given path" do
20+
expect(subject).to be_kind_of(described_class)
21+
expect(subject.path).to eq(path)
22+
end
23+
end
24+
25+
describe "#each" do
26+
context "when given a block" do
27+
it "must parse each line of the input file and yield each value" do
28+
expect { |b|
29+
subject.each(&b)
30+
}.to yield_successive_args(
31+
Ronin::Recon::Values::IP.new('1.2.3.4'),
32+
Ronin::Recon::Values::IPRange.new('1.2.3.4/24'),
33+
Ronin::Recon::Values::Domain.new('example.com'),
34+
Ronin::Recon::Values::Host.new('www.example.com'),
35+
Ronin::Recon::Values::Wildcard.new('*.example.com'),
36+
Ronin::Recon::Values::Website.parse('https://example.com')
37+
)
38+
end
39+
end
40+
41+
context "when no block is given" do
42+
it "must an Enumerator object for the method" do
43+
expect(subject.each.to_a).to eq(
44+
[
45+
Ronin::Recon::Values::IP.new('1.2.3.4'),
46+
Ronin::Recon::Values::IPRange.new('1.2.3.4/24'),
47+
Ronin::Recon::Values::Domain.new('example.com'),
48+
Ronin::Recon::Values::Host.new('www.example.com'),
49+
Ronin::Recon::Values::Wildcard.new('*.example.com'),
50+
Ronin::Recon::Values::Website.parse('https://example.com')
51+
]
52+
)
53+
end
54+
end
55+
end
56+
end

0 commit comments

Comments
 (0)