static void test_log_proto_base(void) { LogProtoServer *proto; assert_gint(log_proto_get_char_size_for_fixed_encoding("iso-8859-2"), 1, NULL); assert_gint(log_proto_get_char_size_for_fixed_encoding("ucs-4"), 4, NULL); log_proto_server_options_set_encoding(&proto_server_options, "ucs-4"); proto = log_proto_binary_record_server_new( log_transport_mock_records_new( /* ucs4, terminated by record size */ "\x00\x00\x00\xe1\x00\x00\x00\x72\x00\x00\x00\x76\x00\x00\x00\xed" /* |...á...r...v...í| */ "\x00\x00\x00\x7a\x00\x00\x00\x74\x00\x00\x01\x71\x00\x00\x00\x72", 32, /* |...z...t...ű...r| */ LTM_EOF), get_inited_proto_server_options(), 32); /* check if error state is not forgotten unless reset_error is called */ proto->status = LPS_ERROR; assert_proto_server_status(proto, proto->status , LPS_ERROR); assert_proto_server_fetch_failure(proto, LPS_ERROR, NULL); log_proto_server_reset_error(proto); assert_proto_server_fetch(proto, "árvíztűr", -1); assert_proto_server_status(proto, proto->status, LPS_SUCCESS); log_proto_server_free(proto); log_proto_server_options_destroy(&proto_server_options); }
static void test_ack_over_eof(LogQueueDiskReliable *dq, LogMessage *msg1, LogMessage *msg2) { log_queue_ack_backlog(&dq->super.super, 3); assert_gint(dq->qbacklog->length, 0, ASSERTION_ERROR("Messages are in the qbacklog")); assert_gint(dq->super.qdisk->hdr->backlog_head, dq->super.qdisk->hdr->read_head, ASSERTION_ERROR("Backlog head in bad position")); }
static void test_rewind_over_eof(LogQueueDiskReliable *dq) { LogMessage *msg3 = log_msg_new_mark(); LogMessage *read_message3; LogPathOptions local_options = LOG_PATH_OPTIONS_INIT; msg3->ack_func = _dummy_ack; log_queue_push_tail(&dq->super.super, msg3, &local_options); gint64 previous_read_head = dq->super.qdisk->hdr->read_head; read_message3 = log_queue_pop_head(&dq->super.super, &local_options); assert_true(read_message3 != NULL, ASSERTION_ERROR("Can't read message from queue")); assert_gint(dq->super.qdisk->hdr->read_head, dq->super.qdisk->hdr->write_head, ASSERTION_ERROR("Read head in bad position")); assert_true(msg3 == read_message3, ASSERTION_ERROR("Message 3 isn't read from qreliable")); log_msg_unref(read_message3); log_queue_rewind_backlog(&dq->super.super, 1); assert_gint(dq->super.qdisk->hdr->read_head, previous_read_head, ASSERTION_ERROR("Read head is corrupted")); read_message3 = log_queue_pop_head(&dq->super.super, &local_options); assert_true(read_message3 != NULL, ASSERTION_ERROR("Can't read message from queue")); assert_gint(dq->super.qdisk->hdr->read_head, dq->super.qdisk->hdr->write_head, ASSERTION_ERROR("Read head in bad position")); assert_true(msg3 == read_message3, ASSERTION_ERROR("Message 3 isn't read from qreliable")); log_msg_drop(msg3, &local_options, AT_PROCESSED); }
void __test_seeks(Journald *journald) { gint result = journald_seek_head(journald); assert_gint(result, 0, ASSERTION_ERROR("Can't seek in empty journald mock")); result = journald_next(journald); assert_gint(result, 0, ASSERTION_ERROR("Bad next step result")); MockEntry *entry = mock_entry_new("test_data1"); mock_entry_add_data(entry, "MESSAGE=test message"); mock_entry_add_data(entry, "KEY=VALUE"); mock_entry_add_data(entry, "HOST=testhost"); journald_mock_add_entry(journald, entry); entry = mock_entry_new("test_data2"); mock_entry_add_data(entry, "MESSAGE=test message2"); mock_entry_add_data(entry, "KEY=VALUE2"); mock_entry_add_data(entry, "HOST=testhost2"); journald_mock_add_entry(journald, entry); result = journald_seek_head(journald); assert_gint(result, 0, ASSERTION_ERROR("Can't seek in journald mock")); result = journald_next(journald); assert_gint(result, 1, ASSERTION_ERROR("Bad next step result")); }
static void _prepare_eof_test(LogQueueDiskReliable *dq, LogMessage **msg1, LogMessage **msg2) { LogPathOptions local_options = LOG_PATH_OPTIONS_INIT; gint64 start_pos = TEST_DISKQ_SIZE; *msg1 = log_msg_new_mark(); *msg2 = log_msg_new_mark(); (*msg1)->ack_func = _dummy_ack; log_msg_add_ack(*msg1, &local_options); (*msg2)->ack_func = _dummy_ack; log_msg_add_ack(*msg2, &local_options); dq->super.qdisk->hdr->write_head = start_pos; dq->super.qdisk->hdr->read_head = QDISK_RESERVED_SPACE + mark_message_serialized_size + 1; dq->super.qdisk->hdr->backlog_head = dq->super.qdisk->hdr->read_head; log_queue_push_tail(&dq->super.super, *msg1, &local_options); log_queue_push_tail(&dq->super.super, *msg2, &local_options); assert_gint(dq->qreliable->length, NUMBER_MESSAGES_IN_QUEUE(2), ASSERTION_ERROR("Messages aren't in qreliable")); assert_gint64(dq->super.qdisk->hdr->write_head, QDISK_RESERVED_SPACE + mark_message_serialized_size, ASSERTION_ERROR("Bad write head")); assert_gint(num_of_ack, 0, ASSERTION_ERROR("Messages are acked")); dq->super.qdisk->hdr->read_head = start_pos; dq->super.qdisk->hdr->backlog_head = dq->super.qdisk->hdr->read_head; }
static void test_clone_string_array_duplicates_elements_while_leaving_token_values_intact(void) { const gchar *arr[] = { "foo", "bar", "baz", NULL }; GList *l, *l2; l = string_array_to_list(arr); l = g_list_append(l, GUINT_TO_POINTER(1)); l = g_list_append(l, GUINT_TO_POINTER(2)); l2 = string_list_clone(l); string_list_free(l); assert_gint(g_list_length(l2), 5, "converted list is not the expected length"); assert_string(l2->data, "foo", "first element is expected to be foo"); assert_string(l2->next->data, "bar", "second element is expected to be bar"); assert_string(l2->next->next->data, "baz", "third element is expected to be baz"); assert_gint(GPOINTER_TO_UINT(l2->next->next->next->data), 1, "fourth element is expected to be a token, with a value of 1"); assert_gint(GPOINTER_TO_UINT(l2->next->next->next->next->data), 2, "fifth element is expected to be a token, with a value of 2"); string_list_free(l2); }
void _foreach_callback_assertions(gchar* name, gint size, gpointer entry, gpointer userdata) { assert_string((gchar *) userdata, "test_userdata", "Userdata is not passed correctly to foreach func!"); assert_string(name, "test", "Name of persist entry does not match!"); TestState *state = (TestState *) entry; assert_gint(state->value, 3, "Content of state does not match!"); assert_gint(size, sizeof(TestState), "Size of state does not match!"); }
void test_log() { GString *command = g_string_sized_new(128); GString *reply; g_string_assign(command,"LOG"); reply = control_connection_message_log(command); assert_string(reply->str, "Invalid arguments received, expected at least one argument", "Bad reply"); g_string_free(reply, TRUE); g_string_assign(command,"LOG fakelog"); reply = control_connection_message_log(command); assert_string(reply->str, "Invalid arguments received", "Bad reply"); g_string_free(reply, TRUE); verbose_flag = 0; debug_flag = 1; trace_flag = 1; g_string_assign(command,"LOG VERBOSE"); reply = control_connection_message_log(command); assert_string(reply->str, "VERBOSE=0", "Bad reply"); g_string_free(reply, TRUE); g_string_assign(command,"LOG VERBOSE ON"); reply = control_connection_message_log(command); assert_string(reply->str, "OK", "Bad reply"); assert_gint(verbose_flag,1,"Flag isn't changed"); g_string_free(reply, TRUE); g_string_assign(command,"LOG VERBOSE OFF"); reply = control_connection_message_log(command); assert_string(reply->str, "OK", "Bad reply"); assert_gint(verbose_flag,0,"Flag isn't changed"); g_string_free(reply, TRUE); debug_flag = 0; verbose_flag = 1; trace_flag = 1; g_string_assign(command,"LOG DEBUG"); reply = control_connection_message_log(command); assert_string(reply->str, "DEBUG=0", "Bad reply"); g_string_free(reply, TRUE); trace_flag = 0; verbose_flag = 1; debug_flag = 1; g_string_assign(command,"LOG TRACE"); reply = control_connection_message_log(command); assert_string(reply->str, "TRACE=0", "Bad reply"); g_string_free(reply, TRUE); g_string_free(command, TRUE); return; }
/* * The method make the following situation * the backlog contains 6 messages * the qbacklog contains 3 messages, * but messages in qbacklog are the end of the backlog */ void _prepare_rewind_backlog_test(LogQueueDiskReliable *dq, gint64 *start_pos) { gint i; for (i = 0; i < 8; i++) { LogPathOptions path_options = LOG_PATH_OPTIONS_INIT; LogMessage *mark_message; mark_message = log_msg_new_mark(); mark_message->ack_func = _dummy_ack; log_queue_push_tail(&dq->super.super, mark_message, &path_options); } /* Lets read the messages and leave them in the backlog */ for (i = 0; i < 8; i++) { LogMessage *msg; LogPathOptions path_options = LOG_PATH_OPTIONS_INIT; msg = log_queue_pop_head(&dq->super.super, &path_options); log_msg_unref(msg); } /* Ack the messages which are not in the qbacklog */ log_queue_ack_backlog(&dq->super.super, 5); assert_gint(dq->qbacklog->length, NUMBER_MESSAGES_IN_QUEUE(3), ASSERTION_ERROR("Incorrect number of items in the qbacklog")); *start_pos = dq->super.qdisk->hdr->read_head; /* Now write 3 more messages and read them from buffer * the number of messages in the qbacklog should not be changed * The backlog should contain 6 messages * from these 6 messages 3 messages are cached in the qbacklog * No readable messages are in the queue */ for (i = 0; i < 3; i++) { LogPathOptions path_options = LOG_PATH_OPTIONS_INIT; LogMessage *mark_message; mark_message = log_msg_new_mark(); mark_message->ack_func = _dummy_ack; log_queue_push_tail(&dq->super.super, mark_message, &path_options); mark_message = log_queue_pop_head(&dq->super.super, &path_options); assert_gint(dq->qreliable->length, 0, ASSERTION_ERROR("Incorrect number of items in the qreliable")); assert_gint(dq->qbacklog->length, NUMBER_MESSAGES_IN_QUEUE(3), ASSERTION_ERROR("Incorrect number of items in the qbacklog")); log_msg_unref(mark_message); } assert_gint(dq->super.qdisk->hdr->backlog_len, 6, ASSERTION_ERROR("Incorrect number of messages in the backlog")); assert_gint(dq->super.qdisk->hdr->length, 0, ASSERTION_ERROR("Reliable diskq isn't empty")); }
void test_rewind_backlog_without_using_qbacklog(LogQueueDiskReliable *dq, gint64 old_read_pos) { /* * Rewind the last 2 messages * - the read_head should be moved to the good position * - the qbacklog and qreliable should be untouched */ log_queue_rewind_backlog(&dq->super.super, 2); assert_gint64(dq->super.qdisk->hdr->read_head, old_read_pos + mark_message_serialized_size, ASSERTION_ERROR("Bad reader position")); assert_gint(dq->qreliable->length, 0, ASSERTION_ERROR("Incorrect number of items in the qreliable")); assert_gint(dq->qbacklog->length, NUMBER_MESSAGES_IN_QUEUE(3), ASSERTION_ERROR("Incorrect number of items in the qbacklog")); }
void _test_default_working_test(TestCase *self, TestSource *src, LogMessage *msg) { const gchar *message = log_msg_get_value(msg, LM_V_MESSAGE, NULL); JournalReaderOptions *options = self->user_data; assert_string(message, "Dummy message", ASSERTION_ERROR("Bad message")); assert_gint(msg->pri, options->default_pri, ASSERTION_ERROR("Bad default prio")); assert_gint(options->fetch_limit, 10, ASSERTION_ERROR("Bad default fetch_limit")); assert_gint(options->max_field_size, 64 * 1024, ASSERTION_ERROR("Bad max field size")); assert_gpointer(options->prefix, NULL, ASSERTION_ERROR("Bad default prefix value")); assert_string(options->recv_time_zone, configuration->recv_time_zone, ASSERTION_ERROR("Bad default timezone")); test_source_finish_tc(src); }
void test_rewind_backlog_partially_used_qbacklog(LogQueueDiskReliable *dq, gint64 old_read_pos) { /* * Rewind more 2 messages * - the reader the should be moved to the good position * - the qreliable should contain 1 items * - the qbackbacklog should contain 2 items */ log_queue_rewind_backlog(&dq->super.super, 2); assert_gint64(dq->super.qdisk->hdr->read_head, old_read_pos - mark_message_serialized_size, ASSERTION_ERROR("Bad reader position")); assert_gint(dq->qreliable->length, NUMBER_MESSAGES_IN_QUEUE(1), ASSERTION_ERROR("Incorrect number of items in the qreliable")); assert_gint(dq->qbacklog->length, NUMBER_MESSAGES_IN_QUEUE(2), ASSERTION_ERROR("Incorrect number of items in the qbacklog")); }
void _test_default_facility_test(TestCase *self, TestSource *src, LogMessage *msg) { gint facility = GPOINTER_TO_INT(self->user_data); assert_gint(msg->pri, facility | LOG_NOTICE, ASSERTION_ERROR("Bad default prio")); test_source_finish_tc(src); }
static gboolean test_vp_obj_stop(const gchar *name, const gchar *prefix, gpointer *prefix_data, const gchar *prev, gpointer *prev_data, gpointer user_data) { static int times_called = 0; switch(times_called) { case 0: assert_string(prefix, "root.test", "First vp_obj_stop but prefix is not 'root.test'!"); assert_string(prev, "root", "Wrong previous prefix"); assert_gint(*((gint*)(*prev_data)), root_data, "Wrong previous data"); break; case 1: assert_string(prefix, "root", "Second vp_obj_stop but prefix is not 'root'!"); break; case 2: assert_true(prefix == 0, "Third vp_obj_stop but prefix is not NULL!"); break; default: assert_false(TRUE, "vp_obj_stop called more times than number of path elements!"); } times_called++; return FALSE; }
void _test_default_level_test(TestCase *self, TestSource *src, LogMessage *msg) { gint level = GPOINTER_TO_INT(self->user_data); assert_gint(msg->pri, LOG_LOCAL0 | level, ASSERTION_ERROR("Bad default prio")); test_source_finish_tc(src); }
LogMessage * parse_log_message(gchar *raw_message_str, gint parse_flags, gchar *bad_hostname_re) { LogMessage *message; GSockAddr *addr = g_sockaddr_inet_new("10.10.10.10", 1010); regex_t bad_hostname; parse_options.flags = parse_flags; parse_options.sdata_param_value_max = 255; if (bad_hostname_re) { assert_gint(regcomp(&bad_hostname, bad_hostname_re, REG_NOSUB | REG_EXTENDED), 0, "Unexpected failure of regcomp(); bad_hostname_re='%s'", bad_hostname_re); parse_options.bad_hostname = &bad_hostname; } message = log_msg_new(raw_message_str, strlen(raw_message_str), addr, &parse_options); if (bad_hostname_re) { regfree(parse_options.bad_hostname); parse_options.bad_hostname = NULL; } simulate_log_readers_effect_on_timezone_offset(message); return message; }
static void _concat_nvpairs_helper(const gchar *name, const gchar *value, gsize value_len, gpointer user_data) { GString *concatenated = (GString *) user_data; g_string_sprintfa(concatenated, "%s=%s\n", name, value); assert_gint(value_len, strlen(value), "foreach() length mismatch"); }
static void assert_create_socket_fails_with_address(GSockAddr *addr) { gint sock; assert_false(create_socket_with_address(addr, &sock), "transport_mapper_open_socket() suceeded unexpectedly"); assert_gint(sock, -1, "failed create_socket returned a non-extremal value on failure"); }
static void test_read_over_eof(LogQueueDiskReliable *dq, LogMessage *msg1, LogMessage *msg2) { LogPathOptions read_options; LogMessage *read_message1; LogMessage *read_message2; read_message1 = log_queue_pop_head(&dq->super.super, &read_options); assert_true(read_message1 != NULL, ASSERTION_ERROR("Can't read message from queue")); read_message2 = log_queue_pop_head(&dq->super.super, &read_options); assert_true(read_message2 != NULL, ASSERTION_ERROR("Can't read message from queue")); assert_gint(dq->qreliable->length, 0, ASSERTION_ERROR("Queue reliable isn't empty")); assert_gint(dq->qbacklog->length, NUMBER_MESSAGES_IN_QUEUE(2), ASSERTION_ERROR("Messages aren't in the qbacklog")); assert_gint(dq->super.qdisk->hdr->read_head, dq->super.qdisk->hdr->write_head, ASSERTION_ERROR("Read head in bad position")); assert_true(msg1 == read_message1, ASSERTION_ERROR("Message 1 isn't read from qreliable")); assert_true(msg2 == read_message2, ASSERTION_ERROR("Message 2 isn't read from qreliable")); }
static void assert_create_socket_fails(void) { gint sock; assert_false(create_socket(&sock), "transport_mapper_open_socket() suceeded unexpectedly"); assert_gint(sock, -1, "failed create_socket returned a non-extremal value on failure"); }
void __test_enumerate(Journald *journald) { const void *data; const void *prev_data; gsize length; gsize prev_len; gint result; journald_restart_data(journald); result = journald_enumerate_data(journald, &data, &length); assert_gint(result, 1, ASSERTION_ERROR("Data should exist")); prev_data = data; prev_len = length; result = journald_enumerate_data(journald, &data, &length); assert_gint(result, 1, ASSERTION_ERROR("Data should exist")); result = journald_enumerate_data(journald, &data, &length); assert_gint(result, 1, ASSERTION_ERROR("Data should exist")); result = journald_enumerate_data(journald, &data, &length); assert_gint(result, 0, ASSERTION_ERROR("Data should not exist")); journald_restart_data(journald); result = journald_enumerate_data(journald, &data, &length); assert_gint(result, 1, ASSERTION_ERROR("Data should exist")); assert_gpointer((gpointer )data, (gpointer )prev_data, ASSERTION_ERROR("restart data should seek the start of the data")); assert_gint(length, prev_len, ASSERTION_ERROR("Bad length after restart data")); result = journald_next(journald); assert_gint(result, 0, ASSERTION_ERROR("Should not contain more elements")); }
void test_rewind_backlog_use_whole_qbacklog(LogQueueDiskReliable *dq) { /* * Rewind more 2 messages * - the reader the should be moved to the backlog head * - the qreliable should contain 3 items * - the qbackbacklog should be empty */ log_queue_rewind_backlog(&dq->super.super, 2); assert_gint64(dq->super.qdisk->hdr->read_head, dq->super.qdisk->hdr->backlog_head, ASSERTION_ERROR("Bad reader position")); assert_gint(dq->qreliable->length, NUMBER_MESSAGES_IN_QUEUE(3), ASSERTION_ERROR("Incorrect number of items in the qreliable")); assert_gint(dq->qbacklog->length, 0, ASSERTION_ERROR("Incorrect number of items in the qbacklog")); }
static void assert_error(GError *error, gint code, const gchar *expected_message) { assert_not_null(error, "GError expected to be non-NULL"); assert_gint(error->code, code, "GError error code is as expected"); if (expected_message) assert_string(error->message, expected_message, "GError error message is as expected"); }
static void assert_hostname_to_hostname_len(gsize buflen, const gchar *hostname, const gchar *expected) { const gchar *result; gsize result_len = 9999; result = resolve_hostname_to_hostname(&result_len, hostname, &host_resolve_options); assert_string(result, expected, "hostname to hostname result mismatch"); assert_gint(result_len, strlen(result), "returned length is not true"); }
static gint accumulator_assert_that_lines_are_starting_with_sequence_number(LogProtoTextServer *self, const guchar *msg, gsize msg_len, gssize consumed_len) { assert_true((msg[0] - '0') == accumulate_seq, "accumulate_line: Message doesn't start with sequence number, msg=%.*s, seq=%d", msg_len, msg, accumulate_seq); assert_gint(consumed_len, -1, "Initial invocation of the accumulator expects -1 as consumed_len"); accumulate_seq++; return LPT_CONSUME_LINE | LPT_EXTRACTED; }
void __test_message_has_no_prefix(TestCase *self, LogMessage *msg) { gchar *requested_name = g_strdup_printf("%s%s", (gchar *)self->user_data, "MESSAGE"); NVHandle handle = log_msg_get_value_handle(requested_name); gssize value_len; log_msg_get_value(msg, handle, &value_len); assert_gint(value_len, 0, ASSERTION_ERROR("MESSAGE has prefix")); g_free(requested_name); }
static void _validate_yielded_counters(StatsCluster *sc, gint type, StatsCounterItem *counter, gpointer user_data) { ValidateCountersState *st = (ValidateCountersState *) user_data; gint t; t = va_arg(st->types, gint); assert_true(t >= 0, "foreach counter returned a new counter, but we expected the end already"); assert_gint(type, t, "Counter type mismatch"); st->validate_count++; }
static gboolean test_vp_value(const gchar *name, const gchar *prefix, TypeHint type, const gchar *value, gsize value_len, gpointer *prefix_data, gpointer user_data) { assert_string(prefix, "root.test", "Wrong prefix"); assert_nstring(value, value_len, "value", -1, "Wrong value"); assert_gint(*((gint*)(*prefix_data)), root_test_data, "Wrong prefix data"); return FALSE; }
static void assert_sockaddr_to_hostname(GSockAddr *sa, const gchar *expected) { const gchar *result; gsize result_len = 9999; result = resolve_sockaddr_to_hostname(&result_len, sa, &host_resolve_options); g_sockaddr_unref(sa); assert_string(result, expected, "resolved name mismatch"); assert_gint(result_len, strlen(result), "returned length is not true"); }
void __test_cursors(Journald *journald) { gchar *cursor; gint result = journald_get_cursor(journald, &cursor); assert_string(cursor, "test_data1", ASSERTION_ERROR("Bad cursor fetched"));\ g_free(cursor); result = journald_next(journald); assert_gint(result, 1, ASSERTION_ERROR("Bad next step result")); result = journald_get_cursor(journald, &cursor); assert_string(cursor, "test_data2", ASSERTION_ERROR("Bad cursor fetched")); g_free(cursor); result = journald_next(journald); assert_gint(result, 0, ASSERTION_ERROR("Should not contain more elements")); result = journald_seek_cursor(journald, "test_data1"); assert_gint(result, 0, ASSERTION_ERROR("Should find cursor")); result = journald_next(journald); assert_gint(result, 1, ASSERTION_ERROR("Bad next step result")); result = journald_get_cursor(journald, &cursor); assert_string(cursor, "test_data1", ASSERTION_ERROR("Bad cursor fetched")); g_free(cursor); result = journald_seek_cursor(journald, "test_data2"); assert_gint(result, 0, ASSERTION_ERROR("Should find cursor")); result = journald_next(journald); assert_gint(result, 1, ASSERTION_ERROR("Bad next step result")); result = journald_get_cursor(journald, &cursor); assert_string(cursor, "test_data2", ASSERTION_ERROR("Bad cursor fetched")); g_free(cursor); }