#include "object/tal.c"

#include <check.h>

#include "alloc.c"
#include "common.c"
#include "file.c"
#include "line_file.c"
#include "mock.c"
#include "data_structure/path_builder.c"
#include "rrdp.h"
#include "types/uri.c"
#include "crypto/base64.c"

/* Mocks */

MOCK(config_get_local_repository, char const *, "tmp/tal", void)
MOCK_ABORT_VOID(cache_setup, void)
MOCK(cache_create, struct rpki_cache *, NULL, char const *tal)
MOCK_VOID(cache_destroy, struct rpki_cache *cache)
MOCK_ABORT_INT(cache_download, struct rpki_cache *cache, struct rpki_uri *uri,
    bool *changed)
MOCK_ABORT_INT(cache_download_alt, struct rpki_cache *cache,
    struct uri_list *uris, bool use_rrdp, uris_dl_cb cb, void *arg)
MOCK_ABORT_PTR(cache_recover, rpki_uri, struct rpki_cache *cache,
    struct uri_list *uris, bool use_rrdp)
MOCK_ABORT_VOID(cache_teardown, void)
MOCK_ABORT_INT(certificate_traverse, struct rpp *rpp_parent,
    struct rpki_uri *cert_uri)
MOCK_ABORT_PTR(db_table_create, db_table, void)
MOCK_VOID(db_table_destroy, struct db_table *table)
MOCK_ABORT_INT(db_table_join, struct db_table *dst, struct db_table *src)
MOCK_ABORT_INT(deferstack_pop, struct cert_stack *stack,
    struct deferred_cert *result)
MOCK_ABORT_VOID(fnstack_cleanup, void)
MOCK_ABORT_VOID(fnstack_init, void)
MOCK_ABORT_VOID(fnstack_push, char const *f)
MOCK_ABORT_INT(handle_roa_v4, uint32_t as, struct ipv4_prefix const *prefix,
    uint8_t max_length, void *arg)
MOCK_ABORT_INT(handle_roa_v6, uint32_t as, struct ipv6_prefix const *prefix,
    uint8_t max_length, void *arg)
MOCK_ABORT_INT(handle_router_key, unsigned char const *ski,
    struct asn_range const *asns, unsigned char const *spk, void *arg)
MOCK_ABORT_VOID(rpp_refput, struct rpp *pp)
MOCK_ABORT_INT(rrdp_update, struct rpki_uri *uri)
MOCK(state_retrieve, struct validation *, NULL, void)
MOCK_ABORT_PTR(validation_certstack, cert_stack, struct validation *state)
MOCK_ABORT_VOID(validation_destroy, struct validation *state)
MOCK_ABORT_INT(validation_prepare, struct validation **out, struct tal *tal,
    struct validation_handler *validation_handler)
MOCK_ABORT_ENUM(validation_pubkey_state, pubkey_state, struct validation *state)
MOCK(validation_tal, struct tal *, NULL, struct validation *state)

/* Tests */

START_TEST(tal_load_normal)
{
	struct tal tal;
	unsigned int i;
	/* Got this by feeding the subjectPublicKeyInfo to `base64 -d`. */
	unsigned char decoded[] = {
	    0x30, 0x82, 0x01, 0x22, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48,
	    0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01,
	    0x0F, 0x00, 0x30, 0x82, 0x01, 0x0A, 0x02, 0x82, 0x01, 0x01, 0x00,
	    0xA9, 0x91, 0x33, 0x85, 0x82, 0xB4, 0xF8, 0xFB, 0x43, 0x38, 0xF7,
	    0xEE, 0x6F, 0xF2, 0x91, 0x73, 0x73, 0x1E, 0x5B, 0x1D, 0xE7, 0x79,
	    0x7C, 0x78, 0xFF, 0x06, 0xE7, 0x25, 0x61, 0x9B, 0x34, 0x0B, 0x5B,
	    0x62, 0xA5, 0xE0, 0xDE, 0xE5, 0x39, 0x27, 0x81, 0xC5, 0xCC, 0xF8,
	    0x24, 0xFD, 0x52, 0x29, 0xA6, 0x04, 0x8A, 0x02, 0x19, 0x4E, 0xD0,
	    0x7E, 0xB4, 0x0D, 0x13, 0xF8, 0xF1, 0xBC, 0xBD, 0x82, 0xBE, 0x7F,
	    0xC8, 0x31, 0xEE, 0xD8, 0xA5, 0xE1, 0x3A, 0x69, 0xCC, 0x83, 0x8E,
	    0xAC, 0x62, 0xC5, 0x08, 0xA5, 0xF8, 0x2D, 0x05, 0x2F, 0x7E, 0x56,
	    0xDA, 0xEA, 0x5B, 0x38, 0x89, 0x7D, 0xBF, 0xA9, 0x90, 0x6B, 0x6E,
	    0x39, 0x67, 0x93, 0x9E, 0x3E, 0xB3, 0x06, 0x60, 0x4D, 0x64, 0xA2,
	    0xBE, 0xE4, 0x09, 0x4C, 0x09, 0x6D, 0x56, 0x3E, 0x1A, 0xF2, 0x94,
	    0x87, 0x01, 0xF9, 0x74, 0x99, 0xC7, 0xCB, 0x64, 0xF4, 0x64, 0xBF,
	    0xDD, 0x23, 0x10, 0xF9, 0x87, 0xCC, 0x57, 0x0C, 0x00, 0xC9, 0x88,
	    0xEC, 0x7B, 0x1D, 0x78, 0x53, 0x3B, 0x68, 0xE0, 0x68, 0xCE, 0x34,
	    0x02, 0xC4, 0xE6, 0x88, 0x75, 0x33, 0x7F, 0xA0, 0x95, 0x14, 0x1D,
	    0xB8, 0x3E, 0xAF, 0xCD, 0x2C, 0x0E, 0x0F, 0xE5, 0x9A, 0x84, 0xC6,
	    0xDC, 0xF6, 0xF0, 0x8E, 0x4C, 0x40, 0xFE, 0xD8, 0xC7, 0x0B, 0x1D,
	    0x12, 0xA0, 0x35, 0xA4, 0x1F, 0xD1, 0x82, 0x7D, 0x6B, 0x58, 0xC6,
	    0xF6, 0x82, 0x48, 0xBC, 0x39, 0x0A, 0x5C, 0x4A, 0x9D, 0x7E, 0xA0,
	    0xD1, 0x92, 0xDC, 0x32, 0xA0, 0x3E, 0xF8, 0x71, 0x5E, 0x7B, 0x6D,
	    0x6D, 0xED, 0x04, 0x07, 0xB1, 0x07, 0xB1, 0xA0, 0x94, 0x84, 0xDB,
	    0x22, 0x7C, 0x90, 0x02, 0xC9, 0x9D, 0x9E, 0x0B, 0x5F, 0x83, 0x62,
	    0xD4, 0x32, 0xB7, 0x11, 0x38, 0x71, 0xCF, 0xF3, 0xA4, 0x0F, 0x64,
	    0x83, 0x63, 0x0D, 0x02, 0x03, 0x01, 0x00, 0x01
	};

	ck_assert_int_eq(tal_init(&tal, "tal/lacnic.tal"), 0);

	ck_assert_uint_eq(tal.uris.len, 3);
	ck_assert_str_eq(tal.uris.array[0]->global,
	    "rsync://repository.lacnic.net/rpki/lacnic/rta-lacnic-rpki.cer");
	ck_assert_str_eq(tal.uris.array[1]->global, "https://potato");
	ck_assert_str_eq(tal.uris.array[2]->global, "rsync://potato");

	ck_assert_uint_eq(ARRAY_LEN(decoded), tal.spki_len);
	for (i = 0; i < ARRAY_LEN(decoded); i++)
		ck_assert_uint_eq(tal.spki[i], decoded[i]);

	tal_cleanup(&tal);
}
END_TEST

static Suite *tal_load_suite(void)
{
	Suite *suite;
	TCase *core;

	core = tcase_create("Core");
	tcase_add_test(core, tal_load_normal);

	suite = suite_create("tal_load()");
	suite_add_tcase(suite, core);
	return suite;
}

int main(void)
{
	Suite *suite;
	SRunner *runner;
	int tests_failed;

	suite = tal_load_suite();

	runner = srunner_create(suite);
	srunner_run_all(runner, CK_NORMAL);
	tests_failed = srunner_ntests_failed(runner);
	srunner_free(runner);

	return (tests_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}
