mirror of
https://github.com/transmission/transmission
synced 2024-12-25 01:03:01 +00:00
(trunk, libT) #5517 when parsing a torrent's metainfo's path list, allow empty components because some .torrent files innocently have them. Update unit tests.
This commit is contained in:
parent
4a61241406
commit
f433f2ee17
3 changed files with 104 additions and 33 deletions
|
@ -7,12 +7,14 @@
|
|||
* $Id$
|
||||
*/
|
||||
|
||||
#include "transmission.h"
|
||||
|
||||
#include "libtransmission-test.h"
|
||||
|
||||
#include "transmission.h"
|
||||
|
||||
#include <errno.h>
|
||||
|
||||
static int
|
||||
test1 (void)
|
||||
test_magnet_link (void)
|
||||
{
|
||||
tr_info inf;
|
||||
tr_ctor * ctor;
|
||||
|
@ -43,4 +45,64 @@ test1 (void)
|
|||
return 0;
|
||||
}
|
||||
|
||||
MAIN_SINGLE_TEST (test1)
|
||||
#define BEFORE_PATH "d10:created by25:Transmission/2.82 (14160)13:creation datei1402280218e8:encoding5:UTF-84:infod5:filesld6:lengthi2e4:pathl"
|
||||
#define AFTER_PATH "eed6:lengthi2e4:pathl5:b.txteee4:name3:foo12:piece lengthi32768e6:pieces20:ÞÉ`âMs¡Å;˺¬.åÂà7:privatei0eee"
|
||||
|
||||
static int
|
||||
test_metainfo (void)
|
||||
{
|
||||
size_t i;
|
||||
const struct {
|
||||
int expected_benc_err;
|
||||
int expected_parse_result;
|
||||
const void * benc;
|
||||
} metainfo[] = {
|
||||
{ 0, TR_PARSE_OK, BEFORE_PATH "5:a.txt" AFTER_PATH },
|
||||
|
||||
/* allow empty components, but not =all= empty components, see bug #5517 */
|
||||
{ 0, TR_PARSE_OK, BEFORE_PATH "0:5:a.txt" AFTER_PATH },
|
||||
{ 0, TR_PARSE_ERR, BEFORE_PATH "0:0:" AFTER_PATH },
|
||||
|
||||
/* don't allow path components in a filename */
|
||||
{ 0, TR_PARSE_ERR, BEFORE_PATH "7:a/a.txt" AFTER_PATH },
|
||||
|
||||
/* fail on "." components */
|
||||
{ 0, TR_PARSE_ERR, BEFORE_PATH "1:.5:a.txt" AFTER_PATH },
|
||||
{ 0, TR_PARSE_ERR, BEFORE_PATH "5:a.txt1:." AFTER_PATH },
|
||||
|
||||
/* fail on ".." components */
|
||||
{ 0, TR_PARSE_ERR, BEFORE_PATH "2:..5:a.txt" AFTER_PATH },
|
||||
{ 0, TR_PARSE_ERR, BEFORE_PATH "5:a.txt2:.." AFTER_PATH },
|
||||
|
||||
/* fail on empty string */
|
||||
{ EILSEQ, TR_PARSE_ERR, "" }
|
||||
};
|
||||
|
||||
tr_logSetLevel(0); /* yes, we already know these will generate errors, thank you... */
|
||||
|
||||
|
||||
for (i=0; i<(sizeof(metainfo) / sizeof(metainfo[0])); i++)
|
||||
{
|
||||
tr_ctor * ctor = tr_ctorNew (NULL);
|
||||
const int err = tr_ctorSetMetainfo (ctor, metainfo[i].benc, strlen(metainfo[i].benc));
|
||||
check_int_eq (metainfo[i].expected_benc_err, err);
|
||||
if (!err)
|
||||
{
|
||||
const tr_parse_result parse_result = tr_torrentParse (ctor, NULL);
|
||||
check_int_eq (metainfo[i].expected_parse_result, parse_result);
|
||||
}
|
||||
tr_ctorFree (ctor);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
main (void)
|
||||
{
|
||||
const testFunc tests[] = { test_magnet_link,
|
||||
test_metainfo };
|
||||
|
||||
return runTests (tests, NUM_TESTS (tests));
|
||||
}
|
||||
|
||||
|
|
|
@ -76,7 +76,6 @@ static bool
|
|||
path_component_is_suspicious (const char * component)
|
||||
{
|
||||
return (component == NULL)
|
||||
|| (*component == '\0')
|
||||
|| (strpbrk (component, PATH_DELIMITER_CHARS) != NULL)
|
||||
|| (strcmp (component, ".") == 0)
|
||||
|| (strcmp (component, "..") == 0);
|
||||
|
@ -86,6 +85,7 @@ static bool
|
|||
getfile (char ** setme, const char * root, tr_variant * path, struct evbuffer * buf)
|
||||
{
|
||||
bool success = false;
|
||||
size_t root_len = 0;
|
||||
|
||||
*setme = NULL;
|
||||
|
||||
|
@ -99,7 +99,8 @@ getfile (char ** setme, const char * root, tr_variant * path, struct evbuffer *
|
|||
|
||||
success = true;
|
||||
evbuffer_drain (buf, evbuffer_get_length (buf));
|
||||
evbuffer_add (buf, root, strlen (root));
|
||||
root_len = strlen (root);
|
||||
evbuffer_add (buf, root, root_len);
|
||||
|
||||
for (i=0; i<n; i++)
|
||||
{
|
||||
|
@ -113,11 +114,19 @@ getfile (char ** setme, const char * root, tr_variant * path, struct evbuffer *
|
|||
break;
|
||||
}
|
||||
|
||||
if (!*str)
|
||||
continue;
|
||||
|
||||
evbuffer_add (buf, TR_PATH_DELIMITER_STR, 1);
|
||||
evbuffer_add (buf, str, len);
|
||||
}
|
||||
}
|
||||
|
||||
if (success && (evbuffer_get_length (buf) <= root_len))
|
||||
{
|
||||
success = false;
|
||||
}
|
||||
|
||||
if (success)
|
||||
{
|
||||
*setme = tr_utf8clean ((char*)evbuffer_pullup (buf, -1), evbuffer_get_length (buf));
|
||||
|
|
|
@ -248,8 +248,8 @@ tr_variantParseBenc (const void * buf_in,
|
|||
break;
|
||||
}
|
||||
|
||||
if (!err)
|
||||
err = !top->type || !tr_ptrArrayEmpty(&stack);
|
||||
if (!err && (!top->type || !tr_ptrArrayEmpty(&stack)))
|
||||
err = EILSEQ;
|
||||
|
||||
if (!err && setme_end)
|
||||
*setme_end = (const char*) buf;
|
||||
|
|
Loading…
Reference in a new issue