libpsyc/test/test_packet_id.c

84 lines
2.0 KiB
C
Raw Normal View History

2011-12-28 22:45:16 +00:00
#include <stdlib.h>
#include <stdio.h>
#include <psyc.h>
#include <psyc/packet.h>
#include <psyc/render.h>
uint8_t verbose;
2011-12-28 22:45:16 +00:00
int
packet_id (char *context, size_t contextlen,
char *source, size_t sourcelen,
char *target, size_t targetlen,
char *counter, size_t counterlen,
char *fragment, size_t fragmentlen,
char *result, size_t resultlen)
{
PsycList list;
PsycElem elems[PSYC_PACKET_ID_ELEMS];
memset(&list, 0, sizeof(PsycList));
memset(elems, 0, sizeof(PsycElem) * PSYC_PACKET_ID_ELEMS);
psyc_packet_id(&list, elems, context, contextlen,
source, sourcelen, target, targetlen,
counter, counterlen, fragment, fragmentlen);
size_t idlen = list.length;
2011-12-28 22:45:16 +00:00
char *id = malloc(idlen);
psyc_render_list(&list, id, idlen);
if (verbose)
printf("[%.*s]\n", (int)idlen, id);
2011-12-28 22:45:16 +00:00
int ret = idlen == resultlen && memcmp(result, id, idlen) == 0;
free(id);
return ret;
}
int
main (int argc, char **argv)
{
verbose = argc > 1;
2011-12-28 22:45:16 +00:00
if (!packet_id(PSYC_C2ARG(""),
PSYC_C2ARG("psyc://example.net/~alice"),
PSYC_C2ARG("psyc://example.net/~bob"),
PSYC_C2ARG("1337"),
PSYC_C2ARG("42"),
PSYC_C2ARG("|| psyc://example.net/~alice"
"| psyc://example.net/~bob"
"| 1337| 42")))
2011-12-28 22:45:16 +00:00
return 1;
if (!packet_id(PSYC_C2ARG("psyc://example.net/@bar"),
PSYC_C2ARG("psyc://example.net/~alice"),
PSYC_C2ARG(""),
PSYC_C2ARG("1337"),
PSYC_C2ARG("42"),
PSYC_C2ARG("| psyc://example.net/@bar"
"| psyc://example.net/~alice|"
"| 1337| 42")))
2011-12-28 22:45:16 +00:00
return 2;
if (!packet_id(PSYC_C2ARG("psyc://example.net/@bar"),
PSYC_C2ARG(""),
PSYC_C2ARG("psyc://example.net/~alice"),
PSYC_C2ARG("1337"),
PSYC_C2ARG("42"),
PSYC_C2ARG("| psyc://example.net/@bar|"
"| psyc://example.net/~alice"
"| 1337| 42")))
2011-12-28 22:45:16 +00:00
return 3;
if (!packet_id(PSYC_C2ARG("psyc://example.net/@bar"),
PSYC_C2ARG(""),
PSYC_C2ARG(""),
PSYC_C2ARG(""),
PSYC_C2ARG(""),
PSYC_C2ARG("| psyc://example.net/@bar||||")))
2011-12-28 22:45:16 +00:00
return 4;
return 0;
}