File: proto_lib.c

package info (click to toggle)
syslog-ng 4.8.1-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,572 kB
  • sloc: ansic: 177,639; 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 (184 lines) | stat: -rw-r--r-- 5,658 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
/*
 * Copyright (c) 2012-2019 Balabit
 * Copyright (c) 2012 Balázs Scheidler
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; 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 "proto_lib.h"
#include "grab-logging.h"

#include "cfg.h"
#include <string.h>

LogProtoServerOptions proto_server_options;

void
assert_proto_server_status(LogProtoServer *proto, LogProtoStatus status, LogProtoStatus expected_status)
{
  cr_assert_eq(status, expected_status, "LogProtoServer expected status mismatch");
}

LogProtoStatus
proto_server_fetch(LogProtoServer *proto, const guchar **msg, gsize *msg_len)
{
  Bookmark bookmark;
  LogTransportAuxData aux;
  GSockAddr *saddr;
  gboolean may_read = TRUE;
  LogProtoStatus status;

  start_grabbing_messages();
  do
    {
      log_transport_aux_data_init(&aux);
      status = log_proto_server_fetch(proto, msg, msg_len, &may_read, &aux, &bookmark);
      if (status == LPS_AGAIN)
        status = LPS_SUCCESS;
    }
  while (status == LPS_SUCCESS && *msg == NULL && may_read);

  saddr = aux.peer_addr;
  if (status == LPS_SUCCESS)
    {
      g_sockaddr_unref(saddr);
    }
  else
    {
      cr_assert_null(saddr, "returned saddr must be NULL on failure");
    }
  stop_grabbing_messages();
  return status;
}

LogProtoServer *
construct_server_proto_plugin(const gchar *name, LogTransport *transport)
{
  LogProtoServerFactory *proto_factory;

  log_proto_server_options_init(&proto_server_options, configuration);
  proto_factory = log_proto_server_get_factory(&configuration->plugin_context, name);
  cr_assert_not_null(proto_factory, "error looking up proto factory");
  return log_proto_server_factory_construct(proto_factory, transport, &proto_server_options);
}

void
assert_proto_server_fetch(LogProtoServer *proto, const gchar *expected_msg, gssize expected_msg_len)
{
  const guchar *msg = NULL;
  gsize msg_len = 0;
  LogProtoStatus status;

  status = proto_server_fetch(proto, &msg, &msg_len);

  assert_proto_server_status(proto, status, LPS_SUCCESS);

  if (expected_msg_len < 0)
    expected_msg_len = strlen(expected_msg);

  cr_assert_eq(msg_len, expected_msg_len, "LogProtoServer expected message mismatch (length) "
               "actual: %" G_GSIZE_FORMAT " expected: %" G_GSIZE_FORMAT, msg_len, expected_msg_len);
  cr_assert_arr_eq((const gchar *) msg, expected_msg, expected_msg_len,
                   "LogProtoServer expected message mismatch");
}

void
assert_proto_server_fetch_single_read(LogProtoServer *proto, const gchar *expected_msg, gssize expected_msg_len)
{
  const guchar *msg = NULL;
  gsize msg_len = 0;
  LogProtoStatus status;
  LogTransportAuxData aux;
  Bookmark bookmark;
  gboolean may_read = TRUE;

  start_grabbing_messages();
  log_transport_aux_data_init(&aux);
  status = log_proto_server_fetch(proto, &msg, &msg_len, &may_read, &aux, &bookmark);
  assert_proto_server_status(proto, status, LPS_SUCCESS);

  if (expected_msg)
    {
      if (expected_msg_len < 0)
        expected_msg_len = strlen(expected_msg);

      cr_assert_eq(msg_len, expected_msg_len, "LogProtoServer expected message mismatch (length)");
      cr_assert_arr_eq((const gchar *) msg, expected_msg, expected_msg_len,
                       "LogProtoServer expected message mismatch");
    }
  else
    {
      cr_assert_null(msg, "when single-read finds an incomplete message, msg must be NULL");
      cr_assert_null(aux.peer_addr, "returned saddr must be NULL on success");
    }
  stop_grabbing_messages();
}

void
assert_proto_server_fetch_failure(LogProtoServer *proto, LogProtoStatus expected_status, const gchar *error_message)
{
  const guchar *msg = NULL;
  gsize msg_len = 0;
  LogProtoStatus status;

  status = proto_server_fetch(proto, &msg, &msg_len);

  assert_proto_server_status(proto, status, expected_status);
  if (error_message)
    assert_grabbed_log_contains(error_message);
}

void
assert_proto_server_fetch_ignored_eof(LogProtoServer *proto)
{
  const guchar *msg = NULL;
  gsize msg_len = 0;
  LogProtoStatus status;
  LogTransportAuxData aux;
  Bookmark bookmark;
  gboolean may_read = TRUE;

  start_grabbing_messages();
  log_transport_aux_data_init(&aux);
  status = log_proto_server_fetch(proto, &msg, &msg_len, &may_read, &aux, &bookmark);
  if (status == LPS_AGAIN)
    status = LPS_SUCCESS;
  assert_proto_server_status(proto, status, LPS_SUCCESS);
  cr_assert_null(msg, "when an EOF is ignored msg must be NULL");
  cr_assert_null(aux.peer_addr, "returned saddr must be NULL on success");
  stop_grabbing_messages();
}

void
init_proto_tests(void)
{
  configuration = cfg_new_snippet();
  log_proto_server_options_defaults(&proto_server_options);
}

void
deinit_proto_tests(void)
{
  log_proto_server_options_destroy(&proto_server_options);

  if (configuration)
    cfg_free(configuration);
}