git: d55574c91830 - stable/12 - ICMP checksum test: Fix for big endian

From: Alan Somers <asomers_at_FreeBSD.org>
Date: Thu, 07 Oct 2021 17:41:37 UTC
The branch stable/12 has been updated by asomers:

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

commit d55574c91830d12b308664e4a165accffad22e06
Author:     Charlie Root <renato.riolino@eldorado.org.br>
AuthorDate: 2021-01-12 01:56:12 +0000
Commit:     Alan Somers <asomers@FreeBSD.org>
CommitDate: 2021-10-07 17:38:50 +0000

    ICMP checksum test: Fix for big endian
    
    The in_cksum tests originally tried to simulate a BE environment by
    swapping the byte order of the input.  But that's overcomplicated, and
    didn't actually work on real BE hardware.  The correct testing strategy
    is just to test on the native endianness, and run the tests in both BE
    and LE environments.
    
    Submitted by:           Renato Riolino <renato.riolino@eldorado.org.br>
    Reviewed By:            asomers
    Differential Revision:  https://reviews.freebsd.org/D23193
    
    (cherry picked from commit 292808246db702b9194deb8938e40fd06914aea9)
---
 sbin/ping/tests/in_cksum_test.c | 84 ++++++++++-------------------------------
 1 file changed, 20 insertions(+), 64 deletions(-)

diff --git a/sbin/ping/tests/in_cksum_test.c b/sbin/ping/tests/in_cksum_test.c
index fc266545b432..597f6ac20722 100644
--- a/sbin/ping/tests/in_cksum_test.c
+++ b/sbin/ping/tests/in_cksum_test.c
@@ -39,92 +39,52 @@ __FBSDID("$FreeBSD$");
  * Test cases.
  */
 
-ATF_TC_WITHOUT_HEAD(aligned_even_length_big_endian);
-ATF_TC_BODY(aligned_even_length_big_endian, tc)
+ATF_TC_WITHOUT_HEAD(aligned_even_length);
+ATF_TC_BODY(aligned_even_length, tc)
 {
 	u_char data[] __aligned(sizeof(u_short)) =
 		{0x12, 0x34, 0x56, 0x78};
 	u_short sum;
 
 	sum = in_cksum(data, nitems(data));
-	ATF_REQUIRE(sum == 0x5397);
+	u_char *c_sum = (u_char *)&sum;
+	ATF_REQUIRE(c_sum[0] == 0x97 && c_sum[1] == 0x53);
 }
 
-ATF_TC_WITHOUT_HEAD(aligned_odd_length_big_endian);
-ATF_TC_BODY(aligned_odd_length_big_endian, tc)
+ATF_TC_WITHOUT_HEAD(aligned_odd_length);
+ATF_TC_BODY(aligned_odd_length, tc)
 {
 	u_char data[] __aligned(sizeof(u_short)) =
 		{0x12, 0x34, 0x56, 0x78, 0x9a};
 	u_short sum;
 
 	sum = in_cksum(data, nitems(data));
-	ATF_REQUIRE(sum == 0x52fd);
+	u_char *c_sum = (u_char *)&sum;
+	ATF_REQUIRE(c_sum[0] == 0xfd && c_sum[1] == 0x52);
 }
 
-ATF_TC_WITHOUT_HEAD(aligned_even_length_little_endian);
-ATF_TC_BODY(aligned_even_length_little_endian, tc)
-{
-	u_char data[] __aligned(sizeof(u_short)) =
-		{0x34, 0x12, 0x78, 0x56};
-	u_short sum;
-
-	sum = in_cksum(data, nitems(data));
-	ATF_REQUIRE_MSG(sum == 0x9753, "%d", sum);
-}
-
-ATF_TC_WITHOUT_HEAD(aligned_odd_length_little_endian);
-ATF_TC_BODY(aligned_odd_length_little_endian, tc)
-{
-	u_char data[] __aligned(sizeof(u_short)) =
-		{0x34, 0x12, 0x78, 0x56, 0x00, 0x9a};
-	u_short sum;
-
-	sum = in_cksum(data, nitems(data));
-	ATF_REQUIRE(sum == 0xfd52);
-}
-
-ATF_TC_WITHOUT_HEAD(unaligned_even_length_big_endian);
-ATF_TC_BODY(unaligned_even_length_big_endian, tc)
+ATF_TC_WITHOUT_HEAD(unaligned_even_length);
+ATF_TC_BODY(unaligned_even_length, tc)
 {
 	u_char data[] __aligned(sizeof(u_short)) =
 		{0x00, 0x12, 0x34, 0x56, 0x78};
 	u_short sum;
 
 	sum = in_cksum(data + 1, nitems(data) - 1);
-	ATF_REQUIRE(sum == 0x5397);
+	u_char *c_sum = (u_char *)&sum;
+	ATF_REQUIRE(c_sum[0] == 0x97 && c_sum[1] == 0x53);
 }
 
-ATF_TC_WITHOUT_HEAD(unaligned_odd_length_big_endian);
-ATF_TC_BODY(unaligned_odd_length_big_endian, tc)
+ATF_TC_WITHOUT_HEAD(unaligned_odd_length);
+ATF_TC_BODY(unaligned_odd_length, tc)
 {
 	u_char data[] __aligned(sizeof(u_short)) =
 		{0x00, 0x12, 0x34, 0x56, 0x78, 0x9a};
 	u_short sum;
 
 	sum = in_cksum(data + 1, nitems(data) - 1);
-	ATF_REQUIRE(sum == 0x52fd);
-}
-
-ATF_TC_WITHOUT_HEAD(unaligned_even_length_little_endian);
-ATF_TC_BODY(unaligned_even_length_little_endian, tc)
-{
-	u_char data[] __aligned(sizeof(u_short)) =
-		{0x00, 0x34, 0x12, 0x78, 0x56};
-	u_short sum;
-
-	sum = in_cksum(data + 1, nitems(data) - 1);
-	ATF_REQUIRE_MSG(sum == 0x9753, "%d", sum);
-}
-
-ATF_TC_WITHOUT_HEAD(unaligned_odd_length_little_endian);
-ATF_TC_BODY(unaligned_odd_length_little_endian, tc)
-{
-	u_char data[] __aligned(sizeof(u_short)) =
-		{0x00, 0x34, 0x12, 0x78, 0x56, 0x00, 0x9a};
-	u_short sum;
-
-	sum = in_cksum(data + 1, nitems(data) - 1);
-	ATF_REQUIRE(sum == 0xfd52);
+	u_char *c_sum = (u_char *)&sum;
+	ATF_REQUIRE(c_sum[0] == 0xfd && c_sum[1] == 0x52);
 }
 
 /*
@@ -133,14 +93,10 @@ ATF_TC_BODY(unaligned_odd_length_little_endian, tc)
 
 ATF_TP_ADD_TCS(tp)
 {
-	ATF_TP_ADD_TC(tp, aligned_even_length_big_endian);
-	ATF_TP_ADD_TC(tp, aligned_odd_length_big_endian);
-	ATF_TP_ADD_TC(tp, aligned_even_length_little_endian);
-	ATF_TP_ADD_TC(tp, aligned_odd_length_little_endian);
-	ATF_TP_ADD_TC(tp, unaligned_even_length_big_endian);
-	ATF_TP_ADD_TC(tp, unaligned_odd_length_big_endian);
-	ATF_TP_ADD_TC(tp, unaligned_even_length_little_endian);
-	ATF_TP_ADD_TC(tp, unaligned_odd_length_little_endian);
+	ATF_TP_ADD_TC(tp, aligned_even_length);
+	ATF_TP_ADD_TC(tp, aligned_odd_length);
+	ATF_TP_ADD_TC(tp, unaligned_even_length);
+	ATF_TP_ADD_TC(tp, unaligned_odd_length);
 
 	return (atf_no_error());
 }