<commit_before># This code is free software; you can redistribute it and/or modify it under
# the terms of the new BSD License.
#
# Copyright (c) 2011, <NAME>

require 'helper'
require 'steam/sockets/rcon_socket'

class TestRCONSocket < Test::Unit::TestCase

  context 'A new RCON socket' do

    should 'know its IP and port, but not open a connection' do
      socket = RCONSocket.new '127.0.0.1', 27015

      assert_equal IPAddr.new('127.0.0.1'), socket.instance_variable_get(:@ip)
      assert_equal 27015, socket.instance_variable_get(:@port)
      assert_nil socket.instance_variable_get(:@socket)
    end

  end

  context 'A disconnected RCON socket' do

    setup do
      @socket = RCONSocket.new '127.0.0.1', 27015
    end

    should 'establish the TCP connection when sending' do
      tcp_socket = mock
      tcp_socket.expects(:send).with 'test', 0
      @socket.expects(:connect).with do
        @socket.instance_variable_set :@socket, tcp_socket
      end

      @socket.send mock(:to_s => 'test')
    end

    should 're-establish the TCP connection when sending' do
      tcp_socket = mock
      tcp_socket.expects(:closed?).returns true
      tcp_socket.expects(:send).with 'test', 0
      @socket.expects :connect
      @socket.instance_variable_set :@socket, tcp_socket

      packet = mock
      packet.expects(:to_s).returns 'test'
      @socket.send packet
    end

    should 'be able to establish the TCP connection' do
      tcp_socket = mock
      TCPSocket.expects(:new).with('127.0.0.1', 27015).
        returns tcp_socket

      @socket.connect

      assert_equal tcp_socket, @socket.instance_variable_get(:@socket)
    end

    should 'raise a timeout if the connection cannot be established' do
      @socket.expects(:timeout).raises Timeout::Error

      assert_raises SteamCondenser::TimeoutError do
        @socket.connect
      end
    end

  end

  context 'A connected RCON socket' do

    setup do
      @socket = RCONSocket.new '127.0.0.1', 27015
      @tcp_socket = mock
      @socket.instance_variable_set :@socket, @tcp_socket
    end

    should 'be able to close the TCP connection' do
      @tcp_socket.expects :close

      @socket.close
    end

    should 'receive responses using the TCP connection' do
      buffer = mock
      @socket.instance_variable_set :@buffer, buffer

      buffer.expects(:long).returns 1234
      buffer.expects(:get).twice.returns('test ').returns 'test'
      @socket.expects(:receive_packet).with(4).returns 1
      @socket.expects(:receive_packet).with(1234).returns 1000
      @socket.expects(:receive_packet).with(234).returns 234
      RCONPacketFactory.expects(:packet_from_data).with 'test test'

      @socket.reply
    end

    should 'raise an error if the client has been banned' do<commit_msg>Added test for reset RCON connection<commit_after># This code is free software; you can redistribute it and/or modify it under
# the terms of the new BSD License.
#
# Copyright (c) 2011, <NAME>

require 'helper'
require 'steam/sockets/rcon_socket'

class TestRCONSocket < Test::Unit::TestCase

  context 'A new RCON socket' do

    should 'know its IP and port, but not open a connection' do
      socket = RCONSocket.new '127.0.0.1', 27015

      assert_equal IPAddr.new('127.0.0.1'), socket.instance_variable_get(:@ip)
      assert_equal 27015, socket.instance_variable_get(:@port)
      assert_nil socket.instance_variable_get(:@socket)
    end

  end

  context 'A disconnected RCON socket' do

    setup do
      @socket = RCONSocket.new '127.0.0.1', 27015
    end

    should 'establish the TCP connection when sending' do
      tcp_socket = mock
      tcp_socket.expects(:send).with 'test', 0
      @socket.expects(:connect).with do
        @socket.instance_variable_set :@socket, tcp_socket
      end

      @socket.send mock(:to_s => 'test')
    end

    should 're-establish the TCP connection when sending' do
      tcp_socket = mock
      tcp_socket.expects(:closed?).returns true
      tcp_socket.expects(:send).with 'test', 0
      @socket.expects :connect
      @socket.instance_variable_set :@socket, tcp_socket

      packet = mock
      packet.expects(:to_s).returns 'test'
      @socket.send packet
    end

    should 'be able to establish the TCP connection' do
      tcp_socket = mock
      TCPSocket.expects(:new).with('127.0.0.1', 27015).
        returns tcp_socket

      @socket.connect

      assert_equal tcp_socket, @socket.instance_variable_get(:@socket)
    end

    should 'raise a timeout if the connection cannot be established' do
      @socket.expects(:timeout).raises Timeout::Error

      assert_raises SteamCondenser::TimeoutError do
        @socket.connect
      end
    end

  end

  context 'A connected RCON socket' do

    setup do
      @socket = RCONSocket.new '127.0.0.1', 27015
      @tcp_socket = mock
      @socket.instance_variable_set :@socket, @tcp_socket
    end

    should 'be able to close the TCP connection' do
      @tcp_socket.expects :close

      @socket.close
    end

    should 'receive responses using the TCP connection' do
      buffer = mock
      @socket.instance_variable_set :@buffer, buffer

      buffer.expects(:long).returns 1234
      buffer.expects(:get).twice.returns('test ').returns 'test'
      @socket.expects(:receive_packet).with(4).returns 1
      @socket.expects(:receive_packet).with(1234).returns 1000
      @socket.expects(:receive_packet).with(234).returns 234
      RCONPacketFactory.expects(:packet_from_data).with 'test test'

      @socket.reply
    end

    should 'raise an RCONBanError if the client has been banned' do
      @socket.expects(:receive_packet).with(4).raises Errno::ECONNRESET

      assert_raise RCONBanError do
        assert_nil @socket.reply
      end
    end

    should 'raise an RCONNoAuthError if the connection has been dropped' do
{'fraction_non_alphanumeric': 0.09568640876412188, 'fraction_numerical': 0.027901403628894213, 'mean_word_length': 3.0241046831955924, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 10, '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': '7645046', 'n_tokens_mistral': 1912, 'n_tokens_neox': 1741, 'n_words': 502}