Beispiel #1
0
class TestManager(object):

    def setup_class(self):
        self.tempfile = CleanedTempFile()

    def teardown_class(self):
        self.tempfile.clean()

    def setup(self):
        set_cwd(self.tempfile.mkdtemp())
        self.db = Database()
        self.db.connect(create=True)

    def test_acquire(self):
        past = datetime.datetime.now()
        for c in ["United States", "China", "Germany"]:
            self.db.add_socks5(
                "8.8.8.8", 1337, c, "Unknown",
                city="Unknown", operational=True, username="******",
                password="******", description="Such wow, many socks5"
            )
        m = Manager()
        socks5_1 = m.acquire()
        socks5_2 = m.acquire()
        socks5_3 = m.acquire()
        socks5_4 = m.acquire()
        assert socks5_1.id == 1
        assert socks5_1.last_use > past
        assert socks5_2.id == 2
        assert socks5_2.last_use > past
        assert socks5_3.id == 3
        assert socks5_3.last_use > past
        assert socks5_4.id == 1
        assert socks5_4.last_use > socks5_1.last_use

    def test_acquire_country(self):
        for c in ["United States", "China", "Germany"]:
            self.db.add_socks5(
                "8.8.8.8", 1337, c, "Unknown",
                city="Unknown", operational=True, username="******",
                password="******", description="Such wow, many socks5"
            )
        m = Manager()
        socks5_1 = m.acquire(country="germany")
        assert socks5_1.id == 3
        assert socks5_1.country == "Germany"
        socks5_2 = m.acquire(country="france")
        assert socks5_2 is None

    def test_acquire_no_operational(self):
        self.db.add_socks5(
            "8.8.8.8", 1337, "germany", "Unknown",
            city="Unknown", operational=False, username="******",
            password="******", description="Such wow, many socks5"
        )
        self.db.add_socks5(
            "8.8.8.8", 1337, "china", "Unknown",
            city="Unknown", operational=False, username="******",
            password="******", description="Such wow, many socks5"
        )
        m = Manager()
        socks5_1 = m.acquire()
        assert socks5_1 is None
        socks5_2 = m.acquire(country="china")
        assert socks5_2 is None

        self.db.add_socks5(
            "8.8.8.8", 1337, "france", "Unknown",
            city="Unknown", operational=True, username="******",
            password="******", description="Such wow, many socks5"
        )
        socks5_3 = m.acquire()
        assert socks5_3.id == 3

    def test_acquire_city(self):
        for c in ["Dogedown", "Amsterdam", "Tallinn"]:
            self.db.add_socks5(
                "8.8.8.8", 1337, "Unknown", "Unknown",
                city=c, operational=True, username="******",
                password="******", description="Such wow, many socks5"
            )
        m = Manager()
        socks5_1 = m.acquire(city="tallinn")
        assert socks5_1.id == 3
        assert socks5_1.city == "Tallinn"
        socks5_2 = m.acquire(city="Nowhere")
        assert socks5_2 is None

    def test_acquire_countrycode(self):
        for c in ["DE", "CA", "NL"]:
            self.db.add_socks5(
                "8.8.8.8", 1337, "Unknown", c,
                city="Unknown", operational=True, username="******",
                password="******", description="Such wow, many socks5"
            )
        m = Manager()
        socks5_1 = m.acquire(country_code="ca")
        assert socks5_1.id == 2
        assert socks5_1.country_code == "CA"
        socks5_2 = m.acquire(country_code="FR")
        assert socks5_2 is None

    def test_acquire_mbps(self):
        for c in ["DE", "CA", "NL", "NL", "PL"]:
            self.db.add_socks5(
                "8.8.8.8", 1337, "Unknown", c,
                city="Unknown", operational=True, username="******",
                password="******", description="Such wow, many socks5"
            )
        self.db.set_approx_bandwidth(2, 5.33)
        self.db.set_approx_bandwidth(3, 19.811)
        self.db.set_approx_bandwidth(4, 7.134)
        self.db.set_approx_bandwidth(5, 28.134)

        m = Manager()
        socks5_1 = m.acquire(min_mbps_down=4)
        socks5_2 = m.acquire(min_mbps_down=4, country_code="nl")
        socks5_3 = m.acquire(min_mbps_down=1, country_code="de")
        assert socks5_1.id == 5
        assert socks5_2.id == 3
        assert socks5_3 is None

    def test_acquire_conntime(self):
        for c in ["DE", "CA", "NL", "NL", "PL"]:
            self.db.add_socks5(
                "8.8.8.8", 1337, "Unknown", c,
                city="Unknown", operational=True, username="******",
                password="******", description="Such wow, many socks5"
            )
        self.db.set_connect_time(2, 0.02)
        self.db.set_connect_time(3, 0.1)
        self.db.set_connect_time(4, 0.0054)
        self.db.set_connect_time(5, 1.3)

        m = Manager()
        socks5_1 = m.acquire(max_connect_time=0.01)
        socks5_2 = m.acquire(max_connect_time=0.2, country_code="nl")
        socks5_3 = m.acquire(max_connect_time=0.5, country_code="pl")
        socks5_4 = m.acquire(max_connect_time=0.0001)
        assert socks5_1.id == 4
        assert socks5_2.id == 3
        assert socks5_3 is None
        assert socks5_4 is None

    def test_add(self):
        create_cwd(path=cwd())
        m = Manager()
        res = m.add("8.8.8.8", 1337, description="Many wow")
        assert res.id == 1
        assert res.host == "8.8.8.8"
        assert res.port == 1337
        assert res.country == "United States"
        assert res.country_code == "US"
        assert res.username is None
        assert res.password is None
        all_socks = self.db.list_socks5()
        assert len(all_socks) == 1
        all_socks[0].description == "Many wow"

    def test_add_invalid_entry(self):
        create_cwd(path=cwd())
        m = Manager()
        with pytest.raises(Socks5CreationError):
            m.add("u8a8asd8a8sdad.cheese", -9812381, description="Many wow")

    def test_add_hostname(self):
        create_cwd(path=cwd())
        m = Manager()
        res = m.add("example.com", 1337)
        assert res.id == 1
        assert res.country == "United States"
        assert res.country_code == "US"
        assert res.city == "Norwell"
        assert res.host == "example.com"
        assert res.port == 1337

    def test_add_invalid_auth_usage(self):
        create_cwd(path=cwd())
        m = Manager()
        with pytest.raises(Socks5CreationError):
            m.add("example.com", 1337, username="******")
        with pytest.raises(Socks5CreationError):
            m.add("example.com", 1337, password="******")

    def test_add_auth(self):
        create_cwd(path=cwd())
        m = Manager()
        res = m.add("example.com", 1337, username="******", password="******")
        assert res.id == 1

    def test_add_duplicate(self):
        create_cwd(path=cwd())
        m = Manager()
        m.add("example.com", 1337)
        with pytest.raises(Socks5CreationError):
            m.add("example.com", 1337)

    def test_bulk_add(self):
        create_cwd(path=cwd())
        servers = [
            {
                "host": "8.8.8.8",
                "port": 4242
            },
            {
                "host": "example.com",
                "port": 9133
            }
        ]
        m = Manager()
        assert m.bulk_add(servers) == 2
        allsocks = self.db.list_socks5()
        assert len(allsocks) == 2
        assert allsocks[0].country == "United States"
        assert allsocks[1].country == "United States"

    def test_bulk_add_missinginfo(self):
        create_cwd(path=cwd())
        servers = [
            {
                "port": 4242
            },
            {
                "host": "example.com",
                "port": 9133
            }
        ]
        m = Manager()
        assert m.bulk_add(servers) == 1

    def test_bulk_add_faultyinfo(self):
        create_cwd(path=cwd())
        servers = [
            {
                "host": "98asdj9a8sdj9adsuiuiuiasd.cheese",
                "port": 4242
            },
            {
                "host": "example.com",
                "port": 9133
            }
        ]
        m = Manager()
        assert m.bulk_add(servers) == 1

    def test_bulk_add_invalid_auth_usage(self):
        create_cwd(path=cwd())
        m = Manager()
        servers1 = [
            {
                "host": "8.8.8.8",
                "port": 4242,
                "password": "******"
            },
            {
                "host": "example.com",
                "port": 9133
            }
        ]
        servers2 = [
            {
                "host": "8.8.8.8",
                "port": 4242
            },
            {
                "host": "example.com",
                "username": "******",
                "port": 9133
            }
        ]

        t1 = m.bulk_add(servers1)
        t2 = m.bulk_add(servers2)
        assert t1 == 1
        assert t2 == 1

    def test_bulk_add_strport(self):
        create_cwd(path=cwd())
        servers = [
            {
                "host": "8.8.8.8",
                "port": "4242"
            },
            {
                "host": "example.com",
                "port": 9133
            }
        ]
        m = Manager()
        assert m.bulk_add(servers) == 2
        assert self.db.view_socks5(host="8.8.8.8", port=4242).port == 4242

    def test_bulk_add_description(self):
        create_cwd(path=cwd())
        servers = [
            {
                "host": "8.8.8.8",
                "port": 4242
            },
            {
                "host": "example.com",
                "port": 9133
            }
        ]
        m = Manager()
        assert m.bulk_add(servers, description="Very wow") == 2
        s = self.db.view_socks5(host="8.8.8.8", port=4242)
        assert s.port == 4242
        assert s.description == "Very wow"

    def test_bulk_add_auth(self):
        create_cwd(path=cwd())
        servers = [
            {
                "host": "8.8.8.8",
                "port": 4242,
                "username": "******",
                "password": "******"
            },
            {
                "host": "example.com",
                "port": 9133
            }
        ]
        m = Manager()
        assert m.bulk_add(servers) == 2
        allsocks = self.db.list_socks5()
        assert allsocks[0].username == "hello"
        assert allsocks[0].password == "bye"

    def test_bulk_add_nonew(self):
        create_cwd(path=cwd())
        servers = [
            {
                "host": "Shouldnotexistsstuff789as7h8asdj8asd.tosti",
                "port": 4242
            },
            {
                "host": "example.com",
                "port": None
            }
        ]
        m = Manager()
        with pytest.raises(Socks5CreationError):
            m.bulk_add(servers)

    def test_delete_socks5(self):
        self.db.add_socks5(
            "8.8.8.8", 1337, "germany", "Unknown",
            city="Unknown", operational=False, username="******",
            password="******", description="Such wow, many socks5"
        )
        assert self.db.view_socks5(1).id == 1
        m = Manager()
        m.delete(1)
        assert self.db.view_socks5(1) is None

    def test_delete_all(self):
        for x in range(10):
            self.db.add_socks5(
                "8.8.8.8", x, "germany", "Unknown",
                city="Unknown", operational=False, username="******",
                password="******", description="Such wow, many socks5"
            )

        assert len(self.db.list_socks5()) == 10
        m = Manager()
        m.delete_all()
        assert len(self.db.list_socks5()) == 0

    def test_list_socks5(self):
        for x in range(10):
            self.db.add_socks5(
                "8.8.8.8", x, "germany", "Unknown",
                city="Unknown", operational=False, username="******",
                password="******", description="Such wow, many socks5"
            )
        m = Manager()
        all_socks = m.list_socks5()
        assert len(all_socks) == 10
        for s in all_socks:
            assert isinstance(s, Socks5)
Beispiel #2
0
class TestSocks5(object):
    def setup_class(self):
        self.tempfile = CleanedTempFile()

    def teardown_class(self):
        self.tempfile.clean()

    def setup(self):
        set_cwd(self.tempfile.mkdtemp())
        self.db = Database()
        self.db.connect(create=True)

    def test_add_socks5(self):
        s1id = self.db.add_socks5(
            "9.8.8.8", 4141, "Germany", "DE", city="Berlin",
            description="Very wow"*100
        )
        s2id = self.db.add_socks5(
            "10.8.8.8", 4242, "Germany", "DE"
        )
        s3id = self.db.add_socks5(
            "11.8.8.8", 4343, "Germany", "DE",  username="******",
            password="******"
        )
        s4id = self.db.add_socks5(
            "12.8.8.8", 4444, "Germany", "DE",  username="******",
            password="******", operational=True
        )

        s1 = self.db.view_socks5(s1id)
        s2 = self.db.view_socks5(s2id)
        s3 = self.db.view_socks5(s3id)
        s4 = self.db.view_socks5(s4id)
        assert s1id is not None
        assert isinstance(s2id, int)
        assert s1.host == "9.8.8.8"
        assert s1.city == "Berlin"
        assert s1.description == "Very wow"*100
        assert not s1.operational
        assert s2.host == "10.8.8.8"
        assert s2.port == 4242
        assert s2.country == "Germany"
        assert s2.country_code == "DE"
        assert s3.host == "11.8.8.8"
        assert s3.username == "test"
        assert s3.password == "pass"
        assert s3.port == 4343
        assert s4.host == "12.8.8.8"
        assert s4.port == 4444
        assert s4.operational

    def test_add_socks5_error(self):
        with pytest.raises(Socks5manDatabaseError):
            self.db.add_socks5(
                "8.8.8.8", 7121, "Germany", "DE", operational="veryboolean"
            )

    def test_add_socks5_invalid_country(self):
        with pytest.raises(Socks5manDatabaseError):
            self.db.add_socks5(
                "8.8.8.8", 7121, None, "DE"
            )
    def test_add_socks5_invalid_country_code(self):
        with pytest.raises(Socks5manDatabaseError):
            self.db.add_socks5(
                "8.8.8.8", 7121, "Germany", None
            )
    def test_add_socks5_invalid_host(self):
        with pytest.raises(Socks5manDatabaseError):
            self.db.add_socks5(
                None, 7121, "Germany", "DE"
            )
    def test_add_socks5_invalid_port(self):
        with pytest.raises(Socks5manDatabaseError):
            self.db.add_socks5(
                "8.8.8.8", None, "Germany", "DE"
            )

    def test_repr(self):
        s1id = self.db.add_socks5(
            "9.8.8.8", 4141, "Germany", "DE", city="Berlin",
            description="Very wow"*100
        )
        s2id = self.db.add_socks5(
            "12.8.8.8", 4444, "Germany", "DE",  username="******",
            password="******", operational=True
        )
        s = self.db.view_socks5(s1id)
        s2 = self.db.view_socks5(s2id)
        assert repr(s) == "<Socks5(host=9.8.8.8, port=4141, country=Germany, authenticated=False)>"
        assert repr(s2) == "<Socks5(host=12.8.8.8, port=4444, country=Germany, authenticated=True)>"

    def test_remove_socks5(self):
        id1 = self.db.add_socks5(
            "9.8.8.8", 4141, "Germany", "DE", city="Berlin",
            description="Very wow" * 100
        )
        assert self.db.view_socks5(id1).id == id1
        self.db.remove_socks5(id1)
        assert self.db.view_socks5(id1) is None

    def test_list_socks5_operational(self):
        s1id = self.db.add_socks5(
            "9.8.8.8", 4141, "Germany", "DE", city="Berlin",
            operational=False
        )
        s2id = self.db.add_socks5(
            "9.8.8.8", 4141, "France", "FR", city="Paris",
            operational=True
        )

        res = self.db.list_socks5(operational=True)
        assert len(res) == 1
        assert res[0].id == s2id

        res = self.db.list_socks5(operational=False)
        assert len(res) == 1
        assert res[0].id == s1id

        res = self.db.list_socks5()
        assert len(res) == 2

    def test_list_socks5_country(self):
        s1id = self.db.add_socks5(
            "9.8.8.8", 4141, "Germany", "DE", city="Berlin",
            operational=False
        )
        s2id = self.db.add_socks5(
            "9.8.8.8", 4141, "France", "FR", city="Paris",
            operational=True
        )

        res = self.db.list_socks5(country="france")
        assert len(res) == 1
        assert res[0].id == s2id

        res = self.db.list_socks5(country="FranCE")
        assert len(res) == 1
        assert res[0].id == s2id

    def test_list_socks5_country_code(self):
        s1id = self.db.add_socks5(
            "9.8.8.8", 4141, "Germany", "DE", city="Berlin",
            operational=False
        )
        s2id = self.db.add_socks5(
            "9.8.8.8", 4141, "France", "FR", city="Paris",
            operational=True
        )

        res = self.db.list_socks5(country_code="fr")
        assert len(res) == 1
        assert res[0].id == s2id

        res = self.db.list_socks5(country_code="FR")
        assert len(res) == 1
        assert res[0].id == s2id

    def test_list_socks5_city(self):
        s1id = self.db.add_socks5(
            "9.8.8.8", 4141, "Germany", "DE", city="Berlin",
            operational=False
        )
        s2id = self.db.add_socks5(
            "9.8.8.8", 4141, "France", "FR", city="Paris",
            operational=True
        )
        s3id = self.db.add_socks5(
            "9.10.8.8", 4141, "France", "FR", city="Paris",
            operational=True
        )

        res = self.db.list_socks5(city="berlin")
        assert len(res) == 1
        assert res[0].id == s1id

        res = self.db.list_socks5(city="paris")
        assert len(res) == 2
        assert res[0].id == s2id
        assert res[1].id == s3id

    def test_list_socks5_host(self):
        s1id = self.db.add_socks5(
            "9.8.8.8", 4141, "Germany", "DE", city="Berlin",
            operational=False
        )
        s2id = self.db.add_socks5(
            "9.8.8.8", 4141, "France", "FR", city="Paris",
            operational=True
        )
        s3id = self.db.add_socks5(
            "9.10.8.8", 4141, "France", "FR", city="Paris",
            operational=True
        )

        res = self.db.list_socks5(host="9.8.8.8")
        assert len(res) == 2
        assert res[0].id == s1id
        assert res[1].id == s2id

        res = self.db.list_socks5(host="1.2.3.4")
        assert len(res) == 0

    def test_list_socks5_host_listarg(self):
        s1id = self.db.add_socks5(
            "9.8.8.8", 4141, "Germany", "DE", city="Berlin",
            operational=False
        )
        s2id = self.db.add_socks5(
            "9.8.8.8", 4141, "France", "FR", city="Paris",
            operational=True
        )
        s3id = self.db.add_socks5(
            "9.10.8.8", 4141, "France", "FR", city="Paris",
            operational=True
        )
        s4id = self.db.add_socks5(
            "10.10.8.8", 4141, "France", "FR", city="Paris",
            operational=True
        )

        res = self.db.list_socks5(host=["9.8.8.8", "9.10.8.8"])
        assert len(res) == 3
        assert res[0].id == s1id
        assert res[1].id == s2id
        assert res[2].id == s3id

    def test_view_socks5(self):
        s1id = self.db.add_socks5(
            "9.8.8.8", 4141, "Germany", "DE", city="Berlin",
            operational=False
        )
        assert self.db.view_socks5(s1id).host == "9.8.8.8"
        assert self.db.view_socks5(2) is None
        assert self.db.view_socks5(host="9.8.8.8", port=4141).host == "9.8.8.8"

        with pytest.raises(Socks5manDatabaseError):
            self.db.view_socks5(host="9.8.8.8")
        with pytest.raises(Socks5manDatabaseError):
            self.db.view_socks5(port=4141)
        with pytest.raises(Socks5manDatabaseError):
            self.db.view_socks5()

    def test_find_socks5(self):
        s1id = self.db.add_socks5(
            "9.8.8.8", 4141, "Germany", "DE", city="Berlin",
            operational=False
        )
        s2id = self.db.add_socks5(
            "9.8.8.8", 4141, "France", "FR", city="Paris",
            operational=True
        )
        s3id = self.db.add_socks5(
            "9.8.8.8", 4241, "France", "FR", city="Paris",
            operational=True
        )
        assert self.db.find_socks5()[0].id == s2id
        assert self.db.find_socks5()[0].id == s3id
        assert self.db.find_socks5()[0].id == s2id
        assert len(self.db.find_socks5(limit=1000)) == 2

    def test_find_socks5_no_usage_update(self):
        s1id = self.db.add_socks5(
            "9.8.8.8", 4141, "Germany", "DE", city="Berlin",
            operational=False
        )
        s2id = self.db.add_socks5(
            "9.8.8.8", 4141, "France", "FR", city="Paris",
            operational=True
        )
        s3id = self.db.add_socks5(
            "9.8.8.8", 4241, "France", "FR", city="Paris",
            operational=True
        )
        assert self.db.find_socks5(update_usage=False)[0].id == s2id
        assert self.db.find_socks5(update_usage=False)[0].id == s2id
        assert self.db.find_socks5(update_usage=False)[0].id == s2id
        assert len(self.db.find_socks5(limit=1000)) == 2

    def test_find_socks5_country(self):
        self.db.add_socks5(
            "9.8.8.8", 4141, "Germany", "DE", city="Berlin",
            operational=True
        )
        self.db.add_socks5(
            "9.8.8.8", 4141, "Belgium", "BE", operational=True
        )
        self.db.add_socks5(
            "9.8.8.8", 4141, "France", "FR", city="Paris",
            operational=False
        )
        res = self.db.find_socks5(country="germany")
        assert len(res) == 1
        assert res[0].country == "Germany"
        assert len(self.db.find_socks5(country="france")) == 0

    def test_find_socks5_country_code(self):
        self.db.add_socks5(
            "9.8.8.8", 4141, "Germany", "DE", city="Berlin",
            operational=True
        )
        self.db.add_socks5(
            "9.8.8.8", 4141, "Belgium", "BE", operational=True
        )
        self.db.add_socks5(
            "9.8.8.8", 4141, "France", "FR", city="Paris",
            operational=False
        )
        res = self.db.find_socks5(country_code="de")
        assert len(res) == 1
        assert res[0].country == "Germany"
        assert len(self.db.find_socks5(country="fr")) == 0

    def test_find_socks5_city(self):
        id1 = self.db.add_socks5(
            "9.8.8.8", 4141, "Germany", "DE", city="Berlin",
            operational=True
        )
        id2 = self.db.add_socks5(
            "10.8.8.8", 4141, "Germany", "DE", city="Berlin",
            operational=True
        )
        self.db.add_socks5(
            "9.8.8.8", 4141, "Belgium", "BE", operational=True
        )
        self.db.add_socks5(
            "9.8.8.8", 4141, "France", "FR", operational=True, city="Paris"
        )

        assert self.db.find_socks5(city="berlin")[0].id == id1
        assert self.db.find_socks5(city="berlin")[0].id == id2
        assert self.db.find_socks5(city="berlin")[0].id == id1
        assert len(self.db.find_socks5(city="amsterdam")) == 0

    def test_find_socks5_mbpsdown(self):
        id1 = self.db.add_socks5(
            "9.8.8.8", 4141, "Germany", "DE",
            operational=True
        )
        id2 = self.db.add_socks5(
            "10.8.8.8", 4141, "Germany", "DE",
            operational=True
        )
        id3 = self.db.add_socks5(
            "9.8.8.8", 4141, "Belgium", "BE", operational=True
        )
        id4 = self.db.add_socks5(
            "9.8.8.8", 4141, "France", "FR", operational=True
        )

        self.db.set_approx_bandwidth(id1, 10.1)
        self.db.set_approx_bandwidth(id2, 32.183)
        self.db.set_approx_bandwidth(id3, 0.148)

        assert self.db.find_socks5(min_mbps_down=15.7)[0].id == id2
        assert self.db.find_socks5(min_mbps_down=10)[0].id == id1
        assert self.db.find_socks5(min_mbps_down=10)[0].id == id2
        assert self.db.find_socks5(min_mbps_down=40) == []

    def test_find_socks5_conntime(self):
        id1 = self.db.add_socks5(
            "9.8.8.8", 4141, "Germany", "DE",
            operational=True
        )
        id2 = self.db.add_socks5(
            "10.8.8.8", 4141, "Germany", "DE",
            operational=True
        )
        id3 = self.db.add_socks5(
            "9.8.8.8", 4141, "Belgium", "BE", operational=True
        )
        id4 = self.db.add_socks5(
            "9.8.8.8", 4141, "France", "FR", operational=True
        )

        self.db.set_connect_time(id1, 0.53)
        self.db.set_connect_time(id2, 0.043)
        self.db.set_connect_time(id3, 0.44)

        assert self.db.find_socks5(max_connect_time=0.5)[0].id == id2
        assert self.db.find_socks5(max_connect_time=0.5)[0].id == id3
        assert self.db.find_socks5(max_connect_time=0.5)[0].id == id2
        assert self.db.find_socks5(max_connect_time=0.001) == []

    def test_bulk_add(self):
        s = [
            {
                "host": "8.8.8.8",
                "port": 4242,
                "country": "ads",
                "country_code": "ad"
            },
            {
                "host": "example.com",
                "port": 9133,
                "country": "ads",
                "country_code": "ad"
            }
        ]
        assert len(self.db.list_socks5()) == 0
        self.db.bulk_add_socks5(s)
        assert len(self.db.list_socks5()) == 2

    def test_bulk_add_missing_fields(self):
        s = [
            {
                "host": "8.8.8.8",
                "port": 4242,
                "country": "ads"
            },
            {
                "host": "example.com",
                "port": 9133,
                "country": "ads",
                "country_code": "ad"
            }
        ]
        with pytest.raises(Socks5manDatabaseError):
            self.db.bulk_add_socks5(s)

    def test_set_operational(self):
        id1 = self.db.add_socks5(
            "9.8.8.8", 4141, "Germany", "DE",
            operational=False
        )
        assert not self.db.view_socks5(id1).operational
        self.db.set_operational(id1, True)
        s2 = self.db.view_socks5(id1)
        assert s2.operational
        time.sleep(0.01)
        self.db.set_operational(id1, False)
        s3 = self.db.view_socks5(id1)
        assert not s3.operational
        assert s3.last_check > s2.last_check

    def test_set_operational_nonexist(self):
        # Should not raise exception
        self.db.set_operational(8127313, True)

    def test_set_connect_time(self):
        id1 = self.db.add_socks5(
            "9.8.8.8", 4141, "Germany", "DE",
            operational=False
        )
        assert self.db.view_socks5(id1).connect_time is None
        self.db.set_connect_time(id1, 0.5)
        assert self.db.view_socks5(id1).connect_time == 0.5

    def test_set_bandwidth(self):
        id1 = self.db.add_socks5(
            "9.8.8.8", 4141, "Germany", "DE",
            operational=False
        )
        assert self.db.view_socks5(id1).bandwidth is None
        self.db.set_approx_bandwidth(id1, 10.24)
        assert self.db.view_socks5(id1).bandwidth == 10.24

    def test_delete(self):
        for x in range(25):
            self.db.add_socks5(
                "9.8.8.8", x, "Germany", "DE",
                operational=False
            )
        assert len(self.db.list_socks5()) == 25
        self.db.delete_all_socks5()
        assert len(self.db.list_socks5()) == 0

    def test_bulk_delete_socks5(self):
        ids = []
        for x in range(25):
            i = self.db.add_socks5(
                "9.8.8.8", x, "Germany", "DE",
                operational=False
            )
            if x <= 12:
                ids.append(i)
        assert len(self.db.list_socks5()) == 25
        self.db.bulk_delete_socks5(ids)
        assert len(self.db.list_socks5()) == 12
        for i in ids:
            assert self.db.view_socks5(socks5_id=i) is None

    def test_big_bulk_delete(self):
        bulk_socks = [{
            "host": "8.8.8.8",
            "port": 4242,
            "country": "Germany",
            "country_code": "DE"
        } for x in range(2000)]

        self.db.bulk_add_socks5(bulk_socks)
        id1 = self.db.add_socks5(
            "9.8.8.8", 4242, "Germany", "DE",
            operational=True
        )
        assert len(self.db.list_socks5()) == 2001
        self.db.bulk_delete_socks5(
            [s.id for s in self.db.list_socks5(operational=False)]
        )
        workingsocks = self.db.list_socks5()
        assert len(workingsocks) == 1
        assert workingsocks[0].operational

    def test_update_geoinfo(self):
        id1 = self.db.add_socks5(
            "9.8.8.8", 4242, "Germany", "DE",
            operational=False
        )
        id2 = self.db.add_socks5(
            "9.8.8.8", 4243, "Germany", "DE",
            operational=False
        )
        s = self.db.view_socks5(id1)
        assert s.country == "Germany"
        assert s.city is None
        assert s.country_code == "DE"
        self.db.update_geoinfo(
            id1, country="France", country_code="FR", city="Paris"
        )
        s2 = self.db.view_socks5(id1)
        assert s2.country == "France"
        assert s2.city == "Paris"
        assert s2.country_code == "FR"
        s3 = self.db.view_socks5(id2)
        assert s3.country == "Germany"
        assert s3.city is None
        assert s3.country_code == "DE"

    def test_update_geoninfo_invalid_country(self):
        id1 = self.db.add_socks5(
            "9.8.8.8", 4242, "Germany", "DE",
            operational=False
        )
        with pytest.raises(Socks5manDatabaseError):
            self.db.update_geoinfo(
                id1, country=None, country_code="FR", city="Paris"
            )

    def test_update_geoninfo_invalid_country_code(self):
        id1 = self.db.add_socks5(
            "9.8.8.8", 4242, "Germany", "DE",
            operational=False
        )
        with pytest.raises(Socks5manDatabaseError):
            self.db.update_geoinfo(
                id1, country="France", country_code=None, city="Paris"
            )

    def test_schema_latest_version(self):
        ses = self.db.Session()
        try:
            v = ses.query(AlembicVersion.version_num).first()
            assert v.version_num == SCHEMA_VERSION
        finally:
            ses.close()

    def test_db_migratable_true(self):
        ses = self.db.Session()
        try:
            v = ses.query(AlembicVersion).first()
            v.version_num = "sdfsdfsf"
            ses.add(v)
            ses.commit()
        finally:
            ses.close()

        assert self.db.db_migratable()

    def test_db_migratable_false(self):
        assert not self.db.db_migratable()
Beispiel #3
0
class TestManager(object):

    def setup_class(self):
        self.tempfile = CleanedTempFile()

    def teardown_class(self):
        self.tempfile.clean()

    def setup(self):
        set_cwd(self.tempfile.mkdtemp())
        self.db = Database()
        self.db.connect(create=True)

    def test_acquire(self):
        past = datetime.datetime.now()
        for c in ["United States", "China", "Germany"]:
            self.db.add_socks5(
                "8.8.8.8", 1337, c, "Unknown",
                city="Unknown", operational=True, username="******",
                password="******", description="Such wow, many socks5"
            )
        m = Manager()
        socks5_1 = m.acquire()
        socks5_2 = m.acquire()
        socks5_3 = m.acquire()
        socks5_4 = m.acquire()
        assert socks5_1.id == 1
        assert socks5_1.last_use > past
        assert socks5_2.id == 2
        assert socks5_2.last_use > past
        assert socks5_3.id == 3
        assert socks5_3.last_use > past
        assert socks5_4.id == 1
        assert socks5_4.last_use > socks5_1.last_use

    def test_acquire_country(self):
        for c in ["United States", "China", "Germany"]:
            self.db.add_socks5(
                "8.8.8.8", 1337, c, "Unknown",
                city="Unknown", operational=True, username="******",
                password="******", description="Such wow, many socks5"
            )
        m = Manager()
        socks5_1 = m.acquire(country="germany")
        assert socks5_1.id == 3
        assert socks5_1.country == "Germany"
        socks5_2 = m.acquire(country="france")
        assert socks5_2 is None

    def test_acquire_no_operational(self):
        self.db.add_socks5(
            "8.8.8.8", 1337, "germany", "Unknown",
            city="Unknown", operational=False, username="******",
            password="******", description="Such wow, many socks5"
        )
        self.db.add_socks5(
            "8.8.8.8", 1337, "china", "Unknown",
            city="Unknown", operational=False, username="******",
            password="******", description="Such wow, many socks5"
        )
        m = Manager()
        socks5_1 = m.acquire()
        assert socks5_1 is None
        socks5_2 = m.acquire(country="china")
        assert socks5_2 is None

        self.db.add_socks5(
            "8.8.8.8", 1337, "france", "Unknown",
            city="Unknown", operational=True, username="******",
            password="******", description="Such wow, many socks5"
        )
        socks5_3 = m.acquire()
        assert socks5_3.id == 3

    def test_acquire_city(self):
        for c in ["Dogedown", "Amsterdam", "Tallinn"]:
            self.db.add_socks5(
                "8.8.8.8", 1337, "Unknown", "Unknown",
                city=c, operational=True, username="******",
                password="******", description="Such wow, many socks5"
            )
        m = Manager()
        socks5_1 = m.acquire(city="tallinn")
        assert socks5_1.id == 3
        assert socks5_1.city == "Tallinn"
        socks5_2 = m.acquire(city="Nowhere")
        assert socks5_2 is None

    def test_acquire_countrycode(self):
        for c in ["DE", "CA", "NL"]:
            self.db.add_socks5(
                "8.8.8.8", 1337, "Unknown", c,
                city="Unknown", operational=True, username="******",
                password="******", description="Such wow, many socks5"
            )
        m = Manager()
        socks5_1 = m.acquire(country_code="ca")
        assert socks5_1.id == 2
        assert socks5_1.country_code == "CA"
        socks5_2 = m.acquire(country_code="FR")
        assert socks5_2 is None

    def test_acquire_mbps(self):
        for c in ["DE", "CA", "NL", "NL", "PL"]:
            self.db.add_socks5(
                "8.8.8.8", 1337, "Unknown", c,
                city="Unknown", operational=True, username="******",
                password="******", description="Such wow, many socks5"
            )
        self.db.set_approx_bandwidth(2, 5.33)
        self.db.set_approx_bandwidth(3, 19.811)
        self.db.set_approx_bandwidth(4, 7.134)
        self.db.set_approx_bandwidth(5, 28.134)

        m = Manager()
        socks5_1 = m.acquire(min_mbps_down=4)
        socks5_2 = m.acquire(min_mbps_down=4, country_code="nl")
        socks5_3 = m.acquire(min_mbps_down=1, country_code="de")
        assert socks5_1.id == 5
        assert socks5_2.id == 3
        assert socks5_3 is None

    def test_acquire_conntime(self):
        for c in ["DE", "CA", "NL", "NL", "PL"]:
            self.db.add_socks5(
                "8.8.8.8", 1337, "Unknown", c,
                city="Unknown", operational=True, username="******",
                password="******", description="Such wow, many socks5"
            )
        self.db.set_connect_time(2, 0.02)
        self.db.set_connect_time(3, 0.1)
        self.db.set_connect_time(4, 0.0054)
        self.db.set_connect_time(5, 1.3)

        m = Manager()
        socks5_1 = m.acquire(max_connect_time=0.01)
        socks5_2 = m.acquire(max_connect_time=0.2, country_code="nl")
        socks5_3 = m.acquire(max_connect_time=0.5, country_code="pl")
        socks5_4 = m.acquire(max_connect_time=0.0001)
        assert socks5_1.id == 4
        assert socks5_2.id == 3
        assert socks5_3 is None
        assert socks5_4 is None

    def test_add(self):
        create_cwd(path=cwd())
        m = Manager()
        res = m.add("8.8.8.8", 1337, description="Many wow")
        assert res.id == 1
        assert res.host == "8.8.8.8"
        assert res.port == 1337
        assert res.country == "United States"
        assert res.country_code == "US"
        assert res.username is None
        assert res.password is None
        all_socks = self.db.list_socks5()
        assert len(all_socks) == 1
        assert all_socks[0].id == 1
        assert all_socks[0].host == "8.8.8.8"
        assert all_socks[0].port == 1337
        assert all_socks[0].username is None
        assert all_socks[0].password is None
        assert all_socks[0].country == "United States"
        assert all_socks[0].country_code == "US"

    def test_add_description(self):
        create_cwd(path=cwd())
        m = Manager()
        res = m.add("8.8.8.8", 1337, description="Many wow")
        assert res.id == 1
        assert res.host == "8.8.8.8"
        assert res.port == 1337
        all_socks = self.db.list_socks5()
        assert len(all_socks) == 1
        assert all_socks[0].id == 1
        assert all_socks[0].host == "8.8.8.8"
        assert all_socks[0].port == 1337
        assert all_socks[0].description == "Many wow"

    def test_add_invalid_entry(self):
        create_cwd(path=cwd())
        m = Manager()
        with pytest.raises(Socks5CreationError):
            m.add("u8a8asd8a8sdad.cheese", -9812381, description="Many wow")

    def test_add_hostname(self):
        create_cwd(path=cwd())
        m = Manager()
        res = m.add("example.com", 1337)
        assert res.id == 1
        assert res.country == "United States"
        assert res.country_code == "US"
        assert res.city == "Norwell"
        assert res.host == "example.com"
        assert res.port == 1337
        all_socks = self.db.list_socks5()
        assert len(all_socks) == 1
        assert all_socks[0].host == "example.com"
        assert all_socks[0].port == 1337
        assert all_socks[0].country == "United States"
        assert all_socks[0].country_code == "US"

    def test_add_invalid_auth_usage(self):
        create_cwd(path=cwd())
        m = Manager()
        with pytest.raises(Socks5CreationError):
            m.add("example.com", 1337, username="******")
        with pytest.raises(Socks5CreationError):
            m.add("example.com", 1337, password="******")

    def test_add_auth(self):
        create_cwd(path=cwd())
        m = Manager()
        res = m.add("example.com", 1337, username="******", password="******")
        assert res.id == 1
        all_socks = self.db.list_socks5()
        assert len(all_socks) == 1
        assert all_socks[0].host == "example.com"
        assert all_socks[0].port == 1337
        assert all_socks[0].username == "Hello"
        assert all_socks[0].password == "Bye"

    def test_add_dnsport(self):
        create_cwd(path=cwd())
        m = Manager()
        res = m.add("example.com", 1337, dnsport=5050)
        assert res.id == 1
        all_socks = self.db.list_socks5()
        assert len(all_socks) == 1
        assert all_socks[0].dnsport == 5050
        assert all_socks[0].host == "example.com"
        assert all_socks[0].port == 1337

    def test_add_duplicate(self):
        create_cwd(path=cwd())
        m = Manager()
        m.add("example.com", 1337)
        with pytest.raises(Socks5CreationError):
            m.add("example.com", 1337)

    def test_bulk_add(self):
        create_cwd(path=cwd())
        servers = [
            {
                "host": "8.8.8.8",
                "port": 4242
            },
            {
                "host": "example.com",
                "port": 9133
            }
        ]
        m = Manager()
        assert m.bulk_add(servers) == 2
        allsocks = self.db.list_socks5()
        assert len(allsocks) == 2
        assert allsocks[0].country == "United States"
        assert allsocks[1].country == "United States"

    def test_bulk_add_missinginfo(self):
        create_cwd(path=cwd())
        servers = [
            {
                "port": 4242
            },
            {
                "host": "example.com",
                "port": 9133
            }
        ]
        m = Manager()
        assert m.bulk_add(servers) == 1

    def test_bulk_add_faultyinfo(self):
        create_cwd(path=cwd())
        servers = [
            {
                "host": "98asdj9a8sdj9adsuiuiuiasd.cheese",
                "port": 4242
            },
            {
                "host": "example.com",
                "port": 9133
            }
        ]
        m = Manager()
        assert m.bulk_add(servers) == 1

    def test_bulk_add_invalid_auth_usage(self):
        create_cwd(path=cwd())
        m = Manager()
        servers1 = [
            {
                "host": "8.8.8.8",
                "port": 4242,
                "password": "******"
            },
            {
                "host": "example.com",
                "port": 9133
            }
        ]
        servers2 = [
            {
                "host": "8.8.8.8",
                "port": 4242
            },
            {
                "host": "example.com",
                "username": "******",
                "port": 9133
            }
        ]

        t1 = m.bulk_add(servers1)
        t2 = m.bulk_add(servers2)
        assert t1 == 1
        assert t2 == 1

    def test_bulk_add_strport(self):
        create_cwd(path=cwd())
        servers = [
            {
                "host": "8.8.8.8",
                "port": "4242"
            },
            {
                "host": "example.com",
                "port": 9133
            }
        ]
        m = Manager()
        assert m.bulk_add(servers) == 2
        assert self.db.view_socks5(host="8.8.8.8", port=4242).port == 4242

    def test_bulk_add_description(self):
        create_cwd(path=cwd())
        servers = [
            {
                "host": "8.8.8.8",
                "port": 4242
            },
            {
                "host": "example.com",
                "port": 9133
            }
        ]
        m = Manager()
        assert m.bulk_add(servers, description="Very wow") == 2
        s = self.db.view_socks5(host="8.8.8.8", port=4242)
        assert s.port == 4242
        assert s.description == "Very wow"

    def test_bulk_add_auth(self):
        create_cwd(path=cwd())
        servers = [
            {
                "host": "8.8.8.8",
                "port": 4242,
                "username": "******",
                "password": "******"
            },
            {
                "host": "example.com",
                "port": 9133
            }
        ]
        m = Manager()
        assert m.bulk_add(servers) == 2
        allsocks = self.db.list_socks5()
        assert allsocks[0].username == "hello"
        assert allsocks[0].password == "bye"

    def test_bulk_dnsport(self):
        create_cwd(path=cwd())
        servers = [
            {
                "host": "8.8.8.8",
                "port": 4242,
                "dnsport": 5050
            },
            {
                "host": "example.com",
                "port": 9133
            }
        ]
        m = Manager()
        assert m.bulk_add(servers) == 2
        allsocks = self.db.list_socks5()
        assert allsocks[0].dnsport == 5050

    def test_bulk_add_nonew(self):
        create_cwd(path=cwd())
        servers = [
            {
                "host": "Shouldnotexistsstuff789as7h8asdj8asd.tosti",
                "port": 4242
            },
            {
                "host": "example.com",
                "port": None
            }
        ]
        m = Manager()
        with pytest.raises(Socks5CreationError):
            m.bulk_add(servers)

    def test_delete_socks5(self):
        self.db.add_socks5(
            "8.8.8.8", 1337, "germany", "Unknown",
            city="Unknown", operational=False, username="******",
            password="******", description="Such wow, many socks5"
        )
        assert self.db.view_socks5(1).id == 1
        m = Manager()
        m.delete(1)
        assert self.db.view_socks5(1) is None

    def test_delete_all(self):
        for x in range(10):
            self.db.add_socks5(
                "8.8.8.8", x, "germany", "Unknown",
                city="Unknown", operational=False, username="******",
                password="******", description="Such wow, many socks5"
            )

        assert len(self.db.list_socks5()) == 10
        m = Manager()
        m.delete_all()
        assert len(self.db.list_socks5()) == 0

    def test_list_socks5(self):
        for x in range(10):
            self.db.add_socks5(
                "8.8.8.8", x, "germany", "Unknown",
                city="Unknown", operational=False, username="******",
                password="******", description="Such wow, many socks5"
            )
        m = Manager()
        all_socks = m.list_socks5()
        assert len(all_socks) == 10
        for s in all_socks:
            assert isinstance(s, Socks5)

    def test_list_socks5_description(self):
        for x in range(3):
            self.db.add_socks5(
                "8.8.8.8", x, "germany", "Unknown",
                city="Unknown", operational=False, username="******",
                password="******", description="google dns"
            )
        for x in range(3):
            self.db.add_socks5(
                "1.1.1.1", x, "germany", "Unknown",
                city="Unknown", operational=False, username="******",
                password="******", description="cloudflare dns"
            )
        m = Manager()
        all_socks = m.list_socks5(description="google dns")
        assert len(all_socks) == 3
        for s in all_socks:
            assert isinstance(s, Socks5)

        all_socks = m.list_socks5(description="cloudflare dns")
        assert len(all_socks) == 3
        for s in all_socks:
            assert isinstance(s, Socks5)
Beispiel #4
0
class TestSocks5(object):
    def setup_class(self):
        self.tempfile = CleanedTempFile()

    def teardown_class(self):
        self.tempfile.clean()

    def setup(self):
        set_cwd(self.tempfile.mkdtemp())
        self.db = Database()
        self.db.connect(create=True)

    def test_add_socks5(self):
        s1id = self.db.add_socks5("9.8.8.8",
                                  4141,
                                  "Germany",
                                  "DE",
                                  city="Berlin",
                                  description="Very wow" * 100)
        s2id = self.db.add_socks5("10.8.8.8", 4242, "Germany", "DE")
        s3id = self.db.add_socks5("11.8.8.8",
                                  4343,
                                  "Germany",
                                  "DE",
                                  username="******",
                                  password="******",
                                  dnsport=5050)
        s4id = self.db.add_socks5("12.8.8.8",
                                  4444,
                                  "Germany",
                                  "DE",
                                  username="******",
                                  password="******",
                                  operational=True)

        s1 = self.db.view_socks5(s1id)
        s2 = self.db.view_socks5(s2id)
        s3 = self.db.view_socks5(s3id)
        s4 = self.db.view_socks5(s4id)
        assert s1id is not None
        assert isinstance(s2id, int)
        assert s1.host == "9.8.8.8"
        assert s1.city == "Berlin"
        assert s1.description == "Very wow" * 100
        assert not s1.operational
        assert s2.host == "10.8.8.8"
        assert s2.port == 4242
        assert s2.country == "Germany"
        assert s2.country_code == "DE"
        assert s3.host == "11.8.8.8"
        assert s3.username == "test"
        assert s3.password == "pass"
        assert s3.port == 4343
        assert s3.dnsport == 5050
        assert s4.host == "12.8.8.8"
        assert s4.port == 4444
        assert s4.operational

    def test_add_socks5_error(self):
        with pytest.raises(Socks5manDatabaseError):
            self.db.add_socks5("8.8.8.8",
                               7121,
                               "Germany",
                               "DE",
                               operational="veryboolean")

    def test_add_socks5_invalid_country(self):
        with pytest.raises(Socks5manDatabaseError):
            self.db.add_socks5("8.8.8.8", 7121, None, "DE")

    def test_add_socks5_invalid_country_code(self):
        with pytest.raises(Socks5manDatabaseError):
            self.db.add_socks5("8.8.8.8", 7121, "Germany", None)

    def test_add_socks5_invalid_host(self):
        with pytest.raises(Socks5manDatabaseError):
            self.db.add_socks5(None, 7121, "Germany", "DE")

    def test_add_socks5_invalid_port(self):
        with pytest.raises(Socks5manDatabaseError):
            self.db.add_socks5("8.8.8.8", None, "Germany", "DE")

    def test_repr(self):
        s1id = self.db.add_socks5("9.8.8.8",
                                  4141,
                                  "Germany",
                                  "DE",
                                  city="Berlin",
                                  description="Very wow Very wow")
        s2id = self.db.add_socks5("12.8.8.8",
                                  4444,
                                  "Germany",
                                  "DE",
                                  username="******",
                                  password="******",
                                  operational=True)
        s = self.db.view_socks5(s1id)
        s2 = self.db.view_socks5(s2id)
        assert repr(
            s
        ) == "<Socks5(host=9.8.8.8, port=4141, country=Germany, authenticated=False, description=Very wow Very wow)>"
        assert repr(
            s2
        ) == "<Socks5(host=12.8.8.8, port=4444, country=Germany, authenticated=True, description=None)>"

    def test_remove_socks5(self):
        id1 = self.db.add_socks5("9.8.8.8",
                                 4141,
                                 "Germany",
                                 "DE",
                                 city="Berlin",
                                 description="Very wow" * 100)
        assert self.db.view_socks5(id1).id == id1
        self.db.remove_socks5(id1)
        assert self.db.view_socks5(id1) is None

    def test_list_socks5_operational(self):
        s1id = self.db.add_socks5("9.8.8.8",
                                  4141,
                                  "Germany",
                                  "DE",
                                  city="Berlin",
                                  operational=False)
        s2id = self.db.add_socks5("9.8.8.8",
                                  4141,
                                  "France",
                                  "FR",
                                  city="Paris",
                                  operational=True)

        res = self.db.list_socks5(operational=True)
        assert len(res) == 1
        assert res[0].id == s2id

        res = self.db.list_socks5(operational=False)
        assert len(res) == 1
        assert res[0].id == s1id

        res = self.db.list_socks5()
        assert len(res) == 2

    def test_list_socks5_country(self):
        self.db.add_socks5("9.8.8.8",
                           4141,
                           "Germany",
                           "DE",
                           city="Berlin",
                           operational=False)
        s2id = self.db.add_socks5("9.8.8.8",
                                  4141,
                                  "France",
                                  "FR",
                                  city="Paris",
                                  operational=True)

        res = self.db.list_socks5(country="france")
        assert len(res) == 1
        assert res[0].id == s2id

        res = self.db.list_socks5(country="FranCE")
        assert len(res) == 1
        assert res[0].id == s2id

    def test_list_socks5_country_code(self):
        self.db.add_socks5("9.8.8.8",
                           4141,
                           "Germany",
                           "DE",
                           city="Berlin",
                           operational=False)
        s2id = self.db.add_socks5("9.8.8.8",
                                  4141,
                                  "France",
                                  "FR",
                                  city="Paris",
                                  operational=True)

        res = self.db.list_socks5(country_code="fr")
        assert len(res) == 1
        assert res[0].id == s2id

        res = self.db.list_socks5(country_code="FR")
        assert len(res) == 1
        assert res[0].id == s2id

    def test_list_socks5_city(self):
        s1id = self.db.add_socks5("9.8.8.8",
                                  4141,
                                  "Germany",
                                  "DE",
                                  city="Berlin",
                                  operational=False)
        s2id = self.db.add_socks5("9.8.8.8",
                                  4141,
                                  "France",
                                  "FR",
                                  city="Paris",
                                  operational=True)
        s3id = self.db.add_socks5("9.10.8.8",
                                  4141,
                                  "France",
                                  "FR",
                                  city="Paris",
                                  operational=True)

        res = self.db.list_socks5(city="berlin")
        assert len(res) == 1
        assert res[0].id == s1id

        res = self.db.list_socks5(city="paris")
        assert len(res) == 2
        assert res[0].id == s2id
        assert res[1].id == s3id

    def test_list_socks5_host(self):
        s1id = self.db.add_socks5("9.8.8.8",
                                  4141,
                                  "Germany",
                                  "DE",
                                  city="Berlin",
                                  operational=False)
        s2id = self.db.add_socks5("9.8.8.8",
                                  4141,
                                  "France",
                                  "FR",
                                  city="Paris",
                                  operational=True)
        self.db.add_socks5("9.10.8.8",
                           4141,
                           "France",
                           "FR",
                           city="Paris",
                           operational=True)

        res = self.db.list_socks5(host="9.8.8.8")
        assert len(res) == 2
        assert res[0].id == s1id
        assert res[1].id == s2id

        res = self.db.list_socks5(host="1.2.3.4")
        assert len(res) == 0

    def test_list_socks5_host_listarg(self):
        s1id = self.db.add_socks5("9.8.8.8",
                                  4141,
                                  "Germany",
                                  "DE",
                                  city="Berlin",
                                  operational=False)
        s2id = self.db.add_socks5("9.8.8.8",
                                  4141,
                                  "France",
                                  "FR",
                                  city="Paris",
                                  operational=True)
        s3id = self.db.add_socks5("9.10.8.8",
                                  4141,
                                  "France",
                                  "FR",
                                  city="Paris",
                                  operational=True)
        self.db.add_socks5("10.10.8.8",
                           4141,
                           "France",
                           "FR",
                           city="Paris",
                           operational=True)

        res = self.db.list_socks5(host=["9.8.8.8", "9.10.8.8"])
        assert len(res) == 3
        assert res[0].id == s1id
        assert res[1].id == s2id
        assert res[2].id == s3id

    def test_view_socks5(self):
        s1id = self.db.add_socks5("9.8.8.8",
                                  4141,
                                  "Germany",
                                  "DE",
                                  city="Berlin",
                                  operational=False)
        assert self.db.view_socks5(s1id).host == "9.8.8.8"
        assert self.db.view_socks5(2) is None
        assert self.db.view_socks5(host="9.8.8.8", port=4141).host == "9.8.8.8"

        with pytest.raises(Socks5manDatabaseError):
            self.db.view_socks5(host="9.8.8.8")
        with pytest.raises(Socks5manDatabaseError):
            self.db.view_socks5(port=4141)
        with pytest.raises(Socks5manDatabaseError):
            self.db.view_socks5()

    def test_find_socks5(self):
        s1id = self.db.add_socks5("9.8.8.8",
                                  4141,
                                  "Germany",
                                  "DE",
                                  city="Berlin",
                                  operational=False)
        s2id = self.db.add_socks5("9.8.8.8",
                                  4141,
                                  "France",
                                  "FR",
                                  city="Paris",
                                  operational=True)
        s3id = self.db.add_socks5("9.8.8.8",
                                  4241,
                                  "France",
                                  "FR",
                                  city="Paris",
                                  operational=True)
        assert self.db.find_socks5()[0].id == s2id
        assert self.db.find_socks5()[0].id == s3id
        assert self.db.find_socks5()[0].id == s2id
        assert len(self.db.find_socks5(limit=1000)) == 2

    def test_find_socks5_no_usage_update(self):
        s1id = self.db.add_socks5("9.8.8.8",
                                  4141,
                                  "Germany",
                                  "DE",
                                  city="Berlin",
                                  operational=False)
        s2id = self.db.add_socks5("9.8.8.8",
                                  4141,
                                  "France",
                                  "FR",
                                  city="Paris",
                                  operational=True)
        s3id = self.db.add_socks5("9.8.8.8",
                                  4241,
                                  "France",
                                  "FR",
                                  city="Paris",
                                  operational=True)
        assert self.db.find_socks5(update_usage=False)[0].id == s2id
        assert self.db.find_socks5(update_usage=False)[0].id == s2id
        assert self.db.find_socks5(update_usage=False)[0].id == s2id
        assert len(self.db.find_socks5(limit=1000)) == 2

    def test_find_socks5_country(self):
        self.db.add_socks5("9.8.8.8",
                           4141,
                           "Germany",
                           "DE",
                           city="Berlin",
                           operational=True)
        self.db.add_socks5("9.8.8.8", 4141, "Belgium", "BE", operational=True)
        self.db.add_socks5("9.8.8.8",
                           4141,
                           "France",
                           "FR",
                           city="Paris",
                           operational=False)
        res = self.db.find_socks5(country="germany")
        assert len(res) == 1
        assert res[0].country == "Germany"
        assert len(self.db.find_socks5(country="france")) == 0

    def test_find_socks5_country_code(self):
        self.db.add_socks5("9.8.8.8",
                           4141,
                           "Germany",
                           "DE",
                           city="Berlin",
                           operational=True)
        self.db.add_socks5("9.8.8.8", 4141, "Belgium", "BE", operational=True)
        self.db.add_socks5("9.8.8.8",
                           4141,
                           "France",
                           "FR",
                           city="Paris",
                           operational=False)
        res = self.db.find_socks5(country_code="de")
        assert len(res) == 1
        assert res[0].country == "Germany"
        assert len(self.db.find_socks5(country="fr")) == 0

    def test_find_socks5_city(self):
        id1 = self.db.add_socks5("9.8.8.8",
                                 4141,
                                 "Germany",
                                 "DE",
                                 city="Berlin",
                                 operational=True)
        id2 = self.db.add_socks5("10.8.8.8",
                                 4141,
                                 "Germany",
                                 "DE",
                                 city="Berlin",
                                 operational=True)
        self.db.add_socks5("9.8.8.8", 4141, "Belgium", "BE", operational=True)
        self.db.add_socks5("9.8.8.8",
                           4141,
                           "France",
                           "FR",
                           operational=True,
                           city="Paris")

        assert self.db.find_socks5(city="berlin")[0].id == id1
        assert self.db.find_socks5(city="berlin")[0].id == id2
        assert self.db.find_socks5(city="berlin")[0].id == id1
        assert len(self.db.find_socks5(city="amsterdam")) == 0

    def test_find_socks5_mbpsdown(self):
        id1 = self.db.add_socks5("9.8.8.8",
                                 4141,
                                 "Germany",
                                 "DE",
                                 operational=True)
        id2 = self.db.add_socks5("10.8.8.8",
                                 4141,
                                 "Germany",
                                 "DE",
                                 operational=True)
        id3 = self.db.add_socks5("9.8.8.8",
                                 4141,
                                 "Belgium",
                                 "BE",
                                 operational=True)
        id4 = self.db.add_socks5("9.8.8.8",
                                 4141,
                                 "France",
                                 "FR",
                                 operational=True)

        self.db.set_approx_bandwidth(id1, 10.1)
        self.db.set_approx_bandwidth(id2, 32.183)
        self.db.set_approx_bandwidth(id3, 0.148)

        assert self.db.find_socks5(min_mbps_down=15.7)[0].id == id2
        assert self.db.find_socks5(min_mbps_down=10)[0].id == id1
        assert self.db.find_socks5(min_mbps_down=10)[0].id == id2
        assert self.db.find_socks5(min_mbps_down=40) == []

    def test_find_socks5_conntime(self):
        id1 = self.db.add_socks5("9.8.8.8",
                                 4141,
                                 "Germany",
                                 "DE",
                                 operational=True)
        id2 = self.db.add_socks5("10.8.8.8",
                                 4141,
                                 "Germany",
                                 "DE",
                                 operational=True)
        id3 = self.db.add_socks5("9.8.8.8",
                                 4141,
                                 "Belgium",
                                 "BE",
                                 operational=True)
        id4 = self.db.add_socks5("9.8.8.8",
                                 4141,
                                 "France",
                                 "FR",
                                 operational=True)

        self.db.set_connect_time(id1, 0.53)
        self.db.set_connect_time(id2, 0.043)
        self.db.set_connect_time(id3, 0.44)

        assert self.db.find_socks5(max_connect_time=0.5)[0].id == id2
        assert self.db.find_socks5(max_connect_time=0.5)[0].id == id3
        assert self.db.find_socks5(max_connect_time=0.5)[0].id == id2
        assert self.db.find_socks5(max_connect_time=0.001) == []

    def test_bulk_add(self):
        s = [{
            "host": "8.8.8.8",
            "port": 4242,
            "country": "ads",
            "country_code": "ad"
        }, {
            "host": "example.com",
            "port": 9133,
            "country": "ads",
            "country_code": "ad"
        }]
        assert len(self.db.list_socks5()) == 0
        self.db.bulk_add_socks5(s)
        assert len(self.db.list_socks5()) == 2

    def test_bulk_add_missing_fields(self):
        s = [{
            "host": "8.8.8.8",
            "port": 4242,
            "country": "ads"
        }, {
            "host": "example.com",
            "port": 9133,
            "country": "ads",
            "country_code": "ad"
        }]
        with pytest.raises(Socks5manDatabaseError):
            self.db.bulk_add_socks5(s)

    def test_set_operational(self):
        id1 = self.db.add_socks5("9.8.8.8",
                                 4141,
                                 "Germany",
                                 "DE",
                                 operational=False)
        assert not self.db.view_socks5(id1).operational
        self.db.set_operational(id1, True)
        s2 = self.db.view_socks5(id1)
        assert s2.operational
        time.sleep(0.01)
        self.db.set_operational(id1, False)
        s3 = self.db.view_socks5(id1)
        assert not s3.operational
        assert s3.last_check > s2.last_check

    def test_set_operational_nonexist(self):
        # Should not raise exception
        self.db.set_operational(8127313, True)

    def test_set_connect_time(self):
        id1 = self.db.add_socks5("9.8.8.8",
                                 4141,
                                 "Germany",
                                 "DE",
                                 operational=False)
        assert self.db.view_socks5(id1).connect_time is None
        self.db.set_connect_time(id1, 0.5)
        assert self.db.view_socks5(id1).connect_time == 0.5

    def test_set_bandwidth(self):
        id1 = self.db.add_socks5("9.8.8.8",
                                 4141,
                                 "Germany",
                                 "DE",
                                 operational=False)
        assert self.db.view_socks5(id1).bandwidth is None
        self.db.set_approx_bandwidth(id1, 10.24)
        assert self.db.view_socks5(id1).bandwidth == 10.24

    def test_delete(self):
        for x in range(25):
            self.db.add_socks5("9.8.8.8",
                               x,
                               "Germany",
                               "DE",
                               operational=False)
        assert len(self.db.list_socks5()) == 25
        self.db.delete_all_socks5()
        assert len(self.db.list_socks5()) == 0

    def test_bulk_delete_socks5(self):
        ids = []
        for x in range(25):
            i = self.db.add_socks5("9.8.8.8",
                                   x,
                                   "Germany",
                                   "DE",
                                   operational=False)
            if x <= 12:
                ids.append(i)
        assert len(self.db.list_socks5()) == 25
        self.db.bulk_delete_socks5(ids)
        assert len(self.db.list_socks5()) == 12
        for i in ids:
            assert self.db.view_socks5(socks5_id=i) is None

    def test_big_bulk_delete(self):
        bulk_socks = [{
            "host": "8.8.8.8",
            "port": 4242,
            "country": "Germany",
            "country_code": "DE"
        } for x in range(2000)]

        self.db.bulk_add_socks5(bulk_socks)
        id1 = self.db.add_socks5("9.8.8.8",
                                 4242,
                                 "Germany",
                                 "DE",
                                 operational=True)
        assert len(self.db.list_socks5()) == 2001
        self.db.bulk_delete_socks5(
            [s.id for s in self.db.list_socks5(operational=False)])
        workingsocks = self.db.list_socks5()
        assert len(workingsocks) == 1
        assert workingsocks[0].operational

    def test_update_geoinfo(self):
        id1 = self.db.add_socks5("9.8.8.8",
                                 4242,
                                 "Germany",
                                 "DE",
                                 operational=False)
        id2 = self.db.add_socks5("9.8.8.8",
                                 4243,
                                 "Germany",
                                 "DE",
                                 operational=False)
        s = self.db.view_socks5(id1)
        assert s.country == "Germany"
        assert s.city is None
        assert s.country_code == "DE"
        self.db.update_geoinfo(id1,
                               country="France",
                               country_code="FR",
                               city="Paris")
        s2 = self.db.view_socks5(id1)
        assert s2.country == "France"
        assert s2.city == "Paris"
        assert s2.country_code == "FR"
        s3 = self.db.view_socks5(id2)
        assert s3.country == "Germany"
        assert s3.city is None
        assert s3.country_code == "DE"

    def test_update_geoninfo_invalid_country(self):
        id1 = self.db.add_socks5("9.8.8.8",
                                 4242,
                                 "Germany",
                                 "DE",
                                 operational=False)
        with pytest.raises(Socks5manDatabaseError):
            self.db.update_geoinfo(id1,
                                   country=None,
                                   country_code="FR",
                                   city="Paris")

    def test_update_geoninfo_invalid_country_code(self):
        id1 = self.db.add_socks5("9.8.8.8",
                                 4242,
                                 "Germany",
                                 "DE",
                                 operational=False)
        with pytest.raises(Socks5manDatabaseError):
            self.db.update_geoinfo(id1,
                                   country="France",
                                   country_code=None,
                                   city="Paris")

    def test_schema_latest_version(self):
        ses = self.db.Session()
        try:
            v = ses.query(AlembicVersion.version_num).first()
            assert v.version_num == SCHEMA_VERSION
        finally:
            ses.close()

    def test_db_migratable_true(self):
        ses = self.db.Session()
        try:
            v = ses.query(AlembicVersion).first()
            v.version_num = "sdfsdfsf"
            ses.add(v)
            ses.commit()
        finally:
            ses.close()

        assert self.db.db_migratable()

    def test_db_migratable_false(self):
        assert not self.db.db_migratable()