]> granicus.if.org Git - pdns/commitdiff
Test suite for POST and POST JSON modes
authorAki Tuomi <cmouse@desteem.org>
Wed, 5 Jun 2013 07:15:21 +0000 (10:15 +0300)
committerAki Tuomi <cmouse@desteem.org>
Wed, 5 Jun 2013 07:15:21 +0000 (10:15 +0300)
modules/remotebackend/Makefile.am
modules/remotebackend/test-remotebackend-json.cc [new file with mode: 0644]
modules/remotebackend/test-remotebackend-post.cc [new file with mode: 0644]
modules/remotebackend/test_remotebackend_json [new file with mode: 0755]
modules/remotebackend/test_remotebackend_post [new file with mode: 0755]
modules/remotebackend/testrunner.sh
modules/remotebackend/unittest_json.rb [new file with mode: 0755]
modules/remotebackend/unittest_post.rb [new file with mode: 0755]

index 6ee7318a4fe360d86f534ef4acdccbf7684190a7..c4bf57930f7548dbc6238195f1534f65a694800f 100644 (file)
@@ -5,7 +5,7 @@ AM_CPPFLAGS=@THREADFLAGS@ $(BOOST_CPPFLAGS) $(LIBCURL_CFLAGS) -I../../pdns/ext/r
 #endif
 
 EXTRA_DIST=OBJECTFILES OBJECTLIBS
-EXTRA_PROGRAMS=test_remotebackend_pipe test_remotebackend_http
+EXTRA_PROGRAMS=test_remotebackend_pipe test_remotebackend_http test_remotebackend_post test_remotebackend_json
 EXTRA_LTLIBRARIES=libtestremotebackend.la
 
 lib_LTLIBRARIES = libremotebackend.la 
@@ -16,7 +16,7 @@ libremotebackend_la_LDFLAGS=-module -avoid-version
 libremotebackend_la_LIBS=$(LIBCURL_LIBS)
 
 TESTS_ENVIRONMENT = env BOOST_TEST_LOG_LEVEL=message REMOTEBACKEND_HTTP=$(REMOTEBACKEND_HTTP) ./testrunner.sh 
-TESTS=test_remotebackend_pipe test_remotebackend_http
+TESTS=test_remotebackend_pipe test_remotebackend_http test_remotebackend_post test_remotebackend_json
 
 BUILT_SOURCES=../../pdns/dnslabeltext.cc
 
@@ -38,8 +38,9 @@ libtestremotebackend_la_CFLAGS=$(BOOST_CPPFLAGS) @THREADFLAGS@ $(LIBCURL_CFLAGS)
 libtestremotebackend_la_CXXFLAGS=$(BOOST_CPPFLAGS) @THREADFLAGS@ $(LIBCURL_CFLAGS) -g -O0 -I../../pdns
 
 test_remotebackend_pipe_SOURCES=test-remotebackend.cc test-remotebackend-pipe.cc 
-
 test_remotebackend_http_SOURCES=test-remotebackend.cc test-remotebackend-http.cc ../../config.h
+test_remotebackend_post_SOURCES=test-remotebackend.cc test-remotebackend-post.cc ../../config.h
+test_remotebackend_json_SOURCES=test-remotebackend.cc test-remotebackend-json.cc ../../config.h
 
 test_remotebackend_pipe_CFLAGS=$(BOOST_CPPFLAGS) @THREADFLAGS@ $(LIBCURL_CFLAGS) -g -O0 -I../../pdns
 test_remotebackend_pipe_CXXFLAGS=$(BOOST_CPPFLAGS) @THREADFLAGS@ $(LIBCURL_CFLAGS) -g -O0 -I../../pdns
@@ -48,3 +49,11 @@ test_remotebackend_pipe_LDADD=libtestremotebackend.la @DYNLINKFLAGS@ @THREADFLAG
 test_remotebackend_http_CFLAGS=$(BOOST_CPPFLAGS) @THREADFLAGS@ $(LIBCURL_CFLAGS) -g -O0 -I../../pdns
 test_remotebackend_http_CXXFLAGS=$(BOOST_CPPFLAGS) @THREADFLAGS@ $(LIBCURL_CFLAGS) -g -O0 -I../../pdns
 test_remotebackend_http_LDADD=libtestremotebackend.la @DYNLINKFLAGS@ @THREADFLAGS@ $(BOOST_UNIT_TEST_FRAMEWORK_LDFLAGS) ../../pdns/ext/polarssl-1.1.2/library/libpolarssl.a $(BOOST_UNIT_TEST_FRAMEWORK_LIBS) $(BOOST_SERIALIZATION_LIBS) $(BOOST_PROGRAM_OPTIONS_LIBS) @LIBDL@ $(LIBCURL_LIBS)
+
+test_remotebackend_post_CFLAGS=$(BOOST_CPPFLAGS) @THREADFLAGS@ $(LIBCURL_CFLAGS) -g -O0 -I../../pdns
+test_remotebackend_post_CXXFLAGS=$(BOOST_CPPFLAGS) @THREADFLAGS@ $(LIBCURL_CFLAGS) -g -O0 -I../../pdns
+test_remotebackend_post_LDADD=libtestremotebackend.la @DYNLINKFLAGS@ @THREADFLAGS@ $(BOOST_UNIT_TEST_FRAMEWORK_LDFLAGS) ../../pdns/ext/polarssl-1.1.2/library/libpolarssl.a $(BOOST_UNIT_TEST_FRAMEWORK_LIBS) $(BOOST_SERIALIZATION_LIBS) $(BOOST_PROGRAM_OPTIONS_LIBS) @LIBDL@ $(LIBCURL_LIBS)
+
+test_remotebackend_json_CFLAGS=$(BOOST_CPPFLAGS) @THREADFLAGS@ $(LIBCURL_CFLAGS) -g -O0 -I../../pdns
+test_remotebackend_json_CXXFLAGS=$(BOOST_CPPFLAGS) @THREADFLAGS@ $(LIBCURL_CFLAGS) -g -O0 -I../../pdns
+test_remotebackend_json_LDADD=libtestremotebackend.la @DYNLINKFLAGS@ @THREADFLAGS@ $(BOOST_UNIT_TEST_FRAMEWORK_LDFLAGS) ../../pdns/ext/polarssl-1.1.2/library/libpolarssl.a $(BOOST_UNIT_TEST_FRAMEWORK_LIBS) $(BOOST_SERIALIZATION_LIBS) $(BOOST_PROGRAM_OPTIONS_LIBS) @LIBDL@ $(LIBCURL_LIBS)
diff --git a/modules/remotebackend/test-remotebackend-json.cc b/modules/remotebackend/test-remotebackend-json.cc
new file mode 100644 (file)
index 0000000..1522d52
--- /dev/null
@@ -0,0 +1,73 @@
+#include "pdns/namespaces.hh"
+#include <pdns/dns.hh>
+#include <pdns/dnsbackend.hh>
+#include <pdns/dnspacket.hh>
+#include <pdns/ueberbackend.hh>
+#include <pdns/ahuexception.hh>
+#include <pdns/logger.hh>
+#include <pdns/arguments.hh>
+#include <boost/lexical_cast.hpp>
+#include <rapidjson/rapidjson.h>
+#include <rapidjson/document.h>
+#include "pdns/json.hh"
+#include "pdns/statbag.hh"
+#include "pdns/packetcache.hh"
+
+StatBag S;
+PacketCache PC;
+ArgvMap &arg()
+{
+  static ArgvMap arg;
+  return arg;
+};
+
+class RemoteLoader
+{
+   public:
+      RemoteLoader();
+};
+
+DNSBackend *be;
+
+#ifdef REMOTEBACKEND_HTTP
+
+#define BOOST_TEST_DYN_LINK
+#define BOOST_TEST_MAIN
+#define BOOST_TEST_MODULE unit
+
+#include <boost/test/unit_test.hpp>
+#include <boost/assign/list_of.hpp>
+#include <boost/foreach.hpp>
+#include <boost/tuple/tuple.hpp>
+
+struct RemotebackendSetup {
+    RemotebackendSetup()  {
+       be = 0; 
+       try {
+               // setup minimum arguments
+               ::arg().set("module-dir")="";
+                new RemoteLoader();
+               BackendMakers().launch("remote");
+                // then get us a instance of it 
+                ::arg().set("remote-connection-string")="http:url=http://localhost:62434/dns/endpoint.json,post=1,post_json=1";
+                ::arg().set("remote-dnssec")="yes";
+                be = BackendMakers().all()[0];
+       } catch (AhuException &ex) {
+               BOOST_TEST_MESSAGE("Cannot start remotebackend: " << ex.reason );
+       };
+    }
+    ~RemotebackendSetup()  {  }
+};
+
+BOOST_GLOBAL_FIXTURE( RemotebackendSetup );
+
+#else
+
+#include <iostream>
+
+int main(void) {
+  std::cout << "No HTTP support in remotebackend - skipping test" << std::endl;
+  return 0;
+}
+
+#endif
diff --git a/modules/remotebackend/test-remotebackend-post.cc b/modules/remotebackend/test-remotebackend-post.cc
new file mode 100644 (file)
index 0000000..754e7a9
--- /dev/null
@@ -0,0 +1,73 @@
+#include "pdns/namespaces.hh"
+#include <pdns/dns.hh>
+#include <pdns/dnsbackend.hh>
+#include <pdns/dnspacket.hh>
+#include <pdns/ueberbackend.hh>
+#include <pdns/ahuexception.hh>
+#include <pdns/logger.hh>
+#include <pdns/arguments.hh>
+#include <boost/lexical_cast.hpp>
+#include <rapidjson/rapidjson.h>
+#include <rapidjson/document.h>
+#include "pdns/json.hh"
+#include "pdns/statbag.hh"
+#include "pdns/packetcache.hh"
+
+StatBag S;
+PacketCache PC;
+ArgvMap &arg()
+{
+  static ArgvMap arg;
+  return arg;
+};
+
+class RemoteLoader
+{
+   public:
+      RemoteLoader();
+};
+
+DNSBackend *be;
+
+#ifdef REMOTEBACKEND_HTTP
+
+#define BOOST_TEST_DYN_LINK
+#define BOOST_TEST_MAIN
+#define BOOST_TEST_MODULE unit
+
+#include <boost/test/unit_test.hpp>
+#include <boost/assign/list_of.hpp>
+#include <boost/foreach.hpp>
+#include <boost/tuple/tuple.hpp>
+
+struct RemotebackendSetup {
+    RemotebackendSetup()  {
+       be = 0; 
+       try {
+               // setup minimum arguments
+               ::arg().set("module-dir")="";
+                new RemoteLoader();
+               BackendMakers().launch("remote");
+                // then get us a instance of it 
+                ::arg().set("remote-connection-string")="http:url=http://localhost:62434/dns,post=1";
+                ::arg().set("remote-dnssec")="yes";
+                be = BackendMakers().all()[0];
+       } catch (AhuException &ex) {
+               BOOST_TEST_MESSAGE("Cannot start remotebackend: " << ex.reason );
+       };
+    }
+    ~RemotebackendSetup()  {  }
+};
+
+BOOST_GLOBAL_FIXTURE( RemotebackendSetup );
+
+#else
+
+#include <iostream>
+
+int main(void) {
+  std::cout << "No HTTP support in remotebackend - skipping test" << std::endl;
+  return 0;
+}
+
+#endif
diff --git a/modules/remotebackend/test_remotebackend_json b/modules/remotebackend/test_remotebackend_json
new file mode 100755 (executable)
index 0000000..8433f6a
Binary files /dev/null and b/modules/remotebackend/test_remotebackend_json differ
diff --git a/modules/remotebackend/test_remotebackend_post b/modules/remotebackend/test_remotebackend_post
new file mode 100755 (executable)
index 0000000..83e3a05
Binary files /dev/null and b/modules/remotebackend/test_remotebackend_post differ
index 2828ad061b1d9ed8f54b4e12b8b51f381d151424..7f687a5cafa99cb9a27e56faab66c8f1457ddfff 100755 (executable)
@@ -2,20 +2,18 @@
 
 webrick_pid=""
 
-if [ x"$REMOTEBACKEND_HTTP" == "xyes" ]; then 
-
-if [ `basename "$1"` == "test_remotebackend_http" ]; then 
- ./unittest_http.rb &  
- webrick_pid=$!
- sleep 1
-fi
-
-$1
-rv=$?
-
-if [ ! -z "$webrick_pid" ]; then 
+function start_web() {
+  if [ x"$REMOTEBACKEND_HTTP" == "xyes" ]; then
+   ./unittest_$1.rb &
+   webrick_pid=$!
+   sleep 1
+  fi
+}
+
+function stop_web() {
+ if [ ! -z "$webrick_pid" ]; then
    kill -TERM $webrick_pid
-   # wait a moment for it to die 
+   # wait a moment for it to die
    i=0
    while [ $i -lt 5 ]; do
      sleep 1
@@ -23,13 +21,37 @@ if [ ! -z "$webrick_pid" ]; then
      if [ $? -ne 0 ]; then break; fi
      let i=i+1
    done
-fi
-
-else
-
-$1
-rv=$?
-
-fi
+ fi
+}
+
+mode=`basename "$1"`
+
+case "$mode" in
+  test_remotebackend_pipe)
+    ./test_remotebackend_pipe
+    rv=$?
+  ;;
+  test_remotebackend_http)
+    start_web "http"
+    ./test_remotebackend_http
+    rv=$?
+    stop_web
+  ;;
+  test_remotebackend_post)
+    start_web "post"
+    ./test_remotebackend_post
+    rv=$?
+    stop_web
+  ;;
+  test_remotebackend_json)
+    start_web "json"
+    ./test_remotebackend_json
+    rv=$?
+    stop_web
+  ;;
+  *)
+     echo "Usage: $0 test_remotebackend_(pipe|http|post)"
+  ;;
+esac
 
 exit $rv
diff --git a/modules/remotebackend/unittest_json.rb b/modules/remotebackend/unittest_json.rb
new file mode 100755 (executable)
index 0000000..25691be
--- /dev/null
@@ -0,0 +1,58 @@
+#!/usr/bin/ruby
+
+require 'json'
+require 'thread'
+require "rubygems"
+require "webrick"
+require "./unittest"
+
+class DNSBackendHandler < WEBrick::HTTPServlet::AbstractServlet
+   def initialize(server, dnsbackend)
+     @dnsbackend = dnsbackend
+     @semaphore = Mutex.new
+     @f = File.open("/tmp/tmp.txt","a")
+   end
+
+   def do_POST(req,res)
+     req.continue
+
+     return 400, "Bad request" unless req.path == "/dns/endpoint.json"
+
+     tmp = JSON::parse(req.body)
+     method = tmp["method"].downcase
+     method = "do_#{method}"
+     args = tmp["parameters"]
+    
+     @f.puts method
+     @f.puts args
+
+     @semaphore.synchronize do
+       if @dnsbackend.respond_to?(method.to_sym)
+          result, log = @dnsbackend.send(method.to_sym, args)
+          body = {:result => result, :log => log}
+          res.status = 200
+          res["Content-Type"] = "application/javascript; charset=utf-8"
+          res.body = body.to_json
+        else
+          res.status = 404
+          res["Content-Type"] = "application/javascript; charset=utf-8"
+          res.body = ({:result => false, :log => ["Method not found"]}).to_json
+        end
+     end
+   end
+end
+
+server = WEBrick::HTTPServer.new(
+       :Port=>62434,
+       :BindAddress=>"localhost",
+#      Logger: WEBrick::Log.new("remotebackend-server.log"),
+       :AccessLog=>[ [ File.open("remotebackend-access.log", "w"), WEBrick::AccessLog::COMBINED_LOG_FORMAT ] ] 
+)
+
+be = Handler.new 
+server.mount "/dns", DNSBackendHandler, be
+
+trap('INT') { server.stop }
+trap('TERM') { server.stop }
+
+server.start
diff --git a/modules/remotebackend/unittest_post.rb b/modules/remotebackend/unittest_post.rb
new file mode 100755 (executable)
index 0000000..ae65ce5
--- /dev/null
@@ -0,0 +1,59 @@
+#!/usr/bin/ruby
+
+require 'json'
+require 'thread'
+require "rubygems"
+require "webrick"
+require "./unittest"
+
+class DNSBackendHandler < WEBrick::HTTPServlet::AbstractServlet
+   def initialize(server, dnsbackend)
+     @dnsbackend = dnsbackend
+     @semaphore = Mutex.new
+     @f = File.open("/tmp/tmp.txt","a")
+   end
+
+   def do_POST(req,res)
+     req.continue
+
+     tmp = req.path[/dns\/(.*)/,1]
+     return 400, "Bad request" if (tmp.nil?)
+
+     url = tmp.split('/')
+     method = url.shift.downcase
+     method = "do_#{method}"
+     args = JSON::parse(req.query["parameters"])
+    
+     @f.puts method
+     @f.puts args
+
+     @semaphore.synchronize do
+       if @dnsbackend.respond_to?(method.to_sym)
+          result, log = @dnsbackend.send(method.to_sym, args)
+          body = {:result => result, :log => log}
+          res.status = 200
+          res["Content-Type"] = "application/javascript; charset=utf-8"
+          res.body = body.to_json
+        else
+          res.status = 404
+          res["Content-Type"] = "application/javascript; charset=utf-8"
+          res.body = ({:result => false, :log => ["Method not found"]}).to_json
+        end
+     end
+   end
+end
+
+server = WEBrick::HTTPServer.new(
+       :Port=>62434,
+       :BindAddress=>"localhost",
+#      Logger: WEBrick::Log.new("remotebackend-server.log"),
+       :AccessLog=>[ [ File.open("remotebackend-access.log", "w"), WEBrick::AccessLog::COMBINED_LOG_FORMAT ] ] 
+)
+
+be = Handler.new 
+server.mount "/dns", DNSBackendHandler, be
+
+trap('INT') { server.stop }
+trap('TERM') { server.stop }
+
+server.start