#include "config.h"
#include <assert.h>
#include <common/amount.h>
#include <common/memleak.h>
#include <common/node_id.h>
#include <common/setup.h>
#include <common/status.c>
#include <common/status_levels.h>
#include <common/wireaddr.c>
#include <connectd/netaddress.c>
#include <stdio.h>
#include <wire/wire.h>

#define DEFAULT_PORT 9735

/* AUTOGENERATED MOCKS START */
/* AUTOGENERATED MOCKS END */

int main(int argc, char *argv[])
{
	struct wireaddr wa;
	const char *err;

	common_setup(argv[0]);

	// start with IPv4
	err = parse_wireaddr(tmpctx, "0.0.0.0", DEFAULT_PORT, NULL, &wa);
	assert(!err);
	assert(!IsValid(&wa));
	assert(IsIPv4(&wa));
	assert(!IsIPv6(&wa));
	err = parse_wireaddr(tmpctx, "255.255.255.255", DEFAULT_PORT, NULL, &wa);
	assert(!err);
	assert(!IsValid(&wa));
	assert(IsIPv4(&wa));
	assert(!IsIPv6(&wa));

	err = parse_wireaddr(tmpctx, "127.0.0.1", DEFAULT_PORT, NULL, &wa);
	assert(!err);
	assert(IsValid(&wa));
	assert(IsIPv4(&wa));
	assert(!IsIPv6(&wa));
	assert(IsLocal(&wa));
	assert(address_routable(&wa, true));
	assert(!address_routable(&wa, false));

	err = parse_wireaddr(tmpctx, "0.1.2.3", DEFAULT_PORT, NULL, &wa);
	assert(!err);
	assert(IsValid(&wa));
	assert(IsIPv4(&wa));
	assert(!IsIPv6(&wa));
	assert(IsLocal(&wa));
	assert(address_routable(&wa, true));
	assert(!address_routable(&wa, false));

	err = parse_wireaddr(tmpctx, "10.0.21.42", DEFAULT_PORT, NULL, &wa);
	assert(!err);
	assert(IsValid(&wa));
	assert(IsIPv4(&wa));
	assert(!IsIPv6(&wa));
	assert(!IsLocal(&wa));
	assert(IsRFC1918(&wa));
	assert(!address_routable(&wa, true));
	assert(!address_routable(&wa, false));

	err = parse_wireaddr(tmpctx, "192.168.123.4", DEFAULT_PORT, NULL, &wa);
	assert(!err);
	assert(IsValid(&wa));
	assert(IsIPv4(&wa));
	assert(!IsIPv6(&wa));
	assert(!IsLocal(&wa));
	assert(IsRFC1918(&wa));
	assert(!address_routable(&wa, true));
	assert(!address_routable(&wa, false));

	err = parse_wireaddr(tmpctx, "1.2.3.4", DEFAULT_PORT, NULL, &wa);
	assert(!err);
	assert(IsValid(&wa));
	assert(IsIPv4(&wa));
	assert(!IsIPv6(&wa));
	assert(!IsLocal(&wa));
	assert(address_routable(&wa, true));
	assert(address_routable(&wa, false));

	// now IPv6 stuff
	err = parse_wireaddr(tmpctx, "2142:DEAD:beef::1", DEFAULT_PORT, NULL, &wa);
	assert(!err);
	assert(IsValid(&wa));
	assert(!IsIPv4(&wa));
	assert(IsIPv6(&wa));
	assert(!IsLocal(&wa));
	assert(address_routable(&wa, true));
	assert(address_routable(&wa, false));

	err = parse_wireaddr(tmpctx, "0::0", DEFAULT_PORT, NULL, &wa);
	assert(!err);
	assert(!IsValid(&wa));
	assert(!IsIPv4(&wa));
	assert(IsIPv6(&wa));

	err = parse_wireaddr(tmpctx, "2001:db8::1", DEFAULT_PORT, NULL, &wa);
	assert(!err);
	assert(!IsValid(&wa));
	assert(!IsIPv4(&wa));
	assert(IsIPv6(&wa));

	common_shutdown();
}
