コード例 #1
0
class TestCWD(object):

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

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

    def test_set_cwd(self):
        tmpdir = self.tmpfile.mkdtemp()
        set_cwd(tmpdir)
        from socks5man.misc import _path
        assert _path == tmpdir

    def test_cwd_normal(self):
        tmpdir = self.tmpfile.mkdtemp()
        set_cwd(tmpdir)
        assert cwd("conf") == os.path.join(tmpdir, "conf")

    @mock.patch("socks5man.misc.create_cwd")
    def test_cwd_nonexist(self, mc):
        tempdir = os.path.join(tempfile.gettempdir(), "UTvYHUvitYV")
        set_cwd(tempdir)
        cwd("test")
        mc.assert_called_once()

    def test_cwd_internal(self):
        tmpdir = self.tmpfile.mkdtemp()
        set_cwd(tmpdir)
        p = cwd("conf", "socks5man.conf", internal=True)
        p2 = os.path.join(
            socks5man.__path__[0], "setupdata", "conf", "socks5man.conf"
        )
        assert p == p2

    def test_create_cwd(self):
        tmpdir = self.tmpfile.mkdtemp()
        set_cwd(tmpdir)
        assert os.listdir(tmpdir) == []
        create_cwd(tmpdir)

        assert os.path.isfile(os.path.join(tmpdir, "conf", "socks5man.conf"))
        assert os.path.isfile(
            os.path.join(tmpdir, "geodb", "extracted", "geodblite.mmdb")
        )
        assert os.path.isfile(os.path.join(tmpdir, "geodb", ".version"))
        assert os.path.exists(
            os.path.join(tmpdir, "geodb", "geodblite.tar.gz")
        )
コード例 #2
0
class TestOther(object):

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

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

    def test_md5(self):
        fd, path = self.tempfile.mkstemp()

        os.write(fd, "tosti")
        os.close(fd)
        assert md5(path) == "9e796589d183889f5c65af8b736490bb"

    def test_unpack_mmdb(self):
        tmpdir = self.tempfile.mkdtemp()
        set_cwd(tmpdir)
        tar_p = cwd("geodb", "geodblite.tar.gz", internal=True)
        assert os.listdir(tmpdir) == []
        os.mkdir(os.path.join(tmpdir, "geodb"))
        mmdb_p = os.path.join(tmpdir, "test.mmdb")
        unpack_mmdb(tarpath=tar_p, to=mmdb_p)
        assert os.path.isfile(mmdb_p)
        version_file = os.path.join(tmpdir, "geodb", ".version")
        assert os.path.isfile(version_file)
        assert md5(tar_p) == open(version_file, "rb").read()
        r = geodatabase.Reader(mmdb_p)
        geodata = r.city("8.8.8.8")
        assert geodata.country.name.lower() == "united states"
コード例 #3
0
class TestGeoInfo(object):
    def setup_class(self):
        self.tempfile = CleanedTempFile()

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

    def test_ipv4info(self):
        tmppath = self.tempfile.mkdtemp()
        set_cwd(tmppath)
        create_cwd(tmppath)

        geo = GeoInfo()
        res = geo.ipv4info("93.184.216.34")
        assert res["country"] == "United States"
        assert res["country_code"] == "US"
        assert res["city"] == "Norwell"

    def test_ipv4info_unknown(self):
        tmppath = self.tempfile.mkdtemp()
        set_cwd(tmppath)
        create_cwd(tmppath)

        geo = GeoInfo()
        res = geo.ipv4info("10.0.0.5")
        assert res["country"] == "unknown"
        assert res["country_code"] == "unknown"
        assert res["city"] == "unknown"

    def test_ipv4info_invalid(self):
        tmppath = self.tempfile.mkdtemp()
        set_cwd(tmppath)
        create_cwd(tmppath)

        geo = GeoInfo()
        res = geo.ipv4info("8adna87dasd87asd")
        assert res["country"] == "unknown"
        assert res["country_code"] == "unknown"
        assert res["city"] == "unknown"
コード例 #4
0
class TestConfigValues(object):
    def setup_class(self):
        self.tempfile = CleanedTempFile()

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

    def setup(self):
        set_cwd(self.tempfile.mkdtemp())

    def test_ip_api(self):
        """Verify that the default ip api returns an actual ip"""
        create_cwd(cwd())
        res = urllib.request.urlopen(
            cfg("operationality", "ip_api"),
            timeout=cfg("operationality", "timeout")
        )
        assert res.getcode() == 200
        assert re.match(rb"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$", res.read())

    def test_measure_time_host(self):
        """Verify that the default connection measurement still accepts
         connections"""
        create_cwd(cwd())
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.settimeout(cfg("connection_time", "timeout"))
        s.connect((
            cfg("connection_time", "hostname"), cfg("connection_time", "port")
        ))
        s.close()

    def test_download_url(self):
        """Verify that the url used to measure an approximate bandwidth
        is still available"""
        create_cwd(cwd())
        res = urllib.request.urlopen(
            cfg("bandwidth", "download_url"),
            timeout=cfg("bandwidth", "timeout")
        )
        assert res.getcode() == 200
        assert len(res.read()) > 0

    def test_geoipdb_hash_url(self):
        create_cwd(cwd())
        res = urllib.request.urlopen(cfg("geodb", "geodb_md5_url"))
        assert res.getcode() == 200
        assert len(res.read()) == 32
コード例 #5
0
class TestConfigValues(object):
    def setup_class(self):
        self.tempfile = CleanedTempFile()

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

    def setup(self):
        set_cwd(self.tempfile.mkdtemp())

    def test_ip_api(self):
        """Verify that the default ip api returns an actual ip"""
        create_cwd(cwd())
        res = urllib2.urlopen(
            cfg("operationality", "ip_api"),
            timeout=cfg("operationality", "timeout")
        )
        assert res.getcode() == 200
        assert re.match(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$", res.read())

    def test_measure_time_host(self):
        """Verify that the default connection measurement still accepts
         connections"""
        create_cwd(cwd())
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.settimeout(cfg("connection_time", "timeout"))
        s.connect((
            cfg("connection_time", "hostname"), cfg("connection_time", "port")
        ))
        s.close()

    def test_download_url(self):
        """Verify that the url used to measure an approximate bandwidth
        is still available"""
        create_cwd(cwd())
        res = urllib2.urlopen(
            cfg("bandwidth", "download_url"),
            timeout=cfg("bandwidth", "timeout")
        )
        assert res.getcode() == 200
        assert len(res.read()) > 0

    def test_geoipdb_hash_url(self):
        create_cwd(cwd())
        res = urllib2.urlopen(cfg("geodb", "geodb_md5_url"))
        assert res.getcode() == 200
        assert len(res.read()) == 32
コード例 #6
0
ファイル: test_manager.py プロジェクト: RicoVZ/socks5man
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)
コード例 #7
0
class TestConfig(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)
        Config._cache = {}
        self.confbackup = copy.deepcopy(Config._conf)

    def teardown(self):
        Config._conf = self.confbackup

    def test_cfg_defaults(self):
        create_cwd(cwd())
        assert isinstance(cfg("socks5man", "verify_interval"), int)
        assert isinstance(cfg("socks5man", "bandwidth_interval"), int)
        assert isinstance(cfg("operationality", "ip_api"), (str, basestring))
        assert isinstance(cfg("operationality", "timeout"), int)
        assert isinstance(cfg("connection_time", "enabled"), bool)
        assert isinstance(cfg("connection_time", "timeout"), int)
        assert isinstance(cfg("connection_time", "hostname"),(str, basestring))
        assert isinstance(cfg("connection_time", "port"), int)
        assert isinstance(cfg("bandwidth", "enabled"), bool)
        assert isinstance(cfg("bandwidth", "download_url"), (str, basestring))
        assert isinstance(cfg("bandwidth", "times"), int)
        assert isinstance(cfg("bandwidth", "timeout"), int)
        assert isinstance(cfg("geodb", "geodb_url"), (str, basestring))
        assert isinstance(cfg("geodb", "geodb_md5_url"), (str, basestring))

    def test_cfg_values(self):
        create_cwd(cwd())
        assert cfg("socks5man", "verify_interval") == 300
        assert cfg("operationality", "ip_api") == "http://api.ipify.org"
        assert not cfg("bandwidth", "enabled")
        assert cfg("connection_time", "enabled")

    def test_cfg_invalid(self):
        create_cwd(cwd())
        with pytest.raises(Socks5ConfigError):
            cfg("socks5man", "nonexistingkeystuffdogestosti")
        with pytest.raises(Socks5ConfigError):
            cfg("doges", "wut")

    def test_read_from_cache(self):
        create_cwd(cwd())
        Config._cache = {}
        assert cfg("operationality", "ip_api") == "http://api.ipify.org"
        assert "operationality" in Config._cache
        Config._cache["operationality"]["ip_api"] = "http://example.com"
        assert cfg("operationality", "ip_api") == "http://example.com"

    def test_missing_conf(self):
        create_cwd(cwd())
        os.remove(cwd("conf", "socks5man.conf"))
        Config._cache = {}
        with pytest.raises(Socks5ConfigError):
            cfg("socks5man", "verify_interval")

    def test_invalid_conf(self):
        create_cwd(cwd())
        Config._cache = {}
        with open(cwd("conf", "socks5man.conf"), "wb") as fw:
            fw.write(os.urandom(512))
        with pytest.raises(Socks5ConfigError):
            cfg("socks5man", "verify_interval")

    def test_unknown_section(self):
        create_cwd(cwd())
        Config._cache = {}
        del Config._conf["operationality"]
        with pytest.raises(Socks5ConfigError):
            cfg("socks5man", "verify_interval")

    def test_unknown_option(self):
        create_cwd(cwd())
        Config._cache = {}
        Config._conf["operationality"]["ip_api"] = None
        with pytest.raises(Socks5ConfigError):
            cfg("socks5man", "verify_interval")

    def test_invalid_optiontype(self):
        create_cwd(cwd())
        Config._cache = {}
        Config._conf["operationality"]["ip_api"] = int
        with pytest.raises(Socks5ConfigError):
            cfg("socks5man", "verify_interval")

    def test_confbool(self):
        assert confbool("yes")
        assert confbool("1")
        assert confbool("true")
        assert confbool("on")
        assert confbool("Yes")
        assert confbool("True")
        assert confbool("On")
        assert not confbool("off")
        assert not confbool("0")

    def test_config_read_clear_cache(self):
        create_cwd(cwd())
        Config._cache = {"test": "test"}
        Config().read()
        assert len(Config._cache) > 0
        assert "test" not in Config._cache
コード例 #8
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)
コード例 #9
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_attrs(self):
        self.db.add_socks5("8.8.8.8",
                           1337,
                           "germany",
                           "DE",
                           city="Frankfurt",
                           operational=True,
                           username="******",
                           password="******",
                           description="Such wow, many socks5")
        self.db.set_approx_bandwidth(1, 10.55)
        self.db.set_connect_time(1, 0.07)
        db_socks5 = self.db.view_socks5(1)
        s = Socks5(db_socks5)

        assert s.id == 1
        assert s.host == "8.8.8.8"
        assert s.port == 1337
        assert s.country == "germany"
        assert s.city == "Frankfurt"
        assert s.country_code == "DE"
        assert s.username == "doge"
        assert s.password == "wow"
        assert s.description == "Such wow, many socks5"
        assert s.operational
        assert s.bandwidth == 10.55
        assert s.connect_time == 0.07
        assert s.last_use is None
        assert s.last_check is None
        assert isinstance(s.added_on, datetime.datetime)

    def test_attrs_invalid(self):
        self.db.add_socks5("8.8.8.8",
                           1337,
                           "germany",
                           "DE",
                           city="Frankfurt",
                           operational=True,
                           username="******",
                           password="******",
                           description="Such wow, many socks5")
        self.db.set_approx_bandwidth(1, 10.55)
        self.db.set_connect_time(1, 0.07)
        db_socks5 = self.db.view_socks5(1)
        db_socks5.host = ""
        db_socks5.country = ""
        db_socks5.city = ""
        db_socks5.username = ""
        db_socks5.description = ""
        s = Socks5(db_socks5)
        assert s.host is None
        assert s.country is None
        assert s.city is None
        assert s.username is None
        assert s.description is None

    @mock.patch("socks5man.socks5.get_over_socks5")
    def test_verify(self, mg):
        create_cwd(cwd())
        mg.return_value = b"8.8.8.8"
        self.db.add_socks5("8.8.8.8",
                           1337,
                           "germany",
                           "DE",
                           city="Frankfurt",
                           operational=False,
                           username="******",
                           password="******",
                           description="Such wow, many socks5")
        db_socks5 = self.db.view_socks5(1)
        s = Socks5(db_socks5)
        assert s.verify()
        mg.assert_called_once_with("http://api.ipify.org",
                                   "8.8.8.8",
                                   1337,
                                   username="******",
                                   password="******",
                                   timeout=3)
        db_socks5_2 = self.db.view_socks5(1)
        assert db_socks5_2.operational

    @mock.patch("socks5man.socks5.get_over_socks5")
    def test_verify_fail(self, mg):
        create_cwd(cwd())
        mg.return_value = None
        self.db.add_socks5("8.8.8.8",
                           1337,
                           "germany",
                           "DE",
                           city="Frankfurt",
                           operational=True,
                           username="******",
                           password="******",
                           description="Such wow, many socks5")
        db_socks5 = self.db.view_socks5(1)
        s = Socks5(db_socks5)
        assert not s.verify()
        db_socks5_2 = self.db.view_socks5(1)
        assert not db_socks5_2.operational

    @mock.patch("socks5man.socks5.get_over_socks5")
    def test_verify_private(self, mg):
        create_cwd(cwd())
        mg.return_value = b"8.8.8.8"
        self.db.add_socks5(
            "192.168.0.50",
            1337,
            "germany",
            "DE",
            city="Frankfurt",
            operational=False,
            username="******",
            password="******",
            description="Such wow, many socks5",
        )
        db_socks5 = self.db.view_socks5(1)
        s = Socks5(db_socks5)
        assert s.verify()
        db_socks5_2 = self.db.view_socks5(1)
        assert db_socks5_2.operational

    @mock.patch("socks5man.socks5.get_over_socks5")
    def test_verify_hostname(self, mg):
        create_cwd(cwd())
        mg.return_value = b"93.184.216.34"
        self.db.add_socks5("example.com",
                           1337,
                           "germany",
                           "DE",
                           city="Frankfurt",
                           operational=False,
                           username="******",
                           password="******",
                           description="Such wow, many socks5")
        db_socks5 = self.db.view_socks5(1)
        s = Socks5(db_socks5)
        assert s.verify()
        db_socks5_2 = self.db.view_socks5(1)
        assert db_socks5_2.operational

    @mock.patch("socks5man.socks5.approximate_bandwidth")
    def test_approx_bandwidth(self, ma):
        create_cwd(cwd())
        ma.return_value = 15.10
        self.db.add_socks5("example.com",
                           1337,
                           "germany",
                           "DE",
                           city="Frankfurt",
                           operational=False,
                           username="******",
                           password="******",
                           description="Such wow, many socks5")
        db_socks5 = self.db.view_socks5(1)
        s = Socks5(db_socks5)
        res = s.approx_bandwidth()
        assert res == 15.10
        ma.assert_called_once_with("example.com",
                                   1337,
                                   username="******",
                                   password="******",
                                   times=2,
                                   timeout=10)
        db_socks5_2 = self.db.view_socks5(1)
        assert db_socks5_2.bandwidth == 15.10

    @mock.patch("socks5man.socks5.approximate_bandwidth")
    def test_approx_bandwidth_fail(self, ma):
        create_cwd(cwd())
        ma.return_value = None
        self.db.add_socks5("example.com",
                           1337,
                           "germany",
                           "DE",
                           city="Frankfurt",
                           operational=False,
                           username="******",
                           password="******",
                           description="Such wow, many socks5")
        db_socks5 = self.db.view_socks5(1)
        s = Socks5(db_socks5)
        res = s.approx_bandwidth()
        assert res == None
        db_socks5_2 = self.db.view_socks5(1)
        assert db_socks5_2.bandwidth is None

    @mock.patch("socks5man.socks5.socks")
    def test_measure_conn_time(self, ms):
        create_cwd(cwd())
        socksocket = mock.MagicMock()
        ms.socksocket.return_value = socksocket
        self.db.add_socks5("example.com",
                           1337,
                           "germany",
                           "DE",
                           city="Frankfurt",
                           operational=False,
                           username="******",
                           password="******",
                           description="Such wow, many socks5")
        db_socks5 = self.db.view_socks5(1)
        s = Socks5(db_socks5)
        res = s.measure_connection_time()

        assert isinstance(res, float)
        socksocket.set_proxy.assert_called_once_with(ms.SOCKS5,
                                                     "example.com",
                                                     1337,
                                                     username="******",
                                                     password="******")
        socksocket.settimeout.assert_called_once_with(3)
        socksocket.connect.assert_called_once_with(("api.ipify.org", 80))
        assert self.db.view_socks5(1).connect_time == res

    @mock.patch("socks5man.socks5.socks")
    def test_measure_conn_time_fail(self, ms):
        create_cwd(cwd())
        socksocket = mock.MagicMock()
        ms.socksocket.return_value = socksocket
        socksocket.connect.side_effect = socket.error
        self.db.add_socks5("example.com",
                           1337,
                           "germany",
                           "DE",
                           city="Frankfurt",
                           operational=False,
                           username="******",
                           password="******",
                           description="Such wow, many socks5")
        db_socks5 = self.db.view_socks5(1)
        s = Socks5(db_socks5)
        s.measure_connection_time() is None
        self.db.view_socks5(1).connect_time is None

    def test_socks5_to_dict(self):
        self.db.add_socks5("example.com",
                           1337,
                           "germany",
                           "DE",
                           city="Frankfurt",
                           operational=False,
                           username="******",
                           password="******",
                           description="Such wow, many socks5")
        s = self.db.view_socks5(1)
        socks5 = Socks5(s)
        d = socks5.to_dict()
        assert d["host"] == b"example.com"
        assert d["port"] == 1337
        assert d["country"] == b"germany"
        assert d["country_code"] == b"DE"
        assert d["city"] == b"Frankfurt"
        assert not d["operational"]
        assert d["username"] == b"doge"
        assert d["password"] == b"wow"
        assert d["description"] == b"Such wow, many socks5"
        assert d["added_on"] == socks5.added_on.strftime("%Y-%m-%d %H:%M:%S")

    def test_repr(self):
        self.db.add_socks5("example.com",
                           1337,
                           "germany",
                           "DE",
                           city="Frankfurt",
                           operational=False,
                           username="******",
                           password="******",
                           description="Such wow, many socks5")
        s = self.db.view_socks5(1)
        socks5 = Socks5(s)
        assert repr(
            socks5
        ) == "<Socks5(host=example.com, port=1337, country=germany, authenticated=True)>"

    def test_repr_nonauth(self):
        self.db.add_socks5("example.com",
                           1337,
                           "germany",
                           "DE",
                           city="Frankfurt",
                           operational=False,
                           description="Such wow, many socks5")
        s = self.db.view_socks5(1)
        socks5 = Socks5(s)
        assert repr(
            socks5
        ) == "<Socks5(host=example.com, port=1337, country=germany, authenticated=False)>"

    def test_win_imported_win_inet_pton(self):
        if sys.platform == "win32":
            assert "win_inet_pton" in sys.modules
        else:
            assert "win_inet_pton" not in sys.modules
コード例 #10
0
ファイル: test_database.py プロジェクト: RicoVZ/socks5man
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()
コード例 #11
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()
コード例 #12
0
ファイル: test_socks5.py プロジェクト: RicoVZ/socks5man
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_attrs(self):
        self.db.add_socks5(
            "8.8.8.8", 1337, "germany", "DE",
            city="Frankfurt", operational=True, username="******",
            password="******", description="Such wow, many socks5"
        )
        self.db.set_approx_bandwidth(1, 10.55)
        self.db.set_connect_time(1, 0.07)
        db_socks5 = self.db.view_socks5(1)
        s = Socks5(db_socks5)

        assert s.id == 1
        assert s.host == "8.8.8.8"
        assert s.port == 1337
        assert s.country == "germany"
        assert s.city == "Frankfurt"
        assert s.country_code == "DE"
        assert s.username == "doge"
        assert s.password == "wow"
        assert s.description == "Such wow, many socks5"
        assert s.operational
        assert s.bandwidth == 10.55
        assert s.connect_time == 0.07
        assert s.last_use is None
        assert s.last_check is None
        assert isinstance(s.added_on, datetime.datetime)

    def test_attrs_invalid(self):
        self.db.add_socks5(
            "8.8.8.8", 1337, "germany", "DE",
            city="Frankfurt", operational=True, username="******",
            password="******", description="Such wow, many socks5"
        )
        self.db.set_approx_bandwidth(1, 10.55)
        self.db.set_connect_time(1, 0.07)
        db_socks5 = self.db.view_socks5(1)
        db_socks5.host = ""
        db_socks5.country = ""
        db_socks5.city = ""
        db_socks5.username = ""
        db_socks5.description = ""
        s = Socks5(db_socks5)
        assert s.host is None
        assert s.country is None
        assert s.city is None
        assert s.username is None
        assert s.description is None

    @mock.patch("socks5man.socks5.get_over_socks5")
    def test_verify(self, mg):
        create_cwd(cwd())
        mg.return_value = "8.8.8.8"
        self.db.add_socks5(
            "8.8.8.8", 1337, "germany", "DE",
            city="Frankfurt", operational=False, username="******",
            password="******", description="Such wow, many socks5"
        )
        db_socks5 = self.db.view_socks5(1)
        s = Socks5(db_socks5)
        assert s.verify()
        mg.assert_called_once_with(
            "http://api.ipify.org", "8.8.8.8", 1337, username="******",
            password="******", timeout=3
        )
        db_socks5_2 = self.db.view_socks5(1)
        assert db_socks5_2.operational

    @mock.patch("socks5man.socks5.get_over_socks5")
    def test_verify_fail(self, mg):
        create_cwd(cwd())
        mg.return_value = None
        self.db.add_socks5(
            "8.8.8.8", 1337, "germany", "DE",
            city="Frankfurt", operational=True, username="******",
            password="******", description="Such wow, many socks5"
        )
        db_socks5 = self.db.view_socks5(1)
        s = Socks5(db_socks5)
        assert not s.verify()
        db_socks5_2 = self.db.view_socks5(1)
        assert not db_socks5_2.operational

    @mock.patch("socks5man.socks5.get_over_socks5")
    def test_verify_private(self, mg):
        create_cwd(cwd())
        mg.return_value = "8.8.8.8"
        self.db.add_socks5(
            "192.168.0.50", 1337, "germany", "DE",
            city="Frankfurt", operational=False, username="******",
            password="******", description="Such wow, many socks5"
        )
        db_socks5 = self.db.view_socks5(1)
        s = Socks5(db_socks5)
        assert s.verify()
        db_socks5_2 = self.db.view_socks5(1)
        assert db_socks5_2.operational

    @mock.patch("socks5man.socks5.get_over_socks5")
    def test_verify_hostname(self, mg):
        create_cwd(cwd())
        mg.return_value = "93.184.216.34"
        self.db.add_socks5(
            "example.com", 1337, "germany", "DE",
            city="Frankfurt", operational=False, username="******",
            password="******", description="Such wow, many socks5"
        )
        db_socks5 = self.db.view_socks5(1)
        s = Socks5(db_socks5)
        assert s.verify()
        db_socks5_2 = self.db.view_socks5(1)
        assert db_socks5_2.operational

    @mock.patch("socks5man.socks5.approximate_bandwidth")
    def test_approx_bandwidth(self, ma):
        create_cwd(cwd())
        ma.return_value = 15.10
        self.db.add_socks5(
            "example.com", 1337, "germany", "DE",
            city="Frankfurt", operational=False, username="******",
            password="******", description="Such wow, many socks5"
        )
        db_socks5 = self.db.view_socks5(1)
        s = Socks5(db_socks5)
        res = s.approx_bandwidth()
        assert res == 15.10
        ma.assert_called_once_with(
            "example.com", 1337, username="******", password="******",
            times=2, timeout=10
        )
        db_socks5_2 = self.db.view_socks5(1)
        assert db_socks5_2.bandwidth == 15.10

    @mock.patch("socks5man.socks5.approximate_bandwidth")
    def test_approx_bandwidth_fail(self, ma):
        create_cwd(cwd())
        ma.return_value = None
        self.db.add_socks5(
            "example.com", 1337, "germany", "DE",
            city="Frankfurt", operational=False, username="******",
            password="******", description="Such wow, many socks5"
        )
        db_socks5 = self.db.view_socks5(1)
        s = Socks5(db_socks5)
        res = s.approx_bandwidth()
        assert res == None
        db_socks5_2 = self.db.view_socks5(1)
        assert db_socks5_2.bandwidth is None

    @mock.patch("socks5man.socks5.socks")
    def test_measure_conn_time(self, ms):
        create_cwd(cwd())
        socksocket = mock.MagicMock()
        ms.socksocket.return_value = socksocket
        self.db.add_socks5(
            "example.com", 1337, "germany", "DE",
            city="Frankfurt", operational=False, username="******",
            password="******", description="Such wow, many socks5"
        )
        db_socks5 = self.db.view_socks5(1)
        s = Socks5(db_socks5)
        res = s.measure_connection_time()

        assert isinstance(res, float)
        socksocket.set_proxy.assert_called_once_with(
            ms.SOCKS5, "example.com", 1337, username="******", password="******"
        )
        socksocket.settimeout.assert_called_once_with(3)
        socksocket.connect.assert_called_once_with(
            ("api.ipify.org", 80)
        )
        assert self.db.view_socks5(1).connect_time == res

    @mock.patch("socks5man.socks5.socks")
    def test_measure_conn_time_fail(self, ms):
        create_cwd(cwd())
        socksocket = mock.MagicMock()
        ms.socksocket.return_value = socksocket
        socksocket.connect.side_effect = socket.error
        self.db.add_socks5(
            "example.com", 1337, "germany", "DE",
            city="Frankfurt", operational=False, username="******",
            password="******", description="Such wow, many socks5"
        )
        db_socks5 = self.db.view_socks5(1)
        s = Socks5(db_socks5)
        s.measure_connection_time() is None
        self.db.view_socks5(1).connect_time is None

    def test_socks5_to_dict(self):
        self.db.add_socks5(
            "example.com", 1337, "germany", "DE",
            city="Frankfurt", operational=False, username="******",
            password="******", description="Such wow, many socks5"
        )
        s = self.db.view_socks5(1)
        socks5 = Socks5(s)
        d = socks5.to_dict()
        assert d["host"] == "example.com"
        assert d["port"] == 1337
        assert d["country"] == "germany"
        assert d["country_code"] == "DE"
        assert d["city"] == "Frankfurt"
        assert not d["operational"]
        assert d["username"] == "doge"
        assert d["password"] == "wow"
        assert d["description"] == "Such wow, many socks5"
        assert d["added_on"] == socks5.added_on.strftime("%Y-%m-%d %H:%M:%S")

    def test_repr(self):
        self.db.add_socks5(
            "example.com", 1337, "germany", "DE",
            city="Frankfurt", operational=False, username="******",
            password="******", description="Such wow, many socks5"
        )
        s = self.db.view_socks5(1)
        socks5 = Socks5(s)
        assert repr(socks5) == "<Socks5(host=example.com, port=1337, country=germany, authenticated=True)>"

    def test_repr_nonauth(self):
        self.db.add_socks5(
            "example.com", 1337, "germany", "DE",
            city="Frankfurt", operational=False,
            description="Such wow, many socks5"
        )
        s = self.db.view_socks5(1)
        socks5 = Socks5(s)
        assert repr(socks5) == "<Socks5(host=example.com, port=1337, country=germany, authenticated=False)>"

    def test_win_imported_win_inet_pton(self):
        if sys.platform == "win32":
            assert "win_inet_pton" in sys.modules
        else:
            assert "win_inet_pton" not in sys.modules
コード例 #13
0
class TestVerifyAll(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)

    @mock.patch("socks5man.tools.Socks5")
    def test_success(self, ms):
        create_cwd(cwd())
        socks5 = mock.MagicMock()
        socks5.host = "8.8.8.8"
        socks5.port = 4242
        ms.return_value = socks5
        self.db.add_socks5("8.8.8.8", 4242, "Germany", "DE")

        verify_all()
        socks5.verify.assert_called_once()
        socks5.measure_connection_time.assert_called_once()
        socks5.approx_bandwidth.assert_not_called()

    @mock.patch("socks5man.tools.Socks5")
    def test_fail(self, ms):
        create_cwd(cwd())
        socks5 = mock.MagicMock()
        socks5.host = "8.8.8.8"
        socks5.port = 4242
        socks5.verify.return_value = False
        ms.return_value = socks5
        self.db.add_socks5("8.8.8.8", 4242, "Germany", "DE")

        verify_all()
        socks5.verify.assert_called_once()
        socks5.measure_connection_time.assert_not_called()
        socks5.approx_bandwidth.assert_not_called()

    @mock.patch("socks5man.tools.Socks5")
    def test_bandwidth(self, ms):
        create_cwd(cwd())
        socks5 = mock.MagicMock()
        socks5.host = "8.8.8.8"
        socks5.port = 4242
        ms.return_value = socks5
        self.db.add_socks5("8.8.8.8", 4242, "Germany", "DE")
        Config._cache["bandwidth"]["enabled"] = True

        verify_all()
        socks5.verify.assert_called_once()
        socks5.measure_connection_time.assert_called_once()
        socks5.approx_bandwidth.assert_called_once()
        Config._cache["bandwidth"]["enabled"] = False

    @mock.patch("socks5man.tools.Socks5")
    def test_conntime_fail(self, ms):
        create_cwd(cwd())
        socks5 = mock.MagicMock()
        socks5.host = "8.8.8.8"
        socks5.port = 4242
        socks5.measure_connection_time.return_value = False
        ms.return_value = socks5
        self.db.add_socks5("8.8.8.8", 4242, "Germany", "DE")
        Config._cache["bandwidth"]["enabled"] = True

        verify_all()
        socks5.verify.assert_called_once()
        socks5.measure_connection_time.assert_called_once()
        socks5.approx_bandwidth.assert_not_called()
        Config._cache["bandwidth"]["enabled"] = False

    @mock.patch("socks5man.tools.urllib2.urlopen")
    @mock.patch("socks5man.tools.Socks5")
    def test_download_verify_fail(self, ms, mu):
        create_cwd(cwd())
        socks5 = mock.MagicMock()
        socks5.host = "8.8.8.8"
        socks5.port = 4242
        ms.return_value = socks5
        mu.side_effect = socket.error
        self.db.add_socks5("8.8.8.8", 4242, "Germany", "DE")
        Config._cache["bandwidth"]["enabled"] = True

        verify_all()
        socks5.verify.assert_called_once()
        socks5.measure_connection_time.assert_called_once()
        mu.assert_called_once_with(mock.ANY, timeout=5)
        socks5.approx_bandwidth.assert_not_called()
        Config._cache["bandwidth"]["enabled"] = False