def test_createClientThread(self): hub = Hub(store=MockStore()) socket = MockSocket() expected = HubClient(clientSocket=socket, ip="127.0.0.1", store=MockStore()) client, metadata = hub.createClientThread(socket, "127.0.0.1") self.assertIsInstance(client, HubClient) self.assertEqual(client.clientSocket, expected.clientSocket) self.assertEqual(client.ip, expected.ip)
def test_HubClient_handshake(self): socket = MockSocket() socket.funcOnRecv(lambda _: HUB_VERSION + SEPARATOR) hubClient = HubClient(socket, "127.0.0.1", store=MockStore()) self.assertEqual(Util.getStates(Util.run(hubClient)), STATE_SEQUENCE) self.assertEqual(socket.getSent(), [HUB_VERSION])
def __init__(self, client_socket, ip, **args): super(DummyClient, self).__init__(store=MockStore(), is_thread=True, enable_hub=False, **args) self.client_socket = client_socket self.ip = ip
def test_startStop_single_thread_noHub_daemon(self): ami = DummyAMI(enable_hub=False, is_thread=True, store=MockStore(), is_daemon=True) self.assertEqual(Util.getStates(Util.run(ami)), STATE_SEQUENCE) self.assertEqual(ami.TAG, ami._thread.name) self.assertTrue(ami._thread.daemon)
def test_HubClient_handshake_invalid(self): socket = MockSocket() socket.funcOnRecv(lambda _: b"XXXX") hubClient = HubClient(socket, "127.0.0.1", store=MockStore()) self.assertEqual(Util.getStates(Util.run(hubClient)), FAILED_START_SEQUENCE) self.assertEqual(socket.getSent(), [HUB_VERSION, b"Invalid handshake"])
def test_HubClient_handshake_unsuported(self): socket = MockSocket() socket.funcOnRecv(lambda _: b"HUB/9.9" + SEPARATOR) hubClient = HubClient(socket, "127.0.0.1", store=MockStore()) self.assertEqual(Util.getStates(Util.run(hubClient)), FAILED_START_SEQUENCE) self.assertEqual(socket.getSent(), [HUB_VERSION, b"Unsupported Hub version"])
def test_HubClient_outboundQueue(self): socket = MockSocket() socket.funcOnRecv(lambda _: HUB_VERSION + SEPARATOR) def callback(ami): ami.store.put(OUBOUND_PATH, ["A", "B"]) time.sleep(0.2) hubClient = HubClient(socket, "127.0.0.1", store=MockStore()) self.assertEqual(Util.getStates(Util.run(hubClient, callback)), STATE_SEQUENCE) self.assertEqual(socket.getSent(), [HUB_VERSION, b"A", b"B"])
def test_HubClient_inboundQueue(self): signal = Signal(data=B.serialize(B.create('a', 'p')), channel="ABCDEF") socket = MockSocket() socket.listOnRecv([HUB_VERSION + SEPARATOR, signal.bytes()]) def callback(ami): time.sleep(0.2) hubClient = HubClient(socket, "127.0.0.1", store=MockStore()) self.assertEqual(Util.getStates(Util.run(hubClient, callback)), STATE_SEQUENCE) inbounds = hubClient.store.get(INBOUND_PATH) self.assertEqual(inbounds, [signal.bytes()])
def test_connectClient_loop(self): server = TCPServer(enable_hub=False, is_thread=True, app=LoopDummyClient, store=MockStore()) def callback(ami): s1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s1.connect((server.host, server.port)) s1.send(b"1") self.assertEqual(s1.recv(1024), bytes(len("1"))) s1.send(b"11") self.assertEqual(s1.recv(1024), bytes(len("11"))) s1.close() time.sleep(0.1) states = Util.run(server, run_callback=callback) self.assertEqual(Util.getStates(states), STATE_SEQUENCE)
def test_client_inbound(self): block1 = B.serialize( B.create("t1", "A", { "method": ADD_METHOD, "path": "p1" })) block2 = B.serialize( B.create("t1", "B", { "method": ADD_METHOD, "path": "p1" })) signal1 = Signal(data=block1, channel="Ch1") signal2 = Signal(data=block2, channel="Ch1") socket1 = MockSocket() socket1.listOnRecv( [HUB_VERSION + SEPARATOR, signal1.bytes(), signal2.bytes()]) hub = MockHub(store=MockStore(), subscriptions=["Ch1"]) hub.mockSockets([socket1, socket1]) def callback(ami): s1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s1.connect((hub.host, hub.port)) s1.close() time.sleep(0.2) self.assertEqual(Util.getStates(Util.run(hub, callback)), STATE_SEQUENCE) inbounds = [i[DATA] for i in hub.store.getLogs(INBOUND_PATH)] self.assertEqual( inbounds, [str(signal1.bytes()), str(signal2.bytes())]) p1_path = [i[DATA] for i in hub.store.getLogs("p1")] self.assertEqual(p1_path, ["A", "B"])
def test_commands(self): mock_socket = MockSocket() mock_socket.listOnRecv([ b"user tester\n", b"pass abcdef\n", b"aaa\n" b"exit\n" ]) def callback(ami): time.sleep(0.2) server = FTPServerApp(clientSocket=mock_socket, ip="127.0.0.1", is_thread=True, store=MockStore(), enable_hub=False) server.min_timeout = 0 server.max_timeout = 0 self.assertEqual(Util.getStates(Util.run(server, run_callback=callback)), STATE_SEQUENCE) self.assertEqual(mock_socket.getSent()[1:], [ b"331 User tester OK. Password required", b"530 Login authentication failed", b"530 You aren't logged in" ])
def test_startStop_single_thread_noHub(self): ami = DummyAMI(enable_hub=False, is_thread=True, store=MockStore()) self.assertEqual(Util.getStates(Util.run(ami)), STATE_SEQUENCE)
def test_startstop(self): hub = Hub(store=MockStore()) self.assertEqual(Util.getStates(Util.run(hub)), STATE_SEQUENCE)
def test_emit_offline(self): hub = Hub(store=MockStore()) assert (hub.store.get(OUBOUND_PATH) is None) signal = Signal(channel="CH1", data=B.serialize(B.create('a', 'p'))) hub.emit(signal) assert (hub.store.get(OUBOUND_PATH) == [signal.bytes()])
def test_init_host(self): self.assertEqual(Hub(store=MockStore()).host, "127.0.0.1") ip = "192.168.0.0" self.assertEqual(Hub(host=ip, store=MockStore()).host, ip)
def test_init_port(self): hub = Hub(store=MockStore()) self.assertTrue(hub.port > 1000) self.assertTrue(hub.port < 65000) self.assertEqual(Hub(port=15123, store=MockStore()).port, 15123)
def test_startStop(self): server = TCPServer(enable_hub=False, is_thread=True, store=MockStore()) self.assertEqual(Util.getStates(Util.run(server)), STATE_SEQUENCE)
def test_startStop_loop_process_noHub(self): ami = DummyAMI(enable_hub=False, is_thread=False, store=MockStore(), interval=0.01) self.assertEqual(Util.getStates(Util.run(ami)), STATE_SEQUENCE)