From: Aki Tuomi Date: Wed, 5 Jun 2013 07:15:21 +0000 (+0300) Subject: Test suite for POST and POST JSON modes X-Git-Tag: rec-3.6.0-rc1~698^2~9 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b26e91bb037930423f67ea34de3d4233e0458d5c;p=pdns Test suite for POST and POST JSON modes --- diff --git a/modules/remotebackend/Makefile.am b/modules/remotebackend/Makefile.am index 6ee7318a4..c4bf57930 100644 --- a/modules/remotebackend/Makefile.am +++ b/modules/remotebackend/Makefile.am @@ -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 index 000000000..1522d52ab --- /dev/null +++ b/modules/remotebackend/test-remotebackend-json.cc @@ -0,0 +1,73 @@ +#include "pdns/namespaces.hh" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#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 +#include +#include +#include + +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 + +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 index 000000000..754e7a9af --- /dev/null +++ b/modules/remotebackend/test-remotebackend-post.cc @@ -0,0 +1,73 @@ +#include "pdns/namespaces.hh" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#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 +#include +#include +#include + +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 + +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 index 000000000..8433f6a19 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 index 000000000..83e3a0571 Binary files /dev/null and b/modules/remotebackend/test_remotebackend_post differ diff --git a/modules/remotebackend/testrunner.sh b/modules/remotebackend/testrunner.sh index 2828ad061..7f687a5ca 100755 --- a/modules/remotebackend/testrunner.sh +++ b/modules/remotebackend/testrunner.sh @@ -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 index 000000000..25691be83 --- /dev/null +++ b/modules/remotebackend/unittest_json.rb @@ -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 index 000000000..ae65ce56a --- /dev/null +++ b/modules/remotebackend/unittest_post.rb @@ -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