ip2region/binding/python/util_test.py
2025-10-30 11:37:19 +08:00

116 lines
4.2 KiB
Python

# Copyright 2022 The Ip2Region Authors. All rights reserved.
# Use of this source code is governed by a Apache2.0-style
# license that can be found in the LICENSE file.
# util test script on 2025/10/29
# Author Leon<chenxin619315@gmail.com>
import os
import sys
import time
from xdb import util
script_dir = os.path.dirname(__file__)
data_dir = os.path.join(script_dir, '../../data/')
xdb_v4_path = os.path.join(data_dir, "ip2region_v4.xdb")
xdb_v6_path = os.path.join(data_dir, "ip2region_v6.xdb")
# print(script_dir, data_dir, xdb_v4_path, xdb_v6_path)
def test_version():
print("1, version contants: ")
print("IPv4 -> ", util.IPv4)
print("IPv6 -> ", util.IPv6)
# version from name
print("2, version from name: ")
for name in ["v4", "IPv4", "v4x", "v6", "IPv6", "v6x"]:
print("version_from_name({}) -> ".format(name), util.version_from_name(name))
# version from header
print("3, version from header: ")
v4_header = util.load_header_from_file(xdb_v4_path)
v6_header = util.load_header_from_file(xdb_v6_path)
print("version_from_header(v4_header) -> ", util.version_from_header(v4_header))
print("version_from_header(v6_header) -> ", util.version_from_header(v6_header))
def test_verify():
# v4 xdb verify
try:
util.verify_from_file(xdb_v4_path)
except Exception as e:
print("failed to verify the xdb file `{}`: {}".format(xdb_v4_path, str(e)))
else:
print("xdb file `{}` verified".format(xdb_v4_path))
# v6 xdb verify
try:
util.verify_from_file(xdb_v6_path)
except Exception as e:
print("failed to verify the xdb file `{}`: {}".format(xdb_v6_path, str(e)))
else:
print("xdb file `{}` verified".format(xdb_v6_path))
def test_load_header():
v4_header = util.load_header_from_file(xdb_v4_path)
v6_header = util.load_header_from_file(xdb_v6_path)
print("v4_header -> ", v4_header)
print("v6_header -> ", v6_header)
def test_load_vector_index():
v4_v_index = util.load_vector_index_from_file(xdb_v4_path)
v6_v_index = util.load_vector_index_from_file(xdb_v6_path)
print("v4_v_index.length={}".format(len(v4_v_index)))
print("v6_v_index.length={}".format(len(v6_v_index)))
def test_load_content():
v4_content = util.load_content_from_file(xdb_v4_path)
v6_content = util.load_content_from_file(xdb_v6_path)
print("v4_content.length={}".format(len(v4_content)))
print("v6_content.length={}".format(len(v6_content)))
def test_parse_ip():
ip_list = [
"1.0.0.0", "58.251.30.115", "192.168.1.100", "126.255.32.255", "219.xx.xx.11",
"::", "::1", "fffe::", "2c0f:fff0::", "2c0f:fff0::1", "2a02:26f7:c409:4001::",
"2fff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", "240e:982:e617:ffff:ffff:ffff:ffff:ffff", "::xx:ffff"
]
for ip in ip_list:
try :
ip_bytes = util.parse_ip(ip)
ip_string = util.ip_to_string(ip_bytes)
print("parse_ip({}) -> {{addr:{}, equal:{}}}".format(ip, ip_string, ip_string == ip))
except ValueError as e:
print("failed to parse ip `{}`: {}".format(ip, e))
def test_ip_compare():
ip_list = [
["1.0.0.0", "1.0.0.1", -1],
["192.168.1.101", "192.168.1.90", 1],
["219.133.111.87", "114.114.114.114", 1],
["2000::", "2000:ffff:ffff:ffff:ffff:ffff:ffff:ffff", -1],
["2001:4:112::", "2001:4:112:ffff:ffff:ffff:ffff:ffff", -1],
["ffff::", "2001:4:ffff:ffff:ffff:ffff:ffff:ffff", 1]
]
for ip_pair in ip_list:
ip1 = util.parse_ip(ip_pair[0])
ip2 = util.parse_ip(ip_pair[1])
cmp = util.ip_compare(ip1, ip2)
print("compare({}, {}) -> {} ? {}".format(util.ip_to_string(ip1), util.ip_to_string(ip2), cmp, cmp == ip_pair[2]))
if __name__ == "__main__":
# check and call the specified function
if len(sys.argv) < 2:
sys.exit("please specified the function to test")
func = sys.argv[1]
all_ids = globals()
if func in all_ids and callable(all_ids[func]):
print("+---calling test function {} ...".format(func))
s_time = time.time()
all_ids[func]()
c_time = time.time() - s_time
print(f"|---Done, elapsed {c_time:.6f}s")
else:
sys.exit("unable to call function {}".format(func))