(root)/
glib-2.79.0/
gio/
tests/
credentials.c
       1  /* GLib testing framework examples and tests
       2   *
       3   * Copyright © 2012 Collabora Ltd.
       4   *
       5   * SPDX-License-Identifier: LicenseRef-old-glib-tests
       6   *
       7   * This work is provided "as is"; redistribution and modification
       8   * in whole or in part, in any medium, physical or electronic is
       9   * permitted without restriction.
      10   *
      11   * This work is distributed in the hope that it will be useful,
      12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
      14   *
      15   * In no event shall the authors or contributors be liable for any
      16   * direct, indirect, incidental, special, exemplary, or consequential
      17   * damages (including, but not limited to, procurement of substitute
      18   * goods or services; loss of use, data, or profits; or business
      19   * interruption) however caused and on any theory of liability, whether
      20   * in contract, strict liability, or tort (including negligence or
      21   * otherwise) arising in any way out of the use of this software, even
      22   * if advised of the possibility of such damage.
      23   */
      24  
      25  #include "config.h"
      26  
      27  #include <gio/gio.h>
      28  #include <gio/gcredentialsprivate.h>
      29  
      30  #ifdef G_OS_WIN32
      31  
      32  static void
      33  test_basic (void)
      34  {
      35    GCredentials *creds = g_credentials_new ();
      36    gchar *stringified;
      37    DWORD *pid;
      38  
      39    stringified = g_credentials_to_string (creds);
      40    g_test_message ("%s", stringified);
      41    g_free (stringified);
      42  
      43    pid = g_credentials_get_native (creds,
      44                                    G_CREDENTIALS_TYPE_WIN32_PID);
      45    g_assert_cmpuint (*pid, ==, GetCurrentProcessId ());
      46  
      47    g_object_unref (creds);
      48  }
      49  
      50  #else
      51  
      52  static void
      53  test_basic (void)
      54  {
      55    GCredentials *creds = g_credentials_new ();
      56    GCredentials *other = g_credentials_new ();
      57    gpointer bad_native_creds;
      58  #if G_CREDENTIALS_SUPPORTED
      59    GError *error = NULL;
      60    gboolean set;
      61    pid_t not_me;
      62    gchar *stringified;
      63  #endif
      64  
      65    /* You can always get a credentials object, but it might not work. */
      66    g_assert (creds != NULL);
      67    g_assert (other != NULL);
      68  
      69  #if G_CREDENTIALS_SUPPORTED
      70    g_assert (g_credentials_is_same_user (creds, other, &error));
      71    g_assert_no_error (error);
      72  
      73    if (geteuid () == 0)
      74      not_me = 65534; /* traditionally 'nobody' */
      75    else
      76      not_me = 0;
      77  
      78    g_assert_cmpuint (g_credentials_get_unix_user (creds, &error), ==,
      79        geteuid ());
      80    g_assert_no_error (error);
      81  
      82  #if G_CREDENTIALS_HAS_PID
      83    g_assert_cmpint (g_credentials_get_unix_pid (creds, &error), ==,
      84        getpid ());
      85    g_assert_no_error (error);
      86  #else
      87    g_assert_cmpint (g_credentials_get_unix_pid (creds, &error), ==, -1);
      88    g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
      89    g_clear_error (&error);
      90  #endif
      91  
      92    set = g_credentials_set_unix_user (other, not_me, &error);
      93  #if G_CREDENTIALS_SPOOFING_SUPPORTED
      94    g_assert_no_error (error);
      95    g_assert (set);
      96  
      97    g_assert_cmpuint (g_credentials_get_unix_user (other, &error), ==, not_me);
      98    g_assert (!g_credentials_is_same_user (creds, other, &error));
      99    g_assert_no_error (error);
     100  #else
     101    g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED);
     102    g_assert (!set);
     103    g_clear_error (&error);
     104  
     105    g_assert_cmpuint (g_credentials_get_unix_user (other, &error), ==, geteuid ());
     106    g_assert (g_credentials_is_same_user (creds, other, &error));
     107    g_assert_no_error (error);
     108  #endif
     109  
     110    stringified = g_credentials_to_string (creds);
     111    g_test_message ("%s", stringified);
     112    g_free (stringified);
     113  
     114    stringified = g_credentials_to_string (other);
     115    g_test_message ("%s", stringified);
     116    g_free (stringified);
     117  
     118  #if G_CREDENTIALS_USE_LINUX_UCRED
     119          {
     120            struct ucred *native = g_credentials_get_native (creds,
     121                G_CREDENTIALS_TYPE_LINUX_UCRED);
     122  
     123            g_assert_cmpuint (native->uid, ==, geteuid ());
     124            g_assert_cmpuint (native->pid, ==, getpid ());
     125          }
     126  #elif G_CREDENTIALS_USE_APPLE_XUCRED
     127          {
     128            struct xucred *native = g_credentials_get_native (creds,
     129                G_CREDENTIALS_TYPE_APPLE_XUCRED);
     130  
     131            g_assert_cmpuint (native->cr_version, ==, XUCRED_VERSION);
     132            g_assert_cmpuint (native->cr_uid, ==, geteuid ());
     133          }
     134  #elif G_CREDENTIALS_USE_FREEBSD_CMSGCRED
     135          {
     136            struct cmsgcred *native = g_credentials_get_native (creds,
     137                G_CREDENTIALS_TYPE_FREEBSD_CMSGCRED);
     138  
     139            g_assert_cmpuint (native->cmcred_euid, ==, geteuid ());
     140            g_assert_cmpuint (native->cmcred_pid, ==, getpid ());
     141          }
     142  #elif G_CREDENTIALS_USE_NETBSD_UNPCBID
     143          {
     144            struct unpcbid *native = g_credentials_get_native (creds,
     145                G_CREDENTIALS_TYPE_NETBSD_UNPCBID);
     146  
     147            g_assert_cmpuint (native->unp_euid, ==, geteuid ());
     148            g_assert_cmpuint (native->unp_pid, ==, getpid ());
     149          }
     150  #elif G_CREDENTIALS_USE_OPENBSD_SOCKPEERCRED
     151          {
     152            struct sockpeercred *native = g_credentials_get_native (creds,
     153                G_CREDENTIALS_TYPE_OPENBSD_SOCKPEERCRED);
     154  
     155            g_assert_cmpuint (native->uid, ==, geteuid ());
     156            g_assert_cmpuint (native->pid, ==, getpid ());
     157          }
     158  #elif G_CREDENTIALS_USE_SOLARIS_UCRED
     159          {
     160            ucred_t *native = g_credentials_get_native (creds,
     161                G_CREDENTIALS_TYPE_SOLARIS_UCRED);
     162  
     163            g_assert_cmpuint (ucred_geteuid (native), ==, geteuid ());
     164            g_assert_cmpuint (ucred_getpid (native), ==, getpid ());
     165          }
     166  #else
     167  #error "G_CREDENTIALS_SUPPORTED is set but there is no test for this platform"
     168  #endif
     169  
     170  
     171  #if G_CREDENTIALS_USE_LINUX_UCRED
     172    g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
     173                           "*g_credentials_get_native: Trying to get*"
     174                           "G_CREDENTIALS_TYPE_FREEBSD_CMSGCRED "
     175                           "but only G_CREDENTIALS_TYPE_LINUX_UCRED*"
     176                           "supported*");
     177    bad_native_creds = g_credentials_get_native (creds, G_CREDENTIALS_TYPE_FREEBSD_CMSGCRED);
     178    g_test_assert_expected_messages ();
     179    g_assert_null (bad_native_creds);
     180  #else
     181    g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
     182                           "*g_credentials_get_native: Trying to get*"
     183                           "G_CREDENTIALS_TYPE_LINUX_UCRED "
     184                           "but only G_CREDENTIALS_TYPE_*supported*");
     185    bad_native_creds = g_credentials_get_native (creds, G_CREDENTIALS_TYPE_LINUX_UCRED);
     186    g_test_assert_expected_messages ();
     187    g_assert_null (bad_native_creds);
     188  #endif
     189  
     190  #else /* ! G_CREDENTIALS_SUPPORTED */
     191  
     192    g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
     193                           "*g_credentials_get_native: Trying to get "
     194                           "credentials *but*no support*");
     195    bad_native_creds = g_credentials_get_native (creds, G_CREDENTIALS_TYPE_LINUX_UCRED);
     196    g_test_assert_expected_messages ();
     197    g_assert_null (bad_native_creds);
     198  #endif
     199  
     200    g_object_unref (creds);
     201    g_object_unref (other);
     202  }
     203  
     204  #endif /* !G_OS_WIN32 */
     205  
     206  int
     207  main (int   argc,
     208        char *argv[])
     209  {
     210    g_test_init (&argc, &argv, NULL);
     211  
     212    g_test_add_func ("/credentials/basic", test_basic);
     213  
     214    return g_test_run();
     215  }