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/ldap/server_spec.rb
Views: 11788
# frozen_string_literal: true1require 'rex/text'23RSpec.describe Rex::Proto::LDAP::Server do45subject(:ldif) { nil }67subject(:auth_provider) do8Rex::Proto::LDAP::Auth.new(nil, nil, nil, nil, nil)9end1011subject(:server) do12described_class.new('0.0.0.0', 40000, true, true, ldif, nil, auth_provider)13end1415let(:response) {}1617let(:tcp_server_socket) do18double :tcp_server_socket,19start: nil,20:on_client_connect_proc= => nil,21:on_client_data_proc= => nil,22closed?: false,23close: nil24end2526let(:udp_server_socket) do27double :udp_server_socket,28start: nil,29:on_client_connect_proc= => nil,30:on_client_data_proc= => nil,31closed?: false,32close: nil33end3435let(:udp_monitor_thread) do36instance_double ::Thread, alive?: true37end3839before do40allow(Rex::Socket::TcpServer).to receive(:create).and_return(tcp_server_socket)41allow(Rex::Socket::Udp).to receive(:create).and_return(udp_server_socket)42allow(Rex::ThreadFactory).to receive(:spawn).with('UDPLDAPServerListener', false).and_return(udp_monitor_thread)43server.processed_pdu_handler(Net::LDAP::PDU::BindRequest) do |processed_data|44processed_data = 'Processed Data'45end46end4748context 'initialize' do49it 'sets the server options correctly' do50expect(server.serve_udp).to eq(true)51expect(server.serve_tcp).to eq(true)52expect(server.sock_options).to include('LocalHost' => '0.0.0.0', 'LocalPort' => 40000, 'Comm' => nil)53expect(server.ldif).to eq(ldif)54expect(server.instance_variable_get(:@auth_provider)).to eq(auth_provider)55expect(server.instance_variable_get(:@auth_provider)).to be_a(Rex::Proto::LDAP::Auth)56end57end5859describe '#running?' do60context 'when the server is not running' do61it 'returns false' do62expect(server.running?).to be_nil63end64end6566context 'when the server is running' do67before { server.start }6869it 'returns true' do70expect(server.running?).not_to be_nil71end7273after { server.stop }74end75end7677describe '#start' do78context 'start server with the provided options' do79before { server.start }8081it 'starts the UDP server if serve_udp is true' do82if server.serve_udp83expect(server.udp_sock).to be udp_server_socket84expect(server.running?).to be true85end86end8788it 'starts the TCP server if serve_tcp is true' do89if server.serve_tcp90expect(server.tcp_sock).to be tcp_server_socket91expect(server.running?).to be true92end93end9495after { server.stop }96end97end9899describe '#stop' do100before { server.start }101102it 'stops the server when running' do103server.stop104expect(server.running?).to be nil105end106end107108describe '#dispatch_request' do109it 'calls dispatch_request_proc if it is set' do110client = double('client')111allow(client).to receive(:peerhost) { '1.1.1.1' }112allow(client).to receive(:peerport) { '389' }113allow(client).to receive(:write).with(response)114allow(client).to receive(:close)115116block_called = false117server.dispatch_request_proc = proc { block_called = true }118server.dispatch_request(client, 'LDAP request data')119expect(block_called).to be true120end121122it 'calls default_dispatch_request if dispatch_request_proc is not set' do123client = double('client')124allow(client).to receive(:peerhost) { '1.1.1.1' }125allow(client).to receive(:peerport) { '389' }126allow(client).to receive(:write).with(any_args)127allow(client).to receive(:close)128129expect { server.dispatch_request(client, String.new("02\x02\x01\x01`-\x02\x01\x03\x04\"cn=user,dc=example,dc=com\x80\x04kali").force_encoding('ASCII-8BIT')) }.not_to raise_error130end131end132133describe '#default_dispatch_request' do134it 'returns nil for empty request data' do135client = double('client')136allow(client).to receive(:peerhost) { '1.1.1.1' }137allow(client).to receive(:peerport) { '389' }138allow(client).to receive(:write).with(any_args)139allow(client).to receive(:close)140data = ''141expect { server.default_dispatch_request(client, data) }.not_to raise_error142end143end144145describe '#encode_ldap_response' do146it 'encodes an LDAP response correctly' do147msgid = 1148code = Net::LDAP::ResultCodeSuccess149dn = ''150msg = Net::LDAP::ResultStrings[Net::LDAP::ResultCodeSuccess]151tag = Net::LDAP::PDU::BindResult152context_data = nil153context_code = nil154155response = server.encode_ldap_response(msgid, code, dn, msg, tag, context_data, context_code)156expect(response).to be_a(String)157end158end159160describe '#search_result' do161context 'when searching with no LDIF data' do162it 'returns a random search result' do163result = server.search_result(nil, 1)164165expect(result).to be_nil166end167end168end169170describe '#processed_pdu_handler' do171it 'sets the processed_pdu_handler correctly' do172173expect(server.instance_variable_get(:@pdu_process)[Net::LDAP::PDU::BindRequest]).to be_a(Proc)174expect((server.instance_variable_get(:@pdu_process)[Net::LDAP::PDU::BindRequest]).call({})).to eq('Processed Data')175end176end177178describe '#suitable_response' do179it 'returns the appropriate response type for a given request type' do180expect(server.suitable_response(Net::LDAP::PDU::BindRequest)).to eq(Net::LDAP::PDU::BindResult)181expect(server.suitable_response(Net::LDAP::PDU::SearchRequest)).to eq(Net::LDAP::PDU::SearchResult)182end183end184end185186187