8#include <cgreen/cgreen.h>
9#include <cgreen/mocks.h>
11#include <glib/gstdio.h>
23Ensure (logging, validate_check_log_file)
37 assert_that (g_file_test (
"-", G_FILE_TEST_EXISTS), is_equal_to (FALSE));
40 g_strdup (
"some-file.log"));
42 assert_that (g_file_test (
"some-file.log", G_FILE_TEST_EXISTS),
44 assert_that (g_remove (
"some-file.log"), is_equal_to (0));
47 g_strdup (
"some-dir/some-file.log"));
49 assert_that (g_file_test (
"some-dir/some-file.log", G_FILE_TEST_EXISTS),
51 assert_that (g_remove (
"some-dir/some-file.log"), is_equal_to (0));
52 assert_that (g_rmdir (
"some-dir"), is_equal_to (0));
58 assert_that (g_mkdir_with_parents (
"some-dir", 0700), is_equal_to (0));
59 assert_that (g_creat (
"some-dir/some-file.log", 0400),
60 is_not_equal_to (-1));
62 assert_that (g_chmod (
"some-dir/some-file.log", 0700), is_equal_to (0));
63 assert_that (g_remove (
"some-dir/some-file.log"), is_equal_to (0));
64 assert_that (g_rmdir (
"some-dir"), is_equal_to (0));
69Ensure (logging, should_convert_level_int_from_string)
75 is_equal_to (G_LOG_LEVEL_ERROR));
77 is_equal_to (G_LOG_LEVEL_CRITICAL));
79 is_equal_to (G_LOG_LEVEL_WARNING));
81 is_equal_to (G_LOG_LEVEL_MESSAGE));
84 is_equal_to (G_LOG_LEVEL_DEBUG));
97Ensure (logging, should_convert_facility_int_from_string)
104 is_equal_to (LOG_AUTHPRIV));
129Ensure (logging, should_load_log_configuration)
131 gchar *config_file =
"test_log_config.conf";
132 GSList *log_config_list;
135 FILE *file = fopen (config_file,
"w");
136 assert_that (file, is_not_null);
137 fprintf (file,
"[*]\n"
138 "prepend=%%t %%s %%p - \n"
140 "prepend_time_format=%%Y-%%m-%%d %%H:%%M:%%S\n"
143 "syslog_facility=local0\n"
144 "syslog_ident=test_ident\n");
149 assert_that (log_config_list, is_not_null);
155 is_equal_to_string (
"%t %s %p - "));
157 is_equal_to_string (
":"));
159 is_equal_to_string (
"%Y-%m-%d %H:%M:%S"));
161 is_equal_to_string (
"-"));
163 is_equal_to (G_LOG_LEVEL_DEBUG));
165 is_equal_to_string (
"local0"));
167 is_equal_to_string (
"test_ident"));
171 g_remove (config_file);
176 const gchar *message, gpointer user_data)
178 mock (log_domain, log_level, message, user_data);
183 const gchar *message, gpointer user_data)
185 mock (log_domain, log_level, message, user_data);
190 const gchar *message, gpointer user_data)
192 mock (log_domain, log_level, message, user_data);
195Ensure (logging, should_setup_log_handlers_with_default_handler)
197 gchar *config_file =
"test_log_config.conf";
198 GSList *log_config_list;
201 FILE *file = fopen (config_file,
"w");
202 assert_that (file, is_not_null);
204 fprintf (file,
"[*]\n"
205 "prepend=%%t %%s %%p - \n"
207 "prepend_time_format=%%Y-%%m-%%d %%H:%%M:%%S\n"
214 assert_that (log_config_list, is_not_null);
225 when (log_level, is_equal_to (G_LOG_LEVEL_DEBUG)),
226 when (message, is_equal_to_string (
"test message")),
227 when (user_data, is_equal_to (log_config_list)));
229 g_log (
"foo", G_LOG_LEVEL_DEBUG,
"test message");
233 g_remove (config_file);
236Ensure (logging, should_setup_log_handlers_with_default_domain_handler)
247 when (log_domain, is_equal_to_string (
"")),
248 when (log_level, is_equal_to (G_LOG_LEVEL_DEBUG)),
249 when (message, is_equal_to_string (
"test message")),
250 when (user_data, is_null));
252 when (log_level, is_equal_to (G_LOG_LEVEL_INFO)),
253 when (message, is_equal_to_string (
"test message 2")),
254 when (user_data, is_null));
256 g_log (
"", G_LOG_LEVEL_DEBUG,
"test message");
257 g_log (NULL, G_LOG_LEVEL_INFO,
"test message 2");
260Ensure (logging, should_setup_log_handlers_with_domain_handler)
262 gchar *config_file =
"test_log_config.conf";
263 GSList *log_config_list;
266 FILE *file = fopen (config_file,
"w");
267 assert_that (file, is_not_null);
269 fprintf (file,
"[foo]\n"
270 "prepend=%%t %%s %%p - \n"
272 "prepend_time_format=%%Y-%%m-%%d %%H:%%M:%%S\n"
279 assert_that (log_config_list, is_not_null);
289 expect (
mock_log_func, when (log_domain, is_equal_to_string (
"foo")),
290 when (log_level, is_equal_to (G_LOG_LEVEL_DEBUG)),
291 when (message, is_equal_to_string (
"test message")),
292 when (user_data, is_equal_to (log_config_list)));
294 g_log (
"foo", G_LOG_LEVEL_DEBUG,
"test message");
298 g_remove (config_file);
301Ensure (logging, should_get_time_for_null)
303 assert_that (
get_time (NULL), is_null);
309 TestSuite *suite = create_test_suite ();
310 add_test_with_context (suite, logging, validate_check_log_file);
311 add_test_with_context (suite, logging, should_convert_level_int_from_string);
312 add_test_with_context (suite, logging,
313 should_convert_facility_int_from_string);
314 add_test_with_context (suite, logging, should_load_log_configuration);
315 add_test_with_context (suite, logging,
316 should_setup_log_handlers_with_default_handler);
317 add_test_with_context (suite, logging,
318 should_setup_log_handlers_with_default_domain_handler);
319 add_test_with_context (suite, logging,
320 should_setup_log_handlers_with_domain_handler);
321 add_test_with_context (suite, logging,
322 should_setup_log_handlers_with_domain_handler);
323 add_test_with_context (suite, logging, should_get_time_for_null);
333 suite = create_test_suite ();
337 ret = run_single_test (suite, argv[1], create_text_reporter ());
339 ret = run_test_suite (suite, create_text_reporter ());
341 destroy_test_suite (suite);
Implementation of logging methods.
static int check_log_file(gvm_logging_domain_t *log_domain_entry)
Check permissions of log file and log file directory.
GSList * load_log_configuration(gchar *config_file)
Loads parameters from a config file into a linked list.
void free_log_configuration(GSList *log_domain_list)
Frees all resources loaded by the config loader.
gchar * get_time(gchar *time_fmt)
Returns time as specified in time_fmt strftime format.
static gint level_int_from_string(const gchar *level)
Return the integer corresponding to a log level string.
static gint facility_int_from_string(const gchar *facility)
Return the integer corresponding to a syslog facility string.
static int setup_log_handlers_internal(GSList *gvm_log_config_list, GLogFunc log_func, GLogFunc default_log_func, GLogFunc default_domain_log_func)
void gvm_logging_domain_set_log_file(gvm_logging_domain_t *log_domain, gchar *log_file)
Sets the log file for the logging domain.
GLogLevelFlags * gvm_logging_domain_get_default_level(gvm_logging_domain_t *log_domain)
gvm_logging_domain_t * gvm_logging_domain_new(gchar *log_domain)
Function to initialize logging instance.
gchar * gvm_logging_domain_get_prepend_time_format(gvm_logging_domain_t *log_domain)
gchar * gvm_logging_domain_get_syslog_ident(gvm_logging_domain_t *log_domain)
void gvm_logging_domain_free(gvm_logging_domain_t *log_domain)
Frees the resources associated with the given logging domain.
gchar * gvm_logging_domain_get_log_file(gvm_logging_domain_t *log_domain)
gchar * gvm_logging_domain_get_prepend_separator(gvm_logging_domain_t *log_domain)
gchar * gvm_logging_domain_get_prepend_string(gvm_logging_domain_t *log_domain)
gchar * gvm_logging_domain_get_syslog_facility(gvm_logging_domain_t *log_domain)
struct gvm_logging_domain gvm_logging_domain_t
static TestSuite * logging_test_suite()
static void mock_default_log_func(const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer user_data)
static void mock_default_domain_log_func(const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer user_data)
Ensure(logging, validate_check_log_file)
int main(int argc, char **argv)
static void mock_log_func(const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer user_data)
static TestSuite * logging_test_suite()