30
30
#include "rhash.h"
31
31
#include "test_lib.h"
32
32
33
+ #define COUNTOF (a ) (sizeof(a) / sizeof(*a))
34
+
33
35
/*=========================================================================*
34
36
* Test vectors *
35
37
*=========================================================================*/
@@ -893,7 +895,7 @@ static void test_long_strings(void)
893
895
dbg ("test long strings\n" );
894
896
895
897
/* test all algorithms on 1,000,000 characters of 'a' */
896
- for (count = 0 ; count < ( sizeof ( tests ) / sizeof ( id_to_hash_t ) ); count ++ ) {
898
+ for (count = 0 ; count < COUNTOF ( tests ); count ++ ) {
897
899
unsigned flags = (tests [count ].hash_id == RHASH_BTIH ? CHDT_SET_FILENAME : CHDT_NO_FLAGS );
898
900
assert_rep_hash (tests [count ].hash_id , 'a' , 1000000 , tests [count ].expected_hash , flags );
899
901
}
@@ -1274,22 +1276,42 @@ static void test_import_export(void)
1274
1276
#endif /* !defined(NO_IMPORT_EXPORT) */
1275
1277
}
1276
1278
1279
+ static uint64_t make_hash_mask (size_t count , unsigned hash_ids [])
1280
+ {
1281
+ uint64_t hash_mask = 0 ;
1282
+ size_t i ;
1283
+ for (i = 0 ; i < count ; i ++ ) {
1284
+ hash_mask |= hash_ids [i ];
1285
+ }
1286
+ return hash_mask ;
1287
+ }
1288
+
1277
1289
#define TEST_PATH 0x4000000
1278
1290
1279
1291
/**
1280
1292
* Verify a magnet link.
1281
1293
*/
1282
- static void assert_magnet (const char * expected ,
1283
- rhash ctx , unsigned mask , int flags )
1294
+ static void test_magnet (const char * expected ,
1295
+ rhash ctx , int flags , size_t count , unsigned hash_ids [] )
1284
1296
{
1285
1297
static char out [2800 ];
1286
1298
const char * path = (flags & TEST_PATH ? "test.txt" : NULL );
1287
- size_t size_calculated = rhash_print_magnet (NULL , path , ctx , mask , flags );
1288
- size_t size_printed ;
1299
+ uint64_t hash_mask = make_hash_mask (count , hash_ids );
1300
+ size_t size_calculated = rhash_print_magnet_multi (NULL , 0 , path , ctx , flags , count , hash_ids );
1301
+ size_t size_calculated_legacy = rhash_print_magnet (NULL , path , ctx , (unsigned )hash_mask , flags );
1302
+ size_t size_printed , size_printed_legacy ;
1289
1303
REQUIRE_TRUE (size_calculated < sizeof (out ), "too small buffer for magnet link\n" );
1304
+ CHECK_NE (0 , size_calculated , "non zero buffer size expected to be returned\n" );
1305
+ CHECK_EQ (size_calculated , size_calculated_legacy , "wrong size_calculated_legacy\n" );
1290
1306
1291
1307
flags &= ~TEST_PATH ;
1292
- size_printed = rhash_print_magnet (out , path , ctx , mask , flags );
1308
+ if (size_calculated > 0 ) {
1309
+ size_printed = rhash_print_magnet_multi (out , size_calculated - 1 , path , ctx , flags , count , hash_ids );
1310
+ CHECK_EQ (0 , size_printed , "too small buffer error expected, but not occurred\n" );
1311
+ }
1312
+ size_printed = rhash_print_magnet_multi (out , size_calculated , path , ctx , flags , count , hash_ids );
1313
+ size_printed_legacy = rhash_print_magnet (out , path , ctx , (unsigned )hash_mask , flags );
1314
+ CHECK_EQ (size_printed , size_printed_legacy , "wrong size_calculated_legacy\n" );
1293
1315
1294
1316
if (expected && strcmp (expected , out ) != 0 ) {
1295
1317
log_error2 ("\"%s\" != \"%s\"\n" , expected , out );
@@ -1308,44 +1330,48 @@ static void assert_magnet(const char* expected,
1308
1330
/**
1309
1331
* Test printing of magnet links.
1310
1332
*/
1311
- static void test_magnet (void )
1333
+ static void test_magnet_links (void )
1312
1334
{
1313
- unsigned all_hash_ids [RHASH_HASH_COUNT ];
1314
- unsigned crc32_and_tth_ids [2 ] = { RHASH_CRC32 , RHASH_TTH };
1315
- size_t count = rhash_get_all_algorithms (RHASH_HASH_COUNT , all_hash_ids );
1335
+ unsigned hash_ids_all [RHASH_HASH_COUNT ];
1336
+ unsigned hash_ids_tth [] = { RHASH_TTH };
1337
+ unsigned hash_ids_md5 [] = { RHASH_MD5 };
1338
+ unsigned hash_ids_special [] = { RHASH_ED2K , RHASH_AICH , RHASH_SHA1 , RHASH_BTIH };
1339
+ unsigned hash_ids_crc32_tth [2 ] = { RHASH_CRC32 , RHASH_TTH };
1340
+ unsigned hash_id_all_hashes = RHASH_ALL_HASHES ;
1341
+ size_t count = rhash_get_all_algorithms (RHASH_HASH_COUNT , hash_ids_all );
1316
1342
size_t i ;
1317
1343
rhash ctx ;
1318
- dbg ("test magnet link \n" );
1319
- ctx = rhash_init ( RHASH_ALL_HASHES );
1344
+ dbg ("test magnet links \n" );
1345
+ ctx = rhash_init_multi ( count , hash_ids_all );
1320
1346
rhash_update (ctx , "a" , 1 );
1321
1347
rhash_final (ctx , 0 );
1322
- assert_magnet ("magnet:?xl=1&dn=test.txt&xt=urn:tree:tiger:czquwh3iyxbf5l3bgyugzhassmxu647ip2ike4y" ,
1323
- ctx , RHASH_TTH , RHPR_FILESIZE | TEST_PATH );
1324
- assert_magnet ("magnet:?xl=1&xt=urn:md5:0CC175B9C0F1B6A831C399E269772661" ,
1325
- ctx , RHASH_MD5 , RHPR_FILESIZE | RHPR_UPPERCASE );
1326
- assert_magnet (
1348
+ test_magnet ("magnet:?xl=1&dn=test.txt&xt=urn:tree:tiger:czquwh3iyxbf5l3bgyugzhassmxu647ip2ike4y" ,
1349
+ ctx , RHPR_FILESIZE | TEST_PATH , COUNTOF ( hash_ids_tth ), hash_ids_tth );
1350
+ test_magnet ("magnet:?xl=1&xt=urn:md5:0CC175B9C0F1B6A831C399E269772661" ,
1351
+ ctx , RHPR_FILESIZE | RHPR_UPPERCASE , COUNTOF ( hash_ids_md5 ), hash_ids_md5 );
1352
+ test_magnet (
1327
1353
"xt=urn:ed2k:bde52cb31de33e46245e05fbdbd6fb24&"
1328
1354
"xt=urn:aich:q336in72uwt7zyk5dxolt2xk5i3xmz5y&"
1329
1355
"xt=urn:sha1:q336in72uwt7zyk5dxolt2xk5i3xmz5y&"
1330
1356
"xt=urn:btih:827cd89846fc132e2e67e29c2784c65443bb4dc1" ,
1331
- ctx , RHASH_ED2K | RHASH_AICH | RHASH_SHA1 | RHASH_BTIH , RHPR_NO_MAGNET );
1357
+ ctx , RHPR_NO_MAGNET , COUNTOF ( hash_ids_special ), hash_ids_special );
1332
1358
1333
1359
/* verify length calculation for all hashes */
1334
1360
for (i = 0 ; i < count ; i ++ ) {
1335
- unsigned hash_id = all_hash_ids [i ];
1336
- assert_magnet (NULL , ctx , hash_id , RHPR_FILESIZE | RHPR_NO_MAGNET );
1361
+ unsigned hash_id = hash_ids_all [i ];
1362
+ test_magnet (NULL , ctx , RHPR_FILESIZE | RHPR_NO_MAGNET , 1 , & hash_id );
1337
1363
}
1338
- assert_magnet (NULL , ctx , RHASH_ALL_HASHES , RHPR_FILESIZE | RHPR_NO_MAGNET );
1364
+ test_magnet (NULL , ctx , RHPR_FILESIZE | RHPR_NO_MAGNET , 1 , & hash_id_all_hashes );
1339
1365
rhash_free (ctx );
1340
1366
1341
1367
/* test with two hash functions */
1342
- ctx = rhash_init_multi (2 , crc32_and_tth_ids );
1368
+ ctx = rhash_init_multi (COUNTOF ( hash_ids_crc32_tth ), hash_ids_crc32_tth );
1343
1369
rhash_update (ctx , "abc" , 3 );
1344
1370
rhash_final (ctx , 0 );
1345
- assert_magnet (
1371
+ test_magnet (
1346
1372
"magnet:?xl=3&xt=urn:crc32:352441c2&"
1347
1373
"xt=urn:tree:tiger:asd4ujseh5m47pdyb46kbtsqtsgdklbhyxomuia" ,
1348
- ctx , RHASH_ALL_HASHES , RHPR_FILESIZE );
1374
+ ctx , RHPR_FILESIZE , 1 , & hash_id_all_hashes );
1349
1375
}
1350
1376
1351
1377
/**
@@ -1465,7 +1491,7 @@ int main(int argc, char* argv[])
1465
1491
test_id_getters ();
1466
1492
test_get_context ();
1467
1493
test_import_export ();
1468
- test_magnet ();
1494
+ test_magnet_links ();
1469
1495
if (g_errors_count == 0 )
1470
1496
printf ("All sums are working properly!\n" );
1471
1497
fflush (stdout );
0 commit comments