Crypt-RHash

 view release on metacpan or  search on metacpan

librhash/test_lib.c  view on Meta::CPAN

	rhash ctx = rhash_init_multi(2, hash_ids);
	REQUIRE_TRUE(ctx, "NULL rhash context\n");
	CHECK_EQ(RHASH_ERROR, rhash_get_context(ctx, RHASH_MD5, &context_ptr), "RHASH_ERROR expected\n");
	CHECK_EQ(RHASH_ERROR, rhash_get_context(ctx, RHASH_SHA1, &context_ptr), "RHASH_ERROR expected\n");
	CHECK_EQ(0, rhash_get_context(ctx, RHASH_CRC32, &context_ptr), "failed to get context pointer\n");
	REQUIRE_NE(NULL, context_ptr, "NULL context pointer\n");
	CHECK_EQ(0, *(uint32_t*)context_ptr, "wrong crc32 value\n");
	rhash_update(ctx, "abcd", 4);
	CHECK_EQ(0, rhash_get_context(ctx, RHASH_HAS160, &context_ptr), "failed to get context pointer\n");
	REQUIRE_NE(NULL, context_ptr, "NULL context pointer\n");
	CHECK_EQ(0, memcmp("abcd", context_ptr, 4), "wrong message buffer content in hash context\n");
	CHECK_EQ(4, *((uint64_t*)context_ptr + 8), "wrong message length in hash context\n");
	rhash_free(ctx);
}

static void test_import_export(void)
{
#if !defined(NO_IMPORT_EXPORT)
	unsigned export_id = RHASH_ALL_HASHES;
	uint8_t data[241];
	size_t i;
	size_t min_sizes[3] = { 0, 1024, 8192 };
	dbg("test import/export\n");
	for (i = 0; i < sizeof(data); i++)
		data[i] = (uint8_t)i;
	for(i = 0; i < 3; i++) {
		size_t min_size = min_sizes[i];
		size_t size = 0;
		size_t required_size;
		size_t exported_size;
		void* exported_data;
		unsigned imported_ids[RHASH_HASH_COUNT];
		size_t imported_ids_count;
		size_t j;
		rhash ctx = rhash_init(export_id);
		rhash imported_ctx;
		size_t exported_ids_count = rhash_get_ctx_algorithms(ctx, 0, 0);
		CHECK_TRUE(exported_ids_count > 0, "wrong number of exported algorithms");

		for (; size < min_size; size += sizeof(data))
			rhash_update(ctx, data, sizeof(data));
		if (export_id == RHASH_BTIH || export_id == RHASH_ALL_HASHES) {
			rhash_torrent_set_program_name(ctx, "test");
			rhash_torrent_add_announce(ctx, "url1");
			rhash_torrent_add_announce(ctx, "url2");
			rhash_torrent_add_file(ctx, "file1", 1);
			rhash_torrent_add_file(ctx, "file2", 22);
			rhash_torrent_set_options(ctx, RHASH_TORRENT_OPT_PRIVATE);
			if ((i & 1) != 0)
				rhash_final(ctx, 0);
		}
		dbg2("- call rhash_export NULL\n");
		required_size = rhash_export(ctx, NULL, 0);
		if (!required_size) {
			log_error1("rhash_export failed for block size=%u\n", (unsigned)size);
			return;
		}
		dbg2("- call rhash_export ctx\n");
		exported_data = malloc(required_size);
		exported_size = rhash_export(ctx, exported_data, required_size);
		if (exported_size != required_size) {
			log_error2("rhash_export failed: %u != %u\n", (unsigned)exported_size, (unsigned)required_size);
			return;
		}
		dbg2("- call rhash_import\n");
		imported_ctx = rhash_import(exported_data, required_size);
		if (!imported_ctx) {
			log_error1("rhash_import failed for block size=%u\n", (unsigned)size);
			return;
		}
		free(exported_data);
		dbg2("- call rhash_final ctx\n");
		rhash_final(ctx, 0);
		dbg2("- call rhash_final imported_ctx\n");
		rhash_final(imported_ctx, 0);
		exported_data = NULL;
		imported_ids_count = rhash_get_ctx_algorithms(ctx, RHASH_HASH_COUNT, imported_ids);
		CHECK_EQ(exported_ids_count, imported_ids_count, "wrong number of imported algorithms");
		dbg2("- verify results\n");
		for (j = 0; j < imported_ids_count; j++) {
			unsigned hash_id = imported_ids[j];
			static char out1[240], out2[240];
			REQUIRE_NE(0, rhash_print(out1, ctx, hash_id, RHPR_UPPERCASE), "rhash_print failed");
			REQUIRE_NE(0, rhash_print(out2, imported_ctx, hash_id, RHPR_UPPERCASE), "rhash_print failed");
			if (strcmp(out1, out2) != 0) {
				const char* hash_name = rhash_get_name(hash_id);
				log_error4("import failed, wrong hash %s != %s for %s,  block size=%u\n",
					out1, out2, hash_name, (unsigned)size);
				return;
			}
		}
		rhash_free(ctx);
		rhash_free(imported_ctx);
	}
#endif /* !defined(NO_IMPORT_EXPORT) */
}

#define TEST_PATH 0x4000000

/**
 * Verify a magnet link.
 */
static void assert_magnet(const char* expected,
	rhash ctx, unsigned mask, int flags)
{
	static char out[2800];
	const char* path = (flags & TEST_PATH ? "test.txt" : NULL);
	size_t size_calculated = rhash_print_magnet(NULL, path, ctx, mask, flags);
	size_t size_printed;
	REQUIRE_TRUE(size_calculated < sizeof(out), "too small buffer for magnet link\n");

	flags &= ~TEST_PATH;
	size_printed = rhash_print_magnet(out, path, ctx, mask, flags);

	if (expected && strcmp(expected, out) != 0) {
		log_error2("\"%s\" != \"%s\"\n", expected, out);
	} else {
		size_t length = strlen(out) + 1;
		if (size_printed != length) {
			log_error3("rhash_print_magnet returns wrong length %u != %u for \"%s\"\n",
				(unsigned)size_printed, (unsigned)length, out);



( run in 1.940 second using v1.01-cache-2.11-cpan-995e09ba956 )