]> granicus.if.org Git - pdns/commitdiff
testrunner: Add unit test for lua-auth4.cc
authorAki Tuomi <cmouse@cmouse.fi>
Sun, 11 Jun 2017 22:54:18 +0000 (01:54 +0300)
committerAki Tuomi <cmouse@cmouse.fi>
Mon, 18 Dec 2017 10:53:58 +0000 (12:53 +0200)
pdns/Makefile.am
pdns/test-lua_auth4_cc.cc [new file with mode: 0644]

index 8dff3d1f1feac6368af46200888d6ae18358d0a9..13ec929a173df166082d2230e48488a718153061 100644 (file)
@@ -1154,6 +1154,9 @@ testrunner_SOURCES = \
        iputils.cc \
        ixfr.cc ixfr.hh \
        logger.cc \
+       lua-auth4.hh lua-auth4.cc \
+       lua-base4.hh lua-base4.cc \
+       stubresolver.hh stubresolver.cc \
        misc.cc \
        nameserver.cc \
        nsecrecords.cc \
@@ -1179,6 +1182,7 @@ testrunner_SOURCES = \
        test-iputils_hh.cc \
        test-ixfr_cc.cc \
        test-lock_hh.cc \
+       test-lua_auth4_cc.cc \
        test-md5_hh.cc \
        test-misc_hh.cc \
        test-nameserver_cc.cc \
@@ -1205,6 +1209,7 @@ testrunner_LDADD = \
        $(LIBCRYPTO_LIBS) \
        $(BOOST_UNIT_TEST_FRAMEWORK_LIBS) \
        $(RT_LIBS) \
+       $(LUA_LIBS) \
        $(LIBDL)
 
 if PKCS11
diff --git a/pdns/test-lua_auth4_cc.cc b/pdns/test-lua_auth4_cc.cc
new file mode 100644 (file)
index 0000000..3765459
--- /dev/null
@@ -0,0 +1,73 @@
+#define BOOST_TEST_DYN_LINK
+#define BOOST_TEST_NO_MAIN
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+#include <boost/test/unit_test.hpp>
+#include "arguments.hh"
+#include <utility>
+#include "lua-auth4.hh"
+
+struct SetupArgFixture {
+  SetupArgFixture() {
+    ::arg().set("resolver") = "127.0.0.1";
+  };
+};
+
+BOOST_FIXTURE_TEST_SUITE(lua_auth4_cc, SetupArgFixture)
+
+BOOST_AUTO_TEST_CASE(test_prequery) {
+  const std::string script =
+"function prequery(q)\n"
+"  if q.qdomain == newDN(\"mod.unit.test.\")\n"
+"  then\n"
+"    return true\n"
+"  end\n"
+"  return false\n"
+"end";
+  AuthLua4 lua;
+  DNSPacket *p = new DNSPacket(true);
+  p->qdomain = DNSName("mod.unit.test.");
+  lua.loadString(script);
+  DNSPacket *r;
+  try {
+    r = lua.prequery(p);
+    BOOST_CHECK_EQUAL(r->qdomain.toString(), "mod.unit.test.");
+  } catch (const LuaContext::ExecutionErrorException& e) {
+    try {
+     std::rethrow_if_nested(e);
+    } catch(const std::exception& exp) {
+     theL()<<"Extra info: "<<exp.what();
+    }
+  }
+  delete r;
+  delete p;
+}
+
+BOOST_AUTO_TEST_CASE(test_updatePolicy) {
+  const std::string script =
+"function updatepolicy(query)\n"
+"  princ = query:getPeerPrincipal()\n"
+"  if princ == \"admin@DOMAIN\" or tostring(query:getRemote()) == \"192.168.1.1\"\n"
+"  then\n"
+"    return true\n"
+"  end\n"
+"  return false\n"
+"end";
+  AuthLua4 lua;
+  DNSPacket *p = new DNSPacket(true);
+  ComboAddress ca(std::string("192.168.1.1"));
+  lua.loadString(script);
+  p->setRemote(&ca);
+  p->d_peer_principal = "admin@DOMAIN";
+  BOOST_CHECK_EQUAL(lua.updatePolicy(DNSName("mod.example.com."), QType(QType::A), DNSName("example.com."), p), true);
+  p->d_peer_principal = "";
+  BOOST_CHECK_EQUAL(lua.updatePolicy(DNSName("mod.example.com."), QType(QType::A), DNSName("example.com."), p), true);
+  ca = ComboAddress(std::string("192.168.1.2"));
+  p->setRemote(&ca);
+  BOOST_CHECK_EQUAL(lua.updatePolicy(DNSName("mod.example.com."), QType(QType::A), DNSName("example.com."), p), false);
+  delete p;
+}
+
+BOOST_AUTO_TEST_SUITE_END()