]> granicus.if.org Git - pdns/commitdiff
add rudementary unit tests for our horrible configfile parser, so we don't get it...
authorbert hubert <bert.hubert@netherlabs.nl>
Thu, 7 May 2015 10:12:57 +0000 (12:12 +0200)
committerbert hubert <bert.hubert@netherlabs.nl>
Thu, 7 May 2015 10:13:37 +0000 (12:13 +0200)
pdns/Makefile.am
pdns/test-arguments_cc.cc [new file with mode: 0644]

index e126cb82e2322be2f43ba93c45d6b4d49db5e6c5..e5f7fbd49da6ee00898775347d58fa79e782d214 100644 (file)
@@ -951,6 +951,7 @@ testrunner_SOURCES = \
        responsestats.cc \
        sillyrecords.cc \
        statbag.cc \
+       test-arguments_cc.cc \
        test-base32_cc.cc \
        test-base64_cc.cc \
        test-bindparser_cc.cc \
diff --git a/pdns/test-arguments_cc.cc b/pdns/test-arguments_cc.cc
new file mode 100644 (file)
index 0000000..70f04f3
--- /dev/null
@@ -0,0 +1,61 @@
+#define BOOST_TEST_DYN_LINK
+#define BOOST_TEST_NO_MAIN
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+#include <stdlib.h>
+#include <unistd.h>
+#include <boost/test/unit_test.hpp>
+#include "arguments.hh"
+#include "namespaces.hh" 
+
+BOOST_AUTO_TEST_SUITE(test_arguments_cc)
+
+BOOST_AUTO_TEST_CASE(test_file_parse) {
+  char path[]="/tmp/pdns-test-conf.XXXXXX";
+  int fd=mkstemp(path);
+  if(fd < 0)
+    BOOST_FAIL("Unable to generate a temporary file");
+
+  string config=
+R"(launch=launch=1234
+test=123\
+456
+test2=here # and here it stops
+fail=no
+success=on
+really=yes)";
+
+  int len=write(fd, config.c_str(), config.size());
+
+  BOOST_CHECK_EQUAL(len, config.size());
+  if(!len)
+    return;
+  close(fd);
+  
+  try {
+    ArgvMap arg;
+    for(auto& a : {"launch", "test", "test2", "fail", "success", "really"} )
+      arg.set(a,a);
+    arg.set("default", "default")="no";
+    arg.file(path);
+    unlink(path);
+
+    BOOST_CHECK_EQUAL(arg["launch"], "launch=1234");
+    BOOST_CHECK_EQUAL(arg["test"], "123456");
+    BOOST_CHECK_EQUAL(arg.asNum("test"), 123456);
+    BOOST_CHECK_EQUAL(arg["test2"], "here");
+    BOOST_CHECK_EQUAL(arg.mustDo("fail"), false);
+    BOOST_CHECK_EQUAL(arg.mustDo("success"), true);
+    BOOST_CHECK_EQUAL(arg.mustDo("really"), true);
+    BOOST_CHECK_EQUAL(arg["default"], "no");
+
+  }
+  catch(PDNSException& e) {
+    unlink(path);
+    cerr<<"Exception: "<<e.reason<<endl;
+    BOOST_FAIL("Exception: "+e.reason);
+  }
+};
+
+BOOST_AUTO_TEST_SUITE_END();