File: journald-mock.c

package info (click to toggle)
syslog-ng 4.8.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,456 kB
  • sloc: ansic: 177,631; python: 13,035; cpp: 11,611; makefile: 7,012; sh: 5,147; java: 3,651; xml: 3,344; yacc: 1,377; lex: 599; perl: 193; awk: 190; objc: 162
file content (351 lines) | stat: -rw-r--r-- 9,108 bytes parent folder | download | duplicates (2)
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
 * Copyright (c) 2010-2014 Balabit
 * Copyright (c) 2010-2014 Viktor Juhasz <viktor.juhasz@balabit.com>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published
 * by the Free Software Foundation, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * As an additional exemption you are allowed to compile & link against the
 * OpenSSL libraries as published by the OpenSSL project. See the file
 * COPYING for details.
 *
 */

#include "journald-mock.h"
#include "fdhelpers.h"

#include <fcntl.h>
#include <unistd.h>
#include <glib.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>

#if SYSLOG_NG_SYSTEMD_JOURNAL_MODE == SYSLOG_NG_JOURNALD_SYSTEM
# define MOCK_FUNC(f) f
# define MOCK_STATIC
#else
# define MOCK_FUNC(f) _##f
# define MOCK_STATIC static
#endif /* #if SYSLOG_NG_SYSTEMD_JOURNAL_MODE == SYSLOG_NG_JOURNALD_SYSTEM */

typedef struct _sd_journal_mock sd_journal_mock;
typedef struct _MockJournal MockJournal;

static void sd_journal_mock_journal_updated(sd_journal_mock *self);

struct _MockEntry
{
  GPtrArray *data;
  gint index;
  gchar *cursor;
};

MockEntry *
mock_entry_new(const gchar *cursor)
{
  MockEntry *self = g_new0(MockEntry, 1);
  self->cursor = g_strdup(cursor);
  self->data = g_ptr_array_new();
  return self;
}

void
mock_entry_add_data(MockEntry *self, gchar *data)
{
  g_ptr_array_add(self->data, data);
}

void
mock_entry_free(gpointer s)
{
  MockEntry *self = (MockEntry *)s;
  g_free(self->cursor);
  g_ptr_array_free(self->data, TRUE);
  g_free(self);
}

struct _MockJournal
{
  GList *entries;
};

static MockJournal journal_contents;
static sd_journal_mock *sd_journal_under_test = NULL;

void
mock_journal_add_entry(MockEntry *entry)
{
  journal_contents.entries = g_list_append(journal_contents.entries, entry);

  /* if sd_journal_under_test is NULL, it means we are just collecting journal elements */
  if (sd_journal_under_test)
    sd_journal_mock_journal_updated(sd_journal_under_test);
}

void
mock_journal_free(void)
{
  g_list_free_full(journal_contents.entries, mock_entry_free);
  journal_contents.entries = NULL;
}

struct _sd_journal_mock
{
  int fds[2];
  MockJournal *journal_contents;
  GList *current_pos;
  GList *next_element;
  gboolean opened;
};

static sd_journal_mock *
sd_journal_mock_new(void)
{
  sd_journal_mock *self = g_new0(sd_journal_mock, 1);

  int result = pipe(self->fds);
  g_assert(result == 0);
  g_fd_set_nonblock(self->fds[0], TRUE);
  g_fd_set_nonblock(self->fds[1], TRUE);

  g_assert(sd_journal_under_test == NULL);
  sd_journal_under_test = self;

  self->journal_contents = &journal_contents;
  return self;
}

static void
sd_journal_mock_journal_updated(sd_journal_mock *self)
{
  gboolean first_element = g_list_length(self->journal_contents->entries) == 1;
  gint res = write(self->fds[1], "1", 1);
  if (res < 0)
    {
      fprintf(stderr, "JournaldMOCK: Can't write the pipe's fd: %s\n", strerror(errno));
    }
  if (first_element && !self->next_element)
    {
      self->next_element = self->journal_contents->entries;
    }
}


MOCK_STATIC int
MOCK_FUNC(sd_journal_open)(sd_journal **s, int flags)
{
  sd_journal_mock *self = (sd_journal_mock *) sd_journal_mock_new();
  self->opened = TRUE;
  *s = (sd_journal *) self;
  return 0;
}

MOCK_STATIC int
MOCK_FUNC(sd_journal_open_namespace)(sd_journal **s, const gchar *namespace, int flags)
{
  return sd_journal_open(s, flags);
}

MOCK_STATIC void
MOCK_FUNC(sd_journal_close)(sd_journal *s)
{
  sd_journal_mock *self = (sd_journal_mock *) s;
  self->opened = FALSE;
  close(self->fds[0]);
  close(self->fds[1]);
  g_free(self);

  g_assert(sd_journal_under_test == self);
  sd_journal_under_test = NULL;
}

MOCK_STATIC int
MOCK_FUNC(sd_journal_seek_head)(sd_journal *s)
{
  sd_journal_mock *self = (sd_journal_mock *) s;

  g_assert(self->opened);
  self->next_element = g_list_first(self->journal_contents->entries);
  return 0;
}

MOCK_STATIC int
MOCK_FUNC(sd_journal_seek_tail)(sd_journal *s)
{
  sd_journal_mock *self = (sd_journal_mock *) s;

  g_assert(self->opened);
  self->next_element = g_list_last(self->journal_contents->entries);
  return 0;
}

MOCK_STATIC int
MOCK_FUNC(sd_journal_get_cursor)(sd_journal *s, gchar **cursor)
{
  sd_journal_mock *self = (sd_journal_mock *) s;

  g_assert(self->opened);
  MockEntry *entry = (MockEntry *)self->current_pos->data;
  *cursor = g_strdup(entry->cursor);
  return 0;
}

MOCK_STATIC int
MOCK_FUNC(sd_journal_next)(sd_journal *s)
{
  sd_journal_mock *self = (sd_journal_mock *) s;

  g_assert(self->opened);
  self->current_pos = self->next_element;
  if (self->current_pos)
    {
      self->next_element = self->current_pos->next;
      return 1;
    }
  return 0;
}

MOCK_STATIC void
MOCK_FUNC(sd_journal_restart_data)(sd_journal *s)
{
  sd_journal_mock *self = (sd_journal_mock *) s;

  g_assert(self->opened);
  MockEntry *entry = (MockEntry *)self->current_pos->data;
  entry->index = 0;
}

MOCK_STATIC int
MOCK_FUNC(sd_journal_enumerate_data)(sd_journal *s, const void **data, gsize *length)
{
  sd_journal_mock *self = (sd_journal_mock *) s;

  g_assert(self->opened);
  MockEntry *entry = (MockEntry *)self->current_pos->data;
  if (entry->index >= entry->data->len)
    {
      return 0;
    }
  *data = entry->data->pdata[entry->index];
  *length = strlen(*data);
  entry->index++;
  return 1;
}

static gint
_compare_mock_entries(gconstpointer a, gconstpointer b)
{
  const MockEntry *entry = a;
  const gchar *cursor = b;
  return strcmp(entry->cursor, cursor);
}

MOCK_STATIC int
MOCK_FUNC(sd_journal_seek_cursor)(sd_journal *s, const gchar *cursor)
{
  sd_journal_mock *self = (sd_journal_mock *) s;

  g_assert(self->opened);
  GList *found_element = g_list_find_custom(self->journal_contents->entries, cursor, _compare_mock_entries);
  if (found_element)
    {
      self->next_element = found_element;
      return 0;
    }
  else
    {
      errno = ENOENT;
      return -ENOENT;
    }
}

MOCK_STATIC int
MOCK_FUNC(sd_journal_test_cursor)(sd_journal *self, const gchar *cursor)
{
  return 1;
}

MOCK_STATIC int
MOCK_FUNC(sd_journal_get_fd)(sd_journal *s)
{
  sd_journal_mock *self = (sd_journal_mock *) s;
  g_assert(self->opened);
  return self->fds[0];
}

MOCK_STATIC int
MOCK_FUNC(sd_journal_process)(sd_journal *s)
{
  sd_journal_mock *self = (sd_journal_mock *) s;

  g_assert(self->opened);
  guint8 data;
  gint res = 1;
  while(res > 0)
    {
      res = read(self->fds[0], &data, 1);
      if (res < 0 && errno != EAGAIN)
        {
          fprintf(stderr, "JournaldMOCK: Can't read the pipe's fd: %s\n", strerror(errno));
        }
    }
  return 0;
}

MOCK_STATIC int
MOCK_FUNC(sd_journal_get_realtime_usec)(sd_journal *s, guint64 *usec)
{
  *usec = 1408967385496986;
  return 0;
}

MOCK_STATIC int
MOCK_FUNC(sd_journal_add_match)(sd_journal *s, const void *match, size_t match_len)
{
  return 0;
}

MOCK_STATIC char *
MOCK_FUNC(sd_id128_to_string)(sd_id128_t id, char s[SD_ID128_STRING_MAX])
{
  return s;
}

MOCK_STATIC int
MOCK_FUNC(sd_id128_get_boot)(sd_id128_t *ret)
{
  return 0;
}

#if SYSLOG_NG_SYSTEMD_JOURNAL_MODE != SYSLOG_NG_JOURNALD_SYSTEM

int (*sd_journal_open)(sd_journal **ret, int flags) = _sd_journal_open;
int (*sd_journal_open_namespace)(sd_journal **ret, const char *namespace, int flags) = _sd_journal_open_namespace;
void (*sd_journal_close)(sd_journal *j) = _sd_journal_close;
int (*sd_journal_seek_head)(sd_journal *j) = _sd_journal_seek_head;
int (*sd_journal_seek_tail)(sd_journal *j) = _sd_journal_seek_tail;
int (*sd_journal_get_cursor)(sd_journal *j, char **cursor) = _sd_journal_get_cursor;
int (*sd_journal_next)(sd_journal *j) = _sd_journal_next;
void (*sd_journal_restart_data)(sd_journal *j) = _sd_journal_restart_data;
int (*sd_journal_enumerate_data)(sd_journal *j, const void **data, size_t *length) = _sd_journal_enumerate_data;
int (*sd_journal_seek_cursor)(sd_journal *j, const char *cursor) = _sd_journal_seek_cursor;
int (*sd_journal_test_cursor)(sd_journal *j, const char *cursor) = _sd_journal_test_cursor;
int (*sd_journal_get_fd)(sd_journal *j) = _sd_journal_get_fd;
int (*sd_journal_process)(sd_journal *j) = _sd_journal_process;
int (*sd_journal_get_realtime_usec)(sd_journal *j, uint64_t *usec) = _sd_journal_get_realtime_usec;
int (*sd_journal_add_match)(sd_journal *j, const void *data, size_t size) = _sd_journal_add_match;
char *(*sd_id128_to_string)(sd_id128_t id, char s[SD_ID128_STRING_MAX]) = _sd_id128_to_string;
int (*sd_id128_get_boot)(sd_id128_t *ret) = _sd_id128_get_boot;

#endif // #if SYSLOG_NG_SYSTEMD_JOURNAL_MODE == SYSLOG_NG_JOURNALD_SYSTEM