Browse Source

[1880] Don't create core files when EXPECT_DEATH is used

Mukund Sivaraman 13 years ago
parent
commit
d7191f1b7a

+ 14 - 3
src/lib/log/tests/logger_unittest.cc

@@ -14,6 +14,8 @@
 
 #include <iostream>
 #include <string>
+#include <sys/time.h>
+#include <sys/resource.h>
 
 #include <gtest/gtest.h>
 
@@ -370,8 +372,17 @@ TEST_F(LoggerTest, LoggerNameLength) {
     // Note that we just check that it dies - we don't check what message is
     // output.
     EXPECT_DEATH({
-                    string ok3(Logger::MAX_LOGGER_NAME_SIZE + 1, 'x');
-                    Logger l3(ok3.c_str());
-                 }, ".*");
+        /* Set rlimits so that no coredumps are created. As a new
+           process is forked to run this EXPECT_DEATH test, the rlimits
+           of the parent process that runs the other tests should be
+           unaffected. */
+        rlimit core_limit;
+        core_limit.rlim_cur = 0;
+        core_limit.rlim_max = 0;
+        EXPECT_EQ(setrlimit(RLIMIT_CORE, &core_limit), 0);
+
+        string ok3(Logger::MAX_LOGGER_NAME_SIZE + 1, 'x');
+        Logger l3(ok3.c_str());
+    }, ".*");
 #endif
 }

+ 12 - 1
src/lib/log/tests/message_initializer_2_unittest.cc

@@ -12,6 +12,8 @@
 // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 // PERFORMANCE OF THIS SOFTWARE.
 
+#include <sys/time.h>
+#include <sys/resource.h>
 #include <log/message_initializer.h>
 #include <gtest/gtest.h>
 
@@ -42,7 +44,16 @@ TEST(MessageInitializerTest2, MessageLoadTest) {
 #ifdef EXPECT_DEATH
     // Adding one more should take us over the limit.
     EXPECT_DEATH({
+        /* Set rlimits so that no coredumps are created. As a new
+           process is forked to run this EXPECT_DEATH test, the rlimits
+           of the parent process that runs the other tests should be
+           unaffected. */
+        rlimit core_limit;
+        core_limit.rlim_cur = 0;
+        core_limit.rlim_max = 0;
+        EXPECT_EQ(setrlimit(RLIMIT_CORE, &core_limit), 0);
+
         MessageInitializer initializer2(values);
-        }, ".*");
+      }, ".*");
 #endif
 }

+ 38 - 17
src/lib/server_common/tests/portconfig_unittest.cc

@@ -12,6 +12,9 @@
 // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 // PERFORMANCE OF THIS SOFTWARE.
 
+#include <sys/time.h>
+#include <sys/resource.h>
+
 #include <server_common/portconfig.h>
 #include <testutils/socket_request.h>
 #include <testutils/mockups.h>
@@ -313,13 +316,22 @@ TEST_F(InstallListenAddressesDeathTest, inconsistent) {
     // Make sure it actually kills the application (there should be an abort
     // in this case)
     EXPECT_DEATH({
-                    try {
-                        installListenAddresses(deathAddresses, store_, dnss_);
-                    } catch (...) {
-                        // Prevent exceptions killing the application, we need
-                        // to make sure it dies the real hard way
-                    };
-                 }, "");
+        /* Set rlimits so that no coredumps are created. As a new
+           process is forked to run this EXPECT_DEATH test, the rlimits
+           of the parent process that runs the other tests should be
+           unaffected. */
+        rlimit core_limit;
+        core_limit.rlim_cur = 0;
+        core_limit.rlim_max = 0;
+        EXPECT_EQ(setrlimit(RLIMIT_CORE, &core_limit), 0);
+
+        try {
+          installListenAddresses(deathAddresses, store_, dnss_);
+        } catch (...) {
+          // Prevent exceptions killing the application, we need
+          // to make sure it dies the real hard way
+        };
+      }, "");
 }
 
 // If we are unable to tell the boss we closed a socket, we abort, as we are
@@ -330,16 +342,25 @@ TEST_F(InstallListenAddressesDeathTest, cantClose) {
     // Instruct it to fail on close
     sock_requestor_.break_release_ = true;
     EXPECT_DEATH({
-                    try {
-                        // Setting to empty will close all current sockets.
-                        // And thanks to the break_release_, the close will
-                        // throw, which will make it crash.
-                        installListenAddresses(empty, store_, dnss_);
-                    } catch (...) {
-                        // To make sure it is killed by abort, not by some
-                        // (unhandled) exception
-                    };
-                }, "");
+        /* Set rlimits so that no coredumps are created. As a new
+           process is forked to run this EXPECT_DEATH test, the rlimits
+           of the parent process that runs the other tests should be
+           unaffected. */
+        rlimit core_limit;
+        core_limit.rlim_cur = 0;
+        core_limit.rlim_max = 0;
+        EXPECT_EQ(setrlimit(RLIMIT_CORE, &core_limit), 0);
+
+	try {
+	  // Setting to empty will close all current sockets.
+	  // And thanks to the break_release_, the close will
+	  // throw, which will make it crash.
+	  installListenAddresses(empty, store_, dnss_);
+	} catch (...) {
+	  // To make sure it is killed by abort, not by some
+	  // (unhandled) exception
+	};
+      }, "");
     // And reset it back, so it can safely clean up itself.
     sock_requestor_.break_release_ = false;
 }

+ 12 - 0
src/lib/util/tests/buffer_unittest.cc

@@ -12,6 +12,9 @@
 // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 // PERFORMANCE OF THIS SOFTWARE.
 
+#include <sys/time.h>
+#include <sys/resource.h>
+
 #include <exceptions/exceptions.h>
 
 #include <util/buffer.h>
@@ -185,6 +188,15 @@ TEST_F(BufferTest, outputBufferReadat) {
 #ifdef EXPECT_DEATH
     // We use assert now, so we check it dies
     EXPECT_DEATH({
+        /* Set rlimits so that no coredumps are created. As a new
+           process is forked to run this EXPECT_DEATH test, the rlimits
+           of the parent process that runs the other tests should be
+           unaffected. */
+        rlimit core_limit;
+        core_limit.rlim_cur = 0;
+        core_limit.rlim_max = 0;
+        EXPECT_EQ(setrlimit(RLIMIT_CORE, &core_limit), 0);
+
         try {
             obuffer[sizeof(testdata)];
         } catch (...) {