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/lib/rex/proto/thrift/thrift_array_spec.rb
Views: 1904
1
# -*- coding: binary -*-
2
require 'spec_helper'
3
require 'rex/text'
4
5
RSpec.describe Rex::Proto::Thrift::ThriftArray do
6
context 'when the data type is T_BOOLEAN' do
7
let(:data_type) { Rex::Proto::Thrift::ThriftDataType::T_BOOLEAN }
8
let(:value) { {
9
data_type: data_type,
10
members: [ true ]
11
} }
12
let(:binary_s) { [data_type, 1, 1].pack('CNC') }
13
14
describe '#to_binary_s' do
15
it 'should correctly encode' do
16
expect(described_class.new(value).to_binary_s).to eq binary_s
17
end
18
end
19
20
describe '.read' do
21
it 'should correctly decode' do
22
expect(described_class.read(binary_s).snapshot.keep_if { |k,_| value.key? k }).to eq value
23
end
24
end
25
end
26
27
context 'when the data type is T_I16' do
28
let(:data_type) { Rex::Proto::Thrift::ThriftDataType::T_I16 }
29
let(:number) { 0x7fff - rand(0xffff) }
30
let(:value) { {
31
data_type: data_type,
32
members: [ number ]
33
} }
34
let(:binary_s) { [data_type, 2, number].pack('CNs>') }
35
36
describe '#to_binary_s' do
37
it 'should correctly encode' do
38
expect(described_class.new(value).to_binary_s).to eq binary_s
39
end
40
end
41
42
describe '.read' do
43
it 'should correctly decode' do
44
expect(described_class.read(binary_s).snapshot.keep_if { |k,_| value.key? k }).to eq value
45
end
46
end
47
end
48
49
context 'when the data type is T_I32' do
50
let(:data_type) { Rex::Proto::Thrift::ThriftDataType::T_I32 }
51
let(:number) { 0x7fffffff - rand(0xffffffff) }
52
let(:value) { {
53
data_type: data_type,
54
members: [ number ]
55
} }
56
let(:binary_s) { [data_type, 4, number].pack('CNl>') }
57
58
describe '#to_binary_s' do
59
it 'should correctly encode' do
60
expect(described_class.new(value).to_binary_s).to eq binary_s
61
end
62
end
63
64
describe '.read' do
65
it 'should correctly decode' do
66
expect(described_class.read(binary_s).snapshot.keep_if { |k,_| value.key? k }).to eq value
67
end
68
end
69
end
70
71
context 'when the data type is T_I64' do
72
let(:data_type) { Rex::Proto::Thrift::ThriftDataType::T_I64 }
73
let(:number) { 0x7fffffffffffffff - rand(0xffffffffffffffff) }
74
let(:value) { {
75
data_type: data_type,
76
members: [ number ]
77
} }
78
let(:binary_s) { [data_type, 8, number].pack('CNq>') }
79
80
describe '#to_binary_s' do
81
it 'should correctly encode' do
82
expect(described_class.new(value).to_binary_s).to eq binary_s
83
end
84
end
85
86
describe '.read' do
87
it 'should correctly decode' do
88
expect(described_class.read(binary_s).snapshot.keep_if { |k,_| value.key? k }).to eq value
89
end
90
end
91
end
92
93
context 'when the data type is T_UTF7' do
94
let(:data_type) { Rex::Proto::Thrift::ThriftDataType::T_UTF7 }
95
let(:text) { Rex::Text.rand_text_alphanumeric(10) }
96
let(:value) { {
97
data_type: data_type,
98
members: [ text ]
99
} }
100
let(:binary_s) { [data_type, text.length + 4, text.length].pack('CNN') + text }
101
102
describe '#to_binary_s' do
103
it 'should correctly encode' do
104
expect(described_class.new(value).to_binary_s).to eq binary_s
105
end
106
end
107
108
describe '.read' do
109
it 'should correctly decode' do
110
expect(described_class.read(binary_s).snapshot.keep_if { |k,_| value.key? k }).to eq value
111
end
112
end
113
end
114
115
context 'when the data type is T_STRUCT' do
116
let(:data_type) { Rex::Proto::Thrift::ThriftDataType::T_STRUCT }
117
# use an empty struct
118
let(:object) { [ { data_type: Rex::Proto::Thrift::ThriftDataType::T_STOP } ] }
119
let(:value) { {
120
data_type: data_type,
121
members: [ object ]
122
} }
123
let(:binary_s) { [data_type, 1, 0].pack('CNC') }
124
125
describe '#to_binary_s' do
126
it 'should correctly encode' do
127
expect(described_class.new(value).to_binary_s).to eq binary_s
128
end
129
end
130
131
describe '.read' do
132
it 'should correctly decode' do
133
expect(described_class.read(binary_s).snapshot.keep_if { |k,_| value.key? k }).to eq value
134
end
135
end
136
end
137
138
context 'when the data type is T_SET' do
139
let(:data_type) { Rex::Proto::Thrift::ThriftDataType::T_SET }
140
# use an empty set
141
let(:object) { { data_type: Rex::Proto::Thrift::ThriftDataType::T_I16, members_size: 0 } }
142
let(:value) { {
143
data_type: data_type,
144
members: [ object ]
145
} }
146
let(:binary_s) { [data_type, 5, Rex::Proto::Thrift::ThriftDataType::T_I16, 0].pack('CNCN') }
147
148
describe '#to_binary_s' do
149
it 'should correctly encode' do
150
expect(described_class.new(value).to_binary_s).to eq binary_s
151
end
152
end
153
154
describe '.read' do
155
it 'should correctly decode' do
156
expect(described_class.read(binary_s).snapshot.keep_if { |k,_| value.key? k }).to eq value
157
end
158
end
159
end
160
161
context 'when the data type is T_LIST' do
162
let(:data_type) { Rex::Proto::Thrift::ThriftDataType::T_LIST }
163
# use an empty set
164
let(:object) { { data_type: Rex::Proto::Thrift::ThriftDataType::T_I16, members_size: 0 } }
165
let(:value) { {
166
data_type: data_type,
167
members: [ object ]
168
} }
169
let(:binary_s) { [data_type, 5, Rex::Proto::Thrift::ThriftDataType::T_I16, 0].pack('CNCN') }
170
171
describe '#to_binary_s' do
172
it 'should correctly encode' do
173
expect(described_class.new(value).to_binary_s).to eq binary_s
174
end
175
end
176
177
describe '.read' do
178
it 'should correctly decode' do
179
expect(described_class.read(binary_s).snapshot.keep_if { |k,_| value.key? k }).to eq value
180
end
181
end
182
end
183
end
184
185