summaryrefslogtreecommitdiffstats
path: root/t/test_cee_format.c
blob: d7f886e4f2c10a62adbf668ed675470dc6d72581 (plain)
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
#include "cee-syslog.h"
#include <json.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>

static void
verify_value (struct json_object *jo, const char *key,
              const char *expected_value)
{
  struct json_object *o;
  const char *value;

  o = json_object_object_get (jo, key);

  assert (o != NULL);

  value = json_object_get_string (o);

  assert (value != NULL);
  assert (strcmp (value, expected_value) == 0);
}

static void
verify_value_exists (struct json_object *jo, const char *key)
{
  struct json_object *o;

  o = json_object_object_get (jo, key);
  assert (o != NULL);
}

static void
verify_value_missing (struct json_object *jo, const char *key)
{
  struct json_object *o;

  o = json_object_object_get (jo, key);
  assert (o == NULL);
}

static struct json_object *
parse_msg (const char *msg)
{
  struct json_object *jo;

  jo = json_tokener_parse (msg);
  assert (jo != NULL);

  return jo;
}

static void
test_simple (void)
{
  char *msg;
  struct json_object *jo;

  openlog ("cee-syslog/test_simple", 0, LOG_LOCAL0);

  msg = cee_format (LOG_DEBUG, "hello, I'm %s!", __FUNCTION__, NULL);
  jo = parse_msg (msg);
  free (msg);

  verify_value (jo, "msg", "hello, I'm test_simple!");
  verify_value (jo, "facility", "local0");
  verify_value (jo, "priority", "debug");
  verify_value (jo, "program", "cee-syslog/test_simple");
  verify_value_exists (jo, "pid");
  verify_value_exists (jo, "uid");
  verify_value_exists (jo, "gid");

  json_object_put (jo);

  closelog ();
}

static void
test_no_discover (void)
{
  char *msg;
  struct json_object *jo;

  openlog ("cee-syslog/test_no_discover", LOG_CEE_NODISCOVER, LOG_LOCAL0);

  msg = cee_format (LOG_DEBUG, "hello, I'm %s!", __FUNCTION__, NULL);
  jo = parse_msg (msg);
  free (msg);

  verify_value (jo, "msg", "hello, I'm test_no_discover!");
  verify_value_missing (jo, "facility");
  verify_value_missing (jo, "priority");
  verify_value_missing (jo, "program");
  verify_value_missing (jo, "pid");
  verify_value_missing (jo, "uid");
  verify_value_missing (jo, "gid");

  json_object_put (jo);

  closelog ();
}

static void
test_additional_fields (void)
{
  char *msg;
  struct json_object *jo;

  openlog ("cee-syslog/test_additional_fields", 0, LOG_LOCAL0);

  msg = cee_format (LOG_DEBUG, "testing 1, 2, 3...",
                    "random_number", "%d", 42,
                    "random_string", "fourty-two",
                    NULL);
  jo = parse_msg (msg);
  free (msg);

  verify_value (jo, "msg", "testing 1, 2, 3...");
  verify_value (jo, "random_number", "42");
  verify_value (jo, "random_string", "fourty-two");

  json_object_put (jo);

  closelog ();
}

int
main (void)
{
  test_simple ();
  test_no_discover ();
  test_additional_fields ();

  return 0;
}