1
0
mirror of https://github.com/weechat/weechat.git synced 2026-06-12 14:14:48 +02:00

tests: add tests on function xfer_file_find_filename

This commit is contained in:
Sébastien Helleu
2026-06-04 22:55:29 +02:00
parent 23291acb7b
commit f4dc30ec58
+65 -1
View File
@@ -25,6 +25,11 @@
extern "C"
{
#include <stdlib.h>
#include <string.h>
#include "src/core/core-config-file.h"
#include "src/plugins/xfer/xfer.h"
#include "src/plugins/xfer/xfer-config.h"
#include "src/plugins/xfer/xfer-file.h"
}
@@ -32,6 +37,42 @@ TEST_GROUP(XferFile)
{
};
/*
* Build a "file recv" xfer with the given remote nick (and a fixed filename),
* call xfer_file_find_filename and return a copy of the basename of the local
* filename (the part after the last directory separator).
*
* Note: result must be freed after use.
*/
static char *
test_find_filename_basename (const char *remote_nick)
{
struct t_xfer xfer;
char *pos, *result;
memset (&xfer, 0, sizeof (xfer));
xfer.type = XFER_TYPE_FILE_RECV_ACTIVE;
xfer.remote_nick = strdup (remote_nick);
xfer.filename = strdup ("test.txt");
xfer_file_find_filename (&xfer);
result = NULL;
if (xfer.local_filename)
{
pos = strrchr (xfer.local_filename, DIR_SEPARATOR_CHAR);
result = strdup ((pos) ? pos + 1 : xfer.local_filename);
}
free (xfer.remote_nick);
free (xfer.filename);
free (xfer.local_filename);
free (xfer.temp_local_filename);
return result;
}
/*
* Test functions:
* xfer_file_search_crc32
@@ -91,7 +132,30 @@ TEST(XferFile, FindSuffix)
TEST(XferFile, FindFilename)
{
/* TODO: write tests */
char *basename;
config_file_option_set (xfer_config_file_download_path, "/tmp/weechat_test_xfer", 1);
/* remote nick without directory separator: used as-is */
basename = test_find_filename_basename ("alice");
STRCMP_EQUAL("alice.test.txt", basename);
free (basename);
/*
* remote nick with a directory separator: the separator is replaced by
* "_" so the nick cannot make the file be written outside the download
* directory
*/
basename = test_find_filename_basename ("../foo");
STRCMP_EQUAL(".._foo.test.txt", basename);
free (basename);
/* all directory separators in the nick are replaced */
basename = test_find_filename_basename ("a/b/c");
STRCMP_EQUAL("a_b_c.test.txt", basename);
free (basename);
config_file_option_unset (xfer_config_file_download_path);
}
/*