This is a discussion on Regression test for strlcat (was -> Re: submission of patch for strlcat.c) within the mailing.openbsd.tech forums, part of the OpenBSD category; --> On 4/12/07, Jeffrey 'jf' Lim <jfs.world@gmail.com> wrote: > On 4/12/07, Michael Knudsen <mk@molioner.dk> wrote: > > Please, supply a ...
| |||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| On 4/12/07, Jeffrey 'jf' Lim <jfs.world@gmail.com> wrote: > On 4/12/07, Michael Knudsen <mk@molioner.dk> wrote: > > Please, supply a test suite to fit into > > src/regress/lib/libc/ to go with this diff. This would substantiate your > > claim that the change is good. > > > > sure, will do. Thanks. Will get back to u guys again once i have done > the test suite. > as promised, here's the test suite to fit into regress... (there isn't one at the moment anyway, so...) I have purposefully made the test programmatic, so that if u wish to adjust for testing with a longer length of strings, u can do so very easily, but if not, the code will work fine by itself. It catches all the previous problems pointed out with "no null termination", as well as all the test cases that u might expect. I also thought i might submit a patch for regress/lib/libc/sprintf/Makefile as well - perhaps it's not really necessary practically, since it is, after all, the first target in the Makefile, but... anyway. As always, i am open to suggestions and improvements. -jf --- regress/lib/libc/Makefile.orig 2007-04-12 19:26:11.571554161 +0800 +++ regress/lib/libc/Makefile 2007-04-21 16:33:54.070168566 +0800 @@ -3,7 +3,7 @@ SUBDIR+= _setjmp alloca atexit db getaddrinfo getcap getopt_long hsearch longjmp SUBDIR+= locale malloc SUBDIR+= netdb popen regex setjmp setjmp-signal sigreturn sigsetjmp -SUBDIR+= sprintf strerror strtod strtonum telldir time vis +SUBDIR+= sprintf strerror strlcat strtod strtonum telldir time vis .if (${MACHINE_ARCH} != "vax") SUBDIR+= ieeefp --- regress/lib/libc/strlcat/Makefile.empty 2007-04-21 16:57:35.239360296 +0800 +++ regress/lib/libc/strlcat/Makefile 2007-04-21 17:00:14.811016639 +0800 @@ -0,0 +1,7 @@ NOMAN= PROG=strlcat_test run-regress-strlcat_test: ${PROG} ./${PROG} ..include <bsd.regress.mk> --- regress/lib/libc/strlcat/strlcat_test.c.empty 2007-04-21 18:17:40.597272817 +0800 +++ regress/lib/libc/strlcat/strlcat_test.c 2007-04-21 18:06:15.485790869 +0800 @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2007 Jeffrey 'jf' Lim + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <stdio.h> +#include <string.h> + +#define DESTLEN 10 +char dests[DESTLEN+2][DESTLEN]; /* dests[DESTLEN+1] acts as the "canary" for the non-terminated dests[DESTLEN] */ + +#define SOURCELEN DESTLEN+1 +char sources[SOURCELEN][SOURCELEN]; + +#define setupstrings { \ + memset ((void *) dests, 'd', sizeof(dests)); \ + memset ((void *) sources, 's', sizeof(sources)); \ + for (ri = 0; ri < DESTLEN; ri++) \ + dests[ri][ri] = '\0'; \ + dests[DESTLEN+1][0] = 'C'; \ + for (ri = 0; ri < SOURCELEN; ri++) \ + sources[ri][ri] = '\0'; \ + } + + +int +main(int argc, char *argv[]) +{ + + int ri, i, j; + size_t expected, n; + + setupstrings; + dests[11][0] = 'C'; + + setupstrings; + + for (i = 0; i < SOURCELEN; i++) { + + for (j = 0; j < DESTLEN; j++) { + + expected = j + i; //strlen(dests[j]) + strlen(sources[i]); + n = strlcat (dests[j], sources[i], DESTLEN); + if (n != expected) + exit(1); + + /* checking against chars from dest string */ + for (n = 0; n < j; n++) + if (dests[j][n] != 'd') + exit(1); + + /* check against chars from source string; - 'DESTLEN-1' because of the NULL */ + ri = DESTLEN-1 < n+i ? DESTLEN-1 : n+i; + for (; n < ri; n++) { + if (dests[j][n] != 's') + exit(1); + } + + /* check for NULL termination */ + if (dests[j][n] != '\0') + exit(1); + } + + /* j = DESTLEN - 'size' <= strlen(s) - check last case of non-terminated string */ + expected = DESTLEN + i; //strlen(sources[i]); + n = strlcat (dests[DESTLEN], sources[i], DESTLEN); + if (n != expected) + exit(1); + for (n = 0; n < DESTLEN; n++) + if (dests[DESTLEN][n] != 'd') + exit(1); + if (dests[DESTLEN+1][0] != 'C') + exit(1); + + setupstrings; + } + + exit(0); +} --- regress/lib/libc/sprintf/Makefile.orig 2007-04-21 17:03:38.716909517 +0800 +++ regress/lib/libc/sprintf/Makefile 2007-04-21 17:00:49.694694915 +0800 @@ -4,7 +4,7 @@ PROG=sprintf_test CPPFLAGS+=-I${.CURDIR}/../../../../lib/libc -run-regress-atexit_test: ${PROG} +run-regress-sprintf_test: ${PROG} ./${PROG} .include <bsd.regress.mk> |