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/kademlia/message_spec.rb
Views: 11789
1
# -*- coding: binary -*-
2
require 'spec_helper'
3
4
RSpec.describe Rex::Proto::Kademlia::Message do
5
6
context 'with a body' do
7
let(:type) { 1 }
8
let(:body) { 'test' }
9
let(:data) { "\xE4\x01test" }
10
11
subject(:message) do
12
described_class.new(type, body)
13
end
14
15
describe '#initialize' do
16
it 'constructs properly' do
17
expect(message.type).to eq(type)
18
expect(message.body).to eq(body)
19
end
20
end
21
22
describe '#to_str' do
23
it 'packs properly' do
24
expect(message.to_str).to eq(data)
25
end
26
end
27
28
describe '#from_data' do
29
it 'unpacks supported messages properly' do
30
unpacked = described_class.from_data(data)
31
expect(unpacked.type).to eq(type)
32
expect(unpacked.body).to eq(body)
33
end
34
35
it 'raises on compressed messages' do
36
expect do
37
described_class.from_data("\xE5\x01test")
38
end.to raise_error(NotImplementedError)
39
end
40
end
41
42
describe '#==' do
43
it 'respects equality' do
44
expect(described_class.new(1, 'test')).to eq(described_class.new(1, 'test'))
45
expect(described_class.new(1, 'test')).not_to eq(described_class.new(1, 'not'))
46
expect(described_class.new(1, 'test')).not_to eq(described_class.new(2, 'test'))
47
expect(described_class.new(1, 'test')).not_to eq(described_class.new(2, 'not'))
48
end
49
end
50
end
51
52
context 'without a body' do
53
let(:type) { 2 }
54
let(:body) { '' }
55
let(:data) { "\xE4\x02" }
56
57
subject(:message) do
58
described_class.new(type, body)
59
end
60
61
describe '#initialize' do
62
it 'constructs properly' do
63
expect(message.type).to eq(type)
64
expect(message.body).to eq(body)
65
end
66
end
67
68
describe '#to_str' do
69
it 'packs properly' do
70
expect(message.to_str).to eq(data)
71
end
72
end
73
74
describe '#from_data' do
75
it 'unpacks supported messages properly' do
76
unpacked = described_class.from_data(data)
77
expect(unpacked.type).to eq(type)
78
expect(unpacked.body).to eq(body)
79
end
80
81
it 'raises on compressed messages' do
82
expect do
83
described_class.from_data("\xE5\x01")
84
end.to raise_error(NotImplementedError)
85
end
86
end
87
end
88
end
89
90