From 3fc6365093ad07b2eb5ef859093c5c5eb56ee700 Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Thu, 14 Mar 2013 11:23:47 +0100 Subject: attrs: New p11_attrs_merge() function This takes one set of attributes and merges them into another, without copying memory needlessly. https://bugs.freedesktop.org/show_bug.cgi?id=62329 --- common/tests/test-attrs.c | 103 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) (limited to 'common/tests') diff --git a/common/tests/test-attrs.c b/common/tests/test-attrs.c index 09d0ace..49350be 100644 --- a/common/tests/test-attrs.c +++ b/common/tests/test-attrs.c @@ -290,6 +290,106 @@ test_take (CuTest *tc) p11_attrs_free (attrs); } + +static void +test_merge_replace (CuTest *tc) +{ + CK_ATTRIBUTE initial[] = { + { CKA_LABEL, "label", 5 }, + { CKA_VALUE, "nine", 4 }, + }; + + CK_ATTRIBUTE extra[] = { + { CKA_LABEL, "boooyah", 7 }, + { CKA_APPLICATION, "disco", 5 }, + }; + + 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); + CuAssertPtrNotNull (tc, attrs); + + CuAssertTrue (tc, attrs[0].type == CKA_LABEL); + CuAssertIntEquals (tc, 7, attrs[0].ulValueLen); + CuAssertTrue (tc, memcmp (attrs[0].pValue, "boooyah", 7) == 0); + + CuAssertPtrNotNull (tc, attrs); + CuAssertTrue (tc, attrs[1].type == CKA_VALUE); + CuAssertIntEquals (tc, 4, attrs[1].ulValueLen); + CuAssertTrue (tc, memcmp (attrs[1].pValue, "nine", 4) == 0); + + CuAssertPtrNotNull (tc, attrs); + CuAssertTrue (tc, attrs[2].type == CKA_APPLICATION); + CuAssertIntEquals (tc, 5, attrs[2].ulValueLen); + CuAssertTrue (tc, memcmp (attrs[2].pValue, "disco", 5) == 0); + + CuAssertTrue (tc, attrs[3].type == CKA_INVALID); + + p11_attrs_free (attrs); +} + +static void +test_merge_empty (CuTest *tc) +{ + CK_ATTRIBUTE extra[] = { + { CKA_LABEL, "boooyah", 7 }, + { CKA_APPLICATION, "disco", 5 }, + }; + + CK_ATTRIBUTE *attrs = NULL; + CK_ATTRIBUTE *merge; + + merge = p11_attrs_buildn (NULL, extra, 2); + attrs = p11_attrs_merge (attrs, merge, true); + CuAssertPtrNotNull (tc, attrs); + CuAssertPtrEquals (tc, merge, attrs); + + p11_attrs_free (attrs); +} + +static void +test_merge_augment (CuTest *tc) +{ + CK_ATTRIBUTE initial[] = { + { CKA_LABEL, "label", 5 }, + { CKA_VALUE, "nine", 4 }, + }; + + CK_ATTRIBUTE extra[] = { + { CKA_LABEL, "boooyah", 7 }, + { CKA_APPLICATION, "disco", 5 }, + }; + + 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); + CuAssertPtrNotNull (tc, attrs); + + CuAssertTrue (tc, attrs[0].type == CKA_LABEL); + CuAssertIntEquals (tc, 5, attrs[0].ulValueLen); + CuAssertTrue (tc, memcmp (attrs[0].pValue, "label", 5) == 0); + + CuAssertPtrNotNull (tc, attrs); + CuAssertTrue (tc, attrs[1].type == CKA_VALUE); + CuAssertIntEquals (tc, 4, attrs[1].ulValueLen); + CuAssertTrue (tc, memcmp (attrs[1].pValue, "nine", 4) == 0); + + CuAssertPtrNotNull (tc, attrs); + CuAssertTrue (tc, attrs[2].type == CKA_APPLICATION); + CuAssertIntEquals (tc, 5, attrs[2].ulValueLen); + CuAssertTrue (tc, memcmp (attrs[2].pValue, "disco", 5) == 0); + + CuAssertTrue (tc, attrs[3].type == CKA_INVALID); + + p11_attrs_free (attrs); +} + static void test_free_null (CuTest *tc) { @@ -592,6 +692,9 @@ main (void) SUITE_ADD_TEST (suite, test_build_null); SUITE_ADD_TEST (suite, test_dup); SUITE_ADD_TEST (suite, test_take); + SUITE_ADD_TEST (suite, test_merge_replace); + SUITE_ADD_TEST (suite, test_merge_augment); + SUITE_ADD_TEST (suite, test_merge_empty); SUITE_ADD_TEST (suite, test_free_null); SUITE_ADD_TEST (suite, test_match); SUITE_ADD_TEST (suite, test_matchn); -- cgit v1.1