LCOV - code coverage report
Current view: top level - glib/glib/tests - logging.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 504 553 91.1 %
Date: 2024-03-26 05:16:46 Functions: 45 52 86.5 %
Branches: 46 56 82.1 %

           Branch data     Line data    Source code
       1                 :            : #include <stdlib.h>
       2                 :            : #include <string.h>
       3                 :            : #define G_LOG_USE_STRUCTURED 1
       4                 :            : #include <glib.h>
       5                 :            : 
       6                 :            : #ifdef G_OS_WIN32
       7                 :            : #define LINE_END "\r\n"
       8                 :            : #else
       9                 :            : #define LINE_END "\n"
      10                 :            : #endif
      11                 :            : 
      12                 :            : /* Test g_warn macros */
      13                 :            : static void
      14                 :          1 : test_warnings (void)
      15                 :            : {
      16                 :          1 :   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
      17                 :            :                          "*test_warnings*should not be reached*");
      18                 :          1 :   g_warn_if_reached ();
      19                 :          1 :   g_test_assert_expected_messages ();
      20                 :            : 
      21                 :          1 :   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
      22                 :            :                          "*test_warnings*runtime check failed*");
      23                 :          1 :   g_warn_if_fail (FALSE);
      24                 :          1 :   g_test_assert_expected_messages ();
      25                 :          1 : }
      26                 :            : 
      27                 :            : static guint log_count = 0;
      28                 :            : 
      29                 :            : static void
      30                 :          1 : log_handler (const gchar    *log_domain,
      31                 :            :              GLogLevelFlags  log_level,
      32                 :            :              const gchar    *message,
      33                 :            :              gpointer        user_data)
      34                 :            : {
      35                 :          1 :   g_assert_cmpstr (log_domain, ==, "bu");
      36                 :          1 :   g_assert_cmpint (log_level, ==, G_LOG_LEVEL_INFO);
      37                 :            : 
      38                 :          1 :   log_count++;
      39                 :          1 : }
      40                 :            : 
      41                 :            : /* test that custom log handlers only get called for
      42                 :            :  * their domain and level
      43                 :            :  */
      44                 :            : static void
      45                 :          1 : test_set_handler (void)
      46                 :            : {
      47                 :            :   guint id;
      48                 :            : 
      49                 :          1 :   id = g_log_set_handler ("bu", G_LOG_LEVEL_INFO, log_handler, NULL);
      50                 :            : 
      51                 :          1 :   g_log ("bu", G_LOG_LEVEL_DEBUG, "message");
      52                 :          1 :   g_log ("ba", G_LOG_LEVEL_DEBUG, "message");
      53                 :          1 :   g_log ("bu", G_LOG_LEVEL_INFO, "message");
      54                 :          1 :   g_log ("ba", G_LOG_LEVEL_INFO, "message");
      55                 :            : 
      56                 :          1 :   g_assert_cmpint (log_count, ==, 1);
      57                 :            : 
      58                 :          1 :   g_log_remove_handler ("bu", id);
      59                 :          1 : }
      60                 :            : 
      61                 :            : static void
      62                 :          0 : test_default_handler_error (void)
      63                 :            : {
      64                 :          0 :   g_log_set_default_handler (g_log_default_handler, NULL);
      65                 :          0 :   g_error ("message1");
      66                 :            :   exit (0);
      67                 :            : }
      68                 :            : 
      69                 :            : static void
      70                 :          0 : test_default_handler_error_stderr (void)
      71                 :            : {
      72                 :          0 :   g_log_writer_default_set_use_stderr (FALSE);
      73                 :          0 :   g_log_set_default_handler (g_log_default_handler, NULL);
      74                 :          0 :   g_error ("message1");
      75                 :            :   exit (0);
      76                 :            : }
      77                 :            : 
      78                 :            : static void
      79                 :          0 : test_default_handler_critical_stderr (void)
      80                 :            : {
      81                 :          0 :   g_log_writer_default_set_use_stderr (TRUE);
      82                 :          0 :   g_log_set_default_handler (g_log_default_handler, NULL);
      83                 :          0 :   g_critical ("message2");
      84                 :          0 :   exit (0);
      85                 :            : }
      86                 :            : 
      87                 :            : static void
      88                 :          0 : test_default_handler_critical (void)
      89                 :            : {
      90                 :          0 :   g_log_writer_default_set_use_stderr (FALSE);
      91                 :          0 :   g_log_set_default_handler (g_log_default_handler, NULL);
      92                 :          0 :   g_critical ("message2");
      93                 :          0 :   exit (0);
      94                 :            : }
      95                 :            : 
      96                 :            : static void
      97                 :          0 : test_default_handler_warning_stderr (void)
      98                 :            : {
      99                 :          0 :   g_log_writer_default_set_use_stderr (TRUE);
     100                 :          0 :   g_log_set_default_handler (g_log_default_handler, NULL);
     101                 :          0 :   g_warning ("message3");
     102                 :          0 :   exit (0);
     103                 :            : }
     104                 :            : 
     105                 :            : static void
     106                 :          0 : test_default_handler_warning (void)
     107                 :            : {
     108                 :          0 :   g_log_writer_default_set_use_stderr (FALSE);
     109                 :          0 :   g_log_set_default_handler (g_log_default_handler, NULL);
     110                 :          0 :   g_warning ("message3");
     111                 :          0 :   exit (0);
     112                 :            : }
     113                 :            : 
     114                 :            : static void
     115                 :          1 : test_default_handler_message (void)
     116                 :            : {
     117                 :          1 :   g_log_writer_default_set_use_stderr (FALSE);
     118                 :          1 :   g_log_set_default_handler (g_log_default_handler, NULL);
     119                 :          1 :   g_message ("message4");
     120                 :          1 :   exit (0);
     121                 :            : }
     122                 :            : 
     123                 :            : static void
     124                 :          1 : test_default_handler_message_stderr (void)
     125                 :            : {
     126                 :          1 :   g_log_writer_default_set_use_stderr (TRUE);
     127                 :          1 :   g_log_set_default_handler (g_log_default_handler, NULL);
     128                 :          1 :   g_message ("message4");
     129                 :          1 :   exit (0);
     130                 :            : }
     131                 :            : 
     132                 :            : static void
     133                 :          1 : test_default_handler_info (void)
     134                 :            : {
     135                 :          1 :   g_log_writer_default_set_use_stderr (FALSE);
     136                 :          1 :   g_log_set_default_handler (g_log_default_handler, NULL);
     137                 :          1 :   g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "message5");
     138                 :          1 :   exit (0);
     139                 :            : }
     140                 :            : 
     141                 :            : static void
     142                 :          1 : test_default_handler_info_stderr (void)
     143                 :            : {
     144                 :          1 :   g_log_writer_default_set_use_stderr (TRUE);
     145                 :          1 :   g_log_set_default_handler (g_log_default_handler, NULL);
     146                 :          1 :   g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "message5");
     147                 :          1 :   exit (0);
     148                 :            : }
     149                 :            : 
     150                 :            : static void
     151                 :          1 : test_default_handler_bar_info (void)
     152                 :            : {
     153                 :          1 :   g_log_writer_default_set_use_stderr (FALSE);
     154                 :          1 :   g_log_set_default_handler (g_log_default_handler, NULL);
     155                 :            : 
     156                 :          1 :   g_setenv ("G_MESSAGES_DEBUG", "foo bar baz", TRUE);
     157                 :            : 
     158                 :          1 :   g_log ("bar", G_LOG_LEVEL_INFO, "message5");
     159                 :          1 :   exit (0);
     160                 :            : }
     161                 :            : 
     162                 :            : static void
     163                 :          1 : test_default_handler_baz_debug (void)
     164                 :            : {
     165                 :          1 :   g_log_writer_default_set_use_stderr (FALSE);
     166                 :          1 :   g_log_set_default_handler (g_log_default_handler, NULL);
     167                 :            : 
     168                 :          1 :   g_setenv ("G_MESSAGES_DEBUG", "foo bar baz", TRUE);
     169                 :            : 
     170                 :          1 :   g_log ("baz", G_LOG_LEVEL_DEBUG, "message6");
     171                 :          1 :   exit (0);
     172                 :            : }
     173                 :            : 
     174                 :            : static void
     175                 :          1 : test_default_handler_debug (void)
     176                 :            : {
     177                 :          1 :   g_log_writer_default_set_use_stderr (FALSE);
     178                 :          1 :   g_log_set_default_handler (g_log_default_handler, NULL);
     179                 :            : 
     180                 :          1 :   g_setenv ("G_MESSAGES_DEBUG", "all", TRUE);
     181                 :            : 
     182                 :          1 :   g_log ("foo", G_LOG_LEVEL_DEBUG, "6");
     183                 :          1 :   g_log ("bar", G_LOG_LEVEL_DEBUG, "6");
     184                 :          1 :   g_log ("baz", G_LOG_LEVEL_DEBUG, "6");
     185                 :            : 
     186                 :          1 :   exit (0);
     187                 :            : }
     188                 :            : 
     189                 :            : static void
     190                 :          1 : test_default_handler_debug_stderr (void)
     191                 :            : {
     192                 :          1 :   g_log_writer_default_set_use_stderr (TRUE);
     193                 :          1 :   g_log_set_default_handler (g_log_default_handler, NULL);
     194                 :            : 
     195                 :          1 :   g_setenv ("G_MESSAGES_DEBUG", "all", TRUE);
     196                 :            : 
     197                 :          1 :   g_log ("foo", G_LOG_LEVEL_DEBUG, "6");
     198                 :          1 :   g_log ("bar", G_LOG_LEVEL_DEBUG, "6");
     199                 :          1 :   g_log ("baz", G_LOG_LEVEL_DEBUG, "6");
     200                 :            : 
     201                 :          1 :   exit (0);
     202                 :            : }
     203                 :            : 
     204                 :            : static void
     205                 :          1 : test_default_handler_would_drop_env5 (void)
     206                 :            : {
     207                 :          1 :   g_setenv ("G_MESSAGES_DEBUG", "foobar", TRUE);
     208                 :            : 
     209                 :          1 :   g_assert_true (g_log_writer_default_would_drop (G_LOG_LEVEL_DEBUG, "foo"));
     210                 :          1 :   g_assert_true (g_log_writer_default_would_drop (G_LOG_LEVEL_DEBUG, "bar"));
     211                 :          1 : }
     212                 :            : 
     213                 :            : static void
     214                 :          1 : test_default_handler_would_drop_env4 (void)
     215                 :            : {
     216                 :          1 :   g_setenv ("G_MESSAGES_DEBUG", "all", TRUE);
     217                 :            : 
     218                 :          1 :   g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_ERROR, "foo"));
     219                 :          1 :   g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_CRITICAL, "foo"));
     220                 :          1 :   g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_WARNING, "foo"));
     221                 :          1 :   g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_MESSAGE, "foo"));
     222                 :          1 :   g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_INFO, "foo"));
     223                 :          1 :   g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_DEBUG, "foo"));
     224                 :          1 :   g_assert_false (g_log_writer_default_would_drop (1<<G_LOG_LEVEL_USER_SHIFT, "foo"));
     225                 :          1 : }
     226                 :            : 
     227                 :            : static void
     228                 :          1 : test_default_handler_would_drop_env3 (void)
     229                 :            : {
     230                 :          1 :   g_setenv ("G_MESSAGES_DEBUG", "foo bar", TRUE);
     231                 :            : 
     232                 :          1 :   g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_ERROR, "foo"));
     233                 :          1 :   g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_CRITICAL, "foo"));
     234                 :          1 :   g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_WARNING, "foo"));
     235                 :          1 :   g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_MESSAGE, "foo"));
     236                 :          1 :   g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_INFO, "foo"));
     237                 :          1 :   g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_DEBUG, "foo"));
     238                 :          1 :   g_assert_false (g_log_writer_default_would_drop (1<<G_LOG_LEVEL_USER_SHIFT, "foo"));
     239                 :          1 : }
     240                 :            : 
     241                 :            : static void
     242                 :          1 : test_default_handler_would_drop_env2 (void)
     243                 :            : {
     244                 :          1 :   g_setenv ("G_MESSAGES_DEBUG", "  bar    baz ", TRUE);
     245                 :            : 
     246                 :          1 :   g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_ERROR, "foo"));
     247                 :          1 :   g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_CRITICAL, "foo"));
     248                 :          1 :   g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_WARNING, "foo"));
     249                 :          1 :   g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_MESSAGE, "foo"));
     250                 :          1 :   g_assert_true (g_log_writer_default_would_drop (G_LOG_LEVEL_INFO, "foo"));
     251                 :          1 :   g_assert_true (g_log_writer_default_would_drop (G_LOG_LEVEL_DEBUG, "foo"));
     252                 :          1 :   g_assert_false (g_log_writer_default_would_drop (1<<G_LOG_LEVEL_USER_SHIFT, "foo"));
     253                 :          1 : }
     254                 :            : 
     255                 :            : static void
     256                 :          1 : test_default_handler_would_drop_env1 (void)
     257                 :            : {
     258                 :          1 :   g_unsetenv ("G_MESSAGES_DEBUG");
     259                 :            : 
     260                 :          1 :   g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_ERROR, "foo"));
     261                 :          1 :   g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_CRITICAL, "foo"));
     262                 :          1 :   g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_WARNING, "foo"));
     263                 :          1 :   g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_MESSAGE, "foo"));
     264                 :          1 :   g_assert_true (g_log_writer_default_would_drop (G_LOG_LEVEL_INFO, "foo"));
     265                 :          1 :   g_assert_true (g_log_writer_default_would_drop (G_LOG_LEVEL_DEBUG, "foo"));
     266                 :          1 :   g_assert_false (g_log_writer_default_would_drop (1<<G_LOG_LEVEL_USER_SHIFT, "foo"));
     267                 :          1 : }
     268                 :            : 
     269                 :            : static void
     270                 :          1 : test_default_handler_would_drop (void)
     271                 :            : {
     272                 :          1 :   g_unsetenv ("G_MESSAGES_DEBUG");
     273                 :            : 
     274                 :          1 :   g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_ERROR, "foo"));
     275                 :          1 :   g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_CRITICAL, "foo"));
     276                 :          1 :   g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_WARNING, "foo"));
     277                 :          1 :   g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_MESSAGE, "foo"));
     278                 :          1 :   g_assert_true (g_log_writer_default_would_drop (G_LOG_LEVEL_INFO, "foo"));
     279                 :          1 :   g_assert_true (g_log_writer_default_would_drop (G_LOG_LEVEL_DEBUG, "foo"));
     280                 :          1 :   g_assert_false (g_log_writer_default_would_drop (1<<G_LOG_LEVEL_USER_SHIFT, "foo"));
     281                 :            : 
     282                 :            :   /* Expected to have no effect */
     283                 :          1 :   g_setenv ("G_MESSAGES_DEBUG", "all", TRUE);
     284                 :            : 
     285                 :          1 :   g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_ERROR, "foo"));
     286                 :          1 :   g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_CRITICAL, "foo"));
     287                 :          1 :   g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_WARNING, "foo"));
     288                 :          1 :   g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_MESSAGE, "foo"));
     289                 :          1 :   g_assert_true (g_log_writer_default_would_drop (G_LOG_LEVEL_INFO, "foo"));
     290                 :          1 :   g_assert_true (g_log_writer_default_would_drop (G_LOG_LEVEL_DEBUG, "foo"));
     291                 :          1 :   g_assert_false (g_log_writer_default_would_drop (1<<G_LOG_LEVEL_USER_SHIFT, "foo"));
     292                 :            : 
     293                 :            :   {
     294                 :          1 :     const gchar *domains[] = { "all", NULL };
     295                 :          1 :     g_log_writer_default_set_debug_domains (domains);
     296                 :            : 
     297                 :          1 :     g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_ERROR, "foo"));
     298                 :          1 :     g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_CRITICAL, "foo"));
     299                 :          1 :     g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_WARNING, "foo"));
     300                 :          1 :     g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_MESSAGE, "foo"));
     301                 :          1 :     g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_INFO, "foo"));
     302                 :          1 :     g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_DEBUG, "foo"));
     303                 :          1 :     g_assert_false (g_log_writer_default_would_drop (1<<G_LOG_LEVEL_USER_SHIFT, "foo"));
     304                 :            :   }
     305                 :            : 
     306                 :            :   {
     307                 :          1 :     const gchar *domains[] = { "foobar", NULL };
     308                 :          1 :     g_log_writer_default_set_debug_domains (domains);
     309                 :            : 
     310                 :          1 :     g_assert_true (g_log_writer_default_would_drop (G_LOG_LEVEL_DEBUG, "foo"));
     311                 :          1 :     g_assert_true (g_log_writer_default_would_drop (G_LOG_LEVEL_DEBUG, "bar"));
     312                 :            :   }
     313                 :            : 
     314                 :            :   {
     315                 :          1 :     const gchar *domains[] = { "foobar", "bar", NULL };
     316                 :          1 :     g_log_writer_default_set_debug_domains (domains);
     317                 :            : 
     318                 :          1 :     g_assert_true (g_log_writer_default_would_drop (G_LOG_LEVEL_DEBUG, "foo"));
     319                 :          1 :     g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_DEBUG, "bar"));
     320                 :            :   }
     321                 :            : 
     322                 :            :   {
     323                 :          1 :     const gchar *domains[] = { "foobar", "bar", "barfoo", NULL };
     324                 :          1 :     g_log_writer_default_set_debug_domains (domains);
     325                 :            : 
     326                 :          1 :     g_assert_true (g_log_writer_default_would_drop (G_LOG_LEVEL_DEBUG, "foo"));
     327                 :          1 :     g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_DEBUG, "bar"));
     328                 :            :   }
     329                 :            : 
     330                 :            :   {
     331                 :          1 :     const gchar *domains[] = { "", NULL };
     332                 :          1 :     g_log_writer_default_set_debug_domains (domains);
     333                 :            : 
     334                 :          1 :     g_assert_true (g_log_writer_default_would_drop (G_LOG_LEVEL_DEBUG, "foo"));
     335                 :          1 :     g_assert_true (g_log_writer_default_would_drop (G_LOG_LEVEL_DEBUG, "bar"));
     336                 :            :   }
     337                 :            : 
     338                 :            :   {
     339                 :          1 :     const gchar *domains[] = { "foobar", "bar", "foo", "barfoo", NULL };
     340                 :          1 :     g_log_writer_default_set_debug_domains (domains);
     341                 :            : 
     342                 :          1 :     g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_DEBUG, "foo"));
     343                 :          1 :     g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_DEBUG, "bar"));
     344                 :          1 :     g_assert_true (g_log_writer_default_would_drop (G_LOG_LEVEL_DEBUG, "baz"));
     345                 :            :   }
     346                 :            : 
     347                 :            :   {
     348                 :          1 :     const gchar *domains[] = { "foo", "bar", "baz", NULL };
     349                 :          1 :     g_log_writer_default_set_debug_domains (domains);
     350                 :            : 
     351                 :          1 :     g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_DEBUG, "foo"));
     352                 :          1 :     g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_DEBUG, "bar"));
     353                 :          1 :     g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_DEBUG, "baz"));
     354                 :            :   }
     355                 :            : 
     356                 :            : 
     357                 :            :   {
     358                 :          1 :     const gchar *domains[] = { "foo", NULL };
     359                 :          1 :     g_log_writer_default_set_debug_domains (domains);
     360                 :            : 
     361                 :          1 :     g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_DEBUG, "foo"));
     362                 :          1 :     g_assert_true (g_log_writer_default_would_drop (G_LOG_LEVEL_DEBUG, "bar"));
     363                 :          1 :     g_assert_true (g_log_writer_default_would_drop (G_LOG_LEVEL_DEBUG, "foobarbaz"));
     364                 :          1 :     g_assert_true (g_log_writer_default_would_drop (G_LOG_LEVEL_DEBUG, "barfoobaz"));
     365                 :          1 :     g_assert_true (g_log_writer_default_would_drop (G_LOG_LEVEL_DEBUG, "barbazfoo"));
     366                 :            :   }
     367                 :            : 
     368                 :            :   {
     369                 :          1 :     const gchar *domains[] = {NULL};
     370                 :          1 :     g_log_writer_default_set_debug_domains (domains);
     371                 :            :   
     372                 :          1 :     g_assert_true (g_log_writer_default_would_drop (G_LOG_LEVEL_DEBUG, "foo"));
     373                 :          1 :     g_assert_true (g_log_writer_default_would_drop (G_LOG_LEVEL_DEBUG, "bar"));
     374                 :            :   }
     375                 :            : 
     376                 :          1 :   g_log_writer_default_set_debug_domains (NULL);
     377                 :          1 :   g_assert_true (g_log_writer_default_would_drop (G_LOG_LEVEL_DEBUG, "foo"));
     378                 :          1 :   g_assert_true (g_log_writer_default_would_drop (G_LOG_LEVEL_DEBUG, "bar"));
     379                 :            : 
     380                 :          1 :   exit (0);
     381                 :            : }
     382                 :            : 
     383                 :            : static const gchar *
     384                 :     396836 : test_would_drop_robustness_random_domain (void)
     385                 :            : {
     386                 :            :   static const gchar *domains[] = { "foo", "bar", "baz", NULL };
     387                 :     396836 :   return domains[g_random_int_range (0, G_N_ELEMENTS (domains))];
     388                 :            : }
     389                 :            : 
     390                 :            : static gboolean test_would_drop_robustness_stopping;
     391                 :            : 
     392                 :            : static gpointer
     393                 :          2 : test_would_drop_robustness_thread (gpointer data)
     394                 :            : {
     395         [ +  + ]:      88590 :   while (!g_atomic_int_get (&test_would_drop_robustness_stopping))
     396                 :            :     {
     397                 :            :       gsize i;
     398                 :      88588 :       const gchar *domains[4] = { 0 };
     399                 :            : 
     400         [ +  + ]:     354352 :       for (i = 0; i < G_N_ELEMENTS (domains) - 1; i++)
     401                 :     265764 :         domains[i] = test_would_drop_robustness_random_domain ();
     402                 :            : 
     403                 :      88588 :       domains[G_N_ELEMENTS (domains) - 1] = 0;
     404                 :            :     
     405                 :      88588 :       g_log_writer_default_set_debug_domains (domains);
     406                 :            :     }
     407                 :          2 :   return NULL;
     408                 :            : }
     409                 :            : 
     410                 :            : static void
     411                 :          1 : test_default_handler_would_drop_robustness (void)
     412                 :            : {
     413                 :            :   GThread *threads[2];
     414                 :            :   gsize i;
     415                 :          1 :   guint counter = 1024 * 128;
     416                 :          1 :   g_log_writer_default_set_debug_domains (NULL);
     417                 :            : 
     418         [ +  + ]:          3 :   for (i = 0; i < G_N_ELEMENTS (threads); i++)
     419                 :          2 :     threads[i] = g_thread_new (NULL, test_would_drop_robustness_thread, NULL);
     420                 :            : 
     421         [ +  + ]:     131073 :   while (counter-- > 0)
     422                 :     131072 :     g_log_writer_default_would_drop (G_LOG_LEVEL_DEBUG, test_would_drop_robustness_random_domain ());
     423                 :            : 
     424                 :          1 :   g_atomic_int_set (&test_would_drop_robustness_stopping, TRUE);
     425         [ +  + ]:          3 :   for (i = 0; i < G_N_ELEMENTS (threads); i++)
     426                 :          2 :     g_thread_join (threads[i]);
     427                 :          1 : }
     428                 :            : 
     429                 :            : static void
     430                 :          1 : test_default_handler_0x400 (void)
     431                 :            : {
     432                 :          1 :   g_log_writer_default_set_use_stderr (FALSE);
     433                 :          1 :   g_log_set_default_handler (g_log_default_handler, NULL);
     434                 :          1 :   g_log (G_LOG_DOMAIN, 1<<10, "message7");
     435                 :          1 :   exit (0);
     436                 :            : }
     437                 :            : 
     438                 :            : static void
     439                 :          1 : test_default_handler (void)
     440                 :            : {
     441                 :          1 :   g_test_trap_subprocess ("/logging/default-handler/subprocess/error", 0,
     442                 :            :                           G_TEST_SUBPROCESS_DEFAULT);
     443                 :          1 :   g_test_trap_assert_failed ();
     444                 :          1 :   g_test_trap_assert_stderr ("*ERROR*message1*");
     445                 :            : 
     446                 :          1 :   g_test_trap_subprocess ("/logging/default-handler/subprocess/error-stderr", 0,
     447                 :            :                           G_TEST_SUBPROCESS_DEFAULT);
     448                 :          1 :   g_test_trap_assert_failed ();
     449                 :          1 :   g_test_trap_assert_stderr ("*ERROR*message1*");
     450                 :            : 
     451                 :          1 :   g_test_trap_subprocess ("/logging/default-handler/subprocess/critical", 0,
     452                 :            :                           G_TEST_SUBPROCESS_DEFAULT);
     453                 :          1 :   g_test_trap_assert_failed ();
     454                 :          1 :   g_test_trap_assert_stderr ("*CRITICAL*message2*");
     455                 :            : 
     456                 :          1 :   g_test_trap_subprocess ("/logging/default-handler/subprocess/critical-stderr", 0,
     457                 :            :                           G_TEST_SUBPROCESS_DEFAULT);
     458                 :          1 :   g_test_trap_assert_failed ();
     459                 :          1 :   g_test_trap_assert_stderr ("*CRITICAL*message2*");
     460                 :            : 
     461                 :          1 :   g_test_trap_subprocess ("/logging/default-handler/subprocess/warning", 0,
     462                 :            :                           G_TEST_SUBPROCESS_DEFAULT);
     463                 :          1 :   g_test_trap_assert_failed ();
     464                 :          1 :   g_test_trap_assert_stderr ("*WARNING*message3*");
     465                 :            : 
     466                 :          1 :   g_test_trap_subprocess ("/logging/default-handler/subprocess/warning-stderr", 0,
     467                 :            :                           G_TEST_SUBPROCESS_DEFAULT);
     468                 :          1 :   g_test_trap_assert_failed ();
     469                 :          1 :   g_test_trap_assert_stderr ("*WARNING*message3*");
     470                 :            : 
     471                 :          1 :   g_test_trap_subprocess ("/logging/default-handler/subprocess/message", 0,
     472                 :            :                           G_TEST_SUBPROCESS_DEFAULT);
     473                 :          1 :   g_test_trap_assert_passed ();
     474                 :          1 :   g_test_trap_assert_stderr ("*Message*message4*");
     475                 :            : 
     476                 :          1 :   g_test_trap_subprocess ("/logging/default-handler/subprocess/message-stderr", 0,
     477                 :            :                           G_TEST_SUBPROCESS_DEFAULT);
     478                 :          1 :   g_test_trap_assert_passed ();
     479                 :          1 :   g_test_trap_assert_stderr ("*Message*message4*");
     480                 :            : 
     481                 :          1 :   g_test_trap_subprocess ("/logging/default-handler/subprocess/info", 0,
     482                 :            :                           G_TEST_SUBPROCESS_DEFAULT);
     483                 :          1 :   g_test_trap_assert_passed ();
     484                 :          1 :   g_test_trap_assert_stdout_unmatched ("*INFO*message5*");
     485                 :            : 
     486                 :          1 :   g_test_trap_subprocess ("/logging/default-handler/subprocess/info-stderr", 0,
     487                 :            :                           G_TEST_SUBPROCESS_DEFAULT);
     488                 :          1 :   g_test_trap_assert_passed ();
     489                 :          1 :   g_test_trap_assert_stderr_unmatched ("*INFO*message5*");
     490                 :            : 
     491                 :          1 :   g_test_trap_subprocess ("/logging/default-handler/subprocess/bar-info", 0,
     492                 :            :                           G_TEST_SUBPROCESS_DEFAULT);
     493                 :          1 :   g_test_trap_assert_passed ();
     494                 :          1 :   g_test_trap_assert_stdout ("*INFO*message5*");
     495                 :            : 
     496                 :          1 :   g_test_trap_subprocess ("/logging/default-handler/subprocess/baz-debug", 0,
     497                 :            :                           G_TEST_SUBPROCESS_DEFAULT);
     498                 :          1 :   g_test_trap_assert_passed ();
     499                 :          1 :   g_test_trap_assert_stdout ("*DEBUG*message6*");
     500                 :            : 
     501                 :          1 :   g_test_trap_subprocess ("/logging/default-handler/subprocess/debug", 0,
     502                 :            :                           G_TEST_SUBPROCESS_DEFAULT);
     503                 :          1 :   g_test_trap_assert_passed ();
     504                 :          1 :   g_test_trap_assert_stdout ("*DEBUG*6*6*6*");
     505                 :            : 
     506                 :          1 :   g_test_trap_subprocess ("/logging/default-handler/subprocess/debug-stderr", 0,
     507                 :            :                           G_TEST_SUBPROCESS_DEFAULT);
     508                 :          1 :   g_test_trap_assert_passed ();
     509                 :          1 :   g_test_trap_assert_stdout_unmatched ("DEBUG");
     510                 :          1 :   g_test_trap_assert_stderr ("*DEBUG*6*6*6*");
     511                 :            : 
     512                 :          1 :   g_test_trap_subprocess ("/logging/default-handler/subprocess/0x400", 0,
     513                 :            :                           G_TEST_SUBPROCESS_DEFAULT);
     514                 :          1 :   g_test_trap_assert_passed ();
     515                 :          1 :   g_test_trap_assert_stdout ("*LOG-0x400*message7*");
     516                 :            : 
     517                 :          1 :   g_test_trap_subprocess ("/logging/default-handler/subprocess/would-drop", 0,
     518                 :            :                           G_TEST_SUBPROCESS_DEFAULT);
     519                 :          1 :   g_test_trap_assert_passed ();
     520                 :          1 :   g_test_trap_subprocess ("/logging/default-handler/subprocess/would-drop-env1", 0,
     521                 :            :                           G_TEST_SUBPROCESS_DEFAULT);
     522                 :          1 :   g_test_trap_assert_passed ();
     523                 :          1 :   g_test_trap_subprocess ("/logging/default-handler/subprocess/would-drop-env2", 0,
     524                 :            :                           G_TEST_SUBPROCESS_DEFAULT);
     525                 :          1 :   g_test_trap_assert_passed ();
     526                 :          1 :   g_test_trap_subprocess ("/logging/default-handler/subprocess/would-drop-env3", 0,
     527                 :            :                           G_TEST_SUBPROCESS_DEFAULT);
     528                 :          1 :   g_test_trap_assert_passed ();
     529                 :          1 :   g_test_trap_subprocess ("/logging/default-handler/subprocess/would-drop-env4", 0,
     530                 :            :                           G_TEST_SUBPROCESS_DEFAULT);
     531                 :          1 :   g_test_trap_assert_passed ();
     532                 :          1 :   g_test_trap_subprocess ("/logging/default-handler/subprocess/would-drop-env5", 0,
     533                 :            :                           G_TEST_SUBPROCESS_DEFAULT);
     534                 :          1 :   g_test_trap_assert_passed ();
     535                 :          1 :   g_test_trap_subprocess ("/logging/default-handler/subprocess/would-drop-robustness", 0,
     536                 :            :                           G_TEST_SUBPROCESS_DEFAULT);
     537                 :          1 :   g_test_trap_assert_passed ();
     538                 :          1 : }
     539                 :            : 
     540                 :            : static void
     541                 :          1 : test_fatal_log_mask (void)
     542                 :            : {
     543         [ -  + ]:          1 :   if (g_test_subprocess ())
     544                 :            :     {
     545                 :          0 :       g_log_set_fatal_mask ("bu", G_LOG_LEVEL_INFO);
     546                 :          0 :       g_log ("bu", G_LOG_LEVEL_INFO, "fatal");
     547                 :          0 :       return;
     548                 :            :     }
     549                 :          1 :   g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_DEFAULT);
     550                 :          1 :   g_test_trap_assert_failed ();
     551                 :            :   /* G_LOG_LEVEL_INFO isn't printed by default */
     552                 :          1 :   g_test_trap_assert_stdout_unmatched ("*fatal*");
     553                 :            : }
     554                 :            : 
     555                 :            : static gint my_print_count = 0;
     556                 :            : static void
     557                 :          4 : my_print_handler (const gchar *text)
     558                 :            : {
     559                 :          4 :   my_print_count++;
     560                 :          4 : }
     561                 :            : 
     562                 :            : static void
     563                 :          2 : test_print_handler (void)
     564                 :            : {
     565                 :            :   GPrintFunc old_print_handler;
     566                 :            : 
     567                 :          2 :   old_print_handler = g_set_print_handler (my_print_handler);
     568                 :          2 :   g_assert_nonnull (old_print_handler);
     569                 :            : 
     570                 :          2 :   my_print_count = 0;
     571                 :          2 :   g_print ("bu ba");
     572                 :          2 :   g_assert_cmpint (my_print_count, ==, 1);
     573                 :            : 
     574         [ +  + ]:          2 :   if (g_test_subprocess ())
     575                 :            :     {
     576                 :          1 :       g_set_print_handler (NULL);
     577                 :          1 :       old_print_handler ("default handler\n");
     578                 :          1 :       g_print ("bu ba\n");
     579                 :          1 :       return;
     580                 :            :     }
     581                 :            : 
     582                 :          1 :   g_set_print_handler (old_print_handler);
     583                 :          1 :   g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_DEFAULT);
     584                 :          1 :   g_test_trap_assert_stdout ("*default handler" LINE_END "*");
     585                 :          1 :   g_test_trap_assert_stdout ("*bu ba" LINE_END "*");
     586                 :          1 :   g_test_trap_assert_stdout_unmatched ("*# default handler" LINE_END "*");
     587                 :          1 :   g_test_trap_assert_stdout_unmatched ("*# bu ba" LINE_END "*");
     588                 :          1 :   g_test_trap_has_passed ();
     589                 :            : }
     590                 :            : 
     591                 :            : static void
     592                 :          2 : test_printerr_handler (void)
     593                 :            : {
     594                 :            :   GPrintFunc old_printerr_handler;
     595                 :            : 
     596                 :          2 :   old_printerr_handler = g_set_printerr_handler (my_print_handler);
     597                 :          2 :   g_assert_nonnull (old_printerr_handler);
     598                 :            : 
     599                 :          2 :   my_print_count = 0;
     600                 :          2 :   g_printerr ("bu ba");
     601                 :          2 :   g_assert_cmpint (my_print_count, ==, 1);
     602                 :            : 
     603         [ +  + ]:          2 :   if (g_test_subprocess ())
     604                 :            :     {
     605                 :          1 :       g_set_printerr_handler (NULL);
     606                 :          1 :       old_printerr_handler ("default handler\n");
     607                 :          1 :       g_printerr ("bu ba\n");
     608                 :          1 :       return;
     609                 :            :     }
     610                 :            : 
     611                 :          1 :   g_set_printerr_handler (old_printerr_handler);
     612                 :          1 :   g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_DEFAULT);
     613                 :          1 :   g_test_trap_assert_stderr ("*default handler" LINE_END "*");
     614                 :          1 :   g_test_trap_assert_stderr ("*bu ba" LINE_END "*");
     615                 :          1 :   g_test_trap_has_passed ();
     616                 :            : }
     617                 :            : 
     618                 :            : static char *fail_str = "foo";
     619                 :            : static char *log_str = "bar";
     620                 :            : 
     621                 :            : static gboolean
     622                 :          1 : good_failure_handler (const gchar    *log_domain,
     623                 :            :                       GLogLevelFlags  log_level,
     624                 :            :                       const gchar    *msg,
     625                 :            :                       gpointer        user_data)
     626                 :            : {
     627                 :          1 :   g_test_message ("The Good Fail Message Handler\n");
     628                 :          1 :   g_assert ((char *)user_data != log_str);
     629                 :          1 :   g_assert ((char *)user_data == fail_str);
     630                 :            : 
     631                 :          1 :   return FALSE;
     632                 :            : }
     633                 :            : 
     634                 :            : static gboolean
     635                 :          0 : bad_failure_handler (const gchar    *log_domain,
     636                 :            :                      GLogLevelFlags  log_level,
     637                 :            :                      const gchar    *msg,
     638                 :            :                      gpointer        user_data)
     639                 :            : {
     640                 :          0 :   g_test_message ("The Bad Fail Message Handler\n");
     641                 :          0 :   g_assert ((char *)user_data == log_str);
     642                 :          0 :   g_assert ((char *)user_data != fail_str);
     643                 :            : 
     644                 :          0 :   return FALSE;
     645                 :            : }
     646                 :            : 
     647                 :            : static void
     648                 :          1 : test_handler (const gchar    *log_domain,
     649                 :            :               GLogLevelFlags  log_level,
     650                 :            :               const gchar    *msg,
     651                 :            :               gpointer        user_data)
     652                 :            : {
     653                 :          1 :   g_test_message ("The Log Message Handler\n");
     654                 :          1 :   g_assert ((char *)user_data != fail_str);
     655                 :          1 :   g_assert ((char *)user_data == log_str);
     656                 :          1 : }
     657                 :            : 
     658                 :            : static void
     659                 :          1 : bug653052 (void)
     660                 :            : {
     661                 :          1 :   g_test_bug ("https://bugzilla.gnome.org/show_bug.cgi?id=653052");
     662                 :            : 
     663                 :          1 :   g_test_log_set_fatal_handler (good_failure_handler, fail_str);
     664                 :          1 :   g_log_set_default_handler (test_handler, log_str);
     665                 :            : 
     666                 :          1 :   g_return_if_fail (0);
     667                 :            : 
     668                 :            :   g_test_log_set_fatal_handler (bad_failure_handler, fail_str);
     669                 :            :   g_log_set_default_handler (test_handler, log_str);
     670                 :            : 
     671                 :            :   g_return_if_fail (0);
     672                 :            : }
     673                 :            : 
     674                 :            : static void
     675                 :          1 : test_gibberish (void)
     676                 :            : {
     677         [ -  + ]:          1 :   if (g_test_subprocess ())
     678                 :            :     {
     679                 :          0 :       g_warning ("bla bla \236\237\190");
     680                 :          0 :       return;
     681                 :            :     }
     682                 :          1 :   g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_DEFAULT);
     683                 :          1 :   g_test_trap_assert_failed ();
     684                 :          1 :   g_test_trap_assert_stderr ("*bla bla \\x9e\\x9f\\u000190*");
     685                 :            : }
     686                 :            : 
     687                 :            : static GLogWriterOutput
     688                 :          5 : null_log_writer (GLogLevelFlags   log_level,
     689                 :            :                  const GLogField *fields,
     690                 :            :                  gsize            n_fields,
     691                 :            :                  gpointer         user_data)
     692                 :            : {
     693                 :          5 :   log_count++;
     694                 :          5 :   return G_LOG_WRITER_HANDLED;
     695                 :            : }
     696                 :            : 
     697                 :            : typedef struct {
     698                 :            :   const GLogField *fields;
     699                 :            :   gsize n_fields;
     700                 :            : } ExpectedMessage;
     701                 :            : 
     702                 :            : static gboolean
     703                 :         57 : compare_field (const GLogField *f1, const GLogField *f2)
     704                 :            : {
     705         [ +  + ]:         57 :   if (strcmp (f1->key, f2->key) != 0)
     706                 :         38 :     return FALSE;
     707         [ -  + ]:         19 :   if (f1->length != f2->length)
     708                 :          0 :     return FALSE;
     709                 :            : 
     710         [ +  + ]:         19 :   if (f1->length == -1)
     711                 :         18 :     return strcmp (f1->value, f2->value) == 0;
     712                 :            :   else
     713                 :          1 :     return memcmp (f1->value, f2->value, f1->length) == 0;
     714                 :            : }
     715                 :            : 
     716                 :            : static gboolean
     717                 :          4 : compare_fields (const GLogField *f1, gsize n1, const GLogField *f2, gsize n2)
     718                 :            : {
     719                 :            :   gsize i, j;
     720                 :            : 
     721         [ +  + ]:         23 :   for (i = 0; i < n1; i++)
     722                 :            :     {
     723         [ +  - ]:         57 :       for (j = 0; j < n2; j++)
     724                 :            :         {
     725         [ +  + ]:         57 :           if (compare_field (&f1[i], &f2[j]))
     726                 :         19 :             break;
     727                 :            :         }
     728         [ -  + ]:         19 :       if (j == n2)
     729                 :          0 :         return FALSE;
     730                 :            :     }
     731                 :            : 
     732                 :          4 :   return TRUE;
     733                 :            : }
     734                 :            : 
     735                 :            : static GSList *expected_messages = NULL;
     736                 :            : static const guchar binary_field[] = {1, 2, 3, 4, 5};
     737                 :            : 
     738                 :            : 
     739                 :            : static GLogWriterOutput
     740                 :          4 : expect_log_writer (GLogLevelFlags   log_level,
     741                 :            :                    const GLogField *fields,
     742                 :            :                    gsize            n_fields,
     743                 :            :                    gpointer         user_data)
     744                 :            : {
     745                 :          4 :   ExpectedMessage *expected = expected_messages->data;
     746                 :            : 
     747         [ +  - ]:          4 :   if (compare_fields (fields, n_fields, expected->fields, expected->n_fields))
     748                 :            :     {
     749                 :          4 :       expected_messages = g_slist_delete_link (expected_messages, expected_messages);
     750                 :            :     }
     751         [ #  # ]:          0 :   else if ((log_level & G_LOG_LEVEL_DEBUG) != G_LOG_LEVEL_DEBUG)
     752                 :            :     {
     753                 :            :       char *str;
     754                 :            : 
     755                 :          0 :       str = g_log_writer_format_fields (log_level, fields, n_fields, FALSE);
     756                 :          0 :       g_test_fail_printf ("Unexpected message: %s", str);
     757                 :          0 :       g_free (str);
     758                 :            :     }
     759                 :            : 
     760                 :          4 :   return G_LOG_WRITER_HANDLED;
     761                 :            : }
     762                 :            : 
     763                 :            : static void
     764                 :          2 : test_structured_logging_no_state (void)
     765                 :            : {
     766                 :            :   /* Test has to run in a subprocess as it calls g_log_set_writer_func(), which
     767                 :            :    * can only be called once per process. */
     768         [ +  + ]:          2 :   if (g_test_subprocess ())
     769                 :            :     {
     770                 :          1 :       gpointer some_pointer = GUINT_TO_POINTER (0x100);
     771                 :          1 :       guint some_integer = 123;
     772                 :            : 
     773                 :          1 :       log_count = 0;
     774                 :          1 :       g_log_set_writer_func (null_log_writer, NULL, NULL);
     775                 :            : 
     776                 :          1 :       g_log_structured ("some-domain", G_LOG_LEVEL_MESSAGE,
     777                 :            :                         "MESSAGE_ID", "06d4df59e6c24647bfe69d2c27ef0b4e",
     778                 :            :                         "MY_APPLICATION_CUSTOM_FIELD", "some debug string",
     779                 :            :                         "MESSAGE", "This is a debug message about pointer %p and integer %u.",
     780                 :            :                         some_pointer, some_integer);
     781                 :            : 
     782                 :          1 :       g_assert_cmpint (log_count, ==, 1);
     783                 :            :     }
     784                 :            :   else
     785                 :            :     {
     786                 :          1 :       g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_DEFAULT);
     787                 :          1 :       g_test_trap_assert_passed ();
     788                 :            :     }
     789                 :          2 : }
     790                 :            : 
     791                 :            : static void
     792                 :          2 : test_structured_logging_some_state (void)
     793                 :            : {
     794                 :            :   /* Test has to run in a subprocess as it calls g_log_set_writer_func(), which
     795                 :            :    * can only be called once per process. */
     796         [ +  + ]:          2 :   if (g_test_subprocess ())
     797                 :            :     {
     798                 :          1 :       gpointer state_object = NULL;  /* this must not be dereferenced */
     799                 :          1 :       const GLogField fields[] = {
     800                 :            :         { "MESSAGE", "This is a debug message.", -1 },
     801                 :            :         { "MESSAGE_ID", "fcfb2e1e65c3494386b74878f1abf893", -1 },
     802                 :            :         { "MY_APPLICATION_CUSTOM_FIELD", "some debug string", -1 },
     803                 :            :         { "MY_APPLICATION_STATE", state_object, 0 },
     804                 :            :       };
     805                 :            : 
     806                 :          1 :       log_count = 0;
     807                 :          1 :       g_log_set_writer_func (null_log_writer, NULL, NULL);
     808                 :            : 
     809                 :          1 :       g_log_structured_array (G_LOG_LEVEL_DEBUG, fields, G_N_ELEMENTS (fields));
     810                 :            : 
     811                 :          1 :       g_assert_cmpint (log_count, ==, 1);
     812                 :            :     }
     813                 :            :   else
     814                 :            :     {
     815                 :          1 :       g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_DEFAULT);
     816                 :          1 :       g_test_trap_assert_passed ();
     817                 :            :     }
     818                 :          2 : }
     819                 :            : 
     820                 :            : static void
     821                 :          2 : test_structured_logging_robustness (void)
     822                 :            : {
     823                 :            :   /* Test has to run in a subprocess as it calls g_log_set_writer_func(), which
     824                 :            :    * can only be called once per process. */
     825         [ +  + ]:          2 :   if (g_test_subprocess ())
     826                 :            :     {
     827                 :          1 :       log_count = 0;
     828                 :          1 :       g_log_set_writer_func (null_log_writer, NULL, NULL);
     829                 :            : 
     830                 :            :       /* NULL log_domain shouldn't crash */
     831                 :          1 :       g_log (NULL, G_LOG_LEVEL_MESSAGE, "Test");
     832                 :          1 :       g_log_structured (NULL, G_LOG_LEVEL_MESSAGE, "MESSAGE", "Test");
     833                 :            : 
     834                 :          1 :       g_assert_cmpint (log_count, ==, 2);
     835                 :            :     }
     836                 :            :   else
     837                 :            :     {
     838                 :          1 :       g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_DEFAULT);
     839                 :          1 :       g_test_trap_assert_passed ();
     840                 :            :     }
     841                 :          2 : }
     842                 :            : 
     843                 :            : static void
     844                 :          2 : test_structured_logging_roundtrip1 (void)
     845                 :            : {
     846                 :            :   /* Test has to run in a subprocess as it calls g_log_set_writer_func(), which
     847                 :            :    * can only be called once per process. */
     848         [ +  + ]:          2 :   if (g_test_subprocess ())
     849                 :            :     {
     850                 :          1 :       gpointer some_pointer = GUINT_TO_POINTER (0x100);
     851                 :          1 :       gint some_integer = 123;
     852                 :            :       gchar message[200];
     853                 :          1 :       GLogField fields[] = {
     854                 :            :         { "GLIB_DOMAIN", "some-domain", -1 },
     855                 :            :         { "PRIORITY", "5", -1 },
     856                 :            :         { "MESSAGE", "String assigned using g_snprintf() below", -1 },
     857                 :            :         { "MESSAGE_ID", "fcfb2e1e65c3494386b74878f1abf893", -1 },
     858                 :            :         { "MY_APPLICATION_CUSTOM_FIELD", "some debug string", -1 }
     859                 :            :       };
     860                 :          1 :       ExpectedMessage expected = { fields, 5 };
     861                 :            : 
     862                 :            :       /* %p format is implementation defined and depends on the platform */
     863                 :          1 :       g_snprintf (message, sizeof (message),
     864                 :            :                   "This is a debug message about pointer %p and integer %u.",
     865                 :            :                   some_pointer, some_integer);
     866                 :          1 :       fields[2].value = message;
     867                 :            : 
     868                 :          1 :       expected_messages = g_slist_append (NULL, &expected);
     869                 :          1 :       g_log_set_writer_func (expect_log_writer, NULL, NULL);
     870                 :            : 
     871                 :          1 :       g_log_structured ("some-domain", G_LOG_LEVEL_MESSAGE,
     872                 :            :                         "MESSAGE_ID", "fcfb2e1e65c3494386b74878f1abf893",
     873                 :            :                         "MY_APPLICATION_CUSTOM_FIELD", "some debug string",
     874                 :            :                         "MESSAGE", "This is a debug message about pointer %p and integer %u.",
     875                 :            :                         some_pointer, some_integer);
     876                 :            : 
     877         [ -  + ]:          1 :       if (expected_messages != NULL)
     878                 :            :         {
     879                 :            :           char *str;
     880                 :          0 :           ExpectedMessage *msg = expected_messages->data;
     881                 :            : 
     882                 :          0 :           str = g_log_writer_format_fields (0, msg->fields, msg->n_fields, FALSE);
     883                 :          0 :           g_test_fail_printf ("Unexpected message: %s", str);
     884                 :          0 :           g_free (str);
     885                 :            :         }
     886                 :            :     }
     887                 :            :   else
     888                 :            :     {
     889                 :          1 :       g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_DEFAULT);
     890                 :          1 :       g_test_trap_assert_passed ();
     891                 :            :     }
     892                 :          2 : }
     893                 :            : 
     894                 :            : static void
     895                 :          2 : test_structured_logging_roundtrip2 (void)
     896                 :            : {
     897                 :            :   /* Test has to run in a subprocess as it calls g_log_set_writer_func(), which
     898                 :            :    * can only be called once per process. */
     899         [ +  + ]:          2 :   if (g_test_subprocess ())
     900                 :            :     {
     901                 :          1 :       const gchar *some_string = "abc";
     902                 :          1 :       const GLogField fields[] = {
     903                 :            :         { "GLIB_DOMAIN", "some-domain", -1 },
     904                 :            :         { "PRIORITY", "5", -1 },
     905                 :            :         { "MESSAGE", "This is a debug message about string 'abc'.", -1 },
     906                 :            :         { "MESSAGE_ID", "fcfb2e1e65c3494386b74878f1abf893", -1 },
     907                 :            :         { "MY_APPLICATION_CUSTOM_FIELD", "some debug string", -1 }
     908                 :            :       };
     909                 :          1 :       ExpectedMessage expected = { fields, 5 };
     910                 :            : 
     911                 :          1 :       expected_messages = g_slist_append (NULL, &expected);
     912                 :          1 :       g_log_set_writer_func (expect_log_writer, NULL, NULL);
     913                 :            : 
     914                 :          1 :       g_log_structured ("some-domain", G_LOG_LEVEL_MESSAGE,
     915                 :            :                         "MESSAGE_ID", "fcfb2e1e65c3494386b74878f1abf893",
     916                 :            :                         "MY_APPLICATION_CUSTOM_FIELD", "some debug string",
     917                 :            :                         "MESSAGE", "This is a debug message about string '%s'.",
     918                 :            :                         some_string);
     919                 :            : 
     920                 :          1 :       g_assert (expected_messages == NULL);
     921                 :            :     }
     922                 :            :   else
     923                 :            :     {
     924                 :          1 :       g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_DEFAULT);
     925                 :          1 :       g_test_trap_assert_passed ();
     926                 :            :     }
     927                 :          2 : }
     928                 :            : 
     929                 :            : static void
     930                 :          2 : test_structured_logging_roundtrip3 (void)
     931                 :            : {
     932                 :            :   /* Test has to run in a subprocess as it calls g_log_set_writer_func(), which
     933                 :            :    * can only be called once per process. */
     934         [ +  + ]:          2 :   if (g_test_subprocess ())
     935                 :            :     {
     936                 :          1 :       const GLogField fields[] = {
     937                 :            :         { "GLIB_DOMAIN", "some-domain", -1 },
     938                 :            :         { "PRIORITY", "4", -1 },
     939                 :            :         { "MESSAGE", "Test test test.", -1 }
     940                 :            :       };
     941                 :          1 :       ExpectedMessage expected = { fields, 3 };
     942                 :            : 
     943                 :          1 :       expected_messages = g_slist_append (NULL, &expected);
     944                 :          1 :       g_log_set_writer_func (expect_log_writer, NULL, NULL);
     945                 :            : 
     946                 :          1 :       g_log_structured ("some-domain", G_LOG_LEVEL_WARNING,
     947                 :            :                         "MESSAGE", "Test test test.");
     948                 :            : 
     949                 :          1 :       g_assert (expected_messages == NULL);
     950                 :            :     }
     951                 :            :   else
     952                 :            :     {
     953                 :          1 :       g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_DEFAULT);
     954                 :          1 :       g_test_trap_assert_passed ();
     955                 :            :     }
     956                 :          2 : }
     957                 :            : 
     958                 :            : static GVariant *
     959                 :          2 : create_variant_fields (void)
     960                 :            : {
     961                 :            :   GVariant *binary;
     962                 :            :   GVariantBuilder builder;
     963                 :            : 
     964                 :          2 :   binary = g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE, binary_field, G_N_ELEMENTS (binary_field), sizeof (binary_field[0]));
     965                 :            : 
     966                 :          2 :   g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
     967                 :          2 :   g_variant_builder_add (&builder, "{sv}", "MESSAGE_ID", g_variant_new_string ("06d4df59e6c24647bfe69d2c27ef0b4e"));
     968                 :          2 :   g_variant_builder_add (&builder, "{sv}", "MESSAGE", g_variant_new_string ("This is a debug message"));
     969                 :          2 :   g_variant_builder_add (&builder, "{sv}", "MY_APPLICATION_CUSTOM_FIELD", g_variant_new_string ("some debug string"));
     970                 :          2 :   g_variant_builder_add (&builder, "{sv}", "MY_APPLICATION_CUSTOM_FIELD_BINARY", binary);
     971                 :            : 
     972                 :          2 :   return g_variant_builder_end (&builder);
     973                 :            : }
     974                 :            : 
     975                 :            : static void
     976                 :          2 : test_structured_logging_variant1 (void)
     977                 :            : {
     978                 :            :   /* Test has to run in a subprocess as it calls g_log_set_writer_func(), which
     979                 :            :    * can only be called once per process. */
     980         [ +  + ]:          2 :   if (g_test_subprocess ())
     981                 :            :     {
     982                 :          1 :       GVariant *v = create_variant_fields ();
     983                 :            : 
     984                 :          1 :       log_count = 0;
     985                 :          1 :       g_log_set_writer_func (null_log_writer, NULL, NULL);
     986                 :            : 
     987                 :          1 :       g_log_variant ("some-domain", G_LOG_LEVEL_MESSAGE, v);
     988                 :          1 :       g_variant_unref (v);
     989                 :          1 :       g_assert_cmpint (log_count, ==, 1);
     990                 :            :     }
     991                 :            :   else
     992                 :            :     {
     993                 :          1 :       g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_DEFAULT);
     994                 :          1 :       g_test_trap_assert_passed ();
     995                 :            :     }
     996                 :          2 : }
     997                 :            : 
     998                 :            : static void
     999                 :          2 : test_structured_logging_variant2 (void)
    1000                 :            : {
    1001                 :            :   /* Test has to run in a subprocess as it calls g_log_set_writer_func(), which
    1002                 :            :    * can only be called once per process. */
    1003         [ +  + ]:          2 :   if (g_test_subprocess ())
    1004                 :            :     {
    1005                 :          1 :       const GLogField fields[] = {
    1006                 :            :         { "GLIB_DOMAIN", "some-domain", -1 },
    1007                 :            :         { "PRIORITY", "5", -1 },
    1008                 :            :         { "MESSAGE", "This is a debug message", -1 },
    1009                 :            :         { "MESSAGE_ID", "06d4df59e6c24647bfe69d2c27ef0b4e", -1 },
    1010                 :            :         { "MY_APPLICATION_CUSTOM_FIELD", "some debug string", -1 },
    1011                 :            :         { "MY_APPLICATION_CUSTOM_FIELD_BINARY", binary_field, sizeof (binary_field) }
    1012                 :            :       };
    1013                 :          1 :       ExpectedMessage expected = { fields, 6 };
    1014                 :          1 :       GVariant *v = create_variant_fields ();
    1015                 :            : 
    1016                 :          1 :       expected_messages = g_slist_append (NULL, &expected);
    1017                 :          1 :       g_log_set_writer_func (expect_log_writer, NULL, NULL);
    1018                 :            : 
    1019                 :          1 :       g_log_variant ("some-domain", G_LOG_LEVEL_MESSAGE, v);
    1020                 :          1 :       g_variant_unref (v);
    1021                 :          1 :       g_assert (expected_messages == NULL);
    1022                 :            :     }
    1023                 :            :   else
    1024                 :            :     {
    1025                 :          1 :       g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_DEFAULT);
    1026                 :          1 :       g_test_trap_assert_passed ();
    1027                 :            :     }
    1028                 :          2 : }
    1029                 :            : 
    1030                 :            : static void
    1031                 :          1 : test_structured_logging_set_writer_func_twice (void)
    1032                 :            : {
    1033                 :            :   /* Test has to run in a subprocess as it calls g_log_set_writer_func() and
    1034                 :            :    * causes an error. */
    1035         [ -  + ]:          1 :   if (g_test_subprocess ())
    1036                 :            :     {
    1037                 :          0 :       g_log_set_writer_func (null_log_writer, NULL, NULL);
    1038                 :          0 :       g_log_set_writer_func (expect_log_writer, NULL, NULL);
    1039                 :            :     }
    1040                 :            :   else
    1041                 :            :     {
    1042                 :          1 :       g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_DEFAULT);
    1043                 :          1 :       g_test_trap_assert_failed ();
    1044                 :            :     }
    1045                 :          1 : }
    1046                 :            : 
    1047                 :            : int
    1048                 :         27 : main (int argc, char *argv[])
    1049                 :            : {
    1050                 :         27 :   g_unsetenv ("G_MESSAGES_DEBUG");
    1051                 :            : 
    1052                 :         27 :   g_test_init (&argc, &argv, NULL);
    1053                 :            : 
    1054                 :         27 :   g_test_add_func ("/logging/default-handler", test_default_handler);
    1055                 :         27 :   g_test_add_func ("/logging/default-handler/subprocess/error", test_default_handler_error);
    1056                 :         27 :   g_test_add_func ("/logging/default-handler/subprocess/error-stderr", test_default_handler_error_stderr);
    1057                 :         27 :   g_test_add_func ("/logging/default-handler/subprocess/critical", test_default_handler_critical);
    1058                 :         27 :   g_test_add_func ("/logging/default-handler/subprocess/critical-stderr", test_default_handler_critical_stderr);
    1059                 :         27 :   g_test_add_func ("/logging/default-handler/subprocess/warning", test_default_handler_warning);
    1060                 :         27 :   g_test_add_func ("/logging/default-handler/subprocess/warning-stderr", test_default_handler_warning_stderr);
    1061                 :         27 :   g_test_add_func ("/logging/default-handler/subprocess/message", test_default_handler_message);
    1062                 :         27 :   g_test_add_func ("/logging/default-handler/subprocess/message-stderr", test_default_handler_message_stderr);
    1063                 :         27 :   g_test_add_func ("/logging/default-handler/subprocess/info", test_default_handler_info);
    1064                 :         27 :   g_test_add_func ("/logging/default-handler/subprocess/info-stderr", test_default_handler_info_stderr);
    1065                 :         27 :   g_test_add_func ("/logging/default-handler/subprocess/bar-info", test_default_handler_bar_info);
    1066                 :         27 :   g_test_add_func ("/logging/default-handler/subprocess/baz-debug", test_default_handler_baz_debug);
    1067                 :         27 :   g_test_add_func ("/logging/default-handler/subprocess/debug", test_default_handler_debug);
    1068                 :         27 :   g_test_add_func ("/logging/default-handler/subprocess/debug-stderr", test_default_handler_debug_stderr);
    1069                 :         27 :   g_test_add_func ("/logging/default-handler/subprocess/0x400", test_default_handler_0x400);
    1070                 :         27 :   g_test_add_func ("/logging/default-handler/subprocess/would-drop", test_default_handler_would_drop);
    1071                 :         27 :   g_test_add_func ("/logging/default-handler/subprocess/would-drop-env1", test_default_handler_would_drop_env1);
    1072                 :         27 :   g_test_add_func ("/logging/default-handler/subprocess/would-drop-env2", test_default_handler_would_drop_env2);
    1073                 :         27 :   g_test_add_func ("/logging/default-handler/subprocess/would-drop-env3", test_default_handler_would_drop_env3);
    1074                 :         27 :   g_test_add_func ("/logging/default-handler/subprocess/would-drop-env4", test_default_handler_would_drop_env4);
    1075                 :         27 :   g_test_add_func ("/logging/default-handler/subprocess/would-drop-env5", test_default_handler_would_drop_env5);
    1076                 :         27 :   g_test_add_func ("/logging/default-handler/subprocess/would-drop-robustness", test_default_handler_would_drop_robustness);
    1077                 :         27 :   g_test_add_func ("/logging/warnings", test_warnings);
    1078                 :         27 :   g_test_add_func ("/logging/fatal-log-mask", test_fatal_log_mask);
    1079                 :         27 :   g_test_add_func ("/logging/set-handler", test_set_handler);
    1080                 :         27 :   g_test_add_func ("/logging/print-handler", test_print_handler);
    1081                 :         27 :   g_test_add_func ("/logging/printerr-handler", test_printerr_handler);
    1082                 :         27 :   g_test_add_func ("/logging/653052", bug653052);
    1083                 :         27 :   g_test_add_func ("/logging/gibberish", test_gibberish);
    1084                 :         27 :   g_test_add_func ("/structured-logging/no-state", test_structured_logging_no_state);
    1085                 :         27 :   g_test_add_func ("/structured-logging/some-state", test_structured_logging_some_state);
    1086                 :         27 :   g_test_add_func ("/structured-logging/robustness", test_structured_logging_robustness);
    1087                 :         27 :   g_test_add_func ("/structured-logging/roundtrip1", test_structured_logging_roundtrip1);
    1088                 :         27 :   g_test_add_func ("/structured-logging/roundtrip2", test_structured_logging_roundtrip2);
    1089                 :         27 :   g_test_add_func ("/structured-logging/roundtrip3", test_structured_logging_roundtrip3);
    1090                 :         27 :   g_test_add_func ("/structured-logging/variant1", test_structured_logging_variant1);
    1091                 :         27 :   g_test_add_func ("/structured-logging/variant2", test_structured_logging_variant2);
    1092                 :         27 :   g_test_add_func ("/structured-logging/set-writer-func-twice", test_structured_logging_set_writer_func_twice);
    1093                 :            : 
    1094                 :         27 :   return g_test_run ();
    1095                 :            : }

Generated by: LCOV version 1.14