Ejemplo n.º 1
0
class OpenTestCase(unittest.TestCase):
    def setUp(self):
        self.socket_mock = create_autospec(socket, instance=True)

        self.socket_selector_mock = create_autospec(BaseSelector,
                                                    instance=True)

        selector_key = Mock(fileobj=self.socket_mock)

        self.socket_selector_mock.select.return_value = [
            (selector_key, selectors.EVENT_READ)
        ]

        patcher = patch('socket.create_connection')

        create_connection_mock = patcher.start()

        create_connection_mock.return_value = self.socket_mock

        patcher = patch('selectors.DefaultSelector')

        default_selector_mock = patcher.start()

        default_selector_mock.return_value = self.socket_selector_mock

        patcher = patch('ssl.SSLContext.wrap_socket')

        ssl_wrap_socket_mock = patcher.start()

        ssl_wrap_socket_mock.return_value = self.socket_mock

        self.addCleanup(patch.stopall)

    def test_basic_tn3270_negotiation(self):
        # Arrange
        self.telnet = Telnet('IBM-3279-2-E')

        responses = [
            bytes.fromhex('ff fd 18'),
            bytes.fromhex('ff fa 18 01 ff f0'),
            bytes.fromhex('ff fd 19'),
            bytes.fromhex('ff fb 19'),
            bytes.fromhex('ff fd 00'),
            bytes.fromhex('ff fb 00')
        ]

        self.socket_mock.recv = Mock(side_effect=responses)

        self.assertFalse(self.telnet.is_tn3270_negotiated)
        self.assertFalse(self.telnet.is_tn3270e_negotiated)

        # Act
        self.telnet.open('mainframe', 23)

        # Assert
        self.assertTrue(self.telnet.is_tn3270_negotiated)
        self.assertFalse(self.telnet.is_tn3270e_negotiated)

        self.socket_mock.sendall.assert_any_call(bytes.fromhex('ff fb 18'))
        self.socket_mock.sendall.assert_any_call(
            bytes.fromhex(
                'ff fa 18 00 49 42 4d 2d 33 32 37 39 2d 32 2d 45 ff f0'))
        self.socket_mock.sendall.assert_any_call(bytes.fromhex('ff fb 19'))
        self.socket_mock.sendall.assert_any_call(bytes.fromhex('ff fd 19'))
        self.socket_mock.sendall.assert_any_call(bytes.fromhex('ff fb 00'))
        self.socket_mock.sendall.assert_any_call(bytes.fromhex('ff fd 00'))

    def test_tn3270e_negotiation(self):
        # Arrange
        self.telnet = Telnet('IBM-3279-2-E')

        responses = [
            bytes.fromhex('ff fd 28'),
            bytes.fromhex('ff fa 28 08 02 ff f0'),
            bytes.fromhex(
                'ff fa 28 02 04 49 42 4d 2d 33 32 37 38 2d 32 2d 45 01 54 43 50 30 30 30 33 34 ff f0'
            ),
            bytes.fromhex('ff fa 28 03 04 ff f0')
        ]

        self.socket_mock.recv = Mock(side_effect=responses)

        self.assertFalse(self.telnet.is_tn3270_negotiated)
        self.assertFalse(self.telnet.is_tn3270e_negotiated)

        # Act
        self.telnet.open('mainframe', 23)

        # Assert
        self.assertTrue(self.telnet.is_tn3270_negotiated)
        self.assertTrue(self.telnet.is_tn3270e_negotiated)

        self.assertEqual(self.telnet.device_type, 'IBM-3278-2-E')
        self.assertEqual(self.telnet.device_name, 'TCP00034')

        self.socket_mock.sendall.assert_any_call(bytes.fromhex('ff fb 28'))
        self.socket_mock.sendall.assert_any_call(
            bytes.fromhex(
                'ff fa 28 02 07 49 42 4d 2d 33 32 37 38 2d 32 2d 45 ff f0'))
        self.socket_mock.sendall.assert_any_call(
            bytes.fromhex('ff fa 28 03 07 ff f0'))

    def test_basic_tn3270_negotiation_when_tn3270e_not_enabled(self):
        # Arrange
        self.telnet = Telnet('IBM-3279-2-E', is_tn3270e_enabled=False)

        responses = [
            bytes.fromhex('ff fd 28'),
            bytes.fromhex('ff fd 18'),
            bytes.fromhex('ff fa 18 01 ff f0'),
            bytes.fromhex('ff fd 19'),
            bytes.fromhex('ff fb 19'),
            bytes.fromhex('ff fd 00'),
            bytes.fromhex('ff fb 00')
        ]

        self.socket_mock.recv = Mock(side_effect=responses)

        self.assertFalse(self.telnet.is_tn3270_negotiated)
        self.assertFalse(self.telnet.is_tn3270e_negotiated)

        # Act
        self.telnet.open('mainframe', 23)

        # Assert
        self.assertTrue(self.telnet.is_tn3270_negotiated)
        self.assertFalse(self.telnet.is_tn3270e_negotiated)

        self.socket_mock.sendall.assert_any_call(bytes.fromhex('ff fc 28'))
        self.socket_mock.sendall.assert_any_call(bytes.fromhex('ff fb 18'))
        self.socket_mock.sendall.assert_any_call(
            bytes.fromhex(
                'ff fa 18 00 49 42 4d 2d 33 32 37 39 2d 32 2d 45 ff f0'))
        self.socket_mock.sendall.assert_any_call(bytes.fromhex('ff fb 19'))
        self.socket_mock.sendall.assert_any_call(bytes.fromhex('ff fd 19'))
        self.socket_mock.sendall.assert_any_call(bytes.fromhex('ff fb 00'))
        self.socket_mock.sendall.assert_any_call(bytes.fromhex('ff fd 00'))

    def test_unsuccessful_negotiation(self):
        # Arrange
        self.telnet = Telnet('IBM-3279-2-E')

        self.socket_mock.recv = Mock(
            return_value='hello world'.encode('ascii'))

        self.assertFalse(self.telnet.is_tn3270_negotiated)
        self.assertFalse(self.telnet.is_tn3270e_negotiated)

        # Act and assert
        with self.assertRaisesRegex(Exception,
                                    'Unable to negotiate TN3270 mode'):
            self.telnet.open('mainframe', 23)

    def test_tn3270e_negotiation_ssl(self):
        # Arrange
        self.telnet = Telnet('IBM-3279-2-E')

        responses = [
            bytes.fromhex('ff fd 28'),
            bytes.fromhex('ff fa 28 08 02 ff f0'),
            bytes.fromhex(
                'ff fa 28 02 04 49 42 4d 2d 33 32 37 38 2d 32 2d 45 01 54 43 50 30 30 30 33 34 ff f0'
            ),
            bytes.fromhex('ff fa 28 03 04 ff f0')
        ]

        self.socket_mock.recv = Mock(side_effect=responses)

        self.assertFalse(self.telnet.is_tn3270_negotiated)
        self.assertFalse(self.telnet.is_tn3270e_negotiated)

        # Act
        ssl_context = ssl.create_default_context()
        self.telnet.open('mainframe', 23, ssl_context=ssl_context)

        # Assert
        self.assertTrue(self.telnet.is_tn3270_negotiated)
        self.assertTrue(self.telnet.is_tn3270e_negotiated)

        self.assertEqual(self.telnet.device_type, 'IBM-3278-2-E')
        self.assertEqual(self.telnet.device_name, 'TCP00034')

        self.socket_mock.sendall.assert_any_call(bytes.fromhex('ff fb 28'))
        self.socket_mock.sendall.assert_any_call(
            bytes.fromhex(
                'ff fa 28 02 07 49 42 4d 2d 33 32 37 38 2d 32 2d 45 ff f0'))
        self.socket_mock.sendall.assert_any_call(
            bytes.fromhex('ff fa 28 03 07 ff f0'))
Ejemplo n.º 2
0
class OpenTestCase(unittest.TestCase):
    def setUp(self):
        self.telnet = Telnet('IBM-3278-2')

        self.socket_mock = Mock()

        patcher = patch('socket.create_connection')

        create_connection_mock = patcher.start()

        create_connection_mock.return_value = self.socket_mock

        patcher = patch('tn3270.telnet.select')

        select_mock = patcher.start()

        select_mock.return_value = [[self.socket_mock]]

        self.addCleanup(patch.stopall)

    def test_negotiation(self):
        # Arrange
        responses = [
            bytes.fromhex('ff fd 28'),
            bytes.fromhex('ff fd 18'),
            bytes.fromhex('ff fa 18 01 ff f0'),
            bytes.fromhex('ff fd 19'),
            bytes.fromhex('ff fb 19'),
            bytes.fromhex('ff fd 00'),
            bytes.fromhex('ff fb 00')
        ]

        self.socket_mock.recv = Mock(side_effect=responses)

        self.assertFalse(self.telnet.is_3270)

        # Act
        self.telnet.open('mainframe', 23)

        # Assert
        self.socket_mock.sendall.assert_any_call(bytes.fromhex('ff fc 28'))
        self.socket_mock.sendall.assert_any_call(bytes.fromhex('ff fb 18'))
        self.socket_mock.sendall.assert_any_call(
            bytes.fromhex('ff fa 18 00 49 42 4d 2d 33 32 37 38 2d 32 ff f0'))
        self.socket_mock.sendall.assert_any_call(bytes.fromhex('ff fb 19'))
        self.socket_mock.sendall.assert_any_call(bytes.fromhex('ff fd 19'))
        self.socket_mock.sendall.assert_any_call(bytes.fromhex('ff fb 00'))
        self.socket_mock.sendall.assert_any_call(bytes.fromhex('ff fd 00'))

        self.assertTrue(self.telnet.is_3270)

    def test_unsuccessful_negotiation(self):
        # Arrange
        self.socket_mock.recv = Mock(
            return_value='hello world'.encode('ascii'))

        self.assertFalse(self.telnet.is_3270)

        # Act and assert
        with self.assertRaisesRegex(Exception,
                                    'Unable to negotiate 3270 mode'):
            self.telnet.open('mainframe', 23)