Skip to content
Permalink
Browse files

tests: check test suite syntax with checksrc.pl

  • Loading branch information...
tiennou committed Jul 16, 2019
1 parent 013b935 commit ba1c0ba1cbc100ace15b06d50a15a02d9500c4e6
@@ -2,4 +2,7 @@

set -e

perl ./ci/checksrc.pl -i4 -m79 -ASIZEOFNOPAREN -ASNPRINTF -ACOPYRIGHT -AFOPENMODE -Wsrc/libssh2_config.h src/*.[ch] include/*.h example/*.c
FILES="src/*.[ch] include/*.h example/*.c tests/*.[ch]"
WHITELIST="-Wsrc/libssh2_config.h"

perl ./ci/checksrc.pl -i4 -m79 -ASIZEOFNOPAREN -ASNPRINTF -ACOPYRIGHT -AFOPENMODE $WHITELIST $FILES
@@ -170,6 +170,10 @@ add_target_to_copy_dependencies(
BEFORE_TARGETS ${TEST_TARGETS})


if(LINT)
add_dependencies(libssh2_clar lint)
endif()

# TODO convert mansyntax.sh into CMake script.
# XXX Just because we can find all three programs, doesn't mean sh can
# find man and grep
@@ -77,15 +77,13 @@ static int run_command_varg(char **output, const char *command, va_list args)
return -1;
}

/* Rewrite the command to redirect stderr to stdout to we can output it */
if(strlen(command_buf) + 6 >= sizeof(command_buf)) {
fprintf(stderr, "Unable to rewrite command (%s)\n", command);
/* Rewrite the command to redirect stderr to stdout so we can output it */
ret = snprintf(buf, sizeof(buf), "%s 2>&1", command_buf);
if(ret < 0 || ret >= BUFSIZ) {
fprintf(stderr, "Unable to format command (%s)\n", command_buf);
return -1;
}

strncat(command_buf, " 2>&1", 6);

fprintf(stdout, "Command: %s\n", command);
#ifdef WIN32
pipe = _popen(command_buf, "r");
#else
@@ -112,9 +110,9 @@ static int run_command_varg(char **output, const char *command, va_list args)
if(output) {
/* command output may contain a trailing newline, so we trim
* whitespace here */
size_t end = strlen(buf) - 1;
while(end > 0 && isspace(buf[end])) {
buf[end] = '\0';
size_t end = strlen(buf);
while(end > 0 && isspace(buf[end - 1])) {
buf[end - 1] = '\0';
}

*output = strdup(buf);
@@ -134,9 +132,11 @@ static int run_command(char **output, const char *command, ...)
return ret;
}

static int build_openssh_server_docker_image()
static int build_openssh_server_docker_image(void)
{
return run_command(NULL, "docker build -t libssh2/openssh_server openssh_server");
return run_command(NULL,
"docker build -t libssh2/openssh_server openssh_server"
);
}

static int start_openssh_server(char **container_id_out)
@@ -151,7 +151,7 @@ static int stop_openssh_server(char *container_id)
return run_command(NULL, "docker stop %s", container_id);
}

static const char *docker_machine_name()
static const char *docker_machine_name(void)
{
return getenv("DOCKER_MACHINE_NAME");
}
@@ -167,7 +167,9 @@ static int ip_address_from_container(char *container_id, char **ip_address_out)
int attempt_no = 0;
int wait_time = 500;
for(;;) {
return run_command(ip_address_out, "docker-machine ip %s", active_docker_machine);
return run_command(ip_address_out,
"docker-machine ip %s",
active_docker_machine);

if(attempt_no > 5) {
fprintf(
@@ -218,7 +220,8 @@ static int open_socket_to_container(char *container_id)

int ret = ip_address_from_container(container_id, &ip_address);
if(ret != 0) {
fprintf(stderr, "Failed to get IP address for container %s\n", container_id);
fprintf(stderr, "Failed to get IP address for container %s\n",
container_id);
ret = -1;
goto cleanup;
}
@@ -249,7 +252,8 @@ static int open_socket_to_container(char *container_id)

if(connect(sock, (struct sockaddr *)(&sin),
sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "Failed to connect to %s:%s\n", ip_address, port_string);
fprintf(stderr, "Failed to connect to %s:%s\n",
ip_address, port_string);
ret = -1;
goto cleanup;
}
@@ -83,18 +83,21 @@ int main(int argc, char *argv[])
return 1;
}

/* Create a session instance and start it up
* This will trade welcome banners, exchange keys, and setup crypto, compression, and MAC layers
/* Create a session instance and start it up.
*
* This will trade welcome banners, exchange keys, and setup crypto,
* compression, and MAC layers.
*/
session = libssh2_session_init();
if(libssh2_session_startup(session, sock)) {
fprintf(stderr, "Failure establishing SSH session\n");
return 1;
}

/* At this point we haven't authenticated,
* The first thing to do is check the hostkey's fingerprint against our known hosts
* Your app may have it hard coded, may go to a file, may present it to the user, that's your call
/* At this point we haven't authenticated, the first thing to do is check
* the hostkey's fingerprint against our known hosts.
* Your app may have it hard coded, may go to a file, may present it to the
* user, that's your call.
*/
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
printf("Fingerprint: ");
@@ -118,7 +121,11 @@ int main(int argc, char *argv[])

if(auth_pw & 4) {
/* Authenticate by public key */
if(libssh2_userauth_publickey_fromfile(session, username, pubkeyfile, privkeyfile, password)) {
if(libssh2_userauth_publickey_fromfile(session,
username,
pubkeyfile,
privkeyfile,
password)) {
printf("\tAuthentication by public key failed!\n");
goto shutdown;
}
@@ -32,11 +32,13 @@ int test(LIBSSH2_SESSION *session)

if(type == LIBSSH2_HOSTKEY_TYPE_ECDSA_256) {
rc = libssh2_base64_decode(session, &expected_hostkey, &expected_len,
EXPECTED_ECDSA_HOSTKEY, strlen(EXPECTED_ECDSA_HOSTKEY));
EXPECTED_ECDSA_HOSTKEY,
strlen(EXPECTED_ECDSA_HOSTKEY));
}
else if(type == LIBSSH2_HOSTKEY_TYPE_RSA) {
rc = libssh2_base64_decode(session, &expected_hostkey, &expected_len,
EXPECTED_RSA_HOSTKEY, strlen(EXPECTED_RSA_HOSTKEY));
EXPECTED_RSA_HOSTKEY,
strlen(EXPECTED_RSA_HOSTKEY));
}
else {
fprintf(stderr, "Unexpected type of hostkey: %i\n", type);
@@ -49,7 +51,8 @@ int test(LIBSSH2_SESSION *session)
}

if(len != expected_len) {
fprintf(stderr, "Hostkey does not have the expected length %ld != %d\n",
fprintf(stderr,
"Hostkey does not have the expected length %ld != %d\n",
(unsigned long)len, expected_len);
return 1;
}
@@ -17,19 +17,23 @@ static const char *EXPECTED_ECDSA_HOSTKEY =
"AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBC+/syyeKJD9dC2ZH"
"9Q7iJGReR4YM3rUCMsSynkyXojdfSClGCMY7JvWlt30ESjYvxoTfSRGx6WvaqYK/vPoYQ4=";

static const char *EXPECTED_RSA_MD5_HASH_DIGEST = "0C0ED1A5BB10275F76924CE187CE5C5E";
static const char *EXPECTED_RSA_MD5_HASH_DIGEST =
"0C0ED1A5BB10275F76924CE187CE5C5E";

static const char *EXPECTED_RSA_SHA1_HASH_DIGEST =
"F3CD59E2913F4422B80F7B0A82B2B89EAE449387";

static const char *EXPECTED_RSA_SHA256_HASH_DIGEST = "92E3DA49DF3C7F99A828F505ED8239397A5D1F62914459760F878F7510F563A3";
static const char *EXPECTED_RSA_SHA256_HASH_DIGEST =
"92E3DA49DF3C7F99A828F505ED8239397A5D1F62914459760F878F7510F563A3";

static const char *EXPECTED_ECDSA_MD5_HASH_DIGEST = "0402E4D897580BBC911379CBD88BCD3D";
static const char *EXPECTED_ECDSA_MD5_HASH_DIGEST =
"0402E4D897580BBC911379CBD88BCD3D";

static const char *EXPECTED_ECDSA_SHA1_HASH_DIGEST =
"12FDAD1E3B31B10BABB00F2A8D1B9A62C326BD2F";

static const char *EXPECTED_ECDSA_SHA256_HASH_DIGEST = "56FCD975B166C3F0342D0036E44C311A86C0EAE40713B53FC776369BAE7F5264";
static const char *EXPECTED_ECDSA_SHA256_HASH_DIGEST =
"56FCD975B166C3F0342D0036E44C311A86C0EAE40713B53FC776369BAE7F5264";

static const int MD5_HASH_SIZE = 16;
static const int SHA1_HASH_SIZE = 20;
@@ -79,8 +83,9 @@ int test(LIBSSH2_SESSION *session)
calculate_digest(md5_hash, MD5_HASH_SIZE, buf, BUFSIZ);

if(strcmp(buf, EXPECTED_ECDSA_MD5_HASH_DIGEST) != 0) {
fprintf(stderr, "ECDSA MD5 hash not as expected - digest %s != %s\n", buf,
EXPECTED_ECDSA_MD5_HASH_DIGEST);
fprintf(stderr,
"ECDSA MD5 hash not as expected - digest %s != %s\n",
buf, EXPECTED_ECDSA_MD5_HASH_DIGEST);
return 1;
}

@@ -94,12 +99,14 @@ int test(LIBSSH2_SESSION *session)
calculate_digest(sha1_hash, SHA1_HASH_SIZE, buf, BUFSIZ);

if(strcmp(buf, EXPECTED_ECDSA_SHA1_HASH_DIGEST) != 0) {
fprintf(stderr, "ECDSA SHA1 hash not as expected - digest %s != %s\n", buf,
EXPECTED_ECDSA_SHA1_HASH_DIGEST);
fprintf(stderr,
"ECDSA SHA1 hash not as expected - digest %s != %s\n",
buf, EXPECTED_ECDSA_SHA1_HASH_DIGEST);
return 1;
}

sha256_hash = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA256);
sha256_hash = libssh2_hostkey_hash(session,
LIBSSH2_HOSTKEY_HASH_SHA256);
if(sha256_hash == NULL) {
print_last_session_error(
"libssh2_hostkey_hash(LIBSSH2_HOSTKEY_HASH_SHA256)");
@@ -109,8 +116,9 @@ int test(LIBSSH2_SESSION *session)
calculate_digest(sha256_hash, SHA256_HASH_SIZE, buf, BUFSIZ);

if(strcmp(buf, EXPECTED_ECDSA_SHA256_HASH_DIGEST) != 0) {
fprintf(stderr, "ECDSA SHA256 hash not as expected - digest %s != %s\n", buf,
EXPECTED_ECDSA_SHA256_HASH_DIGEST);
fprintf(stderr,
"ECDSA SHA256 hash not as expected - digest %s != %s\n",
buf, EXPECTED_ECDSA_SHA256_HASH_DIGEST);
return 1;
}

@@ -127,8 +135,9 @@ int test(LIBSSH2_SESSION *session)
calculate_digest(md5_hash, MD5_HASH_SIZE, buf, BUFSIZ);

if(strcmp(buf, EXPECTED_RSA_MD5_HASH_DIGEST) != 0) {
fprintf(stderr, "MD5 hash not as expected - digest %s != %s\n", buf,
EXPECTED_RSA_MD5_HASH_DIGEST);
fprintf(stderr,
"MD5 hash not as expected - digest %s != %s\n",
buf, EXPECTED_RSA_MD5_HASH_DIGEST);
return 1;
}

@@ -142,12 +151,14 @@ int test(LIBSSH2_SESSION *session)
calculate_digest(sha1_hash, SHA1_HASH_SIZE, buf, BUFSIZ);

if(strcmp(buf, EXPECTED_RSA_SHA1_HASH_DIGEST) != 0) {
fprintf(stderr, "SHA1 hash not as expected - digest %s != %s\n", buf,
EXPECTED_RSA_SHA1_HASH_DIGEST);
fprintf(stderr,
"SHA1 hash not as expected - digest %s != %s\n",
buf, EXPECTED_RSA_SHA1_HASH_DIGEST);
return 1;
}

sha256_hash = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA256);
sha256_hash = libssh2_hostkey_hash(session,
LIBSSH2_HOSTKEY_HASH_SHA256);
if(sha256_hash == NULL) {
print_last_session_error(
"libssh2_hostkey_hash(LIBSSH2_HOSTKEY_HASH_SHA256)");
@@ -157,8 +168,9 @@ int test(LIBSSH2_SESSION *session)
calculate_digest(sha256_hash, SHA256_HASH_SIZE, buf, BUFSIZ);

if(strcmp(buf, EXPECTED_RSA_SHA256_HASH_DIGEST) != 0) {
fprintf(stderr, "SHA256 hash not as expected - digest %s != %s\n", buf,
EXPECTED_RSA_SHA256_HASH_DIGEST);
fprintf(stderr,
"SHA256 hash not as expected - digest %s != %s\n",
buf, EXPECTED_RSA_SHA256_HASH_DIGEST);
return 1;
}
}
@@ -17,7 +17,8 @@ static void kbd_callback(const char *name, int name_len,
int i;
(void)abstract;
fprintf(stdout, "Kb-int name: %.*s\n", name_len, name);
fprintf(stdout, "Kb-int instruction: %.*s\n", instruction_len, instruction);
fprintf(stdout, "Kb-int instruction: %.*s\n",
instruction_len, instruction);
for(i = 0; i < num_prompts; ++i) {
fprintf(stdout, "Kb-int prompt %d: %.*s\n", i, prompts[i].length,
prompts[i].text);
@@ -4,8 +4,9 @@

#include <stdio.h>

static const char *USERNAME = "libssh2"; /* configured in Dockerfile */
static const char *PASSWORD = "my test password"; /* configured in Dockerfile */
/* configured in Dockerfile */
static const char *USERNAME = "libssh2";
static const char *PASSWORD = "my test password";

static void kbd_callback(const char *name, int name_len,
const char *instruction, int instruction_len,
@@ -18,7 +19,8 @@ static void kbd_callback(const char *name, int name_len,
(void)abstract;

fprintf(stdout, "Kb-int name: %.*s\n", name_len, name);
fprintf(stdout, "Kb-int instruction: %.*s\n", instruction_len, instruction);
fprintf(stdout, "Kb-int instruction: %.*s\n", instruction_len,
instruction);
for(i = 0; i < num_prompts; ++i) {
fprintf(stdout, "Kb-int prompt %d: %.*s\n", i, prompts[i].length,
prompts[i].text);
@@ -4,7 +4,8 @@

#include <stdio.h>

static const char *PASSWORD = "my test password"; /* configured in Dockerfile */
/* configured in Dockerfile */
static const char *PASSWORD = "my test password";
static const char *WRONG_USERNAME = "i dont exist";

int test(LIBSSH2_SESSION *session)
@@ -4,8 +4,9 @@

#include <stdio.h>

static const char *USERNAME = "libssh2"; /* configured in Dockerfile */
static const char *PASSWORD = "my test password"; /* configured in Dockerfile */
/* configured in Dockerfile */
static const char *USERNAME = "libssh2";
static const char *PASSWORD = "my test password";

int test(LIBSSH2_SESSION *session)
{
@@ -4,9 +4,10 @@

#include <stdio.h>

static const char *USERNAME = "libssh2"; /* configured in Dockerfile */
/* configured in Dockerfile */
static const char *USERNAME = "libssh2";
static const char *KEY_FILE_PRIVATE = "key_dsa";
static const char *KEY_FILE_PUBLIC = "key_dsa.pub"; /* configured in Dockerfile */
static const char *KEY_FILE_PUBLIC = "key_dsa.pub";

int test(LIBSSH2_SESSION *session)
{
@@ -4,9 +4,10 @@

#include <stdio.h>

static const char *USERNAME = "libssh2"; /* configured in Dockerfile */
/* configured in Dockerfile */
static const char *USERNAME = "libssh2";
static const char *KEY_FILE_PRIVATE = "key_ed25519";
static const char *KEY_FILE_PUBLIC = "key_ed25519.pub"; /* configured in Dockerfile */
static const char *KEY_FILE_PUBLIC = "key_ed25519.pub";

int test(LIBSSH2_SESSION *session)
{

0 comments on commit ba1c0ba

Please sign in to comment.
You can’t perform that action at this time.