Skip to content

Commit 5c030f5

Browse files
Added test cases for IP2LocationIPTools class.
1 parent 98b480e commit 5c030f5

7 files changed

Lines changed: 101 additions & 17 deletions

File tree

ChangeLog

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
8.7.2 2022-04-15
2+
* Added test cases for IP2LocationIPTools class.
3+
14
8.7.2 2022-03-21
25
* Fixed GitHub issue #19.
36

IP2Location.py

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,12 @@ def inet_pton(t, addr):
9696
socket.inet_pton = inet_pton
9797

9898
def is_ipv4(hostname):
99-
pattern = r'^([0-9]{1,3}[.]){3}[0-9]{1,3}$'
10099
ip_parts = hostname.split('.')
100+
for i in range(0,len(ip_parts)):
101+
if int(ip_parts[i]) > 255:
102+
return False
103+
pattern = r'^([0-9]{1,3}[.]){3}[0-9]{1,3}$'
101104
if match(pattern, hostname) is not None:
102-
ip_parts = hostname.split('.')
103-
for i in range(0,len(ip_parts)):
104-
if int(ip_parts[i]) > 255:
105-
return False
106105
return 4
107106
return False
108107

@@ -674,19 +673,29 @@ def getcredit(self):
674673
class IP2LocationAPIError(Exception):
675674
"""Raise for IP2Location API Error Message"""
676675

677-
class AddressValueError(ValueError):
678-
"""A Value Error related to the address."""
679-
680676
class IP2LocationIPTools(object):
681677

682678
def is_ipv4(self, ip):
683-
ip_parts = ip.split('.')
684-
for i in range(0,len(ip_parts)):
685-
if int(ip_parts[i]) > 255:
679+
if '.' in ip:
680+
ip_parts = ip.split('.')
681+
if len(ip_parts) == 4:
682+
for i in range(0,len(ip_parts)):
683+
if ip_parts[i].isdigit():
684+
if int(ip_parts[i]) > 255:
685+
print('1')
686+
return False
687+
else:
688+
print('2')
689+
return False
690+
pattern = r'^([0-9]{1,3}[.]){3}[0-9]{1,3}$'
691+
if match(pattern, ip) is not None:
692+
return True
693+
else:
694+
print('3')
686695
return False
687-
pattern = r'^([0-9]{1,3}[.]){3}[0-9]{1,3}$'
688-
if match(pattern, ip) is not None:
689-
return True
696+
else:
697+
print('4')
698+
return False
690699
return False
691700

692701
def is_ipv6(self, ip):
@@ -705,22 +714,32 @@ def ipv4_to_decimal(self, ip):
705714
return ipnum
706715

707716
def decimal_to_ipv4(self, decimal):
717+
if decimal.isdigit() is False:
718+
return
708719
if (int(decimal) > 4294967295):
709720
return
710721
else:
711722
return (socket.inet_ntoa(struct.pack('!I', int(decimal))))
712723

713724
def ipv6_to_decimal(self, ip):
725+
if self.is_ipv6(ip) is False:
726+
return
714727
return(int(ipaddress.ip_address(u(ip))))
715728

716729
def decimal_to_ipv6(self, decimal):
730+
if decimal.isdigit() is False:
731+
return
717732
result = ipaddress.IPv6Address(int(decimal))
718733
if (result.ipv4_mapped != None):
719734
return('::ffff:' + str(result.ipv4_mapped))
720735
else:
721736
return str(result)
722737

723738
def ipv4_to_cidr(self, from_ip, to_ip):
739+
if self.is_ipv4(from_ip) is False:
740+
return
741+
if self.is_ipv4(to_ip) is False:
742+
return
724743
startip = ipaddress.IPv4Address(u(from_ip))
725744
endip = ipaddress.IPv4Address(u(to_ip))
726745
ar = [ipaddr for ipaddr in ipaddress.summarize_address_range(startip, endip)]
@@ -730,6 +749,10 @@ def ipv4_to_cidr(self, from_ip, to_ip):
730749
return (ar1)
731750

732751
def ipv6_to_cidr(self, from_ip, to_ip):
752+
if self.is_ipv6(from_ip) is False:
753+
return
754+
if self.is_ipv6(to_ip) is False:
755+
return
733756
startip = ipaddress.IPv6Address(u(from_ip))
734757
endip = ipaddress.IPv6Address(u(to_ip))
735758
ar = [ipaddr for ipaddr in ipaddress.summarize_address_range(startip, endip)]
@@ -739,10 +762,14 @@ def ipv6_to_cidr(self, from_ip, to_ip):
739762
return (ar1)
740763

741764
def cidr_to_ipv4(self, cidr):
765+
if '/' not in cidr:
766+
return
742767
net=ipaddress.ip_network(u(cidr))
743768
return({"ip_start": str(net[0]), "ip_end": str(net[-1])})
744769

745770
def cidr_to_ipv6(self, cidr):
771+
if '/' not in cidr:
772+
return
746773
parts = cidr.split('/')
747774
hexstartaddress = binascii.hexlify(socket.inet_pton(socket.AF_INET6, parts[0]))
748775
if (len(hexstartaddress) < 16):
@@ -764,8 +791,12 @@ def cidr_to_ipv6(self, cidr):
764791
return({"ip_start": self.expand_ipv6(parts[0]), "ip_end": self.expand_ipv6(socket.inet_ntop(socket.AF_INET6, binlastaddress))})
765792

766793
def compressed_ipv6(self, ip):
794+
if self.is_ipv6(ip) is False:
795+
return
767796
return ((ipaddress.IPv6Address(u(ip)).compressed))
768797

769798
def expand_ipv6(self, ip):
799+
if self.is_ipv6(ip) is False:
800+
return
770801
return ((ipaddress.IPv6Address(u(ip)).exploded))
771802

PKG-INFO

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Metadata-Version: 1.0
22
Name: IP2Location
3-
Version: 8.7.2
3+
Version: 8.7.3
44
Summary: Python API for IP2Location database
55
Home-page: http://www.ip2location.com
66
Author: IP2Location

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# IP2Location 8.7.2
1+
# IP2Location 8.7.3
22

33

44
This is a IP2Location Python library that enables the user to find the country, region or state, city, latitude and longitude, ZIP code, time zone, Internet Service Provider (ISP) or company name, domain name, net speed, area code, weather station code, weather station name, mobile country code (MCC), mobile network code (MNC) and carrier brand, elevation, usage type, address type and IAB category by IP address or hostname originates from. The library reads the geo location information from **IP2Location BIN data** file.

sample.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
print ("\n")
5050
print ("Credit Remaining: {}\n".format(ws.getcredit()))
5151

52+
# IP Tools
5253
ipTools = IP2Location.IP2LocationIPTools()
5354
print(str(ipTools.is_ipv4('8.8.8.8')))
5455
print(str(ipTools.is_ipv6('2001:4860:4860::8888')))

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name="IP2Location",
8-
version="8.7.2",
8+
version="8.7.3",
99
author="IP2Location",
1010
author_email="support@ip2location.com",
1111
description="This is an IP geolocation library that enables the user to find the country, region, city, latitude and longitude, ZIP code, time zone, ISP, domain name, area code, weather info, mobile info, elevation, usage type, address type and IAB category from an IP address. It supports both IPv4 and IPv6 lookup.",

tests/test_iptools.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import pytest
4+
5+
import IP2Location
6+
7+
ipTools = IP2Location.IP2LocationIPTools()
8+
9+
def testipv4():
10+
assert ipTools.is_ipv4('8.8.8.8') == True
11+
12+
def testinvalidipv4():
13+
assert ipTools.is_ipv4('8.8.8.555') == False
14+
15+
def testipv6():
16+
assert ipTools.is_ipv6('2001:4860:4860::8888') == True
17+
18+
def testinvalidipv6():
19+
assert ipTools.is_ipv6('2001:4860:4860::ZZZZ') == False
20+
21+
def testipv4decimal():
22+
assert ipTools.ipv4_to_decimal('8.8.8.8') == 134744072
23+
24+
def testdecimalipv4():
25+
assert ipTools.decimal_to_ipv4(134744072) == '8.8.8.8'
26+
27+
def testipv6decimal():
28+
assert ipTools.ipv6_to_decimal('2001:4860:4860::8888') == 42541956123769884636017138956568135816
29+
30+
def testdecimalipv6():
31+
assert ipTools.decimal_to_ipv6(42541956123769884636017138956568135816) == '2001:4860:4860::8888'
32+
33+
def testipv4tocidr():
34+
assert ipTools.ipv4_to_cidr('8.0.0.0', '8.255.255.255') == ['8.0.0.0/8']
35+
36+
def testcidrtoipv4():
37+
assert ipTools.cidr_to_ipv4('8.0.0.0/8') == {'ip_start': '8.0.0.0', 'ip_end': '8.255.255.255'}
38+
39+
def testipv6tocidr():
40+
assert ipTools.ipv6_to_cidr('2002:0000:0000:1234:abcd:ffff:c0a8:0000', '2002:0000:0000:1234:ffff:ffff:ffff:ffff') == ['2002::1234:abcd:ffff:c0a8:0/109', '2002::1234:abcd:ffff:c0b0:0/108', '2002::1234:abcd:ffff:c0c0:0/106', '2002::1234:abcd:ffff:c100:0/104', '2002::1234:abcd:ffff:c200:0/103', '2002::1234:abcd:ffff:c400:0/102', '2002::1234:abcd:ffff:c800:0/101', '2002::1234:abcd:ffff:d000:0/100', '2002::1234:abcd:ffff:e000:0/99', '2002:0:0:1234:abce::/79', '2002:0:0:1234:abd0::/76', '2002:0:0:1234:abe0::/75', '2002:0:0:1234:ac00::/70', '2002:0:0:1234:b000::/68', '2002:0:0:1234:c000::/66']
41+
42+
def testcidrtoipv6():
43+
assert ipTools.cidr_to_ipv6('2002::1234:abcd:ffff:c0a8:101/64') == {'ip_start': '2002:0000:0000:1234:abcd:ffff:c0a8:0101', 'ip_end': '2002:0000:0000:1234:ffff:ffff:ffff:ffff'}
44+
45+
def testcompressipv6():
46+
assert ipTools.compressed_ipv6('2002:0000:0000:1234:FFFF:FFFF:FFFF:FFFF') == '2002::1234:ffff:ffff:ffff:ffff'
47+
48+
def testexpandipv6():
49+
assert ipTools.expand_ipv6('2002::1234:FFFF:FFFF:FFFF:FFFF') == '2002:0000:0000:1234:ffff:ffff:ffff:ffff'

0 commit comments

Comments
 (0)