]>
Commit | Line | Data |
---|---|---|
1 | #ifndef BOOST_TEST_DYN_LINK | |
2 | #define BOOST_TEST_DYN_LINK | |
3 | #endif | |
4 | ||
5 | #define BOOST_TEST_NO_MAIN | |
6 | ||
7 | #ifdef HAVE_CONFIG_H | |
8 | #include "config.h" | |
9 | #endif | |
10 | #include <boost/test/unit_test.hpp> | |
11 | #include "arguments.hh" | |
12 | #include <utility> | |
13 | #include "lua-auth4.hh" | |
14 | ||
15 | struct SetupArgFixture { | |
16 | SetupArgFixture() { | |
17 | ::arg().set("resolver") = "127.0.0.1"; | |
18 | }; | |
19 | }; | |
20 | ||
21 | BOOST_FIXTURE_TEST_SUITE(lua_auth4_cc, SetupArgFixture) | |
22 | ||
23 | BOOST_AUTO_TEST_CASE(test_prequery) { | |
24 | const std::string script = | |
25 | "function prequery(q)\n" | |
26 | " if q.qdomain == newDN(\"mod.unit.test.\")\n" | |
27 | " then\n" | |
28 | " return true\n" | |
29 | " end\n" | |
30 | " return false\n" | |
31 | "end"; | |
32 | AuthLua4 lua; | |
33 | DNSPacket p(true); | |
34 | p.qdomain = DNSName("mod.unit.test."); | |
35 | lua.loadString(script); | |
36 | std::unique_ptr<DNSPacket> r{nullptr}; | |
37 | try { | |
38 | r = lua.prequery(p); | |
39 | BOOST_REQUIRE(r != nullptr); | |
40 | BOOST_CHECK_EQUAL(r->qdomain.toString(), "mod.unit.test."); | |
41 | } catch (const LuaContext::ExecutionErrorException& e) { | |
42 | try { | |
43 | std::rethrow_if_nested(e); | |
44 | } catch(const std::exception& exp) { | |
45 | g_log<<"Extra info: "<<exp.what(); | |
46 | } | |
47 | } | |
48 | } | |
49 | ||
50 | BOOST_AUTO_TEST_CASE(test_updatePolicy) { | |
51 | const std::string script = | |
52 | "function updatepolicy(query)\n" | |
53 | " princ = query:getPeerPrincipal()\n" | |
54 | " if princ == \"admin@DOMAIN\" or tostring(query:getRemote()) == \"192.168.1.1\"\n" | |
55 | " then\n" | |
56 | " return true\n" | |
57 | " end\n" | |
58 | " return false\n" | |
59 | "end"; | |
60 | AuthLua4 lua; | |
61 | DNSPacket p(true); | |
62 | ComboAddress ca(std::string("192.168.1.1")); | |
63 | lua.loadString(script); | |
64 | p.setRemote(&ca); | |
65 | p.d_peer_principal = "admin@DOMAIN"; | |
66 | BOOST_CHECK_EQUAL(lua.updatePolicy(DNSName("mod.example.com."), QType(QType::A), DNSName("example.com."), p), true); | |
67 | p.d_peer_principal = ""; | |
68 | BOOST_CHECK_EQUAL(lua.updatePolicy(DNSName("mod.example.com."), QType(QType::A), DNSName("example.com."), p), true); | |
69 | ca = ComboAddress(std::string("192.168.1.2")); | |
70 | p.setRemote(&ca); | |
71 | BOOST_CHECK_EQUAL(lua.updatePolicy(DNSName("mod.example.com."), QType(QType::A), DNSName("example.com."), p), false); | |
72 | } | |
73 | ||
74 | BOOST_AUTO_TEST_SUITE_END() |