Improve ssh_options_get(ssh_session, enum ssh_options_e, char**).
* Use SSH_ERROR and SSH_OK instead of `-1` and `0`. * Re-factor for code duplication * No longer call `ssh_set_error_invalid(ssh_session)` when the ssh_session is NULL.
Этот коммит содержится в:
родитель
2c04994443
Коммит
17f396ffab
@ -838,57 +838,42 @@ int ssh_options_get_port(ssh_session session, unsigned int* port_target) {
|
|||||||
* your responsibility to free the memory using
|
* your responsibility to free the memory using
|
||||||
* ssh_free().
|
* ssh_free().
|
||||||
*
|
*
|
||||||
* @return 0 on success, < 0 on error.
|
* @return SSH_OK on success, SSH_ERROR on error.
|
||||||
*/
|
*/
|
||||||
int ssh_options_get(ssh_session session, enum ssh_options_e type, char** value)
|
int ssh_options_get(ssh_session session, enum ssh_options_e type, char** value)
|
||||||
{
|
{
|
||||||
|
char* src = NULL;
|
||||||
if (session == NULL) {
|
if (session == NULL) {
|
||||||
return -1;
|
return SSH_ERROR;
|
||||||
}
|
}
|
||||||
switch(type)
|
switch(type)
|
||||||
{
|
{
|
||||||
case SSH_OPTIONS_HOST: {
|
case SSH_OPTIONS_HOST: {
|
||||||
if (session->host == NULL) {
|
src = session->host;
|
||||||
ssh_set_error_invalid(session);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
*value = strdup(session->host);
|
|
||||||
if (*value == NULL) {
|
|
||||||
ssh_set_error_oom(session);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case SSH_OPTIONS_USER: {
|
case SSH_OPTIONS_USER: {
|
||||||
if (session->username == NULL) {
|
src = session->username;
|
||||||
ssh_set_error_invalid(session);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
*value = strdup(session->username);
|
|
||||||
if (*value == NULL) {
|
|
||||||
ssh_set_error_oom(session);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case SSH_OPTIONS_IDENTITY: {
|
case SSH_OPTIONS_IDENTITY: {
|
||||||
if (session->identity == NULL) {
|
src = ssh_iterator_value(char *, ssh_list_get_iterator(session->identity));
|
||||||
ssh_set_error_invalid(session);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
*value = strdup(session->identity->root->data);
|
|
||||||
if(*value == NULL){
|
|
||||||
ssh_set_error_oom(session);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
ssh_set_error(session, SSH_REQUEST_DENIED, "Unknown ssh option %d", type);
|
ssh_set_error(session, SSH_REQUEST_DENIED, "Unknown ssh option %d", type);
|
||||||
return -1;
|
return SSH_ERROR;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return 0;
|
if (src == NULL) {
|
||||||
|
return SSH_ERROR;
|
||||||
|
}
|
||||||
|
*value = strdup(src);
|
||||||
|
if (*value == NULL) {
|
||||||
|
ssh_set_error_oom(session);
|
||||||
|
return SSH_ERROR;
|
||||||
|
}
|
||||||
|
return SSH_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -83,7 +83,7 @@ static void torture_options_get_user(void **state) {
|
|||||||
char* user = NULL;
|
char* user = NULL;
|
||||||
int rc;
|
int rc;
|
||||||
rc = ssh_options_set(session, SSH_OPTIONS_USER, "magicaltrevor");
|
rc = ssh_options_set(session, SSH_OPTIONS_USER, "magicaltrevor");
|
||||||
assert_true(rc == 0);
|
assert_true(rc == SSH_OK);
|
||||||
rc = ssh_options_get(session, SSH_OPTIONS_USER, &user);
|
rc = ssh_options_get(session, SSH_OPTIONS_USER, &user);
|
||||||
assert_string_equal(user, "magicaltrevor");
|
assert_string_equal(user, "magicaltrevor");
|
||||||
}
|
}
|
||||||
@ -98,7 +98,7 @@ static void torture_options_set_fd(void **state) {
|
|||||||
assert_true(session->fd == fd);
|
assert_true(session->fd == fd);
|
||||||
|
|
||||||
rc = ssh_options_set(session, SSH_OPTIONS_FD, NULL);
|
rc = ssh_options_set(session, SSH_OPTIONS_FD, NULL);
|
||||||
assert_true(rc == -1);
|
assert_true(rc == SSH_ERROR);
|
||||||
assert_true(session->fd == SSH_INVALID_SOCKET);
|
assert_true(session->fd == SSH_INVALID_SOCKET);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -161,14 +161,14 @@ static void torture_options_get_identity(void **state) {
|
|||||||
rc = ssh_options_set(session, SSH_OPTIONS_ADD_IDENTITY, "identity1");
|
rc = ssh_options_set(session, SSH_OPTIONS_ADD_IDENTITY, "identity1");
|
||||||
assert_true(rc == 0);
|
assert_true(rc == 0);
|
||||||
rc = ssh_options_get(session, SSH_OPTIONS_IDENTITY, &identity);
|
rc = ssh_options_get(session, SSH_OPTIONS_IDENTITY, &identity);
|
||||||
assert_true(rc == 0);
|
assert_true(rc == SSH_OK);
|
||||||
assert_string_equal(identity, "identity1");
|
assert_string_equal(identity, "identity1");
|
||||||
|
|
||||||
rc = ssh_options_set(session, SSH_OPTIONS_IDENTITY, "identity2");
|
rc = ssh_options_set(session, SSH_OPTIONS_IDENTITY, "identity2");
|
||||||
assert_true(rc == 0);
|
assert_true(rc == 0);
|
||||||
assert_string_equal(session->identity->root->data, "identity2");
|
assert_string_equal(session->identity->root->data, "identity2");
|
||||||
rc = ssh_options_get(session, SSH_OPTIONS_IDENTITY, &identity);
|
rc = ssh_options_get(session, SSH_OPTIONS_IDENTITY, &identity);
|
||||||
assert_true(rc == 0);
|
assert_true(rc == SSH_OK);
|
||||||
assert_string_equal(identity, "identity2");
|
assert_string_equal(identity, "identity2");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Загрузка…
x
Ссылка в новой задаче
Block a user