Greenbone Vulnerability Management Libraries 22.32.0
logging_tests.c File Reference
#include "logging.c"
#include <cgreen/cgreen.h>
#include <cgreen/mocks.h>
#include <fcntl.h>
#include <glib/gstdio.h>
Include dependency graph for logging_tests.c:

Go to the source code of this file.

Functions

 Describe (logging)
 BeforeEach (logging)
 AfterEach (logging)
 Ensure (logging, validate_check_log_file)
 Ensure (logging, should_convert_level_int_from_string)
 Ensure (logging, should_convert_facility_int_from_string)
 Ensure (logging, should_load_log_configuration)
static void mock_log_func (const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer user_data)
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, should_setup_log_handlers_with_default_handler)
 Ensure (logging, should_setup_log_handlers_with_default_domain_handler)
 Ensure (logging, should_setup_log_handlers_with_domain_handler)
 Ensure (logging, should_get_time_for_null)
static TestSuite * logging_test_suite ()
int main (int argc, char **argv)

Function Documentation

◆ AfterEach()

AfterEach ( logging )

Definition at line 19 of file logging_tests.c.

20{
21}

◆ BeforeEach()

BeforeEach ( logging )

Definition at line 15 of file logging_tests.c.

16{
17}

◆ Describe()

Describe ( logging )

◆ Ensure() [1/8]

Ensure ( logging ,
should_convert_facility_int_from_string  )

Definition at line 97 of file logging_tests.c.

98{
99 assert_that (facility_int_from_string (NULL), is_equal_to (LOG_LOCAL0));
100 assert_that (facility_int_from_string (""), is_equal_to (LOG_LOCAL0));
101
102 assert_that (facility_int_from_string ("auth"), is_equal_to (LOG_AUTH));
103 assert_that (facility_int_from_string ("authpriv"),
104 is_equal_to (LOG_AUTHPRIV));
105 assert_that (facility_int_from_string ("cron"), is_equal_to (LOG_CRON));
106 assert_that (facility_int_from_string ("daemon"), is_equal_to (LOG_DAEMON));
107 assert_that (facility_int_from_string ("ftp"), is_equal_to (LOG_FTP));
108 assert_that (facility_int_from_string ("kern"), is_equal_to (LOG_KERN));
109 assert_that (facility_int_from_string ("lpr"), is_equal_to (LOG_LPR));
110 assert_that (facility_int_from_string ("mail"), is_equal_to (LOG_MAIL));
111 assert_that (facility_int_from_string ("mark"), is_equal_to (INTERNAL_MARK));
112 assert_that (facility_int_from_string ("news"), is_equal_to (LOG_NEWS));
113 assert_that (facility_int_from_string ("syslog"), is_equal_to (LOG_SYSLOG));
114 assert_that (facility_int_from_string ("user"), is_equal_to (LOG_USER));
115 assert_that (facility_int_from_string ("uucp"), is_equal_to (LOG_UUCP));
116 assert_that (facility_int_from_string ("local0"), is_equal_to (LOG_LOCAL0));
117 assert_that (facility_int_from_string ("local1"), is_equal_to (LOG_LOCAL1));
118 assert_that (facility_int_from_string ("local2"), is_equal_to (LOG_LOCAL2));
119 assert_that (facility_int_from_string ("local3"), is_equal_to (LOG_LOCAL3));
120 assert_that (facility_int_from_string ("local4"), is_equal_to (LOG_LOCAL4));
121 assert_that (facility_int_from_string ("local5"), is_equal_to (LOG_LOCAL5));
122 assert_that (facility_int_from_string ("local6"), is_equal_to (LOG_LOCAL6));
123 assert_that (facility_int_from_string ("local7"), is_equal_to (LOG_LOCAL7));
124
125 assert_that (facility_int_from_string (NULL), is_equal_to (LOG_LOCAL0));
126 assert_that (facility_int_from_string ("unknown"), is_equal_to (LOG_LOCAL0));
127}
static gint facility_int_from_string(const gchar *facility)
Return the integer corresponding to a syslog facility string.
Definition logging.c:135

References facility_int_from_string().

Here is the call graph for this function:

◆ Ensure() [2/8]

Ensure ( logging ,
should_convert_level_int_from_string  )

Definition at line 69 of file logging_tests.c.

70{
71 assert_that (level_int_from_string (NULL), is_equal_to (0));
72 assert_that (level_int_from_string (""), is_equal_to (0));
73
74 assert_that (level_int_from_string ("error"),
75 is_equal_to (G_LOG_LEVEL_ERROR));
76 assert_that (level_int_from_string ("critical"),
77 is_equal_to (G_LOG_LEVEL_CRITICAL));
78 assert_that (level_int_from_string ("warning"),
79 is_equal_to (G_LOG_LEVEL_WARNING));
80 assert_that (level_int_from_string ("message"),
81 is_equal_to (G_LOG_LEVEL_MESSAGE));
82 assert_that (level_int_from_string ("info"), is_equal_to (G_LOG_LEVEL_INFO));
83 assert_that (level_int_from_string ("debug"),
84 is_equal_to (G_LOG_LEVEL_DEBUG));
85
86 assert_that (level_int_from_string ("4"), is_equal_to (G_LOG_LEVEL_ERROR));
87 assert_that (level_int_from_string ("8"), is_equal_to (G_LOG_LEVEL_CRITICAL));
88 assert_that (level_int_from_string ("16"), is_equal_to (G_LOG_LEVEL_WARNING));
89 assert_that (level_int_from_string ("32"), is_equal_to (G_LOG_LEVEL_MESSAGE));
90 assert_that (level_int_from_string ("64"), is_equal_to (G_LOG_LEVEL_INFO));
91 assert_that (level_int_from_string ("128"), is_equal_to (G_LOG_LEVEL_DEBUG));
92 assert_that (level_int_from_string ("123"), is_equal_to (123));
93
94 assert_that (level_int_from_string ("A"), is_equal_to (0));
95}
static gint level_int_from_string(const gchar *level)
Return the integer corresponding to a log level string.
Definition logging.c:104

References level_int_from_string().

Here is the call graph for this function:

◆ Ensure() [3/8]

Ensure ( logging ,
should_get_time_for_null  )

Definition at line 301 of file logging_tests.c.

302{
303 assert_that (get_time (NULL), is_null);
304}
gchar * get_time(gchar *time_fmt)
Returns time as specified in time_fmt strftime format.
Definition logging.c:57

References get_time().

Here is the call graph for this function:

◆ Ensure() [4/8]

Ensure ( logging ,
should_load_log_configuration  )

Definition at line 129 of file logging_tests.c.

130{
131 gchar *config_file = "test_log_config.conf";
132 GSList *log_config_list;
133
134 /* Create a temporary configuration file */
135 FILE *file = fopen (config_file, "w");
136 assert_that (file, is_not_null);
137 fprintf (file, "[*]\n"
138 "prepend=%%t %%s %%p - \n"
139 "separator=:\n"
140 "prepend_time_format=%%Y-%%m-%%d %%H:%%M:%%S\n"
141 "file=-\n"
142 "level=debug\n"
143 "syslog_facility=local0\n"
144 "syslog_ident=test_ident\n");
145 fclose (file);
146
147 /* Load the configuration */
148 log_config_list = load_log_configuration (config_file);
149 assert_that (log_config_list, is_not_null);
150
151 /* Verify the configuration */
152 gvm_logging_domain_t *log_domain_entry =
153 (gvm_logging_domain_t *) log_config_list->data;
154 assert_that (gvm_logging_domain_get_prepend_string (log_domain_entry),
155 is_equal_to_string ("%t %s %p - "));
156 assert_that (gvm_logging_domain_get_prepend_separator (log_domain_entry),
157 is_equal_to_string (":"));
158 assert_that (gvm_logging_domain_get_prepend_time_format (log_domain_entry),
159 is_equal_to_string ("%Y-%m-%d %H:%M:%S"));
160 assert_that (gvm_logging_domain_get_log_file (log_domain_entry),
161 is_equal_to_string ("-"));
162 assert_that (*gvm_logging_domain_get_default_level (log_domain_entry),
163 is_equal_to (G_LOG_LEVEL_DEBUG));
164 assert_that (gvm_logging_domain_get_syslog_facility (log_domain_entry),
165 is_equal_to_string ("local0"));
166 assert_that (gvm_logging_domain_get_syslog_ident (log_domain_entry),
167 is_equal_to_string ("test_ident"));
168
169 /* Clean up */
170 free_log_configuration (log_config_list);
171 g_remove (config_file);
172}
GSList * load_log_configuration(gchar *config_file)
Loads parameters from a config file into a linked list.
Definition logging.c:161
void free_log_configuration(GSList *log_domain_list)
Frees all resources loaded by the config loader.
Definition logging.c:286
GLogLevelFlags * gvm_logging_domain_get_default_level(gvm_logging_domain_t *log_domain)
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)
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

References free_log_configuration(), gvm_logging_domain_get_default_level(), gvm_logging_domain_get_log_file(), gvm_logging_domain_get_prepend_separator(), gvm_logging_domain_get_prepend_string(), gvm_logging_domain_get_prepend_time_format(), gvm_logging_domain_get_syslog_facility(), gvm_logging_domain_get_syslog_ident(), and load_log_configuration().

Here is the call graph for this function:

◆ Ensure() [5/8]

Ensure ( logging ,
should_setup_log_handlers_with_default_domain_handler  )

Definition at line 236 of file logging_tests.c.

237{
238 /* Setup log handlers */
241
242 /* Verify the log handlers setup */
243 never_expect (mock_default_log_func);
244 never_expect (mock_log_func);
245
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));
251 expect (mock_default_domain_log_func, when (log_domain, 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));
255
256 g_log ("", G_LOG_LEVEL_DEBUG, "test message");
257 g_log (NULL, G_LOG_LEVEL_INFO, "test message 2");
258}
static int setup_log_handlers_internal(GSList *gvm_log_config_list, GLogFunc log_func, GLogFunc default_log_func, GLogFunc default_domain_log_func)
Definition logging.c:910
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)
static void mock_log_func(const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer user_data)

References mock_default_domain_log_func(), mock_default_log_func(), mock_log_func(), and setup_log_handlers_internal().

Here is the call graph for this function:

◆ Ensure() [6/8]

Ensure ( logging ,
should_setup_log_handlers_with_default_handler  )

Definition at line 195 of file logging_tests.c.

196{
197 gchar *config_file = "test_log_config.conf";
198 GSList *log_config_list;
199
200 /* Create a temporary configuration file */
201 FILE *file = fopen (config_file, "w");
202 assert_that (file, is_not_null);
203 // fputs ("", file);
204 fprintf (file, "[*]\n"
205 "prepend=%%t %%s %%p - \n"
206 "separator=:\n"
207 "prepend_time_format=%%Y-%%m-%%d %%H:%%M:%%S\n"
208 "file=-\n"
209 "level=debug\n");
210 fclose (file);
211
212 /* Load the configuration */
213 log_config_list = load_log_configuration (config_file);
214 assert_that (log_config_list, is_not_null);
215
216 /* Setup log handlers */
220
221 /* Verify the log handlers setup */
222 never_expect (mock_log_func);
223 never_expect (mock_default_domain_log_func);
224 expect (mock_default_log_func, when (log_domain, is_equal_to_string ("foo")),
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)));
228
229 g_log ("foo", G_LOG_LEVEL_DEBUG, "test message");
230
231 /* Clean up */
232 free_log_configuration (log_config_list);
233 g_remove (config_file);
234}

References free_log_configuration(), load_log_configuration(), mock_default_domain_log_func(), mock_default_log_func(), mock_log_func(), and setup_log_handlers_internal().

Here is the call graph for this function:

◆ Ensure() [7/8]

Ensure ( logging ,
should_setup_log_handlers_with_domain_handler  )

Definition at line 260 of file logging_tests.c.

261{
262 gchar *config_file = "test_log_config.conf";
263 GSList *log_config_list;
264
265 /* Create a temporary configuration file */
266 FILE *file = fopen (config_file, "w");
267 assert_that (file, is_not_null);
268 // fputs ("", file);
269 fprintf (file, "[foo]\n"
270 "prepend=%%t %%s %%p - \n"
271 "separator=:\n"
272 "prepend_time_format=%%Y-%%m-%%d %%H:%%M:%%S\n"
273 "file=-\n"
274 "level=debug\n");
275 fclose (file);
276
277 /* Load the configuration */
278 log_config_list = load_log_configuration (config_file);
279 assert_that (log_config_list, is_not_null);
280
281 /* Setup log handlers */
285
286 /* Verify the log handlers setup */
287 never_expect (mock_default_log_func);
288 never_expect (mock_default_domain_log_func);
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)));
293
294 g_log ("foo", G_LOG_LEVEL_DEBUG, "test message");
295
296 /* Clean up */
297 free_log_configuration (log_config_list);
298 g_remove (config_file);
299}

References free_log_configuration(), load_log_configuration(), mock_default_domain_log_func(), mock_default_log_func(), mock_log_func(), and setup_log_handlers_internal().

Here is the call graph for this function:

◆ Ensure() [8/8]

Ensure ( logging ,
validate_check_log_file  )

Definition at line 23 of file logging_tests.c.

24{
25 gvm_logging_domain_t *log_domain_entry =
26 gvm_logging_domain_new (g_strdup ("test"));
27 assert_that (check_log_file (log_domain_entry), is_equal_to (0));
28
29 gvm_logging_domain_set_log_file (log_domain_entry, g_strdup (""));
30 assert_that (check_log_file (log_domain_entry), is_equal_to (0));
31
32 gvm_logging_domain_set_log_file (log_domain_entry, g_strdup ("syslog"));
33 assert_that (check_log_file (log_domain_entry), is_equal_to (0));
34
35 gvm_logging_domain_set_log_file (log_domain_entry, g_strdup ("-"));
36 assert_that (check_log_file (log_domain_entry), is_equal_to (0));
37 assert_that (g_file_test ("-", G_FILE_TEST_EXISTS), is_equal_to (FALSE));
38
39 gvm_logging_domain_set_log_file (log_domain_entry,
40 g_strdup ("some-file.log"));
41 assert_that (check_log_file (log_domain_entry), is_equal_to (0));
42 assert_that (g_file_test ("some-file.log", G_FILE_TEST_EXISTS),
43 is_equal_to (1));
44 assert_that (g_remove ("some-file.log"), is_equal_to (0));
45
46 gvm_logging_domain_set_log_file (log_domain_entry,
47 g_strdup ("some-dir/some-file.log"));
48 assert_that (check_log_file (log_domain_entry), is_equal_to (0));
49 assert_that (g_file_test ("some-dir/some-file.log", G_FILE_TEST_EXISTS),
50 is_equal_to (1));
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));
53
54 if (getuid () != 0)
55 {
56 /* root can always write. therefore test writing to a non-writable file as
57 * non-root user only */
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));
61 assert_that (check_log_file (log_domain_entry), is_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));
65 }
66 gvm_logging_domain_free (log_domain_entry);
67}
static int check_log_file(gvm_logging_domain_t *log_domain_entry)
Check permissions of log file and log file directory.
Definition logging.c:853
void gvm_logging_domain_set_log_file(gvm_logging_domain_t *log_domain, gchar *log_file)
Sets the log file for the logging domain.
gvm_logging_domain_t * gvm_logging_domain_new(gchar *log_domain)
Function to initialize logging instance.
void gvm_logging_domain_free(gvm_logging_domain_t *log_domain)
Frees the resources associated with the given logging domain.

References check_log_file(), gvm_logging_domain_free(), gvm_logging_domain_new(), and gvm_logging_domain_set_log_file().

Here is the call graph for this function:

◆ logging_test_suite()

TestSuite * logging_test_suite ( )
static

Definition at line 307 of file logging_tests.c.

308{
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);
324 return suite;
325}

◆ main()

int main ( int argc,
char ** argv )

Definition at line 328 of file logging_tests.c.

329{
330 int ret;
331 TestSuite *suite;
332
333 suite = create_test_suite ();
334 add_suite (suite, logging_test_suite ());
335
336 if (argc > 1)
337 ret = run_single_test (suite, argv[1], create_text_reporter ());
338 else
339 ret = run_test_suite (suite, create_text_reporter ());
340
341 destroy_test_suite (suite);
342
343 return ret;
344}
static TestSuite * logging_test_suite()

References logging_test_suite().

Here is the call graph for this function:

◆ mock_default_domain_log_func()

void mock_default_domain_log_func ( const gchar * log_domain,
GLogLevelFlags log_level,
const gchar * message,
gpointer user_data )
static

Definition at line 189 of file logging_tests.c.

191{
192 mock (log_domain, log_level, message, user_data);
193}

Referenced by Ensure(), Ensure(), and Ensure().

Here is the caller graph for this function:

◆ mock_default_log_func()

void mock_default_log_func ( const gchar * log_domain,
GLogLevelFlags log_level,
const gchar * message,
gpointer user_data )
static

Definition at line 182 of file logging_tests.c.

184{
185 mock (log_domain, log_level, message, user_data);
186}

Referenced by Ensure(), Ensure(), and Ensure().

Here is the caller graph for this function:

◆ mock_log_func()

void mock_log_func ( const gchar * log_domain,
GLogLevelFlags log_level,
const gchar * message,
gpointer user_data )
static

Definition at line 175 of file logging_tests.c.

177{
178 mock (log_domain, log_level, message, user_data);
179}

Referenced by Ensure(), Ensure(), and Ensure().

Here is the caller graph for this function: