Skip to content
Permalink
Browse files

tests: check test suite syntax with checksrc.pl

  • Loading branch information...
tiennou committed Jul 16, 2019
1 parent 15e216f commit 36f380425ab3407b7f592b98bbc1048ae454d56e
@@ -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
@@ -83,7 +83,7 @@ static int run_command_varg(char **output, const char *command, va_list args)
return -1;
}

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

fprintf(stdout, "Command: %s\n", command);
#ifdef WIN32
@@ -136,7 +136,9 @@ static int run_command(char **output, const char *command, ...)

static int build_openssh_server_docker_image()
{
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)
@@ -167,7 +169,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 +222,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 +254,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)
{
@@ -35,8 +35,11 @@ int test(LIBSSH2_SESSION *session)
return 1;
}

rc = libssh2_userauth_publickey_frommemory(session, USERNAME, strlen(USERNAME),
NULL, 0, buffer, len, NULL);
rc = libssh2_userauth_publickey_frommemory(session,
USERNAME, strlen(USERNAME),
NULL, 0,
buffer, len,
NULL);

free(buffer);

@@ -66,8 +69,8 @@ int read_file(const char *path, char **out_buffer, size_t *out_len)
fp = fopen(path, "r");

if(!fp) {
fprintf(stderr, "File could not be read.");
return 1;
fprintf(stderr, "File could not be read.");
return 1;
}

fseek(fp, 0L, SEEK_END);
@@ -76,16 +79,16 @@ int read_file(const char *path, char **out_buffer, size_t *out_len)

buffer = calloc(1, len + 1);
if(!buffer) {
fclose(fp);
fprintf(stderr, "Could not alloc memory.");
return 1;
fclose(fp);
fprintf(stderr, "Could not alloc memory.");
return 1;
}

if(1 != fread(buffer, len, 1, fp)) {
fclose(fp);
free(buffer);
fprintf(stderr, "Could not read file into memory.");
return 1;
fclose(fp);
free(buffer);
fprintf(stderr, "Could not read file into memory.");
return 1;
}

fclose(fp);
@@ -4,10 +4,11 @@

#include <stdio.h>

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

int test(LIBSSH2_SESSION *session)
{

0 comments on commit 36f3804

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