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/lib/rex/proto/pjl/client_spec.rb
Views: 11789
1
require 'spec_helper'
2
require 'msfenv'
3
4
RSpec.describe Rex::Proto::PJL::Client do
5
context "methods" do
6
let(:default_response) do
7
'OK'
8
end
9
10
let(:sock) do
11
s = double("sock")
12
allow(s).to receive(:put).with(an_instance_of(String))
13
allow(s).to receive(:get).and_return(default_response)
14
s
15
end
16
17
let(:cli) do
18
Rex::Proto::PJL::Client.new(sock)
19
end
20
21
context "#initialize" do
22
it "should initialize a 'sock' ivar" do
23
expect(cli.instance_variable_get(:@sock).class).to eq(RSpec::Mocks::Double)
24
end
25
end
26
27
context "#begin_job" do
28
it "should send a PJL start request without an error" do
29
expect { cli.begin_job }.to_not raise_error
30
end
31
end
32
33
context "#end_job" do
34
it "should send a PJL end request without an error" do
35
expect { cli.end_job }.to_not raise_error
36
end
37
end
38
39
context "#info" do
40
it "should raise an exception for not having a category" do
41
expect { cli.info(nil) }.to raise_error(ArgumentError)
42
end
43
44
it "should receive a response for an INFO request" do
45
expect(cli.info(:id)).to eq(default_response)
46
end
47
end
48
49
context "#info_id" do
50
it "should return the version information" do
51
fake_version = '"1337"'
52
allow(cli).to receive(:info).with(an_instance_of(Symbol)).and_return(fake_version)
53
expect(cli.info_id).to eq('1337')
54
end
55
end
56
57
context "#info_variables" do
58
it "should return the environment variables" do
59
fake_env_vars = "#{Rex::Proto::PJL::Info::VARIABLES}\r\nPASSWORD=DISABLED\f"
60
allow(cli).to receive(:info).with(an_instance_of(Symbol)).and_return(fake_env_vars)
61
expect(cli.info_variables).to eq('PASSWORD=DISABLED')
62
end
63
end
64
65
context "#info_filesys" do
66
it "should return the volumes" do
67
fake_volumes = "[1 TABLE]\r\nDIR\f"
68
allow(cli).to receive(:info).with(an_instance_of(Symbol)).and_return(fake_volumes)
69
expect(cli.info_filesys).to eq('DIR')
70
end
71
end
72
73
context "#get_rdymsg" do
74
it "should return a READY message" do
75
fake_ready_message = 'DISPLAY="RES"'
76
allow(cli).to receive(:info).with(an_instance_of(Symbol)).and_return(fake_ready_message)
77
expect(cli.get_rdymsg).to eq('RES')
78
end
79
end
80
81
context "#set_rdymsg" do
82
it "should send a READY message without an error" do
83
expect { cli.set_rdymsg("") }.to_not raise_error
84
end
85
end
86
87
context "#fsinit" do
88
it "should raise an exception due to an invalid volume" do
89
expect { cli.fsinit("BAD") }.to raise_error(ArgumentError)
90
end
91
92
it "should send a FS INIT message without an error" do
93
expect { cli.fsinit("1:") }.to_not raise_error
94
end
95
end
96
97
context "#fsquery" do
98
it "should raise an exception due to an invalid path" do
99
expect { cli.fsquery("BAD") }.to raise_error(ArgumentError)
100
end
101
102
it "should query a file" do
103
response = "TYPE=FILE SIZE=1337\r\n\f"
104
tmp_sock = double("sock")
105
allow(tmp_sock).to receive(:put).with(an_instance_of(String))
106
allow(tmp_sock).to receive(:get).with(Rex::Proto::PJL::DEFAULT_TIMEOUT).and_return(response)
107
tmp_cli = Rex::Proto::PJL::Client.new(tmp_sock)
108
expect(tmp_cli.fsquery("1:")).to eq(true)
109
end
110
end
111
112
context "#fsdirlist" do
113
it "should raise an exception due to an invalid path" do
114
expect { cli.fsdirlist("BAD") }.to raise_error(ArgumentError)
115
end
116
117
it "should return a LIST directory response" do
118
response = "ENTRY=1\r\nDIR\f"
119
tmp_sock = double("sock")
120
allow(tmp_sock).to receive(:put).with(an_instance_of(String))
121
allow(tmp_sock).to receive(:get).with(Rex::Proto::PJL::DEFAULT_TIMEOUT).and_return(response)
122
tmp_cli = Rex::Proto::PJL::Client.new(tmp_sock)
123
expect(tmp_cli.fsdirlist("1:")).to eq('DIR')
124
end
125
end
126
127
context "#fsupload" do
128
it "should raise an exception due to an invalid path" do
129
expect { cli.fsupload("BAD") }.to raise_error(ArgumentError)
130
end
131
132
it "should return a file" do
133
response = "SIZE=1337\r\nFILE\f"
134
tmp_sock = double("sock")
135
allow(tmp_sock).to receive(:put).with(an_instance_of(String))
136
allow(tmp_sock).to receive(:get).with(Rex::Proto::PJL::DEFAULT_TIMEOUT).and_return(response)
137
tmp_cli = Rex::Proto::PJL::Client.new(tmp_sock)
138
expect(tmp_cli.fsupload("1:")).to eq('FILE')
139
end
140
end
141
142
context "#fsdownload" do
143
it "should raise an exception due to an invalid path" do
144
expect { cli.fsdownload("/dev/null", "BAD") }.to raise_error(ArgumentError)
145
end
146
147
it "should upload a file" do
148
response = "TYPE=FILE SIZE=1337\r\n\f"
149
tmp_sock = double("sock")
150
allow(tmp_sock).to receive(:put).with(an_instance_of(String))
151
allow(tmp_sock).to receive(:get).with(Rex::Proto::PJL::DEFAULT_TIMEOUT).and_return(response)
152
tmp_cli = Rex::Proto::PJL::Client.new(tmp_sock)
153
expect(tmp_cli.fsdownload("/dev/null", "1:")).to eq(true)
154
end
155
156
it "should upload data from a string" do
157
response = "TYPE=FILE SIZE=1337\r\n\f"
158
tmp_sock = double("sock")
159
allow(tmp_sock).to receive(:put).with(an_instance_of(String))
160
allow(tmp_sock).to receive(:get).with(Rex::Proto::PJL::DEFAULT_TIMEOUT).and_return(response)
161
tmp_cli = Rex::Proto::PJL::Client.new(tmp_sock)
162
expect(tmp_cli.fsdownload("Miscellaneous Data", "1:root/.workspace/.garbage.", is_file: false)).to eq(true)
163
end
164
end
165
166
context "#fsdelete" do
167
it "should raise an exception due to an invalid path" do
168
expect { cli.fsdelete("BAD") }.to raise_error(ArgumentError)
169
end
170
171
it "should delete a file" do
172
response = "FILEERROR=3\r\n\f"
173
tmp_sock = double("sock")
174
allow(tmp_sock).to receive(:put).with(an_instance_of(String))
175
allow(tmp_sock).to receive(:get).with(Rex::Proto::PJL::DEFAULT_TIMEOUT).and_return(response)
176
tmp_cli = Rex::Proto::PJL::Client.new(tmp_sock)
177
expect(tmp_cli.fsdelete("1:")).to eq(true)
178
end
179
end
180
end
181
end
182
183