1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
/* GIO TLS tests
*
* Copyright (C) 2011 Collabora, Ltd.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* In addition, when the library is used with OpenSSL, a special
* exception applies. Refer to the LICENSE_EXCEPTION file for details.
*
* Author: Stef Walter <stefw@collabora.co.uk>
*/
#include <gio/gio.h>
#include <sys/types.h>
#include <string.h>
#include "pkcs11/gpkcs11array.h"
typedef struct {
GPkcs11Array *array;
} TestArray;
static void
setup_array (TestArray *test,
gconstpointer unused)
{
test->array = g_pkcs11_array_new ();
g_assert (test->array);
}
static void
teardown_array (TestArray *test,
gconstpointer unused)
{
g_pkcs11_array_unref (test->array);
}
static void
test_add_find (TestArray *test,
gconstpointer data)
{
CK_ATTRIBUTE attr;
const CK_ATTRIBUTE *check;
const gchar *value = "test";
attr.type = CKA_LABEL;
attr.ulValueLen = strlen (value) + 1;
attr.pValue = (gpointer)value;
g_pkcs11_array_add (test->array, &attr);
memset (&attr, 0, sizeof (attr));
check = g_pkcs11_array_find (test->array, CKA_LABEL);
g_assert (check != NULL);
g_assert_cmpuint ((guint)check->ulValueLen, ==, strlen (value) + 1);
g_assert_cmpstr (check->pValue, ==, value);
g_assert (check->pValue != value);
/* Should be copied properly, and be independent from stack value */
g_assert (check != &attr);
check = g_pkcs11_array_find (test->array, CKA_ID);
g_assert (check == NULL);
g_assert_cmpuint (test->array->count, ==, 1);
/* Adding a second value of same type, should add a duplicate */
attr.type = CKA_LABEL;
attr.ulValueLen = 3;
attr.pValue = "bye";
g_pkcs11_array_add (test->array, &attr);
g_assert_cmpuint (test->array->count, ==, 2);
}
static void
test_set_find (TestArray *test,
gconstpointer data)
{
CK_ATTRIBUTE attr;
const CK_ATTRIBUTE *check;
const gchar *value = "test";
attr.type = CKA_LABEL;
attr.ulValueLen = strlen (value) + 1;
attr.pValue = (gpointer)value;
g_pkcs11_array_set (test->array, &attr);
memset (&attr, 0, sizeof (attr));
check = g_pkcs11_array_find (test->array, CKA_LABEL);
g_assert (check != NULL);
g_assert_cmpuint ((guint)check->ulValueLen, ==, strlen (value) + 1);
g_assert_cmpstr (check->pValue, ==, value);
g_assert (check->pValue != value);
/* Should be copied properly, and be independent from stack value */
g_assert (check != &attr);
/* Adding a second value of same type should override */
attr.type = CKA_LABEL;
attr.ulValueLen = 3;
attr.pValue = "bye";
g_pkcs11_array_set (test->array, &attr);
g_assert_cmpuint (test->array->count, ==, 1);
}
static void
test_value (TestArray *test,
gconstpointer data)
{
const CK_ATTRIBUTE *check;
const gchar *value = "test";
/* Add with null termiator */
g_pkcs11_array_add_value (test->array, CKA_LABEL, value, -1);
check = g_pkcs11_array_find (test->array, CKA_LABEL);
g_assert (check != NULL);
g_assert_cmpuint ((guint)check->ulValueLen, ==, strlen (value));
g_assert (memcmp (check->pValue, value, check->ulValueLen) == 0);
g_assert (check->pValue != value);
/* Add with value length */
g_pkcs11_array_add_value (test->array, CKA_ID, value, 3);
check = g_pkcs11_array_find (test->array, CKA_ID);
g_assert (check != NULL);
g_assert_cmpuint ((guint)check->ulValueLen, ==, 3);
g_assert (memcmp (check->pValue, value, check->ulValueLen) == 0);
g_assert (check->pValue != value);
g_assert_cmpuint (test->array->count, ==, 2);
/* Set should override */
g_pkcs11_array_set_value (test->array, CKA_LABEL, "boring", 6);
check = g_pkcs11_array_find (test->array, CKA_LABEL);
g_assert (check != NULL);
g_assert_cmpuint ((guint)check->ulValueLen, ==, 6);
g_assert (memcmp (check->pValue, "boring", check->ulValueLen) == 0);
g_assert_cmpuint (test->array->count, ==, 2);
/* Override with calculated length */
g_pkcs11_array_set_value (test->array, CKA_LABEL, "boring", -1);
check = g_pkcs11_array_find (test->array, CKA_LABEL);
g_assert (check != NULL);
g_assert_cmpuint ((guint)check->ulValueLen, ==, 6);
g_assert (memcmp (check->pValue, "boring", check->ulValueLen) == 0);
g_assert_cmpuint (test->array->count, ==, 2);
}
static void
test_boolean (TestArray *test,
gconstpointer data)
{
const CK_ATTRIBUTE *check;
gboolean bval = FALSE;
g_pkcs11_array_add_boolean (test->array, CKA_TOKEN, TRUE);
if (!g_pkcs11_array_find_boolean (test->array, CKA_TOKEN, &bval))
g_assert_not_reached ();
g_assert (bval == TRUE);
/* Check that it's actually formatted right */
check = g_pkcs11_array_find (test->array, CKA_TOKEN);
g_assert (check != NULL);
g_assert_cmpuint (check->ulValueLen, ==, sizeof (CK_BBOOL));
g_assert (check->pValue != NULL);
g_assert (*((CK_BBOOL*)check->pValue) == CK_TRUE);
/* Check FALSE */
g_pkcs11_array_add_boolean (test->array, CKA_ENCRYPT, FALSE);
/* Check that it's actually formatted right */
check = g_pkcs11_array_find (test->array, CKA_ENCRYPT);
g_assert (check != NULL);
g_assert_cmpuint (check->ulValueLen, ==, sizeof (CK_BBOOL));
g_assert (check->pValue != NULL);
g_assert (*((CK_BBOOL*)check->pValue) == CK_FALSE);
g_assert_cmpuint (test->array->count, ==, 2);
/* Add a non boolean value */
g_pkcs11_array_add_value (test->array, CKA_LABEL, "label", -1);
/* Shouldn't work to find boolean on that */
if (g_pkcs11_array_find_boolean (test->array, CKA_LABEL, &bval))
g_assert_not_reached ();
g_assert_cmpuint (test->array->count, ==, 3);
/* Set should override */
g_pkcs11_array_set_boolean (test->array, CKA_TOKEN, FALSE);
if (!g_pkcs11_array_find_boolean (test->array, CKA_TOKEN, &bval))
g_assert_not_reached ();
g_assert (bval == FALSE);
g_assert_cmpuint (test->array->count, ==, 3);
}
static void
test_ulong (TestArray *test,
gconstpointer data)
{
const CK_ATTRIBUTE *check;
gulong uval = FALSE;
g_pkcs11_array_add_ulong (test->array, CKA_PIXEL_X, 38938);
if (!g_pkcs11_array_find_ulong (test->array, CKA_PIXEL_X, &uval))
g_assert_not_reached ();
g_assert (uval == 38938UL);
g_assert_cmpuint (test->array->count, ==, 1);
/* Check that it's actually formatted right */
check = g_pkcs11_array_find (test->array, CKA_PIXEL_X);
g_assert (check != NULL);
g_assert_cmpuint (check->ulValueLen, ==, sizeof (CK_ULONG));
g_assert (check->pValue != NULL);
g_assert (*((CK_ULONG*)check->pValue) == 38938UL);
/* Check -1, since this is used regularly */
g_pkcs11_array_add_ulong (test->array, CKA_MODULUS_BITS, (gulong)-1);
/* Check that it's actually formatted right */
check = g_pkcs11_array_find (test->array, CKA_MODULUS_BITS);
g_assert (check != NULL);
g_assert_cmpuint (check->ulValueLen, ==, sizeof (CK_ULONG));
g_assert (check->pValue != NULL);
g_assert (*((CK_ULONG*)check->pValue) == (CK_ULONG)-1);
g_assert_cmpuint (test->array->count, ==, 2);
/* Add a non ulong length value */
g_pkcs11_array_add_value (test->array, CKA_LABEL, "label", -1);
g_assert_cmpuint (test->array->count, ==, 3);
/* Shouldn't work to find ulong on that */
if (g_pkcs11_array_find_ulong (test->array, CKA_LABEL, &uval))
g_assert_not_reached ();
/* Set should override */
g_pkcs11_array_set_ulong (test->array, CKA_PIXEL_X, 48);
if (!g_pkcs11_array_find_ulong (test->array, CKA_PIXEL_X, &uval))
g_assert_not_reached ();
g_assert (uval == 48UL);
g_assert_cmpuint (test->array->count, ==, 3);
}
static void
test_boxed (TestArray *test,
gconstpointer data)
{
GPkcs11Array *array;
/* Should reference */
array = g_boxed_copy (G_TYPE_PKCS11_ARRAY, test->array);
g_assert (array == test->array);
/* Should unreference */
g_boxed_free (G_TYPE_PKCS11_ARRAY, array);
}
int
main (int argc,
char *argv[])
{
g_test_init (&argc, &argv, NULL);
g_test_add ("/pkcs11/array/add-find", TestArray, NULL,
setup_array, test_add_find, teardown_array);
g_test_add ("/pkcs11/array/set-find", TestArray, NULL,
setup_array, test_set_find, teardown_array);
g_test_add ("/pkcs11/array/value", TestArray, NULL,
setup_array, test_value, teardown_array);
g_test_add ("/pkcs11/array/boolean", TestArray, NULL,
setup_array, test_boolean, teardown_array);
g_test_add ("/pkcs11/array/ulong", TestArray, NULL,
setup_array, test_ulong, teardown_array);
g_test_add ("/pkcs11/array/boxed", TestArray, NULL,
setup_array, test_boxed, teardown_array);
return g_test_run();
}
|