(root)/
glib-2.79.0/
gio/
tests/
tls-database.c
       1  /* GLib testing framework examples and tests
       2   *
       3   * Copyright (C) Matthew Waters <matthew@centricular.com>.
       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  
      21  #include "config.h"
      22  
      23  #include <gio/gio.h>
      24  
      25  #include "gtesttlsbackend.h"
      26  
      27  static void
      28  set_default_database (void)
      29  {
      30    GTlsBackend *backend;
      31    GTlsDatabase *default_db, *file_db, *test_db;
      32    GError *error = NULL;
      33    gchar *path;
      34  
      35    backend = g_tls_backend_get_default ();
      36    g_assert_nonnull (backend);
      37  
      38    default_db = g_tls_backend_get_default_database (backend);
      39    g_assert_nonnull (default_db);
      40  
      41    path = g_test_build_filename (G_TEST_DIST, "cert-tests", "cert1.pem", NULL);
      42    file_db = g_tls_file_database_new (path, &error);
      43    g_assert_no_error (error);
      44    g_assert_nonnull (file_db);
      45  
      46    /* setting a default database makes get_default_database return that database */
      47    g_tls_backend_set_default_database (backend, file_db);
      48    test_db = g_tls_backend_get_default_database (backend);
      49    g_assert_nonnull (test_db);
      50    g_assert_true (test_db == file_db);
      51    g_object_unref (test_db);
      52  
      53    /* setting a NULL default database returns the original default database */
      54    g_tls_backend_set_default_database (backend, NULL);
      55    test_db = g_tls_backend_get_default_database (backend);
      56    g_assert_nonnull (test_db);
      57    g_assert_true (test_db == default_db);
      58  
      59    g_object_unref (default_db);
      60    g_object_unref (file_db);
      61    g_object_unref (test_db);
      62    g_free (path);
      63  }
      64  
      65  int
      66  main (int   argc,
      67        char *argv[])
      68  {
      69    g_test_init (&argc, &argv, NULL);
      70  
      71    _g_test_tls_backend_get_type ();
      72  
      73    g_test_add_func ("/tls-backend/set-default-database",
      74                     set_default_database);
      75  
      76    return g_test_run();
      77  }