Exemplo n.º 1
0
static int
test1 (void)
{
    tr_info inf;
    tr_ctor * ctor;
    const char * magnet_link;
    tr_parse_result parse_result;

    /* background info @ http://wiki.theory.org/BitTorrent_Magnet-URI_Webseeding */
    magnet_link = "magnet:?"
                  "xt=urn:btih:14FFE5DD23188FD5CB53A1D47F1289DB70ABF31E"
                  "&dn=ubuntu+12+04+1+desktop+32+bit"
                  "&tr=http%3A%2F%2Ftracker.publicbt.com%2Fannounce"
                  "&tr=udp%3A%2F%2Ftracker.publicbt.com%3A80"
                  "&ws=http://transmissionbt.com ";
    ctor = tr_ctorNew (NULL);
    tr_ctorSetMetainfoFromMagnetLink (ctor, magnet_link);
    parse_result = tr_torrentParse (ctor, &inf);
    check_int_eq (inf.fileCount, 0); /* cos it's a magnet link */
    check_int_eq (parse_result, TR_PARSE_OK);
    check_int_eq (inf.trackerCount, 2);
    check_streq ("http://tracker.publicbt.com/announce", inf.trackers[0].announce);
    check_streq ("udp://tracker.publicbt.com:80", inf.trackers[1].announce);
    check_int_eq (inf.webseedCount, 1);
    check_streq ("http://transmissionbt.com", inf.webseeds[0]);

    /* cleanup */
    tr_metainfoFree (&inf);
    tr_ctorFree (ctor);
    return 0;
}
Exemplo n.º 2
0
static int
test_error_set (void)
{
  tr_error * err = NULL;

  tr_error_prefix (&err, "error: ");
  check (err == NULL);

  tr_error_set (&err, 1, "error: %s (%d)", "oops", 2);
  check (err != NULL);
  check_int_eq (1, err->code);
  check_streq ("error: oops (2)", err->message);
  tr_error_clear (&err);
  check (err == NULL);

  tr_error_set_literal (&err, 2, "oops");
  check (err != NULL);
  check_int_eq (2, err->code);
  check_streq ("oops", err->message);

  tr_error_prefix (&err, "error: ");
  check (err != NULL);
  check_int_eq (2, err->code);
  check_streq ("error: oops", err->message);

  tr_error_free (err);

  return 0;
}
Exemplo n.º 3
0
static int
test_strstrip (void)
{
  char *in, *out;

  /* strstrip */
  in = tr_strdup ("   test    ");
  out = tr_strstrip (in);
  check (in == out);
  check_streq ("test", out);
  tr_free (in);

  /* strstrip */
  in = tr_strdup (" test test ");
  out = tr_strstrip (in);
  check (in == out);
  check_streq ("test test", out);
  tr_free (in);

  /* strstrip */
  in = tr_strdup ("test");
  out = tr_strstrip (in);
  check (in == out);
  check_streq ("test", out);
  tr_free (in);

  return 0;
}
Exemplo n.º 4
0
static int
test_utf8( void )
{
    const char * in;
    char * out;

    in = "hello world";
    out = tr_utf8clean( in, -1 );
    check_streq (in, out);
    tr_free( out );

    in = "hello world";
    out = tr_utf8clean( in, 5 );
    check_streq ("hello", out);
    tr_free( out );

    /* this version is not utf-8 */
    in = "Трудно быть Богом";
    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 );

    /* same string, but utf-8 clean */
    in = "Òðóäíî áûòü Áîãîì";
    out = tr_utf8clean( in, -1 );
    check( out != NULL );
    check( tr_utf8_validate( out, -1, NULL ) );
    check_streq (in, out);
    tr_free( out );

    return 0;
}
Exemplo n.º 5
0
static int
test_error_propagate (void)
{
  tr_error * err = NULL;
  tr_error * err2 = NULL;

  tr_error_set_literal (&err, 1, "oops");
  check (err != NULL);
  check_int_eq (1, err->code);
  check_streq ("oops", err->message);

  tr_error_propagate (&err2, &err);
  check (err2 != NULL);
  check_int_eq (1, err2->code);
  check_streq ("oops", err2->message);
  check (err == NULL);

  tr_error_propagate_prefixed (&err, &err2, "error: ");
  check (err != NULL);
  check_int_eq (1, err->code);
  check_streq ("error: oops", err->message);
  check (err2 == NULL);

  tr_error_propagate (NULL, &err);
  check (err == NULL);

  tr_error_free (err2);

  return 0;
}
Exemplo n.º 6
0
static int
test_utf8 (void)
{
    const char      * in = "{ \"key\": \"Letöltések\" }";
    tr_variant           top;
    const char      * str;
    char            * json;
    int               err;
    const tr_quark key = tr_quark_new ("key", 3);

    err = tr_variantFromJson (&top, in, strlen(in));
    check (!err);
    check (tr_variantIsDict (&top));
    check (tr_variantDictFindStr (&top, key, &str, NULL));
    check_streq ("Letöltések", str);
    if (!err)
        tr_variantFree (&top);

    in = "{ \"key\": \"\\u005C\" }";
    err = tr_variantFromJson (&top, in, strlen(in));
    check (!err);
    check (tr_variantIsDict (&top));
    check (tr_variantDictFindStr (&top, key, &str, NULL));
    check_streq ("\\", str);
    if (!err)
        tr_variantFree (&top);

    /**
     * 1. Feed it JSON-escaped nonascii to the JSON decoder.
     * 2. Confirm that the result is UTF-8.
     * 3. Feed the same UTF-8 back into the JSON encoder.
     * 4. Confirm that the result is JSON-escaped.
     * 5. Dogfood that result back into the parser.
     * 6. Confirm that the result is UTF-8.
     */
    in = "{ \"key\": \"Let\\u00f6lt\\u00e9sek\" }";
    err = tr_variantFromJson (&top, in, strlen(in));
    check (!err);
    check (tr_variantIsDict (&top));
    check (tr_variantDictFindStr (&top, key, &str, NULL));
    check_streq ("Letöltések", str);
    json = tr_variantToStr (&top, TR_VARIANT_FMT_JSON, NULL);
    if (!err)
        tr_variantFree (&top);
    check (json);
    check (strstr (json, "\\u00f6") != NULL);
    check (strstr (json, "\\u00e9") != NULL);
    err = tr_variantFromJson (&top, json, strlen(json));
    check (!err);
    check (tr_variantIsDict (&top));
    check (tr_variantDictFindStr (&top, key, &str, NULL));
    check_streq ("Letöltések", str);
    if (!err)
        tr_variantFree (&top);
    tr_free (json);

    return 0;
}
Exemplo n.º 7
0
static int
test_buildpath (void)
{
  char * out;

  out = tr_buildPath ("foo", "bar", NULL);
  check_streq ("foo" TR_PATH_DELIMITER_STR "bar", out);
  tr_free (out);

  out = tr_buildPath ("", "foo", "bar", NULL);
  check_streq (TR_PATH_DELIMITER_STR "foo" TR_PATH_DELIMITER_STR "bar", out);
  tr_free (out);

  return 0;
}
Exemplo n.º 8
0
static int
testMerge( void )
{
    tr_benc dest, src;
    int64_t i;
    const char * s;

    /* initial dictionary (default values)  */
    tr_bencInitDict( &dest, 10 );
    tr_bencDictAddInt( &dest, "i1", 1 );
    tr_bencDictAddInt( &dest, "i2", 2 );
    tr_bencDictAddInt( &dest, "i4", -35 ); /* remains untouched */
    tr_bencDictAddStr( &dest, "s5", "abc" );
    tr_bencDictAddStr( &dest, "s6", "def" );
    tr_bencDictAddStr( &dest, "s7", "127.0.0.1" ); /* remains untouched */

    /* new dictionary, will overwrite items in dest  */
    tr_bencInitDict( &src, 10 );
    tr_bencDictAddInt( &src, "i1", 1 );     /* same value */
    tr_bencDictAddInt( &src, "i2", 4 );     /* new value */
    tr_bencDictAddInt( &src, "i3", 3 );     /* new key:value */
    tr_bencDictAddStr( &src, "s5", "abc" ); /* same value */
    tr_bencDictAddStr( &src, "s6", "xyz" ); /* new value */
    tr_bencDictAddStr( &src, "s8", "ghi" ); /* new key:value */

    tr_bencMergeDicts( &dest, /*const*/ &src );

    check( tr_bencDictFindInt( &dest, "i1", &i ));
    check_int_eq (1, i);
    check( tr_bencDictFindInt( &dest, "i2", &i ));
    check_int_eq (4, i);
    check( tr_bencDictFindInt( &dest, "i3", &i ));
    check_int_eq (3, i);
    check( tr_bencDictFindInt( &dest, "i4", &i ));
    check_int_eq (-35, i);
    check( tr_bencDictFindStr( &dest, "s5", &s ));
    check_streq ("abc", s);
    check( tr_bencDictFindStr( &dest, "s6", &s ));
    check_streq ("xyz", s);
    check( tr_bencDictFindStr( &dest, "s7", &s ));
    check_streq ("127.0.0.1", s);
    check( tr_bencDictFindStr( &dest, "s8", &s ));
    check_streq ("ghi", s);

    tr_bencFree( &dest );
    tr_bencFree( &src );
    return 0;
}
Exemplo n.º 9
0
static int
test_utf8 (void)
{
  const char * in;
  char * out;

  in = "hello world";
  out = tr_utf8clean (in, TR_BAD_SIZE);
  check_streq (in, out);
  tr_free (out);

  in = "hello world";
  out = tr_utf8clean (in, 5);
  check_streq ("hello", out);
  tr_free (out);

  /* this version is not utf-8 (but cp866) */
  in = "\x92\xE0\xE3\xA4\xAD\xAE \xA1\xEB\xE2\xEC \x81\xAE\xA3\xAE\xAC";
  out = tr_utf8clean (in, 17);
  check (out != NULL);
  check ((strlen (out) == 17) || (strlen (out) == 33));
  check (tr_utf8_validate (out, TR_BAD_SIZE, NULL));
  tr_free (out);

  /* same string, but utf-8 clean */
  in = "Трудно быть Богом";
  out = tr_utf8clean (in, TR_BAD_SIZE);
  check (out != NULL);
  check (tr_utf8_validate (out, TR_BAD_SIZE, NULL));
  check_streq (in, out);
  tr_free (out);

  in = "\xF4\x00\x81\x82";
  out = tr_utf8clean (in, 4);
  check (out != NULL);
  check ((strlen (out) == 1) || (strlen (out) == 2));
  check (tr_utf8_validate (out, TR_BAD_SIZE, NULL));
  tr_free (out);

  in = "\xF4\x33\x81\x82";
  out = tr_utf8clean (in, 4);
  check (out != NULL);
  check ((strlen (out) == 4) || (strlen (out) == 7));
  check (tr_utf8_validate (out, TR_BAD_SIZE, NULL));
  tr_free (out);

  return 0;
}
Exemplo n.º 10
0
static int
test_elements (void)
{
    const char * in;
    tr_variant top;
    const char * str;
    bool f;
    double d;
    int64_t i;
    int err = 0;
    tr_quark key;

    in = "{ \"string\": \"hello world\","
         "  \"escaped\": \"bell \\b formfeed \\f linefeed \\n carriage return \\r tab \\t\","
         "  \"int\": 5, "
         "  \"float\": 6.5, "
         "  \"true\": true, "
         "  \"false\": false, "
         "  \"null\": null }";

    err = tr_variantFromJson (&top, in, strlen(in));
    check_int_eq (0, err);
    check (tr_variantIsDict (&top));
    str = NULL;
    key = tr_quark_new ("string", 6);
    check (tr_variantDictFindStr (&top, key, &str, NULL));
    check_streq ("hello world", str);
    check (tr_variantDictFindStr (&top, tr_quark_new("escaped",7), &str, NULL));
    check_streq ("bell \b formfeed \f linefeed \n carriage return \r tab \t", str);
    i = 0;
    check (tr_variantDictFindInt (&top, tr_quark_new("int",3), &i));
    check_int_eq (5, i);
    d = 0;
    check (tr_variantDictFindReal (&top, tr_quark_new("float",5), &d));
    check_int_eq (65, ((int)(d*10)));
    f = false;
    check (tr_variantDictFindBool (&top, tr_quark_new("true",4), &f));
    check_int_eq (true, f);
    check (tr_variantDictFindBool (&top, tr_quark_new("false",5), &f));
    check_int_eq (false, f);
    check (tr_variantDictFindStr (&top, tr_quark_new("null",4), &str, NULL));
    check_streq ("", str);

    if (!err)
        tr_variantFree (&top);

    return 0;
}
Exemplo n.º 11
0
static int
test_path_xname (const struct xname_test_data * data,
                 size_t                         data_size,
                 char *                      (* func) (const char *, tr_error **))
{
  for (size_t i = 0; i < data_size; ++i)
    {
      tr_error * err = NULL;
      char * name = func (data[i].input, &err);

      if (data[i].output != NULL)
        {
          check (name != NULL);
          check (err == NULL);
          check_streq (data[i].output, name);
          tr_free (name);
        }
      else
        {
          check (name == NULL);
          check (err != NULL);
          tr_error_clear (&err);
        }
    }

  return 0;
}
Exemplo n.º 12
0
static int
test_static_quarks (void)
{
  int i;

  for (i=0; i<TR_N_KEYS; i++)
    {
      tr_quark q;
      size_t len;
      const char * str;

      str = tr_quark_get_string ((tr_quark)i, &len);
      check_uint_eq (strlen(str), len);
      check (tr_quark_lookup (str, len, &q));
      check_int_eq (i, (int)q);
    }

  for (i=0; i+1<TR_N_KEYS; i++)
    {
      size_t len1, len2;
      const char *str1, *str2;

      str1 = tr_quark_get_string ((tr_quark)i, &len1);
      str2 = tr_quark_get_string ((tr_quark)(i+1), &len2);

      check (strcmp (str1, str2) < 0);
    }

  const tr_quark q = tr_quark_new (NULL, TR_BAD_SIZE);
  check_int_eq (TR_KEY_NONE, (int)q);
  check_streq ("", tr_quark_get_string (q, NULL));

  return 0;
}
Exemplo n.º 13
0
static int
testStackSmash( void )
{
    int             i;
    int             len;
    int             err;
    uint8_t *       in;
    const uint8_t * end;
    tr_benc         val;
    char *          saved;
    const int       depth = STACK_SMASH_DEPTH;

    in = tr_new( uint8_t, depth * 2 + 1 );
    for( i = 0; i < depth; ++i )
    {
        in[i] = 'l';
        in[depth + i] = 'e';
    }
    in[depth * 2] = '\0';
    err = tr_bencParse( in, in + ( depth * 2 ), &val, &end );
    check( !err );
    check( end == in + ( depth * 2 ) );
    saved = tr_bencToStr( &val, TR_FMT_BENC, &len );
    check_streq ((char*)in, saved);
    tr_free( in );
    tr_free( saved );
    tr_bencFree( &val );

    return 0;
}
Exemplo n.º 14
0
static int
testString( const char * str,
            int          isGood )
{
    tr_benc         val;
    const uint8_t * end = NULL;
    char *          saved;
    const size_t    len = strlen( str );
    int             savedLen;
    int             err = tr_bencParse( str, str + len, &val, &end );

    if( !isGood )
    {
        check( err );
    }
    else
    {
        check( !err );
#if 0
        fprintf( stderr, "in: [%s]\n", str );
        fprintf( stderr, "out:\n%s", tr_bencToStr( &val, TR_FMT_JSON, NULL ) );
#endif
        check( end == (const uint8_t*)str + len );
        saved = tr_bencToStr( &val, TR_FMT_BENC, &savedLen );
        check_streq (str, saved);
        check_int_eq (savedLen, len);
        tr_free( saved );
        tr_bencFree( &val );
    }
    return 0;
}
Exemplo n.º 15
0
static int
test_url (void)
{
  int port;
  char * scheme;
  char * host;
  char * path;
  char * str;
  const char * url;

  url = "http://1";
  check (tr_urlParse (url, TR_BAD_SIZE, &scheme, &host, &port, &path));
  check_streq ("http", scheme);
  check_streq ("1", host);
  check_streq ("/", path);
  check_int_eq (80, port);
  tr_free (scheme);
  tr_free (path);
  tr_free (host);

  url = "http://www.some-tracker.org/some/path";
  check (tr_urlParse (url, TR_BAD_SIZE, &scheme, &host, &port, &path));
  check_streq ("http", scheme);
  check_streq ("www.some-tracker.org", host);
  check_streq ("/some/path", path);
  check_int_eq (80, port);
  tr_free (scheme);
  tr_free (path);
  tr_free (host);

  url = "http://www.some-tracker.org:80/some/path";
  check (tr_urlParse (url, TR_BAD_SIZE, &scheme, &host, &port, &path));
  check_streq ("http", scheme);
  check_streq ("www.some-tracker.org", host);
  check_streq ("/some/path", path);
  check_int_eq (80, port);
  tr_free (scheme);
  tr_free (path);
  tr_free (host);

  url = "http%3A%2F%2Fwww.example.com%2F~user%2F%3Ftest%3D1%26test1%3D2";
  str = tr_http_unescape (url, strlen (url));
  check_streq ("http://www.example.com/~user/?test=1&test1=2", str);
  tr_free (str);

  return 0;
}
Exemplo n.º 16
0
static int
test1 (void)
{
    const char * in =
        "{\n"
        "    \"headers\": {\n"
        "        \"type\": \"request\",\n"
        "        \"tag\": 666\n"
        "    },\n"
        "    \"body\": {\n"
        "        \"name\": \"torrent-info\",\n"
        "        \"arguments\": {\n"
        "            \"ids\": [ 7, 10 ]\n"
        "        }\n"
        "    }\n"
        "}\n";
    tr_variant      top, *headers, *body, *args, *ids;
    const char * str;
    int64_t      i;
    const int    err = tr_variantFromJson (&top, in, strlen(in));

    check (!err);
    check (tr_variantIsDict (&top));
    check ((headers = tr_variantDictFind (&top, tr_quark_new("headers",7))));
    check (tr_variantIsDict (headers));
    check (tr_variantDictFindStr (headers, tr_quark_new("type",4), &str, NULL));
    check_streq ("request", str);
    check (tr_variantDictFindInt (headers, TR_KEY_tag, &i));
    check_int_eq (666, i);
    check ((body = tr_variantDictFind (&top, tr_quark_new("body",4))));
    check (tr_variantDictFindStr (body, TR_KEY_name, &str, NULL));
    check_streq ("torrent-info", str);
    check ((args = tr_variantDictFind (body, tr_quark_new("arguments",9))));
    check (tr_variantIsDict (args));
    check ((ids = tr_variantDictFind (args, TR_KEY_ids)));
    check (tr_variantIsList (ids));
    check_int_eq (2, tr_variantListSize (ids));
    check (tr_variantGetInt (tr_variantListChild (ids, 0), &i));
    check_int_eq (7, i);
    check (tr_variantGetInt (tr_variantListChild (ids, 1), &i));
    check_int_eq (10, i);

    tr_variantFree (&top);
    return 0;
}
Exemplo n.º 17
0
static int
test_truncd( void )
{
    char buf[32];
    const double nan = sqrt( -1 );

    tr_snprintf( buf, sizeof( buf ), "%.2f%%", 99.999 );
    check_streq("100.00%", buf);

    tr_snprintf( buf, sizeof( buf ), "%.2f%%", tr_truncd( 99.999, 2 ) );
    check_streq("99.99%", buf);

    tr_snprintf( buf, sizeof( buf ), "%.4f", tr_truncd( 403650.656250, 4 ) );
    check_streq("403650.6562", buf);

    tr_snprintf( buf, sizeof( buf ), "%.2f", tr_truncd( 2.15, 2 ) );
    check_streq( "2.15", buf );

    tr_snprintf( buf, sizeof( buf ), "%.2f", tr_truncd( 2.05, 2 ) );
    check_streq( "2.05", buf );

    tr_snprintf( buf, sizeof( buf ), "%.2f", tr_truncd( 3.3333, 2 ) );
    check_streq( "3.33", buf );

    tr_snprintf( buf, sizeof( buf ), "%.0f", tr_truncd( 3.3333, 0 ) );
    check_streq( "3", buf );

    tr_snprintf( buf, sizeof( buf ), "%.2f", tr_truncd( nan, 2 ) );
    check( strstr( buf, "nan" ) != NULL );

    return 0;
}
Exemplo n.º 18
0
static int
test_env (void)
{
  const char * test_key = "TR_TEST_ENV";
  int x;
  char * s;

  unsetenv (test_key);

  check (!tr_env_key_exists (test_key));
  x = tr_env_get_int (test_key, 123);
  check_int_eq (123, x);
  s = tr_env_get_string (test_key, NULL);
  check (s == NULL);
  s = tr_env_get_string (test_key, "a");
  check_streq ("a", s);
  tr_free (s);

  setenv (test_key, "", 1);

  check (tr_env_key_exists (test_key));
  x = tr_env_get_int (test_key, 456);
  check_int_eq (456, x);
  s = tr_env_get_string (test_key, NULL);
  check_streq ("", s);
  tr_free (s);
  s = tr_env_get_string (test_key, "b");
  check_streq ("", s);
  tr_free (s);

  setenv (test_key, "135", 1);

  check (tr_env_key_exists (test_key));
  x = tr_env_get_int (test_key, 789);
  check_int_eq (135, x);
  s = tr_env_get_string (test_key, NULL);
  check_streq ("135", s);
  tr_free (s);
  s = tr_env_get_string (test_key, "c");
  check_streq ("135", s);
  tr_free (s);

  return 0;
}
Exemplo n.º 19
0
static int
test_strdup_printf (void)
{
  char * s, * s2, * s3;

  s = tr_strdup_printf ("%s", "test");
  check_streq ("test", s);
  tr_free (s);

  s = tr_strdup_printf ("%d %s %c %u", -1, "0", '1', 2);
  check_streq ("-1 0 1 2", s);
  tr_free (s);

  s3 = tr_malloc0 (4098);
  memset (s3, '-', 4097);
  s3[2047] = 't';
  s3[2048] = 'e';
  s3[2049] = 's';
  s3[2050] = 't';

  s2 = tr_malloc0 (4096);
  memset (s2, '-', 4095);
  s2[2047] = '%';
  s2[2048] = 's';

  s = tr_strdup_printf (s2, "test");
  check_streq (s3, s);
  tr_free (s);

  tr_free (s2);

  s = tr_strdup_printf ("%s", s3);
  check_streq (s3, s);
  tr_free (s);

  tr_free (s3);

  s = test_strdup_printf_valist ("\n-%s-%s-%s-\n", "\r", "\t", "\b");
  check_streq ("\n-\r-\t-\b-\n", s);
  tr_free (s);

  return 0;
}
Exemplo n.º 20
0
static int
test_strip_positional_args (void)
{
  const char * in;
  const char * out;
  const char * expected;

  in = "Hello %1$s foo %2$.*f";
  expected = "Hello %s foo %.*f";
  out = tr_strip_positional_args (in);
  check_streq (expected, out);

  in = "Hello %1$'d foo %2$'f";
  expected = "Hello %d foo %f";
  out = tr_strip_positional_args (in);
  check_streq (expected, out);

  return 0;
}
Exemplo n.º 21
0
static int
test_base64( void )
{
    char *in, *out;
    int   len;

    /* base64 */
    out = tr_base64_encode( "YOYO!", -1, &len );
    check_streq ("WU9ZTyE=", out);
    check_int_eq (8, len);
    in = tr_base64_decode( out, -1, &len );
    check_streq ("YOYO!", in);
    check_int_eq (5, len);
    tr_free( in );
    tr_free( out );
    out = tr_base64_encode( NULL, 0, &len );
    check( out == NULL );
    check_int_eq (0, len);

    return 0;
}
Exemplo n.º 22
0
static int
test1 (void)
{
    int i;
    const char * uri;
    tr_magnet_info * info;
    const int dec[] = { 210, 53, 64, 16, 163, 202, 74, 222, 91, 116,
                        39, 187, 9, 58, 98, 163, 137, 159, 243, 129 };

    uri = "magnet:?xt=urn:btih:"
          "d2354010a3ca4ade5b7427bb093a62a3899ff381"
          "&dn=Display%20Name"
          "&tr=http%3A%2F%2Ftracker.openbittorrent.com%2Fannounce"
          "&tr=http%3A%2F%2Ftracker.opentracker.org%2Fannounce"
          "&ws=http%3A%2F%2Fserver.webseed.org%2Fpath%2Fto%2Ffile";
    info = tr_magnetParse (uri);
    check (info != NULL);
    check_int_eq (2, info->trackerCount);
    check_streq (info->trackers[0], "http://tracker.openbittorrent.com/announce");
    check_streq (info->trackers[1], "http://tracker.opentracker.org/announce");
    check_int_eq (1, info->webseedCount);
    check_streq ("http://server.webseed.org/path/to/file", info->webseeds[0]);
    check_streq ("Display Name", info->displayName);
    for (i=0; i<20; ++i)
        check (info->hash[i] == dec[i]);
    tr_magnetFree (info);
    info = NULL;

    /* same thing but in base32 encoding */
    uri = "magnet:?xt=urn:btih:"
          "2I2UAEFDZJFN4W3UE65QSOTCUOEZ744B"
          "&dn=Display%20Name"
          "&tr=http%3A%2F%2Ftracker.openbittorrent.com%2Fannounce"
          "&ws=http%3A%2F%2Fserver.webseed.org%2Fpath%2Fto%2Ffile"
          "&tr=http%3A%2F%2Ftracker.opentracker.org%2Fannounce";
    info = tr_magnetParse (uri);
    check (info != NULL);
    check_int_eq (2, info->trackerCount);
    check_streq ("http://tracker.openbittorrent.com/announce", info->trackers[0]);
    check_streq ("http://tracker.opentracker.org/announce", info->trackers[1]);
    check_int_eq (1, info->webseedCount);
    check_streq ("http://server.webseed.org/path/to/file", info->webseeds[0]);
    check_streq ("Display Name", info->displayName);
    for (i=0; i<20; ++i)
        check (info->hash[i] == dec[i]);
    tr_magnetFree (info);
    info = NULL;

    return 0;
}
Exemplo n.º 23
0
static int
testParse2( void )
{
    tr_benc top;
    tr_benc top2;
    int64_t intVal;
    const char * strVal;
    double realVal;
    bool boolVal;
    int len;
    char * benc;
    const uint8_t * end;

    tr_bencInitDict( &top, 0 );
    tr_bencDictAddBool( &top, "this-is-a-bool", true );
    tr_bencDictAddInt( &top, "this-is-an-int", 1234 );
    tr_bencDictAddReal( &top, "this-is-a-real", 0.5 );
    tr_bencDictAddStr( &top, "this-is-a-string", "this-is-a-string" );

    benc = tr_bencToStr( &top, TR_FMT_BENC, &len );
    check_streq( "d14:this-is-a-booli1e14:this-is-a-real8:0.50000016:this-is-a-string16:this-is-a-string14:this-is-an-inti1234ee", benc );
    check( !tr_bencParse( benc, benc+len, &top2, &end ) );
    check( (char*)end == benc + len );
    check( tr_bencIsDict( &top2 ) );
    check( tr_bencDictFindInt( &top, "this-is-an-int", &intVal ) );
    check_int_eq (1234, intVal);
    check( tr_bencDictFindBool( &top, "this-is-a-bool", &boolVal ) );
    check( boolVal == true );
    check( tr_bencDictFindStr( &top, "this-is-a-string", &strVal ) );
    check_streq ("this-is-a-string", strVal);
    check( tr_bencDictFindReal( &top, "this-is-a-real", &realVal ) );
    check_int_eq (50, (int)(realVal*100));

    tr_bencFree( &top2 );
    tr_free( benc );
    tr_bencFree( &top );

    return 0;
}
Exemplo n.º 24
0
static int
test_list (void)
{
  size_t len;
  int64_t i;
  const char * str;
  tr_variant top;

  tr_rpc_parse_list_str (&top, "12", -1);
  check (tr_variantIsInt (&top));
  check (tr_variantGetInt (&top, &i));
  check_int_eq (12, i);
  tr_variantFree (&top);

  tr_rpc_parse_list_str (&top, "12", 1);
  check (tr_variantIsInt (&top));
  check (tr_variantGetInt (&top, &i));
  check_int_eq (1, i);
  tr_variantFree (&top);

  tr_rpc_parse_list_str (&top, "6,7", -1);
  check (tr_variantIsList (&top));
  check (tr_variantListSize (&top) == 2);
  check (tr_variantGetInt (tr_variantListChild (&top, 0), &i));
  check_int_eq (6, i);
  check (tr_variantGetInt (tr_variantListChild (&top, 1), &i));
  check_int_eq (7, i);
  tr_variantFree (&top);

  tr_rpc_parse_list_str (&top, "asdf", -1);
  check (tr_variantIsString (&top));
  check (tr_variantGetStr (&top, &str, &len));
  check_int_eq (4, len);
  check_streq ("asdf", str);
  tr_variantFree (&top);

  tr_rpc_parse_list_str (&top, "1,3-5", -1);
  check (tr_variantIsList (&top));
  check (tr_variantListSize (&top) == 4);
  check (tr_variantGetInt (tr_variantListChild (&top, 0), &i));
  check_int_eq (1, i);
  check (tr_variantGetInt (tr_variantListChild (&top, 1), &i));
  check_int_eq (3, i);
  check (tr_variantGetInt (tr_variantListChild (&top, 2), &i));
  check_int_eq (4, i);
  check (tr_variantGetInt (tr_variantListChild (&top, 3), &i));
  check_int_eq (5, i);
  tr_variantFree (&top);

  return 0;
}
Exemplo n.º 25
0
static int
test_hex (void)
{
  char hex1[41];
  char hex2[41];
  uint8_t sha1[20];

  memcpy (hex1, "fb5ef5507427b17e04b69cef31fa3379b456735a", 41);
  tr_hex_to_sha1 (sha1, hex1);
  tr_sha1_to_hex (hex2, sha1);
  check_streq (hex1, hex2);

  return 0;
}
Exemplo n.º 26
0
static int
test_hex (void)
{
  char hex1[41];
  char hex2[41];
  uint8_t binary[20];

  memcpy (hex1, "fb5ef5507427b17e04b69cef31fa3379b456735a", 41);
  tr_hex_to_binary (hex1, binary, 20);
  tr_binary_to_hex (binary, hex2, 20);
  check_streq (hex1, hex2);

  return 0;
}
Exemplo n.º 27
0
static int
test_unescape (void)
{
    const char * in = "{ \"string-1\": \"\\/usr\\/lib\" }";
    tr_variant top;
    const char * str;

    const int err = tr_variantFromJson (&top, in, strlen(in));
    check_int_eq (0, err);
    check (tr_variantDictFindStr (&top, tr_quark_new("string-1",8), &str, NULL));
    check_streq ("/usr/lib", str);

    tr_variantFree (&top);
    return 0;
}
Exemplo n.º 28
0
static int
test_encrypt_decrypt (void)
{
  tr_crypto a, b;
  uint8_t hash[SHA_DIGEST_LENGTH];
  const char test1[] = { "test1" };
  char buf11[sizeof (test1)], buf12[sizeof (test1)];
  const char test2[] = { "@#)C$@)#(*%bvkdjfhwbc039bc4603756VB3)" };
  char buf21[sizeof (test2)], buf22[sizeof (test2)];
  int i;

  for (i = 0; i < SHA_DIGEST_LENGTH; ++i)
    hash[i] = i;

  tr_cryptoConstruct (&a, hash, false);
  tr_cryptoConstruct (&b, hash, true);
  tr_cryptoComputeSecret (&a, tr_cryptoGetMyPublicKey (&b, &i));
  tr_cryptoComputeSecret (&b, tr_cryptoGetMyPublicKey (&a, &i));

  tr_cryptoEncryptInit (&a);
  tr_cryptoEncrypt (&a, sizeof (test1), test1, buf11);
  tr_cryptoDecryptInit (&b);
  tr_cryptoDecrypt (&b, sizeof (test1), buf11, buf12);
  check_streq (test1, buf12);

  tr_cryptoEncryptInit (&b);
  tr_cryptoEncrypt (&b, sizeof (test2), test2, buf21);
  tr_cryptoDecryptInit (&a);
  tr_cryptoDecrypt (&a, sizeof (test2), buf21, buf22);
  check_streq (test2, buf22);

  tr_cryptoDestruct (&b);
  tr_cryptoDestruct (&a);

  return 0;
}
Exemplo n.º 29
0
static int
test3 (void)
{
    const char * in = "{ \"error\": 2,"
                      "  \"errorString\": \"torrent not registered with this tracker 6UHsVW'*C\","
                      "  \"eta\": 262792,"
                      "  \"id\": 25,"
                      "  \"leftUntilDone\": 2275655680 }";
    tr_variant top;
    const char * str;

    const int err = tr_variantFromJson (&top, in, strlen(in));
    check (!err);
    check (tr_variantDictFindStr (&top, TR_KEY_errorString, &str, NULL));
    check_streq ("torrent not registered with this tracker 6UHsVW'*C", str);

    tr_variantFree (&top);
    return 0;
}
Exemplo n.º 30
0
static int
testJSONSnippet( const char * benc_str,
                 const char * expected )
{
    tr_benc top;
    char * serialized;
    struct evbuffer * buf;

    tr_bencLoad( benc_str, strlen( benc_str ), &top, NULL );
    buf = tr_bencToBuf( &top, TR_FMT_JSON );
    serialized = (char*) evbuffer_pullup( buf, -1 );
    stripWhitespace( serialized );
#if 0
    fprintf( stderr, "benc: %s\n", benc_str );
    fprintf( stderr, "json: %s\n", serialized );
    fprintf( stderr, "want: %s\n", expected );
#endif
    check_streq (expected, serialized);
    tr_bencFree( &top );
    evbuffer_free( buf );
    return 0;
}