Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/spec/lib/rex/proto/thrift/thrift_array_spec.rb
Views: 11788
# -*- coding: binary -*-1require 'spec_helper'2require 'rex/text'34RSpec.describe Rex::Proto::Thrift::ThriftArray do5context 'when the data type is T_BOOLEAN' do6let(:data_type) { Rex::Proto::Thrift::ThriftDataType::T_BOOLEAN }7let(:value) { {8data_type: data_type,9members: [ true ]10} }11let(:binary_s) { [data_type, 1, 1].pack('CNC') }1213describe '#to_binary_s' do14it 'should correctly encode' do15expect(described_class.new(value).to_binary_s).to eq binary_s16end17end1819describe '.read' do20it 'should correctly decode' do21expect(described_class.read(binary_s).snapshot.keep_if { |k,_| value.key? k }).to eq value22end23end24end2526context 'when the data type is T_I16' do27let(:data_type) { Rex::Proto::Thrift::ThriftDataType::T_I16 }28let(:number) { 0x7fff - rand(0xffff) }29let(:value) { {30data_type: data_type,31members: [ number ]32} }33let(:binary_s) { [data_type, 2, number].pack('CNs>') }3435describe '#to_binary_s' do36it 'should correctly encode' do37expect(described_class.new(value).to_binary_s).to eq binary_s38end39end4041describe '.read' do42it 'should correctly decode' do43expect(described_class.read(binary_s).snapshot.keep_if { |k,_| value.key? k }).to eq value44end45end46end4748context 'when the data type is T_I32' do49let(:data_type) { Rex::Proto::Thrift::ThriftDataType::T_I32 }50let(:number) { 0x7fffffff - rand(0xffffffff) }51let(:value) { {52data_type: data_type,53members: [ number ]54} }55let(:binary_s) { [data_type, 4, number].pack('CNl>') }5657describe '#to_binary_s' do58it 'should correctly encode' do59expect(described_class.new(value).to_binary_s).to eq binary_s60end61end6263describe '.read' do64it 'should correctly decode' do65expect(described_class.read(binary_s).snapshot.keep_if { |k,_| value.key? k }).to eq value66end67end68end6970context 'when the data type is T_I64' do71let(:data_type) { Rex::Proto::Thrift::ThriftDataType::T_I64 }72let(:number) { 0x7fffffffffffffff - rand(0xffffffffffffffff) }73let(:value) { {74data_type: data_type,75members: [ number ]76} }77let(:binary_s) { [data_type, 8, number].pack('CNq>') }7879describe '#to_binary_s' do80it 'should correctly encode' do81expect(described_class.new(value).to_binary_s).to eq binary_s82end83end8485describe '.read' do86it 'should correctly decode' do87expect(described_class.read(binary_s).snapshot.keep_if { |k,_| value.key? k }).to eq value88end89end90end9192context 'when the data type is T_UTF7' do93let(:data_type) { Rex::Proto::Thrift::ThriftDataType::T_UTF7 }94let(:text) { Rex::Text.rand_text_alphanumeric(10) }95let(:value) { {96data_type: data_type,97members: [ text ]98} }99let(:binary_s) { [data_type, text.length + 4, text.length].pack('CNN') + text }100101describe '#to_binary_s' do102it 'should correctly encode' do103expect(described_class.new(value).to_binary_s).to eq binary_s104end105end106107describe '.read' do108it 'should correctly decode' do109expect(described_class.read(binary_s).snapshot.keep_if { |k,_| value.key? k }).to eq value110end111end112end113114context 'when the data type is T_STRUCT' do115let(:data_type) { Rex::Proto::Thrift::ThriftDataType::T_STRUCT }116# use an empty struct117let(:object) { [ { data_type: Rex::Proto::Thrift::ThriftDataType::T_STOP } ] }118let(:value) { {119data_type: data_type,120members: [ object ]121} }122let(:binary_s) { [data_type, 1, 0].pack('CNC') }123124describe '#to_binary_s' do125it 'should correctly encode' do126expect(described_class.new(value).to_binary_s).to eq binary_s127end128end129130describe '.read' do131it 'should correctly decode' do132expect(described_class.read(binary_s).snapshot.keep_if { |k,_| value.key? k }).to eq value133end134end135end136137context 'when the data type is T_SET' do138let(:data_type) { Rex::Proto::Thrift::ThriftDataType::T_SET }139# use an empty set140let(:object) { { data_type: Rex::Proto::Thrift::ThriftDataType::T_I16, members_size: 0 } }141let(:value) { {142data_type: data_type,143members: [ object ]144} }145let(:binary_s) { [data_type, 5, Rex::Proto::Thrift::ThriftDataType::T_I16, 0].pack('CNCN') }146147describe '#to_binary_s' do148it 'should correctly encode' do149expect(described_class.new(value).to_binary_s).to eq binary_s150end151end152153describe '.read' do154it 'should correctly decode' do155expect(described_class.read(binary_s).snapshot.keep_if { |k,_| value.key? k }).to eq value156end157end158end159160context 'when the data type is T_LIST' do161let(:data_type) { Rex::Proto::Thrift::ThriftDataType::T_LIST }162# use an empty set163let(:object) { { data_type: Rex::Proto::Thrift::ThriftDataType::T_I16, members_size: 0 } }164let(:value) { {165data_type: data_type,166members: [ object ]167} }168let(:binary_s) { [data_type, 5, Rex::Proto::Thrift::ThriftDataType::T_I16, 0].pack('CNCN') }169170describe '#to_binary_s' do171it 'should correctly encode' do172expect(described_class.new(value).to_binary_s).to eq binary_s173end174end175176describe '.read' do177it 'should correctly decode' do178expect(described_class.read(binary_s).snapshot.keep_if { |k,_| value.key? k }).to eq value179end180end181end182end183184185