Blame tls/tests/pkcs11-array.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/gpkcs11array.h"
Packit 9bedce
Packit 9bedce
typedef struct {
Packit 9bedce
  GPkcs11Array *array;
Packit 9bedce
} TestArray;
Packit 9bedce
Packit 9bedce
static void
Packit 9bedce
setup_array (TestArray          *test,
Packit 9bedce
             gconstpointer       unused)
Packit 9bedce
{
Packit 9bedce
  test->array = g_pkcs11_array_new ();
Packit 9bedce
  g_assert (test->array);
Packit 9bedce
}
Packit 9bedce
Packit 9bedce
static void
Packit 9bedce
teardown_array (TestArray       *test,
Packit 9bedce
                gconstpointer    unused)
Packit 9bedce
{
Packit 9bedce
  g_pkcs11_array_unref (test->array);
Packit 9bedce
}
Packit 9bedce
Packit 9bedce
static void
Packit 9bedce
test_add_find (TestArray      *test,
Packit 9bedce
               gconstpointer   data)
Packit 9bedce
{
Packit 9bedce
  CK_ATTRIBUTE attr;
Packit 9bedce
  const CK_ATTRIBUTE *check;
Packit 9bedce
  const gchar *value = "test";
Packit 9bedce
Packit 9bedce
  attr.type = CKA_LABEL;
Packit 9bedce
  attr.ulValueLen = strlen (value) + 1;
Packit 9bedce
  attr.pValue = (gpointer)value;
Packit 9bedce
  g_pkcs11_array_add (test->array, &attr);
Packit 9bedce
  memset (&attr, 0, sizeof (attr));
Packit 9bedce
Packit 9bedce
  check = g_pkcs11_array_find (test->array, CKA_LABEL);
Packit 9bedce
  g_assert (check != NULL);
Packit 9bedce
  g_assert_cmpuint ((guint)check->ulValueLen, ==, strlen (value) + 1);
Packit 9bedce
  g_assert_cmpstr (check->pValue, ==, value);
Packit 9bedce
  g_assert (check->pValue != value);
Packit 9bedce
Packit 9bedce
  /* Should be copied properly, and be independent from stack value */
Packit 9bedce
  g_assert (check != &attr);
Packit 9bedce
Packit 9bedce
  check = g_pkcs11_array_find (test->array, CKA_ID);
Packit 9bedce
  g_assert (check == NULL);
Packit 9bedce
  g_assert_cmpuint (test->array->count, ==, 1);
Packit 9bedce
Packit 9bedce
  /* Adding a second value of same type, should add a duplicate */
Packit 9bedce
  attr.type = CKA_LABEL;
Packit 9bedce
  attr.ulValueLen = 3;
Packit 9bedce
  attr.pValue = "bye";
Packit 9bedce
  g_pkcs11_array_add (test->array, &attr);
Packit 9bedce
  g_assert_cmpuint (test->array->count, ==, 2);
Packit 9bedce
}
Packit 9bedce
Packit 9bedce
static void
Packit 9bedce
test_set_find (TestArray      *test,
Packit 9bedce
               gconstpointer   data)
Packit 9bedce
{
Packit 9bedce
  CK_ATTRIBUTE attr;
Packit 9bedce
  const CK_ATTRIBUTE *check;
Packit 9bedce
  const gchar *value = "test";
Packit 9bedce
Packit 9bedce
  attr.type = CKA_LABEL;
Packit 9bedce
  attr.ulValueLen = strlen (value) + 1;
Packit 9bedce
  attr.pValue = (gpointer)value;
Packit 9bedce
  g_pkcs11_array_set (test->array, &attr);
Packit 9bedce
  memset (&attr, 0, sizeof (attr));
Packit 9bedce
Packit 9bedce
  check = g_pkcs11_array_find (test->array, CKA_LABEL);
Packit 9bedce
  g_assert (check != NULL);
Packit 9bedce
  g_assert_cmpuint ((guint)check->ulValueLen, ==, strlen (value) + 1);
Packit 9bedce
  g_assert_cmpstr (check->pValue, ==, value);
Packit 9bedce
  g_assert (check->pValue != value);
Packit 9bedce
Packit 9bedce
  /* Should be copied properly, and be independent from stack value */
Packit 9bedce
  g_assert (check != &attr);
Packit 9bedce
Packit 9bedce
  /* Adding a second value of same type should override */
Packit 9bedce
  attr.type = CKA_LABEL;
Packit 9bedce
  attr.ulValueLen = 3;
Packit 9bedce
  attr.pValue = "bye";
Packit 9bedce
  g_pkcs11_array_set (test->array, &attr);
Packit 9bedce
  g_assert_cmpuint (test->array->count, ==, 1);
Packit 9bedce
}
Packit 9bedce
Packit 9bedce
static void
Packit 9bedce
test_value (TestArray      *test,
Packit 9bedce
            gconstpointer   data)
Packit 9bedce
{
Packit 9bedce
  const CK_ATTRIBUTE *check;
Packit 9bedce
  const gchar *value = "test";
Packit 9bedce
Packit 9bedce
  /* Add with null termiator */
Packit 9bedce
  g_pkcs11_array_add_value (test->array, CKA_LABEL, value, -1);
Packit 9bedce
  check = g_pkcs11_array_find (test->array, CKA_LABEL);
Packit 9bedce
  g_assert (check != NULL);
Packit 9bedce
  g_assert_cmpuint ((guint)check->ulValueLen, ==, strlen (value));
Packit 9bedce
  g_assert (memcmp (check->pValue, value, check->ulValueLen) == 0);
Packit 9bedce
  g_assert (check->pValue != value);
Packit 9bedce
Packit 9bedce
  /* Add with value length */
Packit 9bedce
  g_pkcs11_array_add_value (test->array, CKA_ID, value, 3);
Packit 9bedce
  check = g_pkcs11_array_find (test->array, CKA_ID);
Packit 9bedce
  g_assert (check != NULL);
Packit 9bedce
  g_assert_cmpuint ((guint)check->ulValueLen, ==, 3);
Packit 9bedce
  g_assert (memcmp (check->pValue, value, check->ulValueLen) == 0);
Packit 9bedce
  g_assert (check->pValue != value);
Packit 9bedce
  g_assert_cmpuint (test->array->count, ==, 2);
Packit 9bedce
Packit 9bedce
  /* Set should override */
Packit 9bedce
  g_pkcs11_array_set_value (test->array, CKA_LABEL, "boring", 6);
Packit 9bedce
  check = g_pkcs11_array_find (test->array, CKA_LABEL);
Packit 9bedce
  g_assert (check != NULL);
Packit 9bedce
  g_assert_cmpuint ((guint)check->ulValueLen, ==, 6);
Packit 9bedce
  g_assert (memcmp (check->pValue, "boring", check->ulValueLen) == 0);
Packit 9bedce
  g_assert_cmpuint (test->array->count, ==, 2);
Packit 9bedce
Packit 9bedce
  /* Override with calculated length */
Packit 9bedce
  g_pkcs11_array_set_value (test->array, CKA_LABEL, "boring", -1);
Packit 9bedce
  check = g_pkcs11_array_find (test->array, CKA_LABEL);
Packit 9bedce
  g_assert (check != NULL);
Packit 9bedce
  g_assert_cmpuint ((guint)check->ulValueLen, ==, 6);
Packit 9bedce
  g_assert (memcmp (check->pValue, "boring", check->ulValueLen) == 0);
Packit 9bedce
  g_assert_cmpuint (test->array->count, ==, 2);
Packit 9bedce
Packit 9bedce
}
Packit 9bedce
Packit 9bedce
static void
Packit 9bedce
test_boolean (TestArray      *test,
Packit 9bedce
              gconstpointer   data)
Packit 9bedce
{
Packit 9bedce
  const CK_ATTRIBUTE *check;
Packit 9bedce
  gboolean bval = FALSE;
Packit 9bedce
Packit 9bedce
  g_pkcs11_array_add_boolean (test->array, CKA_TOKEN, TRUE);
Packit 9bedce
  if (!g_pkcs11_array_find_boolean (test->array, CKA_TOKEN, &bval))
Packit 9bedce
    g_assert_not_reached ();
Packit 9bedce
  g_assert (bval == TRUE);
Packit 9bedce
Packit 9bedce
  /* Check that it's actually formatted right */
Packit 9bedce
  check = g_pkcs11_array_find (test->array, CKA_TOKEN);
Packit 9bedce
  g_assert (check != NULL);
Packit 9bedce
  g_assert_cmpuint (check->ulValueLen, ==, sizeof (CK_BBOOL));
Packit 9bedce
  g_assert (check->pValue != NULL);
Packit 9bedce
  g_assert (*((CK_BBOOL*)check->pValue) == CK_TRUE);
Packit 9bedce
Packit 9bedce
  /* Check FALSE */
Packit 9bedce
  g_pkcs11_array_add_boolean (test->array, CKA_ENCRYPT, FALSE);
Packit 9bedce
Packit 9bedce
  /* Check that it's actually formatted right */
Packit 9bedce
  check = g_pkcs11_array_find (test->array, CKA_ENCRYPT);
Packit 9bedce
  g_assert (check != NULL);
Packit 9bedce
  g_assert_cmpuint (check->ulValueLen, ==, sizeof (CK_BBOOL));
Packit 9bedce
  g_assert (check->pValue != NULL);
Packit 9bedce
  g_assert (*((CK_BBOOL*)check->pValue) == CK_FALSE);
Packit 9bedce
  g_assert_cmpuint (test->array->count, ==, 2);
Packit 9bedce
Packit 9bedce
  /* Add a non boolean value */
Packit 9bedce
  g_pkcs11_array_add_value (test->array, CKA_LABEL, "label", -1);
Packit 9bedce
Packit 9bedce
  /* Shouldn't work to find boolean on that */
Packit 9bedce
  if (g_pkcs11_array_find_boolean (test->array, CKA_LABEL, &bval))
Packit 9bedce
    g_assert_not_reached ();
Packit 9bedce
  g_assert_cmpuint (test->array->count, ==, 3);
Packit 9bedce
Packit 9bedce
  /* Set should override */
Packit 9bedce
  g_pkcs11_array_set_boolean (test->array, CKA_TOKEN, FALSE);
Packit 9bedce
  if (!g_pkcs11_array_find_boolean (test->array, CKA_TOKEN, &bval))
Packit 9bedce
    g_assert_not_reached ();
Packit 9bedce
  g_assert (bval == FALSE);
Packit 9bedce
  g_assert_cmpuint (test->array->count, ==, 3);
Packit 9bedce
}
Packit 9bedce
Packit 9bedce
static void
Packit 9bedce
test_ulong (TestArray      *test,
Packit 9bedce
            gconstpointer   data)
Packit 9bedce
{
Packit 9bedce
  const CK_ATTRIBUTE *check;
Packit 9bedce
  gulong uval = FALSE;
Packit 9bedce
Packit 9bedce
  g_pkcs11_array_add_ulong (test->array, CKA_PIXEL_X, 38938);
Packit 9bedce
  if (!g_pkcs11_array_find_ulong (test->array, CKA_PIXEL_X, &uval))
Packit 9bedce
    g_assert_not_reached ();
Packit 9bedce
  g_assert (uval == 38938UL);
Packit 9bedce
  g_assert_cmpuint (test->array->count, ==, 1);
Packit 9bedce
Packit 9bedce
  /* Check that it's actually formatted right */
Packit 9bedce
  check = g_pkcs11_array_find (test->array, CKA_PIXEL_X);
Packit 9bedce
  g_assert (check != NULL);
Packit 9bedce
  g_assert_cmpuint (check->ulValueLen, ==, sizeof (CK_ULONG));
Packit 9bedce
  g_assert (check->pValue != NULL);
Packit 9bedce
  g_assert (*((CK_ULONG*)check->pValue) == 38938UL);
Packit 9bedce
Packit 9bedce
  /* Check -1, since this is used regularly */
Packit 9bedce
  g_pkcs11_array_add_ulong (test->array, CKA_MODULUS_BITS, (gulong)-1);
Packit 9bedce
Packit 9bedce
  /* Check that it's actually formatted right */
Packit 9bedce
  check = g_pkcs11_array_find (test->array, CKA_MODULUS_BITS);
Packit 9bedce
  g_assert (check != NULL);
Packit 9bedce
  g_assert_cmpuint (check->ulValueLen, ==, sizeof (CK_ULONG));
Packit 9bedce
  g_assert (check->pValue != NULL);
Packit 9bedce
  g_assert (*((CK_ULONG*)check->pValue) == (CK_ULONG)-1);
Packit 9bedce
  g_assert_cmpuint (test->array->count, ==, 2);
Packit 9bedce
Packit 9bedce
  /* Add a non ulong length value */
Packit 9bedce
  g_pkcs11_array_add_value (test->array, CKA_LABEL, "label", -1);
Packit 9bedce
  g_assert_cmpuint (test->array->count, ==, 3);
Packit 9bedce
Packit 9bedce
  /* Shouldn't work to find ulong on that */
Packit 9bedce
  if (g_pkcs11_array_find_ulong (test->array, CKA_LABEL, &uval))
Packit 9bedce
    g_assert_not_reached ();
Packit 9bedce
Packit 9bedce
  /* Set should override */
Packit 9bedce
  g_pkcs11_array_set_ulong (test->array, CKA_PIXEL_X, 48);
Packit 9bedce
  if (!g_pkcs11_array_find_ulong (test->array, CKA_PIXEL_X, &uval))
Packit 9bedce
    g_assert_not_reached ();
Packit 9bedce
  g_assert (uval == 48UL);
Packit 9bedce
  g_assert_cmpuint (test->array->count, ==, 3);
Packit 9bedce
}
Packit 9bedce
Packit 9bedce
static void
Packit 9bedce
test_boxed (TestArray      *test,
Packit 9bedce
            gconstpointer   data)
Packit 9bedce
{
Packit 9bedce
  GPkcs11Array *array;
Packit 9bedce
Packit 9bedce
  /* Should reference */
Packit 9bedce
  array = g_boxed_copy (G_TYPE_PKCS11_ARRAY, test->array);
Packit 9bedce
  g_assert (array == test->array);
Packit 9bedce
Packit 9bedce
  /* Should unreference */
Packit 9bedce
  g_boxed_free (G_TYPE_PKCS11_ARRAY, array);
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/array/add-find", TestArray, NULL,
Packit 9bedce
              setup_array, test_add_find, teardown_array);
Packit 9bedce
  g_test_add ("/pkcs11/array/set-find", TestArray, NULL,
Packit 9bedce
              setup_array, test_set_find, teardown_array);
Packit 9bedce
  g_test_add ("/pkcs11/array/value", TestArray, NULL,
Packit 9bedce
              setup_array, test_value, teardown_array);
Packit 9bedce
  g_test_add ("/pkcs11/array/boolean", TestArray, NULL,
Packit 9bedce
              setup_array, test_boolean, teardown_array);
Packit 9bedce
  g_test_add ("/pkcs11/array/ulong", TestArray, NULL,
Packit 9bedce
              setup_array, test_ulong, teardown_array);
Packit 9bedce
  g_test_add ("/pkcs11/array/boxed", TestArray, NULL,
Packit 9bedce
              setup_array, test_boxed, teardown_array);
Packit 9bedce
Packit 9bedce
  return g_test_run();
Packit 9bedce
}