2016-10-11 05:58:09 +03:00
|
|
|
#!/usr/bin/env python
|
2017-01-01 00:36:24 +03:00
|
|
|
#
|
|
|
|
# test_ipaddress.py - tests for ipaddress support
|
|
|
|
#
|
2019-02-17 04:34:52 +03:00
|
|
|
# Copyright (C) 2016-2019 Daniele Varrazzo <daniele.varrazzo@gmail.com>
|
2020-01-18 00:10:44 +03:00
|
|
|
# Copyright (C) 2020 The Psycopg Team
|
2016-10-11 05:58:09 +03:00
|
|
|
#
|
|
|
|
# psycopg2 is free software: you can redistribute it and/or modify it
|
|
|
|
# under the terms of the GNU Lesser General Public License as published
|
|
|
|
# by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# psycopg2 is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
|
|
|
# License for more details.
|
|
|
|
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2017-12-04 05:47:19 +03:00
|
|
|
from . import testutils
|
2017-12-02 04:59:53 +03:00
|
|
|
import unittest
|
2016-10-11 05:58:09 +03:00
|
|
|
|
|
|
|
import psycopg2
|
|
|
|
import psycopg2.extras
|
|
|
|
|
2018-12-01 22:34:01 +03:00
|
|
|
try:
|
2019-03-16 21:56:56 +03:00
|
|
|
import ipaddress as ip
|
2018-12-01 22:34:01 +03:00
|
|
|
except ImportError:
|
|
|
|
# Python 2
|
2019-03-16 21:56:56 +03:00
|
|
|
ip = None
|
2016-10-11 05:58:09 +03:00
|
|
|
|
|
|
|
|
2019-03-16 21:56:56 +03:00
|
|
|
@unittest.skipIf(ip is None, "'ipaddress' module not available")
|
2017-02-06 23:33:35 +03:00
|
|
|
class NetworkingTestCase(testutils.ConnectingTestCase):
|
2016-10-11 05:58:09 +03:00
|
|
|
def test_inet_cast(self):
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
psycopg2.extras.register_ipaddress(cur)
|
|
|
|
|
|
|
|
cur.execute("select null::inet")
|
|
|
|
self.assert_(cur.fetchone()[0] is None)
|
|
|
|
|
|
|
|
cur.execute("select '127.0.0.1/24'::inet")
|
|
|
|
obj = cur.fetchone()[0]
|
|
|
|
self.assert_(isinstance(obj, ip.IPv4Interface), repr(obj))
|
|
|
|
self.assertEquals(obj, ip.ip_interface('127.0.0.1/24'))
|
|
|
|
|
|
|
|
cur.execute("select '::ffff:102:300/128'::inet")
|
|
|
|
obj = cur.fetchone()[0]
|
|
|
|
self.assert_(isinstance(obj, ip.IPv6Interface), repr(obj))
|
|
|
|
self.assertEquals(obj, ip.ip_interface('::ffff:102:300/128'))
|
|
|
|
|
2017-02-06 23:33:35 +03:00
|
|
|
@testutils.skip_before_postgres(8, 2)
|
2016-10-11 05:58:09 +03:00
|
|
|
def test_inet_array_cast(self):
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
psycopg2.extras.register_ipaddress(cur)
|
|
|
|
cur.execute("select '{NULL,127.0.0.1,::ffff:102:300/128}'::inet[]")
|
|
|
|
l = cur.fetchone()[0]
|
|
|
|
self.assert_(l[0] is None)
|
|
|
|
self.assertEquals(l[1], ip.ip_interface('127.0.0.1'))
|
|
|
|
self.assertEquals(l[2], ip.ip_interface('::ffff:102:300/128'))
|
|
|
|
self.assert_(isinstance(l[1], ip.IPv4Interface), l)
|
|
|
|
self.assert_(isinstance(l[2], ip.IPv6Interface), l)
|
|
|
|
|
|
|
|
def test_inet_adapt(self):
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
psycopg2.extras.register_ipaddress(cur)
|
|
|
|
|
|
|
|
cur.execute("select %s", [ip.ip_interface('127.0.0.1/24')])
|
|
|
|
self.assertEquals(cur.fetchone()[0], '127.0.0.1/24')
|
|
|
|
|
|
|
|
cur.execute("select %s", [ip.ip_interface('::ffff:102:300/128')])
|
|
|
|
self.assertEquals(cur.fetchone()[0], '::ffff:102:300/128')
|
|
|
|
|
2020-07-28 00:58:43 +03:00
|
|
|
@testutils.skip_if_crdb("cidr")
|
2016-10-11 05:58:09 +03:00
|
|
|
def test_cidr_cast(self):
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
psycopg2.extras.register_ipaddress(cur)
|
|
|
|
|
|
|
|
cur.execute("select null::cidr")
|
|
|
|
self.assert_(cur.fetchone()[0] is None)
|
|
|
|
|
|
|
|
cur.execute("select '127.0.0.0/24'::cidr")
|
|
|
|
obj = cur.fetchone()[0]
|
|
|
|
self.assert_(isinstance(obj, ip.IPv4Network), repr(obj))
|
|
|
|
self.assertEquals(obj, ip.ip_network('127.0.0.0/24'))
|
|
|
|
|
|
|
|
cur.execute("select '::ffff:102:300/128'::cidr")
|
|
|
|
obj = cur.fetchone()[0]
|
|
|
|
self.assert_(isinstance(obj, ip.IPv6Network), repr(obj))
|
|
|
|
self.assertEquals(obj, ip.ip_network('::ffff:102:300/128'))
|
|
|
|
|
2020-07-28 00:58:43 +03:00
|
|
|
@testutils.skip_if_crdb("cidr")
|
2017-02-06 23:33:35 +03:00
|
|
|
@testutils.skip_before_postgres(8, 2)
|
2016-10-11 05:58:09 +03:00
|
|
|
def test_cidr_array_cast(self):
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
psycopg2.extras.register_ipaddress(cur)
|
|
|
|
cur.execute("select '{NULL,127.0.0.1,::ffff:102:300/128}'::cidr[]")
|
|
|
|
l = cur.fetchone()[0]
|
|
|
|
self.assert_(l[0] is None)
|
|
|
|
self.assertEquals(l[1], ip.ip_network('127.0.0.1'))
|
|
|
|
self.assertEquals(l[2], ip.ip_network('::ffff:102:300/128'))
|
|
|
|
self.assert_(isinstance(l[1], ip.IPv4Network), l)
|
|
|
|
self.assert_(isinstance(l[2], ip.IPv6Network), l)
|
|
|
|
|
|
|
|
def test_cidr_adapt(self):
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
psycopg2.extras.register_ipaddress(cur)
|
|
|
|
|
|
|
|
cur.execute("select %s", [ip.ip_network('127.0.0.0/24')])
|
|
|
|
self.assertEquals(cur.fetchone()[0], '127.0.0.0/24')
|
|
|
|
|
|
|
|
cur.execute("select %s", [ip.ip_network('::ffff:102:300/128')])
|
|
|
|
self.assertEquals(cur.fetchone()[0], '::ffff:102:300/128')
|
|
|
|
|
2018-10-23 02:39:14 +03:00
|
|
|
|
2016-10-11 05:58:09 +03:00
|
|
|
def test_suite():
|
|
|
|
return unittest.TestLoader().loadTestsFromName(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|