INN commit: trunk (19 files)
INN Commit
rra at isc.org
Thu Aug 18 14:19:02 UTC 2011
Date: Thursday, August 18, 2011 @ 07:19:02
Author: iulius
Revision: 9350
synchronize our libraries with rra-c-util 3.7
* Update to the latest release of rra-c-util (3.7).
Keep the specific part for setenv-t.c with the run of a shell script wrapper.
* Add support for "make tests" in the tests directory.
* Add support for $SOURCE and $BUILD in runtests.
* mkstemp.c: add a check for (errno != EISDIR).
* snprintf.c: remove the HAVE_LONG_DOUBLE check. All modern compilers
have "long double".
Modified:
trunk/lib/fdflags.c
trunk/lib/mkstemp.c
trunk/lib/snprintf.c
trunk/tests/Makefile
trunk/tests/lib/asprintf-t.c
trunk/tests/lib/buffer-t.c
trunk/tests/lib/concat-t.c
trunk/tests/lib/fakewrite.c
trunk/tests/lib/getaddrinfo-t.c
trunk/tests/lib/getnameinfo-t.c
trunk/tests/lib/inet_aton-t.c
trunk/tests/lib/inet_ntoa-t.c
trunk/tests/lib/inet_ntop-t.c
trunk/tests/lib/mkstemp-t.c
trunk/tests/lib/setenv-t.c
trunk/tests/lib/snprintf-t.c
trunk/tests/lib/strlcat-t.c
trunk/tests/lib/strlcpy-t.c
trunk/tests/lib/xwrite-t.c
---------------------------+
lib/fdflags.c | 2
lib/mkstemp.c | 2
lib/snprintf.c | 5
tests/Makefile | 7 -
tests/lib/asprintf-t.c | 54 +++++--
tests/lib/buffer-t.c | 229 ++++++++++++++++++---------------
tests/lib/concat-t.c | 56 +++++---
tests/lib/fakewrite.c | 73 ++++++++--
tests/lib/getaddrinfo-t.c | 301 +++++++++++++++++++++++++++-----------------
tests/lib/getnameinfo-t.c | 121 +++++++++++------
tests/lib/inet_aton-t.c | 139 +++++++++++---------
tests/lib/inet_ntoa-t.c | 40 ++++-
tests/lib/inet_ntop-t.c | 65 +++++----
tests/lib/mkstemp-t.c | 64 +++++----
tests/lib/setenv-t.c | 63 +++++----
tests/lib/snprintf-t.c | 137 +++++++++++---------
tests/lib/strlcat-t.c | 88 ++++++++----
tests/lib/strlcpy-t.c | 77 +++++++----
tests/lib/xwrite-t.c | 86 ++++++++----
19 files changed, 1009 insertions(+), 600 deletions(-)
Modified: lib/fdflags.c
===================================================================
--- lib/fdflags.c 2011-08-18 14:12:08 UTC (rev 9349)
+++ lib/fdflags.c 2011-08-18 14:19:02 UTC (rev 9350)
@@ -13,7 +13,7 @@
#include <fcntl.h>
/*
-** Set a file to close on exec.
+** Set a file to close on exec (or clear that setting if the flag is false).
**
** One is supposed to retrieve the flags, add FD_CLOEXEC, and then set
** them, although I've never seen a system with any flags other than
Modified: lib/mkstemp.c
===================================================================
--- lib/mkstemp.c 2011-08-18 14:12:08 UTC (rev 9349)
+++ lib/mkstemp.c 2011-08-18 14:19:02 UTC (rev 9350)
@@ -67,7 +67,7 @@
working /= 62;
}
fd = open(template, O_RDWR | O_CREAT | O_EXCL, 0600);
- if (fd >= 0 || errno != EEXIST)
+ if (fd >= 0 || (errno != EEXIST && errno != EISDIR))
return fd;
/* This is a relatively random increment. Cut off the tail end of
Modified: lib/snprintf.c
===================================================================
--- lib/snprintf.c 2011-08-18 14:12:08 UTC (rev 9349)
+++ lib/snprintf.c 2011-08-18 14:19:02 UTC (rev 9350)
@@ -107,11 +107,8 @@
#define VA_SHIFT(v,t) ; /* no-op for ANSI */
#define VA_END va_end(ap)
-#ifdef HAVE_LONG_DOUBLE
+/* Assume all compilers support long double, per Autoconf documentation. */
#define LDOUBLE long double
-#else
-#define LDOUBLE double
-#endif
#ifdef HAVE_LONG_LONG_INT
# define LLONG long long
Modified: tests/Makefile
===================================================================
--- tests/Makefile 2011-08-18 14:12:08 UTC (rev 9349)
+++ tests/Makefile 2011-08-18 14:19:02 UTC (rev 9350)
@@ -2,8 +2,9 @@
include ../Makefile.global
-top = ..
-CFLAGS = $(GCFLAGS) -I.
+top = ..
+RUNTESTS_CFLAGS = -DSOURCE='"$(abs_builddir)/tests"' -DBUILD='"$(abs_builddir)/tests"'
+CFLAGS = $(GCFLAGS) $(RUNTESTS_CFLAGS) -I.
## The tests that need to be built. Tests in the form of shell scripts
## or some other form that doesn't require compiling shouldn't be in this
@@ -25,7 +26,7 @@
EXTRA = runtests clients/server-list lib/setenv.tr lib/xmalloc
-all check test: $(TESTS) $(EXTRA)
+all check test tests: $(TESTS) $(EXTRA)
./runtests TESTS
build: $(TESTS) $(EXTRA)
Modified: tests/lib/asprintf-t.c
===================================================================
--- tests/lib/asprintf-t.c 2011-08-18 14:12:08 UTC (rev 9349)
+++ tests/lib/asprintf-t.c 2011-08-18 14:19:02 UTC (rev 9350)
@@ -1,11 +1,33 @@
-/* $Id$ */
-/* asprintf and vasprintf test suite. */
+/*
+ * asprintf and vasprintf test suite.
+ *
+ * $Id$
+ *
+ * The canonical version of this file is maintained in the rra-c-util package,
+ * which can be found at <http://www.eyrie.org/~eagle/software/rra-c-util/>.
+ *
+ * Written by Russ Allbery <rra at stanford.edu>
+ *
+ * The authors hereby relinquish any claim to any copyright that they may have
+ * in this work, whether granted under contract or by operation of law or
+ * international treaty, and hereby commit to the public, at large, that they
+ * shall not, at any time in the future, seek to enforce any copyright in this
+ * work against any person or entity, or prevent any person or entity from
+ * copying, publishing, distributing or creating derivative works of this
+ * work.
+ */
+#define LIBTEST_NEW_FORMAT 1
+
#include "config.h"
#include "clibrary.h"
#include "libtest.h"
+int test_asprintf(char **, const char *, ...)
+ __attribute__((__format__(printf, 2, 3)));
+int test_vasprintf(char **, const char *, va_list);
+
static int
vatest(char **result, const char *format, ...)
{
@@ -13,7 +35,7 @@
int status;
va_start(args, format);
- status = vasprintf(result, format, args);
+ status = test_vasprintf(result, format, args);
va_end(args);
return status;
}
@@ -23,25 +45,25 @@
{
char *result = NULL;
- test_init(12);
+ plan(12);
- ok_int(1, 7, 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, true);
- ok_int(4, 0, 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, true);
+ 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, true);
- 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, true);
+ ok(12, "free vasprintf of empty string");
return 0;
}
Modified: tests/lib/buffer-t.c
===================================================================
--- tests/lib/buffer-t.c 2011-08-18 14:12:08 UTC (rev 9349)
+++ tests/lib/buffer-t.c 2011-08-18 14:19:02 UTC (rev 9350)
@@ -1,6 +1,24 @@
-/* $Id$ */
-/* buffer test suite. */
+/*
+ * buffer test suite.
+ *
+ * $Id$
+ *
+ * The canonical version of this file is maintained in the rra-c-util package,
+ * which can be found at <http://www.eyrie.org/~eagle/software/rra-c-util/>.
+ *
+ * Written by Russ Allbery <rra at stanford.edu>
+ *
+ * The authors hereby relinquish any claim to any copyright that they may have
+ * in this work, whether granted under contract or by operation of law or
+ * international treaty, and hereby commit to the public, at large, that they
+ * shall not, at any time in the future, seek to enforce any copyright in this
+ * work against any person or entity, or prevent any person or entity from
+ * copying, publishing, distributing or creating derivative works of this
+ * work.
+ */
+#define LIBTEST_NEW_FORMAT 1
+
#include "config.h"
#include "clibrary.h"
#include <fcntl.h>
@@ -14,6 +32,7 @@
static const char test_string2[] = " of the buffer system";
static const char test_string3[] = "This is a test\0 of the buffer system";
+
static void
test_vsprintf(struct buffer *buffer, bool append, const char *format, ...)
{
@@ -24,6 +43,7 @@
va_end(args);
}
+
int
main(void)
{
@@ -35,41 +55,41 @@
ssize_t count;
size_t offset;
- test_init(89);
+ plan(89);
/* buffer_set, buffer_append, buffer_swap */
buffer_set(&one, test_string1, sizeof(test_string1));
- ok_int(1, 1024, one.size);
- ok_int(2, 0, one.used);
- ok_int(3, sizeof(test_string1), one.left);
- ok_string(4, test_string1, one.data);
+ is_int(1024, one.size, "minimum size is 1024");
+ is_int(0, one.used, "used starts at 0");
+ is_int(sizeof(test_string1), one.left, "left is correct");
+ is_string(test_string1, one.data, "data is corect");
buffer_append(&one, test_string2, sizeof(test_string2));
- ok_int(5, 1024, one.size);
- ok_int(6, 0, one.used);
- ok_int(7, sizeof(test_string3), one.left);
- ok(8, memcmp(one.data, test_string3, sizeof(test_string3)) == 0);
+ is_int(1024, one.size, "appended data doesn't change size");
+ is_int(0, one.used, "or used");
+ is_int(sizeof(test_string3), one.left, "but left is the right size");
+ ok(memcmp(one.data, test_string3, sizeof(test_string3)) == 0,
+ "and the resulting data is correct");
one.left -= sizeof(test_string1);
one.used += sizeof(test_string1);
buffer_append(&one, test_string1, sizeof(test_string1));
- ok_int(9, 1024, one.size);
- ok_int(10, sizeof(test_string1), one.used);
- ok_int(11, sizeof(test_string3), one.left);
- ok(12,
- memcmp(one.data + one.used, test_string2, sizeof(test_string2)) == 0);
- ok(13,
- memcmp(one.data + one.used + sizeof(test_string2), test_string1,
- sizeof(test_string1)) == 0);
+ is_int(1024, one.size, "size still isn't larger after adding data");
+ is_int(sizeof(test_string1), one.used, "and used is preserved on append");
+ is_int(sizeof(test_string3), one.left, "and left is updated properly");
+ ok(memcmp(one.data + one.used, test_string2, sizeof(test_string2)) == 0,
+ "and the middle data is unchanged");
+ ok(memcmp(one.data + one.used + sizeof(test_string2), test_string1,
+ sizeof(test_string1)) == 0, "and the final data is correct");
buffer_set(&one, test_string1, sizeof(test_string1));
buffer_set(&two, test_string2, sizeof(test_string2));
buffer_swap(&one, &two);
- ok_int(14, 1024, one.size);
- ok_int(15, 0, one.used);
- ok_int(16, sizeof(test_string2), one.left);
- ok_string(17, test_string2, one.data);
- ok_int(18, 1024, two.size);
- ok_int(19, 0, two.used);
- ok_int(20, sizeof(test_string1), two.left);
- ok_string(21, test_string1, two.data);
+ is_int(1024, one.size, "swap #1 size is correct");
+ is_int(0, one.used, "swap #1 used is correct");
+ is_int(sizeof(test_string2), one.left, "swap #1 left is correct");
+ is_string(test_string2, one.data, "swap #1 data is correct");
+ is_int(1024, two.size, "swap #2 size is correct");
+ is_int(0, two.used, "swap #2 used is correct");
+ is_int(sizeof(test_string1), two.left, "swap #2 left is correct");
+ is_string(test_string1, two.data, "swap #2 data is correct");
free(one.data);
free(two.data);
one.data = NULL;
@@ -79,58 +99,61 @@
/* buffer_resize */
three = buffer_new();
- ok(22, three != NULL);
- ok_int(23, 0, three->size);
+ ok(three != NULL, "buffer_new works");
+ is_int(0, three->size, "initial size is 0");
buffer_set(three, test_string1, sizeof(test_string1));
- ok_int(24, 1024, three->size);
+ is_int(1024, three->size, "size becomes 1024 when adding data");
buffer_resize(three, 512);
- ok_int(25, 1024, three->size);
+ is_int(1024, three->size, "resizing to something smaller doesn't change");
buffer_resize(three, 1025);
- ok_int(26, 2048, three->size);
+ is_int(2048, three->size, "resizing to something larger goes to 2048");
buffer_free(three);
/* buffer_read, buffer_find_string, buffer_compact */
fd = open("buffer-test", O_RDWR | O_CREAT | O_TRUNC, 0666);
if (fd < 0)
- sysdie("cannot create buffer-test");
+ sysbail("cannot create buffer-test");
data = xmalloc(2048);
memset(data, 'a', 1023);
data[1023] = '\r';
data[1024] = '\n';
memset(data + 1025, 'b', 1023);
if (xwrite(fd, data, 2048) < 2048)
- sysdie("cannot write to buffer-test");
+ sysbail("cannot write to buffer-test");
if (lseek(fd, 0, SEEK_SET) == (off_t) -1)
- sysdie("cannot rewind buffer-test");
+ sysbail("cannot rewind buffer-test");
three = buffer_new();
- ok(27, three != NULL);
- ok_int(28, 0, three->size);
+ ok(three != NULL, "buffer_new works");
+ is_int(0, three->size, "and initial size is 0");
buffer_resize(three, 1024);
- ok_int(29, 1024, three->size);
+ is_int(1024, three->size, "resize to 1024 works");
count = buffer_read(three, fd);
- ok_int(30, 1024, count);
+ is_int(1024, count, "reading into a buffer of size 1024 reads 1024");
offset = 0;
- ok(31, !buffer_find_string(three, "\r\n", 0, &offset));
- ok_int(32, 0, offset);
- ok(33, memcmp(three->data, data, three->size) == 0);
+ ok(!buffer_find_string(three, "\r\n", 0, &offset),
+ "buffer_find_string with truncated string fails");
+ is_int(0, offset, "and offset is unchanged");
+ ok(memcmp(three->data, data, three->size) == 0, "buffer data is correct");
buffer_resize(three, 2048);
- ok_int(34, 2048, three->size);
+ is_int(2048, three->size, "resizing the buffer to 2048 works");
count = buffer_read(three, fd);
- ok_int(35, 1024, count);
- ok(36, memcmp(three->data, data, 2048) == 0);
- ok(37, !buffer_find_string(three, "\r\n", 1024, &offset));
- ok_int(38, 0, offset);
- ok(39, buffer_find_string(three, "\r\n", 0, &offset));
- ok_int(40, 1023, offset);
+ is_int(1024, count, "and now we can read the rest of the data");
+ ok(memcmp(three->data, data, 2048) == 0, "and it's all there");
+ ok(!buffer_find_string(three, "\r\n", 1024, &offset),
+ "buffer_find_string with a string starting before offset fails");
+ is_int(0, offset, "and offset is unchanged");
+ ok(buffer_find_string(three, "\r\n", 0, &offset),
+ "finding the string on the whole buffer works");
+ is_int(1023, offset, "and returns the correct location");
three->used += 400;
three->left -= 400;
buffer_compact(three);
- ok_int(41, 2048, three->size);
- ok_int(42, 0, three->used);
- ok_int(43, 1648, three->left);
- ok(44, memcmp(three->data, data + 400, 1648) == 0);
+ is_int(2048, three->size, "compacting buffer doesn't change the size");
+ is_int(0, three->used, "but used is now zero");
+ is_int(1648, three->left, "and left is decreased appropriately");
+ ok(memcmp(three->data, data + 400, 1648) == 0, "and the data is correct");
count = buffer_read(three, fd);
- ok_int(45, 0, count);
+ is_int(0, count, "reading at EOF returns 0");
close(fd);
unlink("buffer-test");
free(data);
@@ -139,75 +162,75 @@
/* buffer_sprintf */
three = buffer_new();
buffer_sprintf(three, true, "testing %d testing", 6);
- ok_int(46, 0, three->used);
- ok_int(47, 17, three->left);
+ is_int(0, three->used, "buffer_sprintf doesn't change used");
+ is_int(17, three->left, "but sets left correctly");
buffer_append(three, "", 1);
- ok_int(48, 18, three->left);
- ok_string(49, "testing 6 testing", three->data);
+ is_int(18, three->left, "appending a nul works");
+ is_string("testing 6 testing", three->data, "and the data is correct");
three->left--;
three->used += 5;
three->left -= 5;
buffer_sprintf(three, true, " %d", 7);
- ok_int(50, 14, three->left);
+ is_int(14, three->left, "appending a digit works");
buffer_append(three, "", 1);
- ok_string(51, "testing 6 testing 7", three->data);
+ is_string("testing 6 testing 7", three->data, "and the data is correct");
buffer_sprintf(three, false, "%d testing", 8);
- ok_int(52, 9, three->left);
- ok_string(53, "8 testing", three->data);
+ is_int(9, three->left, "replacing the buffer works");
+ is_string("8 testing", three->data, "and the results are correct");
data = xmalloc(1050);
memset(data, 'a', 1049);
data[1049] = '\0';
- ok_int(54, 1024, three->size);
+ is_int(1024, three->size, "size before large sprintf is 1024");
buffer_sprintf(three, false, "%s", data);
- ok_int(55, 2048, three->size);
- ok_int(56, 1049, three->left);
+ is_int(2048, three->size, "size after large sprintf is 2048");
+ is_int(1049, three->left, "and left is correct");
buffer_append(three, "", 1);
- ok_string(57, data, three->data);
+ is_string(data, three->data, "and data is correct");
free(data);
buffer_free(three);
/* buffer_read_all */
fd = open("buffer-test", O_RDWR | O_CREAT | O_TRUNC, 0666);
if (fd < 0)
- sysdie("cannot create buffer-test");
+ sysbail("cannot create buffer-test");
data = xmalloc(2049);
memset(data, 'a', 2049);
if (xwrite(fd, data, 2049) < 2049)
- sysdie("cannot write to buffer-test");
+ sysbail("cannot write to buffer-test");
if (lseek(fd, 0, SEEK_SET) == (off_t) -1)
- sysdie("cannot rewind buffer-test");
+ sysbail("cannot rewind buffer-test");
three = buffer_new();
- ok(58, buffer_read_all(three, fd));
- ok_int(59, 0, three->used);
- ok_int(60, 2049, three->left);
- ok_int(61, 4096, three->size);
- ok(62, memcmp(data, three->data, 2049) == 0);
+ ok(buffer_read_all(three, fd), "buffer_read_all succeeds");
+ is_int(0, three->used, "and unused is zero");
+ is_int(2049, three->left, "and left is correct");
+ is_int(4096, three->size, "and size is correct");
+ ok(memcmp(data, three->data, 2049) == 0, "and data is correct");
if (lseek(fd, 0, SEEK_SET) == (off_t) -1)
- sysdie("cannot rewind buffer-test");
- ok(63, buffer_read_all(three, fd));
- ok_int(64, 0, three->used);
- ok_int(65, 4098, three->left);
- ok_int(66, 8192, three->size);
- ok(67, memcmp(data, three->data + 2049, 2049) == 0);
+ sysbail("cannot rewind buffer-test");
+ ok(buffer_read_all(three, fd), "reading again succeeds");
+ is_int(0, three->used, "and used is correct");
+ is_int(4098, three->left, "and left is now larger");
+ is_int(8192, three->size, "and size doubled");
+ ok(memcmp(data, three->data + 2049, 2049) == 0, "and data is correct");
/* buffer_read_file */
if (lseek(fd, 0, SEEK_SET) == (off_t) -1)
- sysdie("cannot rewind buffer-test");
+ sysbail("cannot rewind buffer-test");
buffer_free(three);
three = buffer_new();
- ok(68, buffer_read_file(three, fd));
- ok_int(69, 0, three->used);
- ok_int(70, 2049, three->left);
- ok_int(71, 3072, three->size);
- ok(72, memcmp(data, three->data, 2049) == 0);
+ ok(buffer_read_file(three, fd), "buffer_read_file succeeds");
+ is_int(0, three->used, "and leaves unused at 0");
+ is_int(2049, three->left, "and left is correct");
+ is_int(3072, three->size, "and size is a multiple of 1024");
+ ok(memcmp(data, three->data, 2049) == 0, "and the data is correct");
/* buffer_read_all and buffer_read_file errors */
close(fd);
- ok(73, !buffer_read_all(three, fd));
- ok_int(74, 3072, three->size);
- ok(75, !buffer_read_file(three, fd));
- ok_int(76, 3072, three->size);
- ok_int(77, 2049, three->left);
+ ok(!buffer_read_all(three, fd), "buffer_read_all on closed fd fails");
+ is_int(3072, three->size, "and size is unchanged");
+ ok(!buffer_read_file(three, fd), "buffer_read_file on closed fd fails");
+ is_int(3072, three->size, "and size is unchanged");
+ is_int(2049, three->left, "and left is unchanged");
unlink("buffer-test");
free(data);
buffer_free(three);
@@ -215,30 +238,30 @@
/* buffer_vsprintf */
three = buffer_new();
test_vsprintf(three, true, "testing %d testing", 6);
- ok_int(78, 0, three->used);
- ok_int(79, 17, three->left);
+ is_int(0, three->used, "buffer_vsprintf leaves used as 0");
+ is_int(17, three->left, "and left is correct");
buffer_append(three, "", 1);
- ok_int(80, 18, three->left);
- ok_string(81, "testing 6 testing", three->data);
+ is_int(18, three->left, "and left is correct after appending a nul");
+ is_string("testing 6 testing", three->data, "and data is correct");
three->left--;
three->used += 5;
three->left -= 5;
test_vsprintf(three, true, " %d", 7);
- ok_int(82, 14, three->left);
+ is_int(14, three->left, "and appending results in the correct left");
buffer_append(three, "", 1);
- ok_string(83, "testing 6 testing 7", three->data);
+ is_string("testing 6 testing 7", three->data, "and the right data");
test_vsprintf(three, false, "%d testing", 8);
- ok_int(84, 9, three->left);
- ok_string(85, "8 testing", three->data);
+ is_int(9, three->left, "replacing the buffer results in the correct size");
+ is_string("8 testing", three->data, "and the correct data");
data = xmalloc(1050);
memset(data, 'a', 1049);
data[1049] = '\0';
- ok_int(86, 1024, three->size);
+ is_int(1024, three->size, "size is 1024 before large vsprintf");
test_vsprintf(three, false, "%s", data);
- ok_int(87, 2048, three->size);
- ok_int(88, 1049, three->left);
+ is_int(2048, three->size, "and 2048 afterwards");
+ is_int(1049, three->left, "and left is correct");
buffer_append(three, "", 1);
- ok_string(89, data, three->data);
+ is_string(data, three->data, "and data is correct");
free(data);
buffer_free(three);
Modified: tests/lib/concat-t.c
===================================================================
--- tests/lib/concat-t.c 2011-08-18 14:12:08 UTC (rev 9349)
+++ tests/lib/concat-t.c 2011-08-18 14:19:02 UTC (rev 9350)
@@ -1,34 +1,54 @@
-/* $Id$ */
-/* concat test suite. */
+/*
+ * concat test suite.
+ *
+ * $Id$
+ *
+ * The canonical version of this file is maintained in the rra-c-util package,
+ * which can be found at <http://www.eyrie.org/~eagle/software/rra-c-util/>.
+ *
+ * Written by Russ Allbery <rra at stanford.edu>
+ *
+ * The authors hereby relinquish any claim to any copyright that they may have
+ * in this work, whether granted under contract or by operation of law or
+ * international treaty, and hereby commit to the public, at large, that they
+ * shall not, at any time in the future, seek to enforce any copyright in this
+ * work against any person or entity, or prevent any person or entity from
+ * copying, publishing, distributing or creating derivative works of this
+ * work.
+ */
+#define LIBTEST_NEW_FORMAT 1
+
#include <stdio.h>
#include <string.h>
#include "inn/libinn.h"
#include "libtest.h"
-#define END (char *) 0
+#define END (char *) 0
-/* Memory leaks everywhere! Whoo-hoo! */
+/*
+ * Memory leaks everywhere! Whoo-hoo!
+ */
int
main(void)
{
- test_init(13);
+ plan(13);
- ok_string( 1, "a", concat("a", END));
- ok_string( 2, "ab", concat("a", "b", END));
- ok_string( 3, "ab", concat("ab", "", END));
- ok_string( 4, "ab", concat("", "ab", END));
- ok_string( 5, "", concat("", END));
- ok_string( 6, "abcde", concat("ab", "c", "", "de", END));
- ok_string( 7, "abcde", concat("abc", "de", END, "f", END));
+ is_string("a", concat("a", END), "concat 1");
+ is_string("ab", concat("a", "b", END), "concat 2");
+ is_string("ab", concat("ab", "", END), "concat 3");
+ is_string("ab", concat("", "ab", END), "concat 4");
+ is_string("", concat("", END), "concat 5");
+ is_string("abcde", concat("ab", "c", "", "de", END), "concat 6");
+ is_string("abcde", concat("abc", "de", END, "f", END), "concat 7");
- ok_string( 8, "/foo", concatpath("/bar", "/foo"));
- ok_string( 9, "/foo/bar", concatpath("/foo", "bar"));
- ok_string(10, "./bar", concatpath("/foo", "./bar"));
- ok_string(11, "/bar/baz/foo/bar", concatpath("/bar/baz", "foo/bar"));
- ok_string(12, "./foo", concatpath(NULL, "foo"));
- ok_string(13, "/foo/bar", concatpath(NULL, "/foo/bar"));
+ is_string("/foo", concatpath("/bar", "/foo"), "path 1");
+ is_string("/foo/bar", concatpath("/foo", "bar"), "path 2");
+ is_string("./bar", concatpath("/foo", "./bar"), "path 3");
+ is_string("/bar/baz/foo/bar", concatpath("/bar/baz", "foo/bar"), "path 4");
+ is_string("./foo", concatpath(NULL, "foo"), "path 5");
+ is_string("/foo/bar", concatpath(NULL, "/foo/bar"), "path 6");
return 0;
}
Modified: tests/lib/fakewrite.c
===================================================================
--- tests/lib/fakewrite.c 2011-08-18 14:12:08 UTC (rev 9349)
+++ tests/lib/fakewrite.c 2011-08-18 14:19:02 UTC (rev 9350)
@@ -1,6 +1,34 @@
-/* $Id$ */
-/* Fake write and writev functions for testing xwrite and xwritev. */
+/*
+ * Fake write and writev functions for testing xwrite and xwritev.
+ *
+ * $Id$
+ *
+ * The canonical version of this file is maintained in the rra-c-util package,
+ * which can be found at <http://www.eyrie.org/~eagle/software/rra-c-util/>.
+ *
+ * Copyright 2000, 2001, 2002, 2004 Russ Allbery <rra at stanford.edu>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+#define LIBTEST_NEW_FORMAT 1
+
#include "config.h"
#include "clibrary.h"
#include <errno.h>
@@ -12,21 +40,30 @@
ssize_t fake_pwrite(int, const void *, size_t, off_t);
ssize_t fake_writev(int, const struct iovec *, int);
-/* All the data is actually written into this buffer. We use write_offset
- to track how far we've written. */
+/*
+ * All the data is actually written into this buffer. We use write_offset
+ * to track how far we've written.
+ */
char write_buffer[256];
size_t write_offset = 0;
-/* If write_interrupt is non-zero, then half of the calls to write or writev
- will fail, returning -1 with errno set to EINTR. */
+/*
+ * If write_interrupt is non-zero, then half of the calls to write or writev
+ * will fail, returning -1 with errno set to EINTR.
+ */
int write_interrupt = 0;
-/* If write_fail is non-zero, all writes or writevs will return 0,
- indicating no progress in writing out the buffer. */
+/*
+ * If write_fail is non-zero, all writes or writevs will return 0, indicating
+ * no progress in writing out the buffer.
+ */
int write_fail = 0;
-/* Accept a write request and write only the first 32 bytes of it into
- write_buffer (or as much as will fit), returning the amount written. */
+
+/*
+ * Accept a write request and write only the first 32 bytes of it into
+ * write_buffer (or as much as will fit), returning the amount written.
+ */
ssize_t
fake_write(int fd UNUSED, const void *data, size_t n)
{
@@ -46,9 +83,12 @@
return total;
}
-/* Accept a pwrite request and write only the first 32 bytes of it into
- write_buffer at the specified offset (or as much as will fit), returning
- the amount written. */
+
+/*
+ * Accept a pwrite request and write only the first 32 bytes of it into
+ * write_buffer at the specified offset (or as much as will fit), returning
+ * the amount written.
+ */
ssize_t
fake_pwrite(int fd UNUSED, const void *data, size_t n, off_t offset)
{
@@ -71,8 +111,11 @@
return total;
}
-/* Accept an xwrite request and write only the first 32 bytes of it into
- write_buffer (or as much as will fit), returning the amount written. */
+
+/*
+ * Accept an xwrite request and write only the first 32 bytes of it into
+ * write_buffer (or as much as will fit), returning the amount written.
+ */
ssize_t
fake_writev(int fd UNUSED, const struct iovec *iov, int iovcnt)
{
Modified: tests/lib/getaddrinfo-t.c
===================================================================
--- tests/lib/getaddrinfo-t.c 2011-08-18 14:12:08 UTC (rev 9349)
+++ tests/lib/getaddrinfo-t.c 2011-08-18 14:19:02 UTC (rev 9350)
@@ -1,6 +1,24 @@
-/* $Id$ */
-/* getaddrinfo test suite. */
+/*
+ * getaddrinfo test suite.
+ *
+ * $Id$
+ *
+ * The canonical version of this file is maintained in the rra-c-util package,
+ * which can be found at <http://www.eyrie.org/~eagle/software/rra-c-util/>.
+ *
+ * Written by Russ Allbery <rra at stanford.edu>
+ *
+ * The authors hereby relinquish any claim to any copyright that they may have
+ * in this work, whether granted under contract or by operation of law or
+ * international treaty, and hereby commit to the public, at large, that they
+ * shall not, at any time in the future, seek to enforce any copyright in this
+ * work against any person or entity, or prevent any person or entity from
+ * copying, publishing, distributing or creating derivative works of this
+ * work.
+ */
+#define LIBTEST_NEW_FORMAT 1
+
#include "config.h"
#include "clibrary.h"
#include "portable/socket.h"
@@ -8,8 +26,11 @@
#include "inn/messages.h"
#include "libtest.h"
-/* If the native platform doesn't support AI_NUMERICSERV or AI_NUMERICHOST,
- pick some other values for them. */
+/*
+ * If the native platform doesn't support AI_NUMERICSERV or AI_NUMERICHOST,
+ * pick some other values for them that match the values set in our
+ * implementation.
+ */
#if AI_NUMERICSERV == 0
# undef AI_NUMERICSERV
# define AI_NUMERICSERV 0x0080
@@ -32,175 +53,231 @@
struct sockaddr_in *saddr;
struct hostent *host;
struct in_addr addr;
- int i;
- bool found;
+ struct servent *service;
+ int i, result;
+ int found;
- test_init(75);
+ plan(75);
- ok_string(1, "Host name lookup failure", test_gai_strerror(1));
- ok_string(2, "System error", test_gai_strerror(9));
- ok_string(3, "Unknown error", test_gai_strerror(40));
- ok_string(4, "Unknown error", test_gai_strerror(-37));
+ is_string("Host name lookup failure", test_gai_strerror(1),
+ "gai_strerror(1)");
+ is_string("System error", test_gai_strerror(9), "gai_strerror(9)");
+ is_string("Unknown error", test_gai_strerror(40), "gai_strerror(40)");
+ is_string("Unknown error", test_gai_strerror(-37), "gai_strerror(-37)");
- ok(5, test_getaddrinfo(NULL, "25", NULL, &ai) == 0);
- ok(6, ai->ai_family == AF_INET);
- ok(7, ai->ai_socktype == 0);
- ok(8, ai->ai_protocol == IPPROTO_TCP);
- ok(9, ai->ai_canonname == NULL);
- ok(10, ai->ai_addrlen == sizeof(struct sockaddr_in));
+ ok(test_getaddrinfo(NULL, "25", NULL, &ai) == 0, "service of 25");
+ is_int(AF_INET, ai->ai_family, "...right family");
+ is_int(0, ai->ai_socktype, "...right socktype");
+ is_int(IPPROTO_TCP, ai->ai_protocol, "...right protocol");
+ is_string(NULL, ai->ai_canonname, "...no canonname");
+ is_int(sizeof(struct sockaddr_in), ai->ai_addrlen, "...right addrlen");
saddr = (struct sockaddr_in *) ai->ai_addr;
- ok(11, saddr->sin_port == htons(25));
- ok(12, saddr->sin_addr.s_addr == INADDR_LOOPBACK);
+ is_int(htons(25), saddr->sin_port, "...right port");
+ ok(saddr->sin_addr.s_addr == INADDR_LOOPBACK, "...right address");
test_freeaddrinfo(ai);
memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_PASSIVE;
hints.ai_socktype = SOCK_STREAM;
- ok(13, test_getaddrinfo(NULL, "25", &hints, &ai) == 0);
- ok(14, ai->ai_socktype == SOCK_STREAM);
+ ok(test_getaddrinfo(NULL, "25", &hints, &ai) == 0, "passive lookup");
+ is_int(SOCK_STREAM, ai->ai_socktype, "...right socktype");
saddr = (struct sockaddr_in *) ai->ai_addr;
- ok(15, saddr->sin_port == htons(25));
- ok(16, saddr->sin_addr.s_addr == INADDR_ANY);
+ is_int(htons(25), saddr->sin_port, "...right port");
+ ok(saddr->sin_addr.s_addr == INADDR_ANY, "...right address");
test_freeaddrinfo(ai);
- hints.ai_socktype = 0;
- ok(17, test_getaddrinfo(NULL, "smtp", &hints, &ai) == 0);
- ok(18, ai->ai_socktype == SOCK_STREAM);
- saddr = (struct sockaddr_in *) ai->ai_addr;
- ok(19, saddr->sin_port == htons(25));
- ok(20, saddr->sin_addr.s_addr == INADDR_ANY);
- test_freeaddrinfo(ai);
+ service = getservbyname("smtp", "tcp");
+ if (service == NULL)
+ skip_block(4, "smtp service not found");
+ else {
+ hints.ai_socktype = 0;
+ ok(test_getaddrinfo(NULL, "smtp", &hints, &ai) == 0,
+ "service of smtp");
+ is_int(SOCK_STREAM, ai->ai_socktype, "...right socktype");
+ saddr = (struct sockaddr_in *) ai->ai_addr;
+ is_int(htons(25), saddr->sin_port, "...right port");
+ ok(saddr->sin_addr.s_addr == INADDR_ANY, "...right address");
+ test_freeaddrinfo(ai);
+ }
hints.ai_flags = AI_NUMERICSERV;
- ok(21, test_getaddrinfo(NULL, "smtp", &hints, &ai) == EAI_NONAME);
- ok(22, test_getaddrinfo(NULL, "25 smtp", &hints, &ai) == EAI_NONAME);
- ok(23, test_getaddrinfo(NULL, "25 ", &hints, &ai) == EAI_NONAME);
- ok(24, test_getaddrinfo(NULL, "25", &hints, &ai) == 0);
+ ok(test_getaddrinfo(NULL, "smtp", &hints, &ai) == EAI_NONAME,
+ "AI_NUMERICSERV with smtp");
+ ok(test_getaddrinfo(NULL, "25 smtp", &hints, &ai) == EAI_NONAME,
+ "AI_NUMERICSERV with 25 smtp");
+ ok(test_getaddrinfo(NULL, "25 ", &hints, &ai) == EAI_NONAME,
+ "AI_NUMERICSERV with 25 space");
+ ok(test_getaddrinfo(NULL, "25", &hints, &ai) == 0,
+ "valid AI_NUMERICSERV");
saddr = (struct sockaddr_in *) ai->ai_addr;
- ok(25, saddr->sin_port == htons(25));
- ok(26, saddr->sin_addr.s_addr == INADDR_LOOPBACK);
+ is_int(htons(25), saddr->sin_port, "...right port");
+ ok(saddr->sin_addr.s_addr == INADDR_LOOPBACK, "...right address");
test_freeaddrinfo(ai);
- ok(27, test_getaddrinfo(NULL, NULL, NULL, &ai) == EAI_NONAME);
+ ok(test_getaddrinfo(NULL, NULL, NULL, &ai) == EAI_NONAME, "EAI_NONAME");
hints.ai_flags = 2000;
- ok(28, test_getaddrinfo(NULL, "25", &hints, &ai) == EAI_BADFLAGS);
+ ok(test_getaddrinfo(NULL, "25", &hints, &ai) == EAI_BADFLAGS,
+ "EAI_BADFLAGS");
hints.ai_flags = 0;
hints.ai_socktype = SOCK_RAW;
- ok(29, test_getaddrinfo(NULL, "25", &hints, &ai) == EAI_SOCKTYPE);
+ ok(test_getaddrinfo(NULL, "25", &hints, &ai) == EAI_SOCKTYPE,
+ "EAI_SOCKTYPE");
hints.ai_socktype = 0;
hints.ai_family = AF_UNIX;
- ok(30, test_getaddrinfo(NULL, "25", &hints, &ai) == EAI_FAMILY);
+ ok(test_getaddrinfo(NULL, "25", &hints, &ai) == EAI_FAMILY, "EAI_FAMILY");
hints.ai_family = AF_UNSPEC;
inet_aton("10.20.30.40", &addr);
- ok(31, test_getaddrinfo("10.20.30.40", NULL, NULL, &ai) == 0);
- ok(32, ai->ai_family == AF_INET);
- ok(33, ai->ai_socktype == 0);
- ok(34, ai->ai_protocol == IPPROTO_TCP);
- ok(35, ai->ai_canonname == NULL);
- ok(36, ai->ai_addrlen == sizeof(struct sockaddr_in));
+ ok(test_getaddrinfo("10.20.30.40", NULL, NULL, &ai) == 0,
+ "IP address lookup");
+ is_int(AF_INET, ai->ai_family, "...right family");
+ is_int(0, ai->ai_socktype, "...right socktype");
+ is_int(IPPROTO_TCP, ai->ai_protocol, "...right protocol");
+ is_string(NULL, ai->ai_canonname, "...no canonname");
+ is_int(sizeof(struct sockaddr_in), ai->ai_addrlen, "...right addrlen");
saddr = (struct sockaddr_in *) ai->ai_addr;
- ok(37, saddr->sin_port == 0);
- ok(38, saddr->sin_addr.s_addr == addr.s_addr);
+ is_int(0, saddr->sin_port, "...right port");
+ ok(saddr->sin_addr.s_addr == addr.s_addr, "...right address");
test_freeaddrinfo(ai);
- ok(39, test_getaddrinfo("10.20.30.40", "smtp", &hints, &ai) == 0);
- ok(40, ai->ai_family == AF_INET);
- ok(41, ai->ai_socktype == SOCK_STREAM);
- ok(42, ai->ai_protocol == IPPROTO_TCP);
- ok(43, ai->ai_canonname == NULL);
- saddr = (struct sockaddr_in *) ai->ai_addr;
- ok(44, saddr->sin_port == htons(25));
- ok(45, saddr->sin_addr.s_addr == addr.s_addr);
- test_freeaddrinfo(ai);
+ if (service == NULL)
+ skip_block(7, "smtp service not found");
+ else {
+ ok(test_getaddrinfo("10.20.30.40", "smtp", &hints, &ai) == 0,
+ "IP address lookup with smtp service");
+ is_int(AF_INET, ai->ai_family, "...right family");
+ is_int(SOCK_STREAM, ai->ai_socktype, "...right socktype");
+ is_int(IPPROTO_TCP, ai->ai_protocol, "...right protocol");
+ is_string(NULL, ai->ai_canonname, "...no canonname");
+ saddr = (struct sockaddr_in *) ai->ai_addr;
+ is_int(htons(25), saddr->sin_port, "...right port");
+ ok(saddr->sin_addr.s_addr == addr.s_addr, "...right address");
+ test_freeaddrinfo(ai);
+ }
hints.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV;
- ok(46, test_getaddrinfo("10.2.3.4", "smtp", &hints, &ai) == EAI_NONAME);
- ok(47, test_getaddrinfo("example.com", "25", &hints, &ai) == EAI_NONAME);
- ok(48, test_getaddrinfo("10.20.30.40", "25", &hints, &ai) == 0);
+ ok(test_getaddrinfo("10.2.3.4", "smtp", &hints, &ai) == EAI_NONAME,
+ "AI_NUMERICHOST and AI_NUMERICSERV with symbolic service");
+ ok(test_getaddrinfo("example.com", "25", &hints, &ai) == EAI_NONAME,
+ "AI_NUMERICHOST and AI_NUMERICSERV with symbolic name");
+ ok(test_getaddrinfo("10.20.30.40", "25", &hints, &ai) == 0,
+ "valid AI_NUMERICHOST and AI_NUMERICSERV");
saddr = (struct sockaddr_in *) ai->ai_addr;
- ok(49, saddr->sin_port == htons(25));
- ok(50, saddr->sin_addr.s_addr == addr.s_addr);
+ is_int(htons(25), saddr->sin_port, "...right port");
+ ok(saddr->sin_addr.s_addr == addr.s_addr, "...right address");
test_freeaddrinfo(ai);
- hints.ai_flags = AI_NUMERICHOST | AI_CANONNAME;
- ok(51, test_getaddrinfo("10.20.30.40", "smtp", &hints, &ai) == 0);
- ok_string(52, "10.20.30.40", ai->ai_canonname);
- saddr = (struct sockaddr_in *) ai->ai_addr;
- ok(53, saddr->sin_port == htons(25));
- ok(54, saddr->sin_addr.s_addr == addr.s_addr);
- test_freeaddrinfo(ai);
+ if (service == NULL)
+ skip_block(4, "smtp service not found");
+ else {
+ hints.ai_flags = AI_NUMERICHOST | AI_CANONNAME;
+ ok(test_getaddrinfo("10.20.30.40", "smtp", &hints, &ai) == 0,
+ "AI_NUMERICHOST and AI_CANONNAME");
+ is_string("10.20.30.40", ai->ai_canonname, "...right canonname");
+ saddr = (struct sockaddr_in *) ai->ai_addr;
+ is_int(htons(25), saddr->sin_port, "...right port");
+ ok(saddr->sin_addr.s_addr == addr.s_addr, "...right address");
+ test_freeaddrinfo(ai);
+ }
- hints.ai_flags = 0;
- hints.ai_socktype = SOCK_DGRAM;
- ok(55, test_getaddrinfo("10.20.30.40", "domain", &hints, &ai) == 0);
- ok(56, ai->ai_socktype == SOCK_DGRAM);
- ok(57, ai->ai_canonname == NULL);
- saddr = (struct sockaddr_in *) ai->ai_addr;
- ok(58, saddr->sin_port == htons(53));
- ok(59, saddr->sin_addr.s_addr == addr.s_addr);
- test_freeaddrinfo(ai);
+ service = getservbyname("domain", "udp");
+ if (service == NULL)
+ skip_block(5, "domain service not found");
+ else {
+ hints.ai_flags = 0;
+ hints.ai_socktype = SOCK_DGRAM;
+ ok(test_getaddrinfo("10.20.30.40", "domain", &hints, &ai) == 0,
+ "domain service with UDP hint");
+ is_int(SOCK_DGRAM, ai->ai_socktype, "...right socktype");
+ is_string(NULL, ai->ai_canonname, "...no canonname");
+ saddr = (struct sockaddr_in *) ai->ai_addr;
+ is_int(htons(53), saddr->sin_port, "...right port");
+ ok(saddr->sin_addr.s_addr == addr.s_addr, "...right address");
+ test_freeaddrinfo(ai);
+ }
/* Hopefully this will always resolve. */
- hints.ai_socktype = SOCK_STREAM;
- ok(60, test_getaddrinfo("www.isc.org", "80", &hints, &ai) == 0);
- ok(61, ai->ai_socktype == SOCK_STREAM);
- ok(62, ai->ai_canonname == NULL);
- saddr = (struct sockaddr_in *) ai->ai_addr;
- ok(63, saddr->sin_port == htons(80));
- ok(64, saddr->sin_addr.s_addr != INADDR_ANY);
- addr = saddr->sin_addr;
- test_freeaddrinfo(ai);
+ host = gethostbyname("www.isc.org");
+ if (host == NULL)
+ skip_block(9, "cannot look up www.isc.org");
+ else {
+ hints.ai_flags = 0;
+ hints.ai_socktype = SOCK_STREAM;
+ ok(test_getaddrinfo("www.isc.org", "80", &hints, &ai) == 0,
+ "lookup of www.isc.org");
+ is_int(SOCK_STREAM, ai->ai_socktype, "...right socktype");
+ is_string(NULL, ai->ai_canonname, "...no canonname");
+ saddr = (struct sockaddr_in *) ai->ai_addr;
+ is_int(htons(80), saddr->sin_port, "...right port");
+ ok(saddr->sin_addr.s_addr != INADDR_ANY, "...address is something");
+ addr = saddr->sin_addr;
+ test_freeaddrinfo(ai);
- hints.ai_flags = AI_CANONNAME;
- ok(65, test_getaddrinfo("www.isc.org", "80", &hints, &ai) == 0);
- ok(66, ai->ai_canonname != NULL);
- saddr = (struct sockaddr_in *) ai->ai_addr;
- ok(67, saddr->sin_port == htons(80));
- ok(68, saddr->sin_addr.s_addr == addr.s_addr);
- test_freeaddrinfo(ai);
+ hints.ai_flags = AI_CANONNAME;
+ ok(test_getaddrinfo("www.isc.org", "80", &hints, &ai) == 0,
+ "lookup of www.isc.org with A_CANONNAME");
+ ok(ai->ai_canonname != NULL, "...canonname isn't null");
+ saddr = (struct sockaddr_in *) ai->ai_addr;
+ is_int(htons(80), saddr->sin_port, "...right port");
+ ok(saddr->sin_addr.s_addr == addr.s_addr, "...and same address");
+ test_freeaddrinfo(ai);
+ }
/* Included because it had multiple A records. */
- ok(69, test_getaddrinfo("cnn.com", "80", NULL, &ai) == 0);
- saddr = (struct sockaddr_in *) ai->ai_addr;
- ok(70, saddr->sin_port == htons(80));
- ok(71, saddr->sin_addr.s_addr != INADDR_ANY);
- addr = saddr->sin_addr;
- test_freeaddrinfo(ai);
+ host = gethostbyname("cnn.com");
+ if (host == NULL)
+ skip_block(3, "cannot look up cnn.com");
+ else {
+ ok(test_getaddrinfo("cnn.com", "80", NULL, &ai) == 0,
+ "lookup of cnn.com with multiple A records");
+ saddr = (struct sockaddr_in *) ai->ai_addr;
+ is_int(htons(80), saddr->sin_port, "...right port");
+ ok(saddr->sin_addr.s_addr != INADDR_ANY, "...address is something");
+ test_freeaddrinfo(ai);
+ }
- hints.ai_flags = AI_CANONNAME;
- ok(72, test_getaddrinfo("foo.invalid", NULL, NULL, &ai) == EAI_NONAME);
+ host = gethostbyname("addrinfo-test.invalid");
+ if (host != NULL)
+ skip("lookup of addrinfo-test.invalid succeeded");
+ else {
+ result = test_getaddrinfo("addrinfo-test.invalid", NULL, NULL, &ai);
+ is_int(EAI_NONAME, result, "lookup of invalid address");
+ }
+
host = gethostbyname("cnn.com");
if (host == NULL) {
- skip_block(73, 3, "cannot look up cnn.com");
+ skip_block(3, "cannot look up cnn.com");
exit(0);
}
- ok(73, test_getaddrinfo("cnn.com", NULL, &hints, &ai) == 0);
+ hints.ai_socktype = SOCK_STREAM;
+ hints.ai_flags = AI_CANONNAME;
+ ok(test_getaddrinfo("cnn.com", NULL, &hints, &ai) == 0,
+ "lookup of cnn.com");
saddr = (struct sockaddr_in *) ai->ai_addr;
- ok(74, saddr->sin_port == 0);
+ is_int(0, saddr->sin_port, "...port is 0");
first = ai;
- for (found = false; ai != NULL; ai = ai->ai_next) {
+ for (found = 0; ai != NULL; ai = ai->ai_next) {
if (!strcmp(ai->ai_canonname, first->ai_canonname) == 0) {
- ok(75, false);
+ ok(0, "...canonname matches");
break;
}
if (ai != first && ai->ai_canonname == first->ai_canonname) {
- ok(75, false);
+ ok(0, "...each canonname is a separate pointer");
break;
}
- found = false;
+ found = 0;
saddr = (struct sockaddr_in *) ai->ai_addr;
addr = saddr->sin_addr;
for (i = 0; host->h_addr_list[i] != NULL; i++)
if (memcmp(&addr, host->h_addr_list[i], host->h_length) == 0)
- found = true;
+ found = 1;
if (!found) {
- ok(75, false);
+ ok(0, "...result found in gethostbyname address list");
break;
}
}
if (found)
- ok(75, true);
+ ok(1, "...result found in gethostbyname address list");
test_freeaddrinfo(ai);
return 0;
Modified: tests/lib/getnameinfo-t.c
===================================================================
--- tests/lib/getnameinfo-t.c 2011-08-18 14:12:08 UTC (rev 9349)
+++ tests/lib/getnameinfo-t.c 2011-08-18 14:19:02 UTC (rev 9350)
@@ -1,6 +1,24 @@
-/* $Id$ */
-/* getnameinfo test suite. */
+/*
+ * getnameinfo test suite.
+ *
+ * $Id$
+ *
+ * The canonical version of this file is maintained in the rra-c-util package,
+ * which can be found at <http://www.eyrie.org/~eagle/software/rra-c-util/>.
+ *
+ * Written by Russ Allbery <rra at stanford.edu>
+ *
+ * The authors hereby relinquish any claim to any copyright that they may have
+ * in this work, whether granted under contract or by operation of law or
+ * international treaty, and hereby commit to the public, at large, that they
+ * shall not, at any time in the future, seek to enforce any copyright in this
+ * work against any person or entity, or prevent any person or entity from
+ * copying, publishing, distributing or creating derivative works of this
+ * work.
+ */
+#define LIBTEST_NEW_FORMAT 1
+
#include "config.h"
#include "clibrary.h"
#include "portable/socket.h"
@@ -25,78 +43,99 @@
struct sockaddr *sa = (struct sockaddr *) &sin;
int status;
struct hostent *hp;
+ struct servent *serv;
char *name;
- test_init(26);
+ plan(26);
- /* Test the easy stuff that requires no assumptions. Hopefully everyone
- has nntp, exec, and biff as services. */
+ /*
+ * Test the easy stuff that requires no assumptions. Hopefully everyone
+ * has nntp, exec, and biff as services. exec and biff are special since
+ * they're one of the rare universal pairs of services that are both on
+ * the same port, but with different protocols.
+ */
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons(119);
inet_aton("10.20.30.40", &sin.sin_addr);
status = test_getnameinfo(sa, sizeof(sin), NULL, 0, NULL, 0, 0);
- ok_int(1, EAI_NONAME, status);
+ is_int(EAI_NONAME, status, "EAI_NONAME from NULL");
status = test_getnameinfo(sa, sizeof(sin), node, 0, service, 0, 0);
- ok_int(2, EAI_NONAME, status);
+ is_int(EAI_NONAME, status, "EAI_NONAME from length of 0");
status = test_getnameinfo(sa, sizeof(sin), NULL, 0, service,
sizeof(service), 0);
- ok_int(3, 0, status);
- ok_string(4, "nntp", service);
- service[0] = '\0';
- status = test_getnameinfo(sa, sizeof(sin), NULL, 0, service, 1, 0);
- ok_int(5, EAI_OVERFLOW, status);
- status = test_getnameinfo(sa, sizeof(sin), NULL, 0, service, 4, 0);
- ok_int(6, EAI_OVERFLOW, status);
- status = test_getnameinfo(sa, sizeof(sin), NULL, 0, service, 5, 0);
- ok_int(7, 0, status);
- ok_string(8, "nntp", service);
+ is_int(0, status, "lookup of port 119");
+ serv = getservbyname("nntp", "tcp");
+ if (serv == NULL)
+ skip_block(5, "nntp service not found");
+ else {
+ is_string("nntp", service, "...found nntp");
+ service[0] = '\0';
+ status = test_getnameinfo(sa, sizeof(sin), NULL, 0, service, 1, 0);
+ is_int(EAI_OVERFLOW, status, "EAI_OVERFLOW with one character");
+ status = test_getnameinfo(sa, sizeof(sin), NULL, 0, service, 4, 0);
+ is_int(EAI_OVERFLOW, status, "EAI_OVERFLOW with four characters");
+ status = test_getnameinfo(sa, sizeof(sin), NULL, 0, service, 5, 0);
+ is_int(0, status, "fits in five characters");
+ is_string("nntp", service, "...and found nntp");
+ }
status = test_getnameinfo(sa, sizeof(sin), NULL, 0, service,
sizeof(service), NI_NUMERICSERV);
- ok_int(9, 0, status);
- ok_string(10, "119", service);
+ is_int(0, status, "NI_NUMERICSERV");
+ is_string("119", service, "...and returns number");
sin.sin_port = htons(512);
status = test_getnameinfo(sa, sizeof(sin), NULL, 0, service,
sizeof(service), 0);
- ok_int(11, 0, status);
- ok_string(12, "exec", service);
+ is_int(0, status, "lookup of 512 TCP");
+ serv = getservbyname("exec", "tcp");
+ if (serv == NULL)
+ skip("exec service not found");
+ else
+ is_string("exec", service, "...and found exec");
status = test_getnameinfo(sa, sizeof(sin), NULL, 0, service,
sizeof(service), NI_DGRAM);
- ok_int(13, 0, status);
- ok_string(14, "biff", service);
+ is_int(0, status, "lookup of 512 UDP");
+ serv = getservbyname("biff", "udp");
+ if (serv == NULL)
+ skip("biff service not found");
+ else
+ is_string("biff", service, "...and found biff");
status = test_getnameinfo(sa, sizeof(sin), node, sizeof(node), NULL, 0,
NI_NUMERICHOST);
- ok_int(15, 0, status);
- ok_string(16, "10.20.30.40", node);
+ is_int(0, status, "lookup with NI_NUMERICHOST");
+ is_string("10.20.30.40", node, "...and found correct IP address");
node[0] = '\0';
status = test_getnameinfo(sa, sizeof(sin), node, 1, NULL, 0,
NI_NUMERICHOST);
- ok_int(17, EAI_OVERFLOW, status);
+ is_int(EAI_OVERFLOW, status, "EAI_OVERFLOW with one character");
status = test_getnameinfo(sa, sizeof(sin), node, 11, NULL, 0,
NI_NUMERICHOST);
- ok_int(18, EAI_OVERFLOW, status);
+ is_int(EAI_OVERFLOW, status, "EAI_OVERFLOW with 11 characters");
status = test_getnameinfo(sa, sizeof(sin), node, 12, NULL, 0,
NI_NUMERICHOST);
- ok_int(19, 0, status);
- ok_string(20, "10.20.30.40", node);
+ is_int(0, status, "fits into 12 characters");
+ is_string("10.20.30.40", node, "...and found correct IP address");
- /* Okay, now it gets annoying. Do a forward and then reverse lookup of
- some well-known host and make sure that getnameinfo returns the same
- results. This may need to be skipped. */
+ /*
+ * Okay, now it gets annoying. Do a forward and then reverse lookup of
+ * some well-known host and make sure that getnameinfo returns the same
+ * results. This may need to be skipped.
+ */
hp = gethostbyname("www.isc.org");
if (hp == NULL)
- skip_block(21, 2, "cannot look up www.isc.org");
+ skip_block(2, "cannot look up www.isc.org");
else {
memcpy(&sin.sin_addr, hp->h_addr, sizeof(sin.sin_addr));
- hp = gethostbyaddr((char *) &sin.sin_addr, sizeof(sin.sin_addr), AF_INET);
+ hp = gethostbyaddr((const void *) &sin.sin_addr, sizeof(sin.sin_addr),
+ AF_INET);
if (hp == NULL || strchr(hp->h_name, '.') == NULL)
- skip_block(21, 2, "cannot reverse-lookup www.isc.org");
+ skip_block(2, "cannot reverse-lookup www.isc.org");
else {
name = xstrdup(hp->h_name);
status = test_getnameinfo(sa, sizeof(sin), node, sizeof(node),
NULL, 0, 0);
- ok_int(21, 0, status);
- ok_string(22, name, node);
+ is_int(0, status, "lookup of www.isc.org IP address");
+ is_string(name, node, "...matches gethostbyaddr");
free(name);
}
}
@@ -104,16 +143,16 @@
/* Hope that no one is weird enough to put 0.0.0.0 into DNS. */
inet_aton("0.0.0.0", &sin.sin_addr);
status = test_getnameinfo(sa, sizeof(sin), node, sizeof(node), NULL, 0, 0);
- ok_int(23, 0, status);
- ok_string(24, "0.0.0.0", node);
+ is_int(0, status, "lookup of 0.0.0.0");
+ is_string("0.0.0.0", node, "...returns the IP address");
node[0] = '\0';
status = test_getnameinfo(sa, sizeof(sin), node, sizeof(node), NULL, 0,
NI_NAMEREQD);
- ok_int(25, EAI_NONAME, status);
+ is_int(EAI_NONAME, status, "...fails with NI_NAMEREQD");
sin.sin_family = AF_UNIX;
status = test_getnameinfo(sa, sizeof(sin), node, sizeof(node), NULL, 0, 0);
- ok_int(26, EAI_FAMILY, status);
+ is_int(EAI_FAMILY, status, "EAI_FAMILY");
return 0;
}
Modified: tests/lib/inet_aton-t.c
===================================================================
--- tests/lib/inet_aton-t.c 2011-08-18 14:12:08 UTC (rev 9349)
+++ tests/lib/inet_aton-t.c 2011-08-18 14:19:02 UTC (rev 9350)
@@ -1,6 +1,24 @@
-/* $Id$ */
-/* inet_aton test suite. */
+/*
+ * inet_aton test suite.
+ *
+ * $Id$
+ *
+ * The canonical version of this file is maintained in the rra-c-util package,
+ * which can be found at <http://www.eyrie.org/~eagle/software/rra-c-util/>.
+ *
+ * Written by Russ Allbery <rra at stanford.edu>
+ *
+ * The authors hereby relinquish any claim to any copyright that they may have
+ * in this work, whether granted under contract or by operation of law or
+ * international treaty, and hereby commit to the public, at large, that they
+ * shall not, at any time in the future, seek to enforce any copyright in this
+ * work against any person or entity, or prevent any person or entity from
+ * copying, publishing, distributing or creating derivative works of this
+ * work.
+ */
+#define LIBTEST_NEW_FORMAT 1
+
#include "config.h"
#include "clibrary.h"
#include <netinet/in.h>
@@ -9,90 +27,87 @@
int test_inet_aton(const char *, struct in_addr *);
+
static void
-test_addr(int n, const char *string, unsigned long addr)
+test_addr(const char *string, unsigned long addr)
{
- bool success, okay;
+ int success;
struct in_addr in;
success = test_inet_aton(string, &in);
- okay = (success && in.s_addr == htonl(addr));
-
- ok(n, okay);
- if (!okay && !success)
- diag(" success: %d\n", success);
- if (!okay && in.s_addr != htonl(addr))
- diag(" want: %lx\n saw: %lx\n", (unsigned long) htonl(addr),
- (unsigned long) in.s_addr);
+ ok(success, "inet_aton on %s", string);
+ is_hex(htonl(addr), in.s_addr, "...matches expected value");
}
+
static void
-test_fail(int n, const char *string)
+test_fail(const char *string)
{
struct in_addr in;
int success;
in.s_addr = htonl(0x01020304UL);
success = test_inet_aton(string, &in);
- success = (success == 0 && in.s_addr == htonl(0x01020304UL));
- ok(n, success);
+ ok(success == 0, "inet_aton on %s fails", string);
+ is_hex(htonl(0x01020304UL), in.s_addr, "...and leaves in unchanged");
}
+
int
main(void)
{
- test_init(46);
+ plan(92);
- test_addr( 1, "0.0.0.0", 0);
- test_addr( 2, "127.0.0.000000", 0x7f000000UL);
- test_addr( 3, "255.255.255.255", 0xffffffffUL);
- test_addr( 4, "172.200.232.199", 0xacc8e8c7UL);
- test_addr( 5, "1.2.3.4", 0x01020304UL);
+ test_addr( "0.0.0.0", 0);
+ test_addr( "127.0.0.000000", 0x7f000000UL);
+ test_addr( "255.255.255.255", 0xffffffffUL);
+ test_addr( "172.200.232.199", 0xacc8e8c7UL);
+ test_addr( "1.2.3.4", 0x01020304UL);
- test_addr( 6, "0x0.0x0.0x0.0x0", 0);
- test_addr( 7, "0x7f.0x000.0x0.0x00", 0x7f000000UL);
- test_addr( 8, "0xff.0xFf.0xFF.0xff", 0xffffffffUL);
- test_addr( 9, "0xAC.0xc8.0xe8.0xC7", 0xacc8e8c7UL);
- test_addr(10, "0xAa.0xbB.0xCc.0xdD", 0xaabbccddUL);
- test_addr(11, "0xEe.0xfF.0.0x00000", 0xeeff0000UL);
- test_addr(12, "0x1.0x2.0x00003.0x4", 0x01020304UL);
+ test_addr( "0x0.0x0.0x0.0x0", 0);
+ test_addr("0x7f.0x000.0x0.0x00", 0x7f000000UL);
+ test_addr("0xff.0xFf.0xFF.0xff", 0xffffffffUL);
+ test_addr("0xAC.0xc8.0xe8.0xC7", 0xacc8e8c7UL);
+ test_addr("0xAa.0xbB.0xCc.0xdD", 0xaabbccddUL);
+ test_addr("0xEe.0xfF.0.0x00000", 0xeeff0000UL);
+ test_addr("0x1.0x2.0x00003.0x4", 0x01020304UL);
- test_addr(13, "000000.00.000.00", 0);
- test_addr(14, "0177.0", 0x7f000000UL);
- test_addr(15, "0377.0377.0377.0377", 0xffffffffUL);
- test_addr(16, "0254.0310.0350.0307", 0xacc8e8c7UL);
- test_addr(17, "00001.02.3.00000004", 0x01020304UL);
+ test_addr( "000000.00.000.00", 0);
+ test_addr( "0177.0", 0x7f000000UL);
+ test_addr("0377.0377.0377.0377", 0xffffffffUL);
+ test_addr("0254.0310.0350.0307", 0xacc8e8c7UL);
+ test_addr("00001.02.3.00000004", 0x01020304UL);
- test_addr(18, "16909060", 0x01020304UL);
- test_addr(19, "172.062164307", 0xacc8e8c7UL);
- test_addr(20, "172.0xc8.0xe8c7", 0xacc8e8c7UL);
- test_addr(21, "127.1", 0x7f000001UL);
- test_addr(22, "0xffffffff", 0xffffffffUL);
- test_addr(23, "127.0xffffff", 0x7fffffffUL);
- test_addr(24, "127.127.0xffff", 0x7f7fffffUL);
+ test_addr( "16909060", 0x01020304UL);
+ test_addr( "172.062164307", 0xacc8e8c7UL);
+ test_addr( "172.0xc8.0xe8c7", 0xacc8e8c7UL);
+ test_addr( "127.1", 0x7f000001UL);
+ test_addr( "0xffffffff", 0xffffffffUL);
+ test_addr( "127.0xffffff", 0x7fffffffUL);
+ test_addr( "127.127.0xffff", 0x7f7fffffUL);
- test_fail(25, "");
- test_fail(26, "Donald Duck!");
- test_fail(27, "a127.0.0.1");
- test_fail(28, "aaaabbbb");
- test_fail(29, "0x100000000");
- test_fail(30, "0xfffffffff");
- test_fail(31, "127.0xfffffff");
- test_fail(32, "127.376926742");
- test_fail(33, "127.127.01452466");
- test_fail(34, "127.127.127.0x100");
- test_fail(35, "256.0");
- test_fail(36, "127.0378.127.127");
- test_fail(37, "127.127.0x100.127");
- test_fail(38, "127.0.o.1");
- test_fail(39, "127.127.127.127v");
- test_fail(40, "ef.127.127.127");
- test_fail(41, "0128.127.127.127");
- test_fail(42, "0xeg.127");
- test_fail(43, ".127.127");
- test_fail(44, "127.127.");
- test_fail(45, "127..127");
- test_fail(46, "de.ad.be.ef");
+ test_fail( "");
+ test_fail( "Donald Duck!");
+ test_fail( "a127.0.0.1");
+ test_fail( "aaaabbbb");
+ test_fail( "0x100000000");
+ test_fail( "0xfffffffff");
+ test_fail( "127.0xfffffff");
+ test_fail( "127.376926742");
+ test_fail( "127.127.01452466");
+ test_fail("127.127.127.0x100");
+ test_fail( "256.0");
+ test_fail( "127.0378.127.127");
+ test_fail("127.127.0x100.127");
+ test_fail( "127.0.o.1");
+ test_fail( "127.127.127.127v");
+ test_fail( "ef.127.127.127");
+ test_fail( "0128.127.127.127");
+ test_fail( "0xeg.127");
+ test_fail( ".127.127");
+ test_fail( "127.127.");
+ test_fail( "127..127");
+ test_fail( "de.ad.be.ef");
return 0;
}
Modified: tests/lib/inet_ntoa-t.c
===================================================================
--- tests/lib/inet_ntoa-t.c 2011-08-18 14:12:08 UTC (rev 9349)
+++ tests/lib/inet_ntoa-t.c 2011-08-18 14:19:02 UTC (rev 9350)
@@ -1,6 +1,24 @@
-/* $Id$ */
-/* inet_ntoa test suite. */
+/*
+ * inet_ntoa test suite.
+ *
+ * $Id$
+ *
+ * The canonical version of this file is maintained in the rra-c-util package,
+ * which can be found at <http://www.eyrie.org/~eagle/software/rra-c-util/>.
+ *
+ * Written by Russ Allbery <rra at stanford.edu>
+ *
+ * The authors hereby relinquish any claim to any copyright that they may have
+ * in this work, whether granted under contract or by operation of law or
+ * international treaty, and hereby commit to the public, at large, that they
+ * shall not, at any time in the future, seek to enforce any copyright in this
+ * work against any person or entity, or prevent any person or entity from
+ * copying, publishing, distributing or creating derivative works of this
+ * work.
+ */
+#define LIBTEST_NEW_FORMAT 1
+
#include "config.h"
#include "clibrary.h"
#include <netinet/in.h>
@@ -9,25 +27,27 @@
const char *test_inet_ntoa(const struct in_addr);
+
static void
-test_addr(int n, const char *expected, unsigned long addr)
+test_addr(const char *expected, unsigned long addr)
{
struct in_addr in;
in.s_addr = htonl(addr);
- ok_string(n, expected, test_inet_ntoa(in));
+ is_string(expected, test_inet_ntoa(in), "address %s", expected);
}
+
int
main(void)
{
- test_init(5);
+ plan(5);
- test_addr(1, "0.0.0.0", 0x0);
- test_addr(2, "127.0.0.0", 0x7f000000UL);
- test_addr(3, "255.255.255.255", 0xffffffffUL);
- test_addr(4, "172.200.232.199", 0xacc8e8c7UL);
- test_addr(5, "1.2.3.4", 0x01020304UL);
+ test_addr( "0.0.0.0", 0x0);
+ test_addr( "127.0.0.0", 0x7f000000UL);
+ test_addr("255.255.255.255", 0xffffffffUL);
+ test_addr("172.200.232.199", 0xacc8e8c7UL);
+ test_addr( "1.2.3.4", 0x01020304UL);
return 0;
}
Modified: tests/lib/inet_ntop-t.c
===================================================================
--- tests/lib/inet_ntop-t.c 2011-08-18 14:12:08 UTC (rev 9349)
+++ tests/lib/inet_ntop-t.c 2011-08-18 14:19:02 UTC (rev 9350)
@@ -1,6 +1,24 @@
-/* $Id$ */
-/* inet_ntop test suite. */
+/*
+ * inet_ntop test suite.
+ *
+ * $Id$
+ *
+ * The canonical version of this file is maintained in the rra-c-util package,
+ * which can be found at <http://www.eyrie.org/~eagle/software/rra-c-util/>.
+ *
+ * Written by Russ Allbery <rra at stanford.edu>
+ *
+ * The authors hereby relinquish any claim to any copyright that they may have
+ * in this work, whether granted under contract or by operation of law or
+ * international treaty, and hereby commit to the public, at large, that they
+ * shall not, at any time in the future, seek to enforce any copyright in this
+ * work against any person or entity, or prevent any person or entity from
+ * copying, publishing, distributing or creating derivative works of this
+ * work.
+ */
+#define LIBTEST_NEW_FORMAT 1
+
#include "config.h"
#include "clibrary.h"
#include "portable/socket.h"
@@ -8,48 +26,47 @@
#include "libtest.h"
+/* Some systems too old to have inet_ntop don't have EAFNOSUPPORT. */
#ifndef EAFNOSUPPORT
# define EAFNOSUPPORT EDOM
#endif
const char *test_inet_ntop(int, const void *, char *, socklen_t);
-static int
-test_addr(int n, const char *expected, unsigned long addr)
+
+static void
+test_addr(const char *expected, unsigned long addr)
{
struct in_addr in;
char result[INET_ADDRSTRLEN];
in.s_addr = htonl(addr);
if (test_inet_ntop(AF_INET, &in, result, sizeof(result)) == NULL) {
- ok(n++, false);
- diag("cannot convert %lu: %s", addr, strerror(errno));
+ printf("# cannot convert %lu: %s", addr, strerror(errno));
+ ok(0, "converting %s", expected);
} else
- ok(n++, true);
- ok_string(n++, expected, result);
- return n;
+ ok(1, "converting %s", expected);
+ is_string(expected, result, "...with correct result");
}
+
int
main(void)
{
- int n;
+ plan(6 + 5 * 2);
- test_init(6 + 5 * 2);
+ ok(test_inet_ntop(AF_UNIX, NULL, NULL, 0) == NULL, "AF_UNIX failure");
+ is_int(EAFNOSUPPORT, errno, "...with right errno");
+ ok(test_inet_ntop(AF_INET, NULL, NULL, 0) == NULL, "empty buffer");
+ is_int(ENOSPC, errno, "...with right errno");
+ ok(test_inet_ntop(AF_INET, NULL, NULL, 11) == NULL, "NULL buffer");
+ is_int(ENOSPC, errno, "...with right errno");
- ok(1, test_inet_ntop(AF_UNIX, NULL, NULL, 0) == NULL);
- ok_int(2, EAFNOSUPPORT, errno);
- ok(3, test_inet_ntop(AF_INET, NULL, NULL, 0) == NULL);
- ok_int(4, ENOSPC, errno);
- ok(5, test_inet_ntop(AF_INET, NULL, NULL, 11) == NULL);
- ok_int(6, ENOSPC, errno);
+ test_addr( "0.0.0.0", 0x0);
+ test_addr( "127.0.0.0", 0x7f000000UL);
+ test_addr("255.255.255.255", 0xffffffffUL);
+ test_addr("172.200.232.199", 0xacc8e8c7UL);
+ test_addr( "1.2.3.4", 0x01020304UL);
- n = 7;
- n = test_addr(n, "0.0.0.0", 0x0);
- n = test_addr(n, "127.0.0.0", 0x7f000000UL);
- n = test_addr(n, "255.255.255.255", 0xffffffffUL);
- n = test_addr(n, "172.200.232.199", 0xacc8e8c7UL);
- n = test_addr(n, "1.2.3.4", 0x01020304UL);
-
return 0;
}
Modified: tests/lib/mkstemp-t.c
===================================================================
--- tests/lib/mkstemp-t.c 2011-08-18 14:12:08 UTC (rev 9349)
+++ tests/lib/mkstemp-t.c 2011-08-18 14:19:02 UTC (rev 9350)
@@ -1,6 +1,24 @@
-/* $Id$ */
-/* mkstemp test suite */
+/*
+ * mkstemp test suite.
+ *
+ * $Id$
+ *
+ * The canonical version of this file is maintained in the rra-c-util package,
+ * which can be found at <http://www.eyrie.org/~eagle/software/rra-c-util/>.
+ *
+ * Written by Russ Allbery <rra at stanford.edu>
+ *
+ * The authors hereby relinquish any claim to any copyright that they may have
+ * in this work, whether granted under contract or by operation of law or
+ * international treaty, and hereby commit to the public, at large, that they
+ * shall not, at any time in the future, seek to enforce any copyright in this
+ * work against any person or entity, or prevent any person or entity from
+ * copying, publishing, distributing or creating derivative works of this
+ * work.
+ */
+#define LIBTEST_NEW_FORMAT 1
+
#include "config.h"
#include "clibrary.h"
#include <errno.h>
@@ -22,43 +40,43 @@
struct stat st1, st2;
ssize_t length;
- test_init(20);
+ plan(20);
/* First, test a few error messages. */
errno = 0;
- ok_int(1, -1, test_mkstemp(tooshort));
- ok(2, errno == EINVAL);
- ok_string(3, "XXXXX", tooshort);
+ 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;
- ok_int(4, -1, test_mkstemp(bad1));
- ok(5, errno == EINVAL);
- ok_string(6, "/foo/barXXXXX", bad1);
+ 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;
- ok_int(7, -1, test_mkstemp(bad2));
- ok(8, errno == EINVAL);
- ok_string(9, "/foo/barXXXXXX.out", bad2);
+ 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(10, fd >= 0);
- ok(11, strcmp(template, "tsXXXXXXX") != 0);
- ok(12, strncmp(template, "tsX", 3) == 0);
- ok(13, access(template, F_OK) == 0);
+ 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(14, stat(template, &st1) == 0);
- ok(15, fstat(fd, &st2) == 0);
- ok(16, st1.st_ino == st2.st_ino);
+ 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);
- ok(17, write(fd, template, length) == length);
- ok(18, lseek(fd, 0, SEEK_SET) == 0);
- ok(19, read(fd, buffer, length) == length);
+ 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';
- ok_string(20, template, buffer);
+ is_string(template, buffer, "...and matches what we wrote");
close(fd);
return 0;
Modified: tests/lib/setenv-t.c
===================================================================
--- tests/lib/setenv-t.c 2011-08-18 14:12:08 UTC (rev 9349)
+++ tests/lib/setenv-t.c 2011-08-18 14:19:02 UTC (rev 9350)
@@ -1,6 +1,24 @@
-/* $Id$ */
-/* setenv test suite. */
+/*
+ * setenv test suite.
+ *
+ * $Id$
+ *
+ * The canonical version of this file is maintained in the rra-c-util package,
+ * which can be found at <http://www.eyrie.org/~eagle/software/rra-c-util/>.
+ *
+ * Written by Russ Allbery <rra at stanford.edu>
+ *
+ * The authors hereby relinquish any claim to any copyright that they may have
+ * in this work, whether granted under contract or by operation of law or
+ * international treaty, and hereby commit to the public, at large, that they
+ * shall not, at any time in the future, seek to enforce any copyright in this
+ * work against any person or entity, or prevent any person or entity from
+ * copying, publishing, distributing or creating derivative works of this
+ * work.
+ */
+#define LIBTEST_NEW_FORMAT 1
+
#include "config.h"
#include "clibrary.h"
#include <errno.h>
@@ -15,42 +33,43 @@
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)
{
char *value;
int status;
+ plan(12);
+
if (getenv(test_var))
- die("%s already in the environment!", test_var);
+ bail("%s already in the environment!", test_var);
- test_init(12);
+ 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");
- ok(1, test_setenv(test_var, test_value1, 0) == 0);
- ok_string(2, test_value1, getenv(test_var));
- ok(3, test_setenv(test_var, test_value2, 0) == 0);
- ok_string(4, test_value1, getenv(test_var));
- ok(5, test_setenv(test_var, test_value2, 1) == 0);
- ok_string(6, test_value2, getenv(test_var));
- ok(7, test_setenv(test_var, "", 1) == 0);
- ok_string(8, "", getenv(test_var));
-
/* We're run by a shell script wrapper that sets resource limits such
- that we can allocate one string of this size but not two. Note that
- Linux doesn't support data limits, so skip if we get an unexpected
- success here. */
+ * that we can allocate one string of this size but not two. Note that
+ * Linux doesn't support data limits, so skip if we get an unexpected
+ * success here.
+ */
value = xmalloc(100 * 1024);
memset(value, 'A', 100 * 1024 - 1);
value[100 * 1024 - 1] = 0;
- ok(9, test_setenv(test_var, value, 0) == 0);
- ok_string(10, "", getenv(test_var));
+ ok(test_setenv(test_var, value, 0) == 0, "set string 3");
+ is_string("", getenv(test_var), "...and getenv empty");
status = test_setenv(test_var, value, 1);
if (status == 0) {
- skip_block(11, 2, "no data limit support");
+ skip_block(2, "no data limit support");
} else {
- ok(11, (status == -1) && (errno == ENOMEM));
- ok_string(12, "", getenv(test_var));
+ ok((status == -1) && (errno == ENOMEM), "overwrite string 3");
+ is_string("", getenv(test_var), "...and getenv still empty");
}
-
return 0;
}
Modified: tests/lib/snprintf-t.c
===================================================================
--- tests/lib/snprintf-t.c 2011-08-18 14:12:08 UTC (rev 9349)
+++ tests/lib/snprintf-t.c 2011-08-18 14:19:02 UTC (rev 9350)
@@ -1,11 +1,41 @@
-/* $Id$ */
-/* snprintf test suite. */
+/*
+ * snprintf test suite.
+ *
+ * $Id$
+ *
+ * The canonical version of this file is maintained in the rra-c-util package,
+ * which can be found at <http://www.eyrie.org/~eagle/software/rra-c-util/>.
+ *
+ * Written by Russ Allbery <rra at stanford.edu>
+ * Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006
+ * Russ Allbery <rra at stanford.edu>
+ * Copyright 2009, 2010
+ * The Board of Trustees of the Leland Stanford Junior University
+ * Copyright 1995 Patrick Powell
+ * Copyright 2001 Hrvoje Niksic
+ *
+ * This code is based on code written by Patrick Powell (papowell at astart.com)
+ * It may be used for any purpose as long as this notice remains intact
+ * on all source code distributions
+ */
+#define LIBTEST_NEW_FORMAT 1
+
#include "config.h"
#include "clibrary.h"
#include "libtest.h"
+/*
+ * Disable the requirement that format strings be literals. We need variable
+ * formats for easy testing.
+ */
+//#pragma GCC diagnostic ignored "-Wformat-nonliteral"
+
+/*
+ * 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);
@@ -70,8 +100,9 @@
0
};
+
static void
-test_format(int n, bool truncate, const char *expected, int count,
+test_format(bool trunc, const char *expected, int count,
const char *format, ...)
{
char buf[128];
@@ -79,97 +110,83 @@
va_list args;
va_start(args, format);
- result = test_vsnprintf(buf, truncate ? 32 : sizeof(buf), format, args);
+ result = test_vsnprintf(buf, trunc ? 32 : sizeof(buf), format, args);
va_end(args);
- if (!strcmp(buf, expected) && result == count) {
- ok(n, true);
- } else {
- ok(n, false);
- diag(" format: %s\n", format);
- if (strcmp(buf, expected))
- diag(" saw: %s\n want: %s\n", buf, expected);
- if (result != count)
- diag(" %d != %d\n", result, count);
- }
+ is_string(expected, buf, "format %s, wanted %s", format, expected);
+ is_int(count, result, "...and output length correct");
}
+
int
main(void)
{
- int n, i, count;
+ int i, count;
unsigned int j;
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)));
+ 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);
- 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);
+ 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(6, true, "abcd", 4, "%s", "abcd");
- test_format(7, true, "20", 2, "%d", 20);
- test_format(8, true, "Test ab", 7, "Test %.2s", "abcd");
- test_format(9, true, "a", 1, "%c", 'a');
- test_format(10, true, "", 0, "");
- test_format(11, true, "abcdefghijklmnopqrstuvwxyz01234", 36, "%s",
- string);
- test_format(12, true, "abcdefghij", 10, "%.10s", string);
- test_format(13, true, " abcdefghij", 12, "%12.10s", string);
- test_format(14, true, " abcdefghijklmnopqrstuvwxyz0", 40, "%40s",
- string);
- test_format(15, true, "abcdefghij ", 14, "%-14.10s", string);
- test_format(16, true, " abcdefghijklmnopq", 50, "%50s",
- string);
- test_format(17, true, "%abcd%", 6, "%%%0s%%", "abcd");
- test_format(18, true, "", 0, "%.0s", string);
- test_format(19, true, "abcdefghijklmnopqrstuvwxyz 444", 32, "%.26s %d",
+ 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, true, "abcdefghijklmnopqrstuvwxyz -2.", 32,
- "%.26s %.1f", string, -2.5);
- test_format(21, true, "abcdefghij4444", 14, "%.10s%n%d", string, &count,
- 4444);
- ok(22, count == 10);
- test_format(23, true, "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, true, "(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, false, 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, false, 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, false, 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, false, 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, false, lgbuf, count, ullong_formats[i],
+ test_format(false, lgbuf, count, ullong_formats[i],
ullong_nums[j]);
}
Modified: tests/lib/strlcat-t.c
===================================================================
--- tests/lib/strlcat-t.c 2011-08-18 14:12:08 UTC (rev 9349)
+++ tests/lib/strlcat-t.c 2011-08-18 14:19:02 UTC (rev 9350)
@@ -1,6 +1,24 @@
-/* $Id$ */
-/* strlcat test suite. */
+/*
+ * strlcat test suite.
+ *
+ * $Id$
+ *
+ * The canonical version of this file is maintained in the rra-c-util package,
+ * which can be found at <http://www.eyrie.org/~eagle/software/rra-c-util/>.
+ *
+ * Written by Russ Allbery <rra at stanford.edu>
+ *
+ * The authors hereby relinquish any claim to any copyright that they may have
+ * in this work, whether granted under contract or by operation of law or
+ * international treaty, and hereby commit to the public, at large, that they
+ * shall not, at any time in the future, seek to enforce any copyright in this
+ * work against any person or entity, or prevent any person or entity from
+ * copying, publishing, distributing or creating derivative works of this
+ * work.
+ */
+#define LIBTEST_NEW_FORMAT 1
+
#include "config.h"
#include "clibrary.h"
@@ -8,47 +26,57 @@
size_t test_strlcat(char *, const char *, size_t);
+
int
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;
}
Modified: tests/lib/strlcpy-t.c
===================================================================
--- tests/lib/strlcpy-t.c 2011-08-18 14:12:08 UTC (rev 9349)
+++ tests/lib/strlcpy-t.c 2011-08-18 14:19:02 UTC (rev 9350)
@@ -1,6 +1,24 @@
-/* $Id$ */
-/* strlcpy test suite. */
+/*
+ * strlcpy test suite.
+ *
+ * $Id$
+ *
+ * The canonical version of this file is maintained in the rra-c-util package,
+ * which can be found at <http://www.eyrie.org/~eagle/software/rra-c-util/>.
+ *
+ * Written by Russ Allbery <rra at stanford.edu>
+ *
+ * The authors hereby relinquish any claim to any copyright that they may have
+ * in this work, whether granted under contract or by operation of law or
+ * international treaty, and hereby commit to the public, at large, that they
+ * shall not, at any time in the future, seek to enforce any copyright in this
+ * work against any person or entity, or prevent any person or entity from
+ * copying, publishing, distributing or creating derivative works of this
+ * work.
+ */
+#define LIBTEST_NEW_FORMAT 1
+
#include "config.h"
#include "clibrary.h"
@@ -8,42 +26,49 @@
size_t test_strlcpy(char *, const char *, size_t);
+
int
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;
}
Modified: tests/lib/xwrite-t.c
===================================================================
--- tests/lib/xwrite-t.c 2011-08-18 14:12:08 UTC (rev 9349)
+++ tests/lib/xwrite-t.c 2011-08-18 14:19:02 UTC (rev 9350)
@@ -1,6 +1,36 @@
-/* $Id$ */
-/* Test suite for xwrite and xwritev. */
+/*
+ * Test suite for xwrite, xwritev, and xpwrite.
+ *
+ * $Id$
+ *
+ * The canonical version of this file is maintained in the rra-c-util package,
+ * which can be found at <http://www.eyrie.org/~eagle/software/rra-c-util/>.
+ *
+ * Copyright 2000, 2001, 2002, 2004 Russ Allbery <rra at stanford.edu>
+ * Copyright 2009
+ * The Board of Trustees of the Leland Stanford Junior University
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+#define LIBTEST_NEW_FORMAT 1
+
#include "config.h"
#include "clibrary.h"
#include <sys/uio.h>
@@ -17,40 +47,38 @@
extern int write_interrupt;
extern int write_fail;
+
static void
-test_write(int n, int status, int total)
+test_write(int status, int total, const char *name)
{
- int success;
-
- success = (status == total && memcmp(data, write_buffer, 256) == 0);
- ok(n, success);
- if (!success && status != total)
- diag(" status %d, total %d\n", status, total);
+ is_int(total, status, "%s return status", name);
+ ok(memcmp(data, write_buffer, 256) == 0, "%s output", name);
}
+
int
main(void)
{
int i;
struct iovec iov[4];
- test_init(19);
+ plan(38);
/* Test xwrite. */
for (i = 0; i < 256; i++)
data[i] = i;
- test_write(1, xwrite(0, data, 256), 256);
+ test_write(xwrite(0, data, 256), 256, "xwrite");
write_offset = 0;
write_interrupt = 1;
memset(data, 0, 256);
- test_write(2, xwrite(0, data, 256), 256);
+ test_write(xwrite(0, data, 256), 256, "xwrite interrupted");
write_offset = 0;
for (i = 0; i < 32; i++)
data[i] = i * 2;
- test_write(3, xwrite(0, data, 32), 32);
+ test_write(xwrite(0, data, 32), 32, "xwrite first block");
for (i = 32; i < 65; i++)
data[i] = i * 2;
- test_write(4, xwrite(0, data + 32, 33), 33);
+ test_write(xwrite(0, data + 32, 33), 33, "xwrite second block");
write_offset = 0;
write_interrupt = 0;
@@ -58,7 +86,7 @@
memset(data, 0, 256);
iov[0].iov_base = data;
iov[0].iov_len = 256;
- test_write(5, xwritev(0, iov, 1), 256);
+ test_write(xwritev(0, iov, 1), 256, "xwritev");
write_offset = 0;
for (i = 0; i < 256; i++)
data[i] = i;
@@ -67,18 +95,18 @@
iov[1].iov_len = 16;
iov[2].iov_base = &data[144];
iov[2].iov_len = 112;
- test_write(6, xwritev(0, iov, 3), 256);
+ test_write(xwritev(0, iov, 3), 256, "xwritev with multiple iovs");
write_offset = 0;
write_interrupt = 1;
memset(data, 0, 256);
iov[0].iov_len = 32;
iov[1].iov_base = &data[32];
iov[1].iov_len = 224;
- test_write(7, xwritev(0, iov, 2), 256);
+ test_write(xwritev(0, iov, 2), 256, "xwritev interrupted");
for (i = 0; i < 32; i++)
data[i] = i * 2;
write_offset = 0;
- test_write(8, xwritev(0, iov, 1), 32);
+ test_write(xwritev(0, iov, 1), 32, "xwritev first block");
for (i = 32; i < 65; i++)
data[i] = i * 2;
iov[0].iov_base = &data[32];
@@ -89,39 +117,39 @@
iov[2].iov_len = 8;
iov[3].iov_base = &data[57];
iov[3].iov_len = 8;
- test_write(9, xwritev(0, iov, 4), 33);
+ test_write(xwritev(0, iov, 4), 33, "xwritev second block");
write_offset = 0;
write_interrupt = 0;
/* Test xpwrite. */
for (i = 0; i < 256; i++)
data[i] = i;
- test_write(10, xpwrite(0, data, 256, 0), 256);
+ test_write(xpwrite(0, data, 256, 0), 256, "xpwrite");
write_interrupt = 1;
memset(data + 1, 0, 255);
- test_write(11, xpwrite(0, data + 1, 255, 1), 255);
+ test_write(xpwrite(0, data + 1, 255, 1), 255, "xpwrite interrupted");
for (i = 0; i < 32; i++)
data[i + 32] = i * 2;
- test_write(12, xpwrite(0, data + 32, 32, 32), 32);
+ test_write(xpwrite(0, data + 32, 32, 32), 32, "xpwrite first block");
for (i = 32; i < 65; i++)
data[i + 32] = i * 2;
- test_write(13, xpwrite(0, data + 64, 33, 64), 33);
+ test_write(xpwrite(0, data + 64, 33, 64), 33, "xpwrite second block");
write_interrupt = 0;
/* Test failures. */
write_fail = 1;
- test_write(14, xwrite(0, data + 1, 255), -1);
+ test_write(xwrite(0, data + 1, 255), -1, "xwrite fail");
iov[0].iov_base = data + 1;
iov[0].iov_len = 255;
- test_write(15, xwritev(0, iov, 1), -1);
- test_write(16, xpwrite(0, data + 1, 255, 0), -1);
+ test_write(xwritev(0, iov, 1), -1, "xwritev fail");
+ test_write(xpwrite(0, data + 1, 255, 0), -1, "xpwrite fail");
/* Test zero-length writes. */
- test_write(17, xwrite(0, " ", 0), 0);
- test_write(18, xpwrite(0, " ", 0, 2), 0);
+ test_write(xwrite(0, " ", 0), 0, "xwrite zero length");
+ test_write(xpwrite(0, " ", 0, 2), 0, "xpwrite zero length");
iov[0].iov_base = data + 1;
iov[0].iov_len = 2;
- test_write(19, xwritev(0, iov, 0), 0);
+ test_write(xwritev(0, iov, 0), 0, "xwritev zero length");
return 0;
}
More information about the inn-committers
mailing list