1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-11-03 19:37:16 -05:00

Add tests for format_call_external_argv

This commit is contained in:
William Wennerström 2020-12-07 16:16:15 +01:00
parent 4a1c118b8b
commit 867d895469
No known key found for this signature in database
GPG Key ID: E1382990BEDD319B
5 changed files with 91 additions and 19 deletions

View File

@ -4809,8 +4809,7 @@ cmd_disco(ProfWin* window, const char* const command, gchar** args)
return TRUE; return TRUE;
} }
// TODO(wstrm): Move this into its own tools such as HTTPUpload or // TODO: Move this into its own tools such as HTTPUpload or AESGCMDownload.
// AESGCMDownload.
#ifdef HAVE_OMEMO #ifdef HAVE_OMEMO
char* char*
_add_omemo_stream(int* fd, FILE** fh, char** err) _add_omemo_stream(int* fd, FILE** fh, char** err)
@ -9126,35 +9125,39 @@ cmd_url_open(ProfWin* window, const char* const command, gchar** args)
return TRUE; return TRUE;
} }
gchar* scheme = g_uri_parse_scheme(url); gchar* scheme = NULL;
char* cmd_template = NULL;
char* filename = NULL;
scheme = g_uri_parse_scheme(url);
if (scheme == NULL) { if (scheme == NULL) {
cons_show("URL '%s' is not valid.", args[1]); cons_show("URL '%s' is not valid.", args[1]);
return TRUE; goto out;
} }
char* cmd_template = prefs_get_string_with_option(PREF_URL_OPEN_CMD, scheme); cmd_template = prefs_get_string_with_option(PREF_URL_OPEN_CMD, scheme);
if (cmd_template == NULL) { if (cmd_template == NULL) {
cons_show("No default open command found in url open preferences"); cons_show("No default open command found in url open preferences");
return TRUE; goto out;
} }
#ifdef HAVE_OMEMO #ifdef HAVE_OMEMO
// OMEMO URLs (aesgcm://) must be saved and decrypted before being opened. // OMEMO URLs (aesgcm://) must be saved and decrypted before being opened.
if (0 == g_strcmp0(scheme, "aesgcm")) { if (0 == g_strcmp0(scheme, "aesgcm")) {
char* filename = unique_filename_from_url(url, files_get_data_path(DIR_DOWNLOADS)); filename = unique_filename_from_url(url, files_get_data_path(DIR_DOWNLOADS));
_url_aesgcm_method(window, cmd_template, url, filename); _url_aesgcm_method(window, cmd_template, url, filename);
free(filename);
goto out; goto out;
} }
#endif #endif
_url_external_method(cmd_template, url, NULL); _url_external_method(cmd_template, url, NULL);
#ifdef HAVE_OMEMO
out: out:
#endif
free(cmd_template); free(cmd_template);
free(scheme); free(filename);
g_free(scheme);
return TRUE; return TRUE;
} }
@ -9174,21 +9177,25 @@ cmd_url_save(ProfWin* window, const char* const command, gchar** args)
gchar* url = args[1]; gchar* url = args[1];
gchar* path = g_strdup(args[2]); gchar* path = g_strdup(args[2]);
gchar* scheme = NULL;
char* filename = NULL;
char* cmd_template = NULL;
gchar* scheme = g_uri_parse_scheme(url); scheme = g_uri_parse_scheme(url);
if (scheme == NULL) { if (scheme == NULL) {
cons_show("URL '%s' is not valid.", args[1]); cons_show("URL '%s' is not valid.", args[1]);
return TRUE; goto out;
} }
char* filename = unique_filename_from_url(url, path); filename = unique_filename_from_url(url, path);
if (filename == NULL) { if (filename == NULL) {
cons_show("Failed to generate unique filename" cons_show("Failed to generate unique filename"
"from URL '%s' for path '%s'", "from URL '%s' for path '%s'",
url, path); url, path);
goto out;
} }
char* cmd_template = prefs_get_string_with_option(PREF_URL_SAVE_CMD, scheme); cmd_template = prefs_get_string_with_option(PREF_URL_SAVE_CMD, scheme);
if (cmd_template == NULL) { if (cmd_template == NULL) {
if (g_strcmp0(scheme, "http") == 0 if (g_strcmp0(scheme, "http") == 0
|| g_strcmp0(scheme, "https") == 0) { || g_strcmp0(scheme, "https") == 0) {
@ -9204,8 +9211,14 @@ cmd_url_save(ProfWin* window, const char* const command, gchar** args)
_url_external_method(cmd_template, url, filename); _url_external_method(cmd_template, url, filename);
} }
out:
free(filename);
free(cmd_template); free(cmd_template);
g_free(scheme);
g_free(path);
return TRUE; return TRUE;
} }

View File

@ -155,7 +155,7 @@ aesgcm_file_get(void* userdata)
aesgcm_dl->filename, aesgcm_dl->filename,
aesgcm_dl->filename); aesgcm_dl->filename);
// TODO(wstrm): Log the error. // TODO: Log the error.
if (!call_external(argv, NULL, NULL)) { if (!call_external(argv, NULL, NULL)) {
http_print_transfer_update(aesgcm_dl->window, aesgcm_dl->url, http_print_transfer_update(aesgcm_dl->window, aesgcm_dl->url,
"Downloading '%s' failed: Unable to call " "Downloading '%s' failed: Unable to call "
@ -163,7 +163,7 @@ aesgcm_file_get(void* userdata)
aesgcm_dl->url, aesgcm_dl->url,
aesgcm_dl->cmd_template, aesgcm_dl->cmd_template,
aesgcm_dl->filename, aesgcm_dl->filename,
"TODO(wstrm): Log the error"); "TODO: Log the error");
} }
g_strfreev(argv); g_strfreev(argv);

View File

@ -330,6 +330,63 @@ strip_quotes_strips_both(void** state)
free(result); free(result);
} }
typedef struct
{
char* template;
char* url;
char* filename;
char* argv;
} format_call_external_argv_t;
void
format_call_external_argv_td(void** state)
{
enum table { num_tests = 4 };
format_call_external_argv_t tests[num_tests] = {
(format_call_external_argv_t){
.template = "/bin/echo %u %p",
.url = "https://example.org",
.filename = "image.jpeg",
.argv = "/bin/echo https://example.org image.jpeg",
},
(format_call_external_argv_t){
.template = "/bin/echo %p %u",
.url = "https://example.org",
.filename = "image.jpeg",
.argv = "/bin/echo image.jpeg https://example.org",
},
(format_call_external_argv_t){
.template = "/bin/echo %p",
.url = "https://example.org",
.filename = "image.jpeg",
.argv = "/bin/echo image.jpeg",
},
(format_call_external_argv_t){
.template = "/bin/echo %u",
.url = "https://example.org",
.filename = "image.jpeg",
.argv = "/bin/echo https://example.org",
},
};
gchar** got_argv = NULL;
gchar* got_argv_str = NULL;
for (int i = 0; i < num_tests; i++) {
got_argv = format_call_external_argv(
tests[i].template,
tests[i].url,
tests[i].filename);
got_argv_str = g_strjoinv(" ", got_argv);
assert_string_equal(got_argv_str, tests[i].argv);
g_strfreev(got_argv);
g_free(got_argv_str);
}
}
typedef struct typedef struct
{ {
char* url; char* url;
@ -438,8 +495,8 @@ unique_filename_from_url_td(void** state)
}, },
}; };
char* got_filename; char* got_filename = NULL;
char* exp_filename; char* exp_filename = NULL;
for (int i = 0; i < num_tests; i++) { for (int i = 0; i < num_tests; i++) {
got_filename = unique_filename_from_url(tests[i].url, tests[i].path); got_filename = unique_filename_from_url(tests[i].url, tests[i].path);
exp_filename = g_build_filename(tests[i].target, tests[i].basename, NULL); exp_filename = g_build_filename(tests[i].target, tests[i].basename, NULL);

View File

@ -32,3 +32,4 @@ void strip_quotes_strips_both(void** state);
void prof_partial_occurrences_tests(void** state); void prof_partial_occurrences_tests(void** state);
void prof_whole_occurrences_tests(void** state); void prof_whole_occurrences_tests(void** state);
void unique_filename_from_url_td(void** state); void unique_filename_from_url_td(void** state);
void format_call_external_argv_td(void** state);

View File

@ -90,6 +90,7 @@ main(int argc, char* argv[])
unit_test(strip_quotes_strips_first), unit_test(strip_quotes_strips_first),
unit_test(strip_quotes_strips_last), unit_test(strip_quotes_strips_last),
unit_test(strip_quotes_strips_both), unit_test(strip_quotes_strips_both),
unit_test(format_call_external_argv_td),
unit_test(unique_filename_from_url_td), unit_test(unique_filename_from_url_td),
unit_test(clear_empty), unit_test(clear_empty),