From: Michael Paquier Date: Tue, 28 Jan 2025 23:49:48 +0000 (+0900) Subject: Improve test coverage of network address functions X-Git-Tag: REL_18_BETA1~992 X-Git-Url: http://git.postgresql.org/gitweb/?a=commitdiff_plain;h=4f071349c0c28543fa8b84a5ce0b2f619c089ace;p=postgresql.git Improve test coverage of network address functions The following functions were not covered by any tests: - abbrev(inet) - set_masklen(cidr) - set_masklen(inet) - netmask(inet) - hostmask(inet) While on it, this improves the output of some of the existing queries in the test inet to use better aliases. Author: Aleksander Alekseev Reviewed-by: Jacob Champion, Keisuke Kuroda, Tom Lane Discussion: https://postgr.es/m/CAJ7c6TOyZ9bGNrDK6Z3Q0gr9ow8ZpOm+=+01mpE0dsdH4C+u9A@mail.gmail.com --- diff --git a/src/test/regress/expected/inet.out b/src/test/regress/expected/inet.out index b6895d9ced0..1705bff4dd3 100644 --- a/src/test/regress/expected/inet.out +++ b/src/test/regress/expected/inet.out @@ -88,31 +88,32 @@ SELECT i AS inet, host(i), text(i), family(i) FROM INET_TBL; ::4.3.2.1/24 | ::4.3.2.1 | ::4.3.2.1/24 | 6 (17 rows) -SELECT c AS cidr, abbrev(c) FROM INET_TBL; - cidr | abbrev ---------------------+-------------------- - 192.168.1.0/24 | 192.168.1/24 - 192.168.1.0/26 | 192.168.1.0/26 - 192.168.1.0/24 | 192.168.1/24 - 192.168.1.0/24 | 192.168.1/24 - 192.168.1.0/24 | 192.168.1/24 - 192.168.1.0/24 | 192.168.1/24 - 10.0.0.0/8 | 10/8 - 10.0.0.0/32 | 10.0.0.0/32 - 10.1.2.3/32 | 10.1.2.3/32 - 10.1.2.0/24 | 10.1.2/24 - 10.1.0.0/16 | 10.1/16 - 10.0.0.0/8 | 10/8 - 10.0.0.0/8 | 10/8 - 10.0.0.0/8 | 10/8 - 10:23::f1/128 | 10:23::f1/128 - 10:23::8000/113 | 10:23::8000/113 - ::ffff:1.2.3.4/128 | ::ffff:1.2.3.4/128 +SELECT c AS cidr, abbrev(c) AS "abbrev(cidr)", + i AS inet, abbrev(i) AS "abbrev(inet)" FROM INET_TBL; + cidr | abbrev(cidr) | inet | abbrev(inet) +--------------------+--------------------+------------------+------------------ + 192.168.1.0/24 | 192.168.1/24 | 192.168.1.226/24 | 192.168.1.226/24 + 192.168.1.0/26 | 192.168.1.0/26 | 192.168.1.226 | 192.168.1.226 + 192.168.1.0/24 | 192.168.1/24 | 192.168.1.0/24 | 192.168.1.0/24 + 192.168.1.0/24 | 192.168.1/24 | 192.168.1.0/25 | 192.168.1.0/25 + 192.168.1.0/24 | 192.168.1/24 | 192.168.1.255/24 | 192.168.1.255/24 + 192.168.1.0/24 | 192.168.1/24 | 192.168.1.255/25 | 192.168.1.255/25 + 10.0.0.0/8 | 10/8 | 10.1.2.3/8 | 10.1.2.3/8 + 10.0.0.0/32 | 10.0.0.0/32 | 10.1.2.3/8 | 10.1.2.3/8 + 10.1.2.3/32 | 10.1.2.3/32 | 10.1.2.3 | 10.1.2.3 + 10.1.2.0/24 | 10.1.2/24 | 10.1.2.3/24 | 10.1.2.3/24 + 10.1.0.0/16 | 10.1/16 | 10.1.2.3/16 | 10.1.2.3/16 + 10.0.0.0/8 | 10/8 | 10.1.2.3/8 | 10.1.2.3/8 + 10.0.0.0/8 | 10/8 | 11.1.2.3/8 | 11.1.2.3/8 + 10.0.0.0/8 | 10/8 | 9.1.2.3/8 | 9.1.2.3/8 + 10:23::f1/128 | 10:23::f1/128 | 10:23::f1/64 | 10:23::f1/64 + 10:23::8000/113 | 10:23::8000/113 | 10:23::ffff | 10:23::ffff + ::ffff:1.2.3.4/128 | ::ffff:1.2.3.4/128 | ::4.3.2.1/24 | ::4.3.2.1/24 (17 rows) -SELECT c AS cidr, broadcast(c), - i AS inet, broadcast(i) FROM INET_TBL; - cidr | broadcast | inet | broadcast +SELECT c AS cidr, broadcast(c) AS "broadcast(cidr)", + i AS inet, broadcast(i) AS "broadcast(inet)" FROM INET_TBL; + cidr | broadcast(cidr) | inet | broadcast(inet) --------------------+------------------+------------------+--------------------------------------- 192.168.1.0/24 | 192.168.1.255/24 | 192.168.1.226/24 | 192.168.1.255/24 192.168.1.0/26 | 192.168.1.63/26 | 192.168.1.226 | 192.168.1.226 @@ -190,6 +191,50 @@ SELECT c AS cidr, masklen(c) AS "masklen(cidr)", 10.0.0.0/8 | 8 | 9.1.2.3/8 | 8 (4 rows) +SELECT i AS inet, netmask(i) AS "netmask(inet)" FROM INET_TBL; + inet | netmask(inet) +------------------+----------------------------------------- + 192.168.1.226/24 | 255.255.255.0 + 192.168.1.226 | 255.255.255.255 + 192.168.1.0/24 | 255.255.255.0 + 192.168.1.0/25 | 255.255.255.128 + 192.168.1.255/24 | 255.255.255.0 + 192.168.1.255/25 | 255.255.255.128 + 10.1.2.3/8 | 255.0.0.0 + 10.1.2.3/8 | 255.0.0.0 + 10.1.2.3 | 255.255.255.255 + 10.1.2.3/24 | 255.255.255.0 + 10.1.2.3/16 | 255.255.0.0 + 10.1.2.3/8 | 255.0.0.0 + 11.1.2.3/8 | 255.0.0.0 + 9.1.2.3/8 | 255.0.0.0 + 10:23::f1/64 | ffff:ffff:ffff:ffff:: + 10:23::ffff | ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff + ::4.3.2.1/24 | ffff:ff00:: +(17 rows) + +SELECT i AS inet, hostmask(i) AS "hostmask(inet)" FROM INET_TBL; + inet | hostmask(inet) +------------------+------------------------------------ + 192.168.1.226/24 | 0.0.0.255 + 192.168.1.226 | 0.0.0.0 + 192.168.1.0/24 | 0.0.0.255 + 192.168.1.0/25 | 0.0.0.127 + 192.168.1.255/24 | 0.0.0.255 + 192.168.1.255/25 | 0.0.0.127 + 10.1.2.3/8 | 0.255.255.255 + 10.1.2.3/8 | 0.255.255.255 + 10.1.2.3 | 0.0.0.0 + 10.1.2.3/24 | 0.0.0.255 + 10.1.2.3/16 | 0.0.255.255 + 10.1.2.3/8 | 0.255.255.255 + 11.1.2.3/8 | 0.255.255.255 + 9.1.2.3/8 | 0.255.255.255 + 10:23::f1/64 | ::ffff:ffff:ffff:ffff + 10:23::ffff | :: + ::4.3.2.1/24 | 0:ff:ffff:ffff:ffff:ffff:ffff:ffff +(17 rows) + SELECT c AS cidr, i AS inet FROM INET_TBL WHERE c = i; cidr | inet @@ -238,29 +283,59 @@ SELECT max(c) AS max, min(c) AS min FROM INET_TBL; 10:23::8000/113 | 10.0.0.0/8 (1 row) --- check the conversion to/from text and set_netmask -SELECT set_masklen(inet(text(i)), 24) FROM INET_TBL; - set_masklen ------------------- - 192.168.1.226/24 - 192.168.1.226/24 - 192.168.1.0/24 - 192.168.1.0/24 - 192.168.1.255/24 - 192.168.1.255/24 - 10.1.2.3/24 - 10.1.2.3/24 - 10.1.2.3/24 - 10.1.2.3/24 - 10.1.2.3/24 - 10.1.2.3/24 - 11.1.2.3/24 - 9.1.2.3/24 - 10:23::f1/24 - 10:23::ffff/24 - ::4.3.2.1/24 +-- check the conversion to/from text and setting netmask +SELECT c AS cidr, set_masklen(cidr(text(c)), 24) AS "set_masklen(cidr)", + i AS inet, set_masklen(inet(text(i)), 24) AS "set_masklen(inet)" FROM INET_TBL; + cidr | set_masklen(cidr) | inet | set_masklen(inet) +--------------------+-------------------+------------------+------------------- + 192.168.1.0/24 | 192.168.1.0/24 | 192.168.1.226/24 | 192.168.1.226/24 + 192.168.1.0/26 | 192.168.1.0/24 | 192.168.1.226 | 192.168.1.226/24 + 192.168.1.0/24 | 192.168.1.0/24 | 192.168.1.0/24 | 192.168.1.0/24 + 192.168.1.0/24 | 192.168.1.0/24 | 192.168.1.0/25 | 192.168.1.0/24 + 192.168.1.0/24 | 192.168.1.0/24 | 192.168.1.255/24 | 192.168.1.255/24 + 192.168.1.0/24 | 192.168.1.0/24 | 192.168.1.255/25 | 192.168.1.255/24 + 10.0.0.0/8 | 10.0.0.0/24 | 10.1.2.3/8 | 10.1.2.3/24 + 10.0.0.0/32 | 10.0.0.0/24 | 10.1.2.3/8 | 10.1.2.3/24 + 10.1.2.3/32 | 10.1.2.0/24 | 10.1.2.3 | 10.1.2.3/24 + 10.1.2.0/24 | 10.1.2.0/24 | 10.1.2.3/24 | 10.1.2.3/24 + 10.1.0.0/16 | 10.1.0.0/24 | 10.1.2.3/16 | 10.1.2.3/24 + 10.0.0.0/8 | 10.0.0.0/24 | 10.1.2.3/8 | 10.1.2.3/24 + 10.0.0.0/8 | 10.0.0.0/24 | 11.1.2.3/8 | 11.1.2.3/24 + 10.0.0.0/8 | 10.0.0.0/24 | 9.1.2.3/8 | 9.1.2.3/24 + 10:23::f1/128 | 10::/24 | 10:23::f1/64 | 10:23::f1/24 + 10:23::8000/113 | 10::/24 | 10:23::ffff | 10:23::ffff/24 + ::ffff:1.2.3.4/128 | ::/24 | ::4.3.2.1/24 | ::4.3.2.1/24 +(17 rows) + +-- check that netmask is treated as maximum value when set to -1 +SELECT c AS cidr, set_masklen(cidr(text(c)), -1) AS "set_masklen(cidr)", + i AS inet, set_masklen(inet(text(i)), -1) AS "set_masklen(inet)" FROM INET_TBL; + cidr | set_masklen(cidr) | inet | set_masklen(inet) +--------------------+--------------------+------------------+------------------- + 192.168.1.0/24 | 192.168.1.0/32 | 192.168.1.226/24 | 192.168.1.226 + 192.168.1.0/26 | 192.168.1.0/32 | 192.168.1.226 | 192.168.1.226 + 192.168.1.0/24 | 192.168.1.0/32 | 192.168.1.0/24 | 192.168.1.0 + 192.168.1.0/24 | 192.168.1.0/32 | 192.168.1.0/25 | 192.168.1.0 + 192.168.1.0/24 | 192.168.1.0/32 | 192.168.1.255/24 | 192.168.1.255 + 192.168.1.0/24 | 192.168.1.0/32 | 192.168.1.255/25 | 192.168.1.255 + 10.0.0.0/8 | 10.0.0.0/32 | 10.1.2.3/8 | 10.1.2.3 + 10.0.0.0/32 | 10.0.0.0/32 | 10.1.2.3/8 | 10.1.2.3 + 10.1.2.3/32 | 10.1.2.3/32 | 10.1.2.3 | 10.1.2.3 + 10.1.2.0/24 | 10.1.2.0/32 | 10.1.2.3/24 | 10.1.2.3 + 10.1.0.0/16 | 10.1.0.0/32 | 10.1.2.3/16 | 10.1.2.3 + 10.0.0.0/8 | 10.0.0.0/32 | 10.1.2.3/8 | 10.1.2.3 + 10.0.0.0/8 | 10.0.0.0/32 | 11.1.2.3/8 | 11.1.2.3 + 10.0.0.0/8 | 10.0.0.0/32 | 9.1.2.3/8 | 9.1.2.3 + 10:23::f1/128 | 10:23::f1/128 | 10:23::f1/64 | 10:23::f1 + 10:23::8000/113 | 10:23::8000/128 | 10:23::ffff | 10:23::ffff + ::ffff:1.2.3.4/128 | ::ffff:1.2.3.4/128 | ::4.3.2.1/24 | ::4.3.2.1 (17 rows) +-- check that invalid netmask is rejected +SELECT set_masklen(inet(text(i)), 33) FROM INET_TBL; +ERROR: invalid mask length: 33 +SELECT set_masklen(cidr(text(c)), 33) FROM INET_TBL; +ERROR: invalid mask length: 33 -- check that btree index works correctly CREATE INDEX inet_idx1 ON inet_tbl(i); SET enable_seqscan TO off; diff --git a/src/test/regress/sql/inet.sql b/src/test/regress/sql/inet.sql index 3910eac3bc4..8f276856df9 100644 --- a/src/test/regress/sql/inet.sql +++ b/src/test/regress/sql/inet.sql @@ -34,9 +34,10 @@ SELECT c AS cidr, i AS inet FROM INET_TBL; -- now test some support functions SELECT i AS inet, host(i), text(i), family(i) FROM INET_TBL; -SELECT c AS cidr, abbrev(c) FROM INET_TBL; -SELECT c AS cidr, broadcast(c), - i AS inet, broadcast(i) FROM INET_TBL; +SELECT c AS cidr, abbrev(c) AS "abbrev(cidr)", + i AS inet, abbrev(i) AS "abbrev(inet)" FROM INET_TBL; +SELECT c AS cidr, broadcast(c) AS "broadcast(cidr)", + i AS inet, broadcast(i) AS "broadcast(inet)" FROM INET_TBL; SELECT c AS cidr, network(c) AS "network(cidr)", i AS inet, network(i) AS "network(inet)" FROM INET_TBL; SELECT c AS cidr, masklen(c) AS "masklen(cidr)", @@ -46,6 +47,9 @@ SELECT c AS cidr, masklen(c) AS "masklen(cidr)", i AS inet, masklen(i) AS "masklen(inet)" FROM INET_TBL WHERE masklen(c) <= 8; +SELECT i AS inet, netmask(i) AS "netmask(inet)" FROM INET_TBL; +SELECT i AS inet, hostmask(i) AS "hostmask(inet)" FROM INET_TBL; + SELECT c AS cidr, i AS inet FROM INET_TBL WHERE c = i; @@ -60,8 +64,15 @@ SELECT i, c, SELECT max(i) AS max, min(i) AS min FROM INET_TBL; SELECT max(c) AS max, min(c) AS min FROM INET_TBL; --- check the conversion to/from text and set_netmask -SELECT set_masklen(inet(text(i)), 24) FROM INET_TBL; +-- check the conversion to/from text and setting netmask +SELECT c AS cidr, set_masklen(cidr(text(c)), 24) AS "set_masklen(cidr)", + i AS inet, set_masklen(inet(text(i)), 24) AS "set_masklen(inet)" FROM INET_TBL; +-- check that netmask is treated as maximum value when set to -1 +SELECT c AS cidr, set_masklen(cidr(text(c)), -1) AS "set_masklen(cidr)", + i AS inet, set_masklen(inet(text(i)), -1) AS "set_masklen(inet)" FROM INET_TBL; +-- check that invalid netmask is rejected +SELECT set_masklen(inet(text(i)), 33) FROM INET_TBL; +SELECT set_masklen(cidr(text(c)), 33) FROM INET_TBL; -- check that btree index works correctly CREATE INDEX inet_idx1 ON inet_tbl(i);