BIND 10 trac826, updated. b0f98e14ae4007a27287e2020fea4f50b91ab83b libutil_unittest

BIND 10 source code commits bind10-changes at lists.isc.org
Tue Jun 26 22:33:50 UTC 2012


The branch, trac826 has been updated
       via  b0f98e14ae4007a27287e2020fea4f50b91ab83b (commit)
      from  08ccf748d3b3a9ef7ef849b27045089a16f42166 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit b0f98e14ae4007a27287e2020fea4f50b91ab83b
Author: Francis Dupont <fdupont at isc.org>
Date:   Wed Jun 27 00:33:40 2012 +0200

    libutil_unittest

-----------------------------------------------------------------------

Summary of changes:
 src/lib/util/unittests/Makefile.am                 |    4 +
 src/lib/util/unittests/mock_socketsession.h        |  154 ++++++++++++++++++++
 .../resource.cc}                                   |   41 +++---
 .../resource.h}                                    |   39 +++--
 4 files changed, 196 insertions(+), 42 deletions(-)
 create mode 100644 src/lib/util/unittests/mock_socketsession.h
 copy src/lib/util/{interprocess_sync_null.cc => unittests/resource.cc} (68%)
 copy src/lib/util/{interprocess_sync_null.cc => unittests/resource.h} (54%)

-----------------------------------------------------------------------
diff --git a/src/lib/util/unittests/Makefile.am b/src/lib/util/unittests/Makefile.am
index bbb0d49..3007a83 100644
--- a/src/lib/util/unittests/Makefile.am
+++ b/src/lib/util/unittests/Makefile.am
@@ -6,10 +6,14 @@ libutil_unittests_la_SOURCES = fork.h fork.cc resolver.h
 libutil_unittests_la_SOURCES += newhook.h newhook.cc
 libutil_unittests_la_SOURCES += testdata.h testdata.cc
 if HAVE_GTEST
+libutil_unittests_la_SOURCES += resource.h resource.cc
 libutil_unittests_la_SOURCES += run_all.h run_all.cc
 libutil_unittests_la_SOURCES += textdata.h
 endif
 
+# For now, this isn't needed for libutil_unittests
+EXTRA_DIST = mock_socketsession.h
+
 libutil_unittests_la_CPPFLAGS = $(AM_CPPFLAGS)
 if HAVE_GTEST
 libutil_unittests_la_CPPFLAGS += $(GTEST_INCLUDES)
diff --git a/src/lib/util/unittests/mock_socketsession.h b/src/lib/util/unittests/mock_socketsession.h
new file mode 100644
index 0000000..8078265
--- /dev/null
+++ b/src/lib/util/unittests/mock_socketsession.h
@@ -0,0 +1,154 @@
+// Copyright (C) 2012  Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#ifndef __UTIL_UNITTESTS_MOCKSOCKETSESSION_H
+#define __UTIL_UNITTESTS_MOCKSOCKETSESSION_H 1
+
+#include <exceptions/exceptions.h>
+
+#include <util/io/socketsession.h>
+#include <util/io/sockaddr_util.h>
+
+#include <cassert>
+#include <cstring>
+#include <vector>
+
+#include <sys/socket.h>
+#include <stdint.h>
+
+namespace isc {
+namespace util {
+namespace unittests {
+
+/// \brief Mock socket session forwarder.
+///
+/// It emulates the behavior of SocketSessionForwarder without involving
+/// network communication, and allowing the tester to customize the behavior
+/// and to examine forwarded data afterwards.
+class MockSocketSessionForwarder :
+    public isc::util::io::BaseSocketSessionForwarder
+{
+public:
+    MockSocketSessionForwarder() :
+        is_connected_(false), connect_ok_(true), push_ok_(true),
+        close_ok_(true)
+    {}
+
+    virtual void connectToReceiver() {
+        if (!connect_ok_) {
+            isc_throw(isc::util::io::SocketSessionError, "socket session "
+                      "forwarding connection disabled for test");
+        }
+        if (is_connected_) {
+            isc_throw(isc::util::io::SocketSessionError, "duplicate connect");
+        }
+        is_connected_ = true;
+    }
+    virtual void close() {
+        if (!is_connected_) {
+            isc_throw(isc::util::io::SocketSessionError, "duplicate close");
+        }
+        is_connected_ = false;
+    }
+
+    // Pushing a socket session.  It copies the given session data
+    // so that the test code can check the values later via the getter
+    // methods.  Complete deep copy will be created, so the caller doesn't
+    // have to keep the parameters valid after the call to this method.
+    virtual void push(int sock, int family, int type, int protocol,
+                      const struct sockaddr& local_end,
+                      const struct sockaddr& remote_end,
+                      const void* data, size_t data_len)
+    {
+        if (!push_ok_) {
+            isc_throw(isc::util::io::SocketSessionError,
+                       "socket session forwarding is disabled for test");
+        }
+        if (!is_connected_) {
+            isc_throw(isc::util::io::SocketSessionError,
+                       "socket session is being pushed before connected");
+        }
+
+        // Copy parameters for later checks
+        pushed_sock_ = sock;
+        pushed_family_ = family;
+        pushed_type_ = type;
+        pushed_protocol_ = protocol;
+        assert(io::internal::getSALength(local_end) <=
+               sizeof(pushed_local_end_ss_));
+        std::memcpy(&pushed_local_end_ss_, &local_end,
+                    io::internal::getSALength(local_end));
+        assert(io::internal::getSALength(remote_end) <=
+               sizeof(pushed_remote_end_ss_));
+        std::memcpy(&pushed_remote_end_ss_, &remote_end,
+                    io::internal::getSALength(remote_end));
+        pushed_data_.resize(data_len);
+        std::memcpy(&pushed_data_[0], data, data_len);
+    }
+
+    // Allow the test code to check if the connection is established.
+    bool isConnected() const { return (is_connected_); }
+
+    // Allow the test code to customize the forwarder behavior wrt whether
+    // a specific operation should succeed or fail.
+    void disableConnect() { connect_ok_ = false; }
+    void enableConnect() { connect_ok_ = true; }
+    void disableClose() { close_ok_ = false; }
+    void disablePush() { push_ok_ = false; }
+    void enablePush() { push_ok_ = true; }
+
+    // Read-only accessors to recorded parameters to the previous successful
+    // call to push().  Return values are undefined if there has been no
+    // successful call to push().
+    // Note that we use convertSockAddr() to convert sockaddr_storage to
+    // sockaddr.  It should be safe since we use the storage in its literal
+    // sense; it was originally filled with the binary image of another
+    // sockaddr structure, and we are going to return the image opaquely
+    // as a sockaddr structure without touching the data.
+    int getPushedSock() const { return (pushed_sock_); }
+    int getPushedFamily() const { return (pushed_family_); }
+    int getPushedType() const { return (pushed_type_); }
+    int getPushedProtocol() const { return (pushed_protocol_); }
+    const struct sockaddr& getPushedLocalend() const {
+        return (*io::internal::convertSockAddr(&pushed_local_end_ss_));
+    }
+    const struct sockaddr& getPushedRemoteend() const {
+        return (*io::internal::convertSockAddr(&pushed_remote_end_ss_));
+    }
+    const std::vector<uint8_t>& getPushedData() const {
+        return (pushed_data_);
+    }
+
+private:
+    bool is_connected_;
+    bool connect_ok_;
+    bool push_ok_;
+    bool close_ok_;
+    int pushed_sock_;
+    int pushed_family_;
+    int pushed_type_;
+    int pushed_protocol_;
+    struct sockaddr_storage pushed_local_end_ss_;
+    struct sockaddr_storage pushed_remote_end_ss_;
+    std::vector<uint8_t> pushed_data_;
+};
+
+} // end of unittests
+} // end of util
+} // end of isc
+#endif  // __UTIL_UNITTESTS_MOCKSOCKETSESSION_H
+
+// Local Variables:
+// mode: c++
+// End:
diff --git a/src/lib/util/unittests/resource.cc b/src/lib/util/unittests/resource.cc
new file mode 100644
index 0000000..f5108fa
--- /dev/null
+++ b/src/lib/util/unittests/resource.cc
@@ -0,0 +1,41 @@
+// Copyright (C) 2012  Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#include <config.h>
+
+#include "resource.h"
+
+#include <gtest/gtest.h>
+
+#ifndef _WIN32
+#include <sys/time.h>
+#include <sys/resource.h>
+#endif
+
+namespace isc {
+namespace util {
+namespace unittests {
+
+void
+dontCreateCoreDumps() {
+#ifndef_WIN32
+    const rlimit core_limit = {0, 0};
+
+    EXPECT_EQ(setrlimit(RLIMIT_CORE, &core_limit), 0);
+#endif
+}
+
+} // end of namespace unittests
+} // end of namespace util
+} // end of namespace isc
diff --git a/src/lib/util/unittests/resource.h b/src/lib/util/unittests/resource.h
new file mode 100644
index 0000000..6430ab2
--- /dev/null
+++ b/src/lib/util/unittests/resource.h
@@ -0,0 +1,39 @@
+// Copyright (C) 2012  Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#ifndef __UTIL_UNITTESTS_RESOURCE_H
+#define __UTIL_UNITTESTS_RESOURCE_H 1
+
+namespace isc {
+namespace util {
+namespace unittests {
+
+/// Don't create core dumps.
+///
+/// This function sets the core size to 0, inhibiting the creation of
+/// core dumps. It is meant to be used in testcases where EXPECT_DEATH
+/// is used, where processes abort (and create cores in the process).
+/// As a new process is forked to run EXPECT_DEATH tests, the rlimits of
+/// the parent process that runs the other tests should be unaffected.
+void dontCreateCoreDumps();
+
+} // end of namespace unittests
+} // end of namespace util
+} // end of namespace isc
+
+#endif /* __UTIL_UNITTESTS_RESOURCE_H */
+
+// Local Variables:
+// mode: c++
+// End:



More information about the bind10-changes mailing list