2014-11-23 09:53:28 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2015-01-22 12:00:23 +01:00
|
|
|
* Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org) *
|
2014-11-23 09:53:28 +01:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#include "config/configcompiler.hpp"
|
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE(config_ops)
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(simple)
|
|
|
|
{
|
2014-12-12 15:39:15 +01:00
|
|
|
ScriptFrame frame;
|
2014-11-23 09:53:28 +01:00
|
|
|
Expression *expr;
|
|
|
|
Dictionary::Ptr dict;
|
|
|
|
|
2014-11-23 12:35:13 +01:00
|
|
|
expr = ConfigCompiler::CompileText("<test>", "");
|
|
|
|
BOOST_CHECK(expr->Evaluate(frame) == Empty);
|
|
|
|
delete expr;
|
|
|
|
|
|
|
|
expr = ConfigCompiler::CompileText("<test>", "\n3");
|
|
|
|
BOOST_CHECK(expr->Evaluate(frame) == 3);
|
|
|
|
delete expr;
|
|
|
|
|
|
|
|
expr = ConfigCompiler::CompileText("<test>", "{ 3\n\n5 }");
|
2015-01-08 10:30:34 +01:00
|
|
|
BOOST_CHECK_THROW(expr->Evaluate(frame), ScriptError);
|
2014-11-23 12:35:13 +01:00
|
|
|
delete expr;
|
|
|
|
|
2014-11-23 09:53:28 +01:00
|
|
|
expr = ConfigCompiler::CompileText("<test>", "1 + 3");
|
|
|
|
BOOST_CHECK(expr->Evaluate(frame) == 4);
|
|
|
|
delete expr;
|
|
|
|
|
|
|
|
expr = ConfigCompiler::CompileText("<test>", "3 - 1");
|
|
|
|
BOOST_CHECK(expr->Evaluate(frame) == 2);
|
|
|
|
delete expr;
|
|
|
|
|
|
|
|
expr = ConfigCompiler::CompileText("<test>", "5m * 10");
|
|
|
|
BOOST_CHECK(expr->Evaluate(frame) == 3000);
|
|
|
|
delete expr;
|
|
|
|
|
|
|
|
expr = ConfigCompiler::CompileText("<test>", "5m / 5");
|
|
|
|
BOOST_CHECK(expr->Evaluate(frame) == 60);
|
|
|
|
delete expr;
|
|
|
|
|
|
|
|
expr = ConfigCompiler::CompileText("<test>", "7 & 3");
|
|
|
|
BOOST_CHECK(expr->Evaluate(frame) == 3);
|
|
|
|
delete expr;
|
|
|
|
|
|
|
|
expr = ConfigCompiler::CompileText("<test>", "2 | 3");
|
|
|
|
BOOST_CHECK(expr->Evaluate(frame) == 3);
|
|
|
|
delete expr;
|
|
|
|
|
|
|
|
expr = ConfigCompiler::CompileText("<test>", "true && false");
|
|
|
|
BOOST_CHECK(!expr->Evaluate(frame));
|
|
|
|
delete expr;
|
|
|
|
|
|
|
|
expr = ConfigCompiler::CompileText("<test>", "true || false");
|
|
|
|
BOOST_CHECK(expr->Evaluate(frame));
|
|
|
|
delete expr;
|
|
|
|
|
|
|
|
expr = ConfigCompiler::CompileText("<test>", "3 < 5");
|
|
|
|
BOOST_CHECK(expr->Evaluate(frame));
|
|
|
|
delete expr;
|
|
|
|
|
|
|
|
expr = ConfigCompiler::CompileText("<test>", "3 > 5");
|
|
|
|
BOOST_CHECK(!expr->Evaluate(frame));
|
|
|
|
delete expr;
|
|
|
|
|
|
|
|
expr = ConfigCompiler::CompileText("<test>", "3 <= 3");
|
|
|
|
BOOST_CHECK(expr->Evaluate(frame));
|
|
|
|
delete expr;
|
|
|
|
|
|
|
|
expr = ConfigCompiler::CompileText("<test>", "3 >= 3");
|
|
|
|
BOOST_CHECK(expr->Evaluate(frame));
|
|
|
|
delete expr;
|
|
|
|
|
|
|
|
expr = ConfigCompiler::CompileText("<test>", "2 + 3 * 4");
|
|
|
|
BOOST_CHECK(expr->Evaluate(frame) == 14);
|
|
|
|
delete expr;
|
|
|
|
|
|
|
|
expr = ConfigCompiler::CompileText("<test>", "(2 + 3) * 4");
|
|
|
|
BOOST_CHECK(expr->Evaluate(frame) == 20);
|
|
|
|
delete expr;
|
|
|
|
|
2014-11-23 23:19:46 +01:00
|
|
|
expr = ConfigCompiler::CompileText("<test>", "2 * - 3");
|
|
|
|
BOOST_CHECK(expr->Evaluate(frame) == -6);
|
|
|
|
delete expr;
|
|
|
|
|
|
|
|
expr = ConfigCompiler::CompileText("<test>", "-(2 + 3)");
|
|
|
|
BOOST_CHECK(expr->Evaluate(frame) == -5);
|
|
|
|
delete expr;
|
|
|
|
|
|
|
|
expr = ConfigCompiler::CompileText("<test>", "- 2 * 2 - 2 * 3 - 4 * - 5");
|
|
|
|
BOOST_CHECK(expr->Evaluate(frame) == 10);
|
|
|
|
delete expr;
|
|
|
|
|
2014-11-23 09:53:28 +01:00
|
|
|
expr = ConfigCompiler::CompileText("<test>", "!0 == true");
|
|
|
|
BOOST_CHECK(expr->Evaluate(frame));
|
|
|
|
delete expr;
|
|
|
|
|
2014-11-23 12:35:13 +01:00
|
|
|
expr = ConfigCompiler::CompileText("<test>", "~0");
|
|
|
|
BOOST_CHECK(expr->Evaluate(frame) == (double)~(long)0);
|
2014-11-23 09:53:28 +01:00
|
|
|
delete expr;
|
|
|
|
|
|
|
|
expr = ConfigCompiler::CompileText("<test>", "4 << 8");
|
|
|
|
BOOST_CHECK(expr->Evaluate(frame) == 1024);
|
|
|
|
delete expr;
|
|
|
|
|
|
|
|
expr = ConfigCompiler::CompileText("<test>", "1024 >> 4");
|
|
|
|
BOOST_CHECK(expr->Evaluate(frame) == 64);
|
|
|
|
delete expr;
|
|
|
|
|
|
|
|
expr = ConfigCompiler::CompileText("<test>", "2 << 3 << 4");
|
|
|
|
BOOST_CHECK(expr->Evaluate(frame) == 256);
|
|
|
|
delete expr;
|
|
|
|
|
|
|
|
expr = ConfigCompiler::CompileText("<test>", "256 >> 4 >> 3");
|
|
|
|
BOOST_CHECK(expr->Evaluate(frame) == 2);
|
|
|
|
delete expr;
|
|
|
|
|
|
|
|
expr = ConfigCompiler::CompileText("<test>", "\"hello\" == \"hello\"");
|
|
|
|
BOOST_CHECK(expr->Evaluate(frame));
|
|
|
|
delete expr;
|
|
|
|
|
|
|
|
expr = ConfigCompiler::CompileText("<test>", "\"hello\" != \"hello\"");
|
|
|
|
BOOST_CHECK(!expr->Evaluate(frame));
|
|
|
|
delete expr;
|
|
|
|
|
|
|
|
expr = ConfigCompiler::CompileText("<test>", "\"foo\" in [ \"foo\", \"bar\" ]");
|
|
|
|
BOOST_CHECK(expr->Evaluate(frame));
|
|
|
|
delete expr;
|
|
|
|
|
|
|
|
expr = ConfigCompiler::CompileText("<test>", "\"foo\" in [ \"bar\", \"baz\" ]");
|
|
|
|
BOOST_CHECK(!expr->Evaluate(frame));
|
|
|
|
delete expr;
|
|
|
|
|
|
|
|
expr = ConfigCompiler::CompileText("<test>", "\"foo\" in null");
|
|
|
|
BOOST_CHECK(!expr->Evaluate(frame));
|
|
|
|
delete expr;
|
|
|
|
|
|
|
|
expr = ConfigCompiler::CompileText("<test>", "\"foo\" in \"bar\"");
|
2014-12-10 15:06:09 +01:00
|
|
|
BOOST_CHECK_THROW(expr->Evaluate(frame), ScriptError);
|
2014-11-23 09:53:28 +01:00
|
|
|
delete expr;
|
|
|
|
|
|
|
|
expr = ConfigCompiler::CompileText("<test>", "\"foo\" !in [ \"bar\", \"baz\" ]");
|
|
|
|
BOOST_CHECK(expr->Evaluate(frame));
|
|
|
|
delete expr;
|
|
|
|
|
|
|
|
expr = ConfigCompiler::CompileText("<test>", "\"foo\" !in [ \"foo\", \"bar\" ]");
|
|
|
|
BOOST_CHECK(!expr->Evaluate(frame));
|
|
|
|
delete expr;
|
|
|
|
|
|
|
|
expr = ConfigCompiler::CompileText("<test>", "\"foo\" !in null");
|
|
|
|
BOOST_CHECK(expr->Evaluate(frame));
|
|
|
|
delete expr;
|
|
|
|
|
|
|
|
expr = ConfigCompiler::CompileText("<test>", "\"foo\" !in \"bar\"");
|
2014-12-10 15:06:09 +01:00
|
|
|
BOOST_CHECK_THROW(expr->Evaluate(frame), ScriptError);
|
2014-11-23 09:53:28 +01:00
|
|
|
delete expr;
|
|
|
|
|
|
|
|
expr = ConfigCompiler::CompileText("<test>", "{ a += 3 }");
|
|
|
|
dict = expr->Evaluate(frame);
|
|
|
|
delete expr;
|
|
|
|
BOOST_CHECK(dict->GetLength() == 1);
|
|
|
|
BOOST_CHECK(dict->Get("a") == 3);
|
|
|
|
|
2014-11-23 12:35:13 +01:00
|
|
|
expr = ConfigCompiler::CompileText("<test>", "test");
|
2014-12-10 15:06:09 +01:00
|
|
|
BOOST_CHECK_THROW(expr->Evaluate(frame), ScriptError);
|
2014-11-23 12:35:13 +01:00
|
|
|
delete expr;
|
2014-11-25 12:26:49 +01:00
|
|
|
|
|
|
|
expr = ConfigCompiler::CompileText("<test>", "null + 3");
|
|
|
|
BOOST_CHECK(expr->Evaluate(frame) == 3);
|
|
|
|
delete expr;
|
|
|
|
|
|
|
|
expr = ConfigCompiler::CompileText("<test>", "3 + null");
|
|
|
|
BOOST_CHECK(expr->Evaluate(frame) == 3);
|
|
|
|
delete expr;
|
|
|
|
|
|
|
|
expr = ConfigCompiler::CompileText("<test>", "\"test\" + 3");
|
|
|
|
BOOST_CHECK(expr->Evaluate(frame) == "test3");
|
|
|
|
delete expr;
|
2015-01-07 13:26:10 +01:00
|
|
|
|
|
|
|
expr = ConfigCompiler::CompileText("<test>", "\"\\\"te\\\\st\"");
|
|
|
|
BOOST_CHECK(expr->Evaluate(frame) == "\"te\\st");
|
|
|
|
delete expr;
|
|
|
|
|
|
|
|
expr = ConfigCompiler::CompileText("<test>", "\"\\'test\"");
|
2015-01-07 15:00:20 +01:00
|
|
|
BOOST_CHECK_THROW(expr->Evaluate(frame), ScriptError);
|
2015-01-07 13:26:10 +01:00
|
|
|
delete expr;
|
2014-11-23 12:35:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(advanced)
|
|
|
|
{
|
2014-12-12 15:39:15 +01:00
|
|
|
ScriptFrame frame;
|
2014-11-23 12:35:13 +01:00
|
|
|
Expression *expr;
|
2015-02-03 14:02:32 +01:00
|
|
|
Function::Ptr func;
|
2014-11-23 12:35:13 +01:00
|
|
|
|
|
|
|
expr = ConfigCompiler::CompileText("<test>", "regex(\"^Hello\", \"Hello World\")");
|
|
|
|
BOOST_CHECK(expr->Evaluate(frame));
|
|
|
|
delete expr;
|
|
|
|
|
|
|
|
expr = ConfigCompiler::CompileText("<test>", "__boost_test()");
|
2014-12-10 15:06:09 +01:00
|
|
|
BOOST_CHECK_THROW(expr->Evaluate(frame), ScriptError);
|
2014-11-23 12:35:13 +01:00
|
|
|
delete expr;
|
|
|
|
|
|
|
|
Object::Ptr self = new Object();
|
2014-12-12 15:39:15 +01:00
|
|
|
ScriptFrame frame2(self);
|
2014-11-23 12:35:13 +01:00
|
|
|
expr = ConfigCompiler::CompileText("<test>", "this");
|
|
|
|
BOOST_CHECK(expr->Evaluate(frame2) == Value(self));
|
|
|
|
delete expr;
|
|
|
|
|
2014-12-16 10:37:21 +01:00
|
|
|
expr = ConfigCompiler::CompileText("<test>", "var v = 7; v");
|
2014-11-23 12:35:13 +01:00
|
|
|
BOOST_CHECK(expr->Evaluate(frame));
|
|
|
|
delete expr;
|
|
|
|
|
|
|
|
expr = ConfigCompiler::CompileText("<test>", "{ a = 3 }.a");
|
|
|
|
BOOST_CHECK(expr->Evaluate(frame) == 3);
|
|
|
|
delete expr;
|
|
|
|
|
|
|
|
expr = ConfigCompiler::CompileText("<test>", "[ 2, 3 ][1]");
|
|
|
|
BOOST_CHECK(expr->Evaluate(frame) == 3);
|
|
|
|
delete expr;
|
|
|
|
|
2014-12-16 10:37:21 +01:00
|
|
|
expr = ConfigCompiler::CompileText("<test>", "var v = { a = 3}; v.a");
|
2014-11-23 12:35:13 +01:00
|
|
|
BOOST_CHECK(expr->Evaluate(frame) == 3);
|
2014-11-23 15:20:51 +01:00
|
|
|
delete expr;
|
2014-11-23 12:35:13 +01:00
|
|
|
|
|
|
|
expr = ConfigCompiler::CompileText("<test>", "a = 3 b = 3");
|
2014-12-10 15:06:09 +01:00
|
|
|
BOOST_CHECK_THROW(expr->Evaluate(frame), ScriptError);
|
2014-12-14 11:55:44 +01:00
|
|
|
delete expr;
|
2014-11-24 00:04:26 +01:00
|
|
|
|
2014-12-11 13:25:06 +01:00
|
|
|
expr = ConfigCompiler::CompileText("<test>", "function() { 3 }()");
|
2014-11-24 00:04:26 +01:00
|
|
|
BOOST_CHECK(expr->Evaluate(frame) == 3);
|
|
|
|
delete expr;
|
|
|
|
|
2014-12-11 13:25:06 +01:00
|
|
|
expr = ConfigCompiler::CompileText("<test>", "function() { return 3, 5 }()");
|
2014-11-24 00:04:26 +01:00
|
|
|
BOOST_CHECK(expr->Evaluate(frame) == 3);
|
|
|
|
delete expr;
|
2014-12-08 11:03:50 +01:00
|
|
|
|
|
|
|
expr = ConfigCompiler::CompileText("<test>", "typeof([]) == Array");
|
|
|
|
BOOST_CHECK(expr->Evaluate(frame));
|
|
|
|
delete expr;
|
|
|
|
|
|
|
|
expr = ConfigCompiler::CompileText("<test>", "typeof({}) == Dictionary");
|
|
|
|
BOOST_CHECK(expr->Evaluate(frame));
|
|
|
|
delete expr;
|
2014-12-09 16:46:29 +01:00
|
|
|
|
|
|
|
expr = ConfigCompiler::CompileText("<test>", "typeof(3) == Number");
|
|
|
|
BOOST_CHECK(expr->Evaluate(frame));
|
|
|
|
delete expr;
|
|
|
|
|
|
|
|
expr = ConfigCompiler::CompileText("<test>", "typeof(\"test\") == String");
|
|
|
|
BOOST_CHECK(expr->Evaluate(frame));
|
|
|
|
delete expr;
|
2014-12-09 20:48:19 +01:00
|
|
|
|
2015-02-02 12:35:07 +01:00
|
|
|
expr = ConfigCompiler::CompileText("<test>", "(7 | 8) == 15");
|
2014-12-09 20:48:19 +01:00
|
|
|
BOOST_CHECK(expr->Evaluate(frame));
|
|
|
|
delete expr;
|
|
|
|
|
2015-02-02 12:35:07 +01:00
|
|
|
expr = ConfigCompiler::CompileText("<test>", "(7 ^ 8) == 15");
|
2014-12-09 20:48:19 +01:00
|
|
|
BOOST_CHECK(expr->Evaluate(frame));
|
|
|
|
delete expr;
|
|
|
|
|
2015-02-02 12:35:07 +01:00
|
|
|
expr = ConfigCompiler::CompileText("<test>", "(7 & 15) == 7");
|
2014-12-09 20:48:19 +01:00
|
|
|
BOOST_CHECK(expr->Evaluate(frame));
|
|
|
|
delete expr;
|
|
|
|
|
|
|
|
expr = ConfigCompiler::CompileText("<test>", "7 in [7] == true");
|
|
|
|
BOOST_CHECK(expr->Evaluate(frame));
|
|
|
|
delete expr;
|
|
|
|
|
|
|
|
expr = ConfigCompiler::CompileText("<test>", "7 !in [7] == false");
|
|
|
|
BOOST_CHECK(expr->Evaluate(frame));
|
|
|
|
delete expr;
|
|
|
|
|
2015-02-02 12:35:07 +01:00
|
|
|
expr = ConfigCompiler::CompileText("<test>", "(7 | 8) > 14");
|
2014-12-09 20:48:19 +01:00
|
|
|
BOOST_CHECK(expr->Evaluate(frame));
|
|
|
|
delete expr;
|
|
|
|
|
2015-02-02 12:35:07 +01:00
|
|
|
expr = ConfigCompiler::CompileText("<test>", "(7 ^ 8) > 14");
|
2014-12-09 20:48:19 +01:00
|
|
|
BOOST_CHECK(expr->Evaluate(frame));
|
|
|
|
delete expr;
|
|
|
|
|
2015-02-02 12:35:07 +01:00
|
|
|
expr = ConfigCompiler::CompileText("<test>", "(7 & 15) > 6");
|
2014-12-09 20:48:19 +01:00
|
|
|
BOOST_CHECK(expr->Evaluate(frame));
|
|
|
|
delete expr;
|
2014-12-14 11:55:44 +01:00
|
|
|
|
|
|
|
expr = ConfigCompiler::CompileText("<test>", "\"a\" = 3");
|
2014-12-15 15:12:24 +01:00
|
|
|
BOOST_CHECK_THROW(expr->Evaluate(frame), ScriptError);
|
2014-12-14 11:55:44 +01:00
|
|
|
delete expr;
|
|
|
|
|
|
|
|
expr = ConfigCompiler::CompileText("<test>", "3 = 3");
|
|
|
|
BOOST_CHECK_THROW(expr->Evaluate(frame), ScriptError);
|
|
|
|
delete expr;
|
2014-12-15 16:42:34 +01:00
|
|
|
|
|
|
|
expr = ConfigCompiler::CompileText("<test>", "var e; e");
|
|
|
|
BOOST_CHECK(expr->Evaluate(frame).IsEmpty());
|
|
|
|
delete expr;
|
|
|
|
|
|
|
|
expr = ConfigCompiler::CompileText("<test>", "var e = 3; e");
|
|
|
|
BOOST_CHECK(expr->Evaluate(frame) == 3);
|
|
|
|
delete expr;
|
2014-12-15 17:23:18 +01:00
|
|
|
|
|
|
|
expr = ConfigCompiler::CompileText("<test>", "Array.x");
|
|
|
|
BOOST_CHECK_THROW(expr->Evaluate(frame), ScriptError);
|
|
|
|
delete expr;
|
2015-02-03 14:02:32 +01:00
|
|
|
|
|
|
|
expr = ConfigCompiler::CompileText("<test>", "{{ 3 }}");
|
|
|
|
func = expr->Evaluate(frame);
|
|
|
|
BOOST_CHECK(func->Invoke() == 3);
|
|
|
|
delete expr;
|
2014-11-23 09:53:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|