Blame tls/tests/pkcs11-pin.c

Packit 9bedce
/* GIO TLS tests
Packit 9bedce
 *
Packit 9bedce
 * Copyright (C) 2011 Collabora, Ltd.
Packit 9bedce
 *
Packit 9bedce
 * This library is free software; you can redistribute it and/or
Packit 9bedce
 * modify it under the terms of the GNU Lesser General Public
Packit 9bedce
 * License as published by the Free Software Foundation; either
Packit 9bedce
 * version 2 of the License, or (at your option) any later version.
Packit 9bedce
 *
Packit 9bedce
 * This library is distributed in the hope that it will be useful,
Packit 9bedce
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 9bedce
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 9bedce
 * Lesser General Public License for more details.
Packit 9bedce
 *
Packit 9bedce
 * You should have received a copy of the GNU Lesser General
Packit 9bedce
 * Public License along with this library; if not, write to the
Packit 9bedce
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Packit 9bedce
 * Boston, MA 02111-1307, USA.
Packit 9bedce
 *
Packit 9bedce
 * In addition, when the library is used with OpenSSL, a special
Packit 9bedce
 * exception applies. Refer to the LICENSE_EXCEPTION file for details.
Packit 9bedce
 *
Packit 9bedce
 * Author: Stef Walter <stefw@collabora.co.uk>
Packit 9bedce
 */
Packit 9bedce
Packit 9bedce
#include <gio/gio.h>
Packit 9bedce
Packit 9bedce
#include <sys/types.h>
Packit 9bedce
#include <string.h>
Packit 9bedce
Packit 9bedce
#include "pkcs11/gpkcs11pin.h"
Packit 9bedce
Packit 9bedce
typedef struct {
Packit 9bedce
  GTlsPassword *pin;
Packit 9bedce
} TestPin;
Packit 9bedce
Packit 9bedce
static void
Packit 9bedce
setup_pin (TestPin          *test,
Packit 9bedce
           gconstpointer     unused)
Packit 9bedce
{
Packit 9bedce
  test->pin = g_pkcs11_pin_new (G_TLS_PASSWORD_RETRY, "Test description");
Packit 9bedce
  g_assert (G_IS_PKCS11_PIN (test->pin));
Packit 9bedce
  g_assert (G_IS_TLS_PASSWORD (test->pin));
Packit 9bedce
}
Packit 9bedce
Packit 9bedce
static void
Packit 9bedce
teardown_pin (TestPin       *test,
Packit 9bedce
              gconstpointer  unused)
Packit 9bedce
{
Packit 9bedce
  g_assert_cmpint (G_OBJECT (test->pin)->ref_count, ==, 1);
Packit 9bedce
  g_object_unref (test->pin);
Packit 9bedce
}
Packit 9bedce
Packit 9bedce
static void
Packit 9bedce
test_attributes (TestPin        *test,
Packit 9bedce
                 gconstpointer   data)
Packit 9bedce
{
Packit 9bedce
  GTlsPasswordFlags flags;
Packit 9bedce
  const gchar *description;
Packit 9bedce
Packit 9bedce
  flags = g_tls_password_get_flags (test->pin);
Packit 9bedce
  g_assert_cmpuint (flags, ==, G_TLS_PASSWORD_RETRY);
Packit 9bedce
Packit 9bedce
  description = g_tls_password_get_description (test->pin);
Packit 9bedce
  g_assert_cmpstr (description, ==, "Test description");
Packit 9bedce
}
Packit 9bedce
Packit 9bedce
static void
Packit 9bedce
test_warnings (TestPin        *test,
Packit 9bedce
               gconstpointer   data)
Packit 9bedce
{
Packit 9bedce
  const gchar *warning;
Packit 9bedce
Packit 9bedce
  g_tls_password_set_flags (test->pin, G_TLS_PASSWORD_RETRY);
Packit 9bedce
  warning = g_tls_password_get_warning (test->pin);
Packit 9bedce
  g_assert (warning != NULL);
Packit 9bedce
Packit 9bedce
  g_tls_password_set_flags (test->pin, G_TLS_PASSWORD_FINAL_TRY);
Packit 9bedce
  warning = g_tls_password_get_warning (test->pin);
Packit 9bedce
  g_assert (warning != NULL);
Packit 9bedce
Packit 9bedce
  g_tls_password_set_flags (test->pin, G_TLS_PASSWORD_MANY_TRIES);
Packit 9bedce
  warning = g_tls_password_get_warning (test->pin);
Packit 9bedce
  g_assert (warning != NULL);
Packit 9bedce
Packit 9bedce
  g_tls_password_set_flags (test->pin, (GTlsPasswordFlags)0x10000000);
Packit 9bedce
  warning = g_tls_password_get_warning (test->pin);
Packit 9bedce
  g_assert (warning == NULL);
Packit 9bedce
Packit 9bedce
}
Packit 9bedce
Packit 9bedce
static void
Packit 9bedce
test_set_get_value (TestPin        *test,
Packit 9bedce
                    gconstpointer   data)
Packit 9bedce
{
Packit 9bedce
  const guchar *value;
Packit 9bedce
  gsize n_value = G_MAXSIZE;
Packit 9bedce
Packit 9bedce
  value = g_tls_password_get_value (test->pin, &n_value);
Packit 9bedce
  g_assert_cmpuint (n_value, ==, 0);
Packit 9bedce
  g_assert (value == NULL);
Packit 9bedce
Packit 9bedce
  g_tls_password_set_value (test->pin, (const guchar *)"secret", -1);
Packit 9bedce
Packit 9bedce
  value = g_tls_password_get_value (test->pin, &n_value);
Packit 9bedce
  g_assert_cmpuint (n_value, ==, 6);
Packit 9bedce
  g_assert (!strncmp ((const gchar *)value, "secret", n_value));
Packit 9bedce
Packit 9bedce
  g_tls_password_set_value (test->pin, (const guchar *)"other", 5);
Packit 9bedce
Packit 9bedce
  value = g_tls_password_get_value (test->pin, &n_value);
Packit 9bedce
  g_assert_cmpuint (n_value, ==, 5);
Packit 9bedce
  g_assert (!strncmp ((const gchar *)value, "other", n_value));
Packit 9bedce
}
Packit 9bedce
Packit 9bedce
static void
Packit 9bedce
test_internal_pin (TestPin        *test,
Packit 9bedce
                   gconstpointer   data)
Packit 9bedce
{
Packit 9bedce
  P11KitPin *pin;
Packit 9bedce
  const unsigned char *value;
Packit 9bedce
  size_t n_value;
Packit 9bedce
Packit 9bedce
  g_tls_password_set_value (test->pin, (const guchar *)"secret", -1);
Packit 9bedce
Packit 9bedce
  pin = g_pkcs11_pin_steal_internal (G_PKCS11_PIN (test->pin));
Packit 9bedce
Packit 9bedce
  value = p11_kit_pin_get_value (pin, &n_value);
Packit 9bedce
  g_assert_cmpuint (n_value, ==, 6);
Packit 9bedce
  g_assert (!strncmp ((const gchar *)value, "secret", n_value));
Packit 9bedce
Packit 9bedce
  p11_kit_pin_unref (pin);
Packit 9bedce
}
Packit 9bedce
Packit 9bedce
int
Packit 9bedce
main (int   argc,
Packit 9bedce
      char *argv[])
Packit 9bedce
{
Packit 9bedce
  g_test_init (&argc, &argv, NULL);
Packit 9bedce
Packit 9bedce
  g_test_add ("/pkcs11/pin/attributes", TestPin, NULL,
Packit 9bedce
              setup_pin, test_attributes, teardown_pin);
Packit 9bedce
  g_test_add ("/pkcs11/pin/warnings", TestPin, NULL,
Packit 9bedce
              setup_pin, test_warnings, teardown_pin);
Packit 9bedce
  g_test_add ("/pkcs11/pin/set-get-value", TestPin, NULL,
Packit 9bedce
              setup_pin, test_set_get_value, teardown_pin);
Packit 9bedce
  g_test_add ("/pkcs11/pin/internal-pin", TestPin, NULL,
Packit 9bedce
              setup_pin, test_internal_pin, teardown_pin);
Packit 9bedce
Packit 9bedce
  return g_test_run();
Packit 9bedce
}