X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Futils_format_json_test.c;h=389004d3b99ccbd5a1e5661ff207b6c17a6bdf1a;hb=0d9f395599348e735e6f461e1c328293bef0d060;hp=aa275fd208515c82ce42ab20a4ef1e16fc2df3f4;hpb=c7d859191d8fbfc129dea8cfaf9e13e78fd6642c;p=collectd.git diff --git a/src/utils_format_json_test.c b/src/utils_format_json_test.c index aa275fd2..389004d3 100644 --- a/src/utils_format_json_test.c +++ b/src/utils_format_json_test.c @@ -24,50 +24,60 @@ * Florian octo Forster */ -#include "testing.h" +/* Workaround for Solaris 10 defining label_t + * Issue #1301 + */ + +#include "config.h" +#if KERNEL_SOLARIS +#ifndef _POSIX_C_SOURCE +#define _POSIX_C_SOURCE 200112L +#endif +#undef __EXTENSIONS__ +#endif + #include "collectd.h" -#include "utils_format_json.h" + #include "common.h" /* for STATIC_ARRAY_SIZE */ +#include "testing.h" +#include "utils_format_json.h" #include #include #if HAVE_YAJL_YAJL_VERSION_H -# include +#include #endif #if YAJL_MAJOR > 1 -# define HAVE_YAJL_V2 1 +#define HAVE_YAJL_V2 1 #endif -typedef struct -{ +typedef struct { char const *key; char const *value; } label_t; -typedef struct -{ +typedef struct { label_t *expected_labels; - size_t expected_labels_num; + size_t expected_labels_num; label_t *current_label; } test_case_t; #if HAVE_YAJL_V2 -static int test_map_key (void *ctx, unsigned char const *key, size_t key_len) +static int test_map_key(void *ctx, unsigned char const *key, size_t key_len) #else -static int test_map_key (void *ctx, unsigned char const *key, unsigned int key_len) +static int test_map_key(void *ctx, unsigned char const *key, + unsigned int key_len) #endif { test_case_t *c = ctx; size_t i; c->current_label = NULL; - for (i = 0; i < c->expected_labels_num; i++) - { + for (i = 0; i < c->expected_labels_num; i++) { label_t *l = c->expected_labels + i; - if ((strlen (l->key) == key_len) - && (strncmp (l->key, (char const *) key, key_len) == 0)) - { + if ((strlen(l->key) == key_len) && + (strncmp(l->key, (char const *)key, key_len) == 0)) { c->current_label = l; break; } @@ -76,41 +86,41 @@ static int test_map_key (void *ctx, unsigned char const *key, unsigned int key_l return 1; /* continue */ } -static int expect_label (char const *name, char const *got, char const *want) -{ - _Bool ok = (strcmp (got, want) == 0); +static int expect_label(char const *name, char const *got, char const *want) { + _Bool ok = (strcmp(got, want) == 0); char msg[1024]; if (ok) - snprintf (msg, sizeof (msg), "label[\"%s\"] = \"%s\"", name, got); + snprintf(msg, sizeof(msg), "label[\"%s\"] = \"%s\"", name, got); else - snprintf (msg, sizeof (msg), "label[\"%s\"] = \"%s\", want \"%s\"", name, got, want); + snprintf(msg, sizeof(msg), "label[\"%s\"] = \"%s\", want \"%s\"", name, got, + want); - OK1 (ok, msg); + OK1(ok, msg); return 0; } #if HAVE_YAJL_V2 -static int test_string (void *ctx, unsigned char const *value, size_t value_len) +static int test_string(void *ctx, unsigned char const *value, size_t value_len) #else -static int test_string (void *ctx, unsigned char const *value, unsigned int value_len) +static int test_string(void *ctx, unsigned char const *value, + unsigned int value_len) #endif { test_case_t *c = ctx; - if (c->current_label != NULL) - { + if (c->current_label != NULL) { label_t *l = c->current_label; char *got; int status; - got = malloc (value_len + 1); - memmove (got, value, value_len); + got = malloc(value_len + 1); + memmove(got, value, value_len); got[value_len] = 0; - status = expect_label (l->key, got, l->value); + status = expect_label(l->key, got, l->value); - free (got); + free(got); if (status != 0) return 0; /* abort */ @@ -119,50 +129,54 @@ static int test_string (void *ctx, unsigned char const *value, unsigned int valu return 1; /* continue */ } -static int expect_json_labels (char *json, label_t *labels, size_t labels_num) -{ +static int expect_json_labels(char *json, label_t *labels, size_t labels_num) { yajl_callbacks funcs = { - .yajl_string = test_string, - .yajl_map_key = test_map_key, + .yajl_string = test_string, .yajl_map_key = test_map_key, }; - test_case_t c = { labels, labels_num, NULL }; + test_case_t c = {labels, labels_num, NULL}; yajl_handle hndl; #if HAVE_YAJL_V2 - CHECK_NOT_NULL (hndl = yajl_alloc (&funcs, /* alloc = */ NULL, &c)); + CHECK_NOT_NULL(hndl = yajl_alloc(&funcs, /* alloc = */ NULL, &c)); #else - CHECK_NOT_NULL (hndl = yajl_alloc (&funcs, /* config = */ NULL, /* alloc = */ NULL, &c)); + CHECK_NOT_NULL( + hndl = yajl_alloc(&funcs, /* config = */ NULL, /* alloc = */ NULL, &c)); #endif - OK (yajl_parse (hndl, (unsigned char *) json, strlen (json)) == yajl_status_ok); + OK(yajl_parse(hndl, (unsigned char *)json, strlen(json)) == yajl_status_ok); - yajl_free (hndl); + yajl_free(hndl); return 0; } -DEF_TEST(notification) -{ +DEF_TEST(notification) { label_t labels[] = { - {"summary", "this is a message"}, - {"alertname", "collectd_unit_test"}, - {"instance", "example.com"}, - {"service", "collectd"}, - {"unit", "case"}, + {"summary", "this is a message"}, + {"alertname", "collectd_unit_test"}, + {"instance", "example.com"}, + {"service", "collectd"}, + {"unit", "case"}, }; /* 1448284606.125 ^= 1555083754651779072 */ - notification_t n = { NOTIF_WARNING, 1555083754651779072ULL, "this is a message", - "example.com", "unit", "", "test", "case", NULL }; + notification_t n = {NOTIF_WARNING, + 1555083754651779072ULL, + "this is a message", + "example.com", + "unit", + "", + "test", + "case", + NULL}; char got[1024]; - CHECK_ZERO (format_json_notification (got, sizeof (got), &n)); + CHECK_ZERO(format_json_notification(got, sizeof(got), &n)); // printf ("got = \"%s\";\n", got); - return expect_json_labels (got, labels, STATIC_ARRAY_SIZE (labels)); + return expect_json_labels(got, labels, STATIC_ARRAY_SIZE(labels)); } -int main (void) -{ +int main(void) { RUN_TEST(notification); END_TEST;