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