2025-01-14 14:42:39 +00:00
|
|
|
/* Unit tests libconfig to jansson converters.
|
2017-03-27 12:38:19 +02:00
|
|
|
*
|
2025-01-14 14:42:39 +00:00
|
|
|
* Author: Steffen Vogel <post@steffenvogel.de>
|
|
|
|
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2017-03-27 12:38:19 +02:00
|
|
|
|
|
|
|
#include <criterion/criterion.h>
|
|
|
|
|
2019-04-05 11:01:49 +02:00
|
|
|
#ifdef WITH_CONFIG
|
2017-08-03 00:19:27 +02:00
|
|
|
|
2017-03-27 12:38:19 +02:00
|
|
|
#include <jansson.h>
|
|
|
|
#include <libconfig.h>
|
|
|
|
|
2019-03-26 07:01:10 +01:00
|
|
|
#include <villas/config_helper.hpp>
|
2025-01-14 14:42:39 +00:00
|
|
|
#include <villas/utils.hpp>
|
2017-03-27 12:38:19 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
using namespace villas::node;
|
|
|
|
|
2017-03-27 12:38:19 +02:00
|
|
|
const char *cfg_example = "test : \n"
|
2025-01-14 14:42:39 +00:00
|
|
|
"{\n"
|
|
|
|
" hallo = 1L;\n"
|
|
|
|
"};\n"
|
|
|
|
"liste = ( 1.1, 2L, 3L, 4L, \n"
|
|
|
|
" {\n"
|
|
|
|
" objekt : \n"
|
|
|
|
" {\n"
|
|
|
|
" key = \"value\";\n"
|
|
|
|
" };\n"
|
|
|
|
" } );\n";
|
2017-03-27 12:38:19 +02:00
|
|
|
|
|
|
|
const char *json_example = "{\n"
|
2025-01-14 14:42:39 +00:00
|
|
|
" \"test\": {\n"
|
|
|
|
" \"hallo\": 1\n"
|
|
|
|
" },\n"
|
|
|
|
" \"liste\": [\n"
|
|
|
|
" 1.1000000000000001,\n"
|
|
|
|
" 2,\n"
|
|
|
|
" 3,\n"
|
|
|
|
" 4,\n"
|
|
|
|
" {\n"
|
|
|
|
" \"objekt\": {\n"
|
|
|
|
" \"key\": \"value\"\n"
|
|
|
|
" }\n"
|
|
|
|
" }\n"
|
|
|
|
" ]\n"
|
|
|
|
"}";
|
2017-03-27 12:38:19 +02:00
|
|
|
|
2020-09-11 14:57:05 +02:00
|
|
|
// cppcheck-suppress syntaxError
|
2025-01-14 14:42:39 +00:00
|
|
|
Test(config, config_to_json) {
|
|
|
|
int ret;
|
|
|
|
config_t cfg;
|
|
|
|
config_setting_t *cfg_root;
|
|
|
|
json_t *json;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
config_init(&cfg);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
ret = config_read_string(&cfg, cfg_example);
|
|
|
|
cr_assert_eq(ret, CONFIG_TRUE);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
cfg_root = config_root_setting(&cfg);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
json = config_to_json(cfg_root);
|
|
|
|
cr_assert_not_null(json);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
char *str = json_dumps(json, JSON_INDENT(2));
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
//printf("%s\n", str);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
json_decref(json);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
cr_assert_str_eq(str, json_example);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
config_destroy(&cfg);
|
2017-03-27 12:38:19 +02:00
|
|
|
}
|
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
Test(config, json_to_config) {
|
|
|
|
config_t cfg;
|
|
|
|
config_setting_t *cfg_root;
|
|
|
|
json_t *json;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
// For config_write()
|
|
|
|
FILE *f;
|
|
|
|
char str[1024];
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
config_init(&cfg);
|
2017-03-27 12:38:19 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
cfg_root = config_root_setting(&cfg);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
json = json_loads(json_example, 0, nullptr);
|
|
|
|
cr_assert_not_null(json);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
json_to_config(json, cfg_root);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
//config_write(&cfg, stdout);
|
2017-03-27 12:38:19 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
f = fmemopen(str, sizeof(str), "w+");
|
|
|
|
config_write(&cfg, f);
|
|
|
|
fclose(f);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
cr_assert_str_eq(str, cfg_example);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
json_decref(json);
|
2017-04-15 20:38:58 +02:00
|
|
|
}
|
2017-08-03 00:19:27 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
#endif // WITH_CONFIG
|