]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[4501] Added unit tests for unused headers/classes/exceptions
authorFrancis Dupont <fdupont@isc.org>
Wed, 1 Mar 2017 22:53:42 +0000 (23:53 +0100)
committerFrancis Dupont <fdupont@isc.org>
Wed, 1 Mar 2017 22:53:42 +0000 (23:53 +0100)
src/lib/cc/tests/data_unittests.cc
src/lib/dhcpsrv/tests/host_mgr_unittest.cc
src/lib/dhcpsrv/tests/memfile_lease_mgr_unittest.cc
src/lib/dns/tests/dns_exceptions_unittest.cc
src/lib/hooks/tests/library_manager_unittest.cc
src/lib/util/tests/process_spawn_unittest.cc

index 1c8054a800cf2bd003d24352982aeb2178222368..bc83927ffeedab548952ac4b78b5a0513ce46478 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2009-2016 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2009-2017 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -100,7 +100,7 @@ TEST(Element, from_and_to_json) {
 
     BOOST_FOREACH(const std::string& s, sv) {
         // Test two types of fromJSON(): with string and istream.
-        for (int i = 0; i < 2; ++i) {
+        for (unsigned i = 0; i < 2; ++i) {
             // test << operator, which uses Element::str()
             if (i == 0) {
                 el = Element::fromJSON(s);
@@ -555,6 +555,12 @@ TEST(Element, escape) {
     EXPECT_NO_THROW(Element::fromJSON("\"\\\"\\\"\""));
     // A whitespace test
     EXPECT_NO_THROW(Element::fromJSON("\"  \n  \r \t \f  \n \n    \t\""));
+    // Escape for forward slash is optional
+    ASSERT_NO_THROW(Element::fromJSON("\"foo\\/bar\""));
+    EXPECT_EQ("foo/bar", Element::fromJSON("\"foo\\/bar\"")->stringValue());
+    // Control characters
+    StringElement bell("foo\abar");
+    EXPECT_EQ("\"foo\\u0007bar\"", bell.str());
 }
 
 // This test verifies that a backslash can be used in element content
@@ -655,6 +661,12 @@ TEST(Element, MapElement) {
     el->set(long_maptag, Element::create("bar"));
     EXPECT_EQ("bar", el->find(long_maptag)->stringValue());
 
+    // Null pointer value
+    el.reset(new MapElement());
+    ConstElementPtr null_ptr;
+    el->set("value", null_ptr);
+    EXPECT_FALSE(el->get("value"));
+    EXPECT_EQ("{ \"value\": None }", el->str());
 }
 
 TEST(Element, to_and_from_wire) {
@@ -857,8 +869,9 @@ TEST(Element, constRemoveIdentical) {
     c = Element::fromJSON("{ \"a\": 1 }");
     EXPECT_EQ(*removeIdentical(a, b), *c);
 
-    EXPECT_THROW(removeIdentical(Element::create(1), Element::create(2)),
-                 TypeError);
+    // removeIdentical() is overloaded so force the first argument to const
+    ConstElementPtr bad = Element::create(1);
+    EXPECT_THROW(removeIdentical(bad, Element::create(2)), TypeError);
 }
 
 TEST(Element, merge) {
@@ -999,6 +1012,10 @@ TEST(Element, preprocessor) {
     EXPECT_THROW(Element::fromJSON(dbl_head_comment), JSONError);
     EXPECT_THROW(Element::fromJSON(dbl_mid_comment), JSONError);
     EXPECT_THROW(Element::fromJSON(dbl_tail_comment), JSONError);
+
+    // For coverage
+    std::istringstream iss(no_comment);
+    EXPECT_TRUE(exp->equals(*Element::fromJSON(iss, true)));
 }
 
 TEST(Element, getPosition) {
index 8e922d355dee07ce55ab79f134224bb54828c2e8..71c432cf70f5d67a99145deb1c703156c73acc69 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2016 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2014-2017 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -388,6 +388,18 @@ TEST_F(HostMgrTest, get6ByPrefix) {
     testGet6ByPrefix(*getCfgHosts(), *getCfgHosts());
 }
 
+// This test verifies that without a host data source an exception is thrown.
+TEST_F(HostMgrTest, addNoDataSource) {
+    // Remove all configuration.
+    CfgMgr::instance().clear();
+    // Recreate HostMgr instance.
+    HostMgr::create();
+
+    HostPtr host(new Host(hwaddrs_[0]->toText(false), "hw-address",
+                          SubnetID(1), SubnetID(0), IOAddress("192.0.2.5")));
+    EXPECT_THROW(HostMgr::instance().add(host), NoHostDataSourceManager);
+}
+
 // The following tests require MySQL enabled.
 #if defined HAVE_MYSQL
 
index f52f2c1bd362ab1c4bb034d895f110c35fb4e171..cb7ba0e36d2182e7e9ca2cef9eaa69ef10c2d147 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2012-2016 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2012-2017 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -375,6 +375,12 @@ TEST_F(MemfileLeaseMgrTest, constructor) {
     EXPECT_THROW(lease_mgr.reset(new Memfile_LeaseMgr(pmap)), isc::BadValue);
 }
 
+// Checks if there is no lease manager NoLeaseManager is thrown.
+TEST_F(MemfileLeaseMgrTest, noLeaseManager) {
+    LeaseMgrFactory::destroy();
+    EXPECT_THROW(LeaseMgrFactory::instance(), NoLeaseManager);
+}
+
 // Checks if the getType() and getName() methods both return "memfile".
 TEST_F(MemfileLeaseMgrTest, getTypeAndName) {
     startBackend(V4);
index bef41628e6bee300add4845f75b7dcfc7b10d0dc..10ea1ef64417d7fcd2bf440089ac6b49a71947b1 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2015 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2014-2015,2017 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -42,6 +42,7 @@ TEST(DNSExceptionsTest, checkExceptionsHierarchy) {
         const isc::dns::Exception& exception_cast2 =
           dynamic_cast<const isc::dns::Exception&>(exception);
         // to avoid compiler warning
+        exception_cast.getRcode();
         exception_cast.what();
         exception_cast2.what();
     });
@@ -53,6 +54,7 @@ TEST(DNSExceptionsTest, checkExceptionsHierarchy) {
         const isc::dns::Exception& exception_cast2 =
           dynamic_cast<const isc::dns::Exception&>(exception);
         // to avoid compiler warning
+        exception_cast.getRcode();
         exception_cast.what();
         exception_cast2.what();
     });
index 367ceb943058d89520f196f8faed89bfebfc5517..1aa446d0858eff84cca575243365ba49a56db217 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2013-2016 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2013-2017 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -128,6 +128,13 @@ public:
 };
 
 
+// Check that LibraryManager constructor requires a not null manager
+
+TEST_F(LibraryManagerTest, NullManager) {
+    EXPECT_THROW(PublicLibraryManager(std::string("foo"), 0, 0),
+                 NoCalloutManager);
+}
+
 // Check that openLibrary() reports an error when it can't find the specified
 // library.
 
index 30533730f4f3e43b4ab06708aa95a6f774aa647e..1dc894d2024677ebce614a48e7f3bdba540aed90 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2015 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2015,2017 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -90,6 +90,21 @@ bool waitForProcessFast(const ProcessSpawn& process, const pid_t pid,
     return (true);
 }
 
+// This test verifies that if the thread calling spawn has SIGCHLD
+// already block ProcessSpawnError is thrown (@todo the second error
+// case: fork() failling)
+TEST(ProcessSpawn, sigchldBlocked) {
+    std::vector<std::string> args;
+    ProcessSpawn process(getApp(), args);
+    sigset_t sset;
+    sigemptyset(&sset);
+    sigaddset(&sset, SIGCHLD);
+    sigset_t osset;
+    pthread_sigmask(SIG_BLOCK, &sset, &osset);
+    EXPECT_THROW(process.spawn(), ProcessSpawnError);
+    sigprocmask(SIG_SETMASK, &osset, 0);
+}
+
 // This test verifies that the external application can be ran with
 // arguments and that the exit code is gathered.
 TEST(ProcessSpawn, spawnWithArgs) {