aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakub Jelen <jjelen@redhat.com>2020-04-29 14:19:45 +0200
committerAndreas Schneider <asn@cryptomilk.org>2020-05-05 14:23:06 +0200
commit08a70bb4745c3bb4296e1fa187762eb07507e348 (patch)
treeee71ad80d6a061ee88bd908efabe2755389ecdb8
parent3d0ecd37fe6b438a89ddc424fdd59a2689f96f2d (diff)
downloadlibssh-08a70bb4745c3bb4296e1fa187762eb07507e348.tar.gz
libssh-08a70bb4745c3bb4296e1fa187762eb07507e348.tar.xz
libssh-08a70bb4745c3bb4296e1fa187762eb07507e348.zip
tests: Cover ssh_options_getopt with unit tests
Signed-off-by: Jakub Jelen <jjelen@redhat.com> Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
-rw-r--r--tests/unittests/torture_options.c148
1 files changed, 148 insertions, 0 deletions
diff --git a/tests/unittests/torture_options.c b/tests/unittests/torture_options.c
index 0ddf3cf3..d2ceedd5 100644
--- a/tests/unittests/torture_options.c
+++ b/tests/unittests/torture_options.c
@@ -913,6 +913,152 @@ static void torture_options_copy(void **state)
ssh_free(new);
}
+#define EXECUTABLE_NAME "test-exec"
+static void torture_options_getopt(void **state)
+{
+ ssh_session session = *state;
+ int rc;
+ int previous_level, new_level;
+ const char *argv[] = {EXECUTABLE_NAME, "-l", "username", "-p", "222",
+ "-vv", "-v", "-r", "-c", "aes128-ctr",
+ "-i", "id_rsa", "-C", "-2", "-1", NULL};
+ int argc = sizeof(argv)/sizeof(char *) - 1;
+ const char *argv_invalid[] = {EXECUTABLE_NAME, "-r", "-d", NULL};
+
+ previous_level = ssh_get_log_level();
+
+ /* Test with all the supported options */
+ rc = ssh_options_getopt(session, &argc, (char **)argv);
+#ifdef _MSC_VER
+ /* Not supported in windows */
+ assert_ssh_return_code_equal(session, rc, -1);
+#else
+ assert_ssh_return_code(session, rc);
+
+ /* Restore the log level to previous value first */
+ new_level = ssh_get_log_level();
+ assert_int_equal(new_level, 3); /* 2 + 1 -v's */
+ rc = ssh_set_log_level(previous_level);
+ assert_int_equal(rc, SSH_OK);
+
+ assert_ssh_return_code(session, rc);
+ assert_string_equal(session->opts.username, "username");
+ assert_int_equal(session->opts.port, 222);
+ /* The -r (usersa) is noop */
+ assert_string_equal(session->opts.wanted_methods[SSH_CRYPT_C_S],
+ "aes128-ctr");
+ assert_string_equal(session->opts.wanted_methods[SSH_CRYPT_S_C],
+ "aes128-ctr");
+ assert_string_equal(session->opts.identity->root->data, "id_rsa");
+ assert_string_equal(session->opts.wanted_methods[SSH_COMP_C_S],
+ "zlib@openssh.com,zlib");
+ assert_string_equal(session->opts.wanted_methods[SSH_COMP_S_C],
+ "zlib@openssh.com,zlib");
+ /* -1 and -2 are noop */
+
+
+ /* It should ignore unknown arguments */
+ argv[1] = "-F";
+ argv[2] = "config_file";
+ argv[3] = NULL;
+ argc = 3;
+ rc = ssh_options_getopt(session, &argc, (char **)argv);
+ assert_ssh_return_code(session, rc);
+ assert_int_equal(argc, 3);
+ assert_string_equal(argv[0], EXECUTABLE_NAME);
+ assert_string_equal(argv[1], "-F");
+ assert_string_equal(argv[2], "config_file");
+
+
+ /* It should not mess with unknown arguments order */
+ argv[1] = "-F";
+ argv[2] = "config_file";
+ argv[3] = "-M";
+ argv[4] = "hmac-sha1";
+ argv[5] = "-X";
+ argv[6] = NULL;
+ argc = 6;
+ rc = ssh_options_getopt(session, &argc, (char **)argv);
+ assert_ssh_return_code(session, rc);
+ assert_int_equal(argc, 6);
+ assert_string_equal(argv[0], EXECUTABLE_NAME);
+ assert_string_equal(argv[1], "-F");
+ assert_string_equal(argv[2], "config_file");
+ assert_string_equal(argv[3], "-M");
+ assert_string_equal(argv[4], "hmac-sha1");
+ assert_string_equal(argv[5], "-X");
+
+
+ /* Trailing arguments should be passed as they are */
+ argv[1] = "-F";
+ argv[2] = "config_file";
+ argv[3] = "-M";
+ argv[4] = "hmac-sha1";
+ argv[5] = "example.com";
+ argv[6] = NULL;
+ argc = 6;
+ rc = ssh_options_getopt(session, &argc, (char **)argv);
+ assert_ssh_return_code(session, rc);
+ assert_int_equal(argc, 6);
+ assert_string_equal(argv[0], EXECUTABLE_NAME);
+ assert_string_equal(argv[1], "-F");
+ assert_string_equal(argv[2], "config_file");
+ assert_string_equal(argv[3], "-M");
+ assert_string_equal(argv[4], "hmac-sha1");
+ assert_string_equal(argv[5], "example.com");
+
+
+ /* Invalid configuration combination -d and -r (for some reason?) */
+ argc = 3;
+ rc = ssh_options_getopt(session, &argc, (char **)argv_invalid);
+ assert_ssh_return_code_equal(session, rc, SSH_ERROR);
+ assert_int_equal(argc, 3);
+ assert_string_equal(argv_invalid[0], EXECUTABLE_NAME);
+ assert_string_equal(argv_invalid[1], "-r");
+ assert_string_equal(argv_invalid[2], "-d");
+
+
+ /* Corner case: only one argument */
+ argv[1] = "-C";
+ argv[2] = NULL;
+ argc = 2;
+ rc = ssh_options_set(session, SSH_OPTIONS_COMPRESSION, "no");
+ assert_ssh_return_code(session, rc);
+ assert_string_equal(session->opts.wanted_methods[SSH_COMP_C_S],
+ "none");
+ assert_string_equal(session->opts.wanted_methods[SSH_COMP_S_C],
+ "none");
+
+ rc = ssh_options_getopt(session, &argc, (char **)argv);
+ assert_ssh_return_code(session, rc);
+ assert_int_equal(argc, 1);
+ assert_string_equal(argv[0], EXECUTABLE_NAME);
+ assert_string_equal(session->opts.wanted_methods[SSH_COMP_C_S],
+ "zlib@openssh.com,zlib");
+ assert_string_equal(session->opts.wanted_methods[SSH_COMP_S_C],
+ "zlib@openssh.com,zlib");
+
+ /* Corner case: only hostname is not parsed */
+ argv[1] = "example.com";
+ argv[2] = NULL;
+ argc = 2;
+ rc = ssh_options_getopt(session, &argc, (char **)argv);
+ assert_ssh_return_code(session, rc);
+ assert_int_equal(argc, 2);
+ assert_string_equal(argv[0], EXECUTABLE_NAME);
+ assert_string_equal(argv[1], "example.com");
+
+ /* Corner case: no arguments */
+ argv[1] = NULL;
+ argc = 1;
+ rc = ssh_options_getopt(session, &argc, (char **)argv);
+ assert_ssh_return_code(session, rc);
+ assert_int_equal(argc, 1);
+ assert_string_equal(argv[0], EXECUTABLE_NAME);
+
+#endif /* _NSC_VER */
+}
+
#ifdef WITH_SERVER
const char template[] = "temp_dir_XXXXXX";
@@ -1705,6 +1851,8 @@ int torture_run_tests(void) {
setup, teardown),
cmocka_unit_test_setup_teardown(torture_options_config_match_multi,
setup, teardown),
+ cmocka_unit_test_setup_teardown(torture_options_getopt,
+ setup, teardown),
};
#ifdef WITH_SERVER