コード例 #1
0
    def test_receive_SetLength_Valid(self):
        data = bytearray([1, 2, 3, 4, 5, 6, 7, 8])

        with mock.patch(
                'uhppote_rfid.controller_socket.socket') as mock_socket:
            mockSocket = ControllerSocket('127.0.0.1')
            mockSocket.socket.recv.return_value = data

            mockSocket.connect()
            self.assertEquals(mockSocket.receive(len(data)), data)
コード例 #2
0
    def setUp(self):
        """
        .. function:: setUp()

           Runs a server locally on port 60000 to listen for connections to respond accordingly.
        """
        self.server = socket.socket()
        self.server.bind(('127.0.0.1', 60000))
        self.server.listen(1)

        self.socket = ControllerSocket('127.0.0.1')
コード例 #3
0
    def test_receive_DefaultLength_Valid(self):
        arr = [1, 2, 3, 4, 5, 6, 7, 8]

        with mock.patch(
                'uhppote_rfid.controller_socket.socket') as mock_socket:
            mockSocket = ControllerSocket('127.0.0.1')
            mockSocket.socket.recv.return_value = bytearray(arr)

            data = bytearray()
            for i in range(0, 8):
                data.extend(arr)

            mockSocket.connect()
            self.assertEquals(mockSocket.receive(), data)
コード例 #4
0
    def test_send_Interrupt_Exception(self):
        with mock.patch(
                'uhppote_rfid.controller_socket.socket') as mock_socket:
            mockSocket = ControllerSocket('127.0.0.1')
            mockSocket.socket.send.return_value = 0

            mockSocket.connect()

            with self.assertRaises(SocketTransmitException):
                mockSocket.send('hello')
コード例 #5
0
    def test_receive_Cutoff_Exception(self):
        with mock.patch(
                'uhppote_rfid.controller_socket.socket') as mock_socket:
            mockSocket = ControllerSocket('127.0.0.1')
            mockSocket.socket.recv.return_value = ''

            mockSocket.connect()

            with self.assertRaises(SocketTransmitException):
                mockSocket.receive()
コード例 #6
0
    def test_send_ByteArray_Valid(self):
        data = bytearray(['h', 'e', 'l', 'l', 'o'])

        with mock.patch(
                'uhppote_rfid.controller_socket.socket') as mock_socket:
            mockSocket = ControllerSocket('127.0.0.1')
            mockSocket.socket.send.return_value = len(data)

            mockSocket.connect()
            mockSocket.send(data)
            mockSocket.socket.send.assert_called_with(data)
コード例 #7
0
    def test_send_Bytes_Valid(self):
        data = bytes([10, 20, 30, 40])

        with mock.patch(
                'uhppote_rfid.controller_socket.socket') as mock_socket:
            mockSocket = ControllerSocket('127.0.0.1')
            mockSocket.socket.send.return_value = len(data)

            mockSocket.connect()
            mockSocket.send(data)
            mockSocket.socket.send.assert_called_with(data)
コード例 #8
0
    def test_send_String_Valid(self):
        data = 'Hello World'

        with mock.patch(
                'uhppote_rfid.controller_socket.socket') as mock_socket:
            mockSocket = ControllerSocket('127.0.0.1')
            mockSocket.socket.send.return_value = len(data)

            mockSocket.connect()
            mockSocket.send(data)
            mockSocket.socket.send.assert_called_with(data)
コード例 #9
0
 def test_constructor_IntegerHost_Exception(self):
     with self.assertRaises(ValueError):
         ControllerSocket(55)
コード例 #10
0
 def test_constructor_EmptyHost_Exception(self):
     with self.assertRaises(ValueError):
         ControllerSocket('')
コード例 #11
0
 def test_constructor_ByteArrayPort_Exception(self):
     with self.assertRaises(ValueError):
         ControllerSocket('127.0.0.1', bytearray([0, 5, 2]))
コード例 #12
0
 def test_constructor_FloatPort_Exception(self):
     with self.assertRaises(ValueError):
         ControllerSocket('127.0.0.1', 1.1)
コード例 #13
0
 def test_constructor_NonIntStringPort_Exception(self):
     with self.assertRaises(ValueError):
         ControllerSocket('127.0.0.1', 'ab')
コード例 #14
0
 def test_constructor_LargePort_Exception(self):
     with self.assertRaises(ValueError):
         ControllerSocket('127.0.0.1', 65535 + 1)
コード例 #15
0
 def test_constructor_IntegerPort_Valid(self):
     socket = ControllerSocket("127.0.0.1", 59)
     self.assertEquals(socket.getPort(), 59)
コード例 #16
0
 def test_constructor_StringIntegerPort_Valid(self):
     socket = ControllerSocket("127.0.0.1", '128')
     self.assertEquals(socket.getPort(), 128)
コード例 #17
0
class TestControllerSocket(unittest.TestCase):
    """
    Tests UHPPOTE socket transmission by emulating the control board's server.
    """
    def setUp(self):
        """
        .. function:: setUp()

           Runs a server locally on port 60000 to listen for connections to respond accordingly.
        """
        self.server = socket.socket()
        self.server.bind(('127.0.0.1', 60000))
        self.server.listen(1)

        self.socket = ControllerSocket('127.0.0.1')

    def tearDown(self):
        """
        .. function:: tearDown()

           Cleanly shuts down the test suite's server.
        """
        self.socket.close()
        self.sockt = None

        self.server.close()
        self.server = None

    # Socket.__init__

    def test_constructor_NegativePort_Exception(self):
        with self.assertRaises(ValueError):
            ControllerSocket('127.0.0.1', -1)

    def test_constructor_ZeroPort_Exception(self):
        with self.assertRaises(ValueError):
            ControllerSocket('127.0.0.1', 0)

    def test_constructor_LargePort_Exception(self):
        with self.assertRaises(ValueError):
            ControllerSocket('127.0.0.1', 65535 + 1)

    def test_constructor_BlankPort_Exception(self):
        with self.assertRaises(ValueError):
            ControllerSocket('127.0.0.1', '')

    def test_constructor_NonIntStringPort_Exception(self):
        with self.assertRaises(ValueError):
            ControllerSocket('127.0.0.1', 'ab')

    def test_constructor_FloatPort_Exception(self):
        with self.assertRaises(ValueError):
            ControllerSocket('127.0.0.1', 1.1)

    def test_constructor_ByteArrayPort_Exception(self):
        with self.assertRaises(ValueError):
            ControllerSocket('127.0.0.1', bytearray([0, 5, 2]))

    def test_constructor_EmptyHost_Exception(self):
        with self.assertRaises(ValueError):
            ControllerSocket('')

    def test_constructor_IntegerHost_Exception(self):
        with self.assertRaises(ValueError):
            ControllerSocket(55)

    def test_constructor_ByteArrayHost_TooLongException(self):
        with self.assertRaises(ValueError):
            ControllerSocket(bytearray([127, 0, 0, 1, 5]))

    def test_constructor_ByteArrayHost_TooShortException(self):
        with self.assertRaises(ValueError):
            ControllerSocket(bytearray([127, 0, 0]))

    def test_constructor_NegativeIP01_Exception(self):
        with self.assertRaises(ValueError):
            ControllerSocket('-1.0.0.0')

    def test_constructor_NegativeIP02_Exception(self):
        with self.assertRaises(ValueError):
            ControllerSocket('0.-1.0.0')

    def test_constructor_NegativeIP03_Exception(self):
        with self.assertRaises(ValueError):
            ControllerSocket('0.0.-3.0')

    def test_constructor_NegativeIP04_Exception(self):
        with self.assertRaises(ValueError):
            ControllerSocket('0.0.0.-1')

    def test_constructor_TooLongHost_Exception(self):
        with self.assertRaises(ValueError):
            ControllerSocket(
                'longhost.longhost.longhost.longhost.longhost.longhost.longhost.longhost.longhost.longhost.longhost.longhost.longhost.longhost.longhost.longhost.longhost.longhost.longhost.longhost.longhost.longhost.longhost.longhost.longhost.longhost.longhost.longhost.long'
            )

    def test_constructor_BadChar_Exception(self):
        with self.assertRaises(ValueError):
            ControllerSocket('Hello*World')

    def test_constructor_DefaultPort_Valid(self):
        self.assertEquals(self.socket.getPort(), 60000)

    def test_constructor_IntegerPort_Valid(self):
        socket = ControllerSocket("127.0.0.1", 59)
        self.assertEquals(socket.getPort(), 59)

    def test_constructor_StringIntegerPort_Valid(self):
        socket = ControllerSocket("127.0.0.1", '128')
        self.assertEquals(socket.getPort(), 128)

    def test_constructor_StringHost_Valid(self):
        self.assertEquals(self.socket.getHost(), "127.0.0.1")

    def test_constructor_ByteArrayHost_Valid(self):
        socket = ControllerSocket(bytearray([127, 0, 0, 1]))
        self.assertEquals(socket.getHost(), "127.0.0.1")

    def test_constructor_DotAtEndHost_Valid(self):
        socket = ControllerSocket("localhost.")
        self.assertEquals(socket.getHost(), "localhost")

    # Socket.connect

    def test_connect_ZeroAttempts_Exception(self):
        with self.assertRaises(ValueError):
            self.socket.connect(0)

    def test_connect_NegativeAttempts_Exception(self):
        with self.assertRaises(ValueError):
            self.socket.connect(-1)

    def test_connect_DefaultAttemptsFail_Exception(self):
        socket = ControllerSocket('badhost')
        with self.assertRaises(SocketConnectionException):
            socket.connect()

    def test_connect_ConnectLocal_Success(self):
        try:
            self.socket.connect()
        except SocketConnectionException, e:
            self.fail("Unexpected SocketConnectionException raisesd: %s" %
                      str(e))
コード例 #18
0
 def test_constructor_BadChar_Exception(self):
     with self.assertRaises(ValueError):
         ControllerSocket('Hello*World')
コード例 #19
0
 def test_constructor_ByteArrayHost_TooShortException(self):
     with self.assertRaises(ValueError):
         ControllerSocket(bytearray([127, 0, 0]))
コード例 #20
0
 def test_constructor_ByteArrayHost_Valid(self):
     socket = ControllerSocket(bytearray([127, 0, 0, 1]))
     self.assertEquals(socket.getHost(), "127.0.0.1")
コード例 #21
0
 def test_constructor_DotAtEndHost_Valid(self):
     socket = ControllerSocket("localhost.")
     self.assertEquals(socket.getHost(), "localhost")
コード例 #22
0
 def test_connect_DefaultAttemptsFail_Exception(self):
     socket = ControllerSocket('badhost')
     with self.assertRaises(SocketConnectionException):
         socket.connect()
コード例 #23
0
 def test_constructor_NegativeIP04_Exception(self):
     with self.assertRaises(ValueError):
         ControllerSocket('0.0.0.-1')
コード例 #24
0
 def test_constructor_TooLongHost_Exception(self):
     with self.assertRaises(ValueError):
         ControllerSocket(
             'longhost.longhost.longhost.longhost.longhost.longhost.longhost.longhost.longhost.longhost.longhost.longhost.longhost.longhost.longhost.longhost.longhost.longhost.longhost.longhost.longhost.longhost.longhost.longhost.longhost.longhost.longhost.longhost.long'
         )