rrdlabels.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #define NETDATA_RRD_INTERNALS
  3. #include "rrd.h"
  4. // ----------------------------------------------------------------------------
  5. // labels sanitization
  6. /*
  7. * All labels follow these rules:
  8. *
  9. * Character Symbol Values Names
  10. * UTF-8 characters UTF-8 yes -> _
  11. * Lower case letter [a-z] yes yes
  12. * Upper case letter [A-Z] yes -> [a-z]
  13. * Digit [0-9] yes yes
  14. * Underscore _ yes yes
  15. * Minus - yes yes
  16. * Plus + yes -> _
  17. * Colon : yes -> _
  18. * Semicolon ; -> : -> _
  19. * Equal = -> : -> _
  20. * Period . yes yes
  21. * Comma , -> . -> .
  22. * Slash / yes yes
  23. * Backslash \ -> / -> /
  24. * At @ yes -> _
  25. * Space yes -> _
  26. * Opening parenthesis ( yes -> _
  27. * Closing parenthesis ) yes -> _
  28. * anything else -> _ -> _
  29. *
  30. * The above rules should allow users to set in tags (indicative):
  31. *
  32. * 1. hostnames and domain names as-is
  33. * 2. email addresses as-is
  34. * 3. floating point numbers, converted to always use a dot as the decimal point
  35. *
  36. * Leading and trailing spaces and control characters are removed from both label
  37. * names and values.
  38. *
  39. * Multiple spaces inside the label name or the value are removed (only 1 is retained).
  40. * In names spaces are also converted to underscores.
  41. *
  42. * Names that are only underscores are rejected (they do not enter the dictionary).
  43. *
  44. * The above rules do not require any conversion to be included in JSON strings.
  45. *
  46. * Label names and values are truncated to LABELS_MAX_LENGTH (200) characters.
  47. *
  48. * When parsing, label key and value are separated by the first colon (:) found.
  49. * So label:value1:value2 is parsed as key = "label", value = "value1:value2"
  50. *
  51. * This means a label key cannot contain a colon (:) - it is converted to
  52. * underscore if it does.
  53. *
  54. */
  55. #define RRDLABELS_MAX_NAME_LENGTH 200
  56. #define RRDLABELS_MAX_VALUE_LENGTH 800 // 800 in bytes, up to 200 UTF-8 characters
  57. static unsigned char label_spaces_char_map[256];
  58. static unsigned char label_names_char_map[256];
  59. static unsigned char label_values_char_map[256] = {
  60. [0] = '\0', //
  61. [1] = '_', //
  62. [2] = '_', //
  63. [3] = '_', //
  64. [4] = '_', //
  65. [5] = '_', //
  66. [6] = '_', //
  67. [7] = '_', //
  68. [8] = '_', //
  69. [9] = '_', //
  70. [10] = '_', //
  71. [11] = '_', //
  72. [12] = '_', //
  73. [13] = '_', //
  74. [14] = '_', //
  75. [15] = '_', //
  76. [16] = '_', //
  77. [17] = '_', //
  78. [18] = '_', //
  79. [19] = '_', //
  80. [20] = '_', //
  81. [21] = '_', //
  82. [22] = '_', //
  83. [23] = '_', //
  84. [24] = '_', //
  85. [25] = '_', //
  86. [26] = '_', //
  87. [27] = '_', //
  88. [28] = '_', //
  89. [29] = '_', //
  90. [30] = '_', //
  91. [31] = '_', //
  92. [32] = ' ', // SPACE keep
  93. [33] = '_', // !
  94. [34] = '_', // "
  95. [35] = '_', // #
  96. [36] = '_', // $
  97. [37] = '_', // %
  98. [38] = '_', // &
  99. [39] = '_', // '
  100. [40] = '(', // ( keep
  101. [41] = ')', // ) keep
  102. [42] = '_', // *
  103. [43] = '+', // + keep
  104. [44] = '.', // , convert , to .
  105. [45] = '-', // - keep
  106. [46] = '.', // . keep
  107. [47] = '/', // / keep
  108. [48] = '0', // 0 keep
  109. [49] = '1', // 1 keep
  110. [50] = '2', // 2 keep
  111. [51] = '3', // 3 keep
  112. [52] = '4', // 4 keep
  113. [53] = '5', // 5 keep
  114. [54] = '6', // 6 keep
  115. [55] = '7', // 7 keep
  116. [56] = '8', // 8 keep
  117. [57] = '9', // 9 keep
  118. [58] = ':', // : keep
  119. [59] = ':', // ; convert ; to :
  120. [60] = '_', // <
  121. [61] = ':', // = convert = to :
  122. [62] = '_', // >
  123. [63] = '_', // ?
  124. [64] = '@', // @
  125. [65] = 'A', // A keep
  126. [66] = 'B', // B keep
  127. [67] = 'C', // C keep
  128. [68] = 'D', // D keep
  129. [69] = 'E', // E keep
  130. [70] = 'F', // F keep
  131. [71] = 'G', // G keep
  132. [72] = 'H', // H keep
  133. [73] = 'I', // I keep
  134. [74] = 'J', // J keep
  135. [75] = 'K', // K keep
  136. [76] = 'L', // L keep
  137. [77] = 'M', // M keep
  138. [78] = 'N', // N keep
  139. [79] = 'O', // O keep
  140. [80] = 'P', // P keep
  141. [81] = 'Q', // Q keep
  142. [82] = 'R', // R keep
  143. [83] = 'S', // S keep
  144. [84] = 'T', // T keep
  145. [85] = 'U', // U keep
  146. [86] = 'V', // V keep
  147. [87] = 'W', // W keep
  148. [88] = 'X', // X keep
  149. [89] = 'Y', // Y keep
  150. [90] = 'Z', // Z keep
  151. [91] = '[', // [ keep
  152. [92] = '/', // backslash convert \ to /
  153. [93] = ']', // ] keep
  154. [94] = '_', // ^
  155. [95] = '_', // _ keep
  156. [96] = '_', // `
  157. [97] = 'a', // a keep
  158. [98] = 'b', // b keep
  159. [99] = 'c', // c keep
  160. [100] = 'd', // d keep
  161. [101] = 'e', // e keep
  162. [102] = 'f', // f keep
  163. [103] = 'g', // g keep
  164. [104] = 'h', // h keep
  165. [105] = 'i', // i keep
  166. [106] = 'j', // j keep
  167. [107] = 'k', // k keep
  168. [108] = 'l', // l keep
  169. [109] = 'm', // m keep
  170. [110] = 'n', // n keep
  171. [111] = 'o', // o keep
  172. [112] = 'p', // p keep
  173. [113] = 'q', // q keep
  174. [114] = 'r', // r keep
  175. [115] = 's', // s keep
  176. [116] = 't', // t keep
  177. [117] = 'u', // u keep
  178. [118] = 'v', // v keep
  179. [119] = 'w', // w keep
  180. [120] = 'x', // x keep
  181. [121] = 'y', // y keep
  182. [122] = 'z', // z keep
  183. [123] = '_', // {
  184. [124] = '_', // |
  185. [125] = '_', // }
  186. [126] = '_', // ~
  187. [127] = '_', //
  188. [128] = '_', //
  189. [129] = '_', //
  190. [130] = '_', //
  191. [131] = '_', //
  192. [132] = '_', //
  193. [133] = '_', //
  194. [134] = '_', //
  195. [135] = '_', //
  196. [136] = '_', //
  197. [137] = '_', //
  198. [138] = '_', //
  199. [139] = '_', //
  200. [140] = '_', //
  201. [141] = '_', //
  202. [142] = '_', //
  203. [143] = '_', //
  204. [144] = '_', //
  205. [145] = '_', //
  206. [146] = '_', //
  207. [147] = '_', //
  208. [148] = '_', //
  209. [149] = '_', //
  210. [150] = '_', //
  211. [151] = '_', //
  212. [152] = '_', //
  213. [153] = '_', //
  214. [154] = '_', //
  215. [155] = '_', //
  216. [156] = '_', //
  217. [157] = '_', //
  218. [158] = '_', //
  219. [159] = '_', //
  220. [160] = '_', //
  221. [161] = '_', //
  222. [162] = '_', //
  223. [163] = '_', //
  224. [164] = '_', //
  225. [165] = '_', //
  226. [166] = '_', //
  227. [167] = '_', //
  228. [168] = '_', //
  229. [169] = '_', //
  230. [170] = '_', //
  231. [171] = '_', //
  232. [172] = '_', //
  233. [173] = '_', //
  234. [174] = '_', //
  235. [175] = '_', //
  236. [176] = '_', //
  237. [177] = '_', //
  238. [178] = '_', //
  239. [179] = '_', //
  240. [180] = '_', //
  241. [181] = '_', //
  242. [182] = '_', //
  243. [183] = '_', //
  244. [184] = '_', //
  245. [185] = '_', //
  246. [186] = '_', //
  247. [187] = '_', //
  248. [188] = '_', //
  249. [189] = '_', //
  250. [190] = '_', //
  251. [191] = '_', //
  252. [192] = '_', //
  253. [193] = '_', //
  254. [194] = '_', //
  255. [195] = '_', //
  256. [196] = '_', //
  257. [197] = '_', //
  258. [198] = '_', //
  259. [199] = '_', //
  260. [200] = '_', //
  261. [201] = '_', //
  262. [202] = '_', //
  263. [203] = '_', //
  264. [204] = '_', //
  265. [205] = '_', //
  266. [206] = '_', //
  267. [207] = '_', //
  268. [208] = '_', //
  269. [209] = '_', //
  270. [210] = '_', //
  271. [211] = '_', //
  272. [212] = '_', //
  273. [213] = '_', //
  274. [214] = '_', //
  275. [215] = '_', //
  276. [216] = '_', //
  277. [217] = '_', //
  278. [218] = '_', //
  279. [219] = '_', //
  280. [220] = '_', //
  281. [221] = '_', //
  282. [222] = '_', //
  283. [223] = '_', //
  284. [224] = '_', //
  285. [225] = '_', //
  286. [226] = '_', //
  287. [227] = '_', //
  288. [228] = '_', //
  289. [229] = '_', //
  290. [230] = '_', //
  291. [231] = '_', //
  292. [232] = '_', //
  293. [233] = '_', //
  294. [234] = '_', //
  295. [235] = '_', //
  296. [236] = '_', //
  297. [237] = '_', //
  298. [238] = '_', //
  299. [239] = '_', //
  300. [240] = '_', //
  301. [241] = '_', //
  302. [242] = '_', //
  303. [243] = '_', //
  304. [244] = '_', //
  305. [245] = '_', //
  306. [246] = '_', //
  307. [247] = '_', //
  308. [248] = '_', //
  309. [249] = '_', //
  310. [250] = '_', //
  311. [251] = '_', //
  312. [252] = '_', //
  313. [253] = '_', //
  314. [254] = '_', //
  315. [255] = '_' //
  316. };
  317. __attribute__((constructor)) void initialize_labels_keys_char_map(void) {
  318. // copy the values char map to the names char map
  319. size_t i;
  320. for(i = 0; i < 256 ;i++)
  321. label_names_char_map[i] = label_values_char_map[i];
  322. // apply overrides to the label names map
  323. label_names_char_map['A'] = 'a';
  324. label_names_char_map['B'] = 'b';
  325. label_names_char_map['C'] = 'c';
  326. label_names_char_map['D'] = 'd';
  327. label_names_char_map['E'] = 'e';
  328. label_names_char_map['F'] = 'f';
  329. label_names_char_map['G'] = 'g';
  330. label_names_char_map['H'] = 'h';
  331. label_names_char_map['I'] = 'i';
  332. label_names_char_map['J'] = 'j';
  333. label_names_char_map['K'] = 'k';
  334. label_names_char_map['L'] = 'l';
  335. label_names_char_map['M'] = 'm';
  336. label_names_char_map['N'] = 'n';
  337. label_names_char_map['O'] = 'o';
  338. label_names_char_map['P'] = 'p';
  339. label_names_char_map['Q'] = 'q';
  340. label_names_char_map['R'] = 'r';
  341. label_names_char_map['S'] = 's';
  342. label_names_char_map['T'] = 't';
  343. label_names_char_map['U'] = 'u';
  344. label_names_char_map['V'] = 'v';
  345. label_names_char_map['W'] = 'w';
  346. label_names_char_map['X'] = 'x';
  347. label_names_char_map['Y'] = 'y';
  348. label_names_char_map['Z'] = 'z';
  349. label_names_char_map['='] = '_';
  350. label_names_char_map[':'] = '_';
  351. label_names_char_map['+'] = '_';
  352. label_names_char_map[';'] = '_';
  353. label_names_char_map['@'] = '_';
  354. label_names_char_map['('] = '_';
  355. label_names_char_map[')'] = '_';
  356. label_names_char_map[' '] = '_';
  357. label_names_char_map['\\'] = '/';
  358. // create the spaces map
  359. for(i = 0; i < 256 ;i++)
  360. label_spaces_char_map[i] = (isspace(i) || iscntrl(i) || !isprint(i))?1:0;
  361. }
  362. size_t text_sanitize(unsigned char *dst, const unsigned char *src, size_t dst_size, unsigned char *char_map, bool utf, const char *empty, size_t *multibyte_length) {
  363. if(unlikely(!dst_size)) return 0;
  364. if(unlikely(!src || !*src)) {
  365. strncpyz((char *)dst, empty, dst_size);
  366. dst[dst_size - 1] = '\0';
  367. size_t len = strlen((char *)dst);
  368. if(multibyte_length) *multibyte_length = len;
  369. return len;
  370. }
  371. unsigned char *d = dst;
  372. // make room for the final string termination
  373. unsigned char *end = &d[dst_size - 1];
  374. // copy while converting, but keep only one white space
  375. // we start wil last_is_space = 1 to skip leading spaces
  376. int last_is_space = 1;
  377. size_t mblen = 0;
  378. while(*src && d < end) {
  379. unsigned char c = *src;
  380. if(IS_UTF8_STARTBYTE(c) && IS_UTF8_BYTE(src[1]) && d + 2 < end) {
  381. // UTF-8 multi-byte encoded character
  382. // find how big this character is (2-4 bytes)
  383. size_t utf_character_size = 2;
  384. while(utf_character_size < 4 && src[utf_character_size] && IS_UTF8_BYTE(src[utf_character_size]) && !IS_UTF8_STARTBYTE(src[utf_character_size]))
  385. utf_character_size++;
  386. if(utf) {
  387. while(utf_character_size) {
  388. utf_character_size--;
  389. *d++ = *src++;
  390. }
  391. }
  392. else {
  393. // UTF-8 characters are not allowed.
  394. // Assume it is an underscore
  395. // and skip all except the first byte
  396. *d++ = '_';
  397. src += (utf_character_size - 1);
  398. }
  399. last_is_space = 0;
  400. mblen++;
  401. continue;
  402. }
  403. if(label_spaces_char_map[c]) {
  404. // a space character
  405. if(!last_is_space) {
  406. // add one space
  407. *d++ = char_map[c];
  408. mblen++;
  409. }
  410. last_is_space++;
  411. }
  412. else {
  413. *d++ = char_map[c];
  414. last_is_space = 0;
  415. mblen++;
  416. }
  417. src++;
  418. }
  419. // remove the last trailing space
  420. if(last_is_space && d > dst) {
  421. d--;
  422. mblen--;
  423. }
  424. // put a termination at the end of what we copied
  425. *d = '\0';
  426. // check if dst is all underscores and empty it if it is
  427. if(*dst == '_') {
  428. unsigned char *t = dst;
  429. while (*t == '_') t++;
  430. if (unlikely(*t == '\0')) {
  431. *dst = '\0';
  432. mblen = 0;
  433. }
  434. }
  435. if(unlikely(*dst == '\0')) {
  436. strncpyz((char *)dst, empty, dst_size);
  437. dst[dst_size - 1] = '\0';
  438. mblen = strlen((char *)dst);
  439. if(multibyte_length) *multibyte_length = mblen;
  440. return mblen;
  441. }
  442. if(multibyte_length) *multibyte_length = mblen;
  443. return d - dst;
  444. }
  445. static inline size_t rrdlabels_sanitize_name(char *dst, const char *src, size_t dst_size) {
  446. return text_sanitize((unsigned char *)dst, (const unsigned char *)src, dst_size, label_names_char_map, 0, "", NULL);
  447. }
  448. static inline size_t rrdlabels_sanitize_value(char *dst, const char *src, size_t dst_size) {
  449. return text_sanitize((unsigned char *)dst, (const unsigned char *)src, dst_size, label_values_char_map, 1, "[none]", NULL);
  450. }
  451. // ----------------------------------------------------------------------------
  452. // rrdlabels_create()
  453. typedef struct rrdlabel {
  454. STRING *label_value;
  455. RRDLABEL_SRC label_source;
  456. } RRDLABEL;
  457. static void rrdlabel_insert_callback(const DICTIONARY_ITEM *item __maybe_unused, void *value, void *dict_ptr __maybe_unused) {
  458. RRDLABEL *lb = (RRDLABEL *)value;
  459. // label_value is already allocated by the STRING
  460. lb->label_source |= RRDLABEL_FLAG_NEW;
  461. lb->label_source &= ~RRDLABEL_FLAG_OLD;
  462. }
  463. static void rrdlabel_delete_callback(const DICTIONARY_ITEM *item __maybe_unused, void *value, void *dict_ptr __maybe_unused) {
  464. RRDLABEL *lb = (RRDLABEL *)value;
  465. string_freez(lb->label_value);
  466. lb->label_value = NULL;
  467. }
  468. static bool rrdlabel_conflict_callback(const DICTIONARY_ITEM *item __maybe_unused, void *oldvalue, void *newvalue, void *dict_ptr __maybe_unused) {
  469. RRDLABEL *lbold = (RRDLABEL *)oldvalue;
  470. RRDLABEL *lbnew = (RRDLABEL *)newvalue;
  471. if(lbold->label_value == lbnew->label_value) {
  472. // they are the same
  473. lbold->label_source |= lbnew->label_source;
  474. lbold->label_source |= RRDLABEL_FLAG_OLD;
  475. lbold->label_source &= ~RRDLABEL_FLAG_NEW;
  476. // free the new one
  477. string_freez(lbnew->label_value);
  478. return false;
  479. }
  480. // they are different
  481. string_freez(lbold->label_value);
  482. lbold->label_value = lbnew->label_value;
  483. lbold->label_source = lbnew->label_source;
  484. lbold->label_source |= RRDLABEL_FLAG_NEW;
  485. lbold->label_source &= ~RRDLABEL_FLAG_OLD;
  486. return true;
  487. }
  488. DICTIONARY *rrdlabels_create(void) {
  489. DICTIONARY *dict = dictionary_create_advanced(DICT_OPTION_DONT_OVERWRITE_VALUE | DICT_OPTION_FIXED_SIZE,
  490. &dictionary_stats_category_rrdlabels, sizeof(RRDLABEL));
  491. dictionary_register_insert_callback(dict, rrdlabel_insert_callback, dict);
  492. dictionary_register_delete_callback(dict, rrdlabel_delete_callback, dict);
  493. dictionary_register_conflict_callback(dict, rrdlabel_conflict_callback, dict);
  494. return dict;
  495. }
  496. // ----------------------------------------------------------------------------
  497. // rrdlabels_destroy()
  498. void rrdlabels_destroy(DICTIONARY *labels_dict) {
  499. dictionary_destroy(labels_dict);
  500. }
  501. void rrdlabels_flush(DICTIONARY *labels_dict) {
  502. dictionary_flush(labels_dict);
  503. }
  504. // ----------------------------------------------------------------------------
  505. // rrdlabels_add()
  506. static void labels_add_already_sanitized(DICTIONARY *dict, const char *key, const char *value, RRDLABEL_SRC ls) {
  507. if(ls & RRDLABEL_FLAG_NEW) ls &= ~RRDLABEL_FLAG_NEW;
  508. if(ls & RRDLABEL_FLAG_OLD) ls &= ~RRDLABEL_FLAG_OLD;
  509. RRDLABEL tmp = {
  510. .label_source = ls,
  511. .label_value = string_strdupz(value)
  512. };
  513. dictionary_set(dict, key, &tmp, sizeof(RRDLABEL));
  514. }
  515. void rrdlabels_add(DICTIONARY *dict, const char *name, const char *value, RRDLABEL_SRC ls) {
  516. if(!dict) {
  517. netdata_log_error("%s(): called with NULL dictionary.", __FUNCTION__ );
  518. return;
  519. }
  520. char n[RRDLABELS_MAX_NAME_LENGTH + 1], v[RRDLABELS_MAX_VALUE_LENGTH + 1];
  521. rrdlabels_sanitize_name(n, name, RRDLABELS_MAX_NAME_LENGTH);
  522. rrdlabels_sanitize_value(v, value, RRDLABELS_MAX_VALUE_LENGTH);
  523. if(!*n) {
  524. netdata_log_error("%s: cannot add name '%s' (value '%s') which is sanitized as empty string", __FUNCTION__, name, value);
  525. return;
  526. }
  527. labels_add_already_sanitized(dict, n, v, ls);
  528. }
  529. static const char *get_quoted_string_up_to(char *dst, size_t dst_size, const char *string, char upto1, char upto2) {
  530. size_t len = 0;
  531. char *d = dst, quote = 0;
  532. while(*string && len++ < dst_size) {
  533. if(unlikely(!quote && (*string == '\'' || *string == '"'))) {
  534. quote = *string++;
  535. continue;
  536. }
  537. if(unlikely(quote && *string == quote)) {
  538. quote = 0;
  539. string++;
  540. continue;
  541. }
  542. if(unlikely(quote && *string == '\\' && string[1])) {
  543. string++;
  544. *d++ = *string++;
  545. continue;
  546. }
  547. if(unlikely(!quote && (*string == upto1 || *string == upto2))) break;
  548. *d++ = *string++;
  549. }
  550. *d = '\0';
  551. if(*string) string++;
  552. return string;
  553. }
  554. void rrdlabels_add_pair(DICTIONARY *dict, const char *string, RRDLABEL_SRC ls) {
  555. if(!dict) {
  556. netdata_log_error("%s(): called with NULL dictionary.", __FUNCTION__ );
  557. return;
  558. }
  559. char name[RRDLABELS_MAX_NAME_LENGTH + 1];
  560. string = get_quoted_string_up_to(name, RRDLABELS_MAX_NAME_LENGTH, string, '=', ':');
  561. char value[RRDLABELS_MAX_VALUE_LENGTH + 1];
  562. get_quoted_string_up_to(value, RRDLABELS_MAX_VALUE_LENGTH, string, '\0', '\0');
  563. rrdlabels_add(dict, name, value, ls);
  564. }
  565. // ----------------------------------------------------------------------------
  566. // rrdlabels_get_value_to_buffer_or_null()
  567. void rrdlabels_get_value_to_buffer_or_null(DICTIONARY *labels, BUFFER *wb, const char *key, const char *quote, const char *null) {
  568. if(!labels) return;
  569. const DICTIONARY_ITEM *acquired_item = dictionary_get_and_acquire_item(labels, key);
  570. RRDLABEL *lb = dictionary_acquired_item_value(acquired_item);
  571. if(lb && lb->label_value)
  572. buffer_sprintf(wb, "%s%s%s", quote, string2str(lb->label_value), quote);
  573. else
  574. buffer_strcat(wb, null);
  575. dictionary_acquired_item_release(labels, acquired_item);
  576. }
  577. void rrdlabels_value_to_buffer_array_item_or_null(DICTIONARY *labels, BUFFER *wb, const char *key) {
  578. if(!labels) return;
  579. const DICTIONARY_ITEM *acquired_item = dictionary_get_and_acquire_item(labels, key);
  580. RRDLABEL *lb = dictionary_acquired_item_value(acquired_item);
  581. if(lb && lb->label_value)
  582. buffer_json_add_array_item_string(wb, string2str(lb->label_value));
  583. else
  584. buffer_json_add_array_item_string(wb, NULL);
  585. dictionary_acquired_item_release(labels, acquired_item);
  586. }
  587. // ----------------------------------------------------------------------------
  588. // rrdlabels_get_value_to_char_or_null()
  589. void rrdlabels_get_value_strdup_or_null(DICTIONARY *labels, char **value, const char *key) {
  590. const DICTIONARY_ITEM *acquired_item = dictionary_get_and_acquire_item(labels, key);
  591. RRDLABEL *lb = dictionary_acquired_item_value(acquired_item);
  592. *value = (lb && lb->label_value) ? strdupz(string2str(lb->label_value)) : NULL;
  593. dictionary_acquired_item_release(labels, acquired_item);
  594. }
  595. void rrdlabels_get_value_strcpyz(DICTIONARY *labels, char *dst, size_t dst_len, const char *key) {
  596. const DICTIONARY_ITEM *acquired_item = dictionary_get_and_acquire_item(labels, key);
  597. RRDLABEL *lb = dictionary_acquired_item_value(acquired_item);
  598. if(lb && lb->label_value)
  599. strncpyz(dst, string2str(lb->label_value), dst_len);
  600. else
  601. dst[0] = '\0';
  602. dictionary_acquired_item_release(labels, acquired_item);
  603. }
  604. STRING *rrdlabels_get_value_string_dup(DICTIONARY *labels, const char *key) {
  605. const DICTIONARY_ITEM *acquired_item = dictionary_get_and_acquire_item(labels, key);
  606. RRDLABEL *lb = dictionary_acquired_item_value(acquired_item);
  607. STRING *ret = NULL;
  608. if(lb && lb->label_value)
  609. ret = string_dup(lb->label_value);
  610. dictionary_acquired_item_release(labels, acquired_item);
  611. return ret;
  612. }
  613. STRING *rrdlabels_get_value_to_buffer_or_unset(DICTIONARY *labels, BUFFER *wb, const char *key, const char *unset) {
  614. const DICTIONARY_ITEM *acquired_item = dictionary_get_and_acquire_item(labels, key);
  615. RRDLABEL *lb = dictionary_acquired_item_value(acquired_item);
  616. STRING *ret = NULL;
  617. if(lb && lb->label_value)
  618. buffer_strcat(wb, string2str(lb->label_value));
  619. else
  620. buffer_strcat(wb, unset);
  621. dictionary_acquired_item_release(labels, acquired_item);
  622. return ret;
  623. }
  624. // ----------------------------------------------------------------------------
  625. // rrdlabels_unmark_all()
  626. // remove labels RRDLABEL_FLAG_OLD and RRDLABEL_FLAG_NEW from all dictionary items
  627. static int remove_flags_old_new(const DICTIONARY_ITEM *item __maybe_unused, void *value, void *data __maybe_unused) {
  628. RRDLABEL *lb = (RRDLABEL *)value;
  629. if(lb->label_source & RRDLABEL_FLAG_OLD) lb->label_source &= ~RRDLABEL_FLAG_OLD;
  630. if(lb->label_source & RRDLABEL_FLAG_NEW) lb->label_source &= ~RRDLABEL_FLAG_NEW;
  631. return 1;
  632. }
  633. void rrdlabels_unmark_all(DICTIONARY *labels) {
  634. dictionary_walkthrough_read(labels, remove_flags_old_new, NULL);
  635. }
  636. // ----------------------------------------------------------------------------
  637. // rrdlabels_remove_all_unmarked()
  638. // remove dictionary items that are neither old, nor new
  639. static int remove_not_old_not_new_callback(const DICTIONARY_ITEM *item, void *value, void *data) {
  640. const char *name = dictionary_acquired_item_name(item);
  641. DICTIONARY *dict = (DICTIONARY *)data;
  642. RRDLABEL *lb = (RRDLABEL *)value;
  643. if(!(lb->label_source & (RRDLABEL_FLAG_OLD | RRDLABEL_FLAG_NEW | RRDLABEL_FLAG_PERMANENT))) {
  644. dictionary_del(dict, name);
  645. return 1;
  646. }
  647. return 0;
  648. }
  649. void rrdlabels_remove_all_unmarked(DICTIONARY *labels) {
  650. dictionary_walkthrough_write(labels, remove_not_old_not_new_callback, labels);
  651. }
  652. // ----------------------------------------------------------------------------
  653. // rrdlabels_walkthrough_read()
  654. struct labels_walkthrough {
  655. int (*callback)(const char *name, const char *value, RRDLABEL_SRC ls, void *data);
  656. void *data;
  657. };
  658. static int labels_walkthrough_callback(const DICTIONARY_ITEM *item, void *value, void *data) {
  659. const char *name = dictionary_acquired_item_name(item);
  660. struct labels_walkthrough *d = (struct labels_walkthrough *)data;
  661. RRDLABEL *lb = (RRDLABEL *)value;
  662. RRDLABEL_SRC ls = lb->label_source;
  663. if(ls & RRDLABEL_FLAG_NEW) ls &= ~RRDLABEL_FLAG_NEW;
  664. if(ls & RRDLABEL_FLAG_OLD) ls &= ~RRDLABEL_FLAG_OLD;
  665. return d->callback(name, string2str(lb->label_value), ls, d->data);
  666. }
  667. int rrdlabels_walkthrough_read(DICTIONARY *labels, int (*callback)(const char *name, const char *value, RRDLABEL_SRC ls, void *data), void *data) {
  668. struct labels_walkthrough d = {
  669. .callback = callback,
  670. .data = data
  671. };
  672. return dictionary_walkthrough_read(labels, labels_walkthrough_callback, &d);
  673. }
  674. int rrdlabels_sorted_walkthrough_read(DICTIONARY *labels, int (*callback)(const char *name, const char *value, RRDLABEL_SRC ls, void *data), void *data) {
  675. struct labels_walkthrough d = {
  676. .callback = callback,
  677. .data = data
  678. };
  679. return dictionary_sorted_walkthrough_read(labels, labels_walkthrough_callback, &d);
  680. }
  681. // ----------------------------------------------------------------------------
  682. // rrdlabels_migrate_to_these()
  683. // migrate an existing label list to a new list, INPLACE
  684. static int copy_label_to_dictionary_callback(const DICTIONARY_ITEM *item, void *value, void *data) {
  685. const char *name = dictionary_acquired_item_name(item);
  686. DICTIONARY *dst = (DICTIONARY *)data;
  687. RRDLABEL *lb = (RRDLABEL *)value;
  688. labels_add_already_sanitized(dst, name, string2str(lb->label_value), lb->label_source);
  689. return 1;
  690. }
  691. void rrdlabels_migrate_to_these(DICTIONARY *dst, DICTIONARY *src) {
  692. if(!dst || !src) return;
  693. // remove the RRDLABEL_FLAG_OLD and RRDLABEL_FLAG_NEW from all items
  694. rrdlabels_unmark_all(dst);
  695. // Mark the existing ones as RRDLABEL_FLAG_OLD,
  696. // or the newly added ones as RRDLABEL_FLAG_NEW
  697. dictionary_walkthrough_read(src, copy_label_to_dictionary_callback, dst);
  698. // remove the unmarked dst
  699. rrdlabels_remove_all_unmarked(dst);
  700. }
  701. void rrdlabels_copy(DICTIONARY *dst, DICTIONARY *src) {
  702. if(!dst || !src) return;
  703. dictionary_walkthrough_read(src, copy_label_to_dictionary_callback, dst);
  704. }
  705. // ----------------------------------------------------------------------------
  706. // rrdlabels_match_simple_pattern()
  707. // returns true when there are keys in the dictionary matching a simple pattern
  708. struct simple_pattern_match_name_value {
  709. size_t searches;
  710. SIMPLE_PATTERN *pattern;
  711. char equal;
  712. };
  713. static int simple_pattern_match_name_only_callback(const DICTIONARY_ITEM *item, void *value, void *data) {
  714. const char *name = dictionary_acquired_item_name(item);
  715. struct simple_pattern_match_name_value *t = (struct simple_pattern_match_name_value *)data;
  716. (void)value;
  717. // we return -1 to stop the walkthrough on first match
  718. t->searches++;
  719. if(simple_pattern_matches(t->pattern, name)) return -1;
  720. return 0;
  721. }
  722. static int simple_pattern_match_name_and_value_callback(const DICTIONARY_ITEM *item, void *value, void *data) {
  723. const char *name = dictionary_acquired_item_name(item);
  724. struct simple_pattern_match_name_value *t = (struct simple_pattern_match_name_value *)data;
  725. RRDLABEL *lb = (RRDLABEL *)value;
  726. // we return -1 to stop the walkthrough on first match
  727. t->searches++;
  728. if(simple_pattern_matches(t->pattern, name)) return -1;
  729. size_t len = RRDLABELS_MAX_NAME_LENGTH + RRDLABELS_MAX_VALUE_LENGTH + 2; // +1 for =, +1 for \0
  730. char tmp[len], *dst = &tmp[0];
  731. const char *v = string2str(lb->label_value);
  732. // copy the name
  733. while(*name) *dst++ = *name++;
  734. // add the equal
  735. *dst++ = t->equal;
  736. // add the value
  737. while(*v) *dst++ = *v++;
  738. // terminate it
  739. *dst = '\0';
  740. t->searches++;
  741. if(simple_pattern_matches_length_extract(t->pattern, tmp, dst - tmp, NULL, 0) == SP_MATCHED_POSITIVE)
  742. return -1;
  743. return 0;
  744. }
  745. bool rrdlabels_match_simple_pattern_parsed(DICTIONARY *labels, SIMPLE_PATTERN *pattern, char equal, size_t *searches) {
  746. if (!labels) return false;
  747. struct simple_pattern_match_name_value t = {
  748. .searches = 0,
  749. .pattern = pattern,
  750. .equal = equal
  751. };
  752. int ret = dictionary_walkthrough_read(labels, equal?simple_pattern_match_name_and_value_callback:simple_pattern_match_name_only_callback, &t);
  753. if(searches)
  754. *searches = t.searches;
  755. return (ret == -1)?true:false;
  756. }
  757. bool rrdlabels_match_simple_pattern(DICTIONARY *labels, const char *simple_pattern_txt) {
  758. if (!labels) return false;
  759. SIMPLE_PATTERN *pattern = simple_pattern_create(simple_pattern_txt, " ,|\t\r\n\f\v", SIMPLE_PATTERN_EXACT, true);
  760. char equal = '\0';
  761. const char *s;
  762. for(s = simple_pattern_txt; *s ; s++) {
  763. if (*s == '=' || *s == ':') {
  764. equal = *s;
  765. break;
  766. }
  767. }
  768. bool ret = rrdlabels_match_simple_pattern_parsed(labels, pattern, equal, NULL);
  769. simple_pattern_free(pattern);
  770. return ret;
  771. }
  772. // ----------------------------------------------------------------------------
  773. // Log all labels
  774. static int rrdlabels_log_label_to_buffer_callback(const DICTIONARY_ITEM *item, void *value, void *data) {
  775. const char *name = dictionary_acquired_item_name(item);
  776. BUFFER *wb = (BUFFER *)data;
  777. RRDLABEL *lb = (RRDLABEL *)value;
  778. buffer_sprintf(wb, "Label: %s: \"%s\" (", name, string2str(lb->label_value));
  779. size_t sources = 0;
  780. if(lb->label_source & RRDLABEL_SRC_AUTO) {
  781. buffer_sprintf(wb, "auto");
  782. sources++;
  783. }
  784. if(lb->label_source & RRDLABEL_SRC_CONFIG)
  785. buffer_sprintf(wb, "%snetdata.conf", sources++?",":"");
  786. if(lb->label_source & RRDLABEL_SRC_K8S)
  787. buffer_sprintf(wb, "%sk8s", sources++?",":"");
  788. if(lb->label_source & RRDLABEL_SRC_ACLK)
  789. buffer_sprintf(wb, "%saclk", sources++?",":"");
  790. if(!sources)
  791. buffer_strcat(wb, "unknown");
  792. buffer_strcat(wb, ")\n");
  793. return 1;
  794. }
  795. void rrdlabels_log_to_buffer(DICTIONARY *labels, BUFFER *wb) {
  796. dictionary_sorted_walkthrough_read(labels, rrdlabels_log_label_to_buffer_callback, wb);
  797. }
  798. // ----------------------------------------------------------------------------
  799. // rrdlabels_to_buffer()
  800. struct labels_to_buffer {
  801. BUFFER *wb;
  802. bool (*filter_callback)(const char *name, const char *value, RRDLABEL_SRC ls, void *data);
  803. void *filter_data;
  804. void (*name_sanitizer)(char *dst, const char *src, size_t dst_size);
  805. void (*value_sanitizer)(char *dst, const char *src, size_t dst_size);
  806. const char *before_each;
  807. const char *quote;
  808. const char *equal;
  809. const char *between_them;
  810. size_t count;
  811. };
  812. static int label_to_buffer_callback(const DICTIONARY_ITEM *item, void *value, void *data) {
  813. const char *name = dictionary_acquired_item_name(item);
  814. struct labels_to_buffer *t = (struct labels_to_buffer *)data;
  815. RRDLABEL *lb = (RRDLABEL *)value;
  816. size_t n_size = (t->name_sanitizer ) ? ( RRDLABELS_MAX_NAME_LENGTH * 2 ) : 1;
  817. size_t v_size = (t->value_sanitizer) ? ( RRDLABELS_MAX_VALUE_LENGTH * 2 ) : 1;
  818. char n[n_size];
  819. char v[v_size];
  820. const char *nn = name, *vv = string2str(lb->label_value);
  821. if(t->name_sanitizer) {
  822. t->name_sanitizer(n, name, n_size);
  823. nn = n;
  824. }
  825. if(t->value_sanitizer) {
  826. t->value_sanitizer(v, string2str(lb->label_value), v_size);
  827. vv = v;
  828. }
  829. if(!t->filter_callback || t->filter_callback(name, string2str(lb->label_value), lb->label_source, t->filter_data)) {
  830. buffer_sprintf(t->wb, "%s%s%s%s%s%s%s%s%s", t->count++?t->between_them:"", t->before_each, t->quote, nn, t->quote, t->equal, t->quote, vv, t->quote);
  831. return 1;
  832. }
  833. return 0;
  834. }
  835. int rrdlabels_to_buffer(DICTIONARY *labels, BUFFER *wb, const char *before_each, const char *equal, const char *quote, const char *between_them, bool (*filter_callback)(const char *name, const char *value, RRDLABEL_SRC ls, void *data), void *filter_data, void (*name_sanitizer)(char *dst, const char *src, size_t dst_size), void (*value_sanitizer)(char *dst, const char *src, size_t dst_size)) {
  836. struct labels_to_buffer tmp = {
  837. .wb = wb,
  838. .filter_callback = filter_callback,
  839. .filter_data = filter_data,
  840. .name_sanitizer = name_sanitizer,
  841. .value_sanitizer = value_sanitizer,
  842. .before_each = before_each,
  843. .equal = equal,
  844. .quote = quote,
  845. .between_them = between_them,
  846. .count = 0
  847. };
  848. return dictionary_walkthrough_read(labels, label_to_buffer_callback, (void *)&tmp);
  849. }
  850. void rrdlabels_to_buffer_json_members(DICTIONARY *labels, BUFFER *wb) {
  851. RRDLABEL *lb;
  852. dfe_start_read(labels, lb) {
  853. buffer_json_member_add_string(wb, lb_dfe.name, string2str(lb->label_value));
  854. }
  855. dfe_done(lb);
  856. }
  857. void rrdset_update_rrdlabels(RRDSET *st, DICTIONARY *new_rrdlabels) {
  858. if(!st->rrdlabels)
  859. st->rrdlabels = rrdlabels_create();
  860. if (new_rrdlabels)
  861. rrdlabels_migrate_to_these(st->rrdlabels, new_rrdlabels);
  862. rrdset_flag_set(st, RRDSET_FLAG_METADATA_UPDATE);
  863. rrdhost_flag_set(st->rrdhost, RRDHOST_FLAG_METADATA_UPDATE);
  864. }
  865. // ----------------------------------------------------------------------------
  866. // rrdlabels unit test
  867. struct rrdlabels_unittest_add_a_pair {
  868. const char *pair;
  869. const char *expected_name;
  870. const char *expected_value;
  871. const char *name;
  872. const char *value;
  873. RRDLABEL_SRC ls;
  874. int errors;
  875. };
  876. int rrdlabels_unittest_add_a_pair_callback(const char *name, const char *value, RRDLABEL_SRC ls, void *data) {
  877. struct rrdlabels_unittest_add_a_pair *t = (struct rrdlabels_unittest_add_a_pair *)data;
  878. t->name = name;
  879. t->value = value;
  880. t->ls = ls;
  881. if(strcmp(name, t->expected_name) != 0) {
  882. fprintf(stderr, "name is wrong, found \"%s\", expected \"%s\"", name, t->expected_name);
  883. t->errors++;
  884. }
  885. if(value == NULL && t->expected_value == NULL) {
  886. ;
  887. }
  888. else if(value == NULL || t->expected_value == NULL) {
  889. fprintf(stderr, "value is wrong, found \"%s\", expected \"%s\"", value?value:"(null)", t->expected_value?t->expected_value:"(null)");
  890. t->errors++;
  891. }
  892. else if(strcmp(value, t->expected_value) != 0) {
  893. fprintf(stderr, "values don't match, found \"%s\", expected \"%s\"", value, t->expected_value);
  894. t->errors++;
  895. }
  896. return 1;
  897. }
  898. int rrdlabels_unittest_add_a_pair(const char *pair, const char *name, const char *value) {
  899. DICTIONARY *labels = rrdlabels_create();
  900. int errors;
  901. fprintf(stderr, "rrdlabels_add_pair(labels, %s) ... ", pair);
  902. rrdlabels_add_pair(labels, pair, RRDLABEL_SRC_CONFIG);
  903. struct rrdlabels_unittest_add_a_pair tmp = {
  904. .pair = pair,
  905. .expected_name = name,
  906. .expected_value = value,
  907. .errors = 0
  908. };
  909. int ret = rrdlabels_walkthrough_read(labels, rrdlabels_unittest_add_a_pair_callback, &tmp);
  910. errors = tmp.errors;
  911. if(ret != 1) {
  912. fprintf(stderr, "failed to get \"%s\" label", name);
  913. errors++;
  914. }
  915. if(!errors)
  916. fprintf(stderr, " OK, name='%s' and value='%s'\n", tmp.name, tmp.value?tmp.value:"(null)");
  917. else
  918. fprintf(stderr, " FAILED\n");
  919. rrdlabels_destroy(labels);
  920. return errors;
  921. }
  922. int rrdlabels_unittest_add_pairs() {
  923. fprintf(stderr, "\n%s() tests\n", __FUNCTION__);
  924. int errors = 0;
  925. // basic test
  926. errors += rrdlabels_unittest_add_a_pair("tag=value", "tag", "value");
  927. errors += rrdlabels_unittest_add_a_pair("tag:value", "tag", "value");
  928. // test newlines
  929. errors += rrdlabels_unittest_add_a_pair(" tag = \t value \r\n", "tag", "value");
  930. // test : in values
  931. errors += rrdlabels_unittest_add_a_pair("tag=:value", "tag", ":value");
  932. errors += rrdlabels_unittest_add_a_pair("tag::value", "tag", ":value");
  933. errors += rrdlabels_unittest_add_a_pair(" tag = :value ", "tag", ":value");
  934. errors += rrdlabels_unittest_add_a_pair(" tag : :value ", "tag", ":value");
  935. errors += rrdlabels_unittest_add_a_pair("tag:5", "tag", "5");
  936. errors += rrdlabels_unittest_add_a_pair("tag:55", "tag", "55");
  937. errors += rrdlabels_unittest_add_a_pair("tag:aa", "tag", "aa");
  938. errors += rrdlabels_unittest_add_a_pair("tag:a", "tag", "a");
  939. // test empty values
  940. errors += rrdlabels_unittest_add_a_pair("tag", "tag", "[none]");
  941. errors += rrdlabels_unittest_add_a_pair("tag:", "tag", "[none]");
  942. errors += rrdlabels_unittest_add_a_pair("tag:\"\"", "tag", "[none]");
  943. errors += rrdlabels_unittest_add_a_pair("tag:''", "tag", "[none]");
  944. errors += rrdlabels_unittest_add_a_pair("tag:\r\n", "tag", "[none]");
  945. errors += rrdlabels_unittest_add_a_pair("tag\r\n", "tag", "[none]");
  946. // test UTF-8 in values
  947. errors += rrdlabels_unittest_add_a_pair("tag: country:Ελλάδα", "tag", "country:Ελλάδα");
  948. errors += rrdlabels_unittest_add_a_pair("\"tag\": \"country:Ελλάδα\"", "tag", "country:Ελλάδα");
  949. errors += rrdlabels_unittest_add_a_pair("\"tag\": country:\"Ελλάδα\"", "tag", "country:Ελλάδα");
  950. errors += rrdlabels_unittest_add_a_pair("\"tag=1\": country:\"Gre\\\"ece\"", "tag_1", "country:Gre_ece");
  951. errors += rrdlabels_unittest_add_a_pair("\"tag=1\" = country:\"Gre\\\"ece\"", "tag_1", "country:Gre_ece");
  952. errors += rrdlabels_unittest_add_a_pair("\t'LABE=L'\t=\t\"World\" peace", "labe_l", "World peace");
  953. errors += rrdlabels_unittest_add_a_pair("\t'LA\\'B:EL'\t=\tcountry:\"World\":\"Europe\":\"Greece\"", "la_b_el", "country:World:Europe:Greece");
  954. errors += rrdlabels_unittest_add_a_pair("\t'LA\\'B:EL'\t=\tcountry\\\"World\"\\\"Europe\"\\\"Greece\"", "la_b_el", "country/World/Europe/Greece");
  955. errors += rrdlabels_unittest_add_a_pair("NAME=\"VALUE\"", "name", "VALUE");
  956. errors += rrdlabels_unittest_add_a_pair("\"NAME\" : \"VALUE\"", "name", "VALUE");
  957. errors += rrdlabels_unittest_add_a_pair("NAME: \"VALUE\"", "name", "VALUE");
  958. return errors;
  959. }
  960. int rrdlabels_unittest_check_simple_pattern(DICTIONARY *labels, const char *pattern, bool expected) {
  961. fprintf(stderr, "rrdlabels_match_simple_pattern(labels, \"%s\") ... ", pattern);
  962. bool ret = rrdlabels_match_simple_pattern(labels, pattern);
  963. fprintf(stderr, "%s, got %s expected %s\n", (ret == expected)?"OK":"FAILED", ret?"true":"false", expected?"true":"false");
  964. return (ret == expected)?0:1;
  965. }
  966. int rrdlabels_unittest_simple_pattern() {
  967. fprintf(stderr, "\n%s() tests\n", __FUNCTION__);
  968. int errors = 0;
  969. DICTIONARY *labels = rrdlabels_create();
  970. rrdlabels_add(labels, "tag1", "value1", RRDLABEL_SRC_CONFIG);
  971. rrdlabels_add(labels, "tag2", "value2", RRDLABEL_SRC_CONFIG);
  972. rrdlabels_add(labels, "tag3", "value3", RRDLABEL_SRC_CONFIG);
  973. errors += rrdlabels_unittest_check_simple_pattern(labels, "*", true);
  974. errors += rrdlabels_unittest_check_simple_pattern(labels, "tag", false);
  975. errors += rrdlabels_unittest_check_simple_pattern(labels, "tag*", true);
  976. errors += rrdlabels_unittest_check_simple_pattern(labels, "*1", true);
  977. errors += rrdlabels_unittest_check_simple_pattern(labels, "value*", false);
  978. errors += rrdlabels_unittest_check_simple_pattern(labels, "*=value*", true);
  979. errors += rrdlabels_unittest_check_simple_pattern(labels, "*:value*", true);
  980. errors += rrdlabels_unittest_check_simple_pattern(labels, "*2", true);
  981. errors += rrdlabels_unittest_check_simple_pattern(labels, "*2 *3", true);
  982. errors += rrdlabels_unittest_check_simple_pattern(labels, "!tag3 *2", true);
  983. errors += rrdlabels_unittest_check_simple_pattern(labels, "tag1 tag2", true);
  984. errors += rrdlabels_unittest_check_simple_pattern(labels, "tag1tag2", false);
  985. errors += rrdlabels_unittest_check_simple_pattern(labels, "invalid1 invalid2 tag3", true);
  986. errors += rrdlabels_unittest_check_simple_pattern(labels, "!tag1 tag4", false);
  987. errors += rrdlabels_unittest_check_simple_pattern(labels, "tag1=value1", true);
  988. errors += rrdlabels_unittest_check_simple_pattern(labels, "tag1=value2", false);
  989. errors += rrdlabels_unittest_check_simple_pattern(labels, "tag*=value*", true);
  990. errors += rrdlabels_unittest_check_simple_pattern(labels, "!tag*=value*", false);
  991. errors += rrdlabels_unittest_check_simple_pattern(labels, "!tag2=something2 tag2=*2", true);
  992. rrdlabels_destroy(labels);
  993. return errors;
  994. }
  995. int rrdlabels_unittest_sanitize_value(const char *src, const char *expected) {
  996. char buf[RRDLABELS_MAX_VALUE_LENGTH + 1];
  997. size_t len = rrdlabels_sanitize_value(buf, src, RRDLABELS_MAX_VALUE_LENGTH);
  998. size_t expected_len = strlen(expected);
  999. int err = 0;
  1000. if(strcmp(buf, expected) != 0) err = 1;
  1001. if(len != expected_len) err = 1;
  1002. fprintf(stderr, "%s(%s): %s, expected '%s', got '%s', expected bytes = %zu, got bytes = %zu\n", __FUNCTION__, src, (err==1)?"FAILED":"OK", expected, buf, expected_len, strlen(buf));
  1003. return err;
  1004. }
  1005. int rrdlabels_unittest_sanitization() {
  1006. int errors = 0;
  1007. errors += rrdlabels_unittest_sanitize_value("", "[none]");
  1008. errors += rrdlabels_unittest_sanitize_value("1", "1");
  1009. errors += rrdlabels_unittest_sanitize_value(" hello world ", "hello world");
  1010. errors += rrdlabels_unittest_sanitize_value("[none]", "[none]");
  1011. // 2-byte UTF-8
  1012. errors += rrdlabels_unittest_sanitize_value(" Ελλάδα ", "Ελλάδα");
  1013. errors += rrdlabels_unittest_sanitize_value("aŰbŲcŴ", "aŰbŲcŴ");
  1014. errors += rrdlabels_unittest_sanitize_value("Ű b Ų c Ŵ", "Ű b Ų c Ŵ");
  1015. // 3-byte UTF-8
  1016. errors += rrdlabels_unittest_sanitize_value("‱", "‱");
  1017. errors += rrdlabels_unittest_sanitize_value("a‱b", "a‱b");
  1018. errors += rrdlabels_unittest_sanitize_value("a ‱ b", "a ‱ b");
  1019. // 4-byte UTF-8
  1020. errors += rrdlabels_unittest_sanitize_value("𩸽", "𩸽");
  1021. errors += rrdlabels_unittest_sanitize_value("a𩸽b", "a𩸽b");
  1022. errors += rrdlabels_unittest_sanitize_value("a 𩸽 b", "a 𩸽 b");
  1023. // mixed multi-byte
  1024. errors += rrdlabels_unittest_sanitize_value("Ű‱𩸽‱Ű", "Ű‱𩸽‱Ű");
  1025. return errors;
  1026. }
  1027. int rrdlabels_unittest(void) {
  1028. int errors = 0;
  1029. errors += rrdlabels_unittest_sanitization();
  1030. errors += rrdlabels_unittest_add_pairs();
  1031. errors += rrdlabels_unittest_simple_pattern();
  1032. fprintf(stderr, "%d errors found\n", errors);
  1033. return errors;
  1034. }