签入版本
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
"""utils/ip2region.py:畸形 IP 不得抛异常中断日志(内部降级返回"未知")。
|
||||
|
||||
回归场景:IP 形如 172.abc 时,int(parts[1]) 曾抛 ValueError 冒泡到
|
||||
record_login_log / record_operation_log,使日志整段丢失。
|
||||
"""
|
||||
|
||||
from utils.ip2region import _is_private_ip, get_ip_location, get_ip_region
|
||||
|
||||
|
||||
def test_is_private_ip_handles_malformed_ip():
|
||||
# 畸形 IP 不再抛 ValueError,按非内网处理(交查询侧兜底为"未知")
|
||||
assert _is_private_ip('172.abc') is False
|
||||
assert _is_private_ip('172.') is False
|
||||
assert _is_private_ip('172.a.b') is False
|
||||
|
||||
|
||||
def test_is_private_ip_private_ranges():
|
||||
assert _is_private_ip('172.16.5.5') is True
|
||||
assert _is_private_ip('172.31.5.5') is True
|
||||
assert _is_private_ip('172.32.5.5') is False # 不在 172.16/12 私有段
|
||||
assert _is_private_ip('10.1.2.3') is True
|
||||
assert _is_private_ip('192.168.1.1') is True
|
||||
assert _is_private_ip('8.8.8.8') is False
|
||||
|
||||
|
||||
def test_get_ip_location_malformed_ip_degrades_to_unknown():
|
||||
# 畸形 IP:不抛异常,降级返回"未知"(xdb 未加载时非内网 IP 也返回"未知")
|
||||
assert get_ip_location('172.abc') == '未知'
|
||||
|
||||
|
||||
def test_get_ip_region_malformed_ip_returns_unknown_dict():
|
||||
region = get_ip_region('172.abc')
|
||||
assert region['country'] == '未知'
|
||||
assert set(region) == {'country', 'province', 'city', 'isp', 'country_code'}
|
||||
|
||||
|
||||
def test_get_ip_location_private_ip_still_works():
|
||||
# 内网 IP 无需 xdb,仍正常返回"内网IP"
|
||||
assert get_ip_location('10.0.0.1') == '内网IP'
|
||||
Reference in New Issue
Block a user