Skip to content

Commit

Permalink
config: glob support for include with test
Browse files Browse the repository at this point in the history
  • Loading branch information
NoName115 authored and Jakuje committed Nov 7, 2017
1 parent 119c9c9 commit 5f2a45d
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/config.c
Expand Up @@ -27,6 +27,7 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <glob.h>

#include "libssh/priv.h"
#include "libssh/session.h"
Expand Down Expand Up @@ -221,6 +222,31 @@ static void local_parse_file(ssh_session session, const char *filename, int *par
return;
}

static void local_parse_glob(ssh_session session, const char *fileglob, int *parsing, int seen[]) {
glob_t globbuf;
int rt;
u_int i;

memset(&globbuf, 0, sizeof(globbuf));
rt = glob(fileglob, GLOB_TILDE, NULL, &globbuf);
if (rt == GLOB_NOMATCH) {
globfree(&globbuf);
return;
}
else if (rt != 0) {
SSH_LOG(SSH_LOG_RARE, "Glob error: %s",
fileglob);
globfree(&globbuf);
return;
}

for (i = 0; i < globbuf.gl_pathc; i++) {
local_parse_file(session, globbuf.gl_pathv[i], parsing, seen);
}

globfree(&globbuf);
}

static int ssh_config_parse_line(ssh_session session, const char *line,
unsigned int count, int *parsing, int seen[]) {
enum ssh_config_opcode_e opcode;
Expand Down Expand Up @@ -266,7 +292,7 @@ static int ssh_config_parse_line(ssh_session session, const char *line,

p = ssh_config_get_str_tok(&s, NULL);
if (p && *parsing) {
local_parse_file(session, p, parsing, seen);
local_parse_glob(session, p, parsing, seen);
}
break;
case SOC_HOST: {
Expand Down
40 changes: 40 additions & 0 deletions tests/unittests/torture_config.c
Expand Up @@ -10,6 +10,9 @@
#define LIBSSH_TESTCONFIG2 "libssh_testconfig2.tmp"
#define LIBSSH_TESTCONFIG3 "libssh_testconfig3.tmp"
#define LIBSSH_TESTCONFIG4 "libssh_testconfig4.tmp"
#define LIBSSH_TESTCONFIG5 "libssh_testconfig5.tmp"
#define LIBSSH_TESTCONFIG6 "libssh_testconfig6.tmp"
#define LIBSSH_TESTCONFIGGLOB "libssh_testc*[36].tmp"

#define USERNAME "testuser"
#define PROXYCMD "ssh -q -W %h:%p gateway.example.com"
Expand All @@ -25,6 +28,8 @@ static int setup_config_files(void **state)
unlink(LIBSSH_TESTCONFIG2);
unlink(LIBSSH_TESTCONFIG3);
unlink(LIBSSH_TESTCONFIG4);
unlink(LIBSSH_TESTCONFIG5);
unlink(LIBSSH_TESTCONFIG6);

torture_write_file(LIBSSH_TESTCONFIG1,
"User "USERNAME"\nInclude "LIBSSH_TESTCONFIG2"\n\n");
Expand All @@ -40,6 +45,13 @@ static int setup_config_files(void **state)
torture_write_file(LIBSSH_TESTCONFIG4,
"Port 123\nPort 456\n");

/* Testing glob include */
torture_write_file(LIBSSH_TESTCONFIG5,
"User "USERNAME"\nInclude "LIBSSH_TESTCONFIGGLOB"\n\n");

torture_write_file(LIBSSH_TESTCONFIG6,
"ProxyCommand "PROXYCMD"\n\n");

session = ssh_new();
*state = session;

Expand All @@ -52,6 +64,8 @@ static int teardown(void **state)
unlink(LIBSSH_TESTCONFIG2);
unlink(LIBSSH_TESTCONFIG3);
unlink(LIBSSH_TESTCONFIG4);
unlink(LIBSSH_TESTCONFIG5);
unlink(LIBSSH_TESTCONFIG6);

ssh_free(*state);

Expand Down Expand Up @@ -105,6 +119,29 @@ static void torture_config_double_ports(void **state) {
assert_true(ret == 0);
}

static void torture_config_glob(void **state) {
ssh_session session = *state;
int ret;
char *v;

ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG5);
assert_true(ret == 0);

/* Test the variable presence */

ret = ssh_options_get(session, SSH_OPTIONS_PROXYCOMMAND, &v);
assert_true(ret == 0);

assert_string_equal(v, PROXYCMD);
ssh_string_free_char(v);

ret = ssh_options_get(session, SSH_OPTIONS_IDENTITY, &v);
assert_true(ret == 0);

assert_string_equal(v, ID_FILE);
ssh_string_free_char(v);
}

int torture_run_tests(void) {
int rc;
struct CMUnitTest tests[] = {
Expand All @@ -114,6 +151,9 @@ int torture_run_tests(void) {
cmocka_unit_test_setup_teardown(torture_config_double_ports,
setup_config_files,
teardown),
cmocka_unit_test_setup_teardown(torture_config_glob,
setup_config_files,
teardown),
};


Expand Down

0 comments on commit 5f2a45d

Please sign in to comment.