Hi hackers,

Recently I played with lcov [1]. In the process it was discovered that
the following functions are not executed by our tests:

- abbrev(inet)
- set_masklen(cidr,int4)
- netmask(inet)
- hostmask(inet)
- inet_client_addr()
- inet_client_port()
- inet_server_addr()
- inet_server_port()

The proposed patch fixes this. For the last four functions the return
values are not checked, only the fact that the functions are callable
and don't crash.

This improves code coverage of src/backend/utils/adt/network.c from
69.8% to 80.1%.

[1]: 
https://postgr.es/m/CAJ7c6TPYPF93%2ByWi%3DThKiOsnhqLpeTmctMrJWz3xRQobGSY6BA%40mail.gmail.com

--
Best regards,
Aleksander Alekseev
From e3a19689a335bf5c0c6aecd19e6fe3d17d456b2b Mon Sep 17 00:00:00 2001
From: Aleksander Alekseev <aleksander@timescale.com>
Date: Thu, 31 Oct 2024 17:54:56 +0300
Subject: [PATCH v1] Improve code coverage of network address functions

The following functions were not covered by tests:

- abbrev(inet)
- set_masklen(cidr,int4)
- netmask(inet)
- hostmask(inet)
- inet_client_addr()
- inet_client_port()
- inet_server_addr()
- inet_server_port()

Correct this.

Aleksander Alekseev, reviewed by TODO FIXME
Discussion: TODO FIXME
---
 src/test/regress/expected/inet.out | 104 +++++++++++++++++++++++++++++
 src/test/regress/sql/inet.sql      |  14 ++++
 2 files changed, 118 insertions(+)

diff --git a/src/test/regress/expected/inet.out b/src/test/regress/expected/inet.out
index b6895d9ced..c903c6c93b 100644
--- a/src/test/regress/expected/inet.out
+++ b/src/test/regress/expected/inet.out
@@ -190,6 +190,72 @@ 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, abbrev(i) AS "abbrev(inet)" FROM INET_TBL;
+       inet       |   abbrev(inet)   
+------------------+------------------
+ 192.168.1.226/24 | 192.168.1.226/24
+ 192.168.1.226    | 192.168.1.226
+ 192.168.1.0/24   | 192.168.1.0/24
+ 192.168.1.0/25   | 192.168.1.0/25
+ 192.168.1.255/24 | 192.168.1.255/24
+ 192.168.1.255/25 | 192.168.1.255/25
+ 10.1.2.3/8       | 10.1.2.3/8
+ 10.1.2.3/8       | 10.1.2.3/8
+ 10.1.2.3         | 10.1.2.3
+ 10.1.2.3/24      | 10.1.2.3/24
+ 10.1.2.3/16      | 10.1.2.3/16
+ 10.1.2.3/8       | 10.1.2.3/8
+ 11.1.2.3/8       | 11.1.2.3/8
+ 9.1.2.3/8        | 9.1.2.3/8
+ 10:23::f1/64     | 10:23::f1/64
+ 10:23::ffff      | 10:23::ffff
+ ::4.3.2.1/24     | ::4.3.2.1/24
+(17 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      
@@ -261,6 +327,44 @@ SELECT set_masklen(inet(text(i)), 24) FROM INET_TBL;
  ::4.3.2.1/24
 (17 rows)
 
+SELECT set_masklen(cidr(text(c)), 24) FROM INET_TBL;
+  set_masklen   
+----------------
+ 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
+ 10.0.0.0/24
+ 10.0.0.0/24
+ 10.1.2.0/24
+ 10.1.2.0/24
+ 10.1.0.0/24
+ 10.0.0.0/24
+ 10.0.0.0/24
+ 10.0.0.0/24
+ 10::/24
+ 10::/24
+ ::/24
+(17 rows)
+
+-- just making sure these functions are callable and don't crash
+SELECT 'ok' AS result
+FROM unnest(ARRAY[
+    text(inet_client_addr()),
+    text(inet_client_port()),
+    text(inet_server_addr()),
+    text(inet_server_port())
+  ]) AS x;
+ result 
+--------
+ ok
+ ok
+ ok
+ ok
+(4 rows)
+
 -- 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 3910eac3bc..abf6a6ddae 100644
--- a/src/test/regress/sql/inet.sql
+++ b/src/test/regress/sql/inet.sql
@@ -46,6 +46,10 @@ 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, abbrev(i) AS "abbrev(inet)" FROM INET_TBL;
+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;
 
@@ -62,6 +66,16 @@ 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;
+SELECT set_masklen(cidr(text(c)), 24) FROM INET_TBL;
+
+-- just making sure these functions are callable and don't crash
+SELECT 'ok' AS result
+FROM unnest(ARRAY[
+    text(inet_client_addr()),
+    text(inet_client_port()),
+    text(inet_server_addr()),
+    text(inet_server_port())
+  ]) AS x;
 
 -- check that btree index works correctly
 CREATE INDEX inet_idx1 ON inet_tbl(i);
-- 
2.47.0

Reply via email to