<commit_before>require 'test_helper'

class TestLDAPConnection < Test::Unit::TestCase
  def test_unresponsive_host
    assert_raise Net::LDAP::LdapError do
      Net::LDAP::Connection.new(:server => 'test.mocked.com', :port => 636)
    end
  end

  def test_blocked_port
    flexmock(TCPSocket).should_receive(:new).and_raise(SocketError)
    assert_raise Net::LDAP::LdapError do
      Net::LDAP::Connection.new(:server => 'test.mocked.com', :port => 636)
    end
  end

  def test_raises_unknown_exceptions
    error = Class.new(StandardError)
    flexmock(TCPSocket).should_receive(:new).and_raise(error)
    assert_raise error do
      Net::LDAP::Connection.new(:server => 'test.mocked.com', :port => 636)
    end
  end

  def test_modify_ops_delete
    args = { :operations => [ [ :delete, "mail" ] ] }
    result = Net::LDAP::Connection.modify_ops(args[:operations])
    expected = [ "0\r\n\x01\x010\b\x04\x04mail1\x00" ]
    assert_equal(expected, result)
  end

  def test_modify_ops_add
    args = { :operations => [ [ :add, "mail", "<EMAIL>" ] ] }
    result = Net::LDAP::Connection.modify_ops(args[:operations])
    expected = [ "0#\n\x01\x000\x1E\x04\x04mail1\x16\x04\x14testuser@example.<EMAIL>" ]
    assert_equal(expected, result)
  end

  def test_modify_ops_replace
    args = { :operations =>[ [ :replace, "mail", "<EMAIL>" ] ] }
    result = Net::LDAP::Connection.modify_ops(args[:operations])
    expected = [ "0#\n\x01\x020\x1E\x04\x04mail1\x16\x04\x14test<EMAIL>" ]
    assert_equal(expected, result)
  end
end


class TestLDAPConnectionErrors < Test::Unit::TestCase
  def setup
    @tcp_socket = flexmock(:connection)
    @tcp_socket.should_receive(:write)
    flexmock(TCPSocket).should_receive(:new).and_return(@tcp_socket)
    @connection = Net::LDAP::Connection.new(:server => 'test.mocked.com', :port => 636)
  end

  def test_error_failed_operation
    ber = Net::BER::BerIdentifiedArray.new([53, "", "The provided password value was rejected by a password validator:  The provided password did not contain enough characters from the character set 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.  The minimum number of characters from that set that must be present in user passwords is 1"])
    ber.ber_identifier = Net::LDAP::PDU::ModifyResponse
    @tcp_socket.should_receive(:read_ber).and_return([2, ber])

    result = @connection.modify(:dn => "1", :operations => [[:replace, "mail", "<EMAIL>"]])
    assert_predicate result, :failure?
    assert_equal "The provided password value was rejected by a password validator:  The provided password did not contain enough characters from the character set 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.  The minimum number of characters from that set that must be present in user passwords is 1", result.error_message
  end

  def test_no_error_on_success
    ber = Net::BER::BerIdentifiedArray.new([0, "", ""])
    ber.ber_identifier = Net::LDAP::PDU::ModifyResponse
    @tcp_socket.should_receive(:read_ber).and_return([2, ber])

    result = @connection.modify(:dn => "1", :operations => [[:replace, "mail", "<EMAIL>"]])
    assert_predicate result, :success?
    assert_equal "", result.error_message
  end
end

class TestLDAPInstrumentation < Test::Unit::TestCase
  def setup
    @tcp_socket = flexmock(:connection)
    @tcp_socket.should_receive(:write)
    flexmock(TCPSocket).should_receive(:new).and_return(@tcp_socket)

    @service = MockInstrumentationService.new
    @connection = Net::LDAP::Connection.new \
      :server => 'test.mocked.com',
      :port => 636,
      :instrumentation_service => @service
  end

  def test_write_net_ldap_connection_event
    ber = Net::BER::BerIdentifiedArray.new([0, "", ""])
    ber.ber_identifier = Net::LDAP::PDU::BindResult
    read_result = [2, ber]
    @tcp_socket.should_receive(:read_ber).and_return(read_result)

    events = @service.subscribe "write.net_ldap_connection"

    result = @connection.bind(method: :anon)
    assert_predicate result, :success?

    # a write event
    payload, result = events.pop
    assert payload.has_key?(:result)
    assert payload.has_key?(:content_length)<commit_msg>Clarify connection instrumentation test class<commit_after>require 'test_helper'

class TestLDAPConnection < Test::Unit::TestCase
  def test_unresponsive_host
    assert_raise Net::LDAP::LdapError do
      Net::LDAP::Connection.new(:server => 'test.mocked.com', :port => 636)
    end
  end

  def test_blocked_port
    flexmock(TCPSocket).should_receive(:new).and_raise(SocketError)
    assert_raise Net::LDAP::LdapError do
      Net::LDAP::Connection.new(:server => 'test.mocked.com', :port => 636)
    end
  end

  def test_raises_unknown_exceptions
    error = Class.new(StandardError)
    flexmock(TCPSocket).should_receive(:new).and_raise(error)
    assert_raise error do
      Net::LDAP::Connection.new(:server => 'test.mocked.com', :port => 636)
    end
  end

  def test_modify_ops_delete
    args = { :operations => [ [ :delete, "mail" ] ] }
    result = Net::LDAP::Connection.modify_ops(args[:operations])
    expected = [ "0\r\n\x01\x010\b\x04\x04mail1\x00" ]
    assert_equal(expected, result)
  end

  def test_modify_ops_add
    args = { :operations => [ [ :add, "mail", "<EMAIL>" ] ] }
    result = Net::LDAP::Connection.modify_ops(args[:operations])
    expected = [ "0#\n\x01\x000\x1E\x04\x04mail1\x16\x04\x14<EMAIL>" ]
    assert_equal(expected, result)
  end

  def test_modify_ops_replace
    args = { :operations =>[ [ :replace, "mail", "<EMAIL>" ] ] }
    result = Net::LDAP::Connection.modify_ops(args[:operations])
    expected = [ "0#\n\x01\x020\x1E\x04\x04mail1\x16\x04\x14test<EMAIL>" ]
    assert_equal(expected, result)
  end
end


class TestLDAPConnectionErrors < Test::Unit::TestCase
  def setup
    @tcp_socket = flexmock(:connection)
    @tcp_socket.should_receive(:write)
    flexmock(TCPSocket).should_receive(:new).and_return(@tcp_socket)
    @connection = Net::LDAP::Connection.new(:server => 'test.mocked.com', :port => 636)
  end

  def test_error_failed_operation
    ber = Net::BER::BerIdentifiedArray.new([53, "", "The provided password value was rejected by a password validator:  The provided password did not contain enough characters from the character set 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.  The minimum number of characters from that set that must be present in user passwords is 1"])
    ber.ber_identifier = Net::LDAP::PDU::ModifyResponse
    @tcp_socket.should_receive(:read_ber).and_return([2, ber])

    result = @connection.modify(:dn => "1", :operations => [[:replace, "mail", "<EMAIL>"]])
    assert_predicate result, :failure?
    assert_equal "The provided password value was rejected by a password validator:  The provided password did not contain enough characters from the character set 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.  The minimum number of characters from that set that must be present in user passwords is 1", result.error_message
  end

  def test_no_error_on_success
    ber = Net::BER::BerIdentifiedArray.new([0, "", ""])
    ber.ber_identifier = Net::LDAP::PDU::ModifyResponse
    @tcp_socket.should_receive(:read_ber).and_return([2, ber])

    result = @connection.modify(:dn => "1", :operations => [[:replace, "mail", "<EMAIL>"]])
    assert_predicate result, :success?
    assert_equal "", result.error_message
  end
end

class TestLDAPConnectionInstrumentation < Test::Unit::TestCase
  def setup
    @tcp_socket = flexmock(:connection)
    @tcp_socket.should_receive(:write)
    flexmock(TCPSocket).should_receive(:new).and_return(@tcp_socket)

    @service = MockInstrumentationService.new
    @connection = Net::LDAP::Connection.new \
      :server => 'test.mocked.com',
      :port => 636,
      :instrumentation_service => @service
  end

  def test_write_net_ldap_connection_event
    ber = Net::BER::BerIdentifiedArray.new([0, "", ""])
    ber.ber_identifier = Net::LDAP::PDU::BindResult
    read_result = [2, ber]
    @tcp_socket.should_receive(:read_ber).and_return(read_result)

    events = @service.subscribe "write.net_ldap_connection"

    result = @connection.bind(method: :anon)
    assert_predicate result, :success?

    # a write event
    payload, result = events.pop
    assert payload.has_key?(:result)
    assert payload.has_key?(:content_length)
{'fraction_non_alphanumeric': 0.1322655017594952, 'fraction_numerical': 0.018201674554058973, 'mean_word_length': 4.531543624161074, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2734761', 'n_tokens_mistral': 2809, 'n_tokens_neox': 2680, 'n_words': 545}