File: test_json.c

package info (click to toggle)
swupdate 2025.12%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,004 kB
  • sloc: ansic: 66,621; python: 6,291; makefile: 791; sh: 538; javascript: 229
file content (117 lines) | stat: -rw-r--r-- 3,398 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117

/*
 * Author: Christian Storm
 * Copyright (C) 2016, Siemens AG
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include <stdlib.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include <json-c/json.h>
#include "suricatta/suricatta.h"

#define JSON_OBJECT_FREED 1
#define JSONQUOTE(...) #__VA_ARGS__

extern json_object *json_get_key(json_object *json_root, const char *key);
extern json_object *json_get_path_key(json_object *json_root,
				      const char **json_path);

static int json_setup(void **state)
{
	/* clang-format off */
	const char *json_string = JSONQUOTE(
	{
		"name": "hawkBit",
		"id": 5,
		"artifacts" : {
			"count": 3
		},
		"config" : {
			"polling" : {
				"sleep" : "00:01:00"
			}
		}
	});
	/* clang-format on */
	return (*state = json_tokener_parse(json_string)) == NULL ? 1 : 0;
}

static int json_teardown(void **state)
{
	return (json_object_put(*state) != JSON_OBJECT_FREED) ? 1 : 0;
}

extern json_object *__real_json_get_key(json_object *json_root,
					const char *key);
json_object *__wrap_json_get_key(json_object *json_root, const char *key);
json_object *__wrap_json_get_key(json_object *json_root, const char *key)
{
	return (struct json_object *)__real_json_get_key(json_root, key);
}

extern json_object *__real_json_get_path_key(json_object *json_root,
					     const char **json_path);
json_object *__wrap_json_get_path_key(json_object *json_root,
				      const char **json_path);
json_object *__wrap_json_get_path_key(json_object *json_root,
				      const char **json_path)
{
	return __real_json_get_path_key(json_root, json_path);
}

extern json_bool __real_json_object_object_get_ex(struct json_object *obj,
						  const char *key,
						  struct json_object **value);
json_bool __wrap_json_object_object_get_ex(struct json_object *obj,
					   const char *key,
					   struct json_object **value);
json_bool __wrap_json_object_object_get_ex(struct json_object *obj,
					   const char *key,
					   struct json_object **value)
{
	return __real_json_object_object_get_ex(obj, key, value);
}

static void test_json_get_path_key(void **state)
{
	json_object *json_data = json_get_path_key(
	    *state, (const char *[]){"artifacts", "count", NULL});
	assert_non_null(json_data);
	assert_int_equal(json_object_get_type(json_data), json_type_int);
	assert_int_equal(json_object_get_int(json_data), 3);
}

static void test_json_get_key(void **state)
{
	/* parse it via json_get_key(...) function ... */
	json_object *json_child = NULL;
	json_child = json_get_key(*state, "name");
	assert_non_null(json_child);
	assert_int_equal(json_object_get_type(json_child), json_type_string);
	assert_string_equal(json_object_get_string(json_child), "hawkBit");

	/* ... and check it by manual parsing */
	assert_true(json_object_object_get_ex(*state, "name", &json_child));
	assert_int_equal(json_object_get_type(json_child), json_type_string);
	assert_string_equal(json_object_get_string(json_child), "hawkBit");

	/* check not found keys return NULL */
	assert_null((json_child = json_get_key(*state, "wrongname")));
}

int main(void)
{
	int error_count = 0;
	const struct CMUnitTest json_tests[] = {
	    cmocka_unit_test(test_json_get_key),
	    cmocka_unit_test(test_json_get_path_key),
	};
	error_count += cmocka_run_group_tests_name("json", json_tests,
						   json_setup, json_teardown);
	return error_count;
}