1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2020 Intel Corporation
7 #include "telemetry_json.h"
12 test_basic_array(void)
14 const char *expected = "[\"meaning of life\",42]";
18 printf("%s: ", __func__);
19 used = rte_tel_json_empty_array(buf, sizeof(buf), used);
20 if (used != 2 || strcmp(buf, "[]"))
23 used = rte_tel_json_add_array_string(buf, sizeof(buf), used,
25 used = rte_tel_json_add_array_int(buf, sizeof(buf), used, 42);
27 printf("buf = '%s', expected = '%s'\n", buf, expected);
28 if (used != (int)strlen(expected))
30 return strncmp(expected, buf, sizeof(buf));
36 const char *expected = "{\"weddings\":4,\"funerals\":1}";
40 used = rte_tel_json_add_obj_u64(buf, sizeof(buf), used,
42 used = rte_tel_json_add_obj_u64(buf, sizeof(buf), used,
45 printf("%s: buf = '%s', expected = '%s'\n", __func__, buf, expected);
46 if (used != (int)strlen(expected))
48 return strncmp(expected, buf, sizeof(buf));
52 test_overflow_array(void)
54 static const char * const strs[] = {"Arsenal", "Chelsea", "Liverpool",
56 const char *expected = "[\"Arsenal\",\"Chelsea\"]";
60 for (i = 0; i < (int)RTE_DIM(strs); i++)
61 used = rte_tel_json_add_array_string(buf, sizeof(buf), used,
64 printf("%s: buf = '%s', expected = '%s'\n", __func__, buf, expected);
65 if (buf[used - 1] != ']')
67 if (used != (int)strlen(expected))
69 return strncmp(expected, buf, sizeof(buf));
73 test_overflow_obj(void)
75 static const char * const names[] = {"Italy", "Wales", "Scotland",
76 "Ireland", "England", "France"};
77 const int vals[RTE_DIM(names)] = {20, 61, 10, 40, 55, 35};
78 const char *expected = "{\"Italy\":20,\"Wales\":61}";
82 for (i = 0; i < (int)RTE_DIM(names); i++)
83 used = rte_tel_json_add_obj_u64(buf, sizeof(buf), used,
86 printf("%s: buf = '%s', expected = '%s'\n", __func__, buf, expected);
87 if (buf[used - 1] != '}')
89 if (used != (int)strlen(expected))
91 return strncmp(expected, buf, sizeof(buf));
95 test_large_array_element(void)
97 static const char str[] = "A really long string to overflow buffer";
98 /* buffer should be unmodified so initial value and expected are same */
99 const char *expected = "ABC";
100 char buf[sizeof(str) - 5] = "ABC";
103 used = rte_tel_json_add_array_string(buf, sizeof(buf), used, str);
104 printf("%s: buf = '%s', expected = '%s'\n", __func__, buf, expected);
106 return strlen(buf) != 0;
110 test_large_obj_element(void)
112 static const char str[] = "A really long string to overflow buffer";
113 /* buffer should be unmodified so initial value and expected are same */
114 const char *expected = "XYZ";
115 char buf[sizeof(str) - 5] = "XYZ";
118 used = rte_tel_json_add_obj_u64(buf, sizeof(buf), used, str, 0);
119 printf("%s: buf = '%s', expected = '%s'\n", __func__, buf, expected);
121 return strlen(buf) != 0;
125 test_telemetry_json(void)
127 if (test_basic_array() < 0 ||
128 test_basic_obj() < 0 ||
129 test_overflow_array() < 0 ||
130 test_overflow_obj() < 0 ||
131 test_large_array_element() < 0 ||
132 test_large_obj_element() < 0)
137 REGISTER_TEST_COMMAND(telemetry_json_autotest, test_telemetry_json);