def test_invalid_conf(self): create_cwd(cwd()) Config._cache = {} with open(cwd("conf", "socks5man.conf"), "w") as fw: fw.write("socks5man to dominate them all") with pytest.raises(Socks5ConfigError): cfg("socks5man", "verify_interval")
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_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_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
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_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_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_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_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_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_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"
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(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_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_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_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_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()
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_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_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
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
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
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_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
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_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
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_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") )
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_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_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_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_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
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
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_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
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
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_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
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
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_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_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
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_add_auth(self): create_cwd(path=cwd()) m = Manager() res = m.add("example.com", 1337, username="******", password="******") assert res.id == 1
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_invalid_optiontype(self): create_cwd(cwd()) Config._cache = {} Config._conf["operationality"]["ip_api"] = int with pytest.raises(Socks5ConfigError): cfg("socks5man", "verify_interval")