diff options
Diffstat (limited to 'tests/server/torture_server.c')
-rw-r--r-- | tests/server/torture_server.c | 188 |
1 files changed, 16 insertions, 172 deletions
diff --git a/tests/server/torture_server.c b/tests/server/torture_server.c index 518fc972..910cd1bd 100644 --- a/tests/server/torture_server.c +++ b/tests/server/torture_server.c @@ -45,44 +45,14 @@ const char template[] = "temp_dir_XXXXXX"; struct test_server_st { struct torture_state *state; - struct server_state_st *ss; char *cwd; char *temp_dir; }; -static int setup_default_server(void **state) +static int libssh_server_setup(void **state) { - struct torture_state *s; - struct server_state_st *ss; - struct test_server_st *tss; -#ifdef HAVE_DSA - char dsa_hostkey[1024]; -#endif /* HAVE_DSA */ - - char ed25519_hostkey[1024] = {0}; - char rsa_hostkey[1024]; - char ecdsa_hostkey[1024]; - //char trusted_ca_pubkey[1024]; - - char sshd_path[1024]; - struct stat sb; - - const char *sftp_server_locations[] = { - "/usr/lib/ssh/sftp-server", - "/usr/libexec/sftp-server", - "/usr/libexec/openssh/sftp-server", - "/usr/lib/openssh/sftp-server", /* Debian */ - }; - - size_t sftp_sl_size = ARRAY_SIZE(sftp_server_locations); - const char *sftp_server; - - size_t i; - int rc; - - char pid_str[1024]; - - pid_t pid; + struct test_server_st *tss = NULL; + struct torture_state *s = NULL; assert_non_null(state); @@ -90,144 +60,26 @@ static int setup_default_server(void **state) assert_non_null(tss); torture_setup_socket_dir((void **)&s); - assert_non_null(s->socket_dir); - - /* Set the default interface for the server */ - setenv("SOCKET_WRAPPER_DEFAULT_IFACE", "10", 1); - setenv("PAM_WRAPPER", "1", 1); - - snprintf(sshd_path, - sizeof(sshd_path), - "%s/sshd", - s->socket_dir); - - rc = mkdir(sshd_path, 0755); - assert_return_code(rc, errno); - - snprintf(ed25519_hostkey, - sizeof(ed25519_hostkey), - "%s/sshd/ssh_host_ed25519_key", - s->socket_dir); - torture_write_file(ed25519_hostkey, - torture_get_openssh_testkey(SSH_KEYTYPE_ED25519, 0)); - -#ifdef HAVE_DSA - snprintf(dsa_hostkey, - sizeof(dsa_hostkey), - "%s/sshd/ssh_host_dsa_key", - s->socket_dir); - torture_write_file(dsa_hostkey, torture_get_testkey(SSH_KEYTYPE_DSS, 0)); -#endif /* HAVE_DSA */ - - snprintf(rsa_hostkey, - sizeof(rsa_hostkey), - "%s/sshd/ssh_host_rsa_key", - s->socket_dir); - torture_write_file(rsa_hostkey, torture_get_testkey(SSH_KEYTYPE_RSA, 0)); - - snprintf(ecdsa_hostkey, - sizeof(ecdsa_hostkey), - "%s/sshd/ssh_host_ecdsa_key", - s->socket_dir); - torture_write_file(ecdsa_hostkey, - torture_get_testkey(SSH_KEYTYPE_ECDSA_P521, 0)); - - sftp_server = getenv("TORTURE_SFTP_SERVER"); - if (sftp_server == NULL) { - for (i = 0; i < sftp_sl_size; i++) { - sftp_server = sftp_server_locations[i]; - rc = lstat(sftp_server, &sb); - if (rc == 0) { - break; - } - } - } - assert_non_null(sftp_server); - - /* Create default server state */ - ss = (struct server_state_st *)calloc(1, sizeof(struct server_state_st)); - assert_non_null(ss); - - ss->address = strdup("127.0.0.10"); - assert_non_null(ss->address); - - ss->port = 22; - - ss->ecdsa_key = strdup(ecdsa_hostkey); - assert_non_null(ss->ecdsa_key); - -#ifdef HAVE_DSA - ss->dsa_key = strdup(dsa_hostkey); - assert_non_null(ss->dsa_key); -#endif /* HAVE_DSA */ - - ss->ed25519_key = strdup(ed25519_hostkey); - assert_non_null(ed25519_hostkey); - - ss->rsa_key = strdup(rsa_hostkey); - assert_non_null(ss->rsa_key); - - ss->host_key = NULL; + torture_setup_create_libssh_config((void **)&s); - /* Use default username and password (set in default_handle_session_cb) */ - ss->expected_username = NULL; - ss->expected_password = NULL; - - ss->verbosity = torture_libssh_verbosity(); - - ss->auth_methods = SSH_AUTH_METHOD_PASSWORD | SSH_AUTH_METHOD_PUBLICKEY; - -#ifdef WITH_PCAP - ss->with_pcap = 1; - ss->pcap_file = strdup(s->pcap_file); - assert_non_null(ss->pcap_file); -#endif - - /* TODO make configurable */ - ss->max_tries = 3; - ss->error = 0; - - /* Use the default session handling function */ - ss->handle_session = default_handle_session_cb; - assert_non_null(ss->handle_session); - - /* Do not use global configuration */ - ss->parse_global_config = false; - - /* Start the server using the default values */ - pid = fork_run_server(ss); - if (pid < 0) { - fail(); - } - - snprintf(pid_str, sizeof(pid_str), "%d", pid); - - torture_write_file(s->srv_pidfile, (const char *)pid_str); - - setenv("SOCKET_WRAPPER_DEFAULT_IFACE", "21", 1); - unsetenv("PAM_WRAPPER"); - - /* Wait until the sshd is ready to accept connections */ - //rc = torture_wait_for_daemon(5); - //assert_int_equal(rc, 0); - - /* TODO properly wait for the server (use ping approach) */ - /* Wait 200ms */ - usleep(200 * 1000); + /* The second argument is the relative path to the "server" directory binary + */ + torture_setup_libssh_server((void **)&s, "./test_server/test_server"); + assert_non_null(s); tss->state = s; - tss->ss = ss; *state = tss; return 0; } -static int teardown_default_server(void **state) -{ - struct torture_state *s; - struct server_state_st *ss; - struct test_server_st *tss; +static int sshd_teardown(void **state) { + + struct test_server_st *tss = NULL; + struct torture_state *s = NULL; + + assert_non_null(state); tss = *state; assert_non_null(tss); @@ -235,14 +87,9 @@ static int teardown_default_server(void **state) s = tss->state; assert_non_null(s); - ss = tss->ss; - assert_non_null(ss); - - /* This function can be reused */ + /* This function can be reused to teardown the server */ torture_teardown_sshd_server((void **)&s); - free_server_state(tss->ss); - SAFE_FREE(tss->ss); SAFE_FREE(tss); return 0; @@ -540,10 +387,7 @@ int torture_run_tests(void) { ssh_init(); torture_filter_tests(tests); - rc = cmocka_run_group_tests(tests, - setup_default_server, - teardown_default_server); - + rc = cmocka_run_group_tests(tests, libssh_server_setup, sshd_teardown); ssh_finalize(); return rc; |