diff --git a/src/main.c b/src/main.c index 9488d0a4..8c0b590b 100644 --- a/src/main.c +++ b/src/main.c @@ -531,7 +531,7 @@ static int _tf_command_private(const char* file, int argc, char* argv[]) { "id", required_argument, NULL, 'i' }, { "recipients", required_argument, NULL, 'r' }, { "db-path", required_argument, NULL, 'd' }, - { "text", required_argument, NULL, 'c' }, + { "text", required_argument, NULL, 't' }, { "help", no_argument, NULL, 'h' }, { 0 }, }; diff --git a/src/ssb.tests.c b/src/ssb.tests.c index 53d39d62..6195c4b4 100644 --- a/src/ssb.tests.c +++ b/src/ssb.tests.c @@ -1478,4 +1478,97 @@ void tf_ssb_test_triggers(const tf_test_options_t* options) uv_loop_close(&loop); } +static void _subprocess_check_call(const char* command) +{ + int result = system(command); + if (!WIFEXITED(result)) + { + tf_printf("Command did not report exit: %s.\n", command); + abort(); + } + if (WEXITSTATUS(result) != 0) + { + tf_printf("Command returned %d: %s.\n", WEXITSTATUS(result), command); + abort(); + } +} + +static char* _subprocess_check_output(const char* command) +{ + FILE* proc = popen(command, "r"); + if (!proc) + { + tf_printf("Command failed (%s): %s.\n", strerror(errno), command); + abort(); + } + + char* result = NULL; + size_t size = 0; + if (proc) + { + const int k_block_size = 1024; + result = tf_realloc(result, size + k_block_size); + while (true) + { + size_t bytes = fread(result + size, 1, k_block_size, proc); + if (bytes > 0) + { + size += bytes; + } + else + { + break; + } + } + pclose(proc); + } + result = tf_realloc(result, size + 1); + if (result) + { + result[size] = '\0'; + } + return result; +} + +static bool _isspace(char c) +{ + return + c == ' ' || + c == '\t' || + c == '\r' || + c == '\n'; +} + +static char* _trim(char* p) +{ + if (!p) + { + return NULL; + } + + size_t length = strlen(p); + while (length > 0 && _isspace(p[length - 1])) + { + p[length - 1] = '\0'; + length--; + } + return p; +} + +void tf_ssb_test_cli(const tf_test_options_t* options) +{ + tf_printf("Testing CLI.\n"); + unlink("out/test_db0.sqlite"); + + char command[1024]; + snprintf(command, sizeof(command), "%s get_identity -d out/test_db0.sqlite", options->exe_path); + _subprocess_check_call(command); + char* id = _trim(_subprocess_check_output(command)); + tf_printf("id = [%s]\n", id); + + snprintf(command, sizeof(command), "%s private -i %s -r %s -d out/test_db0.sqlite -t '{\"type\": \"post\", \"text\": \"hello world\"}'", options->exe_path, id, id); + _subprocess_check_call(command); + + tf_free(id); +} #endif diff --git a/src/ssb.tests.h b/src/ssb.tests.h index 045e0eb7..9d9f109f 100644 --- a/src/ssb.tests.h +++ b/src/ssb.tests.h @@ -89,4 +89,10 @@ void tf_ssb_test_invite(const tf_test_options_t* options); */ void tf_ssb_test_triggers(const tf_test_options_t* options); +/** +** Test command-line interface. +** @param options The test options. +*/ +void tf_ssb_test_cli(const tf_test_options_t* options); + /** @} */ diff --git a/src/tests.c b/src/tests.c index bc803dcb..03e0dfca 100644 --- a/src/tests.c +++ b/src/tests.c @@ -1079,6 +1079,7 @@ void tf_tests(const tf_test_options_t* options) _tf_test_run(options, "connect_str", tf_ssb_test_connect_str, false); _tf_test_run(options, "invite", tf_ssb_test_invite, false); _tf_test_run(options, "triggers", tf_ssb_test_triggers, false); + _tf_test_run(options, "cli", tf_ssb_test_cli, false); tf_printf("Tests completed.\n"); #endif }