summaryrefslogtreecommitdiffstats
path: root/t/test_umberlog_preload.c
blob: 99451c68dea430570682f237af5fc7a27735c162 (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
#define _GNU_SOURCE 1

#include "umberlog.h"
#include "config.h"
#include "test-common.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>

#include <check.h>


/** This must be the first test!
 *
 * This check verifies the openlog() defaults, and that additional
 * fields are properly added.
 */
START_TEST (test_openlog_defaults)
{
  char *msg;
  struct json_object *jo;

  /* No openlog */

  msg = ul_format (LOG_ALERT, "message", NULL);
  jo = parse_msg (msg);
  free (msg);

  verify_value (jo, "facility", "user");
#ifdef HAVE_PROGRAM_INVOCATION_SHORT_NAME
  verify_value (jo, "program", "test_umberlog_preload");
#else
  verify_value_missing (jo, "program");
#endif
  verify_value_differs (jo, "pid", "0");
  if (getuid () != 0)
    verify_value_differs (jo, "uid", "0");
  if (getgid () != 0)
    verify_value_differs (jo, "gid", "0");
  verify_value_differs (jo, "host", "");

  json_object_put (jo);

  closelog ();
}
END_TEST

/**
 * This verifies that adding any LOG_UL_* flags to openlog() will have
 * no effect.
 */
START_TEST (test_openlog_flags)
{
  char *msg;
  struct json_object *jo;
  char host[_POSIX_HOST_NAME_MAX + 1];

  openlog ("umberlog/test_openlog_flags", LOG_UL_NOIMPLICIT, 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_openlog_flags!");
  verify_value (jo, "facility", "local0");
  verify_value (jo, "priority", "debug");
  verify_value (jo, "program", "umberlog/test_openlog_flags");
  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 ();
}
END_TEST

/**
 * A simple test that options that should be respected, are respected.
 */
START_TEST (test_simple)
{
  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 ();
}
END_TEST

int
main (void)
{
  Suite *s;
  SRunner *sr;
  TCase *ft, *bt;
  int nfailed;

  s = suite_create ("Umberlog (LD_PRELOAD) functional testsuite");

#if DEFAULT_DISCOVER_FLAGS == LOG_UL_ALL
  ft = tcase_create ("Basic tests");
  tcase_add_test (ft, test_openlog_defaults);
  tcase_add_test (ft, test_openlog_flags);
  tcase_add_test (ft, test_simple);
  suite_add_tcase (s, ft);
#endif

  sr = srunner_create (s);

  srunner_run_all (sr, CK_ENV);
  nfailed = srunner_ntests_failed (sr);
  srunner_free (sr);

  return (nfailed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}