Esempio n. 1
0
    def testGetConnection(self, file_server):
        file_server.ip_incoming = {}  # Reset flood protection
        client = ConnectionServer("127.0.0.1", 1545)
        connection = client.getConnection("127.0.0.1", 1544)

        # Get connection by ip/port
        connection2 = client.getConnection("127.0.0.1", 1544)
        assert connection == connection2

        # Get connection by peerid
        assert not client.getConnection("127.0.0.1", 1544, peer_id="notexists", create=False)
        connection2 = client.getConnection("127.0.0.1", 1544, peer_id=connection.handshake["peer_id"], create=False)
        assert connection2 == connection

        connection.close()
        client.stop()
Esempio n. 2
0
    def testStreamFile(self, file_server, site):
        file_server.ip_incoming = {}  # Reset flood protection
        client = ConnectionServer("127.0.0.1", 1545)
        connection = client.getConnection("127.0.0.1", 1544)
        file_server.sites[site.address] = site

        buff = StringIO.StringIO()
        response = connection.request("streamFile", {"site": site.address, "inner_path": "content.json", "location": 0}, buff)
        assert "stream_bytes" in response
        assert "sign" in buff.getvalue()

        # Invalid file
        buff = StringIO.StringIO()
        response = connection.request("streamFile", {"site": site.address, "inner_path": "invalid.file", "location": 0}, buff)
        assert "File read error" in response["error"]

        # Location over size
        buff = StringIO.StringIO()
        response = connection.request(
            "streamFile", {"site": site.address, "inner_path": "content.json", "location": 1024 * 1024}, buff
        )
        assert "File read error" in response["error"]

        # Stream from parent dir
        buff = StringIO.StringIO()
        response = connection.request("streamFile", {"site": site.address, "inner_path": "../users.json", "location": 0}, buff)
        assert "File read error" in response["error"]

        connection.close()
        client.stop()
Esempio n. 3
0
    def testPing(self, file_server, site):
        file_server.ip_incoming = {}  # Reset flood protection
        client = ConnectionServer("127.0.0.1", 1545)
        connection = client.getConnection("127.0.0.1", 1544)

        assert connection.ping()

        connection.close()
        client.stop()
Esempio n. 4
0
    def testFloodProtection(self, file_server):
        file_server.ip_incoming = {}  # Reset flood protection
        whitelist = file_server.whitelist  # Save for reset
        file_server.whitelist = []  # Disable 127.0.0.1 whitelist
        client = ConnectionServer("127.0.0.1", 1545)

        # Only allow 6 connection in 1 minute
        for reconnect in range(6):
            connection = client.getConnection("127.0.0.1", 1544)
            assert connection.handshake
            connection.close()

        # The 7. one will timeout
        with pytest.raises(gevent.Timeout):
            with gevent.Timeout(0.1):
                connection = client.getConnection("127.0.0.1", 1544)

        # Reset whitelist
        file_server.whitelist = whitelist
Esempio n. 5
0
    def testSslConnection(self, file_server):
        file_server.ip_incoming = {}  # Reset flood protection
        client = ConnectionServer("127.0.0.1", 1545)
        assert file_server != client

        # Connect to myself
        with mock.patch('Config.config.ip_local', return_value=[]):  # SSL not used for local ips
            connection = client.getConnection("127.0.0.1", 1544)

        assert len(file_server.connections) == 1
        assert connection.handshake
        assert connection.crypt

        # Close connection
        connection.close()
        client.stop()
        time.sleep(0.01)
        assert len(file_server.connections) == 0
Esempio n. 6
0
    def testSslConnection(self, file_server):
        file_server.ip_incoming = {}  # Reset flood protection
        client = ConnectionServer("127.0.0.1", 1545)
        assert file_server != client

        # Connect to myself
        connection = client.getConnection("127.0.0.1", 1544)
        assert len(file_server.connections) == 1
        assert len(file_server.ips) == 1
        assert connection.handshake
        assert connection.crypt

        # Close connection
        connection.close()
        client.stop()
        time.sleep(0.01)
        assert len(file_server.connections) == 0
        assert len(file_server.ips) == 0
Esempio n. 7
0
    def testRawConnection(self, file_server):
        file_server.ip_incoming = {}  # Reset flood protection
        client = ConnectionServer("127.0.0.1", 1545)
        assert file_server != client

        # Remove all supported crypto
        crypt_supported_bk = CryptConnection.manager.crypt_supported
        CryptConnection.manager.crypt_supported = []

        connection = client.getConnection("127.0.0.1", 1544)
        assert len(file_server.connections) == 1
        assert not connection.crypt

        # Close connection
        connection.close()
        client.stop()
        time.sleep(0.01)
        assert len(file_server.connections) == 0

        # Reset supported crypts
        CryptConnection.manager.crypt_supported = crypt_supported_bk
Esempio n. 8
0
    def testGetFile(self, file_server, site):
        file_server.ip_incoming = {}  # Reset flood protection
        client = ConnectionServer("127.0.0.1", 1545)

        connection = client.getConnection("127.0.0.1", 1544)
        file_server.sites[site.address] = site

        # Normal request
        response = connection.request("getFile", {"site": site.address, "inner_path": "content.json", "location": 0})
        assert "sign" in response["body"]

        response = connection.request("getFile", {"site": site.address, "inner_path": "content.json", "location": 0, "file_size": site.storage.getSize("content.json")})
        assert "sign" in response["body"]

        # Invalid file
        response = connection.request("getFile", {"site": site.address, "inner_path": "invalid.file", "location": 0})
        assert "File read error" in response["error"]

        # Location over size
        response = connection.request("getFile", {"site": site.address, "inner_path": "content.json", "location": 1024 * 1024})
        assert "File read error" in response["error"]

        # Stream from parent dir
        response = connection.request("getFile", {"site": site.address, "inner_path": "../users.json", "location": 0})
        assert "File read error" in response["error"]

        # Invalid site
        response = connection.request("getFile", {"site": "", "inner_path": "users.json", "location": 0})
        assert "Unknown site" in response["error"]

        response = connection.request("getFile", {"site": ".", "inner_path": "users.json", "location": 0})
        assert "Unknown site" in response["error"]

        # Invalid size
        response = connection.request("getFile", {"site": site.address, "inner_path": "content.json", "location": 0, "file_size": 1234})
        assert "File size does not match" in response["error"]

        connection.close()
        client.stop()
Esempio n. 9
0
    def testRawConnection(self, file_server):
        client = ConnectionServer(file_server.ip, 1545)
        assert file_server != client

        # Remove all supported crypto
        crypt_supported_bk = CryptConnection.manager.crypt_supported
        CryptConnection.manager.crypt_supported = []

        with mock.patch('Config.config.ip_local',
                        return_value=[]):  # SSL not used for local ips
            connection = client.getConnection(file_server.ip, 1544)
        assert len(file_server.connections) == 1
        assert not connection.crypt

        # Close connection
        connection.close()
        client.stop()
        time.sleep(0.01)
        assert len(file_server.connections) == 0

        # Reset supported crypts
        CryptConnection.manager.crypt_supported = crypt_supported_bk
Esempio n. 10
0
    def testRawConnection(self, file_server):
        file_server.ip_incoming = {}  # Reset flood protection
        client = ConnectionServer("127.0.0.1", 1545)
        assert file_server != client

        # Remove all supported crypto
        crypt_supported_bk = CryptConnection.manager.crypt_supported
        CryptConnection.manager.crypt_supported = []

        print "---"
        with mock.patch('Config.config.ip_local', return_value=[]):  # SSL not used for local ips
            connection = client.getConnection("127.0.0.1", 1544)
        assert len(file_server.connections) == 1
        assert not connection.crypt

        # Close connection
        connection.close()
        client.stop()
        time.sleep(0.01)
        assert len(file_server.connections) == 0

        # Reset supported crypts
        CryptConnection.manager.crypt_supported = crypt_supported_bk
Esempio n. 11
0
    def testGetFile(self, file_server, site):
        file_server.ip_incoming = {}  # Reset flood protection
        client = ConnectionServer("127.0.0.1", 1545)

        connection = client.getConnection("127.0.0.1", 1544)
        file_server.sites[site.address] = site

        response = connection.request("getFile", {"site": site.address, "inner_path": "content.json", "location": 0})
        assert "sign" in response["body"]

        # Invalid file
        response = connection.request("getFile", {"site": site.address, "inner_path": "invalid.file", "location": 0})
        assert "No such file or directory" in response["error"]

        # Location over size
        response = connection.request("getFile", {"site": site.address, "inner_path": "content.json", "location": 1024 * 1024})
        assert "File read error" in response["error"]

        # Stream from parent dir
        response = connection.request("getFile", {"site": site.address, "inner_path": "../users.json", "location": 0})
        assert "File not allowed" in response["error"]

        connection.close()
        client.stop()
Esempio n. 12
0
    def testGetFile(self, file_server, site):
        file_server.ip_incoming = {}  # Reset flood protection
        client = ConnectionServer(file_server.ip, 1545)

        connection = client.getConnection(file_server.ip, 1544)
        file_server.sites[site.address] = site

        # Normal request
        response = connection.request("getFile", {
            "site": site.address,
            "inner_path": "content.json",
            "location": 0
        })
        assert b"sign" in response["body"]

        response = connection.request(
            "getFile", {
                "site": site.address,
                "inner_path": "content.json",
                "location": 0,
                "file_size": site.storage.getSize("content.json")
            })
        assert b"sign" in response["body"]

        # Invalid file
        response = connection.request("getFile", {
            "site": site.address,
            "inner_path": "invalid.file",
            "location": 0
        })
        assert "File read error" in response["error"]

        # Location over size
        response = connection.request(
            "getFile", {
                "site": site.address,
                "inner_path": "content.json",
                "location": 1024 * 1024
            })
        assert "File read error" in response["error"]

        # Stream from parent dir
        response = connection.request("getFile", {
            "site": site.address,
            "inner_path": "../users.json",
            "location": 0
        })
        assert "File read exception" in response["error"]

        # Invalid site
        response = connection.request("getFile", {
            "site": "",
            "inner_path": "users.json",
            "location": 0
        })
        assert "Unknown site" in response["error"]

        response = connection.request("getFile", {
            "site": ".",
            "inner_path": "users.json",
            "location": 0
        })
        assert "Unknown site" in response["error"]

        # Invalid size
        response = connection.request(
            "getFile", {
                "site": site.address,
                "inner_path": "content.json",
                "location": 0,
                "file_size": 1234
            })
        assert "File size does not match" in response["error"]

        # Invalid path
        for path in [
                "../users.json", "./../users.json", "data/../content.json",
                ".../users.json"
        ]:
            for sep in ["/", "\\"]:
                response = connection.request(
                    "getFile", {
                        "site": site.address,
                        "inner_path": path.replace("/", sep),
                        "location": 0
                    })
                assert response["error"] == 'File read exception'

        connection.close()
        client.stop()
Esempio n. 13
0
    def testGetFile(self, file_server, site):
        file_server.ip_incoming = {}  # Reset flood protection
        client = ConnectionServer("127.0.0.1", 1545)

        connection = client.getConnection("127.0.0.1", 1544)
        file_server.sites[site.address] = site

        # Normal request
        response = connection.request("getFile", {
            "site": site.address,
            "inner_path": "content.json",
            "location": 0
        })
        assert "sign" in response["body"]

        response = connection.request(
            "getFile", {
                "site": site.address,
                "inner_path": "content.json",
                "location": 0,
                "file_size": site.storage.getSize("content.json")
            })
        assert "sign" in response["body"]

        # Invalid file
        response = connection.request("getFile", {
            "site": site.address,
            "inner_path": "invalid.file",
            "location": 0
        })
        assert "File read error" in response["error"]

        # Location over size
        response = connection.request(
            "getFile", {
                "site": site.address,
                "inner_path": "content.json",
                "location": 1024 * 1024
            })
        assert "File read error" in response["error"]

        # Stream from parent dir
        response = connection.request("getFile", {
            "site": site.address,
            "inner_path": "../users.json",
            "location": 0
        })
        assert "File read error" in response["error"]

        # Invalid site
        response = connection.request("getFile", {
            "site": "",
            "inner_path": "users.json",
            "location": 0
        })
        assert "Unknown site" in response["error"]

        response = connection.request("getFile", {
            "site": ".",
            "inner_path": "users.json",
            "location": 0
        })
        assert "Unknown site" in response["error"]

        # Invalid size
        response = connection.request(
            "getFile", {
                "site": site.address,
                "inner_path": "content.json",
                "location": 0,
                "file_size": 1234
            })
        assert "File size does not match" in response["error"]

        connection.close()
        client.stop()