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
|
/*
* Copyright (c) 2002-2013 Balabit
* Copyright (c) 1998-2013 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 "transport/transport-aux-data.h"
#include "apphook.h"
LogTransportAuxData *aux = NULL;
static LogTransportAuxData *
construct_empty_aux(void)
{
LogTransportAuxData *self = g_new(LogTransportAuxData, 1);
log_transport_aux_data_init(self);
return self;
}
static void
free_aux(LogTransportAuxData *self)
{
log_transport_aux_data_destroy(self);
g_free(self);
}
static LogTransportAuxData *
construct_aux_with_some_data(void)
{
LogTransportAuxData *self = construct_empty_aux();
/* set peer_addr twice to validate that peer_addr is correctly reference counted */
log_transport_aux_data_set_peer_addr_ref(self, g_sockaddr_inet_new("1.2.3.4", 5555));
log_transport_aux_data_set_peer_addr_ref(self, g_sockaddr_inet_new("1.2.3.4", 5555));
log_transport_aux_data_add_nv_pair(self, "foo", "bar");
return self;
}
Test(aux_data, test_aux_data_reinit_returns_aux_into_initial_state_without_leaks)
{
log_transport_aux_data_reinit(aux);
cr_assert_null(aux->peer_addr, "aux->peer_addr is not NULL after reinit");
}
static void
_concat_nvpairs_helper(const gchar *name, const gchar *value, gsize value_len, gpointer user_data)
{
GString *concatenated = (GString *) user_data;
g_string_append_printf(concatenated, "%s=%s\n", name, value);
cr_assert_eq(value_len, strlen(value), "foreach() length mismatch");
}
static gchar *
_concat_nvpairs(LogTransportAuxData *self)
{
GString *concatenated = g_string_sized_new(0);
log_transport_aux_data_foreach(self, _concat_nvpairs_helper, concatenated);
return g_string_free(concatenated, FALSE);
}
static void
assert_concatenated_nvpairs(LogTransportAuxData *self, const gchar *expected)
{
gchar *concatenated = _concat_nvpairs(self);
cr_assert_str_eq(concatenated, expected, "foreach() didn't return all-added nvpairs");
g_free(concatenated);
}
Test(aux_data, test_aux_data_added_nvpairs_are_returned_by_foreach_in_order)
{
log_transport_aux_data_add_nv_pair(aux, "super", "lativus");
assert_concatenated_nvpairs(aux, "foo=bar\nsuper=lativus\n");
}
Test(aux_data, test_aux_data_copy_creates_an_identical_copy)
{
LogTransportAuxData aux_copy;
gchar *orig, *copy;
log_transport_aux_data_copy(&aux_copy, aux);
orig = _concat_nvpairs(aux);
copy = _concat_nvpairs(&aux_copy);
cr_assert_str_eq(orig, copy, "copy incorrectly copied aux->nvpairs");
g_free(orig);
g_free(copy);
log_transport_aux_data_destroy(&aux_copy);
}
Test(aux_data, test_aux_data_copy_separates_the_copies)
{
LogTransportAuxData aux_copy;
gchar *orig, *copy;
log_transport_aux_data_copy(&aux_copy, aux);
log_transport_aux_data_add_nv_pair(aux, "super", "lativus");
orig = _concat_nvpairs(aux);
copy = _concat_nvpairs(&aux_copy);
cr_assert_str_neq(orig, copy,
"copy incorrectly copied aux->nvpairs as change to one of them affected the other, orig=%s, copy=%s", orig, copy);
g_free(orig);
g_free(copy);
log_transport_aux_data_destroy(&aux_copy);
}
Test(aux_data, test_add_nv_pair_to_a_NULL_aux_data_will_do_nothing)
{
log_transport_aux_data_add_nv_pair(NULL, "foo", "bar");
assert_concatenated_nvpairs(NULL, "");
}
Test(aux_data, test_aux_data_functions_with_NULL_instance_does_nothing)
{
aux = NULL;
log_transport_aux_data_init(aux);
log_transport_aux_data_reinit(aux);
log_transport_aux_data_destroy(aux);
log_transport_aux_data_init(aux);
/* set peer_addr twice to validate that peer_addr is correctly reference counted */
log_transport_aux_data_set_peer_addr_ref(aux, g_sockaddr_inet_new("1.2.3.4", 5555));
log_transport_aux_data_set_peer_addr_ref(aux, g_sockaddr_inet_new("1.2.3.4", 5555));
}
static void
setup(void)
{
app_startup();
aux = construct_aux_with_some_data();
}
static void
teardown(void)
{
free_aux(aux);
app_shutdown();
}
TestSuite(aux_data, .init = setup, .fini = teardown);
|