From 83422b6be41e710016c53db7191feb4dbb0f1756 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Storsj=C3=B6?= Date: Tue, 13 Sep 2022 15:38:52 +0300 Subject: [PATCH] test: crt-test: Add tests for a bunch of string functions These are functions that, when built with "-D_FORTIFY_SOURCE=2 -O2", can end up calling the corresponding ___chk functions - thus, this can be used for testing those functions. --- test/crt-test.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/test/crt-test.c b/test/crt-test.c index e577ff4..86d3aea 100644 --- a/test/crt-test.c +++ b/test/crt-test.c @@ -157,9 +157,83 @@ int main(int argc, char* argv[]) { char buf[200]; int i; uint64_t myconst = 0xbaadf00dcafe; + void *retptr; + memset(buf, '#', sizeof(buf)); + retptr = memcpy(buf, "foo", 4); + TEST(retptr == buf); + TEST_STR(buf, "foo"); + TEST_INT(buf[5], '#'); + +#if defined(__GLIBC__) || defined(__MINGW32__) + memset(buf, '#', sizeof(buf)); + retptr = mempcpy(buf, "foo", 4); + TEST(retptr == buf + 4); + TEST_STR(buf, "foo"); + TEST_INT(buf[5], '#'); +#endif + + memset(buf, '#', sizeof(buf)); + memcpy(buf, "foobar", 7); + retptr = memmove(buf + 2, buf, 3); + TEST(retptr == buf + 2); + TEST_STR(buf, "fofoor"); + TEST_INT(buf[8], '#'); + + memset(buf, '#', sizeof(buf)); + memcpy(buf, "foobar", 7); + retptr = memmove(buf, buf + 2, 3); + TEST(retptr == buf); + TEST_STR(buf, "obabar"); + TEST_INT(buf[8], '#'); + + retptr = memset(buf, '#', sizeof(buf)); + TEST(retptr == buf); + TEST_INT(buf[0], '#'); + TEST_INT(buf[sizeof(buf)-1], '#'); + + memset(buf, '#', sizeof(buf)); + retptr = strcpy(buf, "foo"); + TEST(retptr == buf); + TEST_STR(buf, "foo"); + TEST_INT(buf[5], '#'); + + memset(buf, '#', sizeof(buf)); + retptr = strncpy(buf, "foobar", 3); + TEST(retptr == buf); + TEST_INT(buf[3], '#'); + buf[3] = '\0'; + TEST_STR(buf, "foo"); + + memset(buf, '#', sizeof(buf)); + retptr = strncpy(buf, "foobar", sizeof(buf)); + TEST(retptr == buf); + TEST_STR(buf, "foobar"); + TEST_INT(buf[sizeof(buf)-1], '\0'); + + memset(buf, '#', sizeof(buf)); + strcpy(buf, "foo"); + retptr = strcat(buf, "bar"); + TEST(retptr == buf); + TEST_STR(buf, "foobar"); + + memset(buf, '#', sizeof(buf)); + strcpy(buf, "foo"); + retptr = strncat(buf, "bar", 5); + TEST(retptr == buf); + TEST_STR(buf, "foobar"); + + memset(buf, '#', sizeof(buf)); + strcpy(buf, "foo"); + retptr = strncat(buf, "bar", 2); + TEST(retptr == buf); + TEST_STR(buf, "fooba"); + + memset(buf, '#', sizeof(buf)); snprintf(buf, sizeof(buf), "%f", 3.141592654); TEST_STR(buf, "3.141593"); + TEST_INT(buf[sizeof(buf)-1], '#'); + snprintf(buf, sizeof(buf), "%e", 42.0); TEST_STR(buf, "4.200000e+01"); snprintf(buf, sizeof(buf), "%a", 42.0);