Browse Source

[trac978] Test for the defaultActionLoader

Michal 'vorner' Vaner 14 years ago
parent
commit
6fe98e3c2a
2 changed files with 46 additions and 0 deletions
  1. 3 0
      src/lib/acl/Makefile.am
  2. 43 0
      src/lib/acl/tests/loader_test.cc

+ 3 - 0
src/lib/acl/Makefile.am

@@ -9,4 +9,7 @@ lib_LTLIBRARIES = libacl.la
 libacl_la_SOURCES = check.h acl.h
 libacl_la_SOURCES += loader.h loader.cc
 
+libacl_la_LIBADD = $(top_builddir)/src/lib/exceptions/libexceptions.la
+libacl_la_LIBADD += $(top_builddir)/src/lib/cc/libcc.la
+
 CLEANFILES = *.gcno *.gcda

+ 43 - 0
src/lib/acl/tests/loader_test.cc

@@ -14,10 +14,53 @@
 
 #include <acl/loader.h>
 
+#include <string>
+#include <gtest/gtest.h>
+
 using namespace isc::acl;
+using namespace std;
+using isc::data::ConstElementPtr;
 
 namespace {
 
+// Just for convenience, create JSON objects from JSON string
+ConstElementPtr el(const string& JSON) {
+    return (isc::data::Element::fromJSON(JSON));
+}
+
+// We don't use the EXPECT_THROW macro, as it doesn't allow us
+// to examine the exception. We want to check the element is stored
+// there as well.
+void testActionLoaderException(const string& JSON) {
+    SCOPED_TRACE("Should throw with input: " + JSON);
+    ConstElementPtr elem(el(JSON));
+    try {
+        defaultActionLoader(elem);
+        FAIL() << "It did not throw";
+    }
+    catch (const LoaderError& error) {
+        // Yes, comparing for pointer equality, that is enough, it
+        // should return the exact instance of the JSON object
+        EXPECT_EQ(elem, error.element());
+    }
+}
+
+// Test the defaultActionLoader function
+TEST(LoaderHelpers, DefaultActionLoader) {
+    // First the three valid inputs
+    EXPECT_EQ(ACCEPT, defaultActionLoader(el("\"ACCEPT\"")));
+    EXPECT_EQ(REJECT, defaultActionLoader(el("\"REJECT\"")));
+    EXPECT_EQ(DROP, defaultActionLoader(el("\"DROP\"")));
+    // Now few invalid ones
+    // String, but unknown one
+    testActionLoaderException("\"UNKNOWN\"");
+    testActionLoaderException("42");
+    testActionLoaderException("true");
+    testActionLoaderException("null");
+    testActionLoaderException("[]");
+    testActionLoaderException("{}");
+}
+
 Loader<bool> loader;
 
 }