rgb-cln/common/test/run-ip_port_parsing.c

85 lines
2.6 KiB
C

#include "../common/wireaddr.c"
#include <stdio.h>
#include <assert.h>
/* AUTOGENERATED MOCKS START */
/* Generated stub for fromwire */
const u8 *fromwire(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, void *copy UNNEEDED, size_t n UNNEEDED)
{ fprintf(stderr, "fromwire called!\n"); abort(); }
/* Generated stub for fromwire_u16 */
u16 fromwire_u16(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
{ fprintf(stderr, "fromwire_u16 called!\n"); abort(); }
/* Generated stub for fromwire_u8 */
u8 fromwire_u8(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
{ fprintf(stderr, "fromwire_u8 called!\n"); abort(); }
/* Generated stub for towire */
void towire(u8 **pptr UNNEEDED, const void *data UNNEEDED, size_t len UNNEEDED)
{ fprintf(stderr, "towire called!\n"); abort(); }
/* Generated stub for towire_u16 */
void towire_u16(u8 **pptr UNNEEDED, u16 v UNNEEDED)
{ fprintf(stderr, "towire_u16 called!\n"); abort(); }
/* Generated stub for towire_u8 */
void towire_u8(u8 **pptr UNNEEDED, u8 v UNNEEDED)
{ fprintf(stderr, "towire_u8 called!\n"); abort(); }
/* AUTOGENERATED MOCKS END */
int main(void)
{
struct wireaddr addr;
tal_t *ctx = tal_tmpctx(NULL);
char *ip;
u16 port;
/* ret = getaddrinfo("[::1]:80", NULL, NULL, &res); */
assert(parse_ip_port(ctx, "[::1]:80", &ip, &port));
assert(streq(ip, "::1"));
assert(port == 80);
assert(!parse_ip_port(ctx, "ip6-localhost", &ip, &port));
assert(streq(ip, "ip6-localhost"));
assert(port == 0);
assert(!parse_ip_port(ctx, "::1", &ip, &port));
assert(streq(ip, "::1"));
assert(port == 0);
assert(parse_ip_port(ctx, "192.168.1.1:8000", &ip, &port));
assert(streq(ip, "192.168.1.1"));
assert(port == 8000);
assert(!parse_ip_port(ctx, "192.168.2.255", &ip, &port));
assert(streq(ip, "192.168.2.255"));
assert(port == 0);
// unusual but possibly valid case
assert(!parse_ip_port(ctx, "[::1]", &ip, &port));
assert(streq(ip, "::1"));
assert(port == 0);
// service names not supported yet
assert(!parse_ip_port(ctx, "[::1]:http", &ip, &port));
assert(streq(ip, "::1"));
assert(port == 0);
// localhost hostnames for backward compat
parse_wireaddr("localhost", &addr, 200);
assert(addr.port == 200);
// string should win the port battle
parse_wireaddr("[::1]:9735", &addr, 500);
assert(addr.port == 9735);
ip = fmt_wireaddr(ctx, &addr);
assert(streq(ip, "[::1]:9735"));
// should use argument if we have no port in string
parse_wireaddr("2001:db8:85a3::8a2e:370:7334", &addr, 9777);
assert(addr.port == 9777);
ip = fmt_wireaddr(ctx, &addr);
assert(streq(ip, "[2001:db8:85a3::8a2e:370:7334]:9777"));
tal_free(ctx);
return 0;
}