summaryrefslogtreecommitdiff
path: root/trust/tests
diff options
context:
space:
mode:
Diffstat (limited to 'trust/tests')
-rw-r--r--trust/tests/Makefile.am9
-rw-r--r--trust/tests/test-builder.c6
-rw-r--r--trust/tests/test-digest.c143
-rw-r--r--trust/tests/test-module.c10
4 files changed, 158 insertions, 10 deletions
diff --git a/trust/tests/Makefile.am b/trust/tests/Makefile.am
index b0581cb..5a6c9ec 100644
--- a/trust/tests/Makefile.am
+++ b/trust/tests/Makefile.am
@@ -20,7 +20,8 @@ noinst_LTLIBRARIES = \
libtrust-test.la
libtrust_test_la_SOURCES = \
- test-trust.c test-trust.h
+ test-trust.c test-trust.h \
+ $(TRUST)/digest.c
LDADD = \
$(top_builddir)/trust/libtrust-testable.la \
@@ -31,9 +32,11 @@ LDADD = \
$(top_builddir)/common/libp11-test.la \
$(top_builddir)/common/libp11-common.la \
$(LIBTASN1_LIBS) \
+ $(HASH_LIBS) \
$(NULL)
CHECK_PROGS = \
+ test-digest \
test-persist \
test-index \
test-parser \
@@ -67,7 +70,9 @@ noinst_PROGRAMS = \
frob_nss_trust_LDADD = \
$(top_builddir)/common/libp11-common.la \
- $(top_builddir)/p11-kit/libp11-kit.la
+ $(top_builddir)/p11-kit/libp11-kit.la \
+ $(HASH_LIBS) \
+ $(NULL)
TESTS = $(CHECK_PROGS)
diff --git a/trust/tests/test-builder.c b/trust/tests/test-builder.c
index 3f71b14..6f9fdcc 100644
--- a/trust/tests/test-builder.c
+++ b/trust/tests/test-builder.c
@@ -42,8 +42,8 @@
#include "attrs.h"
#include "builder.h"
-#include "hash.h"
#include "debug.h"
+#include "digest.h"
#include "index.h"
#include "message.h"
#include "oid.h"
@@ -184,7 +184,7 @@ test_build_certificate (void)
static void
test_build_certificate_empty (void)
{
- unsigned char checksum[P11_HASH_SHA1_LEN];
+ unsigned char checksum[P11_DIGEST_SHA1_LEN];
CK_ULONG domain = 0;
CK_ULONG category = 0;
@@ -221,7 +221,7 @@ test_build_certificate_empty (void)
CK_ATTRIBUTE *extra;
CK_RV rv;
- p11_hash_sha1 (checksum, test_cacert3_ca_der, sizeof (test_cacert3_ca_der), NULL);
+ p11_digest_sha1 (checksum, test_cacert3_ca_der, sizeof (test_cacert3_ca_der), NULL);
attrs = NULL;
extra = NULL;
diff --git a/trust/tests/test-digest.c b/trust/tests/test-digest.c
new file mode 100644
index 0000000..f2cb669
--- /dev/null
+++ b/trust/tests/test-digest.c
@@ -0,0 +1,143 @@
+/*
+ * Copyright (c) 2012 Red Hat Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above
+ * copyright notice, this list of conditions and the
+ * following disclaimer.
+ * * Redistributions in binary form must reproduce the
+ * above copyright notice, this list of conditions and
+ * the following disclaimer in the documentation and/or
+ * other materials provided with the distribution.
+ * * The names of contributors to this software may not be
+ * used to endorse or promote products derived from this
+ * software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
+ * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ *
+ * Author: Stef Walter <stefw@gnome.org>
+ */
+
+#include "config.h"
+#include "test.h"
+
+#include <assert.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "digest.h"
+
+const char *sha1_input[] = {
+ "abc",
+ "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
+ NULL
+};
+
+const char *sha1_checksum[] = {
+ "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D",
+ "\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE\x4A\xA1\xF9\x51\x29\xE5\xE5\x46\x70\xF1",
+ NULL
+};
+
+static void
+test_sha1 (void)
+{
+ unsigned char checksum[P11_DIGEST_SHA1_LEN];
+ size_t len;
+ int i;
+
+ for (i = 0; sha1_input[i] != NULL; i++) {
+ memset (checksum, 0, sizeof (checksum));
+ len = strlen (sha1_input[i]);
+
+ p11_digest_sha1 (checksum, sha1_input[i], len, NULL);
+ assert (memcmp (sha1_checksum[i], checksum, P11_DIGEST_SHA1_LEN) == 0);
+
+ if (len > 6) {
+ p11_digest_sha1 (checksum, sha1_input[i], 6, sha1_input[i] + 6, len - 6, NULL);
+ assert (memcmp (sha1_checksum[i], checksum, P11_DIGEST_SHA1_LEN) == 0);
+ }
+ }
+}
+
+static void
+test_sha1_long (void)
+{
+ unsigned char checksum[P11_DIGEST_SHA1_LEN];
+ char *expected = "\x34\xAA\x97\x3C\xD4\xC4\xDA\xA4\xF6\x1E\xEB\x2B\xDB\xAD\x27\x31\x65\x34\x01\x6F";
+ char *input;
+
+ input = malloc (1000000);
+ assert (input != NULL);
+ memset (input, 'a', 1000000);
+
+ p11_digest_sha1 (checksum, input, 1000000, NULL);
+ assert (memcmp (expected, checksum, P11_DIGEST_SHA1_LEN) == 0);
+
+ free (input);
+}
+
+const char *md5_input[] = {
+ "",
+ "a",
+ "abc",
+ "message digest",
+ "abcdefghijklmnopqrstuvwxyz",
+ NULL
+};
+
+const char *md5_checksum[] = {
+ "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42\x7e",
+ "\x0c\xc1\x75\xb9\xc0\xf1\xb6\xa8\x31\xc3\x99\xe2\x69\x77\x26\x61",
+ "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f\x72",
+ "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61\xd0",
+ "\xc3\xfc\xd3\xd7\x61\x92\xe4\x00\x7d\xfb\x49\x6c\xca\x67\xe1\x3b",
+ NULL
+};
+
+static void
+test_md5 (void)
+{
+ unsigned char checksum[P11_DIGEST_MD5_LEN];
+ size_t len;
+ int i;
+
+ for (i = 0; md5_input[i] != NULL; i++) {
+ memset (checksum, 0, sizeof (checksum));
+ len = strlen (md5_input[i]);
+
+ p11_digest_md5 (checksum, md5_input[i], len, NULL);
+ assert (memcmp (md5_checksum[i], checksum, P11_DIGEST_MD5_LEN) == 0);
+
+ if (len > 5) {
+ p11_digest_md5 (checksum, md5_input[i], 5, md5_input[i] + 5, len - 5, NULL);
+ assert (memcmp (md5_checksum[i], checksum, P11_DIGEST_MD5_LEN) == 0);
+ }
+ }
+}
+
+int
+main (int argc,
+ char *argv[])
+{
+ p11_test (test_sha1, "/digest/sha1");
+ p11_test (test_sha1_long, "/digest/sha1-long");
+ p11_test (test_md5, "/digest/md5");
+ return p11_test_run (argc, argv);
+}
diff --git a/trust/tests/test-module.c b/trust/tests/test-module.c
index 33cdd48..80747da 100644
--- a/trust/tests/test-module.c
+++ b/trust/tests/test-module.c
@@ -43,7 +43,7 @@
#include <string.h>
#include "attrs.h"
-#include "hash.h"
+#include "digest.h"
#include "library.h"
#include "path.h"
#include "parser.h"
@@ -485,8 +485,8 @@ check_trust_object_hashes (CK_SESSION_HANDLE session,
CK_OBJECT_HANDLE trust,
CK_ATTRIBUTE *cert)
{
- unsigned char sha1[P11_HASH_SHA1_LEN];
- unsigned char md5[P11_HASH_MD5_LEN];
+ unsigned char sha1[P11_DIGEST_SHA1_LEN];
+ unsigned char md5[P11_DIGEST_MD5_LEN];
unsigned char check[128];
CK_ATTRIBUTE *value;
CK_RV rv;
@@ -503,10 +503,10 @@ check_trust_object_hashes (CK_SESSION_HANDLE session,
value = p11_attrs_find_valid (cert, CKA_VALUE);
assert_ptr_not_null (value);
- p11_hash_md5 (check, value->pValue, value->ulValueLen, NULL);
+ p11_digest_md5 (check, value->pValue, value->ulValueLen, NULL);
assert (memcmp (md5, check, sizeof (md5)) == 0);
- p11_hash_sha1 (check, value->pValue, value->ulValueLen, NULL);
+ p11_digest_sha1 (check, value->pValue, value->ulValueLen, NULL);
assert (memcmp (sha1, check, sizeof (sha1)) == 0);
}