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/acpp/message_spec.rb
Views: 11788
1
# -*- coding:binary -*-
2
require 'spec_helper'
3
4
5
RSpec.describe Rex::Proto::ACPP::Message do
6
7
subject(:message) do
8
described_class.new
9
end
10
11
# retrieve_public.bin has the contents of a message type 20 (retrieve
12
# settings) message with a password of public. There is no payload.
13
let(:retrieve_public_bin) do
14
File.binread(File.join(File.dirname(__FILE__), 'retrieve_public.bin'))
15
end
16
17
let(:retrieve_public_message) do
18
message.password = 'public'
19
message.type = 20
20
message
21
end
22
23
describe '#==' do
24
it 'considers two different objects composed of equal parts equal' do
25
message2 = described_class.new
26
message2.password = 'public'
27
message2.type = 20
28
expect(message2).to eq(retrieve_public_message)
29
end
30
it 'considers two different objects composed of different parts unequal' do
31
message3 = described_class.new
32
message3.type = 1
33
message3.password = 'private'
34
expect(message3).not_to eq(retrieve_public_message)
35
end
36
end
37
38
describe '#to_s' do
39
it 'encodes properly' do
40
expect(retrieve_public_bin).to eq(retrieve_public_message.to_s)
41
end
42
end
43
44
describe '#decode' do
45
it 'fails to decode if the message is the wrong size' do
46
small = Rex::Text.rand_text_alpha(100)
47
large = Rex::Text.rand_text_alpha(200)
48
expect { described_class.decode(small) }.to raise_error(/size #{small.size}/i)
49
expect { described_class.decode(large) }.to raise_error(/size #{large.size}/i)
50
end
51
it 'fails to decode if the required header is incorrect' do
52
retrieve_public_bin[0,4] = 'blah'
53
expect { described_class.decode(retrieve_public_bin) }.to raise_error(/header/i)
54
end
55
it 'decodes properly when the required checksum is correct' do
56
expect(retrieve_public_message).to eq(described_class.decode(retrieve_public_bin))
57
expect(retrieve_public_message).to eq(described_class.decode(retrieve_public_bin, true))
58
end
59
it 'decodes properly when the non-required checksum is correct' do
60
expect(retrieve_public_message).to eq(described_class.decode(retrieve_public_bin, false))
61
end
62
it 'decodes properly when the message checksum is incorrect' do
63
retrieve_public_bin[7,4] = "\x01\x02\x03\x04"
64
expect { described_class.decode(retrieve_public_bin) }.to raise_error(/message checksum/i)
65
expect { described_class.decode(retrieve_public_bin, true) }.to raise_error(/message checksum/i)
66
expect(retrieve_public_message).to eq(described_class.decode(retrieve_public_bin, false))
67
end
68
end
69
70
describe '#successful?' do
71
it 'is successful when 0' do
72
message = described_class.new
73
message.status = 0
74
expect(message.successful?).to be true
75
end
76
it 'is successful when !0' do
77
message = described_class.new
78
message.status = 1
79
expect(message.successful?).to be false
80
end
81
end
82
end
83
84