2014-01-19 01:09:44 +00:00
|
|
|
/*
|
|
|
|
* This file Copyright (C) 2013-2014 Mnemosyne LLC
|
|
|
|
*
|
2014-01-21 03:10:30 +00:00
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
2014-01-19 01:09:44 +00:00
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
#include <ctype.h> /* isspace() */
|
2012-12-14 04:34:42 +00:00
|
|
|
#include <errno.h> /* EILSEQ */
|
2017-04-21 07:40:57 +00:00
|
|
|
#include <string.h> /* strlen(), strncmp() */
|
2012-12-14 04:34:42 +00:00
|
|
|
|
|
|
|
#include <event2/buffer.h>
|
|
|
|
|
2015-05-04 19:58:34 +00:00
|
|
|
#define __LIBTRANSMISSION_VARIANT_MODULE__
|
2017-11-14 20:21:28 +00:00
|
|
|
|
2012-12-14 04:34:42 +00:00
|
|
|
#include "transmission.h"
|
|
|
|
#include "utils.h" /* tr_free */
|
|
|
|
#include "variant.h"
|
|
|
|
#include "variant-common.h"
|
|
|
|
|
|
|
|
#include "libtransmission-test.h"
|
|
|
|
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifndef _WIN32
|
2012-12-14 04:34:42 +00:00
|
|
|
#define STACK_SMASH_DEPTH (1 * 1000 * 1000)
|
|
|
|
#else
|
2017-04-19 12:04:45 +00:00
|
|
|
#define STACK_SMASH_DEPTH (100 * 1000)
|
2012-12-14 04:34:42 +00:00
|
|
|
#endif
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static int testInt(void)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
uint8_t buf[128];
|
|
|
|
int64_t val;
|
|
|
|
int err;
|
2017-04-20 16:02:19 +00:00
|
|
|
uint8_t const* end;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* good int string */
|
|
|
|
tr_snprintf((char*)buf, sizeof(buf), "i64e");
|
|
|
|
err = tr_bencParseInt(buf, buf + 4, &end, &val);
|
2017-05-29 20:22:36 +00:00
|
|
|
check_int(err, ==, 0);
|
|
|
|
check_int(val, ==, 64);
|
2017-05-30 17:56:12 +00:00
|
|
|
check_ptr(end, ==, buf + 4);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* missing 'e' */
|
|
|
|
end = NULL;
|
|
|
|
val = 888;
|
|
|
|
err = tr_bencParseInt(buf, buf + 3, &end, &val);
|
2017-05-29 20:22:36 +00:00
|
|
|
check_int(err, ==, EILSEQ);
|
|
|
|
check_int(val, ==, 888);
|
2017-05-30 17:56:12 +00:00
|
|
|
check_ptr(end, ==, NULL);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* empty buffer */
|
|
|
|
err = tr_bencParseInt(buf, buf + 0, &end, &val);
|
2017-05-29 20:22:36 +00:00
|
|
|
check_int(err, ==, EILSEQ);
|
|
|
|
check_int(val, ==, 888);
|
2017-05-30 17:56:12 +00:00
|
|
|
check_ptr(end, ==, NULL);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* bad number */
|
|
|
|
tr_snprintf((char*)buf, sizeof(buf), "i6z4e");
|
|
|
|
err = tr_bencParseInt(buf, buf + 5, &end, &val);
|
2017-05-29 20:22:36 +00:00
|
|
|
check_int(err, ==, EILSEQ);
|
|
|
|
check_int(val, ==, 888);
|
2017-05-30 17:56:12 +00:00
|
|
|
check_ptr(end, ==, NULL);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* negative number */
|
|
|
|
tr_snprintf((char*)buf, sizeof(buf), "i-3e");
|
|
|
|
err = tr_bencParseInt(buf, buf + 4, &end, &val);
|
2017-05-29 20:22:36 +00:00
|
|
|
check_int(err, ==, 0);
|
|
|
|
check_int(val, ==, -3);
|
2017-05-30 17:56:12 +00:00
|
|
|
check_ptr(end, ==, buf + 4);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* zero */
|
|
|
|
tr_snprintf((char*)buf, sizeof(buf), "i0e");
|
|
|
|
err = tr_bencParseInt(buf, buf + 4, &end, &val);
|
2017-05-29 20:22:36 +00:00
|
|
|
check_int(err, ==, 0);
|
|
|
|
check_int(val, ==, 0);
|
2017-05-30 17:56:12 +00:00
|
|
|
check_ptr(end, ==, buf + 3);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* no leading zeroes allowed */
|
|
|
|
val = 0;
|
|
|
|
end = NULL;
|
|
|
|
tr_snprintf((char*)buf, sizeof(buf), "i04e");
|
|
|
|
err = tr_bencParseInt(buf, buf + 4, &end, &val);
|
2017-05-29 20:22:36 +00:00
|
|
|
check_int(err, ==, EILSEQ);
|
|
|
|
check_int(val, ==, 0);
|
2017-05-30 17:56:12 +00:00
|
|
|
check_ptr(end, ==, NULL);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
return 0;
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static int testStr(void)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
uint8_t buf[128];
|
|
|
|
int err;
|
|
|
|
int n;
|
2017-04-20 16:02:19 +00:00
|
|
|
uint8_t const* end;
|
|
|
|
uint8_t const* str;
|
2017-04-19 12:04:45 +00:00
|
|
|
size_t len;
|
|
|
|
|
|
|
|
/* string len is designed to overflow */
|
|
|
|
n = tr_snprintf((char*)buf, sizeof(buf), "%zu:boat", (size_t)(SIZE_MAX - 2));
|
|
|
|
err = tr_bencParseStr(buf, buf + n, &end, &str, &len);
|
2017-05-29 20:22:36 +00:00
|
|
|
check_int(err, ==, EILSEQ);
|
2017-05-29 20:39:51 +00:00
|
|
|
check_uint(len, ==, 0);
|
2017-05-30 17:56:12 +00:00
|
|
|
check_ptr(str, ==, NULL);
|
|
|
|
check_ptr(end, ==, NULL);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* good string */
|
|
|
|
n = tr_snprintf((char*)buf, sizeof(buf), "4:boat");
|
|
|
|
err = tr_bencParseStr(buf, buf + n, &end, &str, &len);
|
2017-05-29 20:22:36 +00:00
|
|
|
check_int(err, ==, 0);
|
2017-05-29 20:39:51 +00:00
|
|
|
check_uint(len, ==, 4);
|
2017-05-30 17:56:12 +00:00
|
|
|
check_mem(str, ==, "boat", len);
|
|
|
|
check_ptr(end, ==, buf + 6);
|
2017-04-19 12:04:45 +00:00
|
|
|
str = NULL;
|
|
|
|
end = NULL;
|
|
|
|
len = 0;
|
|
|
|
|
|
|
|
/* string goes past end of buffer */
|
|
|
|
err = tr_bencParseStr(buf, buf + (n - 1), &end, &str, &len);
|
2017-05-29 20:22:36 +00:00
|
|
|
check_int(err, ==, EILSEQ);
|
2017-05-29 20:39:51 +00:00
|
|
|
check_uint(len, ==, 0);
|
2017-05-30 17:56:12 +00:00
|
|
|
check_ptr(str, ==, NULL);
|
|
|
|
check_ptr(end, ==, NULL);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* empty string */
|
|
|
|
n = tr_snprintf((char*)buf, sizeof(buf), "0:");
|
|
|
|
err = tr_bencParseStr(buf, buf + n, &end, &str, &len);
|
2017-05-29 20:22:36 +00:00
|
|
|
check_int(err, ==, 0);
|
2017-05-29 20:39:51 +00:00
|
|
|
check_uint(len, ==, 0);
|
2017-05-30 17:56:12 +00:00
|
|
|
check_uint(*str, ==, '\0');
|
|
|
|
check_ptr(end, ==, buf + 2);
|
2017-04-19 12:04:45 +00:00
|
|
|
str = NULL;
|
|
|
|
end = NULL;
|
|
|
|
len = 0;
|
|
|
|
|
|
|
|
/* short string */
|
|
|
|
n = tr_snprintf((char*)buf, sizeof(buf), "3:boat");
|
|
|
|
err = tr_bencParseStr(buf, buf + n, &end, &str, &len);
|
2017-05-29 20:22:36 +00:00
|
|
|
check_int(err, ==, 0);
|
2017-05-29 20:39:51 +00:00
|
|
|
check_uint(len, ==, 3);
|
2017-05-30 17:56:12 +00:00
|
|
|
check_mem(str, ==, "boa", len);
|
|
|
|
check_ptr(end, ==, buf + 5);
|
2017-04-19 12:04:45 +00:00
|
|
|
str = NULL;
|
|
|
|
end = NULL;
|
|
|
|
len = 0;
|
|
|
|
|
|
|
|
return 0;
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static int testString(char const* str, bool isGood)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant val;
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* end = NULL;
|
2017-04-19 12:04:45 +00:00
|
|
|
char* saved;
|
2017-04-20 16:02:19 +00:00
|
|
|
size_t const len = strlen(str);
|
2017-04-19 12:04:45 +00:00
|
|
|
size_t savedLen;
|
|
|
|
int err;
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
err = tr_variantFromBencFull(&val, str, len, NULL, &end);
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!isGood)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-05-30 17:56:12 +00:00
|
|
|
check_int(err, !=, 0);
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-05-30 17:56:12 +00:00
|
|
|
check_int(err, ==, 0);
|
2012-12-14 04:34:42 +00:00
|
|
|
#if 0
|
2017-04-19 12:04:45 +00:00
|
|
|
fprintf(stderr, "in: [%s]\n", str);
|
|
|
|
fprintf(stderr, "out:\n%s", tr_variantToStr(&val, TR_VARIANT_FMT_JSON, NULL));
|
2012-12-14 04:34:42 +00:00
|
|
|
#endif
|
2017-05-30 17:56:12 +00:00
|
|
|
check_ptr(end, ==, str + len);
|
2017-04-19 12:04:45 +00:00
|
|
|
saved = tr_variantToStr(&val, TR_VARIANT_FMT_BENC, &savedLen);
|
2017-05-29 19:48:02 +00:00
|
|
|
check_str(saved, ==, str);
|
2017-05-29 20:39:51 +00:00
|
|
|
check_uint(savedLen, ==, len);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(saved);
|
|
|
|
tr_variantFree(&val);
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return 0;
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static int testParse(void)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant val;
|
|
|
|
tr_variant* child;
|
|
|
|
tr_variant* child2;
|
|
|
|
char buf[512];
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* end;
|
2017-04-19 12:04:45 +00:00
|
|
|
int err;
|
|
|
|
size_t len;
|
|
|
|
int64_t i;
|
|
|
|
char* saved;
|
|
|
|
|
|
|
|
tr_snprintf((char*)buf, sizeof(buf), "i64e");
|
|
|
|
err = tr_variantFromBencFull(&val, buf, sizeof(buf), NULL, &end);
|
2017-05-30 17:56:12 +00:00
|
|
|
check_int(err, ==, 0);
|
2017-04-19 12:04:45 +00:00
|
|
|
check(tr_variantGetInt(&val, &i));
|
2017-05-29 20:22:36 +00:00
|
|
|
check_int(i, ==, 64);
|
2017-05-30 17:56:12 +00:00
|
|
|
check_ptr(end, ==, buf + 4);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantFree(&val);
|
|
|
|
|
|
|
|
tr_snprintf((char*)buf, sizeof(buf), "li64ei32ei16ee");
|
|
|
|
err = tr_variantFromBencFull(&val, buf, sizeof(buf), NULL, &end);
|
2017-05-30 17:56:12 +00:00
|
|
|
check_int(err, ==, 0);
|
|
|
|
check_ptr(end, ==, buf + strlen((char*)buf));
|
|
|
|
check_uint(val.val.l.count, ==, 3);
|
2017-04-19 12:04:45 +00:00
|
|
|
check(tr_variantGetInt(&val.val.l.vals[0], &i));
|
2017-05-29 20:22:36 +00:00
|
|
|
check_int(i, ==, 64);
|
2017-04-19 12:04:45 +00:00
|
|
|
check(tr_variantGetInt(&val.val.l.vals[1], &i));
|
2017-05-29 20:22:36 +00:00
|
|
|
check_int(i, ==, 32);
|
2017-04-19 12:04:45 +00:00
|
|
|
check(tr_variantGetInt(&val.val.l.vals[2], &i));
|
2017-05-29 20:22:36 +00:00
|
|
|
check_int(i, ==, 16);
|
2017-04-19 12:04:45 +00:00
|
|
|
saved = tr_variantToStr(&val, TR_VARIANT_FMT_BENC, &len);
|
2017-05-29 19:48:02 +00:00
|
|
|
check_str(saved, ==, buf);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(saved);
|
|
|
|
tr_variantFree(&val);
|
|
|
|
|
|
|
|
end = NULL;
|
|
|
|
tr_snprintf((char*)buf, sizeof(buf), "lllee");
|
|
|
|
err = tr_variantFromBencFull(&val, buf, sizeof(buf), NULL, &end);
|
2017-05-30 17:56:12 +00:00
|
|
|
check_int(err, !=, 0);
|
|
|
|
check_ptr(end, ==, NULL);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
end = NULL;
|
|
|
|
tr_snprintf((char*)buf, sizeof(buf), "le");
|
|
|
|
err = tr_variantFromBencFull(&val, buf, sizeof(buf), NULL, &end);
|
2017-05-30 17:56:12 +00:00
|
|
|
check_int(err, ==, 0);
|
|
|
|
check_ptr(end, ==, buf + 2);
|
2017-04-19 12:04:45 +00:00
|
|
|
saved = tr_variantToStr(&val, TR_VARIANT_FMT_BENC, &len);
|
2017-05-29 19:48:02 +00:00
|
|
|
check_str(saved, ==, "le");
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(saved);
|
|
|
|
tr_variantFree(&val);
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((err = testString("llleee", true)) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((err = testString("d3:cow3:moo4:spam4:eggse", true)) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((err = testString("d4:spaml1:a1:bee", true)) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((err = testString("d5:greenli1ei2ei3ee4:spamd1:ai123e3:keyi214eee", true)) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((err = testString("d9:publisher3:bob17:publisher-webpage15:www.example.com18:publisher.location4:homee", true)) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((err = testString("d8:completei1e8:intervali1800e12:min intervali1800e5:peers0:e", true)) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((err = testString("d1:ai0e1:be", false)) != 0) /* odd number of children */
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((err = testString("", false)) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((err = testString(" ", false)) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* nested containers
|
|
|
|
* parse an unsorted dict
|
|
|
|
* save as a sorted dict */
|
|
|
|
end = NULL;
|
|
|
|
tr_snprintf((char*)buf, sizeof(buf), "lld1:bi32e1:ai64eeee");
|
|
|
|
err = tr_variantFromBencFull(&val, buf, sizeof(buf), NULL, &end);
|
2017-05-30 17:56:12 +00:00
|
|
|
check_int(err, ==, 0);
|
|
|
|
check_ptr(end, ==, buf + strlen((char const*)buf));
|
|
|
|
check_ptr((child = tr_variantListChild(&val, 0)), !=, NULL);
|
|
|
|
check_ptr((child2 = tr_variantListChild(child, 0)), !=, NULL);
|
2017-04-19 12:04:45 +00:00
|
|
|
saved = tr_variantToStr(&val, TR_VARIANT_FMT_BENC, &len);
|
2017-05-29 19:48:02 +00:00
|
|
|
check_str(saved, ==, "lld1:ai64e1:bi32eeee");
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(saved);
|
|
|
|
tr_variantFree(&val);
|
|
|
|
|
|
|
|
/* too many endings */
|
|
|
|
end = NULL;
|
|
|
|
tr_snprintf((char*)buf, sizeof(buf), "leee");
|
|
|
|
err = tr_variantFromBencFull(&val, buf, sizeof(buf), NULL, &end);
|
2017-05-30 17:56:12 +00:00
|
|
|
check_int(err, ==, 0);
|
|
|
|
check_ptr(end, ==, buf + 2);
|
2017-04-19 12:04:45 +00:00
|
|
|
saved = tr_variantToStr(&val, TR_VARIANT_FMT_BENC, &len);
|
2017-05-29 19:48:02 +00:00
|
|
|
check_str(saved, ==, "le");
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(saved);
|
|
|
|
tr_variantFree(&val);
|
|
|
|
|
|
|
|
/* no ending */
|
|
|
|
end = NULL;
|
|
|
|
tr_snprintf((char*)buf, sizeof(buf), "l1:a1:b1:c");
|
|
|
|
err = tr_variantFromBencFull(&val, buf, strlen(buf), NULL, &end);
|
2017-05-30 17:56:12 +00:00
|
|
|
check_int(err, !=, 0);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* incomplete string */
|
|
|
|
end = NULL;
|
|
|
|
tr_snprintf((char*)buf, sizeof(buf), "1:");
|
|
|
|
err = tr_variantFromBencFull(&val, buf, strlen(buf), NULL, &end);
|
2017-05-30 17:56:12 +00:00
|
|
|
check_int(err, !=, 0);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
return 0;
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void stripWhitespace(char* in)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-05-13 22:38:31 +00:00
|
|
|
char* out = in;
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (; in != NULL && *in != '\0'; ++in)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
if (!isspace(*in))
|
|
|
|
{
|
|
|
|
*out++ = *in;
|
|
|
|
}
|
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
*out = '\0';
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static int testJSONSnippet(char const* benc_str, char const* expected)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant top;
|
|
|
|
char* serialized;
|
|
|
|
struct evbuffer* buf;
|
|
|
|
|
|
|
|
tr_variantFromBenc(&top, benc_str, strlen(benc_str));
|
|
|
|
buf = tr_variantToBuf(&top, TR_VARIANT_FMT_JSON);
|
|
|
|
serialized = (char*)evbuffer_pullup(buf, -1);
|
|
|
|
stripWhitespace(serialized);
|
2012-12-14 04:34:42 +00:00
|
|
|
#if 0
|
2017-04-19 12:04:45 +00:00
|
|
|
fprintf(stderr, "benc: %s\n", benc_str);
|
|
|
|
fprintf(stderr, "json: %s\n", serialized);
|
|
|
|
fprintf(stderr, "want: %s\n", expected);
|
2012-12-14 04:34:42 +00:00
|
|
|
#endif
|
2017-05-29 19:48:02 +00:00
|
|
|
check_str(serialized, ==, expected);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantFree(&top);
|
|
|
|
evbuffer_free(buf);
|
|
|
|
return 0;
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static int testJSON(void)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int val;
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* benc_str;
|
|
|
|
char const* expected;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
benc_str = "i6e";
|
|
|
|
expected = "6";
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((val = testJSONSnippet(benc_str, expected)) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
benc_str = "d5:helloi1e5:worldi2ee";
|
|
|
|
expected = "{\"hello\":1,\"world\":2}";
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((val = testJSONSnippet(benc_str, expected)) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
benc_str = "d5:helloi1e5:worldi2e3:fooli1ei2ei3eee";
|
|
|
|
expected = "{\"foo\":[1,2,3],\"hello\":1,\"world\":2}";
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((val = testJSONSnippet(benc_str, expected)) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
benc_str = "d5:helloi1e5:worldi2e3:fooli1ei2ei3ed1:ai0eeee";
|
|
|
|
expected = "{\"foo\":[1,2,3,{\"a\":0}],\"hello\":1,\"world\":2}";
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((val = testJSONSnippet(benc_str, expected)) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
benc_str = "d4:argsd6:statusle7:status2lee6:result7:successe";
|
|
|
|
expected = "{\"args\":{\"status\":[],\"status2\":[]},\"result\":\"success\"}";
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((val = testJSONSnippet(benc_str, expected)) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static int testMerge(void)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
size_t len;
|
2017-05-01 15:46:41 +00:00
|
|
|
tr_variant dest;
|
|
|
|
tr_variant src;
|
2017-04-19 12:04:45 +00:00
|
|
|
int64_t i;
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* s;
|
|
|
|
tr_quark const i1 = tr_quark_new("i1", 2);
|
|
|
|
tr_quark const i2 = tr_quark_new("i2", 2);
|
|
|
|
tr_quark const i3 = tr_quark_new("i3", 2);
|
|
|
|
tr_quark const i4 = tr_quark_new("i4", 2);
|
|
|
|
tr_quark const s5 = tr_quark_new("s5", 2);
|
|
|
|
tr_quark const s6 = tr_quark_new("s6", 2);
|
|
|
|
tr_quark const s7 = tr_quark_new("s7", 2);
|
|
|
|
tr_quark const s8 = tr_quark_new("s8", 2);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* initial dictionary (default values) */
|
|
|
|
tr_variantInitDict(&dest, 10);
|
|
|
|
tr_variantDictAddInt(&dest, i1, 1);
|
|
|
|
tr_variantDictAddInt(&dest, i2, 2);
|
|
|
|
tr_variantDictAddInt(&dest, i4, -35); /* remains untouched */
|
|
|
|
tr_variantDictAddStr(&dest, s5, "abc");
|
|
|
|
tr_variantDictAddStr(&dest, s6, "def");
|
|
|
|
tr_variantDictAddStr(&dest, s7, "127.0.0.1"); /* remains untouched */
|
|
|
|
|
|
|
|
/* new dictionary, will overwrite items in dest */
|
|
|
|
tr_variantInitDict(&src, 10);
|
|
|
|
tr_variantDictAddInt(&src, i1, 1); /* same value */
|
|
|
|
tr_variantDictAddInt(&src, i2, 4); /* new value */
|
|
|
|
tr_variantDictAddInt(&src, i3, 3); /* new key:value */
|
|
|
|
tr_variantDictAddStr(&src, s5, "abc"); /* same value */
|
|
|
|
tr_variantDictAddStr(&src, s6, "xyz"); /* new value */
|
|
|
|
tr_variantDictAddStr(&src, s8, "ghi"); /* new key:value */
|
|
|
|
|
|
|
|
tr_variantMergeDicts(&dest, /*const*/ &src);
|
|
|
|
|
|
|
|
check(tr_variantDictFindInt(&dest, i1, &i));
|
2017-05-29 20:22:36 +00:00
|
|
|
check_int(i, ==, 1);
|
2017-04-19 12:04:45 +00:00
|
|
|
check(tr_variantDictFindInt(&dest, i2, &i));
|
2017-05-29 20:22:36 +00:00
|
|
|
check_int(i, ==, 4);
|
2017-04-19 12:04:45 +00:00
|
|
|
check(tr_variantDictFindInt(&dest, i3, &i));
|
2017-05-29 20:22:36 +00:00
|
|
|
check_int(i, ==, 3);
|
2017-04-19 12:04:45 +00:00
|
|
|
check(tr_variantDictFindInt(&dest, i4, &i));
|
2017-05-29 20:22:36 +00:00
|
|
|
check_int(i, ==, -35);
|
2017-04-19 12:04:45 +00:00
|
|
|
check(tr_variantDictFindStr(&dest, s5, &s, &len));
|
2017-05-29 20:39:51 +00:00
|
|
|
check_uint(len, ==, 3);
|
2017-05-29 19:48:02 +00:00
|
|
|
check_str(s, ==, "abc");
|
2017-04-19 12:04:45 +00:00
|
|
|
check(tr_variantDictFindStr(&dest, s6, &s, &len));
|
2017-05-29 20:39:51 +00:00
|
|
|
check_uint(len, ==, 3);
|
2017-05-29 19:48:02 +00:00
|
|
|
check_str(s, ==, "xyz");
|
2017-04-19 12:04:45 +00:00
|
|
|
check(tr_variantDictFindStr(&dest, s7, &s, &len));
|
2017-05-29 20:39:51 +00:00
|
|
|
check_uint(len, ==, 9);
|
2017-05-29 19:48:02 +00:00
|
|
|
check_str(s, ==, "127.0.0.1");
|
2017-04-19 12:04:45 +00:00
|
|
|
check(tr_variantDictFindStr(&dest, s8, &s, &len));
|
2017-05-29 20:39:51 +00:00
|
|
|
check_uint(len, ==, 3);
|
2017-05-29 19:48:02 +00:00
|
|
|
check_str(s, ==, "ghi");
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
tr_variantFree(&dest);
|
|
|
|
tr_variantFree(&src);
|
|
|
|
return 0;
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static int testStackSmash(void)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
size_t len;
|
|
|
|
int err;
|
|
|
|
char* in;
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* end;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant val;
|
|
|
|
char* saved;
|
2017-04-20 16:02:19 +00:00
|
|
|
int const depth = STACK_SMASH_DEPTH;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
in = tr_new(char, depth * 2 + 1);
|
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < depth; ++i)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
in[i] = 'l';
|
|
|
|
in[depth + i] = 'e';
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
in[depth * 2] = '\0';
|
|
|
|
err = tr_variantFromBencFull(&val, in, depth * 2, NULL, &end);
|
2017-05-29 20:22:36 +00:00
|
|
|
check_int(err, ==, 0);
|
2017-05-30 17:56:12 +00:00
|
|
|
check_ptr(end, ==, in + depth * 2);
|
2017-04-19 12:04:45 +00:00
|
|
|
saved = tr_variantToStr(&val, TR_VARIANT_FMT_BENC, &len);
|
2017-05-29 19:48:02 +00:00
|
|
|
check_str(saved, ==, in);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(in);
|
|
|
|
tr_free(saved);
|
|
|
|
tr_variantFree(&val);
|
|
|
|
|
|
|
|
return 0;
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static int testBool(void)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant top;
|
|
|
|
int64_t intVal;
|
|
|
|
bool boolVal;
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_quark const key1 = tr_quark_new("key1", 4);
|
|
|
|
tr_quark const key2 = tr_quark_new("key2", 4);
|
|
|
|
tr_quark const key3 = tr_quark_new("key3", 4);
|
|
|
|
tr_quark const key4 = tr_quark_new("key4", 4);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
tr_variantInitDict(&top, 0);
|
|
|
|
|
|
|
|
tr_variantDictAddBool(&top, key1, false);
|
|
|
|
tr_variantDictAddBool(&top, key2, 0);
|
|
|
|
tr_variantDictAddInt(&top, key3, true);
|
|
|
|
tr_variantDictAddInt(&top, key4, 1);
|
|
|
|
check(tr_variantDictFindBool(&top, key1, &boolVal));
|
|
|
|
check(!boolVal);
|
|
|
|
check(tr_variantDictFindBool(&top, key2, &boolVal));
|
|
|
|
check(!boolVal);
|
|
|
|
check(tr_variantDictFindBool(&top, key3, &boolVal));
|
|
|
|
check(boolVal);
|
|
|
|
check(tr_variantDictFindBool(&top, key4, &boolVal));
|
|
|
|
check(boolVal);
|
|
|
|
check(tr_variantDictFindInt(&top, key1, &intVal));
|
2017-05-30 17:56:12 +00:00
|
|
|
check_int(intVal, ==, 0);
|
2017-04-19 12:04:45 +00:00
|
|
|
check(tr_variantDictFindInt(&top, key2, &intVal));
|
2017-05-30 17:56:12 +00:00
|
|
|
check_int(intVal, ==, 0);
|
2017-04-19 12:04:45 +00:00
|
|
|
check(tr_variantDictFindInt(&top, key3, &intVal));
|
2017-05-30 17:56:12 +00:00
|
|
|
check_int(intVal, !=, 0);
|
2017-04-19 12:04:45 +00:00
|
|
|
check(tr_variantDictFindInt(&top, key4, &intVal));
|
2017-05-30 17:56:12 +00:00
|
|
|
check_int(intVal, !=, 0);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
tr_variantFree(&top);
|
|
|
|
return 0;
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static int testParse2(void)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant top;
|
|
|
|
tr_variant top2;
|
|
|
|
int64_t intVal;
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* strVal;
|
2017-04-19 12:04:45 +00:00
|
|
|
double realVal;
|
|
|
|
bool boolVal;
|
|
|
|
size_t len;
|
|
|
|
char* benc;
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* end;
|
2017-04-19 12:04:45 +00:00
|
|
|
size_t strLen;
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_quark const key_bool = tr_quark_new("this-is-a-bool", TR_BAD_SIZE);
|
|
|
|
tr_quark const key_real = tr_quark_new("this-is-a-real", TR_BAD_SIZE);
|
|
|
|
tr_quark const key_int = tr_quark_new("this-is-an-int", TR_BAD_SIZE);
|
|
|
|
tr_quark const key_str = tr_quark_new("this-is-a-string", TR_BAD_SIZE);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
tr_variantInitDict(&top, 0);
|
|
|
|
tr_variantDictAddBool(&top, key_bool, true);
|
|
|
|
tr_variantDictAddInt(&top, key_int, 1234);
|
|
|
|
tr_variantDictAddReal(&top, key_real, 0.5);
|
|
|
|
tr_variantDictAddStr(&top, key_str, "this-is-a-string");
|
|
|
|
|
|
|
|
benc = tr_variantToStr(&top, TR_VARIANT_FMT_BENC, &len);
|
2017-05-29 19:48:02 +00:00
|
|
|
check_str(benc, ==, "d14:this-is-a-booli1e14:this-is-a-real8:0.50000016:this-is-a-string16:this-is-a-string14:this-is-an-"
|
|
|
|
"inti1234ee");
|
2017-05-30 17:56:12 +00:00
|
|
|
check_int(tr_variantFromBencFull(&top2, benc, len, NULL, &end), ==, 0);
|
|
|
|
check_ptr(end, ==, benc + len);
|
2017-04-19 12:04:45 +00:00
|
|
|
check(tr_variantIsDict(&top2));
|
|
|
|
check(tr_variantDictFindInt(&top, key_int, &intVal));
|
2017-05-29 20:22:36 +00:00
|
|
|
check_int(intVal, ==, 1234);
|
2017-04-19 12:04:45 +00:00
|
|
|
check(tr_variantDictFindBool(&top, key_bool, &boolVal));
|
2017-05-30 17:56:12 +00:00
|
|
|
check(boolVal);
|
2017-04-19 12:04:45 +00:00
|
|
|
check(tr_variantDictFindStr(&top, key_str, &strVal, &strLen));
|
2017-05-29 20:39:51 +00:00
|
|
|
check_uint(strLen, ==, 16);
|
2017-05-29 19:48:02 +00:00
|
|
|
check_str(strVal, ==, "this-is-a-string");
|
2017-04-19 12:04:45 +00:00
|
|
|
check(tr_variantDictFindReal(&top, key_real, &realVal));
|
2017-05-29 20:22:36 +00:00
|
|
|
check_int((int)(realVal * 100), ==, 50);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
tr_variantFree(&top2);
|
|
|
|
tr_free(benc);
|
|
|
|
tr_variantFree(&top);
|
|
|
|
|
|
|
|
return 0;
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
int main(void)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
static testFunc const tests[] =
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
testInt,
|
|
|
|
testStr,
|
|
|
|
testParse,
|
|
|
|
testJSON,
|
|
|
|
testMerge,
|
|
|
|
testBool,
|
|
|
|
testParse2,
|
|
|
|
testStackSmash
|
|
|
|
};
|
|
|
|
|
|
|
|
return runTests(tests, NUM_TESTS(tests));
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|