Skip to content

Commit 8e1a0e0

Browse files
committed
Add spec
1 parent c197e52 commit 8e1a0e0

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

spec/lib/rex/parser/winscp_spec.rb

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
require 'rex/parser/winscp'
2+
3+
INI_SECURITY = "[Configuration\\Security]\nUseMasterPassword=1\nMasterPasswordVerifier=\n"
4+
5+
USERNAME = 'username'
6+
HOST = 'server.feralhosting.com'
7+
PASSWORD='A35C7659654B2AB83C292F392E323D31392F392E2A392E723A392E3D3034332F2835323B723F33312F383A2F383A3B2F3B3B3B'
8+
SAMPLE_INI = <<-END
9+
[Sessions\\[email protected]]
10+
HostName=#{HOST}
11+
Timeout=6000
12+
SshProt=3
13+
UserName=#{USERNAME}
14+
UpdateDirectories=0
15+
Utf=1
16+
Password=#{PASSWORD}
17+
Shell=/bin/bash}
18+
END
19+
20+
describe Rex::Parser::WinSCP do
21+
let(:target) do
22+
d = Class.new { include Rex::Parser::WinSCP }
23+
d.new
24+
end
25+
26+
context "#parse_protocol" do
27+
it "returns 'Unknown' for unknown protocols" do
28+
expect(target.parse_protocol(nil)).to eq('Unknown')
29+
expect(target.parse_protocol(99)).to eq('Unknown')
30+
expect(target.parse_protocol('stuff')).to eq('Unknown')
31+
end
32+
33+
it "returns 'SSH' for protocol 0" do
34+
expect(target.parse_protocol(0)).to eq('SSH')
35+
end
36+
37+
it "returns 'FTP' for protocol 5" do
38+
expect(target.parse_protocol(5)).to eq('FTP')
39+
end
40+
end
41+
42+
context "#decrypt_next_char" do
43+
it "returns 0 and the pwd if pwd length <= 0" do
44+
r, pwd = target.decrypt_next_char('')
45+
expect(r).to eq(0)
46+
expect(pwd).to eq('')
47+
end
48+
49+
it "strips the first two characters from the return value" do
50+
_, pwd = target.decrypt_next_char('A3')
51+
expect(pwd).to eq('')
52+
end
53+
54+
it "returns 255 for 'A3'" do
55+
r, _ = target.decrypt_next_char('A3')
56+
expect(r).to eq(Rex::Parser::WinSCP::PWDALG_SIMPLE_FLAG)
57+
end
58+
end
59+
60+
context "#decrypt_password" do
61+
it "returns 'sdfsdfgsggg' for the example password" do
62+
expect(target.decrypt_password(PASSWORD, "#{USERNAME}#{HOST}")).to eq('sdfsdfgsggg')
63+
end
64+
end
65+
66+
context "#parse_ini" do
67+
it "raises a RuntimeError if ini is nil or empty" do
68+
expect { target.parse_ini('') }.to raise_error(RuntimeError, /No data/i)
69+
expect { target.parse_ini(nil) }.to raise_error(RuntimeError, /No data/i)
70+
end
71+
72+
it "raises a RuntimeError if UseMasterPassword is 1" do
73+
expect { target.parse_ini(INI_SECURITY) }.to raise_error(RuntimeError, /Master/i)
74+
end
75+
76+
it "parses the example ini" do
77+
r = target.parse_ini(SAMPLE_INI).first
78+
expect(r[:hostname]).to eq(HOST)
79+
expect(r[:password]).to eq('sdfsdfgsggg')
80+
expect(r[:username]).to eq(USERNAME)
81+
expect(r[:protocol]).to eq('SSH')
82+
expect(r[:portnumber]).to eq(22)
83+
end
84+
end
85+
86+
context "#read_and_parse_ini" do
87+
it "returns nil if file is empty or doesn't exist" do
88+
File.stub(:read).and_return(nil)
89+
expect(target.read_and_parse_ini('blah')).to be nil
90+
end
91+
92+
it "parses the example ini and return a single result" do
93+
File.stub(:read).and_return(SAMPLE_INI)
94+
expect(target.read_and_parse_ini(SAMPLE_INI).count).to eq 1
95+
end
96+
end
97+
end
98+

0 commit comments

Comments
 (0)