Exemplo n.º 1
0
static void runtest_keys_valid(const char *json_base_fn)
{
	char *json_fn = test_filename(json_base_fn);
	cJSON *tests = read_json(json_fn);
	assert((tests->type & 0xFF) == cJSON_Array);

	unsigned int idx;

	for (idx = 0; idx < cJSON_GetArraySize(tests); idx++) {

	    cJSON *test = cJSON_GetArrayItem(tests, idx);
	    assert((test->type & 0xFF) == cJSON_Array);
	    assert(cJSON_GetArraySize(test) == 3);

		cJSON *j_base58 = cJSON_GetArrayItem(test, 0);
		cJSON *j_payload = cJSON_GetArrayItem(test, 1);
		assert((j_base58->type & 0xFF) == cJSON_String);
		assert((j_payload->type & 0xFF) == cJSON_String);

		cJSON *j_meta = cJSON_GetArrayItem(test, 2);
		assert((j_meta->type & 0xFF) == cJSON_Object);

		cJSON *j_addrtype = cJSON_GetObjectItem(j_meta, "addrType");
		assert(!j_addrtype || ((j_addrtype->type & 0xFF) == cJSON_String));

		cJSON *j_compress = cJSON_GetObjectItem(j_meta, "isCompressed");
		assert(!j_compress || ((j_compress->type & 0xFF) == cJSON_True) ||
		       ((j_compress->type & 0xFF) == cJSON_False));

		bool is_privkey = ((cJSON_GetObjectItem(j_meta, "isPrivkey")->type & 0xFF) == cJSON_True);
		bool is_testnet = ((cJSON_GetObjectItem(j_meta, "isTestnet")->type & 0xFF) == cJSON_True);

		if (is_privkey) {
			test_privkey_valid_enc(
			    j_base58->valuestring,
				hex2str(j_payload->valuestring),
				((j_compress->type & 0xFF) == cJSON_True),
				is_testnet);
			test_privkey_valid_dec(
				j_base58->valuestring,
				hex2str(j_payload->valuestring),
				((j_compress->type & 0xFF) == cJSON_True),
				is_testnet);
		} else {
			test_pubkey_valid_enc(
				j_base58->valuestring,
				hex2str(j_payload->valuestring),
				j_addrtype->valuestring,
				is_testnet);
			test_pubkey_valid_dec(
				j_base58->valuestring,
				hex2str(j_payload->valuestring),
				j_addrtype->valuestring,
				is_testnet);
		}
	}

	free(json_fn);
	cJSON_Delete(tests);
}
Exemplo n.º 2
0
static void runtest_keys_valid(const char *base_fn)
{
	char *fn = NULL;

	fn = test_filename(base_fn);
	json_t *data = read_json(fn);
	assert(json_is_array(data));

	size_t n_tests = json_array_size(data);
	unsigned int i;

	for (i = 0; i < n_tests; i++) {
		json_t *inner;

		inner = json_array_get(data, i);
		assert(json_is_array(inner));

		json_t *j_base58 = json_array_get(inner, 0);
		json_t *j_payload = json_array_get(inner, 1);
		assert(json_is_string(j_base58));
		assert(json_is_string(j_payload));

		json_t *j_meta = json_array_get(inner, 2);
		assert(json_is_object(j_meta));

		json_t *j_addrtype = json_object_get(j_meta, "addrType");
		assert(!j_addrtype || json_is_string(j_addrtype));

		json_t *j_compress = json_object_get(j_meta, "isCompressed");
		assert(!j_compress || json_is_true(j_compress) ||
		       json_is_false(j_compress));

		bool is_privkey = json_is_true(json_object_get(j_meta, "isPrivkey"));
		bool is_testnet = json_is_true(json_object_get(j_meta, "isTestnet"));

		if (is_privkey) {
			test_privkey_valid_enc(
				json_string_value(j_base58),
				hex2str(json_string_value(j_payload)),
				json_is_true(j_compress),
				is_testnet);
			test_privkey_valid_dec(
				json_string_value(j_base58),
				hex2str(json_string_value(j_payload)),
				json_is_true(j_compress),
				is_testnet);
		} else {
			test_pubkey_valid_enc(
				json_string_value(j_base58),
				hex2str(json_string_value(j_payload)),
				json_string_value(j_addrtype),
				is_testnet);
			test_pubkey_valid_dec(
				json_string_value(j_base58),
				hex2str(json_string_value(j_payload)),
				json_string_value(j_addrtype),
				is_testnet);
		}
	}

	free(fn);
	json_decref(data);
}