summaryrefslogtreecommitdiff
path: root/tests/portable
diff options
context:
space:
mode:
authorRuss Allbery <rra@stanford.edu>2010-02-09 18:40:22 -0800
committerRuss Allbery <rra@stanford.edu>2010-02-09 18:40:22 -0800
commitc02942ddc12408f0e5b9d828cddf240519d1fe93 (patch)
tree62f80e0ba359c1a13cee7daee228e3e00011a723 /tests/portable
parentd05f66dbff10b525d37f60ee01d5b9f94bf5192e (diff)
Update to C TAP Harness 1.1 and rra-c-util 3.0 tests
* Update portable and util tests for C TAP Harness 1.1. * Remove the need for Autoconf substitution in test programs. * Support running a single test program with runtests -o. * Properly handle test cases that are skipped in their entirety. * Much improved C TAP library more closely matching Test::More. Rewrite client/basic-t to use the new test library functions and my current test case coding style.
Diffstat (limited to 'tests/portable')
-rw-r--r--tests/portable/asprintf-t.c34
-rw-r--r--tests/portable/mkstemp-t.c73
-rw-r--r--tests/portable/mkstemp.c2
-rw-r--r--tests/portable/setenv-t.c46
-rw-r--r--tests/portable/setenv.c2
-rw-r--r--tests/portable/snprintf-t.c123
-rw-r--r--tests/portable/strlcat-t.c84
-rw-r--r--tests/portable/strlcpy-t.c73
8 files changed, 267 insertions, 170 deletions
diff --git a/tests/portable/asprintf-t.c b/tests/portable/asprintf-t.c
index 689e7c7..04fbd1b 100644
--- a/tests/portable/asprintf-t.c
+++ b/tests/portable/asprintf-t.c
@@ -2,7 +2,8 @@
* asprintf and vasprintf test suite.
*
* Written by Russ Allbery <rra@stanford.edu>
- * Copyright 2006, 2008 Board of Trustees, Leland Stanford Jr. University
+ * Copyright 2006, 2008, 2009
+ * Board of Trustees, Leland Stanford Jr. University
*
* See LICENSE for licensing terms.
*/
@@ -10,9 +11,10 @@
#include <config.h>
#include <portable/system.h>
-#include <tests/libtest.h>
+#include <tests/tap/basic.h>
-int test_asprintf(char **, const char *, ...);
+int test_asprintf(char **, const char *, ...)
+ __attribute__((__format__(printf, 2, 3)));
int test_vasprintf(char **, const char *, va_list);
static int
@@ -32,25 +34,25 @@ main(void)
{
char *result = NULL;
- test_init(12);
+ plan(12);
- ok_int(1, 7, test_asprintf(&result, "%s", "testing"));
- ok_string(2, "testing", result);
+ is_int(7, test_asprintf(&result, "%s", "testing"), "asprintf length");
+ is_string("testing", result, "asprintf result");
free(result);
- ok(3, 1);
- ok_int(4, 0, test_asprintf(&result, "%s", ""));
- ok_string(5, "", result);
+ ok(3, "free asprintf");
+ is_int(0, test_asprintf(&result, "%s", ""), "asprintf empty length");
+ is_string("", result, "asprintf empty string");
free(result);
- ok(6, 1);
+ ok(6, "free asprintf of empty string");
- ok_int(7, 6, vatest(&result, "%d %s", 2, "test"));
- ok_string(8, "2 test", result);
+ is_int(6, vatest(&result, "%d %s", 2, "test"), "vasprintf length");
+ is_string("2 test", result, "vasprintf result");
free(result);
- ok(9, 1);
- ok_int(10, 0, vatest(&result, "%s", ""));
- ok_string(11, "", result);
+ ok(9, "free vasprintf");
+ is_int(0, vatest(&result, "%s", ""), "vasprintf empty length");
+ is_string("", result, "vasprintf empty string");
free(result);
- ok(12, 1);
+ ok(12, "free vasprintf of empty string");
return 0;
}
diff --git a/tests/portable/mkstemp-t.c b/tests/portable/mkstemp-t.c
new file mode 100644
index 0000000..54701f7
--- /dev/null
+++ b/tests/portable/mkstemp-t.c
@@ -0,0 +1,73 @@
+/*
+ * mkstemp test suite.
+ *
+ * Written by Russ Allbery <rra@stanford.edu>
+ * Copyright 2002, 2004, 2008, 2009
+ * Board of Trustees, Leland Stanford Jr. University
+ *
+ * See LICENSE for licensing terms.
+ */
+
+#include <config.h>
+#include <portable/system.h>
+
+#include <errno.h>
+#include <sys/stat.h>
+
+#include <tests/tap/basic.h>
+
+int test_mkstemp(char *template);
+
+int
+main(void)
+{
+ int fd;
+ char template[] = "tsXXXXXXX";
+ char tooshort[] = "XXXXX";
+ char bad1[] = "/foo/barXXXXX";
+ char bad2[] = "/foo/barXXXXXX.out";
+ char buffer[256];
+ struct stat st1, st2;
+ ssize_t length;
+
+ plan(20);
+
+ /* First, test a few error messages. */
+ errno = 0;
+ is_int(-1, test_mkstemp(tooshort), "too short of template");
+ is_int(EINVAL, errno, "...with correct errno");
+ is_string("XXXXX", tooshort, "...and template didn't change");
+ errno = 0;
+ is_int(-1, test_mkstemp(bad1), "bad template");
+ is_int(EINVAL, errno, "...with correct errno");
+ is_string("/foo/barXXXXX", bad1, "...and template didn't change");
+ errno = 0;
+ is_int(-1, test_mkstemp(bad2), "template doesn't end in XXXXXX");
+ is_int(EINVAL, errno, "...with correct errno");
+ is_string("/foo/barXXXXXX.out", bad2, "...and template didn't change");
+ errno = 0;
+
+ /* Now try creating a real file. */
+ fd = test_mkstemp(template);
+ ok(fd >= 0, "mkstemp works with valid template");
+ ok(strcmp(template, "tsXXXXXXX") != 0, "...and template changed");
+ ok(strncmp(template, "tsX", 3) == 0, "...and didn't touch first X");
+ ok(access(template, F_OK) == 0, "...and the file exists");
+
+ /* Make sure that it's the same file as template refers to now. */
+ ok(stat(template, &st1) == 0, "...and stat of template works");
+ ok(fstat(fd, &st2) == 0, "...and stat of open file descriptor works");
+ ok(st1.st_ino == st2.st_ino, "...and they're the same file");
+ unlink(template);
+
+ /* Make sure the open mode is correct. */
+ length = strlen(template);
+ is_int(length, write(fd, template, length), "write to open file works");
+ ok(lseek(fd, 0, SEEK_SET) == 0, "...and rewind works");
+ is_int(length, read(fd, buffer, length), "...and the data is there");
+ buffer[length] = '\0';
+ is_string(template, buffer, "...and matches what we wrote");
+ close(fd);
+
+ return 0;
+}
diff --git a/tests/portable/mkstemp.c b/tests/portable/mkstemp.c
new file mode 100644
index 0000000..4632d3d
--- /dev/null
+++ b/tests/portable/mkstemp.c
@@ -0,0 +1,2 @@
+#define TESTING 1
+#include <portable/mkstemp.c>
diff --git a/tests/portable/setenv-t.c b/tests/portable/setenv-t.c
new file mode 100644
index 0000000..5bc59ce
--- /dev/null
+++ b/tests/portable/setenv-t.c
@@ -0,0 +1,46 @@
+/*
+ * setenv test suite.
+ *
+ * Written by Russ Allbery <rra@stanford.edu>
+ * Copyright 2009 Board of Trustees, Leland Stanford Jr. University
+ * Copyright (c) 2004, 2005, 2006
+ * by Internet Systems Consortium, Inc. ("ISC")
+ * Copyright (c) 1991, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
+ * 2002, 2003 by The Internet Software Consortium and Rich Salz
+ *
+ * See LICENSE for licensing terms.
+ */
+
+#include <config.h>
+#include <portable/system.h>
+
+#include <errno.h>
+
+#include <tests/tap/basic.h>
+
+int test_setenv(const char *name, const char *value, int overwrite);
+
+static const char test_var[] = "SETENV_TEST";
+static const char test_value1[] = "Do not taunt Happy Fun Ball.";
+static const char test_value2[] = "Do not use Happy Fun Ball on concrete.";
+
+
+int
+main(void)
+{
+ plan(8);
+
+ if (getenv(test_var))
+ bail("%s already in the environment!", test_var);
+
+ ok(test_setenv(test_var, test_value1, 0) == 0, "set string 1");
+ is_string(test_value1, getenv(test_var), "...and getenv correct");
+ ok(test_setenv(test_var, test_value2, 0) == 0, "set string 2");
+ is_string(test_value1, getenv(test_var), "...and getenv unchanged");
+ ok(test_setenv(test_var, test_value2, 1) == 0, "overwrite string 2");
+ is_string(test_value2, getenv(test_var), "...and getenv changed");
+ ok(test_setenv(test_var, "", 1) == 0, "overwrite with empty string");
+ is_string("", getenv(test_var), "...and getenv correct");
+
+ return 0;
+}
diff --git a/tests/portable/setenv.c b/tests/portable/setenv.c
new file mode 100644
index 0000000..79a7efd
--- /dev/null
+++ b/tests/portable/setenv.c
@@ -0,0 +1,2 @@
+#define TESTING 1
+#include <portable/setenv.c>
diff --git a/tests/portable/snprintf-t.c b/tests/portable/snprintf-t.c
index 18c2326..ca6ae61 100644
--- a/tests/portable/snprintf-t.c
+++ b/tests/portable/snprintf-t.c
@@ -1,32 +1,25 @@
/*
* snprintf test suite.
*
+ * Written by Russ Allbery <rra@stanford.edu>
+ * Copyright 2009 Board of Trustees, Leland Stanford Jr. University
* Copyright (c) 2004, 2005, 2006
* by Internet Systems Consortium, Inc. ("ISC")
* Copyright (c) 1991, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
* 2002, 2003 by The Internet Software Consortium and Rich Salz
*
- * This code is derived from software contributed to the Internet Software
- * Consortium by Rich Salz.
- *
- * Permission to use, copy, modify, and distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
- * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
- * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
- * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
- * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
- * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
- * PERFORMANCE OF THIS SOFTWARE.
+ * See LICENSE for licensing terms.
*/
#include <config.h>
#include <portable/system.h>
-#include <tests/libtest.h>
+#include <tests/tap/basic.h>
+/*
+ * Intentionally don't add the printf attribute here since we pass a
+ * zero-length printf format during testing and don't want warnings.
+ */
int test_snprintf(char *str, size_t count, const char *fmt, ...);
int test_vsnprintf(char *str, size_t count, const char *fmt, va_list args);
@@ -93,7 +86,7 @@ static unsigned long long ullong_nums[] = {
static void
-test_format(int n, int truncate, const char *expected, int count,
+test_format(bool truncate, const char *expected, int count,
const char *format, ...)
{
char buf[128];
@@ -103,16 +96,8 @@ test_format(int n, int truncate, const char *expected, int count,
va_start(args, format);
result = test_vsnprintf(buf, truncate ? 32 : sizeof(buf), format, args);
va_end(args);
- if (!strcmp(buf, expected) && result == count) {
- printf("ok %d\n", n);
- } else {
- printf("not ok %d\n", n);
- printf(" format: %s\n", format);
- if (strcmp(buf, expected))
- printf(" saw: %s\n want: %s\n", buf, expected);
- if (result != count)
- printf(" %d != %d\n", result, count);
- }
+ is_string(expected, buf, "format %s, wanted %s", format, expected);
+ is_int(count, result, "...and output length correct");
}
@@ -124,75 +109,69 @@ main(void)
long lcount;
char lgbuf[128];
- test_init((26 + (ARRAY_SIZE(fp_formats) - 1) * ARRAY_SIZE(fp_nums)
- + (ARRAY_SIZE(int_formats) - 1) * ARRAY_SIZE(int_nums)
- + (ARRAY_SIZE(uint_formats) - 1) * ARRAY_SIZE(uint_nums)
- + (ARRAY_SIZE(llong_formats) - 1) * ARRAY_SIZE(llong_nums)
- + (ARRAY_SIZE(ullong_formats) - 1) * ARRAY_SIZE(ullong_nums)));
-
- ok(1, test_snprintf(NULL, 0, "%s", "abcd") == 4);
- ok(2, test_snprintf(NULL, 0, "%d", 20) == 2);
- ok(3, test_snprintf(NULL, 0, "Test %.2s", "abcd") == 7);
- ok(4, test_snprintf(NULL, 0, "%c", 'a') == 1);
- ok(5, test_snprintf(NULL, 0, "") == 0);
-
- test_format(6, 1, "abcd", 4, "%s", "abcd");
- test_format(7, 1, "20", 2, "%d", 20);
- test_format(8, 1, "Test ab", 7, "Test %.2s", "abcd");
- test_format(9, 1, "a", 1, "%c", 'a');
- test_format(10, 1, "", 0, "");
- test_format(11, 1, "abcdefghijklmnopqrstuvwxyz01234", 36, "%s",
- string);
- test_format(12, 1, "abcdefghij", 10, "%.10s", string);
- test_format(13, 1, " abcdefghij", 12, "%12.10s", string);
- test_format(14, 1, " abcdefghijklmnopqrstuvwxyz0", 40, "%40s",
- string);
- test_format(15, 1, "abcdefghij ", 14, "%-14.10s", string);
- test_format(16, 1, " abcdefghijklmnopq", 50, "%50s",
- string);
- test_format(17, 1, "%abcd%", 6, "%%%0s%%", "abcd");
- test_format(18, 1, "", 0, "%.0s", string);
- test_format(19, 1, "abcdefghijklmnopqrstuvwxyz 444", 32, "%.26s %d",
+ plan(8 +
+ (18 + (ARRAY_SIZE(fp_formats) - 1) * ARRAY_SIZE(fp_nums)
+ + (ARRAY_SIZE(int_formats) - 1) * ARRAY_SIZE(int_nums)
+ + (ARRAY_SIZE(uint_formats) - 1) * ARRAY_SIZE(uint_nums)
+ + (ARRAY_SIZE(llong_formats) - 1) * ARRAY_SIZE(llong_nums)
+ + (ARRAY_SIZE(ullong_formats) - 1) * ARRAY_SIZE(ullong_nums)) * 2);
+
+ is_int(4, test_snprintf(NULL, 0, "%s", "abcd"), "simple string length");
+ is_int(2, test_snprintf(NULL, 0, "%d", 20), "number length");
+ is_int(7, test_snprintf(NULL, 0, "Test %.2s", "abcd"), "limited string");
+ is_int(1, test_snprintf(NULL, 0, "%c", 'a'), "character length");
+ is_int(0, test_snprintf(NULL, 0, ""), "empty format length");
+
+ test_format(true, "abcd", 4, "%s", "abcd");
+ test_format(true, "20", 2, "%d", 20);
+ test_format(true, "Test ab", 7, "Test %.2s", "abcd");
+ test_format(true, "a", 1, "%c", 'a');
+ test_format(true, "", 0, "");
+ test_format(true, "abcdefghijklmnopqrstuvwxyz01234", 36, "%s", string);
+ test_format(true, "abcdefghij", 10, "%.10s", string);
+ test_format(true, " abcdefghij", 12, "%12.10s", string);
+ test_format(true, " abcdefghijklmnopqrstuvwxyz0", 40, "%40s", string);
+ test_format(true, "abcdefghij ", 14, "%-14.10s", string);
+ test_format(true, " abcdefghijklmnopq", 50, "%50s", string);
+ test_format(true, "%abcd%", 6, "%%%0s%%", "abcd");
+ test_format(true, "", 0, "%.0s", string);
+ test_format(true, "abcdefghijklmnopqrstuvwxyz 444", 32, "%.26s %d",
string, 4444);
- test_format(20, 1, "abcdefghijklmnopqrstuvwxyz -2.", 32,
- "%.26s %.1f", string, -2.5);
- test_format(21, 1, "abcdefghij4444", 14, "%.10s%n%d", string, &count,
- 4444);
- ok(22, count == 10);
- test_format(23, 1, "abcdefghijklmnopqrstuvwxyz01234", 36, "%n%s%ln",
+ test_format(true, "abcdefghijklmnopqrstuvwxyz -2.", 32, "%.26s %.1f",
+ string, -2.5);
+ test_format(true, "abcdefghij4444", 14, "%.10s%n%d", string, &count, 4444);
+ is_int(10, count, "correct output from %%n");
+ test_format(true, "abcdefghijklmnopqrstuvwxyz01234", 36, "%n%s%ln",
&count, string, &lcount);
- ok(24, count == 0);
- ok(25, lcount == 31);
- test_format(26, 1, "(null)", 6, "%s", NULL);
+ is_int(0, count, "correct output from two %%n");
+ is_int(31, lcount, "correct output from long %%ln");
+ test_format(true, "(null)", 6, "%s", NULL);
n = 26;
for (i = 0; fp_formats[i] != NULL; i++)
for (j = 0; j < ARRAY_SIZE(fp_nums); j++) {
count = sprintf(lgbuf, fp_formats[i], fp_nums[j]);
- test_format(++n, 0, lgbuf, count, fp_formats[i], fp_nums[j]);
+ test_format(false, lgbuf, count, fp_formats[i], fp_nums[j]);
}
for (i = 0; int_formats[i] != NULL; i++)
for (j = 0; j < ARRAY_SIZE(int_nums); j++) {
count = sprintf(lgbuf, int_formats[i], int_nums[j]);
- test_format(++n, 0, lgbuf, count, int_formats[i],
- int_nums[j]);
+ test_format(false, lgbuf, count, int_formats[i], int_nums[j]);
}
for (i = 0; uint_formats[i] != NULL; i++)
for (j = 0; j < ARRAY_SIZE(uint_nums); j++) {
count = sprintf(lgbuf, uint_formats[i], uint_nums[j]);
- test_format(++n, 0, lgbuf, count, uint_formats[i],
- uint_nums[j]);
+ test_format(false, lgbuf, count, uint_formats[i], uint_nums[j]);
}
for (i = 0; llong_formats[i] != NULL; i++)
for (j = 0; j < ARRAY_SIZE(llong_nums); j++) {
count = sprintf(lgbuf, llong_formats[i], llong_nums[j]);
- test_format(++n, 0, lgbuf, count, llong_formats[i],
- llong_nums[j]);
+ test_format(false, lgbuf, count, llong_formats[i], llong_nums[j]);
}
for (i = 0; ullong_formats[i] != NULL; i++)
for (j = 0; j < ARRAY_SIZE(ullong_nums); j++) {
count = sprintf(lgbuf, ullong_formats[i], ullong_nums[j]);
- test_format(++n, 0, lgbuf, count, ullong_formats[i],
+ test_format(false, lgbuf, count, ullong_formats[i],
ullong_nums[j]);
}
diff --git a/tests/portable/strlcat-t.c b/tests/portable/strlcat-t.c
index 2f39925..e02c277 100644
--- a/tests/portable/strlcat-t.c
+++ b/tests/portable/strlcat-t.c
@@ -1,31 +1,20 @@
/*
* strlcat test suite.
*
+ * Written by Russ Allbery <rra@stanford.edu>
+ * Copyright 2009 Board of Trustees, Leland Stanford Jr. University
* Copyright (c) 2004, 2005, 2006
* by Internet Systems Consortium, Inc. ("ISC")
* Copyright (c) 1991, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
* 2002, 2003 by The Internet Software Consortium and Rich Salz
*
- * This code is derived from software contributed to the Internet Software
- * Consortium by Rich Salz.
- *
- * Permission to use, copy, modify, and distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
- * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
- * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
- * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
- * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
- * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
- * PERFORMANCE OF THIS SOFTWARE.
+ * See LICENSE for licensing terms.
*/
#include <config.h>
#include <portable/system.h>
-#include <tests/libtest.h>
+#include <tests/tap/basic.h>
size_t test_strlcat(char *, const char *, size_t);
@@ -35,42 +24,51 @@ main(void)
{
char buffer[10] = "";
- test_init(27);
+ plan(27);
- ok_int(1, 3, test_strlcat(buffer, "foo", sizeof(buffer)));
- ok_string(2, "foo", buffer);
- ok_int(3, 7, test_strlcat(buffer, " bar", sizeof(buffer)));
- ok_string(4, "foo bar", buffer);
- ok_int(5, 9, test_strlcat(buffer, "!!", sizeof(buffer)));
- ok_string(6, "foo bar!!", buffer);
- ok_int(7, 10, test_strlcat(buffer, "!", sizeof(buffer)));
- ok_string(8, "foo bar!!", buffer);
- ok(9, buffer[9] == '\0');
+ is_int(3, test_strlcat(buffer, "foo", sizeof(buffer)),
+ "strlcat into empty buffer");
+ is_string("foo", buffer, "...with right output");
+ is_int(7, test_strlcat(buffer, " bar", sizeof(buffer)),
+ "...and append more");
+ is_string("foo bar", buffer, "...and output is still correct");
+ is_int(9, test_strlcat(buffer, "!!", sizeof(buffer)),
+ "...and append to buffer limit");
+ is_string("foo bar!!", buffer, "...output is still correct");
+ is_int(10, test_strlcat(buffer, "!", sizeof(buffer)),
+ "...append one more character");
+ is_string("foo bar!!", buffer, "...and output didn't change");
+ ok(buffer[9] == '\0', "...buffer still nul-terminated");
buffer[0] = '\0';
- ok_int(10, 11, test_strlcat(buffer, "hello world", sizeof(buffer)));
- ok_string(11, "hello wor", buffer);
- ok(12, buffer[9] == '\0');
+ is_int(11, test_strlcat(buffer, "hello world", sizeof(buffer)),
+ "append single long string");
+ is_string("hello wor", buffer, "...string truncates properly");
+ ok(buffer[9] == '\0', "...buffer still nul-terminated");
buffer[0] = '\0';
- ok_int(13, 7, test_strlcat(buffer, "sausage", 5));
- ok_string(14, "saus", buffer);
- ok_int(15, 14, test_strlcat(buffer, "bacon eggs", sizeof(buffer)));
- ok_string(16, "sausbacon", buffer);
+ is_int(7, test_strlcat(buffer, "sausage", 5), "lie about buffer length");
+ is_string("saus", buffer, "...contents are correct");
+ is_int(14, test_strlcat(buffer, "bacon eggs", sizeof(buffer)),
+ "...add more up to real size");
+ is_string("sausbacon", buffer, "...and result is correct");
/* Make sure that with a size of 0, the destination isn't changed. */
- ok_int(17, 11, test_strlcat(buffer, "!!", 0));
- ok_string(18, "sausbacon", buffer);
+ is_int(11, test_strlcat(buffer, "!!", 0), "no change with size of 0");
+ is_string("sausbacon", buffer, "...and content is the same");
/* Now play with empty strings. */
- ok_int(19, 9, test_strlcat(buffer, "", 0));
- ok_string(20, "sausbacon", buffer);
+ is_int(9, test_strlcat(buffer, "", 0),
+ "correct count when appending empty string");
+ is_string("sausbacon", buffer, "...and contents are unchanged");
buffer[0] = '\0';
- ok_int(21, 0, test_strlcat(buffer, "", sizeof(buffer)));
- ok_string(22, "", buffer);
- ok_int(23, 3, test_strlcat(buffer, "foo", 2));
- ok_string(24, "f", buffer);
- ok(25, buffer[1] == '\0');
- ok_int(26, 1, test_strlcat(buffer, "", sizeof(buffer)));
- ok(27, buffer[1] == '\0');
+ is_int(0, test_strlcat(buffer, "", sizeof(buffer)),
+ "correct count when appending empty string to empty buffer");
+ is_string("", buffer, "...and buffer content is correct");
+ is_int(3, test_strlcat(buffer, "foo", 2), "append to length 2 buffer");
+ is_string("f", buffer, "...and got only a single character");
+ ok(buffer[1] == '\0', "...and buffer is still nul-terminated");
+ is_int(1, test_strlcat(buffer, "", sizeof(buffer)),
+ "append an empty string");
+ ok(buffer[1] == '\0', "...and buffer is still nul-terminated");
return 0;
}
diff --git a/tests/portable/strlcpy-t.c b/tests/portable/strlcpy-t.c
index 74c9ecd..ba224ba 100644
--- a/tests/portable/strlcpy-t.c
+++ b/tests/portable/strlcpy-t.c
@@ -1,31 +1,20 @@
/*
* strlcpy test suite.
*
+ * Written by Russ Allbery <rra@stanford.edu>
+ * Copyright 2009 Board of Trustees, Leland Stanford Jr. University
* Copyright (c) 2004, 2005, 2006
* by Internet Systems Consortium, Inc. ("ISC")
* Copyright (c) 1991, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
* 2002, 2003 by The Internet Software Consortium and Rich Salz
*
- * This code is derived from software contributed to the Internet Software
- * Consortium by Rich Salz.
- *
- * Permission to use, copy, modify, and distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
- * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
- * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
- * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
- * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
- * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
- * PERFORMANCE OF THIS SOFTWARE.
+ * See LICENSE for licensing terms.
*/
#include <config.h>
#include <portable/system.h>
-#include <tests/libtest.h>
+#include <tests/tap/basic.h>
size_t test_strlcpy(char *, const char *, size_t);
@@ -35,37 +24,43 @@ main(void)
{
char buffer[10];
- test_init(23);
+ plan(23);
- ok_int(1, 3, test_strlcpy(buffer, "foo", sizeof(buffer)));
- ok_string(2, "foo", buffer);
- ok_int(3, 9, test_strlcpy(buffer, "hello wor", sizeof(buffer)));
- ok_string(4, "hello wor", buffer);
- ok_int(5, 10, test_strlcpy(buffer, "world hell", sizeof(buffer)));
- ok_string(6, "world hel", buffer);
- ok(7, buffer[9] == '\0');
- ok_int(8, 11, test_strlcpy(buffer, "hello world", sizeof(buffer)));
- ok_string(9, "hello wor", buffer);
- ok(10, buffer[9] == '\0');
+ is_int(3, test_strlcpy(buffer, "foo", sizeof(buffer)), "simple strlcpy");
+ is_string("foo", buffer, "...result is correct");
+ is_int(9, test_strlcpy(buffer, "hello wor", sizeof(buffer)),
+ "strlcpy exact length of buffer");
+ is_string("hello wor", buffer, "...result is correct");
+ is_int(10, test_strlcpy(buffer, "world hell", sizeof(buffer)),
+ "strlcpy one more than buffer length");
+ is_string("world hel", buffer, "...result is correct");
+ ok(buffer[9] == '\0', "...buffer is nul-terminated");
+ is_int(11, test_strlcpy(buffer, "hello world", sizeof(buffer)),
+ "strlcpy more than buffer length");
+ is_string("hello wor", buffer, "...result is correct");
+ ok(buffer[9] == '\0', "...buffer is nul-terminated");
/* Make sure that with a size of 0, the destination isn't changed. */
- ok_int(11, 3, test_strlcpy(buffer, "foo", 0));
- ok_string(12, "hello wor", buffer);
+ is_int(3, test_strlcpy(buffer, "foo", 0), "buffer unchanged if size 0");
+ is_string("hello wor", buffer, "...contents still the same");
/* Now play with empty strings. */
- ok_int(13, 0, test_strlcpy(buffer, "", 0));
- ok_string(14, "hello wor", buffer);
- ok_int(15, 0, test_strlcpy(buffer, "", sizeof(buffer)));
- ok_string(16, "", buffer);
- ok_int(17, 3, test_strlcpy(buffer, "foo", 2));
- ok_string(18, "f", buffer);
- ok(19, buffer[1] == '\0');
- ok_int(20, 0, test_strlcpy(buffer, "", 1));
- ok(21, buffer[0] == '\0');
+ is_int(0, test_strlcpy(buffer, "", 0), "copy empty string with size 0");
+ is_string("hello wor", buffer, "...buffer unchanged");
+ is_int(0, test_strlcpy(buffer, "", sizeof(buffer)),
+ "copy empty string into full buffer");
+ is_string("", buffer, "...buffer now empty string");
+ is_int(3, test_strlcpy(buffer, "foo", 2),
+ "copy string into buffer of size 2");
+ is_string("f", buffer, "...got one character");
+ ok(buffer[1] == '\0', "...buffer is nul-terminated");
+ is_int(0, test_strlcpy(buffer, "", 1),
+ "copy empty string into buffer of size 1");
+ ok(buffer[0] == '\0', "...buffer is empty string");
/* Finally, check using strlcpy as strlen. */
- ok_int(22, 3, test_strlcpy(NULL, "foo", 0));
- ok_int(23, 11, test_strlcpy(NULL, "hello world", 0));
+ is_int(3, test_strlcpy(NULL, "foo", 0), "use strlcpy as strlen");
+ is_int(11, test_strlcpy(NULL, "hello world", 0), "...again");
return 0;
}