1
1

tests: Migrated torture_knownhosts to cmockery.

Этот коммит содержится в:
Andreas Schneider 2010-12-28 16:55:18 +01:00
родитель 8bd29736d2
Коммит e6329c72d1
2 изменённых файлов: 62 добавлений и 52 удалений

Просмотреть файл

@ -2,5 +2,5 @@ project(clienttests C)
add_cmockery_test(torture_algorithms torture_algorithms.c ${TORTURE_LIBRARY}) add_cmockery_test(torture_algorithms torture_algorithms.c ${TORTURE_LIBRARY})
add_cmockery_test(torture_auth torture_auth.c ${TORTURE_LIBRARY}) add_cmockery_test(torture_auth torture_auth.c ${TORTURE_LIBRARY})
#add_check_test(torture_knownhosts torture_knownhosts.c ${TORTURE_LIBRARY}) add_cmockery_test(torture_knownhosts torture_knownhosts.c ${TORTURE_LIBRARY})
#add_check_test(torture_proxycommand torture_proxycommand.c ${TORTURE_LIBRARY}) #add_check_test(torture_proxycommand torture_proxycommand.c ${TORTURE_LIBRARY})

Просмотреть файл

@ -22,69 +22,79 @@
#define LIBSSH_STATIC #define LIBSSH_STATIC
#include "torture.h" #include "torture.h"
#include "libssh/libssh.h" #include "session.c"
#include "libssh/priv.h"
#include "libssh/session.h"
ssh_session session;
#define KNOWNHOSTFILES "libssh_torture_knownhosts" #define KNOWNHOSTFILES "libssh_torture_knownhosts"
static void setup(void) { static void setup(void **state) {
int verbosity=torture_libssh_verbosity(); int verbosity=torture_libssh_verbosity();
session = ssh_new(); ssh_session session = ssh_new();
ssh_options_set(session,SSH_OPTIONS_LOG_VERBOSITY,&verbosity);
ssh_options_set(session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
*state = session;
} }
static void teardown(void) { static void teardown(void **state) {
ssh_free(session); ssh_free(*state);
unlink(KNOWNHOSTFILES); unlink(KNOWNHOSTFILES);
ssh_finalize(); ssh_finalize();
} }
START_TEST (torture_knownhosts_port) static void torture_knownhosts_port(void **state) {
{ ssh_session session = *state;
int rc; char buffer[200];
char buffer[200]; FILE *file;
FILE *file; int rc;
//int verbosity=SSH_LOG_FUNCTIONS;
/* Connect to localhost:22, force the port to 1234 and then write
* the known hosts file. Then check that the entry written is
* [localhost]:1234
*/
ssh_options_set(session,SSH_OPTIONS_HOST,"localhost");
ssh_options_set(session,SSH_OPTIONS_KNOWNHOSTS,KNOWNHOSTFILES);
rc=ssh_connect(session);
ck_assert_msg(rc==SSH_OK,ssh_get_error(session));
session->port=1234;
rc=ssh_write_knownhost(session);
ck_assert_msg(rc==SSH_OK,ssh_get_error(session));
ssh_disconnect(session);
ssh_free(session);
file=fopen(KNOWNHOSTFILES,"r");
ck_assert(file != NULL);
fgets(buffer,sizeof(buffer),file);
buffer[sizeof(buffer)-1]='\0';
ck_assert(strstr(buffer,"[localhost]:1234 ") != NULL);
fclose(file);
/* now, connect back to the ssh server and verify the known host line */ /* Connect to localhost:22, force the port to 1234 and then write
session=ssh_new(); * the known hosts file. Then check that the entry written is
ssh_options_set(session,SSH_OPTIONS_HOST,"localhost"); * [localhost]:1234
ssh_options_set(session,SSH_OPTIONS_KNOWNHOSTS,KNOWNHOSTFILES); */
//ssh_options_set(session,SSH_OPTIONS_LOG_VERBOSITY, &verbosity); rc = ssh_options_set(session, SSH_OPTIONS_HOST, "localhost");
rc=ssh_connect(session); assert_true(rc == SSH_OK);
ck_assert_msg(rc==SSH_OK,ssh_get_error(session));
session->port=1234; rc = ssh_options_set(session, SSH_OPTIONS_KNOWNHOSTS, KNOWNHOSTFILES);
rc=ssh_is_server_known(session); assert_true(rc == SSH_OK);
ck_assert_msg(rc==SSH_SERVER_KNOWN_OK,ssh_get_error(session));
ssh_disconnect(session); rc = ssh_connect(session);
assert_true(rc==SSH_OK);
session->port = 1234;
rc = ssh_write_knownhost(session);
assert_true(rc == SSH_OK);
ssh_disconnect(session);
ssh_free(session);
file = fopen(KNOWNHOSTFILES, "r");
assert_true(file != NULL);
fgets(buffer, sizeof(buffer), file);
fclose(file);
buffer[sizeof(buffer) - 1] = '\0';
assert_true(strstr(buffer,"[localhost]:1234 ") != NULL);
/* Now, connect back to the ssh server and verify the known host line */
session = ssh_new();
ssh_options_set(session, SSH_OPTIONS_HOST, "localhost");
ssh_options_set(session, SSH_OPTIONS_KNOWNHOSTS, KNOWNHOSTFILES);
#if 0
ssh_options_set(session,SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
#endif
rc = ssh_connect(session);
assert_true(rc == SSH_OK);
session->port = 1234;
rc = ssh_is_server_known(session);
assert_true(rc == SSH_SERVER_KNOWN_OK);
ssh_disconnect(session);
} }
END_TEST
Suite *torture_make_suite(void) { int torture_run_tests(void) {
Suite *s = suite_create("libssh_knownhosts"); const UnitTest tests[] = {
unit_test_setup_teardown(torture_knownhosts_port, setup, teardown),
};
torture_create_case_fixture(s, "torture_knownhosts_port", return run_tests(tests);
torture_knownhosts_port, setup, teardown);
return s;
} }