From b9ec274bed571db9c705b65bb8c0bb6409c34fc4 Mon Sep 17 00:00:00 2001 From: "Rafael G. Martins" Date: Wed, 21 Dec 2016 23:12:05 +0100 Subject: git-receiver: moved shell quote function to common --- src/blogc-git-receiver/shell-command-parser.c | 25 ----------------- src/blogc-git-receiver/shell-command-parser.h | 1 - src/blogc-git-receiver/shell.c | 2 +- src/common/utils.c | 24 +++++++++++++++++ src/common/utils.h | 5 ++++ .../check_shell_command_parser.c | 29 -------------------- tests/common/check_utils.c | 31 ++++++++++++++++++++++ 7 files changed, 61 insertions(+), 56 deletions(-) diff --git a/src/blogc-git-receiver/shell-command-parser.c b/src/blogc-git-receiver/shell-command-parser.c index 0c867b8..4cfdec8 100644 --- a/src/blogc-git-receiver/shell-command-parser.c +++ b/src/blogc-git-receiver/shell-command-parser.c @@ -99,28 +99,3 @@ error: bc_string_free(rv, true); return NULL; } - - -char* -bgr_shell_quote(const char *command) -{ - // this does not really belongs here, but function is very small - bc_string_t *rv = bc_string_new(); - bc_string_append_c(rv, '\''); - if (command != NULL) { - for (size_t i = 0; i < strlen(command); i++) { - switch (command[i]) { - case '!': - bc_string_append(rv, "'\\!'"); - break; - case '\'': - bc_string_append(rv, "'\\''"); - break; - default: - bc_string_append_c(rv, command[i]); - } - } - } - bc_string_append_c(rv, '\''); - return bc_string_free(rv, false); -} diff --git a/src/blogc-git-receiver/shell-command-parser.h b/src/blogc-git-receiver/shell-command-parser.h index 47054cb..652d671 100644 --- a/src/blogc-git-receiver/shell-command-parser.h +++ b/src/blogc-git-receiver/shell-command-parser.h @@ -10,6 +10,5 @@ #define _SHELL_COMMAND_PARSER_H char* bgr_shell_command_parse(const char *command); -char* bgr_shell_quote(const char *command); #endif /* _SHELL_COMMAND_PARSER_H */ diff --git a/src/blogc-git-receiver/shell.c b/src/blogc-git-receiver/shell.c index d83174f..a21c5bd 100644 --- a/src/blogc-git-receiver/shell.c +++ b/src/blogc-git-receiver/shell.c @@ -51,7 +51,7 @@ bgr_shell(int argc, char *argv[]) } repo = bc_strdup_printf("%s/repos/%s", home, tmp_repo); - quoted_repo = bgr_shell_quote(repo); + quoted_repo = bc_shell_quote(repo); free(tmp_repo); if (0 == strncmp(argv[2], "git-upload-", 11)) // no need to check len here diff --git a/src/common/utils.c b/src/common/utils.c index f745146..9061e40 100644 --- a/src/common/utils.c +++ b/src/common/utils.c @@ -643,3 +643,27 @@ bc_trie_foreach(bc_trie_t *trie, bc_trie_foreach_func_t func, bc_trie_foreach_node(trie->root, str, func, user_data); bc_string_free(str, true); } + + +char* +bc_shell_quote(const char *command) +{ + bc_string_t *rv = bc_string_new(); + bc_string_append_c(rv, '\''); + if (command != NULL) { + for (size_t i = 0; i < strlen(command); i++) { + switch (command[i]) { + case '!': + bc_string_append(rv, "'\\!'"); + break; + case '\'': + bc_string_append(rv, "'\\''"); + break; + default: + bc_string_append_c(rv, command[i]); + } + } + } + bc_string_append_c(rv, '\''); + return bc_string_free(rv, false); +} diff --git a/src/common/utils.h b/src/common/utils.h index 020f243..2bda45d 100644 --- a/src/common/utils.h +++ b/src/common/utils.h @@ -99,4 +99,9 @@ size_t bc_trie_size(bc_trie_t *trie); void bc_trie_foreach(bc_trie_t *trie, bc_trie_foreach_func_t func, void *user_data); + +// shell + +char* bc_shell_quote(const char *command); + #endif /* _UTILS_H */ diff --git a/tests/blogc-git-receiver/check_shell_command_parser.c b/tests/blogc-git-receiver/check_shell_command_parser.c index 9d0e282..a97df60 100644 --- a/tests/blogc-git-receiver/check_shell_command_parser.c +++ b/tests/blogc-git-receiver/check_shell_command_parser.c @@ -205,40 +205,11 @@ test_shell_command_parse(void **state) } -static void -test_shell_quote(void **state) -{ - char *t; - t = bgr_shell_quote(NULL); - assert_string_equal(t, "''"); - free(t); - t = bgr_shell_quote("!bola"); - assert_string_equal(t, "''\\!'bola'"); - free(t); - t = bgr_shell_quote("'bola"); - assert_string_equal(t, "''\\''bola'"); - free(t); - t = bgr_shell_quote("bo!bola"); - assert_string_equal(t, "'bo'\\!'bola'"); - free(t); - t = bgr_shell_quote("bo'bola"); - assert_string_equal(t, "'bo'\\''bola'"); - free(t); - t = bgr_shell_quote("bola!"); - assert_string_equal(t, "'bola'\\!''"); - free(t); - t = bgr_shell_quote("bola'"); - assert_string_equal(t, "'bola'\\'''"); - free(t); -} - - int main(void) { const UnitTest tests[] = { unit_test(test_shell_command_parse), - unit_test(test_shell_quote), }; return run_tests(tests); } diff --git a/tests/common/check_utils.c b/tests/common/check_utils.c index f761cd8..955503b 100644 --- a/tests/common/check_utils.c +++ b/tests/common/check_utils.c @@ -943,6 +943,34 @@ test_trie_foreach(void **state) } +static void +test_shell_quote(void **state) +{ + char *t; + t = bc_shell_quote(NULL); + assert_string_equal(t, "''"); + free(t); + t = bc_shell_quote("!bola"); + assert_string_equal(t, "''\\!'bola'"); + free(t); + t = bc_shell_quote("'bola"); + assert_string_equal(t, "''\\''bola'"); + free(t); + t = bc_shell_quote("bo!bola"); + assert_string_equal(t, "'bo'\\!'bola'"); + free(t); + t = bc_shell_quote("bo'bola"); + assert_string_equal(t, "'bo'\\''bola'"); + free(t); + t = bc_shell_quote("bola!"); + assert_string_equal(t, "'bola'\\!''"); + free(t); + t = bc_shell_quote("bola'"); + assert_string_equal(t, "'bola'\\'''"); + free(t); +} + + int main(void) { @@ -987,6 +1015,9 @@ main(void) unit_test(test_trie_lookup), unit_test(test_trie_size), unit_test(test_trie_foreach), + + // shell + unit_test(test_shell_quote), }; return run_tests(tests); } -- cgit v1.2.3-18-g5258