1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2020 Intel Corporation
3 */
4
5 #include <string.h>
6
7 #include "telemetry_json.h"
8
9 #include "test.h"
10
11 static int
test_basic_array(void)12 test_basic_array(void)
13 {
14 const char *expected = "[\"meaning of life\",42]";
15 char buf[1024];
16 int used = 0;
17
18 printf("%s: ", __func__);
19 used = rte_tel_json_empty_array(buf, sizeof(buf), used);
20 if (used != 2 || strcmp(buf, "[]"))
21 return -1;
22
23 used = rte_tel_json_add_array_string(buf, sizeof(buf), used,
24 "meaning of life");
25 used = rte_tel_json_add_array_int(buf, sizeof(buf), used, 42);
26
27 printf("buf = '%s', expected = '%s'\n", buf, expected);
28 if (used != (int)strlen(expected))
29 return -1;
30 return strncmp(expected, buf, sizeof(buf));
31 }
32
33 static int
test_basic_obj(void)34 test_basic_obj(void)
35 {
36 const char *expected = "{\"weddings\":4,\"funerals\":1}";
37 char buf[1024];
38 int used = 0;
39
40 used = rte_tel_json_add_obj_u64(buf, sizeof(buf), used,
41 "weddings", 4);
42 used = rte_tel_json_add_obj_u64(buf, sizeof(buf), used,
43 "funerals", 1);
44
45 printf("%s: buf = '%s', expected = '%s'\n", __func__, buf, expected);
46 if (used != (int)strlen(expected))
47 return -1;
48 return strncmp(expected, buf, sizeof(buf));
49 }
50
51 static int
test_overflow_array(void)52 test_overflow_array(void)
53 {
54 static const char * const strs[] = {"Arsenal", "Chelsea", "Liverpool",
55 "Spurs"};
56 const char *expected = "[\"Arsenal\",\"Chelsea\"]";
57 char buf[25];
58 int i, used = 0;
59
60 for (i = 0; i < (int)RTE_DIM(strs); i++)
61 used = rte_tel_json_add_array_string(buf, sizeof(buf), used,
62 strs[i]);
63
64 printf("%s: buf = '%s', expected = '%s'\n", __func__, buf, expected);
65 if (buf[used - 1] != ']')
66 return -1;
67 if (used != (int)strlen(expected))
68 return -1;
69 return strncmp(expected, buf, sizeof(buf));
70 }
71
72 static int
test_overflow_obj(void)73 test_overflow_obj(void)
74 {
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}";
79 char buf[25];
80 int i, used = 0;
81
82 for (i = 0; i < (int)RTE_DIM(names); i++)
83 used = rte_tel_json_add_obj_u64(buf, sizeof(buf), used,
84 names[i], vals[i]);
85
86 printf("%s: buf = '%s', expected = '%s'\n", __func__, buf, expected);
87 if (buf[used - 1] != '}')
88 return -1;
89 if (used != (int)strlen(expected))
90 return -1;
91 return strncmp(expected, buf, sizeof(buf));
92 }
93
94 static int
test_large_array_element(void)95 test_large_array_element(void)
96 {
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";
101 int used = 0;
102
103 used = rte_tel_json_add_array_string(buf, sizeof(buf), used, str);
104 printf("%s: buf = '%s', expected = '%s'\n", __func__, buf, expected);
105
106 return strlen(buf) != 0;
107 }
108
109 static int
test_large_obj_element(void)110 test_large_obj_element(void)
111 {
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";
116 int used = 0;
117
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);
120
121 return strlen(buf) != 0;
122 }
123
124 static int
test_telemetry_json(void)125 test_telemetry_json(void)
126 {
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)
133 return -1;
134 return 0;
135 }
136
137 REGISTER_TEST_COMMAND(telemetry_json_autotest, test_telemetry_json);
138