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_data_spec.rb
Views: 11789
# -*- coding: binary -*-1require 'spec_helper'2require 'rex/text'34RSpec.describe Rex::Proto::Thrift::ThriftData do5subject(:instance) { described_class.new }67it { should respond_to :data_type }8it { should respond_to :field_id }9it { should respond_to :data_value }1011it 'is big endian' do12expect(described_class.fields.instance_variable_get(:@hints)[:endian]).to eq :big13end1415it 'tracks the data type in a ThriftDataType field' do16expect(instance.data_type).to be_a Rex::Proto::Thrift::ThriftDataType17end1819it 'tracks the field ID in a Uint16 field' do20expect(instance.field_id).to be_a BinData::Uint16be21end2223it 'tracks the data value in a Choice field' do24expect(instance.data_value).to be_a BinData::Choice25end2627it 'sets the data type correctly by default' do28expect(instance.data_type).to eq Rex::Proto::Thrift::ThriftDataType::T_STOP29end3031context 'when the data type is T_STOP' do32let(:data_type) { Rex::Proto::Thrift::ThriftDataType::T_STOP }33let(:value) { {34data_type: data_type35} }36let(:binary_s) { [data_type].pack('C') }3738describe '#to_binary_s' do39it 'should correctly encode' do40expect(described_class.new(value).to_binary_s).to eq binary_s41end42end4344describe '.read' do45it 'should correctly decode' do46expect(described_class.read(binary_s).snapshot.keep_if { |k,_| value.key? k }).to eq value47end48end49end5051context 'when the data type is T_BOOLEAN' do52let(:data_type) { Rex::Proto::Thrift::ThriftDataType::T_BOOLEAN }53let(:field_id) { rand(0xffff) }54let(:value) { {55data_type: data_type,56field_id: field_id,57data_value: true58} }59let(:binary_s) { [data_type, field_id, 1].pack('CnC') }6061describe '#to_binary_s' do62it 'should correctly encode' do63expect(described_class.new(value).to_binary_s).to eq binary_s64end65end6667describe '.read' do68it 'should correctly decode' do69expect(described_class.read(binary_s).snapshot.keep_if { |k,_| value.key? k }).to eq value70end71end72end7374context 'when the data type is T_I16' do75let(:data_type) { Rex::Proto::Thrift::ThriftDataType::T_I16 }76let(:field_id) { rand(0xffff) }77let(:number) { 0x7fff - rand(0xffff) }78let(:value) { {79data_type: data_type,80field_id: field_id,81data_value: number82} }83let(:binary_s) { [data_type, field_id, number].pack('Cns>') }8485describe '#to_binary_s' do86it 'should correctly encode' do87expect(described_class.new(value).to_binary_s).to eq binary_s88end89end9091describe '.read' do92it 'should correctly decode' do93expect(described_class.read(binary_s).snapshot.keep_if { |k,_| value.key? k }).to eq value94end95end96end9798context 'when the data type is T_I32' do99let(:data_type) { Rex::Proto::Thrift::ThriftDataType::T_I32 }100let(:field_id) { rand(0xffff) }101let(:number) { 0x7fffffff - rand(0xffffffff) }102let(:value) { {103data_type: data_type,104field_id: field_id,105data_value: number106} }107let(:binary_s) { [data_type, field_id, number].pack('Cnl>') }108109describe '#to_binary_s' do110it 'should correctly encode' do111expect(described_class.new(value).to_binary_s).to eq binary_s112end113end114115describe '.read' do116it 'should correctly decode' do117expect(described_class.read(binary_s).snapshot.keep_if { |k,_| value.key? k }).to eq value118end119end120end121122context 'when the data type is T_I64' do123let(:data_type) { Rex::Proto::Thrift::ThriftDataType::T_I64 }124let(:field_id) { rand(0xffff) }125let(:number) { 0x7fffffffffffffff - rand(0xffffffffffffffff) }126let(:value) { {127data_type: data_type,128field_id: field_id,129data_value: number130} }131let(:binary_s) { [data_type, field_id, number].pack('Cnq>') }132133describe '#to_binary_s' do134it 'should correctly encode' do135expect(described_class.new(value).to_binary_s).to eq binary_s136end137end138139describe '.read' do140it 'should correctly decode' do141expect(described_class.read(binary_s).snapshot.keep_if { |k,_| value.key? k }).to eq value142end143end144end145146context 'when the data type is T_UTF7' do147let(:data_type) { Rex::Proto::Thrift::ThriftDataType::T_UTF7 }148let(:field_id) { rand(0xffff) }149let(:text) { Rex::Text.rand_text_alphanumeric(10) }150let(:value) { {151data_type: data_type,152field_id: field_id,153data_value: text154} }155let(:binary_s) { [data_type, field_id, text.length].pack('CnN') + text }156157describe '#to_binary_s' do158it 'should correctly encode' do159expect(described_class.new(value).to_binary_s).to eq binary_s160end161end162163describe '.read' do164it 'should correctly decode' do165expect(described_class.read(binary_s).snapshot.keep_if { |k,_| value.key? k }).to eq value166end167end168end169170context 'when the data type is T_STRUCT' do171let(:data_type) { Rex::Proto::Thrift::ThriftDataType::T_STRUCT }172let(:field_id) { rand(0xffff) }173let(:object) { [ { data_type: Rex::Proto::Thrift::ThriftDataType::T_STOP } ] }174let(:value) { {175data_type: data_type,176field_id: field_id,177data_value: object178} }179let(:binary_s) { [data_type, field_id, 0].pack('CnC') }180181describe '#to_binary_s' do182it 'should correctly encode' do183expect(described_class.new(value).to_binary_s).to eq binary_s184end185end186187describe '.read' do188it 'should correctly decode' do189expect(described_class.read(binary_s).snapshot.keep_if { |k,_| value.key? k }).to eq value190end191end192end193194context 'when the data type is T_SET' do195let(:data_type) { Rex::Proto::Thrift::ThriftDataType::T_SET }196let(:field_id) { rand(0xffff) }197let(:object) { { data_type: Rex::Proto::Thrift::ThriftDataType::T_I16, members_size: 0 } }198let(:value) { {199data_type: data_type,200field_id: field_id,201data_value: object202} }203let(:binary_s) { [data_type, field_id, Rex::Proto::Thrift::ThriftDataType::T_I16, 0].pack('CnCN') }204205describe '#to_binary_s' do206it 'should correctly encode' do207expect(described_class.new(value).to_binary_s).to eq binary_s208end209end210211describe '.read' do212it 'should correctly decode' do213expect(described_class.read(binary_s).snapshot.keep_if { |k,_| value.key? k }).to eq value214end215end216end217218context 'when the data type is T_LIST' do219let(:data_type) { Rex::Proto::Thrift::ThriftDataType::T_LIST }220let(:field_id) { rand(0xffff) }221let(:object) { { data_type: Rex::Proto::Thrift::ThriftDataType::T_I16, members_size: 0 } }222let(:value) { {223data_type: data_type,224field_id: field_id,225data_value: object226} }227let(:binary_s) { [data_type, field_id, Rex::Proto::Thrift::ThriftDataType::T_I16, 0].pack('CnCN') }228229describe '#to_binary_s' do230it 'should correctly encode' do231expect(described_class.new(value).to_binary_s).to eq binary_s232end233end234235describe '.read' do236it 'should correctly decode' do237expect(described_class.read(binary_s).snapshot.keep_if { |k,_| value.key? k }).to eq value238end239end240end241end242243244