File: test-source.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 (193 lines) | stat: -rw-r--r-- 5,271 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
/*
 * 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 <criterion/criterion.h>

#include "test-source.h"
#include "syslog-ng.h"
#include "stats/stats.h"

struct _TestSource
{
  LogPipe super;
  JournalReaderOptions options;
  JournalReader *reader;
  GList *tests;
  GList *current_test;
  TestCase *current_test_case;

  struct iv_task start;
  struct iv_task stop;
  struct iv_task work;
};

static gboolean
__init(LogPipe *s)
{
  TestSource *self = (TestSource *)s;
  GlobalConfig *cfg = log_pipe_get_config(s);

  self->reader = journal_reader_new(cfg);
  journal_reader_options_defaults(&self->options);
  if (self->current_test_case && self->current_test_case->init)
    {
      self->current_test_case->init(self->current_test_case, self, self->reader, &self->options);
    }
  journal_reader_options_init(&self->options, cfg, "test");
  journal_reader_set_options((LogPipe *)self->reader, &self->super, &self->options, "test", NULL);
  log_pipe_append((LogPipe *)self->reader, &self->super);
  cr_assert(log_pipe_init((LogPipe *)self->reader), "%s", "Can't initialize reader");
  return TRUE;
}

static gboolean
__deinit(LogPipe *s)
{
  TestSource *self = (TestSource *)s;
  log_pipe_deinit((LogPipe *)self->reader);
  log_pipe_unref((LogPipe *)self->reader);
  journal_reader_options_destroy(&self->options);
  return TRUE;
}

static void
__free(LogPipe *s)
{
  TestSource *self = (TestSource *)s;
  g_list_free(self->tests);
}

static void
__queue(LogPipe *s, LogMessage *msg, const LogPathOptions *path_options)
{
  TestSource *self = (TestSource *)s;
  if (self->current_test_case && self->current_test_case->checker)
    {
      self->current_test_case->checker(self->current_test_case, self, msg);
    }
  else
    {
      iv_quit();
    }
  log_msg_drop(msg, path_options, AT_PROCESSED);
}

static void
__start_source(gpointer user_data)
{
  TestSource *self = (TestSource *)user_data;
  log_pipe_init(&self->super);
}

static void
__stop_source(gpointer user_data)
{
  TestSource *self = (TestSource *)user_data;
  log_pipe_deinit(&self->super);
  iv_quit();
}

TestSource *
test_source_new(GlobalConfig *cfg)
{
  TestSource *self = g_new0(TestSource, 1);
  log_pipe_init_instance(&self->super, cfg);
  self->super.init = __init;
  self->super.deinit = __deinit;
  self->super.free_fn = __free;
  self->super.queue = __queue;
  journal_reader_options_defaults(&self->options);

  IV_TASK_INIT(&self->start);
  self->start.cookie = self;
  self->start.handler = __start_source;
  IV_TASK_INIT(&self->stop);
  self->stop.cookie = self;
  self->stop.handler = __stop_source;
  return self;
}

void
test_source_add_test_case(TestSource *self, TestCase *tc)
{
  self->tests = g_list_append(self->tests, tc);
}

void
test_source_run_tests(TestSource *self)
{
  self->current_test = self->tests;
  while (self->current_test)
    {
      self->current_test_case = self->current_test->data;
      iv_task_register(&self->start);
      iv_main();
      log_pipe_deinit(&self->super);
      self->current_test = self->current_test->next;
    }
}

void
test_source_finish_tc(TestSource *self)
{
  if (self->current_test_case && self->current_test_case->finish)
    {
      self->current_test_case->finish(self->current_test_case);
    }
  iv_task_register(&self->stop);
}

void *
journal_reader_test_prepare_with_namespace(const gchar *namespace, GlobalConfig *cfg)
{
  TestSource *test = g_new0(TestSource, 1);

  test->reader = journal_reader_new(cfg);
  log_pipe_init_instance(&test->super, cfg);
  journal_reader_options_defaults(&test->options);
  journal_reader_options_init(&test->options, cfg, "namespace_test");
  journal_reader_set_options((LogPipe *)test->reader, &test->super, &test->options, "namespace_test", NULL);
  log_pipe_append((LogPipe *)test->reader, &test->super);

  if (test->options.namespace) g_free(test->options.namespace);
  test->options.namespace = g_strdup(namespace);

  return test;
}

gboolean
journal_reader_test_allocate_namespace(void *test)
{
  TestSource *self = (TestSource *)test;
  return log_pipe_init((LogPipe *)self->reader);
}

void
journal_reader_test_destroy(void *test)
{
  TestSource *self = (TestSource *)test;
  log_pipe_deinit((LogPipe *)self->reader);
  log_pipe_unref((LogPipe *)self->reader);
  journal_reader_options_destroy(&self->options);
  g_free(test);
  test = NULL;
}