[svn] commit: r3163 - in /branches/vorner-sockcreator: ./ src/bin/sockcreator/ src/bin/sockcreator/tests/ src/lib/ src/lib/util/ src/lib/util/io/ src/lib/util/unittests/
BIND 10 source code commits
bind10-changes at lists.isc.org
Sun Oct 10 11:31:33 UTC 2010
Author: vorner
Date: Sun Oct 10 11:31:32 2010
New Revision: 3163
Log:
Cleanups and library splitting
* Some of the code might be useful in other places, so it is split into
libraries (isc::util::io and isc::util::unittests).
* Fixing flags in the sockcreator, leading to new warnings, fixing these
warnings.
* TODO: read_data and write_data were tested implicitly with run(), but
as they moved out to other library, they should have their own
testcase.
Added:
branches/vorner-sockcreator/src/lib/util/
branches/vorner-sockcreator/src/lib/util/Makefile.am
branches/vorner-sockcreator/src/lib/util/io/
branches/vorner-sockcreator/src/lib/util/io/Makefile.am
branches/vorner-sockcreator/src/lib/util/io/fd.cc
branches/vorner-sockcreator/src/lib/util/io/fd.h
branches/vorner-sockcreator/src/lib/util/unittests/
branches/vorner-sockcreator/src/lib/util/unittests/Makefile.am
branches/vorner-sockcreator/src/lib/util/unittests/README
branches/vorner-sockcreator/src/lib/util/unittests/fork.cc
branches/vorner-sockcreator/src/lib/util/unittests/fork.h
Modified:
branches/vorner-sockcreator/configure.ac
branches/vorner-sockcreator/src/bin/sockcreator/Makefile.am
branches/vorner-sockcreator/src/bin/sockcreator/sockcreator.cc
branches/vorner-sockcreator/src/bin/sockcreator/sockcreator.h
branches/vorner-sockcreator/src/bin/sockcreator/tests/Makefile.am
branches/vorner-sockcreator/src/bin/sockcreator/tests/sockcreator_tests.cc
branches/vorner-sockcreator/src/lib/Makefile.am
Modified: branches/vorner-sockcreator/configure.ac
==============================================================================
--- branches/vorner-sockcreator/configure.ac (original)
+++ branches/vorner-sockcreator/configure.ac Sun Oct 10 11:31:32 2010
@@ -508,6 +508,9 @@
src/lib/datasrc/Makefile
src/lib/datasrc/tests/Makefile
src/lib/xfr/Makefile
+ src/lib/util/Makefile
+ src/lib/util/io/Makefile
+ src/lib/util/unittests/Makefile
])
AC_OUTPUT([src/bin/cfgmgr/b10-cfgmgr.py
src/bin/cfgmgr/tests/b10-cfgmgr_test.py
Modified: branches/vorner-sockcreator/src/bin/sockcreator/Makefile.am
==============================================================================
--- branches/vorner-sockcreator/src/bin/sockcreator/Makefile.am (original)
+++ branches/vorner-sockcreator/src/bin/sockcreator/Makefile.am Sun Oct 10 11:31:32 2010
@@ -1,7 +1,18 @@
SUBDIRS = tests
+
+AM_CPPFLAGS = -I$(top_srcdir)/src/lib -I$(top_builddir)/src/lib
+
+AM_CXXFLAGS = $(B10_CXXFLAGS)
+
+if USE_STATIC_LINK
+AM_LDFLAGS = -static
+endif
+
+pkglibexecdir = $(libexecdir)/@PACKAGE@
CLEANFILES = *.gcno *.gcda
pkglibexec_PROGRAMS = b10-sockcreator
b10_sockcreator_SOURCES = sockcreator.cc sockcreator.h main.cc
+b10_sockcreator_LDADD = $(top_builddir)/src/lib/util/io/libutil_io.la
Modified: branches/vorner-sockcreator/src/bin/sockcreator/sockcreator.cc
==============================================================================
--- branches/vorner-sockcreator/src/bin/sockcreator/sockcreator.cc (original)
+++ branches/vorner-sockcreator/src/bin/sockcreator/sockcreator.cc Sun Oct 10 11:31:32 2010
@@ -14,12 +14,16 @@
#include "sockcreator.h"
+#include <util/io/fd.h>
+
#include <unistd.h>
#include <cerrno>
#include <cstring>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
+
+using namespace isc::util::io;
namespace isc {
namespace socket_creator {
@@ -38,8 +42,8 @@
}
int
-send_fd(const int destination, const int payload) {
- // TODO Steal
+send_fd(const int, const int) {
+ return 0;
}
int
@@ -54,7 +58,6 @@
} \
} while (0)
#define WRITE(WHAT, HOW_MANY) do { \
- size_t how_many = (HOW_MANY); \
if (!write_data(output_fd, (WHAT), (HOW_MANY))) { \
return 2; \
} \
@@ -121,6 +124,7 @@
int result(get_sock(sock_type, addr, addr_len));
if (result >= 0) { // We got the socket
WRITE("S", 1);
+ // FIXME: Check the output and write a test for it
send_fd(output_fd, result);
} else {
WRITE("E", 1);
@@ -145,47 +149,5 @@
}
}
-bool
-write_data(const int fd, const char *buffer, const size_t length) {
- size_t rest(length);
- // Just keep writing until all is written
- while (rest) {
- ssize_t written(write(fd, buffer, rest));
- if (rest == -1) {
- if (errno == EINTR) { // Just keep going
- continue;
- } else {
- return false;
- }
- } else { // Wrote something
- rest -= written;
- buffer += written;
- }
- }
- return true;
-}
-
-ssize_t
-read_data(const int fd, char *buffer, const size_t length) {
- size_t rest(length), already(0);
- while (rest) { // Stil something to read
- ssize_t amount(read(fd, buffer, rest));
- if (rest == -1) {
- if (errno == EINTR) { // Continue on interrupted call
- continue;
- } else {
- return -1;
- }
- } else if (amount) {
- already += amount;
- rest -= amount;
- buffer += amount;
- } else { // EOF
- return already;
- }
- }
- return already;
-}
-
} // End of the namespaces
}
Modified: branches/vorner-sockcreator/src/bin/sockcreator/sockcreator.h
==============================================================================
--- branches/vorner-sockcreator/src/bin/sockcreator/sockcreator.h (original)
+++ branches/vorner-sockcreator/src/bin/sockcreator/sockcreator.h Sun Oct 10 11:31:32 2010
@@ -102,38 +102,6 @@
const get_sock_t get_sock_fun = get_sock,
const send_fd_t send_fd_fun = send_fd);
-/*
- * \short write() that writes everything.
- * Wrapper around write(). The difference is, it never writes less data
- * and looks successfull (eg. it blocks until all data are written).
- * Retries on signals.
- *
- * \return True if sucessfull, false otherwise. The errno variable is left
- * intact.
- * \param fd Where to write.
- * \param data The buffer to write.
- * \param length How much data is there to write.
- *
- * TODO: Shouldn't this be in some kind of library?
- */
-bool
-write_data(const int fd, const char *data, const size_t length);
-
-/*
- * \short read() that reads everything.
- * Wrapper around read(). It does not do short reads, if it returns less,
- * it means there was EOF. It retries on signals.
- *
- * \return Number of bytes read or -1 on error.
- * \param fd Where to read data from.
- * \param data Where to put the data.
- * \param length How many of them.
- *
- * TODO: Shouldn't this be in some kind of library?
- */
-ssize_t
-read_data(const int fd, char *buffer, const size_t length);
-
} // End of the namespaces
}
Modified: branches/vorner-sockcreator/src/bin/sockcreator/tests/Makefile.am
==============================================================================
--- branches/vorner-sockcreator/src/bin/sockcreator/tests/Makefile.am (original)
+++ branches/vorner-sockcreator/src/bin/sockcreator/tests/Makefile.am Sun Oct 10 11:31:32 2010
@@ -1,4 +1,16 @@
CLEANFILES = *.gcno *.gcda
+
+AM_CPPFLAGS = -I$(top_srcdir)/src/lib -I$(top_builddir)/src/lib
+# This is here because of juggling with sockaddr pointer.
+# It is somehow required by the BSD socket interface, but it
+# breaks C++ strict aliasing rules, so we need to ask the compiler
+# not to use them.
+AM_CPPFLAGS += -fno-strict-aliasing
+AM_CXXFLAGS = $(B10_CXXFLAGS)
+
+if USE_STATIC_LINK
+AM_LDFLAGS = -static
+endif
TESTS =
if HAVE_GTEST
@@ -10,6 +22,9 @@
run_unittests_CPPFLAGS = $(AM_CPPFLAGS) $(GTEST_INCLUDES)
run_unittests_LDFLAGS = $(AM_LDFLAGS) $(GTEST_LDFLAGS)
run_unittests_LDADD = $(GTEST_LDADD)
+run_unittests_LDADD += $(top_builddir)/src/lib/util/io/libutil_io.la
+run_unittests_LDADD += \
+ $(top_builddir)/src/lib/util/unittests/libutil_unittests.la
endif
noinst_PROGRAMS = $(TESTS)
Modified: branches/vorner-sockcreator/src/bin/sockcreator/tests/sockcreator_tests.cc
==============================================================================
--- branches/vorner-sockcreator/src/bin/sockcreator/tests/sockcreator_tests.cc (original)
+++ branches/vorner-sockcreator/src/bin/sockcreator/tests/sockcreator_tests.cc Sun Oct 10 11:31:32 2010
@@ -14,19 +14,20 @@
#include "../sockcreator.h"
+#include <util/unittests/fork.h>
+#include <util/io/fd.h>
+
#include <gtest/gtest.h>
#include <sys/types.h>
#include <sys/socket.h>
-#include <sys/wait.h>
-#include <signal.h>
#include <netinet/in.h>
#include <unistd.h>
#include <cstring>
-#include <cstdlib>
#include <cerrno>
-#include <cstdio>
using namespace isc::socket_creator;
+using namespace isc::util::unittests;
+using namespace isc::util::io;
namespace {
@@ -116,102 +117,13 @@
}
/*
- * This creates a pipe, forks and feeds the pipe with given data.
- * Used to provide the input in non-blocking/asynchronous way.
- */
-pid_t
-provide_input(int *read_pipe, const char *input, const size_t length) {
- int pipes[2];
- if (pipe(pipes)) {
- return -1;
- }
- *read_pipe = pipes[0];
- pid_t pid(fork());
- if (pid) { // We are in the parent
- return pid;
- } else { // This is in the child, just puth the data there
- close(pipes[0]);
- if (!write_data(pipes[1], input, length)) {
- exit(1);
- } else {
- close(pipes[1]);
- exit(0);
- }
- }
-}
-
-/*
- * This creates a pipe, forks and reads the pipe and compares it
- * with given data. Used to check output of run in asynchronous way.
- */
-pid_t
-check_output(int *write_pipe, const char *output, const size_t length) {
- int pipes[2];
- if (pipe(pipes)) {
- return -1;
- }
- *write_pipe = pipes[1];
- pid_t pid(fork());
- if (pid) { // We are in parent
- return pid;
- } else {
- close(pipes[1]);
- char buffer[length + 1];
- // Try to read one byte more to see if the output ends here
- size_t got_length(read_data(pipes[0], buffer, length + 1));
- bool ok(true);
- if (got_length != length) {
- fprintf(stderr, "Different length (expected %u, got %u)\n",
- static_cast<unsigned>(length),
- static_cast<unsigned>(got_length));
- ok = false;
- }
- if(!ok || memcmp(buffer, output, length)) {
- // If the differ, print what we have
- for(size_t i(0); i != got_length; ++ i) {
- fprintf(stderr, "%02hhx", buffer[i]);
- }
- fprintf(stderr, "\n");
- for(size_t i(0); i != length; ++ i) {
- fprintf(stderr, "%02hhx", output[i]);
- }
- fprintf(stderr, "\n");
- exit(1);
- } else {
- exit(0);
- }
- }
-}
-
-/*
- * Waits for pid to terminate and checks it terminates successfully (with 0).
- */
-bool
-process_ok(pid_t process) {
- int status;
- // Make sure it does terminate when the output is shorter than expected
- /*
- * FIXME: if the timeout is reached, this kill the whole test, not just
- * the waitpid call. Should have signal handler. This is no show-stopper,
- * but we might have another tests to run.
- */
- alarm(3);
- if (waitpid(process, &status, 0) == -1) {
- if (errno == EINTR)
- kill(process, SIGTERM);
- return false;
- }
- return WIFEXITED(status) && WEXITSTATUS(status) == 0;
-}
-
-/*
* Helper functions to pass to run during testing.
*/
int
-get_sock_dummy(const int type, struct sockaddr *addr, const socklen_t addr_len)
+get_sock_dummy(const int type, struct sockaddr *addr, const socklen_t)
{
int result(0);
- int port;
+ int port(0);
/*
* We encode the type and address family into the int and return it.
* Lets ignore the port and address for now
@@ -265,8 +177,11 @@
* the test anyway.
*/
char fd_data(what);
- if (!write_data(destination, &fd_data, 1))
+ if (!write_data(destination, &fd_data, 1)) {
return -1;
+ } else {
+ return 0;
+ }
}
/*
@@ -280,7 +195,7 @@
bool should_succeed = true)
{
// Prepare the input feeder and output checker processes
- int input_fd, output_fd;
+ int input_fd(0), output_fd(0);
pid_t input(provide_input(&input_fd, input_data, input_size)),
output(check_output(&output_fd, output_data, output_size));
ASSERT_NE(-1, input) << "Couldn't start input feeder";
Modified: branches/vorner-sockcreator/src/lib/Makefile.am
==============================================================================
--- branches/vorner-sockcreator/src/lib/Makefile.am (original)
+++ branches/vorner-sockcreator/src/lib/Makefile.am Sun Oct 10 11:31:32 2010
@@ -1,1 +1,1 @@
-SUBDIRS = exceptions dns cc config datasrc python xfr bench
+SUBDIRS = exceptions dns cc config datasrc python xfr bench util
More information about the bind10-changes
mailing list