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/client_spec.rb
Views: 1904
1
# -*- coding: binary -*-
2
require 'spec_helper'
3
require 'rex/text'
4
5
RSpec.describe Rex::Proto::Thrift::Client do
6
let(:target_host) { '127.0.0.1' }
7
let(:target_port) { 1234 }
8
subject(:instance) { described_class.new(target_host, target_port) }
9
10
it { should respond_to :host }
11
it { should respond_to :port }
12
it { should respond_to :ssl }
13
it { should respond_to :timeout }
14
15
it 'should default SSL to false' do
16
expect(instance.ssl).to eq false
17
end
18
19
describe '#call' do
20
let(:method_name) { Rex::Text.rand_text_alphanumeric(10) }
21
22
it 'calls the function and returns the result' do
23
allow(Rex::Proto::Thrift::ThriftHeader).to receive(:new).and_call_original
24
expect(subject).to receive(:send_raw).with("\x80\x01\x00\x01\x00\x00\x00\n#{method_name}\x00\x00\x00\x00\x00".b).and_return(nil)
25
expect(subject).to receive(:recv_raw).with(timeout: subject.timeout).and_return("\x80\x01\x00\x02\x00\x00\x00\n#{method_name}\x00\x00\x00\x00\x00".b)
26
result = instance.call(method_name, { data_type: Rex::Proto::Thrift::ThriftDataType::T_STOP })
27
expect(Rex::Proto::Thrift::ThriftHeader).to have_received(:new).with(method_name: method_name, message_type: Rex::Proto::Thrift::ThriftMessageType::CALL)
28
expect(result).to be_a Array
29
expect(result[0]).to eq({ data_type: Rex::Proto::Thrift::ThriftDataType::T_STOP })
30
end
31
32
it 'raises UnexpectedReplyError on an unexpected message type' do
33
expect(subject).to receive(:send_raw).with("\x80\x01\x00\x01\x00\x00\x00\n#{method_name}\x00\x00\x00\x00\x00".b).and_return(nil)
34
expect(subject).to receive(:recv_raw).with(timeout: subject.timeout).and_return("\x80\x01\x00\x01\x00\x00\x00\n#{method_name}\x00\x00\x00\x00\x00".b)
35
expect {
36
instance.call(method_name, { data_type: Rex::Proto::Thrift::ThriftDataType::T_STOP })
37
}.to raise_error(Rex::Proto::Thrift::Error::UnexpectedReplyError)
38
end
39
40
it 'raises UnexpectedReplyError on an unexpected method name' do
41
expect(subject).to receive(:send_raw).with("\x80\x01\x00\x01\x00\x00\x00\n#{method_name}\x00\x00\x00\x00\x00".b).and_return(nil)
42
expect(subject).to receive(:recv_raw).with(timeout: subject.timeout).and_return("\x80\x01\x00\x02\x00\x00\x00\n#{method_name.swapcase}\x00\x00\x00\x00\x00".b)
43
expect {
44
instance.call(method_name, { data_type: Rex::Proto::Thrift::ThriftDataType::T_STOP })
45
}.to raise_error(Rex::Proto::Thrift::Error::UnexpectedReplyError)
46
end
47
end
48
49
describe '#connect' do
50
it 'creates a rex socket' do
51
expect(Rex::Socket::Tcp).to receive(:create)
52
instance.connect
53
end
54
end
55
end
56
57