1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2020 Intel Corporation
7 #include "../../lib/librte_telemetry/telemetry_json.h"
11 test_basic_array(void)
13 const char *expected = "[\"meaning of life\",42]";
17 printf("%s: ", __func__);
18 used = rte_tel_json_empty_array(buf, sizeof(buf), used);
19 if (used != 2 || strcmp(buf, "[]"))
22 used = rte_tel_json_add_array_string(buf, sizeof(buf), used,
24 used = rte_tel_json_add_array_int(buf, sizeof(buf), used, 42);
26 printf("buf = '%s', expected = '%s'\n", buf, expected);
27 if (used != (int)strlen(expected))
29 return strncmp(expected, buf, sizeof(buf));
35 const char *expected = "{\"weddings\":4,\"funerals\":1}";
39 used = rte_tel_json_add_obj_u64(buf, sizeof(buf), used,
41 used = rte_tel_json_add_obj_u64(buf, sizeof(buf), used,
44 printf("%s: buf = '%s', expected = '%s'\n", __func__, buf, expected);
45 if (used != (int)strlen(expected))
47 return strncmp(expected, buf, sizeof(buf));
51 test_overflow_array(void)
53 static const char * const strs[] = {"Arsenal", "Chelsea", "Liverpool",
55 const char *expected = "[\"Arsenal\",\"Chelsea\"]";
59 for (i = 0; i < (int)RTE_DIM(strs); i++)
60 used = rte_tel_json_add_array_string(buf, sizeof(buf), used,
63 printf("%s: buf = '%s', expected = '%s'\n", __func__, buf, expected);
64 if (buf[used - 1] != ']')
66 if (used != (int)strlen(expected))
68 return strncmp(expected, buf, sizeof(buf));
72 test_overflow_obj(void)
74 static const char * const names[] = {"Italy", "Wales", "Scotland",
75 "Ireland", "England", "France"};
76 const int vals[RTE_DIM(names)] = {20, 61, 10, 40, 55, 35};
77 const char *expected = "{\"Italy\":20,\"Wales\":61}";
81 for (i = 0; i < (int)RTE_DIM(names); i++)
82 used = rte_tel_json_add_obj_u64(buf, sizeof(buf), used,
85 printf("%s: buf = '%s', expected = '%s'\n", __func__, buf, expected);
86 if (buf[used - 1] != '}')
88 if (used != (int)strlen(expected))
90 return strncmp(expected, buf, sizeof(buf));
94 test_large_array_element(void)
96 static const char str[] = "A really long string to overflow buffer";
97 /* buffer should be unmodified so initial value and expected are same */
98 const char *expected = "ABC";
99 char buf[sizeof(str) - 5] = "ABC";
102 used = rte_tel_json_add_array_string(buf, sizeof(buf), used, str);
103 printf("%s: buf = '%s', expected = '%s'\n", __func__, buf, expected);
105 return strlen(buf) != 0;
109 test_large_obj_element(void)
111 static const char str[] = "A really long string to overflow buffer";
112 /* buffer should be unmodified so initial value and expected are same */
113 const char *expected = "XYZ";
114 char buf[sizeof(str) - 5] = "XYZ";
117 used = rte_tel_json_add_obj_u64(buf, sizeof(buf), used, str, 0);
118 printf("%s: buf = '%s', expected = '%s'\n", __func__, buf, expected);
120 return strlen(buf) != 0;
124 test_telemetry_json(void)
126 if (test_basic_array() < 0 ||
127 test_basic_obj() < 0 ||
128 test_overflow_array() < 0 ||
129 test_overflow_obj() < 0 ||
130 test_large_array_element() < 0 ||
131 + test_large_obj_element() < 0)
136 REGISTER_TEST_COMMAND(telemetry_json_autotest, test_telemetry_json);