git: 0c74823f5c13 - stable/14 - ping: Fix the spacing between the time stamp and cp/dp

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Mon, 06 Nov 2023 16:45:54 UTC
The branch stable/14 has been updated by markj:

URL: https://cgit.FreeBSD.org/src/commit/?id=0c74823f5c135a039832f62dd50b79625f26f0fe

commit 0c74823f5c135a039832f62dd50b79625f26f0fe
Author:     Jose Luis Duran <jlduran@gmail.com>
AuthorDate: 2023-04-10 16:58:42 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2023-11-06 16:45:44 +0000

    ping: Fix the spacing between the time stamp and cp/dp
    
    When an echo reply packet is received, the data is compared with the
    sent data.  When a wrong byte is detected the command displays a report
    with the differences.
    
    The first row (the first 8-bytes of data after the ICMP header) should
    include the time stamp (if data is at least 8-bytes), this value is not
    taken into consideration for the comparison.  The remaining rows
    represent the data (padded pattern) received/sent, with each byte being
    compared for differences.
    
    Print the space before (not after), to add an extra space after cp:/dp:
    for better readability when the first time stamp octet is not
    zero-padded, and to remove trailing spaces in the output.
    
    Before:
        cp:99  0  0  c  1  5  c  0␣
            ab cd ab cd ab cd ab cd ab cd ab cd ab cd ab cd␣
            ...
    
    After:
        cp: 99  0  0  c  1  5  c  0
             ab cd ab cd ab cd ab cd ab cd ab cd ab cd ab cd
             ...
    
    Reviewed by:    markj
    MFC after:      1 week
    Pull Request:   https://github.com/freebsd/freebsd-src/pull/863
    Differential Revision:  https://reviews.freebsd.org/D39492
    
    (cherry picked from commit 8db2c5802abe14543504e17ab5ed6325088a63f3)
---
 sbin/ping/ping.c             |  4 ++--
 sbin/ping/tests/test_ping.py | 35 +++++++++++++++++++++++++++++++++++
 2 files changed, 37 insertions(+), 2 deletions(-)

diff --git a/sbin/ping/ping.c b/sbin/ping/ping.c
index bbb103a7361f..fcc27d34ee54 100644
--- a/sbin/ping/ping.c
+++ b/sbin/ping/ping.c
@@ -1261,14 +1261,14 @@ pr_pack(char *buf, ssize_t cc, struct sockaddr_in *from, struct timespec *tv)
 					for (i = 0; i < datalen; ++i, ++cp) {
 						if ((i % 16) == 8)
 							(void)printf("\n\t");
-						(void)printf("%2x ", *cp);
+						(void)printf(" %2x", *cp);
 					}
 					(void)printf("\ndp:");
 					cp = &outpack[ICMP_MINLEN];
 					for (i = 0; i < datalen; ++i, ++cp) {
 						if ((i % 16) == 8)
 							(void)printf("\n\t");
-						(void)printf("%2x ", *cp);
+						(void)printf(" %2x", *cp);
 					}
 					break;
 				}
diff --git a/sbin/ping/tests/test_ping.py b/sbin/ping/tests/test_ping.py
index 52be1c7f91ae..00ddbbd63723 100644
--- a/sbin/ping/tests/test_ping.py
+++ b/sbin/ping/tests/test_ping.py
@@ -275,6 +275,8 @@ def redact(output):
         ("hlim=[0-9]*", "hlim="),
         ("ttl=[0-9]*", "ttl="),
         ("time=[0-9.-]*", "time="),
+        ("cp: .*", "cp: xx xx xx xx xx xx xx xx"),
+        ("dp: .*", "dp: xx xx xx xx xx xx xx xx"),
         ("\(-[0-9\.]+[0-9]+ ms\)", "(- ms)"),
         ("[0-9\.]+/[0-9.]+", "/"),
     ]
@@ -1401,6 +1403,39 @@ ping: time of day goes back (- ms), clamping time to 0
             },
             id="_0_0_special_warp",
         ),
+        pytest.param(
+            {
+                "src": "192.0.2.1",
+                "dst": "192.0.2.2",
+                "icmp_type": 0,
+                "icmp_code": 0,
+                "special": "wrong",
+            },
+            {
+                "returncode": 0,
+                "stdout": """\
+PATTERN: 0x01
+PING 192.0.2.2 (192.0.2.2): 56 data bytes
+64 bytes from: icmp_seq=0 ttl= time= ms
+wrong data byte #55 should be 0x1 but was 0x0
+cp: xx xx xx xx xx xx xx xx
+	  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1
+	  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1
+	  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  0
+dp: xx xx xx xx xx xx xx xx
+	  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1
+	  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1
+	  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1
+
+--- 192.0.2.2 ping statistics ---
+1 packets transmitted, 1 packets received, 0.0% packet loss
+round-trip min/avg/max/stddev = /// ms
+""",
+                "stderr": "",
+                "redacted": True,
+            },
+            id="_0_0_special_wrong",
+        ),
     ]
 
     @pytest.mark.parametrize("pinger_kargs, expected", pinger_testdata)