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
|
/*
* Copyright (c) 2002-2015 Balabit
* Copyright (c) 2015 Viktor Juhasz <viktor.juhasz@balabit.com>
*
* 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 "logmsg/logmsg.h"
#include "logmsg/timestamp-serialize.h"
#include "apphook.h"
SerializeArchive *sa = NULL;
GString *stream = NULL;
UnixTime input_timestamps[LM_TS_MAX];
UnixTime output_timestamps[LM_TS_MAX];
Test(template_timestamp, test_normal_working)
{
cr_assert(timestamp_serialize(sa, input_timestamps), "Failed to serialize timestamps");
cr_assert(timestamp_deserialize(sa, output_timestamps), "Failed to deserialize timestamps");
cr_assert_str_eq((const gchar *)input_timestamps, (const gchar *)output_timestamps,
"The serialized and the deserialized timestamps are not equal");
}
Test(template_timestamp, test_derializing_injured_timestamp)
{
cr_assert(timestamp_serialize(sa, input_timestamps), "Failed to serialize timestamps");
g_string_truncate(stream, 0);
cr_assert_not(timestamp_deserialize(sa, output_timestamps), "Should be failed");
serialize_archive_free(sa);
sa = serialize_string_archive_new(stream);
cr_assert(timestamp_serialize(sa, input_timestamps), "Failed to serialize timestamps");
g_string_truncate(stream, sizeof(guint64));
cr_assert_not(timestamp_deserialize(sa, output_timestamps), "Should be failed");
serialize_archive_free(sa);
sa = serialize_string_archive_new(stream);
cr_assert(timestamp_serialize(sa, input_timestamps), "Failed to serialize timestamps");
g_string_truncate(stream, sizeof(guint64) + sizeof(guint32));
cr_assert_not(timestamp_deserialize(sa, output_timestamps), "Should be failed");
}
static void
setup(void)
{
stream = g_string_new("");
app_startup();
input_timestamps[LM_TS_STAMP].ut_sec = 1;
input_timestamps[LM_TS_STAMP].ut_usec = 2;
input_timestamps[LM_TS_STAMP].ut_gmtoff = 3;
input_timestamps[LM_TS_RECVD].ut_sec = 4;
input_timestamps[LM_TS_RECVD].ut_usec = 5;
input_timestamps[LM_TS_RECVD].ut_gmtoff = 6;
input_timestamps[LM_TS_PROCESSED].ut_sec = 255;
input_timestamps[LM_TS_PROCESSED].ut_usec = 255;
input_timestamps[LM_TS_PROCESSED].ut_gmtoff = -1;
sa = serialize_string_archive_new(stream);
}
static void
teardown(void)
{
serialize_archive_free(sa);
g_string_free(stream, TRUE);
app_shutdown();
}
TestSuite(template_timestamp, .init = setup, .fini = teardown);
|