git: 7aaa37fa1db6 - stable/14 - lib/libc/tests/string/memcmp_test.c: extend test to support custom memcmp function

From: Robert Clausecker <fuz_at_FreeBSD.org>
Date: Thu, 28 Dec 2023 17:20:11 UTC
The branch stable/14 has been updated by fuz:

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

commit 7aaa37fa1db603fe10143b03ed07f3a8db771484
Author:     Robert Clausecker <fuz@FreeBSD.org>
AuthorDate: 2023-07-14 11:56:12 +0000
Commit:     Robert Clausecker <fuz@FreeBSD.org>
CommitDate: 2023-12-28 17:02:41 +0000

    lib/libc/tests/string/memcmp_test.c: extend test to support custom memcmp function
    
    Extend the tests to permit loading an external memcmp function
    and testing it over using the libc version. This was added by the
    example of other tests in the test suite doing the same thing and
    helped tremendously in development.
    
    This change was originally part of D41442 but was taken out to
    permit separate review as extrapolated from @ngie's request in
    D41349.
    
    Sponsored by:   FreeBSD Foundation
    Approved by:    ngie
    Differential Revision:  https://reviews.freebsd.org/D41528
    
    (cherry picked from commit b166580681e3af173ec368656019d02ba1cc55ad)
---
 lib/libc/tests/string/memcmp_test.c | 57 +++++++++++++++++++++----------------
 1 file changed, 33 insertions(+), 24 deletions(-)

diff --git a/lib/libc/tests/string/memcmp_test.c b/lib/libc/tests/string/memcmp_test.c
index bfe209dd1b85..824e0d27ac0e 100644
--- a/lib/libc/tests/string/memcmp_test.c
+++ b/lib/libc/tests/string/memcmp_test.c
@@ -25,18 +25,21 @@
  */
 
 #include <assert.h>
+#include <dlfcn.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
 #include <atf-c.h>
 
+static int (*memcmp_fn)(const void *, const void *, size_t);
+
 ATF_TC_WITHOUT_HEAD(zero);
 ATF_TC_BODY(zero, tc)
 {
 
-	assert(memcmp("a", "b", 0) == 0);
-	assert(memcmp("", "", 0) == 0);
+	assert(memcmp_fn("a", "b", 0) == 0);
+	assert(memcmp_fn("", "", 0) == 0);
 }
 
 ATF_TC_WITHOUT_HEAD(eq);
@@ -48,9 +51,9 @@ ATF_TC_BODY(eq, tc)
 	for (i = 0; i < 256; i++)
 		data1[i] = data2[i] = i ^ 0x55;
 	for (i = 1; i < 256; i++)
-		assert(memcmp(data1, data2, i) == 0);
+		assert(memcmp_fn(data1, data2, i) == 0);
 	for (i = 1; i < 256; i++)
-		assert(memcmp(data1 + i, data2 + i, 256 - i) == 0);
+		assert(memcmp_fn(data1 + i, data2 + i, 256 - i) == 0);
 }
 
 ATF_TC_WITHOUT_HEAD(neq);
@@ -64,9 +67,9 @@ ATF_TC_BODY(neq, tc)
 		data2[i] = i ^ 0x55;
 	}
 	for (i = 1; i < 256; i++)
-		assert(memcmp(data1, data2, i) != 0);
+		assert(memcmp_fn(data1, data2, i) != 0);
 	for (i = 1; i < 256; i++)
-		assert(memcmp(data1 + i, data2 + i, 256 - i) != 0);
+		assert(memcmp_fn(data1 + i, data2 + i, 256 - i) != 0);
 }
 
 ATF_TC_WITHOUT_HEAD(diff);
@@ -80,37 +83,43 @@ ATF_TC_BODY(diff, tc)
 	data1[128] = 255;
 	data2[128] = 0;
 	for (i = 1; i < 66; i++) {
-		assert(memcmp(data1 + 128, data2 + 128, i) == 255);
-		assert(memcmp(data2 + 128, data1 + 128, i) == -255);
-		assert(memcmp(data1 + 129 - i, data2 + 129 - i, i) == 255);
-		assert(memcmp(data2 + 129 - i, data1 + 129 - i, i) == -255);
-		assert(memcmp(data1 + 129 - i, data2 + 129 - i, i * 2) == 255);
-		assert(memcmp(data2 + 129 - i, data1 + 129 - i, i * 2) == -255);
+		assert(memcmp_fn(data1 + 128, data2 + 128, i) == 255);
+		assert(memcmp_fn(data2 + 128, data1 + 128, i) == -255);
+		assert(memcmp_fn(data1 + 129 - i, data2 + 129 - i, i) == 255);
+		assert(memcmp_fn(data2 + 129 - i, data1 + 129 - i, i) == -255);
+		assert(memcmp_fn(data1 + 129 - i, data2 + 129 - i, i * 2) == 255);
+		assert(memcmp_fn(data2 + 129 - i, data1 + 129 - i, i * 2) == -255);
 	}
 	data1[128] = 'c';
 	data2[128] = 'e';
 	for (i = 1; i < 66; i++) {
-		assert(memcmp(data1 + 128, data2 + 128, i) == -2);
-		assert(memcmp(data2 + 128, data1 + 128, i) == 2);
-		assert(memcmp(data1 + 129 - i, data2 + 129 - i, i) == -2);
-		assert(memcmp(data2 + 129 - i, data1 + 129 - i, i) == 2);
-		assert(memcmp(data1 + 129 - i, data2 + 129 - i, i * 2) == -2);
-		assert(memcmp(data2 + 129 - i, data1 + 129 - i, i * 2) == 2);
+		assert(memcmp_fn(data1 + 128, data2 + 128, i) == -2);
+		assert(memcmp_fn(data2 + 128, data1 + 128, i) == 2);
+		assert(memcmp_fn(data1 + 129 - i, data2 + 129 - i, i) == -2);
+		assert(memcmp_fn(data2 + 129 - i, data1 + 129 - i, i) == 2);
+		assert(memcmp_fn(data1 + 129 - i, data2 + 129 - i, i * 2) == -2);
+		assert(memcmp_fn(data2 + 129 - i, data1 + 129 - i, i * 2) == 2);
 	}
 	memset(data1 + 129, 'A', sizeof(data1) - 129);
 	memset(data2 + 129, 'Z', sizeof(data2) - 129);
 	for (i = 1; i < 66; i++) {
-		assert(memcmp(data1 + 128, data2 + 128, i) == -2);
-		assert(memcmp(data2 + 128, data1 + 128, i) == 2);
-		assert(memcmp(data1 + 129 - i, data2 + 129 - i, i) == -2);
-		assert(memcmp(data2 + 129 - i, data1 + 129 - i, i) == 2);
-		assert(memcmp(data1 + 129 - i, data2 + 129 - i, i * 2) == -2);
-		assert(memcmp(data2 + 129 - i, data1 + 129 - i, i * 2) == 2);
+		assert(memcmp_fn(data1 + 128, data2 + 128, i) == -2);
+		assert(memcmp_fn(data2 + 128, data1 + 128, i) == 2);
+		assert(memcmp_fn(data1 + 129 - i, data2 + 129 - i, i) == -2);
+		assert(memcmp_fn(data2 + 129 - i, data1 + 129 - i, i) == 2);
+		assert(memcmp_fn(data1 + 129 - i, data2 + 129 - i, i * 2) == -2);
+		assert(memcmp_fn(data2 + 129 - i, data1 + 129 - i, i * 2) == 2);
 	}
 }
 
 ATF_TP_ADD_TCS(tp)
 {
+	void *dl_handle;
+
+	dl_handle = dlopen(NULL, RTLD_LAZY);
+	memcmp_fn = dlsym(dl_handle, "test_memcmp");
+	if (memcmp_fn == NULL)
+		memcmp_fn = memcmp;
 
 	ATF_TP_ADD_TC(tp, zero);
 	ATF_TP_ADD_TC(tp, eq);