2019-02-25 14:48:22 +01:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2014-10-26 19:59:49 +01:00
|
|
|
|
|
|
|
#include "base/dictionary.hpp"
|
2021-04-30 15:49:52 +02:00
|
|
|
#include "base/function.hpp"
|
2019-03-15 11:26:51 +01:00
|
|
|
#include "base/namespace.hpp"
|
2019-03-18 17:02:12 +01:00
|
|
|
#include "base/array.hpp"
|
2014-10-26 19:59:49 +01:00
|
|
|
#include "base/objectlock.hpp"
|
|
|
|
#include "base/json.hpp"
|
2019-03-15 11:26:51 +01:00
|
|
|
#include <boost/algorithm/string/replace.hpp>
|
2016-09-07 08:20:51 +02:00
|
|
|
#include <BoostTestTargetConfig.h>
|
2014-10-26 19:59:49 +01:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE(base_json)
|
|
|
|
|
2019-03-15 11:26:51 +01:00
|
|
|
BOOST_AUTO_TEST_CASE(encode)
|
|
|
|
{
|
|
|
|
Dictionary::Ptr input (new Dictionary({
|
|
|
|
{ "array", new Array({ new Namespace() }) },
|
|
|
|
{ "false", false },
|
|
|
|
{ "float", -1.25 },
|
2021-04-30 15:49:52 +02:00
|
|
|
{ "fx", new Function("<test>", []() {}) },
|
2019-03-15 11:26:51 +01:00
|
|
|
{ "int", -42 },
|
|
|
|
{ "null", Value() },
|
|
|
|
{ "string", "LF\nTAB\tAUml\xC3\xA4Ill\xC3" },
|
|
|
|
{ "true", true },
|
|
|
|
{ "uint", 23u }
|
|
|
|
}));
|
|
|
|
|
|
|
|
String output (R"EOF({
|
|
|
|
"array": [
|
|
|
|
{}
|
|
|
|
],
|
|
|
|
"false": false,
|
|
|
|
"float": -1.25,
|
2021-04-30 15:49:52 +02:00
|
|
|
"fx": "Object of type 'Function'",
|
2021-03-23 17:52:37 +01:00
|
|
|
"int": -42,
|
2019-03-15 11:26:51 +01:00
|
|
|
"null": null,
|
|
|
|
"string": "LF\nTAB\tAUml\u00e4Ill\ufffd",
|
|
|
|
"true": true,
|
2021-03-23 17:52:37 +01:00
|
|
|
"uint": 23
|
2020-09-02 16:11:43 +02:00
|
|
|
}
|
|
|
|
)EOF");
|
2019-03-15 11:26:51 +01:00
|
|
|
|
|
|
|
BOOST_CHECK(JsonEncode(input, true) == output);
|
|
|
|
|
|
|
|
boost::algorithm::replace_all(output, " ", "");
|
2021-04-30 15:49:52 +02:00
|
|
|
boost::algorithm::replace_all(output, "Objectoftype'Function'", "Object of type 'Function'");
|
2019-03-15 11:26:51 +01:00
|
|
|
boost::algorithm::replace_all(output, "\n", "");
|
|
|
|
|
|
|
|
BOOST_CHECK(JsonEncode(input, false) == output);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(decode)
|
|
|
|
{
|
|
|
|
String input (R"EOF({
|
|
|
|
"array": [
|
|
|
|
{}
|
|
|
|
],
|
|
|
|
"false": false,
|
|
|
|
"float": -1.25,
|
2021-03-23 17:52:37 +01:00
|
|
|
"int": -42,
|
2019-03-15 11:26:51 +01:00
|
|
|
"null": null,
|
|
|
|
"string": "LF\nTAB\tAUmlIll",
|
|
|
|
"true": true,
|
2021-03-23 17:52:37 +01:00
|
|
|
"uint": 23
|
2020-09-02 16:11:43 +02:00
|
|
|
}
|
|
|
|
)EOF");
|
2019-03-15 11:26:51 +01:00
|
|
|
|
|
|
|
boost::algorithm::replace_all(input, "AUml", "AUml\xC3\xA4");
|
|
|
|
boost::algorithm::replace_all(input, "Ill", "Ill\xC3");
|
|
|
|
|
|
|
|
auto output ((Dictionary::Ptr)JsonDecode(input));
|
|
|
|
BOOST_CHECK(output->GetKeys() == std::vector<String>({"array", "false", "float", "int", "null", "string", "true", "uint"}));
|
|
|
|
|
|
|
|
auto array ((Array::Ptr)output->Get("array"));
|
|
|
|
BOOST_CHECK(array->GetLength() == 1u);
|
|
|
|
|
|
|
|
auto array0 ((Dictionary::Ptr)array->Get(0));
|
|
|
|
BOOST_CHECK(array0->GetKeys() == std::vector<String>());
|
|
|
|
|
|
|
|
auto fAlse (output->Get("false"));
|
|
|
|
BOOST_CHECK(fAlse.IsBoolean() && !fAlse.ToBool());
|
|
|
|
|
|
|
|
auto fLoat (output->Get("float"));
|
|
|
|
BOOST_CHECK(fLoat.IsNumber() && fLoat.Get<double>() == -1.25);
|
|
|
|
|
|
|
|
auto iNt (output->Get("int"));
|
|
|
|
BOOST_CHECK(iNt.IsNumber() && iNt.Get<double>() == -42.0);
|
|
|
|
|
|
|
|
BOOST_CHECK(output->Get("null").IsEmpty());
|
|
|
|
|
|
|
|
auto string (output->Get("string"));
|
|
|
|
BOOST_CHECK(string.IsString() && string.Get<String>() == "LF\nTAB\tAUml\xC3\xA4Ill\xEF\xBF\xBD");
|
|
|
|
|
|
|
|
auto tRue (output->Get("true"));
|
|
|
|
BOOST_CHECK(tRue.IsBoolean() && tRue.ToBool());
|
|
|
|
|
|
|
|
auto uint (output->Get("uint"));
|
|
|
|
BOOST_CHECK(uint.IsNumber() && uint.Get<double>() == 23.0);
|
|
|
|
}
|
|
|
|
|
2014-10-26 19:59:49 +01:00
|
|
|
BOOST_AUTO_TEST_CASE(invalid1)
|
|
|
|
{
|
|
|
|
BOOST_CHECK_THROW(JsonDecode("\"1.7"), std::exception);
|
|
|
|
BOOST_CHECK_THROW(JsonDecode("{8: \"test\"}"), std::exception);
|
|
|
|
BOOST_CHECK_THROW(JsonDecode("{\"test\": \"test\""), std::exception);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|