diff --git a/common/test-attrs.c b/common/test-attrs.c index ffa38db3..85258f38 100644 --- a/common/test-attrs.c +++ b/common/test-attrs.c @@ -72,7 +72,18 @@ test_count (void) { CKA_INVALID }, }; + CK_ATTRIBUTE temp[] = { + { CKA_LABEL, "label", 5 }, + { CKA_ENCRYPT, &vtrue, sizeof (vtrue) }, + }; + + CK_ATTRIBUTE wrapped_attrs[] = { + { CKA_WRAP_TEMPLATE, &temp, sizeof (temp) }, + { CKA_INVALID }, + }; + assert_num_eq (2, p11_attrs_count (attrs)); + assert_num_eq (1, p11_attrs_count (wrapped_attrs)); assert_num_eq (0, p11_attrs_count (NULL)); assert_num_eq (0, p11_attrs_count (empty)); } @@ -262,6 +273,7 @@ test_build_recursive (void) assert_ptr_not_null (attrs); assert_num_eq (attrs->type, CKA_WRAP_TEMPLATE); assert_num_eq (attrs->ulValueLen, sizeof (template)); + assert_ptr_cmp (attrs->pValue, !=, template); array = attrs->pValue; /* Check that the CKA_LOCAL attribute has been copied, but * still has the same value */ @@ -278,6 +290,43 @@ test_build_recursive (void) p11_attrs_free (attrs); } +static void +test_build_recursive_null (void) +{ + CK_BBOOL vtrue = CK_TRUE; + CK_ATTRIBUTE template[] = { + { CKA_LABEL, NULL, (CK_ULONG)-1 }, + { CKA_LOCAL, &vtrue, sizeof (vtrue) }, + }; + CK_ATTRIBUTE add = { CKA_WRAP_TEMPLATE, template, sizeof (template) }; + CK_ATTRIBUTE *attrs; + CK_ATTRIBUTE *array; + + attrs = p11_attrs_build (NULL, &add, NULL); + + /* Test the first attribute */ + assert_ptr_not_null (attrs); + assert_num_eq (attrs->type, CKA_WRAP_TEMPLATE); + assert_num_eq (attrs->ulValueLen, sizeof (template)); + assert_ptr_cmp (attrs->pValue, !=, template); + + array = attrs->pValue; + + /* Check that the CKA_LABEL attribute has been copied */ + assert_num_eq (array[0].type, CKA_LABEL); + assert_num_eq (array[0].ulValueLen, (CK_ULONG)-1); + assert_ptr_eq (array[0].pValue, NULL); + + /* Check that the CKA_LOCAL attribute has been copied, but + * still has the same value */ + assert_num_eq (array[1].type, CKA_LOCAL); + assert_num_eq (array[1].ulValueLen, sizeof (vtrue)); + assert_ptr_cmp (array[1].pValue, !=, &vtrue); + assert_num_eq (*(CK_BBOOL *)array[1].pValue, vtrue); + + p11_attrs_free (attrs); +} + static void test_dup (void) { @@ -306,6 +355,52 @@ test_dup (void) p11_attrs_free (attrs); } +static void +test_dup_recursive (void) +{ + CK_BBOOL vtrue = CK_TRUE; + CK_ATTRIBUTE *attrs, *array; + CK_ATTRIBUTE template[] = { + { CKA_LOCAL, &vtrue, sizeof (vtrue) }, + { CKA_LABEL, "label", 5 }, + }; + CK_ATTRIBUTE original[] = { + { CKA_WRAP_TEMPLATE, template, sizeof (template) }, + { CKA_LABEL, "yay", 3 }, + { CKA_INVALID }, + }; + + attrs = p11_attrs_dup (original); + assert_ptr_not_null (attrs); + + /* Test the first attribute */ + assert (attrs[0].type == CKA_WRAP_TEMPLATE); + assert_num_eq (attrs[0].ulValueLen, sizeof (template)); + assert_ptr_cmp (attrs[0].pValue, !=, template); + + array = attrs[0].pValue; + + /* Test the first template attribute */ + assert (array[0].type == CKA_LOCAL); + assert_num_eq (array[0].ulValueLen, sizeof (vtrue)); + assert_ptr_cmp (array[0].pValue, !=, &vtrue); + assert_num_eq (*(CK_BBOOL *)array[0].pValue, vtrue); + + /* Test the second template attribute */ + assert (array[1].type == CKA_LABEL); + assert_num_eq (array[1].ulValueLen, 5); + assert (memcmp (array[1].pValue, "label", 5) == 0); + + /* Test the second attribute */ + assert (attrs[1].type == CKA_LABEL); + assert_num_eq (attrs[1].ulValueLen, 3); + assert (memcmp (attrs[1].pValue, "yay", 3) == 0); + + assert (attrs[2].type == CKA_INVALID); + + p11_attrs_free (attrs); +} + static void test_take (void) { @@ -340,6 +435,62 @@ test_take (void) p11_attrs_free (attrs); } +static void +test_take_recursive (void) +{ + CK_ATTRIBUTE *attrs, *array, *template; + CK_ATTRIBUTE initial_template[] = { + { CKA_LABEL, "yay", 3 }, + }; + CK_ATTRIBUTE initial[] = { + { CKA_WRAP_TEMPLATE, initial_template, sizeof (initial_template) }, + { CKA_VALUE, "nine", 4 }, + }; + + template = malloc (2 * sizeof (CK_ATTRIBUTE)); + assert_ptr_not_null (template); + template[0].type = CKA_LOCAL; + template[0].ulValueLen = sizeof (CK_BBOOL); + template[0].pValue = malloc (template[0].ulValueLen); + assert_ptr_not_null (template[0].pValue); + *(CK_BBOOL *)(template[0].pValue) = CK_TRUE; + template[1].type = CKA_LABEL; + template[1].ulValueLen = 5; + template[1].pValue = malloc (template[1].ulValueLen); + assert_ptr_not_null (template[1].pValue); + memcpy (template[1].pValue, "label", 5); + + attrs = p11_attrs_buildn (NULL, initial, 2); + attrs = p11_attrs_take (attrs, CKA_WRAP_TEMPLATE, template, 2 * sizeof (CK_ATTRIBUTE)); + attrs = p11_attrs_take (attrs, CKA_TOKEN, strdup ("\x01"), 1); + assert_ptr_not_null (attrs); + + assert (attrs[0].type == CKA_WRAP_TEMPLATE); + assert_num_eq (attrs[0].ulValueLen, 2 * sizeof (CK_ATTRIBUTE)); + assert_ptr_not_null (attrs[0].pValue); + + array = attrs[0].pValue; + + assert (array[0].type == CKA_LOCAL); + assert_num_eq (array[0].ulValueLen, sizeof (CK_BBOOL)); + assert (*(CK_BBOOL *)(array[0].pValue) == CK_TRUE); + + assert (array[1].type == CKA_LABEL); + assert_num_eq (array[1].ulValueLen, 5); + assert (memcmp (array[1].pValue, "label", 5) == 0); + + assert (attrs[1].type == CKA_VALUE); + assert_num_eq (4, attrs[1].ulValueLen); + assert (memcmp (attrs[1].pValue, "nine", 4) == 0); + + assert (attrs[2].type == CKA_TOKEN); + assert_num_eq (1, attrs[2].ulValueLen); + assert (memcmp (attrs[2].pValue, "\x01", 1) == 0); + + assert (attrs[3].type == CKA_INVALID); + + p11_attrs_free (attrs); +} static void test_merge_replace (void) @@ -440,6 +591,118 @@ test_merge_augment (void) p11_attrs_free (attrs); } +static void +test_merge_replace_recursive (void) +{ + CK_ATTRIBUTE temp1[] = { + { CKA_VALUE, "nine", 4 }, + }; + CK_ATTRIBUTE initial[] = { + { CKA_WRAP_TEMPLATE, temp1, sizeof (temp1) }, + { CKA_VALUE, "nine", 4 }, + }; + + CK_ATTRIBUTE temp2[] = { + { CKA_VALUE, "seven", 5 }, + { CKA_LABEL, "yay", 3 }, + }; + CK_ATTRIBUTE extra[] = { + { CKA_WRAP_TEMPLATE, temp2, sizeof (temp2) }, + { CKA_APPLICATION, "disco", 5 }, + }; + + CK_ATTRIBUTE *array; + CK_ATTRIBUTE *attrs; + CK_ATTRIBUTE *merge; + + attrs = p11_attrs_buildn (NULL, initial, 2); + merge = p11_attrs_buildn (NULL, extra, 2); + attrs = p11_attrs_merge (attrs, merge, true); + assert_ptr_not_null (attrs); + + assert (attrs[0].type == CKA_WRAP_TEMPLATE); + assert_num_eq (attrs[0].ulValueLen, sizeof (temp2)); + assert_ptr_not_null (attrs[0].pValue); + + array = attrs[0].pValue; + + assert (array[0].type == CKA_VALUE); + assert_num_eq (array[0].ulValueLen, 5); + assert (memcmp (array[0].pValue, "seven", 5) == 0); + + assert (array[1].type == CKA_LABEL); + assert_num_eq (array[1].ulValueLen, 3); + assert (memcmp (array[1].pValue, "yay", 3) == 0); + + assert_ptr_not_null (attrs); + assert (attrs[1].type == CKA_VALUE); + assert_num_eq (4, attrs[1].ulValueLen); + assert (memcmp (attrs[1].pValue, "nine", 4) == 0); + + assert_ptr_not_null (attrs); + assert (attrs[2].type == CKA_APPLICATION); + assert_num_eq (5, attrs[2].ulValueLen); + assert (memcmp (attrs[2].pValue, "disco", 5) == 0); + + assert (attrs[3].type == CKA_INVALID); + + p11_attrs_free (attrs); +} + +static void +test_merge_augment_recursive (void) +{ + CK_ATTRIBUTE temp1[] = { + { CKA_VALUE, "nine", 4 }, + }; + CK_ATTRIBUTE initial[] = { + { CKA_WRAP_TEMPLATE, temp1, sizeof (temp1) }, + { CKA_VALUE, "nine", 4 }, + }; + + CK_ATTRIBUTE temp2[] = { + { CKA_VALUE, "seven", 5 }, + { CKA_LABEL, "yay", 3 }, + }; + CK_ATTRIBUTE extra[] = { + { CKA_WRAP_TEMPLATE, temp2, sizeof (temp2) }, + { CKA_APPLICATION, "disco", 5 }, + }; + + CK_ATTRIBUTE *array; + CK_ATTRIBUTE *attrs; + CK_ATTRIBUTE *merge; + + attrs = p11_attrs_buildn (NULL, initial, 2); + merge = p11_attrs_buildn (NULL, extra, 2); + attrs = p11_attrs_merge (attrs, merge, false); + assert_ptr_not_null (attrs); + + assert (attrs[0].type == CKA_WRAP_TEMPLATE); + assert_num_eq (attrs[0].ulValueLen, sizeof (temp1)); + assert_ptr_not_null (attrs[0].pValue); + + array = attrs[0].pValue; + + assert (array[0].type == CKA_VALUE); + assert_num_eq (array[0].ulValueLen, 4); + assert (memcmp (array[0].pValue, "nine", 4) == 0); + + assert_ptr_not_null (attrs); + assert (attrs[1].type == CKA_VALUE); + assert_num_eq (4, attrs[1].ulValueLen); + assert (memcmp (attrs[1].pValue, "nine", 4) == 0); + + assert_ptr_not_null (attrs); + assert (attrs[2].type == CKA_APPLICATION); + assert_num_eq (5, attrs[2].ulValueLen); + assert (memcmp (attrs[2].pValue, "disco", 5) == 0); + + assert (attrs[3].type == CKA_INVALID); + + p11_attrs_free (attrs); +} + static void test_free_null (void) { @@ -468,6 +731,36 @@ test_equal (void) assert (!p11_attr_equal (&other, &content)); } +static void +test_equal_recursive (void) +{ + CK_ATTRIBUTE temp1[] = { + { CKA_LABEL, "yay", 3 }, + }; + CK_ATTRIBUTE temp2[] = { + { CKA_LABEL, "yay", 3 }, + { CKA_VALUE, "conte", 5 }, + }; + CK_ATTRIBUTE temp3[] = { + { CKA_VALUE, "conte", 5 }, + { CKA_LABEL, "yay", 3 }, + }; + CK_ATTRIBUTE temp4[] = { + { CKA_LABEL, "conte", 5 }, + { CKA_VALUE, "yay", 3 }, + }; + CK_ATTRIBUTE temp_one = { CKA_WRAP_TEMPLATE, temp1, sizeof (temp1) }; + CK_ATTRIBUTE temp_two = { CKA_WRAP_TEMPLATE, temp2, sizeof (temp2) }; + CK_ATTRIBUTE temp_three = { CKA_WRAP_TEMPLATE, temp3, sizeof (temp3) }; + CK_ATTRIBUTE temp_four = { CKA_WRAP_TEMPLATE, temp4, sizeof (temp4) }; + + assert (p11_attr_equal (&temp_one, &temp_one)); + assert (p11_attr_equal (&temp_two, &temp_two)); + assert (!p11_attr_equal (&temp_one, &temp_two)); + assert (!p11_attr_equal (&temp_two, &temp_three)); + assert (!p11_attr_equal (&temp_three, &temp_four)); +} + static void test_hash (void) { @@ -527,9 +820,13 @@ test_find (void) CK_BBOOL vtrue = CK_TRUE; CK_ATTRIBUTE *attr; + CK_ATTRIBUTE template[] = { + { CKA_VALUE, "value", 5 }, + }; CK_ATTRIBUTE attrs[] = { { CKA_LABEL, "label", 5 }, { CKA_TOKEN, &vtrue, sizeof (vtrue) }, + { CKA_WRAP_TEMPLATE, template, sizeof (template) }, { CKA_INVALID }, }; @@ -539,6 +836,9 @@ test_find (void) attr = p11_attrs_find (attrs, CKA_TOKEN); assert_ptr_eq (attrs + 1, attr); + attr = p11_attrs_find (attrs, CKA_WRAP_TEMPLATE); + assert_ptr_eq (attrs + 2, attr); + attr = p11_attrs_find (attrs, CKA_VALUE); assert_ptr_eq (NULL, attr); } @@ -549,21 +849,28 @@ test_findn (void) CK_BBOOL vtrue = CK_TRUE; CK_ATTRIBUTE *attr; + CK_ATTRIBUTE template[] = { + { CKA_VALUE, "value", 5 }, + }; CK_ATTRIBUTE attrs[] = { + { CKA_WRAP_TEMPLATE, template, sizeof (template) }, { CKA_LABEL, "label", 5 }, { CKA_TOKEN, &vtrue, sizeof (vtrue) }, }; - attr = p11_attrs_findn (attrs, 2, CKA_LABEL); + attr = p11_attrs_findn (attrs, 3, CKA_WRAP_TEMPLATE); assert_ptr_eq (attrs + 0, attr); - attr = p11_attrs_findn (attrs, 2, CKA_TOKEN); + attr = p11_attrs_findn (attrs, 3, CKA_LABEL); assert_ptr_eq (attrs + 1, attr); - attr = p11_attrs_findn (attrs, 2, CKA_VALUE); + attr = p11_attrs_findn (attrs, 3, CKA_TOKEN); + assert_ptr_eq (attrs + 2, attr); + + attr = p11_attrs_findn (attrs, 3, CKA_VALUE); assert_ptr_eq (NULL, attr); - attr = p11_attrs_findn (attrs, 1, CKA_TOKEN); + attr = p11_attrs_findn (attrs, 2, CKA_TOKEN); assert_ptr_eq (NULL, attr); } @@ -575,12 +882,16 @@ test_remove (void) CK_ATTRIBUTE *attrs; CK_BBOOL ret; + CK_ATTRIBUTE template[] = { + { CKA_VALUE, "value", 5 }, + }; CK_ATTRIBUTE initial[] = { { CKA_LABEL, "label", 5 }, { CKA_TOKEN, &vtrue, sizeof (vtrue) }, + { CKA_WRAP_TEMPLATE, template, sizeof (template) }, }; - attrs = p11_attrs_buildn (NULL, initial, 2); + attrs = p11_attrs_buildn (NULL, initial, 3); assert_ptr_not_null (attrs); attr = p11_attrs_find (attrs, CKA_LABEL); @@ -595,6 +906,12 @@ test_remove (void) ret = p11_attrs_remove (attrs, CKA_LABEL); assert_num_eq (CK_FALSE, ret); + ret = p11_attrs_remove (attrs, CKA_VALUE); + assert_num_eq (CK_FALSE, ret); + + ret = p11_attrs_remove (attrs, CKA_WRAP_TEMPLATE); + assert_num_eq (CK_TRUE, ret); + p11_attrs_free (attrs); } @@ -605,24 +922,36 @@ test_purge (void) CK_ATTRIBUTE *attr; CK_ATTRIBUTE *attrs; + /* We can't release child attributes without knowing ulValueLen */ + CK_ATTRIBUTE template[] = { + { CKA_VALUE, NULL, 0 }, + }; CK_ATTRIBUTE initial[] = { { CKA_LABEL, "label", 5 }, { CKA_TOKEN, &vtrue, sizeof (vtrue) }, + { CKA_WRAP_TEMPLATE, template, sizeof (template) }, }; - attrs = p11_attrs_buildn (NULL, initial, 2); + attrs = p11_attrs_buildn (NULL, initial, 3); assert_ptr_not_null (attrs); attr = p11_attrs_find (attrs, CKA_LABEL); assert_ptr_eq (attrs + 0, attr); - attr[0].ulValueLen = (CK_ULONG) -1; + attr = p11_attrs_find (attrs, CKA_WRAP_TEMPLATE); + assert_ptr_eq (attrs + 2, attr); + + attrs[0].ulValueLen = (CK_ULONG) -1; + attrs[2].ulValueLen = (CK_ULONG) -1; p11_attrs_purge (attrs); attr = p11_attrs_find (attrs, CKA_LABEL); assert_ptr_eq (NULL, attr); + attr = p11_attrs_find (attrs, CKA_WRAP_TEMPLATE); + assert_ptr_eq (NULL, attr); + p11_attrs_free (attrs); } @@ -654,10 +983,30 @@ test_match (void) { CKA_INVALID }, }; + CK_ATTRIBUTE temp1[] = { + { CKA_LABEL, "yay", 3 }, + { CKA_VALUE, "val", 3 }, + }; + CK_ATTRIBUTE one_recursive[] = { + { CKA_WRAP_TEMPLATE, temp1, sizeof (temp1) }, + { CKA_INVALID }, + }; + + CK_ATTRIBUTE temp2[] = { + { CKA_LABEL, "yay", 3 }, + { CKA_VALUE, "nil", 3 }, + }; + CK_ATTRIBUTE two_recursive[] = { + { CKA_WRAP_TEMPLATE, temp2, sizeof (temp2) }, + { CKA_INVALID }, + }; + assert (p11_attrs_match (attrs, attrs)); assert (p11_attrs_match (attrs, subset)); assert (!p11_attrs_match (attrs, different)); assert (!p11_attrs_match (attrs, extra)); + assert (p11_attrs_match (one_recursive, one_recursive)); + assert (!p11_attrs_match (one_recursive, two_recursive)); } static void @@ -686,9 +1035,29 @@ test_matchn (void) { CKA_TOKEN, &vtrue, sizeof (vtrue) }, }; + CK_ATTRIBUTE temp1[] = { + { CKA_LABEL, "yay", 3 }, + { CKA_VALUE, "val", 3 }, + }; + CK_ATTRIBUTE one_recursive[] = { + { CKA_WRAP_TEMPLATE, temp1, sizeof (temp1) }, + { CKA_INVALID }, + }; + + CK_ATTRIBUTE temp2[] = { + { CKA_LABEL, "nay", 3 }, + { CKA_VALUE, "val", 3 }, + }; + CK_ATTRIBUTE two_recursive[] = { + { CKA_WRAP_TEMPLATE, temp2, sizeof (temp2) }, + { CKA_INVALID }, + }; + assert (p11_attrs_matchn (attrs, subset, 1)); assert (!p11_attrs_matchn (attrs, different, 2)); assert (!p11_attrs_matchn (attrs, extra, 3)); + assert (p11_attrs_matchn (one_recursive, one_recursive, 1)); + assert (!p11_attrs_matchn (one_recursive, two_recursive, 1)); } static void @@ -789,6 +1158,7 @@ main (int argc, char *argv[]) { p11_test (test_equal, "/attrs/equal"); + p11_test (test_equal_recursive, "/attrs/equal-recursive"); p11_test (test_hash, "/attrs/hash"); p11_test (test_to_string, "/attrs/to-string"); @@ -802,11 +1172,16 @@ main (int argc, p11_test (test_build_add, "/attrs/build-add"); p11_test (test_build_null, "/attrs/build-null"); p11_test (test_build_recursive, "/attrs/build-recursive"); + p11_test (test_build_recursive_null, "/attrs/build-recursive-null"); p11_test (test_dup, "/attrs/dup"); + p11_test (test_dup_recursive, "/attrs/dup-recursive"); p11_test (test_take, "/attrs/take"); + p11_test (test_take_recursive, "/attrs/take-recursive"); p11_test (test_merge_replace, "/attrs/merge-replace"); p11_test (test_merge_augment, "/attrs/merge-augment"); p11_test (test_merge_empty, "/attrs/merge-empty"); + p11_test (test_merge_replace_recursive, "/attrs/merge-replace-recursive"); + p11_test (test_merge_augment_recursive, "/attrs/merge-augment-recursive"); p11_test (test_free_null, "/attrs/free-null"); p11_test (test_match, "/attrs/match"); p11_test (test_matchn, "/attrs/matchn");