LCOV - code coverage report
Current view: top level - glib/gio/tests - gdbus-message.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 86 86 100.0 %
Date: 2024-04-23 05:16:05 Functions: 5 5 100.0 %
Branches: 6 6 100.0 %

           Branch data     Line data    Source code
       1                 :            : /* GLib testing framework examples and tests
       2                 :            :  *
       3                 :            :  * Copyright (C) 2008-2010 Red Hat, Inc.
       4                 :            :  *
       5                 :            :  * SPDX-License-Identifier: LGPL-2.1-or-later
       6                 :            :  *
       7                 :            :  * This library is free software; you can redistribute it and/or
       8                 :            :  * modify it under the terms of the GNU Lesser General Public
       9                 :            :  * License as published by the Free Software Foundation; either
      10                 :            :  * version 2.1 of the License, or (at your option) any later version.
      11                 :            :  *
      12                 :            :  * This library is distributed in the hope that it will be useful,
      13                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15                 :            :  * Lesser General Public License for more details.
      16                 :            :  *
      17                 :            :  * You should have received a copy of the GNU Lesser General
      18                 :            :  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
      19                 :            :  *
      20                 :            :  * Author: David Zeuthen <davidz@redhat.com>
      21                 :            :  */
      22                 :            : 
      23                 :            : #include <locale.h>
      24                 :            : #include <gio/gio.h>
      25                 :            : 
      26                 :            : /* ---------------------------------------------------------------------------------------------------- */
      27                 :            : 
      28                 :            : static void
      29                 :          1 : on_notify_locked (GObject    *object,
      30                 :            :                   GParamSpec *pspec,
      31                 :            :                   gpointer    user_data)
      32                 :            : {
      33                 :          1 :   gint *count = user_data;
      34                 :          1 :   *count += 1;
      35                 :          1 : }
      36                 :            : 
      37                 :            : static void
      38                 :          1 : message_lock (void)
      39                 :            : {
      40                 :            :   GDBusMessage *m;
      41                 :            :   gint count;
      42                 :            : 
      43                 :          1 :   count = 0;
      44                 :          1 :   m = g_dbus_message_new ();
      45                 :          1 :   g_signal_connect (m,
      46                 :            :                     "notify::locked",
      47                 :            :                     G_CALLBACK (on_notify_locked),
      48                 :            :                     &count);
      49                 :          1 :   g_assert (!g_dbus_message_get_locked (m));
      50                 :          1 :   g_dbus_message_lock (m);
      51                 :          1 :   g_assert (g_dbus_message_get_locked (m));
      52                 :          1 :   g_assert_cmpint (count, ==, 1);
      53                 :          1 :   g_dbus_message_lock (m);
      54                 :          1 :   g_assert (g_dbus_message_get_locked (m));
      55                 :          1 :   g_assert_cmpint (count, ==, 1);
      56                 :            : 
      57                 :          1 :   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
      58                 :            :                          "*Attempted to modify a locked message*");
      59                 :          1 :   g_dbus_message_set_serial (m, 42);
      60                 :          1 :   g_test_assert_expected_messages ();
      61                 :            : 
      62                 :          1 :   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
      63                 :            :                          "*Attempted to modify a locked message*");
      64                 :          1 :   g_dbus_message_set_byte_order (m, G_DBUS_MESSAGE_BYTE_ORDER_BIG_ENDIAN);
      65                 :          1 :   g_test_assert_expected_messages ();
      66                 :            : 
      67                 :          1 :   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
      68                 :            :                          "*Attempted to modify a locked message*");
      69                 :          1 :   g_dbus_message_set_message_type (m, G_DBUS_MESSAGE_TYPE_METHOD_CALL);
      70                 :          1 :   g_test_assert_expected_messages ();
      71                 :            : 
      72                 :          1 :   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
      73                 :            :                          "*Attempted to modify a locked message*");
      74                 :          1 :   g_dbus_message_set_flags (m, G_DBUS_MESSAGE_FLAGS_NONE);
      75                 :          1 :   g_test_assert_expected_messages ();
      76                 :            : 
      77                 :          1 :   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
      78                 :            :                          "*Attempted to modify a locked message*");
      79                 :          1 :   g_dbus_message_set_body (m, NULL);
      80                 :          1 :   g_test_assert_expected_messages ();
      81                 :            : 
      82                 :          1 :   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
      83                 :            :                          "*Attempted to modify a locked message*");
      84                 :          1 :   g_dbus_message_set_header (m, 0, NULL);
      85                 :          1 :   g_test_assert_expected_messages ();
      86                 :            : 
      87                 :          1 :   g_object_unref (m);
      88                 :          1 : }
      89                 :            : 
      90                 :            : /* ---------------------------------------------------------------------------------------------------- */
      91                 :            : 
      92                 :            : static void
      93                 :          1 : message_copy (void)
      94                 :            : {
      95                 :            :   GDBusMessage *m;
      96                 :            :   GDBusMessage *copy;
      97                 :            :   GError *error;
      98                 :            :   guchar *m_headers;
      99                 :            :   guchar *copy_headers;
     100                 :            :   guint n;
     101                 :            : 
     102                 :          1 :   m = g_dbus_message_new_method_call ("org.example.Name",
     103                 :            :                                       "/org/example/Object",
     104                 :            :                                       "org.example.Interface",
     105                 :            :                                       "Method");
     106                 :          1 :   g_dbus_message_set_serial (m, 42);
     107                 :          1 :   g_dbus_message_set_byte_order (m, G_DBUS_MESSAGE_BYTE_ORDER_BIG_ENDIAN);
     108                 :            : 
     109                 :          1 :   error = NULL;
     110                 :          1 :   copy = g_dbus_message_copy (m, &error);
     111                 :          1 :   g_assert_no_error (error);
     112                 :          1 :   g_assert (G_IS_DBUS_MESSAGE (copy));
     113                 :          1 :   g_assert (m != copy);
     114                 :          1 :   g_assert_cmpint (G_OBJECT (m)->ref_count, ==, 1);
     115                 :          1 :   g_assert_cmpint (G_OBJECT (copy)->ref_count, ==, 1);
     116                 :            : 
     117                 :          1 :   g_assert_cmpint (g_dbus_message_get_serial (copy), ==, g_dbus_message_get_serial (m));
     118                 :          1 :   g_assert_cmpint (g_dbus_message_get_byte_order (copy), ==, g_dbus_message_get_byte_order (m));
     119                 :          1 :   g_assert_cmpint (g_dbus_message_get_flags (copy), ==, g_dbus_message_get_flags (m));
     120                 :          1 :   g_assert_cmpint (g_dbus_message_get_message_type (copy), ==, g_dbus_message_get_message_type (m));
     121                 :          1 :   m_headers = g_dbus_message_get_header_fields (m);
     122                 :          1 :   copy_headers = g_dbus_message_get_header_fields (copy);
     123                 :          1 :   g_assert (m_headers != NULL);
     124                 :          1 :   g_assert (copy_headers != NULL);
     125         [ +  + ]:          5 :   for (n = 0; m_headers[n] != 0; n++)
     126                 :            :     {
     127                 :            :       GVariant *m_val;
     128                 :            :       GVariant *copy_val;
     129                 :          4 :       m_val = g_dbus_message_get_header (m, m_headers[n]);
     130                 :          4 :       copy_val = g_dbus_message_get_header (m, m_headers[n]);
     131                 :          4 :       g_assert (m_val != NULL);
     132                 :          4 :       g_assert (copy_val != NULL);
     133                 :          4 :       g_assert_cmpvariant (m_val, copy_val);
     134                 :            :     }
     135                 :          1 :   g_assert_cmpint (n, >, 0); /* make sure we actually compared headers etc. */
     136                 :          1 :   g_assert_cmpint (copy_headers[n], ==, 0);
     137                 :          1 :   g_free (m_headers);
     138                 :          1 :   g_free (copy_headers);
     139                 :            : 
     140                 :          1 :   g_object_unref (copy);
     141                 :          1 :   g_object_unref (m);
     142                 :          1 : }
     143                 :            : 
     144                 :            : /* ---------------------------------------------------------------------------------------------------- */
     145                 :            : 
     146                 :            : /* Test g_dbus_message_bytes_needed() returns correct results for a variety of
     147                 :            :  * arbitrary binary inputs.*/
     148                 :            : static void
     149                 :          1 : message_bytes_needed (void)
     150                 :            : {
     151                 :            :   const struct
     152                 :            :     {
     153                 :            :       const guint8 blob[16];
     154                 :            :       gssize expected_bytes_needed;
     155                 :            :     }
     156                 :          1 :   vectors[] =
     157                 :            :     {
     158                 :            :       /* Little endian with header rounding */
     159                 :            :       { { 'l', 0, 0, 1,  /* endianness, message type, flags, protocol version */
     160                 :            :           50, 0, 0, 0,  /* body length */
     161                 :            :           1, 0, 0, 0,  /* message serial */
     162                 :            :           7, 0, 0, 0  /* header length */}, 74 },
     163                 :            :       /* Little endian without header rounding */
     164                 :            :       { { 'l', 0, 0, 1,  /* endianness, message type, flags, protocol version */
     165                 :            :           50, 0, 0, 0,  /* body length */
     166                 :            :           1, 0, 0, 0,  /* message serial */
     167                 :            :           8, 0, 0, 0  /* header length */}, 74 },
     168                 :            :       /* Big endian with header rounding */
     169                 :            :       { { 'B', 0, 0, 1,  /* endianness, message type, flags, protocol version */
     170                 :            :           0, 0, 0, 50,  /* body length */
     171                 :            :           0, 0, 0, 1,  /* message serial */
     172                 :            :           0, 0, 0, 7  /* header length */}, 74 },
     173                 :            :       /* Big endian without header rounding */
     174                 :            :       { { 'B', 0, 0, 1,  /* endianness, message type, flags, protocol version */
     175                 :            :           0, 0, 0, 50,  /* body length */
     176                 :            :           0, 0, 0, 1,  /* message serial */
     177                 :            :           0, 0, 0, 8  /* header length */}, 74 },
     178                 :            :       /* Invalid endianness */
     179                 :            :       { { '!', 0, 0, 1,  /* endianness, message type, flags, protocol version */
     180                 :            :           0, 0, 0, 50,  /* body length */
     181                 :            :           0, 0, 0, 1,  /* message serial */
     182                 :            :           0, 0, 0, 8  /* header length */}, -1 },
     183                 :            :       /* Oversized */
     184                 :            :       { { 'l', 0, 0, 1,  /* endianness, message type, flags, protocol version */
     185                 :            :           0, 0, 0, 0x08,  /* body length (128MiB) */
     186                 :            :           1, 0, 0, 0,  /* message serial */
     187                 :            :           7, 0, 0, 0  /* header length */}, -1 },
     188                 :            :     };
     189                 :            :   gsize i;
     190                 :            : 
     191         [ +  + ]:          7 :   for (i = 0; i < G_N_ELEMENTS (vectors); i++)
     192                 :            :     {
     193                 :            :       gssize bytes_needed;
     194                 :          6 :       GError *local_error = NULL;
     195                 :            : 
     196                 :          6 :       g_test_message ("Vector: %" G_GSIZE_FORMAT, i);
     197                 :            : 
     198                 :          6 :       bytes_needed = g_dbus_message_bytes_needed ((guchar *) vectors[i].blob,
     199                 :            :                                                   G_N_ELEMENTS (vectors[i].blob),
     200                 :            :                                                   &local_error);
     201                 :            : 
     202         [ +  + ]:          6 :       if (vectors[i].expected_bytes_needed < 0)
     203                 :          2 :         g_assert_error (local_error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
     204                 :            :       else
     205                 :          4 :         g_assert_no_error (local_error);
     206                 :          6 :       g_assert_cmpint (bytes_needed, ==, vectors[i].expected_bytes_needed);
     207                 :            : 
     208                 :          6 :       g_clear_error (&local_error);
     209                 :            :     }
     210                 :          1 : }
     211                 :            : 
     212                 :            : /* ---------------------------------------------------------------------------------------------------- */
     213                 :            : 
     214                 :            : int
     215                 :          1 : main (int   argc,
     216                 :            :       char *argv[])
     217                 :            : {
     218                 :          1 :   setlocale (LC_ALL, "C");
     219                 :            : 
     220                 :          1 :   g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
     221                 :            : 
     222                 :          1 :   g_test_add_func ("/gdbus/message/lock", message_lock);
     223                 :          1 :   g_test_add_func ("/gdbus/message/copy", message_copy);
     224                 :          1 :   g_test_add_func ("/gdbus/message/bytes-needed", message_bytes_needed);
     225                 :            : 
     226                 :          1 :   return g_test_run ();
     227                 :            : }

Generated by: LCOV version 1.14