CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/spec/tools/jsobfu_spec.rb
Views: 1904
1
require 'spec_helper'
2
3
load Metasploit::Framework.root.join('tools/exploit/jsobfu.rb').to_path
4
5
require 'stringio'
6
7
RSpec.describe Jsobfu do
8
9
let(:fname) {
10
'test.js'
11
}
12
13
let(:js) {
14
%Q|alert("test");|
15
}
16
17
describe Jsobfu::Driver do
18
19
subject do
20
Jsobfu::Driver.new
21
end
22
23
describe '#run' do
24
let(:default_opts) {
25
{ :input => fname, :iteration => 1 }
26
}
27
28
before(:example) do
29
allow(Jsobfu::OptsConsole).to receive(:parse).with(any_args).and_return(default_opts)
30
allow(File).to receive(:open).with(fname, 'rb').and_yield(StringIO.new(js))
31
@out = $stdout
32
$stdout = StringIO.new
33
$stdout.string = ''
34
end
35
36
after(:example) do
37
$stdout = @out
38
end
39
40
context 'when a javascript file is given' do
41
it 'returns an String' do
42
subject.run
43
expect($stdout.string).to be_a(String)
44
end
45
46
it 'returns a non empty String' do
47
subject.run
48
expect($stdout.string).not_to be_empty
49
end
50
51
it 'returns an String different than the original' do
52
subject.run
53
expect($stdout.string).not_to eq(js)
54
end
55
end
56
end
57
end
58
59
60
describe Jsobfu::OptsConsole do
61
subject do
62
Jsobfu::OptsConsole
63
end
64
65
context 'when no options are given' do
66
it 'raises OptionParser::MissingArgument' do
67
expect{subject.parse([])}.to raise_error(OptionParser::MissingArgument)
68
end
69
end
70
71
context 'when -t isn\'t a number' do
72
it 'raises OptionParser::MissingArgument' do
73
args = "-i #{fname} -t NaN".split
74
expect{subject.parse(args)}.to raise_error(OptionParser::InvalidOption)
75
end
76
end
77
end
78
79
end
80
81