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
|
/*
* Copyright (c) 2013-2015 Balabit
* Copyright (c) 2013 Juhász Viktor <jviktor@balabit.hu>
*
* 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 "messages.h"
#include "control/control.h"
#include "control/control-commands.h"
#include "control/control-connection.h"
#include "control-server-dummy.h"
#include "mainloop-control.h"
#include "stats/stats-control.h"
#include "control/control-server.h"
#include "stats/stats-cluster.h"
#include "stats/stats-registry.h"
#include "apphook.h"
#include "cfg-path.h"
ControlServer *control_server;
ControlConnection *control_connection;
MainLoop *main_loop;
void
setup(void)
{
MainLoopOptions main_loop_options = {0};
app_startup();
main_loop = main_loop_get_instance();
main_loop_init(main_loop, &main_loop_options);
main_loop_register_control_commands(main_loop);
stats_register_control_commands();
control_server = control_server_dummy_new();
control_connection = control_connection_dummy_new(control_server);
control_connection_start_watches(control_connection);
}
void
teardown(void)
{
control_server_connection_closed(control_server, control_connection);
control_server_free(control_server);
main_loop_deinit(main_loop);
app_shutdown();
reset_control_command_list();
}
TestSuite(control_cmds, .init = setup, .fini = teardown);
static void
_send_request(const gchar *request)
{
control_connection_dummy_set_input(control_connection, request);
control_connection_dummy_reset_output(control_connection);
}
static void
_fetch_response(const gchar **response)
{
*response = control_connection_dummy_get_output(control_connection);
}
static void
_run_command(const gchar *request, const gchar **response)
{
_send_request(request);
control_connection->handle_input(control_connection);
_fetch_response(response);
}
static gboolean
first_line_eq(const gchar *buf, const gchar *expected)
{
const gchar *nl = strchr(buf, '\n');
return strncmp(buf, expected, nl - buf) == 0;
}
Test(control_cmds, test_listfiles)
{
const gchar *response;
const gchar *db_file = "/opt/syslog-ng/var/db/patterndb.xml";
GString *expected = g_string_new("");
cfg_path_track_file(main_loop_get_current_config(main_loop), db_file, "path_check");
_run_command("LISTFILES", &response);
g_string_printf(expected, "path_check: %s", db_file);
cr_assert(first_line_eq(response, expected->str), "Bad reply: [%s]", response);
g_string_free(expected, TRUE);
}
Test(control_cmds, test_log)
{
const gchar *response;
_run_command("LOG", &response);
cr_assert(first_line_eq(response, "FAIL Invalid arguments received"),
"Bad reply: [%s]", response);
_run_command("LOG fakelog", &response);
cr_assert(first_line_eq(response, "FAIL Invalid arguments received"),
"Bad reply: [%s]", response);
msg_set_log_level(0);
_run_command("LOG VERBOSE", &response);
cr_assert(first_line_eq(response, "OK syslog-ng log level set to 0"),
"Bad reply: [%s]", response);
_run_command("LOG VERBOSE ON", &response);
cr_assert(first_line_eq(response, "OK syslog-ng log level set to 1"),
"Bad reply: [%s]", response);
cr_assert_eq(verbose_flag, 1, "Flag isn't changed");
_run_command("LOG VERBOSE OFF", &response);
cr_assert(first_line_eq(response, "OK syslog-ng log level set to 0"),
"Bad reply: [%s]", response);
cr_assert_eq(verbose_flag, 0, "Flag isn't changed");
msg_set_log_level(1);
_run_command("LOG DEBUG", &response);
cr_assert(first_line_eq(response, "OK syslog-ng log level set to 1"),
"Bad reply: [%s]", response);
_run_command("LOG DEBUG ON", &response);
cr_assert(first_line_eq(response, "OK syslog-ng log level set to 2"),
"Bad reply: [%s]", response);
cr_assert_eq(debug_flag, 1, "Flag isn't changed");
_run_command("LOG DEBUG OFF", &response);
cr_assert(first_line_eq(response, "OK syslog-ng log level set to 1"),
"Bad reply: [%s]", response);
cr_assert_eq(debug_flag, 0, "Flag isn't changed");
msg_set_log_level(2);
_run_command("LOG TRACE", &response);
cr_assert(first_line_eq(response, "OK syslog-ng log level set to 2"),
"Bad reply: [%s]", response);
_run_command("LOG TRACE ON", &response);
cr_assert(first_line_eq(response, "OK syslog-ng log level set to 3"),
"Bad reply: [%s]", response);
cr_assert_eq(trace_flag, 1, "Flag isn't changed");
_run_command("LOG TRACE OFF", &response);
cr_assert(first_line_eq(response, "OK syslog-ng log level set to 2"),
"Bad reply: [%s]", response);
cr_assert_eq(trace_flag, 0, "Flag isn't changed");
}
Test(control_cmds, test_log_level)
{
const gchar *response;
msg_set_log_level(0);
_run_command("LOG LEVEL foo", &response);
cr_assert(first_line_eq(response, "FAIL Invalid arguments received"),
"Bad reply: [%s]", response);
msg_set_log_level(0);
_run_command("LOG LEVEL debug", &response);
cr_assert(first_line_eq(response, "OK syslog-ng log level set to 2"),
"Bad reply: [%s]", response);
msg_set_log_level(1);
_run_command("LOG LEVEL", &response);
cr_assert(first_line_eq(response, "OK syslog-ng log level set to 1"),
"Bad reply: [%s]", response);
}
Test(control_cmds, test_stats)
{
StatsCounterItem *counter = NULL;
gchar **stats_result;
const gchar *response;
stats_lock();
StatsClusterKey sc_key;
stats_cluster_logpipe_key_legacy_set(&sc_key, SCS_CENTER, "id", "received" );
stats_register_counter(0, &sc_key, SC_TYPE_PROCESSED, &counter);
stats_unlock();
_run_command("STATS", &response);
stats_result = g_strsplit(response, "\n", 2);
cr_assert_str_eq(stats_result[0], "SourceName;SourceId;SourceInstance;State;Type;Number",
"Bad reply");
g_strfreev(stats_result);
}
Test(control_cmds, test_reset_stats)
{
StatsCounterItem *counter = NULL;
const gchar *response;
stats_lock();
StatsClusterKey sc_key;
stats_cluster_logpipe_key_legacy_set(&sc_key, SCS_CENTER, "id", "received" );
stats_register_counter(0, &sc_key, SC_TYPE_PROCESSED, &counter);
stats_counter_set(counter, 666);
stats_unlock();
_run_command("RESET_STATS", &response);
cr_assert(first_line_eq(response, "OK The statistics of syslog-ng have been reset to 0."), "Bad reply");
_run_command("STATS", &response);
cr_assert_str_eq(response,
"SourceName;SourceId;SourceInstance;State;Type;Number\ncenter;id;received;a;processed;0\n.\n",
"Bad reply");
}
static void
_original_replace(ControlConnection *cc, GString *result, gpointer user_data, gboolean *cancelled)
{
}
static void
_new_replace(ControlConnection *cc, GString *result, gpointer user_data, gboolean *cancelled)
{
}
static void
_assert_control_command_eq(ControlCommand *cmd, ControlCommand *cmd_other)
{
cr_assert_eq(cmd->func, cmd_other->func);
cr_assert_str_eq(cmd->command_name, cmd_other->command_name);
cr_assert_eq(cmd->user_data, cmd_other->user_data);
}
Test(control_cmds, test_replace_existing_command)
{
control_register_command("REPLACE", _original_replace, (gpointer)0xbaadf00d, FALSE);
ControlCommand *cmd = control_find_command("REPLACE");
ControlCommand expected_original =
{
.func = _original_replace,
.command_name = "REPLACE",
.user_data = (gpointer)0xbaadf00d
};
_assert_control_command_eq(cmd, &expected_original);
control_replace_command("REPLACE", _new_replace, (gpointer)0xd006f00d, FALSE);
ControlCommand *new_cmd = control_find_command("REPLACE");
ControlCommand expected_new =
{
.func = _new_replace,
.command_name = "REPLACE",
.user_data = (gpointer) 0xd006f00d
};
_assert_control_command_eq(new_cmd, &expected_new);
}
Test(control_cmds, test_replace_non_existing_command)
{
control_replace_command("REPLACE", _new_replace, (gpointer)0xd006f00d, FALSE);
ControlCommand *new_cmd = control_find_command("REPLACE");
ControlCommand expected_new =
{
.func = _new_replace,
.command_name = "REPLACE",
.user_data = (gpointer) 0xd006f00d
};
_assert_control_command_eq(new_cmd, &expected_new);
}
|