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
|
/*
* Copyright (c) 2002-2013 Balabit
* Copyright (c) 2013 Viktor Juhasz
* Copyright (c) 2013 Viktor Tusa
*
* 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 "testutils.h"
#include "syslog-ng.h"
#include "apphook.h"
#include "rcptid.h"
#include "libtest/persist_lib.h"
#include <unistd.h>
gboolean verbose = FALSE;
PersistState *state;
void
setup_persist_id_test()
{
state = clean_and_create_persist_state_for_test("test_values.persist");
rcptid_init(state, TRUE);
}
void
teardown_persist_id_test()
{
commit_and_destroy_persist_state(state);
rcptid_deinit();
}
static void
test_rcptid_is_persistent_across_persist_backend_reinits(void)
{
guint64 rcptid;
setup_persist_id_test();
rcptid_set_id(0xFFFFFFFFFFFFFFFE);
rcptid = rcptid_generate_id();
assert_guint64(rcptid, 0xFFFFFFFFFFFFFFFE, "Rcptid initialization to specific value failed!");
state = restart_persist_state(state);
rcptid_deinit();
rcptid_init(state, TRUE);
rcptid = rcptid_generate_id();
assert_guint64(rcptid, 0xFFFFFFFFFFFFFFFF, "Rcptid did not persisted across persist backend reinit!");
teardown_persist_id_test();
}
static void
test_rcptid_overflows_at_64bits_and_is_reset_to_one(void)
{
guint64 rcptid;
setup_persist_id_test();
rcptid_set_id(0xFFFFFFFFFFFFFFFF);
rcptid = rcptid_generate_id();
rcptid = rcptid_generate_id();
assert_guint64(rcptid, (guint64) 1, "Rcptid counter overflow handling did not work!");
teardown_persist_id_test();
}
static void
test_rcptid_is_formatted_as_a_number_when_nonzero(void)
{
GString *formatted_rcptid = g_string_sized_new(0);
setup_persist_id_test();
rcptid_append_formatted_id(formatted_rcptid, 1);
assert_string(formatted_rcptid->str, "1", "RCPTID macro formatting for non-zero number failed!");
teardown_persist_id_test();
g_string_free(formatted_rcptid, TRUE);
}
static void
test_rcptid_is_an_empty_string_when_zero(void)
{
GString *formatted_rcptid = g_string_sized_new(0);
setup_persist_id_test();
rcptid_append_formatted_id(formatted_rcptid, 0);
assert_string(formatted_rcptid->str, "", "RCPTID macro formatting for zero value failed!");
teardown_persist_id_test();
g_string_free(formatted_rcptid, TRUE);
}
static void
rcptid_test_case()
{
test_rcptid_is_persistent_across_persist_backend_reinits();
test_rcptid_overflows_at_64bits_and_is_reset_to_one();
test_rcptid_is_formatted_as_a_number_when_nonzero();
test_rcptid_is_an_empty_string_when_zero();
}
int
main(int argc G_GNUC_UNUSED, char *argv[] G_GNUC_UNUSED)
{
if (argc > 1)
verbose = TRUE;
app_startup();
rcptid_test_case();
return 0;
}
|