2012-12-05 17:29:46 +00:00
|
|
|
|
#include <math.h> /* sqrt () */
|
|
|
|
|
#include <string.h> /* strlen () */
|
2009-06-15 00:11:06 +00:00
|
|
|
|
|
2008-05-20 17:33:54 +00:00
|
|
|
|
#include "transmission.h"
|
2009-06-15 00:11:06 +00:00
|
|
|
|
#include "bitfield.h"
|
2009-01-10 22:48:58 +00:00
|
|
|
|
#include "ConvertUTF.h" /* tr_utf8_validate*/
|
2008-10-30 19:47:00 +00:00
|
|
|
|
#include "platform.h"
|
2008-09-06 02:09:53 +00:00
|
|
|
|
#include "crypto.h"
|
2009-12-10 10:44:06 +00:00
|
|
|
|
#include "utils.h"
|
|
|
|
|
#include "web.h"
|
2008-05-20 17:33:54 +00:00
|
|
|
|
|
2009-11-20 04:38:19 +00:00
|
|
|
|
/* #define VERBOSE */
|
2009-01-23 18:44:15 +00:00
|
|
|
|
#undef VERBOSE
|
2008-06-02 15:52:16 +00:00
|
|
|
|
#define NUM_LOOPS 1
|
|
|
|
|
#define SPEED_TEST 0
|
|
|
|
|
|
|
|
|
|
#if SPEED_TEST
|
2009-01-23 18:44:15 +00:00
|
|
|
|
#define VERBOSE
|
2008-09-23 19:11:04 +00:00
|
|
|
|
#undef NUM_LOOPS
|
|
|
|
|
#define NUM_LOOPS 200
|
2008-06-02 15:52:16 +00:00
|
|
|
|
#endif
|
2008-05-20 17:33:54 +00:00
|
|
|
|
|
2012-08-18 16:07:05 +00:00
|
|
|
|
#include "libtransmission-test.h"
|
|
|
|
|
|
|
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
|
test_base64 (void)
|
2012-08-18 16:07:05 +00:00
|
|
|
|
{
|
|
|
|
|
char *in, *out;
|
|
|
|
|
int len;
|
|
|
|
|
|
|
|
|
|
/* base64 */
|
2012-12-05 17:29:46 +00:00
|
|
|
|
out = tr_base64_encode ("YOYO!", -1, &len);
|
2012-10-14 18:56:37 +00:00
|
|
|
|
check_streq ("WU9ZTyE=", out);
|
|
|
|
|
check_int_eq (8, len);
|
2012-12-05 17:29:46 +00:00
|
|
|
|
in = tr_base64_decode (out, -1, &len);
|
2012-10-14 18:56:37 +00:00
|
|
|
|
check_streq ("YOYO!", in);
|
|
|
|
|
check_int_eq (5, len);
|
2012-12-05 17:29:46 +00:00
|
|
|
|
tr_free (in);
|
|
|
|
|
tr_free (out);
|
|
|
|
|
out = tr_base64_encode (NULL, 0, &len);
|
|
|
|
|
check (out == NULL);
|
2012-10-14 18:56:37 +00:00
|
|
|
|
check_int_eq (0, len);
|
2012-08-18 16:07:05 +00:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2008-05-20 17:33:54 +00:00
|
|
|
|
|
2011-02-23 03:54:04 +00:00
|
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
|
test_bitfield_count_range (void)
|
2011-02-23 03:54:04 +00:00
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
int n;
|
|
|
|
|
int begin;
|
|
|
|
|
int end;
|
|
|
|
|
int count1;
|
|
|
|
|
int count2;
|
2012-12-05 17:29:46 +00:00
|
|
|
|
const int bitCount = 100 + tr_cryptoWeakRandInt (1000);
|
2011-03-28 16:31:05 +00:00
|
|
|
|
tr_bitfield bf;
|
2011-02-23 03:54:04 +00:00
|
|
|
|
|
|
|
|
|
/* generate a random bitfield */
|
2012-12-05 17:29:46 +00:00
|
|
|
|
tr_bitfieldConstruct (&bf, bitCount);
|
|
|
|
|
for (i=0, n=tr_cryptoWeakRandInt (bitCount); i<n; ++i)
|
|
|
|
|
tr_bitfieldAdd (&bf, tr_cryptoWeakRandInt (bitCount));
|
2011-02-23 03:54:04 +00:00
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
|
begin = tr_cryptoWeakRandInt (bitCount);
|
2011-02-23 03:54:04 +00:00
|
|
|
|
do {
|
2012-12-05 17:29:46 +00:00
|
|
|
|
end = tr_cryptoWeakRandInt (bitCount);
|
|
|
|
|
} while (end == begin);
|
|
|
|
|
if (end < begin) {
|
2011-02-23 03:54:04 +00:00
|
|
|
|
const int tmp = begin;
|
|
|
|
|
begin = end;
|
|
|
|
|
end = tmp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
count1 = 0;
|
2012-12-05 17:29:46 +00:00
|
|
|
|
for (i=begin; i<end; ++i)
|
|
|
|
|
if (tr_bitfieldHas (&bf, i))
|
2011-02-23 03:54:04 +00:00
|
|
|
|
++count1;
|
2012-12-05 17:29:46 +00:00
|
|
|
|
count2 = tr_bitfieldCountRange (&bf, begin, end);
|
|
|
|
|
check (count1 == count2);
|
2011-02-23 03:54:04 +00:00
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
|
tr_bitfieldDestruct (&bf);
|
2011-02-23 03:54:04 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2008-06-02 15:52:16 +00:00
|
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
|
test_bitfields (void)
|
2008-09-23 19:11:04 +00:00
|
|
|
|
{
|
|
|
|
|
unsigned int i;
|
2011-03-28 16:31:05 +00:00
|
|
|
|
unsigned int bitcount = 500;
|
|
|
|
|
tr_bitfield field;
|
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
|
tr_bitfieldConstruct (&field, bitcount);
|
2008-06-02 15:52:16 +00:00
|
|
|
|
|
2008-08-13 18:55:27 +00:00
|
|
|
|
/* test tr_bitfieldAdd */
|
2012-12-05 17:29:46 +00:00
|
|
|
|
for (i = 0; i < bitcount; ++i)
|
|
|
|
|
if (! (i % 7))
|
|
|
|
|
tr_bitfieldAdd (&field, i);
|
|
|
|
|
for (i = 0; i < bitcount; ++i)
|
|
|
|
|
check (tr_bitfieldHas (&field, i) == (! (i % 7)));
|
2008-06-02 15:52:16 +00:00
|
|
|
|
|
2008-08-13 18:55:27 +00:00
|
|
|
|
/* test tr_bitfieldAddRange */
|
2012-12-05 17:29:46 +00:00
|
|
|
|
tr_bitfieldAddRange (&field, 0, bitcount);
|
|
|
|
|
for (i = 0; i < bitcount; ++i)
|
|
|
|
|
check (tr_bitfieldHas (&field, i));
|
2008-08-13 18:55:27 +00:00
|
|
|
|
|
|
|
|
|
/* test tr_bitfieldRemRange in the middle of a boundary */
|
2012-12-05 17:29:46 +00:00
|
|
|
|
tr_bitfieldRemRange (&field, 4, 21);
|
|
|
|
|
for (i = 0; i < 64; ++i)
|
|
|
|
|
check (tr_bitfieldHas (&field, i) == ((i < 4) || (i >= 21)));
|
2008-08-13 18:55:27 +00:00
|
|
|
|
|
|
|
|
|
/* test tr_bitfieldRemRange on the boundaries */
|
2012-12-05 17:29:46 +00:00
|
|
|
|
tr_bitfieldAddRange (&field, 0, 64);
|
|
|
|
|
tr_bitfieldRemRange (&field, 8, 24);
|
|
|
|
|
for (i = 0; i < 64; ++i)
|
|
|
|
|
check (tr_bitfieldHas (&field, i) == ((i < 8) || (i >= 24)));
|
2008-08-13 18:55:27 +00:00
|
|
|
|
|
|
|
|
|
/* test tr_bitfieldRemRange when begin & end is on the same word */
|
2012-12-05 17:29:46 +00:00
|
|
|
|
tr_bitfieldAddRange (&field, 0, 64);
|
|
|
|
|
tr_bitfieldRemRange (&field, 4, 5);
|
|
|
|
|
for (i = 0; i < 64; ++i)
|
|
|
|
|
check (tr_bitfieldHas (&field, i) == ((i < 4) || (i >= 5)));
|
2008-06-07 14:41:31 +00:00
|
|
|
|
|
2008-08-14 14:38:13 +00:00
|
|
|
|
/* test tr_bitfieldAddRange */
|
2012-12-05 17:29:46 +00:00
|
|
|
|
tr_bitfieldRemRange (&field, 0, 64);
|
|
|
|
|
tr_bitfieldAddRange (&field, 4, 21);
|
|
|
|
|
for (i = 0; i < 64; ++i)
|
|
|
|
|
check (tr_bitfieldHas (&field, i) == ((4 <= i) && (i < 21)));
|
2008-08-14 14:38:13 +00:00
|
|
|
|
|
|
|
|
|
/* test tr_bitfieldAddRange on the boundaries */
|
2012-12-05 17:29:46 +00:00
|
|
|
|
tr_bitfieldRemRange (&field, 0, 64);
|
|
|
|
|
tr_bitfieldAddRange (&field, 8, 24);
|
|
|
|
|
for (i = 0; i < 64; ++i)
|
|
|
|
|
check (tr_bitfieldHas (&field, i) == ((8 <= i) && (i < 24)));
|
2008-08-14 14:38:13 +00:00
|
|
|
|
|
|
|
|
|
/* test tr_bitfieldAddRange when begin & end is on the same word */
|
2012-12-05 17:29:46 +00:00
|
|
|
|
tr_bitfieldRemRange (&field, 0, 64);
|
|
|
|
|
tr_bitfieldAddRange (&field, 4, 5);
|
|
|
|
|
for (i = 0; i < 64; ++i)
|
|
|
|
|
check (tr_bitfieldHas (&field, i) == ((4 <= i) && (i < 5)));
|
2008-08-14 14:38:13 +00:00
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
|
tr_bitfieldDestruct (&field);
|
2008-06-02 15:52:16 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-25 01:13:35 +00:00
|
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
|
test_strip_positional_args (void)
|
2010-06-25 01:13:35 +00:00
|
|
|
|
{
|
|
|
|
|
const char * in;
|
|
|
|
|
const char * out;
|
|
|
|
|
const char * expected;
|
|
|
|
|
|
|
|
|
|
in = "Hello %1$s foo %2$.*f";
|
|
|
|
|
expected = "Hello %s foo %.*f";
|
2012-12-05 17:29:46 +00:00
|
|
|
|
out = tr_strip_positional_args (in);
|
2012-10-14 18:56:37 +00:00
|
|
|
|
check_streq (expected, out);
|
2010-06-25 01:13:35 +00:00
|
|
|
|
|
|
|
|
|
in = "Hello %1$'d foo %2$'f";
|
|
|
|
|
expected = "Hello %d foo %f";
|
2012-12-05 17:29:46 +00:00
|
|
|
|
out = tr_strip_positional_args (in);
|
2012-10-14 18:56:37 +00:00
|
|
|
|
check_streq (expected, out);
|
2010-06-25 01:13:35 +00:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-05 19:11:30 +00:00
|
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
|
test_strstrip (void)
|
2008-09-05 19:11:30 +00:00
|
|
|
|
{
|
|
|
|
|
char *in, *out;
|
|
|
|
|
|
|
|
|
|
/* strstrip */
|
2012-12-05 17:29:46 +00:00
|
|
|
|
in = tr_strdup (" test ");
|
|
|
|
|
out = tr_strstrip (in);
|
|
|
|
|
check (in == out);
|
2012-10-14 18:56:37 +00:00
|
|
|
|
check_streq ("test", out);
|
2012-12-05 17:29:46 +00:00
|
|
|
|
tr_free (in);
|
2008-09-05 19:11:30 +00:00
|
|
|
|
|
|
|
|
|
/* strstrip */
|
2012-12-05 17:29:46 +00:00
|
|
|
|
in = tr_strdup (" test test ");
|
|
|
|
|
out = tr_strstrip (in);
|
|
|
|
|
check (in == out);
|
2012-10-14 18:56:37 +00:00
|
|
|
|
check_streq ("test test", out);
|
2012-12-05 17:29:46 +00:00
|
|
|
|
tr_free (in);
|
2008-09-05 19:11:30 +00:00
|
|
|
|
|
|
|
|
|
/* strstrip */
|
2012-12-05 17:29:46 +00:00
|
|
|
|
in = tr_strdup ("test");
|
|
|
|
|
out = tr_strstrip (in);
|
|
|
|
|
check (in == out);
|
2012-10-14 18:56:37 +00:00
|
|
|
|
check_streq ("test", out);
|
2012-12-05 17:29:46 +00:00
|
|
|
|
tr_free (in);
|
2008-09-05 19:11:30 +00:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2008-10-30 19:47:00 +00:00
|
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
|
test_buildpath (void)
|
2008-10-30 19:47:00 +00:00
|
|
|
|
{
|
|
|
|
|
char * out;
|
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
|
out = tr_buildPath ("foo", "bar", NULL);
|
2012-10-14 18:56:37 +00:00
|
|
|
|
check_streq ("foo" TR_PATH_DELIMITER_STR "bar", out);
|
2012-12-05 17:29:46 +00:00
|
|
|
|
tr_free (out);
|
2008-10-30 19:47:00 +00:00
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
|
out = tr_buildPath ("", "foo", "bar", NULL);
|
2012-10-14 18:56:37 +00:00
|
|
|
|
check_streq (TR_PATH_DELIMITER_STR "foo" TR_PATH_DELIMITER_STR "bar", out);
|
2012-12-05 17:29:46 +00:00
|
|
|
|
tr_free (out);
|
2008-10-30 19:47:00 +00:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-10 22:48:58 +00:00
|
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
|
test_utf8 (void)
|
2009-01-10 22:48:58 +00:00
|
|
|
|
{
|
|
|
|
|
const char * in;
|
|
|
|
|
char * out;
|
|
|
|
|
|
|
|
|
|
in = "hello world";
|
2012-12-05 17:29:46 +00:00
|
|
|
|
out = tr_utf8clean (in, -1);
|
2012-10-14 18:56:37 +00:00
|
|
|
|
check_streq (in, out);
|
2012-12-05 17:29:46 +00:00
|
|
|
|
tr_free (out);
|
2009-01-10 22:48:58 +00:00
|
|
|
|
|
|
|
|
|
in = "hello world";
|
2012-12-05 17:29:46 +00:00
|
|
|
|
out = tr_utf8clean (in, 5);
|
2012-10-14 18:56:37 +00:00
|
|
|
|
check_streq ("hello", out);
|
2012-12-05 17:29:46 +00:00
|
|
|
|
tr_free (out);
|
2009-01-10 22:48:58 +00:00
|
|
|
|
|
|
|
|
|
/* this version is not utf-8 */
|
|
|
|
|
in = "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>";
|
2012-12-05 17:29:46 +00:00
|
|
|
|
out = tr_utf8clean (in, 17);
|
|
|
|
|
check (out != NULL);
|
|
|
|
|
check ((strlen (out) == 17) || (strlen (out) == 32));
|
|
|
|
|
check (tr_utf8_validate (out, -1, NULL));
|
|
|
|
|
tr_free (out);
|
2009-01-10 22:48:58 +00:00
|
|
|
|
|
|
|
|
|
/* same string, but utf-8 clean */
|
|
|
|
|
in = "Òðóäíî áûòü Áîãîì";
|
2012-12-05 17:29:46 +00:00
|
|
|
|
out = tr_utf8clean (in, -1);
|
|
|
|
|
check (out != NULL);
|
|
|
|
|
check (tr_utf8_validate (out, -1, NULL));
|
2012-10-14 18:56:37 +00:00
|
|
|
|
check_streq (in, out);
|
2012-12-05 17:29:46 +00:00
|
|
|
|
tr_free (out);
|
2009-01-10 22:48:58 +00:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-09 17:25:48 +00:00
|
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
|
test_numbers (void)
|
2009-02-09 17:25:48 +00:00
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
int count;
|
|
|
|
|
int * numbers;
|
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
|
numbers = tr_parseNumberRange ("1-10,13,16-19", -1, &count);
|
|
|
|
|
check_int_eq (15, count);
|
|
|
|
|
check_int_eq (1, numbers[0]);
|
|
|
|
|
check_int_eq (6, numbers[5]);
|
|
|
|
|
check_int_eq (10, numbers[9]);
|
|
|
|
|
check_int_eq (13, numbers[10]);
|
|
|
|
|
check_int_eq (16, numbers[11]);
|
|
|
|
|
check_int_eq (19, numbers[14]);
|
|
|
|
|
tr_free (numbers);
|
|
|
|
|
|
|
|
|
|
numbers = tr_parseNumberRange ("1-5,3-7,2-6", -1, &count);
|
|
|
|
|
check (count == 7);
|
|
|
|
|
check (numbers != NULL);
|
|
|
|
|
for (i=0; i<count; ++i)
|
2012-10-14 18:56:37 +00:00
|
|
|
|
check_int_eq (i+1, numbers[i]);
|
2012-12-05 17:29:46 +00:00
|
|
|
|
tr_free (numbers);
|
2009-02-09 17:25:48 +00:00
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
|
numbers = tr_parseNumberRange ("1-Hello", -1, &count);
|
2012-10-14 18:56:37 +00:00
|
|
|
|
check_int_eq (0, count);
|
2012-12-05 17:29:46 +00:00
|
|
|
|
check (numbers == NULL);
|
2009-02-09 17:25:48 +00:00
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
|
numbers = tr_parseNumberRange ("1-", -1, &count);
|
2012-10-14 18:56:37 +00:00
|
|
|
|
check_int_eq (0, count);
|
2012-12-05 17:29:46 +00:00
|
|
|
|
check (numbers == NULL);
|
2009-02-09 17:25:48 +00:00
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
|
numbers = tr_parseNumberRange ("Hello", -1, &count);
|
2012-10-14 18:56:37 +00:00
|
|
|
|
check_int_eq (0, count);
|
2012-12-05 17:29:46 +00:00
|
|
|
|
check (numbers == NULL);
|
2009-02-09 17:25:48 +00:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2009-10-31 22:16:06 +00:00
|
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
|
compareInts (const void * va, const void * vb)
|
2009-10-31 22:16:06 +00:00
|
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
|
const int a = * (const int *)va;
|
|
|
|
|
const int b = * (const int*)vb;
|
2009-10-31 22:16:06 +00:00
|
|
|
|
return a - b;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
|
test_lowerbound (void)
|
2009-10-31 22:16:06 +00:00
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
const int A[] = { 1, 2, 3, 3, 3, 5, 8 };
|
|
|
|
|
const int expected_pos[] = { 0, 1, 2, 5, 5, 6, 6, 6, 7, 7 };
|
2011-03-22 15:19:54 +00:00
|
|
|
|
const int expected_exact[] = { true, true, true, false, true, false, false, true, false, false };
|
2012-12-05 17:29:46 +00:00
|
|
|
|
const int N = sizeof (A) / sizeof (A[0]);
|
2009-10-31 22:16:06 +00:00
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
|
for (i=1; i<=10; ++i)
|
2009-10-31 22:16:06 +00:00
|
|
|
|
{
|
2011-03-22 15:19:54 +00:00
|
|
|
|
bool exact;
|
2012-12-05 17:29:46 +00:00
|
|
|
|
const int pos = tr_lowerBound (&i, A, N, sizeof (int), compareInts, &exact);
|
2009-10-31 22:16:06 +00:00
|
|
|
|
|
|
|
|
|
#if 0
|
2012-12-05 17:29:46 +00:00
|
|
|
|
fprintf (stderr, "searching for %d. ", i);
|
|
|
|
|
fprintf (stderr, "result: index = %d, ", pos);
|
|
|
|
|
if (pos != N)
|
|
|
|
|
fprintf (stderr, "A[%d] == %d\n", pos, A[pos]);
|
2009-10-31 22:16:06 +00:00
|
|
|
|
else
|
2012-12-05 17:29:46 +00:00
|
|
|
|
fprintf (stderr, "which is off the end.\n");
|
2009-10-31 22:16:06 +00:00
|
|
|
|
#endif
|
2012-12-05 17:29:46 +00:00
|
|
|
|
check_int_eq (expected_pos[i-1], pos);
|
|
|
|
|
check_int_eq (expected_exact[i-1], exact);
|
2009-10-31 22:16:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2009-05-22 05:35:51 +00:00
|
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
|
test_memmem (void)
|
2009-05-22 05:35:51 +00:00
|
|
|
|
{
|
|
|
|
|
char const haystack[12] = "abcabcabcabc";
|
|
|
|
|
char const needle[3] = "cab";
|
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
|
check (tr_memmem (haystack, sizeof haystack, haystack, sizeof haystack) == haystack);
|
|
|
|
|
check (tr_memmem (haystack, sizeof haystack, needle, sizeof needle) == haystack + 2);
|
|
|
|
|
check (tr_memmem (needle, sizeof needle, haystack, sizeof haystack) == NULL);
|
2009-05-22 05:35:51 +00:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-20 04:38:19 +00:00
|
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
|
test_hex (void)
|
2009-11-20 04:38:19 +00:00
|
|
|
|
{
|
|
|
|
|
char hex1[41];
|
|
|
|
|
char hex2[41];
|
|
|
|
|
uint8_t sha1[20];
|
|
|
|
|
/*uint8_t sha2[20];*/
|
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
|
memcpy (hex1, "fb5ef5507427b17e04b69cef31fa3379b456735a", 41);
|
|
|
|
|
tr_hex_to_sha1 (sha1, hex1);
|
|
|
|
|
tr_sha1_to_hex (hex2, sha1);
|
2012-10-14 18:56:37 +00:00
|
|
|
|
check_streq (hex1, hex2);
|
2009-11-20 04:38:19 +00:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-24 02:16:31 +00:00
|
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
|
test_array (void)
|
2009-11-24 02:16:31 +00:00
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
int array[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
2012-12-05 17:29:46 +00:00
|
|
|
|
int n = sizeof (array) / sizeof (array[0]);
|
2009-11-24 02:16:31 +00:00
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
|
tr_removeElementFromArray (array, 5u, sizeof (int), n--);
|
|
|
|
|
for (i=0; i<n; ++i)
|
|
|
|
|
check_int_eq ((i<5 ? i : i+1), array[i]);
|
2009-11-24 02:16:31 +00:00
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
|
tr_removeElementFromArray (array, 0u, sizeof (int), n--);
|
|
|
|
|
for (i=0; i<n; ++i)
|
|
|
|
|
check_int_eq ((i<4 ? i+1 : i+2), array[i]);
|
2009-11-24 02:16:31 +00:00
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
|
tr_removeElementFromArray (array, n-1, sizeof (int), n); n--;
|
|
|
|
|
for (i=0; i<n; ++i)
|
|
|
|
|
check_int_eq ((i<4 ? i+1 : i+2), array[i]);
|
2009-11-24 02:16:31 +00:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-10 09:13:08 +00:00
|
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
|
test_url (void)
|
2009-12-10 09:13:08 +00:00
|
|
|
|
{
|
|
|
|
|
int port;
|
2010-02-20 15:57:05 +00:00
|
|
|
|
char * scheme;
|
2009-12-10 09:13:08 +00:00
|
|
|
|
char * host;
|
|
|
|
|
char * path;
|
2009-12-10 10:44:06 +00:00
|
|
|
|
char * str;
|
2009-12-10 09:13:08 +00:00
|
|
|
|
const char * url;
|
|
|
|
|
|
2011-04-05 22:21:18 +00:00
|
|
|
|
url = "http://1";
|
2012-12-05 17:29:46 +00:00
|
|
|
|
check (!tr_urlParse (url, -1, &scheme, &host, &port, &path));
|
2012-10-14 18:56:37 +00:00
|
|
|
|
check_streq ("http", scheme);
|
|
|
|
|
check_streq ("1", host);
|
|
|
|
|
check_streq ("/", path);
|
|
|
|
|
check_int_eq (80, port);
|
2012-12-05 17:29:46 +00:00
|
|
|
|
tr_free (scheme);
|
|
|
|
|
tr_free (path);
|
|
|
|
|
tr_free (host);
|
2011-04-05 22:21:18 +00:00
|
|
|
|
|
2009-12-10 09:13:08 +00:00
|
|
|
|
url = "http://www.some-tracker.org/some/path";
|
2012-12-05 17:29:46 +00:00
|
|
|
|
check (!tr_urlParse (url, -1, &scheme, &host, &port, &path));
|
2012-10-14 18:56:37 +00:00
|
|
|
|
check_streq ("http", scheme);
|
|
|
|
|
check_streq ("www.some-tracker.org", host);
|
|
|
|
|
check_streq ("/some/path", path);
|
|
|
|
|
check_int_eq (80, port);
|
2012-12-05 17:29:46 +00:00
|
|
|
|
tr_free (scheme);
|
|
|
|
|
tr_free (path);
|
|
|
|
|
tr_free (host);
|
2009-12-10 09:13:08 +00:00
|
|
|
|
|
|
|
|
|
url = "http://www.some-tracker.org:80/some/path";
|
2012-12-05 17:29:46 +00:00
|
|
|
|
check (!tr_urlParse (url, -1, &scheme, &host, &port, &path));
|
2012-10-14 18:56:37 +00:00
|
|
|
|
check_streq ("http", scheme);
|
|
|
|
|
check_streq ("www.some-tracker.org", host);
|
|
|
|
|
check_streq ("/some/path", path);
|
|
|
|
|
check_int_eq (80, port);
|
2012-12-05 17:29:46 +00:00
|
|
|
|
tr_free (scheme);
|
|
|
|
|
tr_free (path);
|
|
|
|
|
tr_free (host);
|
2009-12-10 09:13:08 +00:00
|
|
|
|
|
2009-12-10 10:44:06 +00:00
|
|
|
|
url = "http%3A%2F%2Fwww.example.com%2F~user%2F%3Ftest%3D1%26test1%3D2";
|
2012-12-05 17:29:46 +00:00
|
|
|
|
str = tr_http_unescape (url, strlen (url));
|
2012-10-14 18:56:37 +00:00
|
|
|
|
check_streq ("http://www.example.com/~user/?test=1&test1=2", str);
|
2012-12-05 17:29:46 +00:00
|
|
|
|
tr_free (str);
|
2009-12-10 09:13:08 +00:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-24 17:09:39 +00:00
|
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
|
test_truncd (void)
|
2008-05-20 17:33:54 +00:00
|
|
|
|
{
|
2009-07-14 20:35:48 +00:00
|
|
|
|
char buf[32];
|
2012-12-05 17:29:46 +00:00
|
|
|
|
const double nan = sqrt (-1);
|
2008-05-20 17:33:54 +00:00
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
|
tr_snprintf (buf, sizeof (buf), "%.2f%%", 99.999);
|
|
|
|
|
check_streq ("100.00%", buf);
|
2010-07-24 17:09:39 +00:00
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
|
tr_snprintf (buf, sizeof (buf), "%.2f%%", tr_truncd (99.999, 2));
|
|
|
|
|
check_streq ("99.99%", buf);
|
2009-07-14 20:35:48 +00:00
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
|
tr_snprintf (buf, sizeof (buf), "%.4f", tr_truncd (403650.656250, 4));
|
|
|
|
|
check_streq ("403650.6562", buf);
|
2010-07-24 17:09:39 +00:00
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
|
tr_snprintf (buf, sizeof (buf), "%.2f", tr_truncd (2.15, 2));
|
|
|
|
|
check_streq ("2.15", buf);
|
2010-09-22 16:09:36 +00:00
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
|
tr_snprintf (buf, sizeof (buf), "%.2f", tr_truncd (2.05, 2));
|
|
|
|
|
check_streq ("2.05", buf);
|
2010-10-17 18:29:36 +00:00
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
|
tr_snprintf (buf, sizeof (buf), "%.2f", tr_truncd (3.3333, 2));
|
|
|
|
|
check_streq ("3.33", buf);
|
2010-10-21 23:47:23 +00:00
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
|
tr_snprintf (buf, sizeof (buf), "%.0f", tr_truncd (3.3333, 0));
|
|
|
|
|
check_streq ("3", buf);
|
2010-10-21 23:47:23 +00:00
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
|
tr_snprintf (buf, sizeof (buf), "%.2f", tr_truncd (nan, 2));
|
|
|
|
|
check (strstr (buf, "nan") != NULL);
|
2010-10-21 23:47:23 +00:00
|
|
|
|
|
2010-07-24 17:09:39 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-18 16:07:05 +00:00
|
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
|
test_cryptoRand (void)
|
2012-08-18 16:07:05 +00:00
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
|
/* test that tr_cryptoRandInt () stays in-bounds */
|
|
|
|
|
for (i = 0; i < 100000; ++i)
|
2012-08-18 16:07:05 +00:00
|
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
|
const int val = tr_cryptoRandInt (100);
|
|
|
|
|
check (val >= 0);
|
|
|
|
|
check (val < 100);
|
2012-08-18 16:07:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-23 03:54:04 +00:00
|
|
|
|
struct blah
|
|
|
|
|
{
|
|
|
|
|
uint8_t hash[SHA_DIGEST_LENGTH]; /* pieces hash */
|
|
|
|
|
int8_t priority; /* TR_PRI_HIGH, _NORMAL, or _LOW */
|
|
|
|
|
int8_t dnd; /* "do not download" flag */
|
|
|
|
|
time_t timeChecked; /* the last time we tested this piece */
|
|
|
|
|
};
|
|
|
|
|
|
2010-07-24 17:09:39 +00:00
|
|
|
|
int
|
2012-12-05 17:29:46 +00:00
|
|
|
|
main (void)
|
2010-07-24 17:09:39 +00:00
|
|
|
|
{
|
2012-08-18 16:07:05 +00:00
|
|
|
|
const testFunc tests[] = {
|
|
|
|
|
test_base64, test_hex, test_lowerbound, test_strip_positional_args,
|
|
|
|
|
test_strstrip, test_buildpath, test_utf8, test_numbers, test_memmem,
|
|
|
|
|
test_array, test_url, test_truncd, test_cryptoRand,
|
|
|
|
|
};
|
|
|
|
|
int ret;
|
2010-07-24 17:09:39 +00:00
|
|
|
|
int l;
|
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
|
if ((ret = runTests (tests, NUM_TESTS (tests))))
|
2012-08-18 16:07:05 +00:00
|
|
|
|
return ret;
|
2008-09-06 02:04:37 +00:00
|
|
|
|
|
2008-06-02 15:52:16 +00:00
|
|
|
|
/* simple bitfield tests */
|
2012-12-05 17:29:46 +00:00
|
|
|
|
for (l = 0; l < NUM_LOOPS; ++l)
|
|
|
|
|
if ((ret = test_bitfields ()))
|
2012-08-18 16:07:05 +00:00
|
|
|
|
return ret;
|
2008-06-02 15:52:16 +00:00
|
|
|
|
|
2011-02-23 03:54:04 +00:00
|
|
|
|
/* bitfield count range */
|
2012-12-05 17:29:46 +00:00
|
|
|
|
for (l=0; l<10000; ++l)
|
|
|
|
|
if ((ret = test_bitfield_count_range ()))
|
2012-08-18 16:07:05 +00:00
|
|
|
|
return ret;
|
2011-02-23 03:54:04 +00:00
|
|
|
|
|
2008-05-20 17:33:54 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
|