From 8e0c10ee607cfea17167fbcd540b18d7f23d9fec Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Thu, 28 Sep 2017 00:31:04 +0200 Subject: [PATCH 2/2] Tests for module 'strlcpy'. * tests/test-strlcpy.c: New file, based on tests/unistr/test-strncpy.h. * modules/strlcpy-tests: New file. * tests/test-string-c++.cc: Check the signature of strlcpy. --- ChangeLog | 5 ++ modules/strlcpy-tests | 19 ++++++++ tests/test-string-c++.cc | 5 ++ tests/test-strlcpy.c | 117 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 146 insertions(+) create mode 100644 modules/strlcpy-tests create mode 100644 tests/test-strlcpy.c diff --git a/ChangeLog b/ChangeLog index c005f1c..8fa21e8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,10 @@ 2017-09-27 Bruno Haible + Tests for module 'strlcpy'. + * tests/test-strlcpy.c: New file, based on tests/unistr/test-strncpy.h. + * modules/strlcpy-tests: New file. + * tests/test-string-c++.cc: Check the signature of strlcpy. + New module 'strlcpy'. * lib/string.in.h (_GL_ATTRIBUTE_RETURN_CHECK): New macro. (strlcpy): New declaration. diff --git a/modules/strlcpy-tests b/modules/strlcpy-tests new file mode 100644 index 0000000..fab82e2 --- /dev/null +++ b/modules/strlcpy-tests @@ -0,0 +1,19 @@ +Files: +tests/test-strlcpy.c +tests/zerosize-ptr.h +tests/macros.h +m4/mmap-anon.m4 + +Depends-on: +extensions +getpagesize + +configure.ac: +gl_FUNC_MMAP_ANON +AC_CHECK_HEADERS_ONCE([sys/mman.h]) +AC_CHECK_FUNCS_ONCE([mprotect]) + +Makefile.am: +TESTS += test-strlcpy +check_PROGRAMS += test-strlcpy +test_strlcpy_SOURCES = test-strlcpy.c diff --git a/tests/test-string-c++.cc b/tests/test-string-c++.cc index 877cf68..c3df903 100644 --- a/tests/test-string-c++.cc +++ b/tests/test-string-c++.cc @@ -142,6 +142,11 @@ SIGNATURE_CHECK (GNULIB_NAMESPACE::strerror_r, int, (int, char *, size_t)); SIGNATURE_CHECK (GNULIB_NAMESPACE::strsignal, char *, (int)); #endif +#if GNULIB_TEST_STRLCPY +SIGNATURE_CHECK (GNULIB_NAMESPACE::strlcpy, size_t, + (char *, const char *, size_t)); +#endif + #if GNULIB_TEST_STRVERSCMP SIGNATURE_CHECK (GNULIB_NAMESPACE::strverscmp, int, (const char *, const char *)); diff --git a/tests/test-strlcpy.c b/tests/test-strlcpy.c new file mode 100644 index 0000000..9d5eee9 --- /dev/null +++ b/tests/test-strlcpy.c @@ -0,0 +1,117 @@ +/* Tests of strlcpy(). + Copyright (C) 2010-2017 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +/* Written by Bruno Haible . */ + +#include + +#include + +#include "signature.h" +SIGNATURE_CHECK (strlcpy, size_t, (char *, const char *, size_t)); + +#include + +#include "zerosize-ptr.h" +#include "macros.h" + +#define MAGIC (char)0xBA + +static void +check_single (const char *input, size_t length, size_t n) +{ + char *dest; + size_t result; + size_t i; + + dest = (char *) malloc (1 + n + 1); + ASSERT (dest != NULL); + + for (i = 0; i < 1 + n + 1; i++) + dest[i] = MAGIC; + + result = strlcpy (dest + 1, input, n); + ASSERT (result == length); + + ASSERT (dest[0] == MAGIC); + i = 0; + if (n > 0) + { + for (; i < (n <= length ? n - 1 : length); i++) + ASSERT (dest[1 + i] == input[i]); + ASSERT (dest[1 + i] == '\0'); + i++; + } + for (; i < n + 1; i++) + ASSERT (dest[1 + i] == MAGIC); + + free (dest); +} + +static void +check (const char *input, size_t input_length) +{ + size_t length; + size_t n; + + ASSERT (input_length > 0); + ASSERT (input[input_length - 1] == 0); + length = input_length - 1; /* = strlen (input) */ + + for (n = 0; n <= 2 * length + 2; n++) + check_single (input, length, n); + + /* Check that strlcpy (D, S, N) does not look at more than + strlen (S) + 1 bytes. */ + { + char *page_boundary = (char *) zerosize_ptr (); + + if (page_boundary != NULL) + { + size_t bytes_to_copy = length + 1; + char *copy; + size_t i; + + copy = (char *) page_boundary - bytes_to_copy; + for (i = 0; i < bytes_to_copy; i++) + copy[i] = input[i]; + + for (n = 0; n <= 2 * length + 2; n++) + check_single (copy, length, n); + } + } +} + +int +main () +{ + /* Simple string. */ + { /* "Grüß Gott. Здравствуйте! x=(-b±sqrt(b²-4ac))/(2a) 日本語,中文,한글" */ + static const char input[] = + { 'G', 'r', 0xC3, 0xBC, 0xC3, 0x9F, ' ', 'G', 'o', 't', 't', '.', ' ', + 0xD0, 0x97, 0xD0, 0xB4, 0xD1, 0x80, 0xD0, 0xB0, 0xD0, 0xB2, 0xD1, 0x81, + 0xD1, 0x82, 0xD0, 0xB2, 0xD1, 0x83, 0xD0, 0xB9, 0xD1, 0x82, 0xD0, 0xB5, + '!', ' ', 'x', '=', '(', '-', 'b', 0xC2, 0xB1, 's', 'q', 'r', 't', '(', + 'b', 0xC2, 0xB2, '-', '4', 'a', 'c', ')', ')', '/', '(', '2', 'a', ')', + ' ', ' ', 0xE6, 0x97, 0xA5, 0xE6, 0x9C, 0xAC, 0xE8, 0xAA, 0x9E, ',', + 0xE4, 0xB8, 0xAD, 0xE6, 0x96, 0x87, ',', + 0xED, 0x95, 0x9C, 0xEA, 0xB8, 0x80, '\0' + }; + check (input, SIZEOF (input)); + } + + return 0; +} -- 2.7.4