static void torture_sftp_fsync(void **state) { struct torture_state *s = *state; struct torture_sftp *t = s->ssh.tsftp; char libssh_tmp_file[] = "/tmp/libssh_sftp_test_XXXXXX"; char buf[MAX_XFER_BUF_SIZE] = {0}; char buf_verify[MAX_XFER_BUF_SIZE] = {0}; size_t count; size_t bytesread; ssize_t byteswritten; int fd; sftp_file file; mode_t mask; int rc; FILE *fp; struct stat sb; mask = umask(S_IRWXO | S_IRWXG); fd = mkstemp(libssh_tmp_file); umask(mask); assert_return_code(fd, errno); close(fd); unlink(libssh_tmp_file); file = sftp_open(t->sftp, libssh_tmp_file, O_WRONLY | O_CREAT, 0600); assert_non_null(file); rc = lstat(libssh_tmp_file, &sb); assert_return_code(rc, errno); snprintf(buf, sizeof(buf), "libssh fsync test\n"); count = strlen(buf) + 1; byteswritten = sftp_write(file, buf, count); assert_int_equal(byteswritten, count); rc = sftp_fsync(file); assert_return_code(rc, errno); fp = fopen(libssh_tmp_file, "r"); assert_non_null(fp); rc = fstat(fileno(fp), &sb); assert_return_code(rc, errno); bytesread = fread(buf_verify, sizeof(buf_verify), 1, fp); if (bytesread == 0) { if (!feof(fp)) { assert_int_equal(bytesread, count); } } assert_string_equal(buf, buf_verify); sftp_close(file); fclose(fp); unlink(libssh_tmp_file); }
static int agent_setup(void **state) { struct torture_state *s = *state; char ssh_agent_cmd[4096]; char ssh_agent_sock[1024]; char ssh_agent_pidfile[1024]; char bob_ssh_key[1024]; struct passwd *pwd; int rc; rc = pubkey_setup(state); if (rc != 0) { return rc; } pwd = getpwnam("bob"); assert_non_null(pwd); snprintf(ssh_agent_sock, sizeof(ssh_agent_sock), "%s/agent.sock", s->socket_dir); snprintf(ssh_agent_pidfile, sizeof(ssh_agent_pidfile), "%s/agent.pid", s->socket_dir); /* Production ready code!!! */ snprintf(ssh_agent_cmd, sizeof(ssh_agent_cmd), "eval `ssh-agent -a %s`; echo $SSH_AGENT_PID > %s", ssh_agent_sock, ssh_agent_pidfile); /* run ssh-agent and ssh-add as the normal user */ unsetenv("UID_WRAPPER_ROOT"); rc = system(ssh_agent_cmd); assert_return_code(rc, errno); setenv("SSH_AUTH_SOCK", ssh_agent_sock, 1); setenv("TORTURE_SSH_AGENT_PIDFILE", ssh_agent_pidfile, 1); snprintf(bob_ssh_key, sizeof(bob_ssh_key), "ssh-add %s/.ssh/id_rsa", pwd->pw_dir); rc = system(bob_ssh_key); assert_return_code(rc, errno); return 0; }
static void test_socket_getsockname(void **state) { struct torture_address addr = { .sa_socklen = sizeof(struct sockaddr_in), }; int rc; int s; (void) state; /* unused */ s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); assert_int_not_equal(s, -1); rc = getsockname(s, &addr.sa.in, &addr.sa_socklen); assert_return_code(rc, errno); assert_int_equal(addr.sa.in.sin_family, AF_INET); } #ifdef HAVE_IPV6 static void test_socket_getsockname6(void **state) { struct torture_address addr = { .sa_socklen = sizeof(struct sockaddr_in), }; int rc; int s; (void) state; /* unused */ s = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP); assert_int_not_equal(s, -1); rc = getsockname(s, &addr.sa.s, &addr.sa_socklen); assert_return_code(rc, errno); assert_int_equal(addr.sa.in6.sin6_family, AF_INET6); } #endif int main(void) { int rc; const UnitTest tests[] = { unit_test(test_socket_getsockname), #ifdef HAVE_IPV6 unit_test(test_socket_getsockname6), #endif }; rc = run_tests(tests); return rc; }
static int agent_cert_setup(void **state) { char bob_alt_ssh_key[1024]; struct passwd *pwd; int rc; rc = agent_setup(state); if (rc != 0) { return rc; } pwd = getpwnam("bob"); assert_non_null(pwd); /* remove all keys, load alternative key + cert */ snprintf(bob_alt_ssh_key, sizeof(bob_alt_ssh_key), "ssh-add -D && ssh-add %s/.ssh_cert/id_rsa", pwd->pw_dir); rc = system(bob_alt_ssh_key); assert_return_code(rc, errno); return 0; }
static int setup(void **state) { struct hostkey_state *h; mode_t mask; ssh_threads_set_callbacks(ssh_threads_get_pthread()); ssh_init(); h = malloc(sizeof(struct hostkey_state)); assert_non_null(h); h->hostkey_path = strdup("/tmp/libssh_hostkey_XXXXXX"); mask = umask(S_IRWXO | S_IRWXG); h->fd = mkstemp(h->hostkey_path); umask(mask); assert_return_code(h->fd, errno); close(h->fd); h->key_type = SSH_KEYTYPE_ECDSA; h->hostkey = torture_get_testkey(h->key_type, 0, 0); torture_write_file(h->hostkey_path, h->hostkey); *state = h; return 0; }
static int session_setup(void **state) { struct torture_state *s = *state; int verbosity = torture_libssh_verbosity(); struct passwd *pwd; bool b = false; int rc; pwd = getpwnam("bob"); assert_non_null(pwd); rc = setuid(pwd->pw_uid); assert_return_code(rc, errno); s->ssh.session = ssh_new(); assert_non_null(s->ssh.session); ssh_options_set(s->ssh.session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity); ssh_options_set(s->ssh.session, SSH_OPTIONS_HOST, TORTURE_SSH_SERVER); /* Make sure no other configuration options from system will get used */ rc = ssh_options_set(s->ssh.session, SSH_OPTIONS_PROCESS_CONFIG, &b); assert_ssh_return_code(s->ssh.session, rc); return 0; }
static int session_setup(void **state) { struct torture_state *s = *state; int verbosity = torture_libssh_verbosity(); struct passwd *pwd; int rc; pwd = getpwnam("bob"); assert_non_null(pwd); rc = setuid(pwd->pw_uid); assert_return_code(rc, errno); s->ssh.session = ssh_new(); assert_non_null(s->ssh.session); rc = ssh_options_set(s->ssh.session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity); assert_ssh_return_code(s->ssh.session, rc); rc = ssh_options_set(s->ssh.session, SSH_OPTIONS_HOST, TORTURE_SSH_SERVER); assert_ssh_return_code(s->ssh.session, rc); rc = ssh_options_set(s->ssh.session, SSH_OPTIONS_USER, TORTURE_SSH_USER_ALICE); assert_ssh_return_code(s->ssh.session, rc); return 0; }
/************************************** *** assert_return_code **************************************/ static void test_assert_return_code_fail(void **state) { int fd; (void)state; /* unused */ fd = open("this_file_doesnt_exist.cmocka", 0); assert_return_code(fd, errno); if (fd >= 0) { close(fd); } }
static int session_setup(void **state) { struct torture_state *s = *state; struct passwd *pwd; int rc; pwd = getpwnam("bob"); assert_non_null(pwd); rc = setuid(pwd->pw_uid); assert_return_code(rc, errno); s->ssh.session = torture_ssh_session(TORTURE_SSH_SERVER, NULL, TORTURE_SSH_USER_ALICE, NULL); assert_non_null(s->ssh.session); return 0; }
static int pubkey_setup(void **state) { int rc; struct passwd *pwd; rc = session_setup(state); if (rc != 0) { return rc; } pwd = getpwnam("bob"); assert_non_null(pwd); rc = setuid(pwd->pw_uid); assert_return_code(rc, errno); /* Make sure we do not interfere with another ssh-agent */ unsetenv("SSH_AUTH_SOCK"); unsetenv("SSH_AGENT_PID"); return 0; }
static void test_ssh_channel_request_x11(void **state) { struct hostkey_state *h = (struct hostkey_state *)*state; int rc, event_rc; pthread_t client_pthread; ssh_bind sshbind; ssh_session server; ssh_event event; struct channel_data channel_data; struct ssh_channel_callbacks_struct channel_cb = { .userdata = &channel_data, .channel_x11_req_function = ssh_channel_x11_req }; struct ssh_server_callbacks_struct server_cb = { .userdata = &channel_cb, .auth_password_function = auth_password_accept, .channel_open_request_session_function = channel_open }; memset(&channel_data, 0, sizeof(channel_data)); ssh_callbacks_init(&channel_cb); ssh_callbacks_init(&server_cb); /* Create server */ sshbind = torture_ssh_bind("localhost", TEST_SERVER_PORT, h->key_type, h->hostkey_path); assert_non_null(sshbind); /* Get client to connect */ rc = pthread_create(&client_pthread, NULL, client_thread, NULL); assert_return_code(rc, errno); server = ssh_new(); assert_true(server != NULL); rc = ssh_bind_accept(sshbind, server); assert_int_equal(rc, SSH_OK); /* Handle client connection */ ssh_set_server_callbacks(server, &server_cb); rc = ssh_handle_key_exchange(server); assert_int_equal(rc, SSH_OK); event = ssh_event_new(); assert_true(event != NULL); ssh_event_add_session(event, server); event_rc = SSH_OK; while (!channel_data.req_seen && event_rc == SSH_OK) { event_rc = ssh_event_dopoll(event, -1); } /* Cleanup */ ssh_event_free(event); ssh_free(server); ssh_bind_free(sshbind); rc = pthread_join(client_pthread, NULL); assert_int_equal(rc, 0); assert_true(channel_data.req_seen); assert_int_equal(channel_data.screen_number, x11_screen_number); }