summaryrefslogtreecommitdiff
path: root/lib/ccan/read_write_all/test
diff options
context:
space:
mode:
authorRusty Russell <rusty@rustcorp.com.au>2012-02-22 14:59:32 +1030
committerAmitay Isaacs <amitay@gmail.com>2012-03-07 13:16:16 +1100
commit361f3ea9ee577c5a3e2fed687a0b417b257c31de (patch)
tree6d356c3aa64317c609ff4e208be76e18996a55f8 /lib/ccan/read_write_all/test
parent4f5412dda687c3ff76b426842bf284d01d56a997 (diff)
downloadsamba-361f3ea9ee577c5a3e2fed687a0b417b257c31de.tar.gz
samba-361f3ea9ee577c5a3e2fed687a0b417b257c31de.tar.bz2
samba-361f3ea9ee577c5a3e2fed687a0b417b257c31de.zip
lib/ccan: import failtest and required ccan modules for TDB2 unit tests.
New modules: failtest, list, time, read_write_all and tlist. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Diffstat (limited to 'lib/ccan/read_write_all/test')
-rw-r--r--lib/ccan/read_write_all/test/run-read_all.c76
-rw-r--r--lib/ccan/read_write_all/test/run-write_all.c68
2 files changed, 144 insertions, 0 deletions
diff --git a/lib/ccan/read_write_all/test/run-read_all.c b/lib/ccan/read_write_all/test/run-read_all.c
new file mode 100644
index 0000000000..29f81fc703
--- /dev/null
+++ b/lib/ccan/read_write_all/test/run-read_all.c
@@ -0,0 +1,76 @@
+/* FIXME: Do something tricky to ensure we really do loop in read_all. */
+
+#include <ccan/read_write_all/read_write_all.h>
+#include <ccan/read_write_all/read_write_all.c>
+#include <ccan/tap/tap.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <signal.h>
+#include <sys/wait.h>
+#include <limits.h>
+#include <err.h>
+#include <stdlib.h>
+#include <string.h>
+
+static volatile int sigcount;
+static int p2c[2], c2p[2];
+static void got_signal(int sig)
+{
+ char c = 0;
+ if (write(p2c[1], &c, 1) == 1)
+ sigcount++;
+}
+
+/* < PIPE_BUF *will* be atomic. But > PIPE_BUF only *might* be non-atomic. */
+#define BUFSZ (1024*1024)
+
+int main(int argc, char *argv[])
+{
+ char *buffer;
+ char c = 0;
+ int status;
+ pid_t child;
+
+ buffer = calloc(BUFSZ, 2);
+ plan_tests(6);
+
+ /* We fork and torture parent. */
+ if (pipe(p2c) != 0 || pipe(c2p) != 0)
+ err(1, "pipe");
+ child = fork();
+
+ if (!child) {
+ close(p2c[1]);
+ close(c2p[0]);
+ /* Child. Make sure parent ready, then write in two parts. */
+ if (read(p2c[0], &c, 1) != 1)
+ exit(1);
+ memset(buffer, 0xff, BUFSZ*2);
+ if (!write_all(c2p[1], buffer, BUFSZ))
+ exit(2);
+ if (kill(getppid(), SIGUSR1) != 0)
+ exit(3);
+ /* Make sure they get signal. */
+ if (read(p2c[0], &c, 1) != 1)
+ exit(4);
+ if (write(c2p[1], buffer, BUFSZ) != BUFSZ)
+ exit(5);
+ exit(0);
+ }
+ if (child == -1)
+ err(1, "forking");
+
+ close(p2c[0]);
+ close(c2p[1]);
+ signal(SIGUSR1, got_signal);
+ ok1(write(p2c[1], &c, 1) == 1);
+ ok1(read_all(c2p[0], buffer, BUFSZ*2));
+ ok1(memchr(buffer, 0, BUFSZ*2) == NULL);
+ ok1(sigcount == 1);
+ ok1(wait(&status) == child);
+ ok(WIFEXITED(status) && WEXITSTATUS(status) == 0,
+ "WIFEXITED(status) = %u, WEXITSTATUS(status) = %u",
+ WIFEXITED(status), WEXITSTATUS(status));
+ free(buffer);
+ return exit_status();
+}
diff --git a/lib/ccan/read_write_all/test/run-write_all.c b/lib/ccan/read_write_all/test/run-write_all.c
new file mode 100644
index 0000000000..e2baf48dfe
--- /dev/null
+++ b/lib/ccan/read_write_all/test/run-write_all.c
@@ -0,0 +1,68 @@
+#include <ccan/read_write_all/read_write_all.h>
+#include <ccan/tap/tap.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <signal.h>
+#include <limits.h>
+#include <sys/wait.h>
+#include <err.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+
+static ssize_t test_write(int fd, const void *buf, size_t count);
+#define write test_write
+#include <ccan/read_write_all/read_write_all.c>
+#undef write
+
+static ssize_t write_return;
+
+static ssize_t test_write(int fd, const void *buf, size_t count)
+{
+ if (write_return == 0) {
+ errno = ENOSPC;
+ return 0;
+ }
+
+ if (write_return < 0) {
+ errno = -write_return;
+ /* Don't return EINTR more than once! */
+ if (errno == EINTR)
+ write_return = count;
+ return -1;
+ }
+
+ if (write_return < count)
+ return write_return;
+ return count;
+}
+
+#define BUFSZ 1024
+
+int main(int argc, char *argv[])
+{
+ char *buffer;
+
+ buffer = malloc(BUFSZ);
+ plan_tests(8);
+
+ write_return = -ENOSPC;
+ ok1(!write_all(100, buffer, BUFSZ));
+ ok1(errno == ENOSPC);
+
+ write_return = -EINTR;
+ ok1(write_all(100, buffer, BUFSZ));
+ ok1(errno == EINTR);
+
+ write_return = 1;
+ errno = 0;
+ ok1(write_all(100, buffer, BUFSZ));
+ ok1(errno == 0);
+
+ write_return = BUFSZ;
+ ok1(write_all(100, buffer, BUFSZ));
+ ok1(errno == 0);
+ free(buffer);
+
+ return exit_status();
+}