Ejemplo n.º 1
0
 def __init__(self, server_address, RequestHandlerClass,
              bind_and_activate=False, socket_cls=None, keep_alive=True,
              base_cls=SAPRouter):
     """Initialization of the SAP Router threaded server"""
     SAPNIServerThreaded.__init__(self, server_address, RequestHandlerClass,
                                  bind_and_activate, socket_cls, keep_alive,
                                  base_cls=base_cls)
Ejemplo n.º 2
0
 def start_server(self, handler_cls):
     self.server = SAPNIServerThreaded((self.test_address, self.test_port),
                                       handler_cls,
                                       bind_and_activate=False)
     self.server.allow_reuse_address = True
     self.server.server_bind()
     self.server.server_activate()
     self.server_thread = Thread(target=self.server.serve_forever)
     self.server_thread.start()
Ejemplo n.º 3
0
class PySAPNIServerTest(unittest.TestCase):

    test_port = 8005
    test_address = "127.0.0.1"
    test_string = "TEST" * 10
    handler_cls = SAPNIServerTestHandler

    def setUp(self):
        self.server = SAPNIServerThreaded((self.test_address, self.test_port),
                                          self.handler_cls,
                                          bind_and_activate=False)
        self.server.allow_reuse_address = True
        self.server.server_bind()
        self.server.server_activate()
        self.server_thread = Thread(target=self.server.serve_forever)
        self.server_thread.start()

    def tearDown(self):
        self.server.shutdown()
        self.server.server_close()

    def test_sapniserver(self):
        """Test SAPNIServer"""
        sock = socket.socket()
        sock.connect((self.test_address, self.test_port))
        sock.sendall(pack("!I", len(self.test_string)) + self.test_string)

        response = sock.recv(1024)

        self.assertEqual(len(response), len(self.test_string) + 8)
        self.assertEqual(unpack("!I", response[:4]), (len(self.test_string) + 4, ))
        self.assertEqual(unpack("!I", response[4:8]), (len(self.test_string), ))
        self.assertEqual(response[8:], self.test_string)

        sock.close()
Ejemplo n.º 4
0
class PySAPNIServerTest(unittest.TestCase):

    test_port = 8005
    test_address = "127.0.0.1"
    test_string = "TEST" * 10
    handler_cls = SAPNIServerTestHandler

    def setUp(self):
        self.server = SAPNIServerThreaded((self.test_address, self.test_port),
                                          self.handler_cls,
                                          bind_and_activate=False)
        self.server.allow_reuse_address = True
        self.server.server_bind()
        self.server.server_activate()
        self.server_thread = Thread(target=self.server.serve_forever)
        self.server_thread.start()

    def tearDown(self):
        self.server.shutdown()
        self.server.server_close()

    def test_sapniserver(self):
        """Test SAPNIServer"""
        sock = socket.socket()
        sock.connect((self.test_address, self.test_port))
        sock.sendall(pack("!I", len(self.test_string)) + self.test_string)

        response = sock.recv(1024)

        self.assertEqual(len(response), len(self.test_string) + 8)
        self.assertEqual(unpack("!I", response[:4]),
                         (len(self.test_string) + 4, ))
        self.assertEqual(unpack("!I", response[4:8]),
                         (len(self.test_string), ))
        self.assertEqual(response[8:], self.test_string)

        sock.close()
Ejemplo n.º 5
0
class PySAPRoutedStreamSocketTest(unittest.TestCase):

    test_port = 8005
    test_address = "127.0.0.1"
    test_string = "TEST" * 10

    def start_server(self, handler_cls):
        self.server = SAPNIServerThreaded((self.test_address, self.test_port),
                                          handler_cls,
                                          bind_and_activate=False)
        self.server.allow_reuse_address = True
        self.server.server_bind()
        self.server.server_activate()
        self.server_thread = Thread(target=self.server.serve_forever)
        self.server_thread.start()

    def stop_server(self):
        self.server.shutdown()
        self.server.server_close()
        self.server_thread.join()

    def test_saproutedstreamsocket(self):
        """Test SAPRoutedStreamSocket"""
        self.start_server(SAPRouterServerTestHandler)

        sock = socket.socket()
        sock.connect((self.test_address, self.test_port))

        route = [SAPRouterRouteHop(hostname=self.test_address,
                                   port=self.test_port),
                 SAPRouterRouteHop(hostname="10.0.0.1",
                                   port="3200")]

        self.client = SAPRoutedStreamSocket(sock, route=route,
                                            router_version=40)
        packet = self.client.sr(self.test_string)

        self.assertIn(SAPNI, packet)
        self.assertEqual(packet[SAPNI].length, len(self.test_string) + 4)
        self.assertEqual(unpack("!I", packet[SAPNI].payload.load[:4]), (len(self.test_string), ))
        self.assertEqual(packet[SAPNI].payload.load[4:], self.test_string)

        self.client.close()
        self.stop_server()

    def test_saproutedstreamsocket_route_error(self):
        """Test SAPRoutedStreamSocket throwing of SAPRouteException if
        a route denied return error is received"""
        self.start_server(SAPRouterServerTestHandler)

        sock = socket.socket()
        sock.connect((self.test_address, self.test_port))

        route = [SAPRouterRouteHop(hostname=self.test_address,
                                   port=self.test_port),
                 SAPRouterRouteHop(hostname="10.0.0.2",
                                   port="3200")]

        with self.assertRaises(SAPRouteException):
            self.client = SAPRoutedStreamSocket(sock, route=route,
                                                router_version=40)

        self.stop_server()

    def test_saproutedstreamsocket_error(self):
        """Test SAPRoutedStreamSocket throwing of Exception if an invalid
        or unexpected packet is received"""
        self.start_server(SAPRouterServerTestHandler)

        sock = socket.socket()
        sock.connect((self.test_address, self.test_port))

        with self.assertRaises(Exception):
            self.client = SAPRoutedStreamSocket(sock, route=None,
                                                router_version=40)

        self.stop_server()

    def test_saproutedstreamsocket_getnisocket(self):
        """Test SAPRoutedStreamSocket get nisocket class method"""
        self.start_server(SAPRouterServerTestHandler)

        # Test using a complete route
        route = [SAPRouterRouteHop(hostname=self.test_address,
                                   port=self.test_port),
                 SAPRouterRouteHop(hostname="10.0.0.1",
                                   port="3200")]
        self.client = SAPRoutedStreamSocket.get_nisocket(route=route,
                                                         router_version=40)

        packet = self.client.sr(self.test_string)
        self.assertIn(SAPNI, packet)
        self.assertEqual(packet[SAPNI].length, len(self.test_string) + 4)
        self.assertEqual(unpack("!I", packet[SAPNI].payload.load[:4]), (len(self.test_string), ))
        self.assertEqual(packet[SAPNI].payload.load[4:], self.test_string)

        # Test using a route and a target host/port
        route = [SAPRouterRouteHop(hostname=self.test_address,
                                   port=self.test_port)]
        self.client = SAPRoutedStreamSocket.get_nisocket("10.0.0.1",
                                                         "3200",
                                                         route=route,
                                                         router_version=40)

        packet = self.client.sr(self.test_string)
        self.assertIn(SAPNI, packet)
        self.assertEqual(packet[SAPNI].length, len(self.test_string) + 4)
        self.assertEqual(unpack("!I", packet[SAPNI].payload.load[:4]), (len(self.test_string), ))
        self.assertEqual(packet[SAPNI].payload.load[4:], self.test_string)

        # Test using a route string
        route = "/H/%s/S/%s/H/10.0.0.1/S/3200" % (self.test_address,
                                                  self.test_port)
        self.client = SAPRoutedStreamSocket.get_nisocket(route=route,
                                                         router_version=40)

        packet = self.client.sr(self.test_string)
        self.assertIn(SAPNI, packet)
        self.assertEqual(packet[SAPNI].length, len(self.test_string) + 4)
        self.assertEqual(unpack("!I", packet[SAPNI].payload.load[:4]), (len(self.test_string), ))
        self.assertEqual(packet[SAPNI].payload.load[4:], self.test_string)

        self.client.close()
        self.stop_server()
Ejemplo n.º 6
0
class PySAPNIProxyTest(unittest.TestCase):

    test_proxyport = 8005
    test_serverport = 8006
    test_address = "127.0.0.1"
    test_string = "TEST" * 10
    proxyhandler_cls = SAPNIProxyHandler
    serverhandler_cls = SAPNIServerTestHandler

    def setUp(self):
        self.server = SAPNIServerThreaded((self.test_address, self.test_serverport),
                                          self.serverhandler_cls,
                                          bind_and_activate=False)
        self.server.allow_reuse_address = True
        self.server.server_bind()
        self.server.server_activate()
        self.server_thread = Thread(target=self.server.serve_forever)
        self.server_thread.start()

    def tearDown(self):
        self.server.shutdown()
        self.server.server_close()

    def start_sapniproxy(self, handler_cls):
        self.proxy = SAPNIProxy(self.test_address, self.test_proxyport,
                                self.test_address, self.test_serverport,
                                handler=handler_cls)
        self.proxy_thread = Thread(target=self.handle_sapniproxy)
        self.proxy_thread.start()

    def handle_sapniproxy(self):
        self.proxy.handle_connection()

    def stop_sapniproxy(self):
        self.proxy.stop()

    def test_sapniproxy(self):
        self.start_sapniproxy(self.proxyhandler_cls)

        sock = socket.socket()
        sock.connect((self.test_address, self.test_proxyport))
        sock.sendall(pack("!I", len(self.test_string)) + self.test_string)

        response = sock.recv(1024)

        self.assertEqual(len(response), len(self.test_string) + 8)
        self.assertEqual(unpack("!I", response[:4]), (len(self.test_string) + 4, ))
        self.assertEqual(unpack("!I", response[4:8]), (len(self.test_string), ))
        self.assertEqual(response[8:], self.test_string)

        sock.close()
        self.stop_sapniproxy()

    def test_sapniproxy_process(self):

        class SAPNIProxyHandlerTest(SAPNIProxyHandler):

            def process_client(self, packet):
                return packet / Raw("Client")

            def process_server(self, packet):
                return packet / Raw("Server")

        self.start_sapniproxy(SAPNIProxyHandlerTest)

        sock = socket.socket()
        sock.connect((self.test_address, self.test_proxyport))
        sock.sendall(pack("!I", len(self.test_string)) + self.test_string)

        response = sock.recv(1024)

        expected_reponse = self.test_string + "Client" + "Server"
        self.assertEqual(len(response), len(expected_reponse) + 8)
        self.assertEqual(unpack("!I", response[:4]), (len(expected_reponse) + 4, ))
        self.assertEqual(unpack("!I", response[4:8]), (len(self.test_string) + 6, ))
        self.assertEqual(response[8:], expected_reponse)

        sock.close()
        self.stop_sapniproxy()
Ejemplo n.º 7
0
class PySAPNIProxyTest(unittest.TestCase):

    test_proxyport = 8005
    test_serverport = 8006
    test_address = "127.0.0.1"
    test_string = "TEST" * 10
    proxyhandler_cls = SAPNIProxyHandler
    serverhandler_cls = SAPNIServerTestHandler

    def setUp(self):
        self.server = SAPNIServerThreaded(
            (self.test_address, self.test_serverport),
            self.serverhandler_cls,
            bind_and_activate=False)
        self.server.allow_reuse_address = True
        self.server.server_bind()
        self.server.server_activate()
        self.server_thread = Thread(target=self.server.serve_forever)
        self.server_thread.start()

    def tearDown(self):
        self.server.shutdown()
        self.server.server_close()

    def start_sapniproxy(self, handler_cls):
        self.proxy = SAPNIProxy(self.test_address,
                                self.test_proxyport,
                                self.test_address,
                                self.test_serverport,
                                handler=handler_cls)
        self.proxy_thread = Thread(target=self.handle_sapniproxy)
        self.proxy_thread.start()

    def handle_sapniproxy(self):
        self.proxy.handle_connection()

    def stop_sapniproxy(self):
        self.proxy.stop()

    def test_sapniproxy(self):
        self.start_sapniproxy(self.proxyhandler_cls)

        sock = socket.socket()
        sock.connect((self.test_address, self.test_proxyport))
        sock.sendall(pack("!I", len(self.test_string)) + self.test_string)

        response = sock.recv(1024)

        self.assertEqual(len(response), len(self.test_string) + 8)
        self.assertEqual(unpack("!I", response[:4]),
                         (len(self.test_string) + 4, ))
        self.assertEqual(unpack("!I", response[4:8]),
                         (len(self.test_string), ))
        self.assertEqual(response[8:], self.test_string)

        sock.close()
        self.stop_sapniproxy()

    def test_sapniproxy_process(self):
        class SAPNIProxyHandlerTest(SAPNIProxyHandler):
            def process_client(self, packet):
                return packet / Raw("Client")

            def process_server(self, packet):
                return packet / Raw("Server")

        self.start_sapniproxy(SAPNIProxyHandlerTest)

        sock = socket.socket()
        sock.connect((self.test_address, self.test_proxyport))
        sock.sendall(pack("!I", len(self.test_string)) + self.test_string)

        response = sock.recv(1024)

        expected_reponse = self.test_string + "Client" + "Server"
        self.assertEqual(len(response), len(expected_reponse) + 8)
        self.assertEqual(unpack("!I", response[:4]),
                         (len(expected_reponse) + 4, ))
        self.assertEqual(unpack("!I", response[4:8]),
                         (len(self.test_string) + 6, ))
        self.assertEqual(response[8:], expected_reponse)

        sock.close()
        self.stop_sapniproxy()