CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/spec/rex/parser/arguments_spec.rb
Views: 11623
1
require 'rspec'
2
3
RSpec.describe Rex::Parser::Arguments do
4
let(:subject) do
5
Rex::Parser::Arguments.new(
6
['-h', '--help'] => [false, 'Help banner.'],
7
['-d', '--delete'] => [false, 'Delete'],
8
['-j', '--job'] => [false, 'Run in the context of a job.'],
9
'--long-flag-with-no-corresponding-short-option-name' => [false, 'A long flag with no corresponding short option name'],
10
['-o', '--options'] => [true, 'A comma separated list of options in VAR=VAL format.', '<option>'],
11
['-q', '--quiet'] => [false, 'Run the module in quiet mode with no output'],
12
['-S', '--save'] => [false, 'Save to disk'],
13
['-sd', '--set-default'] => [true, 'A comma separated list of defaults in VAR=VAL format.', '<option>']
14
)
15
end
16
17
describe '#parse' do
18
context 'when flags are provided' do
19
it 'parses a single flag correctly' do
20
input = ['-h']
21
expected_yields = [
22
['-h', 0, nil]
23
]
24
expect { |b| subject.parse(input, &b) }.to yield_successive_args(*expected_yields)
25
end
26
27
it 'parses multiple flags correctly' do
28
input = ['-h', '-h', '-h']
29
expected_yields = [
30
['-h', 0, nil],
31
['-h', 1, nil],
32
['-h', 2, nil],
33
]
34
expect { |b| subject.parse(input, &b) }.to yield_successive_args(*expected_yields)
35
end
36
37
it 'parses multiple flags combined correctly' do
38
input = ['-hq']
39
expected_yields = [
40
['-h', 0, nil],
41
['-q', 0, nil],
42
]
43
expect { |b| subject.parse(input, &b) }.to yield_successive_args(*expected_yields)
44
end
45
46
it 'ignores unknown flags' do
47
input = ['-a', 'action_name']
48
expected_yields = [
49
# '-a' is dropped, 'action_name' is used as an argument
50
[nil, 1, 'action_name'],
51
]
52
expect { |b| subject.parse(input, &b) }.to yield_successive_args(*expected_yields)
53
end
54
55
it 'treats combined flags that do not exist as an argument' do
56
input = ['-unknown-flags']
57
expected_yields = [
58
[nil, 0, '-unknown-flags']
59
]
60
expect { |b| subject.parse(input, &b) }.to yield_successive_args(*expected_yields)
61
end
62
end
63
64
it 'parses a single long flag correctly' do
65
input = ['--help']
66
expected_yields = [
67
['-h', 0, nil]
68
]
69
expect { |b| subject.parse(input, &b) }.to yield_successive_args(*expected_yields)
70
end
71
72
it 'parses multiple long flags correctly' do
73
input = ['--help', '--job']
74
expected_yields = [
75
['-h', 0, nil],
76
['-j', 1, nil]
77
]
78
expect { |b| subject.parse(input, &b) }.to yield_successive_args(*expected_yields)
79
end
80
81
it 'parses a long flag and short flag correctly' do
82
input = ['--help', '-h']
83
expected_yields = [
84
['-h', 0, nil],
85
['-h', 1, nil]
86
]
87
expect { |b| subject.parse(input, &b) }.to yield_successive_args(*expected_yields)
88
end
89
90
it 'parses a short flag when Rex Arguments are in an array correctly' do
91
input = ['-o']
92
expected_yields = [
93
['-o', 0, nil]
94
]
95
expect { |b| subject.parse(input, &b) }.to yield_successive_args(*expected_yields)
96
end
97
98
it 'parses a long flag when in arguments array correctly' do
99
input = ['--options', 'option-arg']
100
expected_yields = [
101
['-o', 0, 'option-arg']
102
]
103
expect { |b| subject.parse(input, &b) }.to yield_successive_args(*expected_yields)
104
end
105
106
it 'parses multiple long flags when in an arguments array correctly' do
107
input = ['--quiet', '--options', 'sample-option']
108
expected_yields = [
109
['-q', 0, nil],
110
['-o', 1, 'sample-option']
111
]
112
expect { |b| subject.parse(input, &b) }.to yield_successive_args(*expected_yields)
113
end
114
115
it 'parses a multi character flag correctly' do
116
input = ['-sd', 'options-arg']
117
expected_yields = [
118
['-sd', 0, 'options-arg']
119
]
120
expect { |b| subject.parse(input, &b) }.to yield_successive_args(*expected_yields)
121
end
122
123
it 'parses an aggregate flag with varied casing flag correctly' do
124
input = ['-Ssd', 'set-default-arg']
125
expected_yields = [
126
['-S', 0, nil],
127
['-sd', 0, 'set-default-arg']
128
]
129
expect { |b| subject.parse(input, &b) }.to yield_successive_args(*expected_yields)
130
end
131
132
it 'parses an aggregate flag with arguments array correctly' do
133
input = ['-Sosd', 'options-arg', 'set-default-arg']
134
expected_yields = [
135
['-S', 0, nil],
136
['-o', 0, 'options-arg'],
137
['-sd', 0, 'set-default-arg']
138
]
139
expect { |b| subject.parse(input, &b) }.to yield_successive_args(*expected_yields)
140
end
141
142
it 'parses an aggregate flag with arguments array and an additional long flag correctly' do
143
input = ['-So', 'options-arg', '--set-default', 'set-default-arg']
144
expected_yields = [
145
['-S', 0, nil],
146
['-o', 0, 'options-arg'],
147
['-sd', 2, 'set-default-arg']
148
]
149
expect { |b| subject.parse(input, &b) }.to yield_successive_args(*expected_yields)
150
end
151
152
it 'parses a non-existent long flag correctly' do
153
input = ['--ultra-quiet']
154
expected_yields = [
155
[nil, 0, '--ultra-quiet']
156
]
157
expect { |b| subject.parse(input, &b) }.to yield_successive_args(*expected_yields)
158
end
159
160
it 'parses a long flag that is not in an array correctly' do
161
input = ['--long-flag-with-no-corresponding-short-option-name']
162
expected_yields = [
163
['--long-flag-with-no-corresponding-short-option-name', 0, nil]
164
]
165
expect { |b| subject.parse(input, &b) }.to yield_successive_args(*expected_yields)
166
end
167
168
context 'when arguments are supplied' do
169
it 'treats an ip address as an argument' do
170
input = ['127.0.0.1']
171
expected_yields = [
172
[nil, 0, '127.0.0.1'],
173
]
174
expect { |b| subject.parse(input, &b) }.to yield_successive_args(*expected_yields)
175
end
176
177
it 'treats assignment as an argument' do
178
input = ['foo=bar']
179
expected_yields = [
180
[nil, 0, 'foo=bar'],
181
]
182
expect { |b| subject.parse(input, &b) }.to yield_successive_args(*expected_yields)
183
end
184
185
it 'treats urls as an argument' do
186
input = ['https://example.com:443/foo/bar?baz=qux&a=b']
187
expected_yields = [
188
[nil, 0, 'https://example.com:443/foo/bar?baz=qux&a=b'],
189
]
190
expect { |b| subject.parse(input, &b) }.to yield_successive_args(*expected_yields)
191
end
192
end
193
end
194
195
describe '#inspect' do
196
it 'prints usage in a sorted order correctly' do
197
expected_output = <<~EXPECTED
198
199
OPTIONS:
200
201
--long-flag-with-no-corresponding-short-option-name A long flag with no corresponding short option name
202
-d, --delete Delete
203
-h, --help Help banner.
204
-j, --job Run in the context of a job.
205
-o, --options <option> A comma separated list of options in VAR=VAL format.
206
-q, --quiet Run the module in quiet mode with no output
207
-S, --save Save to disk
208
-sd, --set-default <option> A comma separated list of defaults in VAR=VAL format.
209
EXPECTED
210
expect(subject.usage).to eq(expected_output)
211
end
212
end
213
end
214
215