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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
|
/* SPDX-License-Identifier: BSD-3-Clause
* Copyright 2020 Intel Corporation
*/
#include <string.h>
#include "telemetry_json.h"
#include "test.h"
static int
test_basic_array(void)
{
const char *expected = "[\"meaning of life\",42]";
char buf[1024];
int used = 0;
printf("%s: ", __func__);
used = rte_tel_json_empty_array(buf, sizeof(buf), used);
if (used != 2 || strcmp(buf, "[]"))
return -1;
used = rte_tel_json_add_array_string(buf, sizeof(buf), used,
"meaning of life");
used = rte_tel_json_add_array_int(buf, sizeof(buf), used, 42);
printf("buf = '%s', expected = '%s'\n", buf, expected);
if (used != (int)strlen(expected))
return -1;
return strncmp(expected, buf, sizeof(buf));
}
static int
test_basic_obj(void)
{
const char *expected = "{\"weddings\":4,\"funerals\":1}";
char buf[1024];
int used = 0;
used = rte_tel_json_add_obj_uint(buf, sizeof(buf), used,
"weddings", 4);
used = rte_tel_json_add_obj_uint(buf, sizeof(buf), used,
"funerals", 1);
printf("%s: buf = '%s', expected = '%s'\n", __func__, buf, expected);
if (used != (int)strlen(expected))
return -1;
return strncmp(expected, buf, sizeof(buf));
}
static int
test_overflow_array(void)
{
static const char * const strs[] = {"Arsenal", "Chelsea", "Liverpool",
"Spurs"};
const char *expected = "[\"Arsenal\",\"Chelsea\"]";
char buf[25];
int i, used = 0;
for (i = 0; i < (int)RTE_DIM(strs); i++)
used = rte_tel_json_add_array_string(buf, sizeof(buf), used,
strs[i]);
printf("%s: buf = '%s', expected = '%s'\n", __func__, buf, expected);
if (buf[used - 1] != ']')
return -1;
if (used != (int)strlen(expected))
return -1;
return strncmp(expected, buf, sizeof(buf));
}
static int
test_overflow_obj(void)
{
static const char * const names[] = {"Italy", "Wales", "Scotland",
"Ireland", "England", "France"};
const int vals[RTE_DIM(names)] = {20, 61, 10, 40, 55, 35};
const char *expected = "{\"Italy\":20,\"Wales\":61}";
char buf[25];
int i, used = 0;
for (i = 0; i < (int)RTE_DIM(names); i++)
used = rte_tel_json_add_obj_uint(buf, sizeof(buf), used, names[i], vals[i]);
printf("%s: buf = '%s', expected = '%s'\n", __func__, buf, expected);
if (buf[used - 1] != '}')
return -1;
if (used != (int)strlen(expected))
return -1;
return strncmp(expected, buf, sizeof(buf));
}
static int
test_large_array_element(void)
{
static const char str[] = "A really long string to overflow buffer";
/* buffer should be unmodified so initial value and expected are same */
const char *expected = "ABC";
char buf[sizeof(str) - 5] = "ABC";
int used = 0;
used = rte_tel_json_add_array_string(buf, sizeof(buf), used, str);
printf("%s: buf = '%s', expected = '%s'\n", __func__, buf, expected);
if (used != 0)
return -1;
return strncmp(expected, buf, sizeof(buf));
}
static int
test_large_obj_element(void)
{
static const char str[] = "A really long string to overflow buffer";
/* buffer should be unmodified so initial value and expected are same */
const char *expected = "XYZ";
char buf[sizeof(str) - 5] = "XYZ";
int used = 0;
used = rte_tel_json_add_obj_uint(buf, sizeof(buf), used, str, 0);
printf("%s: buf = '%s', expected = '%s'\n", __func__, buf, expected);
if (used != 0)
return -1;
return strncmp(expected, buf, sizeof(buf));
}
static int
test_string_char_escaping(void)
{
static const char str[] = "A string across\ntwo lines and \"with quotes\"!";
const char *expected = "\"A string across\\ntwo lines and \\\"with quotes\\\"!\"";
char buf[sizeof(str) + 10] = "";
int used = 0;
used = rte_tel_json_str(buf, sizeof(buf), used, str);
printf("%s: buf = '%s', expected = '%s'\n", __func__, buf, expected);
if (used != (int)strlen(expected))
return -1;
return strncmp(expected, buf, sizeof(buf));
}
static int
test_array_char_escaping(void)
{
/* "meaning of life", with tab between first two words, '\n' at end,
* and "life" in quotes, followed by "all the fish" in quotes
*/
const char *expected = "[\"meaning\\tof \\\"life\\\"\\n\",\"\\\"all the fish\\\"\"]";
char buf[1024];
int used = 0;
used = rte_tel_json_empty_array(buf, sizeof(buf), used);
if (used != 2 || strcmp(buf, "[]"))
return -1;
used = rte_tel_json_add_array_string(buf, sizeof(buf), used, "meaning\tof \"life\"\n");
used = rte_tel_json_add_array_string(buf, sizeof(buf), used, "\"all the fish\"");
printf("buf = '%s', expected = '%s'\n", buf, expected);
if (used != (int)strlen(expected))
return -1;
return strncmp(expected, buf, sizeof(buf));
}
static int
test_obj_char_escaping(void)
{
const char *expected = "{\"good\":\"Clint Eastwood\\n\","
"\"bad\":\"Lee\\tVan\\tCleef\","
"\"ugly\":\"\\rEli Wallach\"}";
char buf[1024];
int used = 0;
used = rte_tel_json_empty_obj(buf, sizeof(buf), used);
if (used != 2 || strcmp(buf, "{}"))
return -1;
used = rte_tel_json_add_obj_str(buf, sizeof(buf), used, "good", "Clint Eastwood\n");
used = rte_tel_json_add_obj_str(buf, sizeof(buf), used, "bad", "Lee\tVan\tCleef");
used = rte_tel_json_add_obj_str(buf, sizeof(buf), used, "ugly", "\rEli Wallach");
printf("buf = '%s', expected = '%s'\n", buf, expected);
if (used != (int)strlen(expected))
return -1;
return strncmp(expected, buf, sizeof(buf));
}
typedef int (*test_fn)(void);
static int
test_telemetry_json(void)
{
unsigned int i;
test_fn fns[] = {
test_basic_array,
test_basic_obj,
test_overflow_array,
test_overflow_obj,
test_large_array_element,
test_large_obj_element,
test_string_char_escaping,
test_array_char_escaping,
test_obj_char_escaping
};
for (i = 0; i < RTE_DIM(fns); i++)
if (fns[i]() == 0)
printf("OK\n");
else {
printf("ERROR\n");
return -1;
}
return 0;
}
REGISTER_FAST_TEST(telemetry_json_autotest, true, true, test_telemetry_json);
|