mirror of
https://github.com/openharmony/third_party_fsverity-utils.git
synced 2026-07-01 10:05:35 -04:00
Add some basic test programs for libfsverity
Add three test programs: 'test_hash_algs', 'test_compute_digest', and 'test_sign_digest'. Nothing fancy yet, just some basic tests to test each library function. With the new Makefile, these get run by 'make check'. Reviewed-by: Jes Sorensen <jsorensen@fb.com> Signed-off-by: Eric Biggers <ebiggers@google.com>
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
/*
|
||||
* Test libfsverity_compute_digest().
|
||||
*
|
||||
* Copyright 2020 Google LLC
|
||||
*/
|
||||
#include "utils.h"
|
||||
|
||||
#define SHA256_DIGEST_SIZE 32
|
||||
|
||||
struct mem_file {
|
||||
u8 *data;
|
||||
size_t size;
|
||||
size_t offset;
|
||||
};
|
||||
|
||||
static int read_fn(void *fd, void *buf, size_t count)
|
||||
{
|
||||
struct mem_file *f = fd;
|
||||
|
||||
ASSERT(count <= f->size - f->offset);
|
||||
memcpy(buf, &f->data[f->offset], count);
|
||||
f->offset += count;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
struct mem_file f = { .size = 1000000 };
|
||||
size_t i;
|
||||
const struct libfsverity_merkle_tree_params params = {
|
||||
.version = 1,
|
||||
.hash_algorithm = FS_VERITY_HASH_ALG_SHA256,
|
||||
.block_size = 4096,
|
||||
.salt_size = 4,
|
||||
.salt = (u8 *)"abcd",
|
||||
.file_size = f.size,
|
||||
};
|
||||
struct libfsverity_digest *d;
|
||||
static const u8 expected_digest[SHA256_DIGEST_SIZE] =
|
||||
"\x91\x79\x00\xb0\xd2\x99\x45\x4a\xa3\x04\xd5\xde\xbc\x6f\x39"
|
||||
"\xe4\xaf\x7b\x5a\xbe\x33\xbd\xbc\x56\x8d\x5d\x8f\x1e\x5c\x4d"
|
||||
"\x86\x52";
|
||||
int err;
|
||||
|
||||
f.data = xmalloc(f.size);
|
||||
for (i = 0; i < f.size; i++)
|
||||
f.data[i] = (i % 11) + (i % 439) + (i % 1103);
|
||||
|
||||
err = libfsverity_compute_digest(&f, read_fn, ¶ms, &d);
|
||||
ASSERT(err == 0);
|
||||
|
||||
ASSERT(d->digest_algorithm == FS_VERITY_HASH_ALG_SHA256);
|
||||
ASSERT(d->digest_size == SHA256_DIGEST_SIZE);
|
||||
ASSERT(!memcmp(d->digest, expected_digest, SHA256_DIGEST_SIZE));
|
||||
|
||||
free(f.data);
|
||||
free(d);
|
||||
printf("test_compute_digest passed\n");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
/*
|
||||
* Test the hash algorithm-related libfsverity APIs.
|
||||
*
|
||||
* Copyright 2020 Google LLC
|
||||
*/
|
||||
#include "utils.h"
|
||||
|
||||
#define SHA256_DIGEST_SIZE 32
|
||||
#define SHA512_DIGEST_SIZE 64
|
||||
|
||||
int main(void)
|
||||
{
|
||||
ASSERT(libfsverity_get_digest_size(0) == -1);
|
||||
ASSERT(libfsverity_get_hash_name(0) == NULL);
|
||||
ASSERT(libfsverity_find_hash_alg_by_name("bad") == 0);
|
||||
ASSERT(libfsverity_find_hash_alg_by_name(NULL) == 0);
|
||||
|
||||
ASSERT(libfsverity_get_digest_size(100) == -1);
|
||||
ASSERT(libfsverity_get_hash_name(100) == NULL);
|
||||
|
||||
ASSERT(libfsverity_get_digest_size(FS_VERITY_HASH_ALG_SHA256) ==
|
||||
SHA256_DIGEST_SIZE);
|
||||
ASSERT(!strcmp("sha256",
|
||||
libfsverity_get_hash_name(FS_VERITY_HASH_ALG_SHA256)));
|
||||
ASSERT(libfsverity_find_hash_alg_by_name("sha256") ==
|
||||
FS_VERITY_HASH_ALG_SHA256);
|
||||
|
||||
ASSERT(libfsverity_get_digest_size(FS_VERITY_HASH_ALG_SHA512) ==
|
||||
SHA512_DIGEST_SIZE);
|
||||
ASSERT(!strcmp("sha512",
|
||||
libfsverity_get_hash_name(FS_VERITY_HASH_ALG_SHA512)));
|
||||
ASSERT(libfsverity_find_hash_alg_by_name("sha512") ==
|
||||
FS_VERITY_HASH_ALG_SHA512);
|
||||
|
||||
printf("test_hash_algs passed\n");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
/*
|
||||
* Test libfsverity_sign_digest().
|
||||
*
|
||||
* Copyright 2020 Google LLC
|
||||
*/
|
||||
#include "utils.h"
|
||||
|
||||
#include <fcntl.h>
|
||||
|
||||
#define SHA256_DIGEST_SIZE 32
|
||||
|
||||
int main(void)
|
||||
{
|
||||
struct libfsverity_digest *d = xzalloc(sizeof(*d) + SHA256_DIGEST_SIZE);
|
||||
const struct libfsverity_signature_params params = {
|
||||
.keyfile = "testdata/key.pem",
|
||||
.certfile = "testdata/cert.pem",
|
||||
};
|
||||
u8 *sig;
|
||||
size_t sig_size;
|
||||
struct filedes file;
|
||||
u8 *expected_sig;
|
||||
u64 expected_sig_size;
|
||||
int err;
|
||||
|
||||
d->digest_algorithm = FS_VERITY_HASH_ALG_SHA256;
|
||||
d->digest_size = SHA256_DIGEST_SIZE;
|
||||
memcpy(d->digest,
|
||||
"\x91\x79\x00\xb0\xd2\x99\x45\x4a\xa3\x04\xd5\xde\xbc\x6f\x39"
|
||||
"\xe4\xaf\x7b\x5a\xbe\x33\xbd\xbc\x56\x8d\x5d\x8f\x1e\x5c\x4d"
|
||||
"\x86\x52", SHA256_DIGEST_SIZE);
|
||||
|
||||
err = libfsverity_sign_digest(d, ¶ms, &sig, &sig_size);
|
||||
ASSERT(err == 0);
|
||||
|
||||
ASSERT(open_file(&file, "testdata/file.sig", O_RDONLY, 0));
|
||||
ASSERT(get_file_size(&file, &expected_sig_size));
|
||||
ASSERT(sig_size == expected_sig_size);
|
||||
expected_sig = xmalloc(sig_size);
|
||||
ASSERT(full_read(&file, expected_sig, sig_size));
|
||||
ASSERT(!memcmp(sig, expected_sig, sig_size));
|
||||
|
||||
free(d);
|
||||
free(sig);
|
||||
free(expected_sig);
|
||||
filedes_close(&file);
|
||||
printf("test_sign_digest passed\n");
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user