Skip to content

Commit 1eb7eb5

Browse files
authored
add WordlistDir specs (#12)
1 parent d232876 commit 1eb7eb5

File tree

2 files changed

+143
-1
lines changed

2 files changed

+143
-1
lines changed

lib/ronin/wordlists/wordlist_dir.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ def delete(name)
185185

186186
return path
187187
else
188-
raise(ArgumentError,"unknown wordlist: #{name.inspect}")
188+
raise(WordlistNotFound,"unknown wordlist: #{name.inspect}")
189189
end
190190
end
191191

spec/wordlist_dir_spec.rb

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
require 'spec_helper'
2+
require 'ronin/wordlists/wordlist_dir'
3+
require 'ronin/wordlists/exceptions'
4+
5+
describe Ronin::Wordlists::WordlistDir do
6+
subject { described_class.new(path) }
7+
8+
let(:fixtures_dir) { File.expand_path(File.join(__dir__, '..', 'spec', 'fixtures')) }
9+
10+
let(:path) { File.join(fixtures_dir, 'wordlists') }
11+
12+
describe "#initialize" do
13+
it "must initialize #path" do
14+
expect(subject.path).to eq(path)
15+
end
16+
end
17+
18+
describe "#each" do
19+
context "when no block is given" do
20+
it "must return an Enumerator" do
21+
expect(subject.each).to be_kind_of(Enumerator)
22+
end
23+
end
24+
25+
context "when block is given" do
26+
let(:wordlists_paths) { Dir["#{subject.path}/*"] }
27+
28+
it "must must yield all wordlists" do
29+
yielded_values = []
30+
31+
subject.each do |value|
32+
yielded_values << value
33+
end
34+
35+
expect(yielded_values).to eq(wordlists_paths)
36+
end
37+
end
38+
end
39+
40+
describe "#find" do
41+
let(:example_wrodlist_path) { File.join(path, "example_wordlist.txt") }
42+
43+
context "if an exact file exist" do
44+
it "must return its path" do
45+
expect(subject.find("example_wordlist.txt")).to eq(example_wrodlist_path)
46+
end
47+
end
48+
49+
context "if an exact file does not exist" do
50+
it "must search for the wordlist file by name" do
51+
expect(subject.find("example_wordlist")).to eq(example_wrodlist_path)
52+
end
53+
end
54+
end
55+
56+
describe "#list" do
57+
let(:all_wordlists) { Dir.children(path) }
58+
59+
context "if argument is not passed" do
60+
it "must list all wordlists in the wordlist directories" do
61+
expect(subject.list).to match_array(all_wordlists)
62+
end
63+
end
64+
65+
context "if argument is passed" do
66+
it "and file is found, it must return an array of wordlists filenames" do
67+
expect(subject.list("foo_wordlist")).to eq(["foo_wordlist.txt"])
68+
end
69+
70+
it "but file is not found, it must return an empty array" do
71+
expect(subject.list("invalid_wordlist")).to eq([])
72+
end
73+
end
74+
end
75+
76+
describe "#open" do
77+
context "when the wordlist file exists within the directory" do
78+
let(:example_wrodlist_path) { File.join(path, "example_wordlist.txt") }
79+
80+
it "must call .open on Wordlist with path" do
81+
wordlist = subject.open("example_wordlist")
82+
83+
expect(wordlist).to be_kind_of(Wordlist::File)
84+
expect(wordlist.path).to eq(example_wrodlist_path)
85+
end
86+
end
87+
88+
context "when the wordlist file cannot be found within the directory" do
89+
let(:filename) { "foo_bar_baz" }
90+
91+
it "must raise a WordlistNotFound error" do
92+
expect {
93+
subject.open(filename)
94+
}.to raise_error(Ronin::Wordlists::WordlistNotFound, "wordlist not found: #{filename.inspect}")
95+
end
96+
end
97+
end
98+
99+
describe "#download" do
100+
context "when given a .git URL" do
101+
let(:url) { "https://github.com/Escape-Technologies/graphql-wordlist.git" }
102+
103+
it "must download wordlist via WordlistRepo" do
104+
expect(Ronin::Wordlists::WordlistRepo).to receive(:download).with(url, subject.path)
105+
106+
subject.download(url)
107+
end
108+
end
109+
110+
context "when given a non-git URL" do
111+
let(:url) { "https://raw.githubusercontent.com/rbsec/dnscan/master/tlds.txt" }
112+
113+
it "must download wordlist via WordlistFile" do
114+
expect(Ronin::Wordlists::WordlistFile).to receive(:download).with(url, subject.path)
115+
116+
subject.download(url)
117+
end
118+
end
119+
end
120+
121+
describe "#delete" do
122+
context "when the wordlist file exists within the directory" do
123+
let(:example_wrodlist_path) { File.join(path, "example_wordlist.txt") }
124+
125+
it "must delete wordlist file" do
126+
expect(File).to receive(:unlink).with(example_wrodlist_path)
127+
128+
subject.delete("example_wordlist")
129+
end
130+
end
131+
132+
context "when the wordlist file does not exist within the directory" do
133+
let(:name) { "invalid_name" }
134+
135+
it "must raise an error" do
136+
expect {
137+
subject.delete(name)
138+
}.to raise_error(Ronin::Wordlists::WordlistNotFound, "unknown wordlist: #{name.inspect}")
139+
end
140+
end
141+
end
142+
end

0 commit comments

Comments
 (0)