| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
#include "lcb-util.h" |
| 10 |
#include "lcb-util-priv.h" |
| 11 |
#include "lcb-message-priv.h" |
| 12 |
|
| 13 |
static void |
| 14 |
test_simple_store_retrieve (void) |
| 15 |
{ |
| 16 |
g_autoptr (GError) error = NULL; |
| 17 |
g_autofree char *db_path = g_test_build_filename (G_TEST_BUILT, "messages.gvdb", NULL); |
| 18 |
GListStore *messages_out; |
| 19 |
GListModel *messages_in; |
| 20 |
LcbMessage *msg1, *msg2; |
| 21 |
LcbMessage *read1, *read2; |
| 22 |
gboolean ok; |
| 23 |
|
| 24 |
msg1 = lcb_message_new ("This is a test", |
| 25 |
4391, 12, 0, 42, LCB_SEVERITY_LEVEL_TEST, |
| 26 |
"Test alert", NULL); |
| 27 |
msg2 = lcb_message_new ("This is another test", |
| 28 |
4391, 125, 1, 31337, LCB_SEVERITY_LEVEL_PRESIDENTIAL, |
| 29 |
"Presidential alert", "00101"); |
| 30 |
|
| 31 |
messages_out = g_list_store_new (LCB_TYPE_MESSAGE); |
| 32 |
g_list_store_append (messages_out, msg1); |
| 33 |
g_list_store_append (messages_out, msg2); |
| 34 |
|
| 35 |
ok = lcb_util_save_messages_to_file (G_LIST_MODEL (messages_out), db_path, &error); |
| 36 |
g_assert_no_error (error); |
| 37 |
g_assert_true (ok); |
| 38 |
g_assert_true (g_file_test (db_path, G_FILE_TEST_EXISTS && G_FILE_TEST_IS_REGULAR)); |
| 39 |
|
| 40 |
messages_in = lcb_util_load_messages_from_file (db_path, &error); |
| 41 |
g_assert_no_error (error); |
| 42 |
g_assert_nonnull (messages_in); |
| 43 |
g_assert_cmpuint (g_list_model_get_n_items (messages_in), ==, 2); |
| 44 |
|
| 45 |
read1 = g_list_model_get_item (messages_in, 0); |
| 46 |
|
| 47 |
g_assert_cmpstr (lcb_message_get_text (msg1), ==, lcb_message_get_text (read1)); |
| 48 |
g_assert_cmpuint (lcb_message_get_channel (msg1), ==, lcb_message_get_channel (read1)); |
| 49 |
g_assert_cmpuint (lcb_message_get_msg_code (msg1), ==, lcb_message_get_msg_code (read1)); |
| 50 |
g_assert_cmpuint (lcb_message_get_update (msg1), ==, lcb_message_get_update (read1)); |
| 51 |
g_assert_cmpint (lcb_message_get_timestamp (msg1), ==, lcb_message_get_timestamp (read1)); |
| 52 |
g_assert_cmpint (lcb_message_get_severity (msg1), ==, lcb_message_get_severity (read1)); |
| 53 |
g_assert_cmpstr (lcb_message_get_severity_subject (msg1), ==, lcb_message_get_severity_subject (read1)); |
| 54 |
g_assert_cmpstr (lcb_message_get_operator_code (msg1), ==, lcb_message_get_operator_code (read1)); |
| 55 |
|
| 56 |
read2 = g_list_model_get_item (G_LIST_MODEL (messages_in), 1); |
| 57 |
|
| 58 |
g_assert_cmpstr (lcb_message_get_text (msg2), ==, lcb_message_get_text (read2)); |
| 59 |
g_assert_cmpuint (lcb_message_get_channel (msg2), ==, lcb_message_get_channel (read2)); |
| 60 |
g_assert_cmpuint (lcb_message_get_msg_code (msg2), ==, lcb_message_get_msg_code (read2)); |
| 61 |
g_assert_cmpuint (lcb_message_get_update (msg2), ==, lcb_message_get_update (read2)); |
| 62 |
g_assert_cmpint (lcb_message_get_timestamp (msg2), ==, lcb_message_get_timestamp (read2)); |
| 63 |
g_assert_cmpint (lcb_message_get_severity (msg2), ==, lcb_message_get_severity (read2)); |
| 64 |
g_assert_cmpstr (lcb_message_get_severity_subject (msg2), ==, lcb_message_get_severity_subject (read2)); |
| 65 |
g_assert_cmpstr (lcb_message_get_operator_code (msg2), ==, lcb_message_get_operator_code (read2)); |
| 66 |
|
| 67 |
g_assert_finalize_object (messages_in); |
| 68 |
g_assert_finalize_object (messages_out); |
| 69 |
|
| 70 |
g_assert_finalize_object (msg1); |
| 71 |
g_assert_finalize_object (read1); |
| 72 |
|
| 73 |
g_assert_finalize_object (msg2); |
| 74 |
g_assert_finalize_object (read2); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
static void |
| 79 |
test_null_bytes (void) |
| 80 |
{ |
| 81 |
g_autoptr (GError) error = NULL; |
| 82 |
g_autofree char *db_path = g_test_build_filename (G_TEST_BUILT, "messages-null.gvdb", NULL); |
| 83 |
GListModel *messages; |
| 84 |
GPtrArray *raw_messages; |
| 85 |
gboolean ok; |
| 86 |
|
| 87 |
raw_messages = lcb_util_load_raw_messages_from_bytes (NULL, &error); |
| 88 |
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA); |
| 89 |
g_assert_null (raw_messages); |
| 90 |
g_clear_error (&error); |
| 91 |
|
| 92 |
messages = lcb_util_load_messages_from_bytes (NULL, &error); |
| 93 |
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA); |
| 94 |
g_assert_null (messages); |
| 95 |
g_clear_error (&error); |
| 96 |
|
| 97 |
ok = lcb_util_save_messages_to_file (NULL, db_path, &error); |
| 98 |
g_assert_no_error (error); |
| 99 |
g_assert_true (ok); |
| 100 |
g_assert_true (g_file_test (db_path, G_FILE_TEST_EXISTS && G_FILE_TEST_IS_REGULAR)); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
static void |
| 105 |
test_empty_messages (void) |
| 106 |
{ |
| 107 |
g_autoptr (GError) error = NULL; |
| 108 |
g_autofree char *db_path = g_test_build_filename (G_TEST_BUILT, "messages-empty.gvdb", NULL); |
| 109 |
GListModel *messages_out, *messages_in; |
| 110 |
GPtrArray *raw_messages; |
| 111 |
gboolean ok; |
| 112 |
|
| 113 |
messages_out = G_LIST_MODEL (g_list_store_new (LCB_TYPE_MESSAGE)); |
| 114 |
|
| 115 |
ok = lcb_util_save_messages_to_file (messages_out, db_path, &error); |
| 116 |
g_assert_no_error (error); |
| 117 |
g_assert_true (ok); |
| 118 |
g_assert_true (g_file_test (db_path, G_FILE_TEST_EXISTS && G_FILE_TEST_IS_REGULAR)); |
| 119 |
g_assert_finalize_object (messages_out); |
| 120 |
|
| 121 |
messages_in = lcb_util_load_messages_from_file (db_path, &error); |
| 122 |
g_assert_no_error (error); |
| 123 |
g_assert_nonnull (messages_in); |
| 124 |
g_assert_cmpuint (g_list_model_get_n_items (messages_in), ==, 0); |
| 125 |
g_assert_finalize_object (messages_in); |
| 126 |
|
| 127 |
raw_messages = lcb_util_load_raw_messages_from_file (db_path, &error); |
| 128 |
g_assert_no_error (error); |
| 129 |
g_assert_nonnull (raw_messages); |
| 130 |
g_assert_cmpuint (raw_messages->len, ==, 0); |
| 131 |
|
| 132 |
g_clear_pointer (&raw_messages, g_ptr_array_unref); |
| 133 |
} |
| 134 |
int |
| 135 |
main (int argc, char *argv[]) |
| 136 |
{ |
| 137 |
g_test_init (&argc, &argv, NULL); |
| 138 |
|
| 139 |
g_test_add_func ("/cbd/util/history/simple_store_retrieve", test_simple_store_retrieve); |
| 140 |
g_test_add_func ("/cbd/util/history/null", test_null_bytes); |
| 141 |
g_test_add_func ("/cbd/util/history/empty", test_empty_messages); |
| 142 |
|
| 143 |
return g_test_run (); |
| 144 |
} |