File: syslog.c

package info (click to toggle)
dbus 1.16.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,048 kB
  • sloc: ansic: 106,015; xml: 9,270; sh: 1,967; python: 242; makefile: 230; cpp: 27
file content (120 lines) | stat: -rw-r--r-- 3,976 bytes parent folder | download
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
/* Manual regression test for syslog support
 *
 * Author: Simon McVittie <simon.mcvittie@collabora.co.uk>
 * Copyright © 2011 Nokia Corporation
 * SPDX-License-Identifier: MIT
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation files
 * (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge,
 * publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include <config.h>

#include <stdlib.h>

#include <glib.h>

#include <dbus/dbus.h>
#include <dbus/dbus-sysdeps.h>

#include "test-utils-glib.h"

typedef struct {
    int dummy;
} Fixture;

static void
setup (Fixture *f,
    gconstpointer data)
{
}

/* hopefully clear enough that people don't think these messages in syslog
 * are a bug */
#define MESSAGE "regression test for _dbus_log(): "

static void
test_syslog_normal (Fixture *f,
    gconstpointer data)
{
  if (g_test_subprocess ())
    {
      _dbus_init_system_log ("test-syslog",
          DBUS_LOG_FLAGS_SYSTEM_LOG | DBUS_LOG_FLAGS_STDERR);
      _dbus_log (DBUS_SYSTEM_LOG_INFO, MESSAGE "%d", 42);
      _dbus_log (DBUS_SYSTEM_LOG_WARNING, MESSAGE "%d", 45);
      _dbus_log (DBUS_SYSTEM_LOG_SECURITY, MESSAGE "%d", 666);
      _dbus_log (DBUS_SYSTEM_LOG_ERROR, MESSAGE "%d", 23);

      _dbus_init_system_log ("test-syslog-stderr", DBUS_LOG_FLAGS_STDERR);
      _dbus_log (DBUS_SYSTEM_LOG_INFO,
          MESSAGE "this should not appear in the syslog");
      _dbus_init_system_log ("test-syslog-both",
          DBUS_LOG_FLAGS_SYSTEM_LOG | DBUS_LOG_FLAGS_STDERR);
      _dbus_log (DBUS_SYSTEM_LOG_INFO,
          MESSAGE "this should appear in the syslog and on stderr");
      _dbus_init_system_log ("test-syslog-only", DBUS_LOG_FLAGS_SYSTEM_LOG);
      _dbus_log (DBUS_SYSTEM_LOG_INFO,
          MESSAGE "this should appear in the syslog only");

      exit (0);
    }

  g_test_trap_subprocess (NULL, 0, 0);
  g_test_trap_assert_passed ();
  /* Deliberately not matching newlines: in Windows they might come out as
   * either \n or \r\n, perhaps depending on GLib version or on whether
   * we're using Wine or real Windows. */
  g_test_trap_assert_stderr ("*" MESSAGE "42"
                             "*" MESSAGE "45"
                             "*" MESSAGE "666"
                             "*" MESSAGE "23"
                             "*test-syslog-stderr*" MESSAGE
                               "this should not appear in the syslog"
                             "*test-syslog-both*" MESSAGE
                               "this should appear in the syslog and "
                               "on stderr"
                             "*");
  g_test_trap_assert_stderr_unmatched ("*this should appear in the syslog "
                                       "only*");
  g_test_trap_assert_stderr_unmatched ("*test-syslog-only*");
}

static void
teardown (Fixture *f,
    gconstpointer data)
{
}

int
main (int argc,
    char **argv)
{
  int ret;

  test_init (&argc, &argv);

  g_test_add ("/syslog/normal", Fixture, NULL, setup, test_syslog_normal,
              teardown);

  ret = g_test_run ();
  dbus_shutdown ();
  return ret;
}