summaryrefslogtreecommitdiffstats
path: root/t/test_umberlog.c
blob: 33ed8bf45aacc501723b2f2690db8aa372eb65f3 (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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#define _GNU_SOURCE 1

#include "umberlog.h"
#include <json.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <limits.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;
  char host[_POSIX_HOST_NAME_MAX + 1];

  openlog ("umberlog/test_simple", 0, LOG_LOCAL0);

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

  gethostname (host, _POSIX_HOST_NAME_MAX);

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

  json_object_put (jo);

  closelog ();
}

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

  openlog ("umberlog/test_no_discover", LOG_UL_NODISCOVER, LOG_LOCAL0);

  msg = ul_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");
  verify_value_missing (jo, "host");
  verify_value_missing (jo, "timestamp");

  json_object_put (jo);

  closelog ();
}

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

  openlog ("umberlog/test_additional_fields", 0, LOG_LOCAL0);

  msg = ul_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 ();
}

static void
test_discover_priority (void)
{
  char *msg, *pid;
  struct json_object *jo;

  openlog ("umberlog/test_discover_priority", 0, LOG_LOCAL0);

  msg = ul_format (LOG_DEBUG, "testing 1, 2, 3...",
                   "pid", "%d", getpid () + 42,
                   NULL);
  jo = parse_msg (msg);
  free (msg);

  verify_value (jo, "msg", "testing 1, 2, 3...");

  if (asprintf (&pid, "%d", getpid ()) == -1)
    abort ();
  verify_value (jo, "pid", pid);
  free (pid);

  json_object_put (jo);

  closelog ();
}

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

  openlog ("umberlog/test_no_timestamp", LOG_UL_NOTIME, LOG_LOCAL0);

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

  verify_value (jo, "msg", "hello, I'm test_no_timestamp!");
  verify_value (jo, "facility", "local0");
  verify_value (jo, "priority", "debug");
  verify_value (jo, "program", "umberlog/test_no_timestamp");
  verify_value_exists (jo, "pid");
  verify_value_exists (jo, "uid");
  verify_value_exists (jo, "gid");
  verify_value_missing (jo, "timestamp");
  verify_value_exists (jo, "host");

  json_object_put (jo);

  closelog ();
}

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

  openlog ("umberlog/test_json_escape", LOG_UL_NODISCOVER, LOG_LOCAL0);

  msg = ul_format (LOG_DEBUG, "%s", __FUNCTION__,
                   "quotes", "Hi, \"quoted value\" speaking!",
                   "\"really\"", "yeah",
                   "control", "foo\nbar",
                   "utf8", "Árvíztűrő tükörfúrógép",
                   "junk", "\013foo",
                   NULL);
  jo = parse_msg (msg);
  free (msg);

  verify_value (jo, "quotes", "Hi, \"quoted value\" speaking!");
  verify_value (jo, "\"really\"", "yeah");
  verify_value (jo, "control", "foo\nbar");
  verify_value (jo, "utf8", "Árvíztűrő tükörfúrógép");
  verify_value (jo, "junk", "\013foo");

  json_object_put (jo);

  closelog ();
}

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

  msg = ul_format (LOG_LOCAL1 | LOG_DEBUG, "%s", __FUNCTION__,
                   NULL);
  jo = parse_msg (msg);
  free (msg);

  verify_value (jo, "facility", "local1");
  verify_value (jo, "priority", "debug");

  json_object_put (jo);
}

int
main (void)
{
  test_simple ();
  test_no_discover ();
  test_additional_fields ();
  test_discover_priority ();
  test_no_timestamp ();
  test_json_escape ();
  test_facprio ();

  return 0;
}