From efeb9a41b28b5dd1fa7d451fa7bad4584683657e Mon Sep 17 00:00:00 2001 From: John Safranek Date: Tue, 13 Jun 2017 09:05:57 -0700 Subject: [PATCH 1/4] rename directory test as tests --- Makefile.am | 2 +- test/include.am | 14 -------------- tests/include.am | 14 ++++++++++++++ {test => tests}/unit.c | 0 4 files changed, 15 insertions(+), 15 deletions(-) delete mode 100644 test/include.am create mode 100644 tests/include.am rename {test => tests}/unit.c (100%) diff --git a/Makefile.am b/Makefile.am index fbecea9ed..c8b640654 100644 --- a/Makefile.am +++ b/Makefile.am @@ -33,7 +33,7 @@ EXTRA_DIST+= LICENSING include src/include.am include wolfssh/include.am include examples/include.am -include test/include.am +include tests/include.am include keys/include.am diff --git a/test/include.am b/test/include.am deleted file mode 100644 index 6e6032c66..000000000 --- a/test/include.am +++ /dev/null @@ -1,14 +0,0 @@ -# vim:ft=automake -# included from Top Level Makefile.am -# All paths should be given relative to the root - -check_PROGRAMS += test/unit.test -noinst_PROGRAMS += test/unit.test - -test_unit_test_SOURCES = test/unit.c - -test_unit_test_CFLAGS = -DNO_MAIN_DRIVER $(AM_CFLAGS) -test_unit_test_LDADD = src/libwolfssh.la $(LIB_STATIC_ADD) -test_unit_test_DEPENDENCIES = src/libwolfssh.la - -DISTCLEANFILES+= test/.libs/unit.test diff --git a/tests/include.am b/tests/include.am new file mode 100644 index 000000000..6e8495e2e --- /dev/null +++ b/tests/include.am @@ -0,0 +1,14 @@ +# vim:ft=automake +# included from Top Level Makefile.am +# All paths should be given relative to the root + +check_PROGRAMS += tests/unit.test +noinst_PROGRAMS += tests/unit.test + +tests_unit_test_SOURCES = tests/unit.c + +tests_unit_test_CFLAGS = -DNO_MAIN_DRIVER $(AM_CFLAGS) +tests_unit_test_LDADD = src/libwolfssh.la $(LIB_STATIC_ADD) +tests_unit_test_DEPENDENCIES = src/libwolfssh.la + +DISTCLEANFILES+= tests/.libs/unit.test diff --git a/test/unit.c b/tests/unit.c similarity index 100% rename from test/unit.c rename to tests/unit.c From c3d0c895d28da89af6f3dde5153ae5fc4f67f66a Mon Sep 17 00:00:00 2001 From: John Safranek Date: Tue, 13 Jun 2017 09:15:44 -0700 Subject: [PATCH 2/4] add stub for the API test --- tests/api.c | 28 ++++++++++++++++++++++++++++ tests/include.am | 2 +- tests/unit.c | 5 +++++ tests/unit.h | 37 +++++++++++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 tests/api.c create mode 100644 tests/unit.h diff --git a/tests/api.c b/tests/api.c new file mode 100644 index 000000000..2f52ffde1 --- /dev/null +++ b/tests/api.c @@ -0,0 +1,28 @@ +/* api.c + * + * Copyright (C) 2014-2017 wolfSSL Inc. + * + * This file is part of wolfSSH. + * + * wolfSSH is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * wolfSSH is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with wolfSSH. If not, see . + */ + + +#include + + +int ApiTest(void) +{ + return 0; +} diff --git a/tests/include.am b/tests/include.am index 6e8495e2e..5e549e7a7 100644 --- a/tests/include.am +++ b/tests/include.am @@ -5,7 +5,7 @@ check_PROGRAMS += tests/unit.test noinst_PROGRAMS += tests/unit.test -tests_unit_test_SOURCES = tests/unit.c +tests_unit_test_SOURCES = tests/unit.c tests/api.c tests_unit_test_CFLAGS = -DNO_MAIN_DRIVER $(AM_CFLAGS) tests_unit_test_LDADD = src/libwolfssh.la $(LIB_STATIC_ADD) diff --git a/tests/unit.c b/tests/unit.c index d2a939994..730fa5e97 100644 --- a/tests/unit.c +++ b/tests/unit.c @@ -23,6 +23,7 @@ #include #include #include +#include /* Utility functions */ @@ -393,6 +394,10 @@ int main(void) testResult = testResult || unitResult; #endif + unitResult = ApiTest(); + printf("API: %s\n", (unitResult == 0 ? "SUCCESS" : "FAILED")); + testResult = testResult || unitResult; + return (testResult ? 1 : 0); } diff --git a/tests/unit.h b/tests/unit.h new file mode 100644 index 000000000..1676076e8 --- /dev/null +++ b/tests/unit.h @@ -0,0 +1,37 @@ +/* unit.h + * + * Copyright (C) 2014-2016 wolfSSL Inc. + * + * This file is part of wolfSSH. + * + * wolfSSH is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * wolfSSH is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with wolfSSH. If not, see . + */ + + +/* + * The unit module contains the main driver for the wolfSSH unit tests. + */ + + +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +int ApiTest(void); + +#ifdef __cplusplus +} +#endif From 24b19daa1652301716f255ff531623d90a5bd54d Mon Sep 17 00:00:00 2001 From: John Safranek Date: Tue, 13 Jun 2017 10:35:11 -0700 Subject: [PATCH 3/4] 1. Copied over some of the test infrastructure from wolfSSL. 2. Added API test cases for only wolfSSH_Init and wolfSSH_Cleanup. --- tests/api.c | 38 ++++++++++++++++++++++++++++++++ tests/unit.c | 2 +- tests/unit.h | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+), 1 deletion(-) diff --git a/tests/api.c b/tests/api.c index 2f52ffde1..095407bf1 100644 --- a/tests/api.c +++ b/tests/api.c @@ -19,10 +19,48 @@ */ +#include #include +#define TEST_SUCCESS (1) +#define TEST_FAIL (0) + +#define testingFmt " %s:" +#define resultFmt " %s\n" +static const char* passed = "passed"; +static const char* failed = "failed"; + + +static int test_wolfSSH_Init(void) +{ + int result; + + printf(testingFmt, "wolfSSH_Init()"); + result = wolfSSH_Init(); + printf(resultFmt, result == WS_SUCCESS ? passed : failed); + + return result; +} + + +static int test_wolfSSH_Cleanup(void) +{ + int result; + + printf(testingFmt, "wolfSSH_Cleanup()"); + result = wolfSSH_Cleanup(); + printf(resultFmt, result == WS_SUCCESS ? passed : failed); + + return result; +} + + int ApiTest(void) { + printf(" Begin API Tests\n"); + AssertIntEQ(test_wolfSSH_Init(), WS_SUCCESS); + AssertIntEQ(test_wolfSSH_Cleanup(), WS_SUCCESS); + printf(" End API Tests\n"); return 0; } diff --git a/tests/unit.c b/tests/unit.c index 730fa5e97..c9cb82134 100644 --- a/tests/unit.c +++ b/tests/unit.c @@ -394,8 +394,8 @@ int main(void) testResult = testResult || unitResult; #endif + printf("Running the API tests\n"); unitResult = ApiTest(); - printf("API: %s\n", (unitResult == 0 ? "SUCCESS" : "FAILED")); testResult = testResult || unitResult; return (testResult ? 1 : 0); diff --git a/tests/unit.h b/tests/unit.h index 1676076e8..1e43b9455 100644 --- a/tests/unit.h +++ b/tests/unit.h @@ -26,12 +26,74 @@ #pragma once +#ifndef WOLFSSH_TESTS_UNIT_H +#define WOLFSSH_TESTS_UNIT_H + + +#include + + #ifdef __cplusplus extern "C" { #endif + +#define Fail(description, result) do { \ + printf("\nERROR - %s line %d failed with:", __FILE__, __LINE__); \ + printf("\n expected: "); printf description; \ + printf("\n result: "); printf result; printf("\n\n"); \ + abort(); \ +} while(0) + +#define Assert(test, description, result) if (!(test)) Fail(description, result) + +#define AssertTrue(x) Assert( (x), ("%s is true", #x), (#x " => FALSE")) +#define AssertFalse(x) Assert(!(x), ("%s is false", #x), (#x " => TRUE")) +#define AssertNotNull(x) Assert( (x), ("%s is not null", #x), (#x " => NULL")) + +#define AssertNull(x) do { \ + void* _x = (void *) (x); \ + \ + Assert(!_x, ("%s is null", #x), (#x " => %p", _x)); \ +} while(0) + +#define AssertInt(x, y, op, er) do { \ + int _x = x; \ + int _y = y; \ + \ + Assert(_x op _y, ("%s " #op " %s", #x, #y), ("%d " #er " %d", _x, _y)); \ +} while(0) + +#define AssertIntEQ(x, y) AssertInt(x, y, ==, !=) +#define AssertIntNE(x, y) AssertInt(x, y, !=, ==) +#define AssertIntGT(x, y) AssertInt(x, y, >, <=) +#define AssertIntLT(x, y) AssertInt(x, y, <, >=) +#define AssertIntGE(x, y) AssertInt(x, y, >=, <) +#define AssertIntLE(x, y) AssertInt(x, y, <=, >) + +#define AssertStr(x, y, op, er) do { \ + const char* _x = x; \ + const char* _y = y; \ + int _z = strcmp(_x, _y); \ + \ + Assert(_z op 0, ("%s " #op " %s", #x, #y), \ + ("\"%s\" " #er " \"%s\"", _x, _y));\ +} while(0) + +#define AssertStrEQ(x, y) AssertStr(x, y, ==, !=) +#define AssertStrNE(x, y) AssertStr(x, y, !=, ==) +#define AssertStrGT(x, y) AssertStr(x, y, >, <=) +#define AssertStrLT(x, y) AssertStr(x, y, <, >=) +#define AssertStrGE(x, y) AssertStr(x, y, >=, <) +#define AssertStrLE(x, y) AssertStr(x, y, <=, >) + + int ApiTest(void); + #ifdef __cplusplus } #endif + + +#endif From 9c8a77c240612652303a60999f0e3f6a3f16dc97 Mon Sep 17 00:00:00 2001 From: John Safranek Date: Wed, 14 Jun 2017 09:09:19 -0700 Subject: [PATCH 4/4] 1. Add a separate API test. 2. Eliminate the unit.h file, included into api.c. --- tests/api.c | 120 +++++++++++++++++++++++++++++++++++++---------- tests/include.am | 14 ++++-- tests/unit.c | 5 -- tests/unit.h | 99 -------------------------------------- 4 files changed, 105 insertions(+), 133 deletions(-) delete mode 100644 tests/unit.h diff --git a/tests/api.c b/tests/api.c index 095407bf1..923d2bd6e 100644 --- a/tests/api.c +++ b/tests/api.c @@ -19,48 +19,120 @@ */ +#include #include -#include -#define TEST_SUCCESS (1) -#define TEST_FAIL (0) +#define Fail(description, result) do { \ + printf("\nERROR - %s line %d failed with:", __FILE__, __LINE__); \ + printf("\n expected: "); printf description; \ + printf("\n result: "); printf result; printf("\n\n"); \ + abort(); \ +} while(0) -#define testingFmt " %s:" -#define resultFmt " %s\n" -static const char* passed = "passed"; -static const char* failed = "failed"; +#define Assert(test, description, result) if (!(test)) Fail(description, result) +#define AssertTrue(x) Assert( (x), ("%s is true", #x), (#x " => FALSE")) +#define AssertFalse(x) Assert(!(x), ("%s is false", #x), (#x " => TRUE")) +#define AssertNotNull(x) Assert( (x), ("%s is not null", #x), (#x " => NULL")) -static int test_wolfSSH_Init(void) +#define AssertNull(x) do { \ + void* _x = (void *) (x); \ + \ + Assert(!_x, ("%s is null", #x), (#x " => %p", _x)); \ +} while(0) + +#define AssertInt(x, y, op, er) do { \ + int _x = x; \ + int _y = y; \ + \ + Assert(_x op _y, ("%s " #op " %s", #x, #y), ("%d " #er " %d", _x, _y)); \ +} while(0) + +#define AssertIntEQ(x, y) AssertInt(x, y, ==, !=) +#define AssertIntNE(x, y) AssertInt(x, y, !=, ==) +#define AssertIntGT(x, y) AssertInt(x, y, >, <=) +#define AssertIntLT(x, y) AssertInt(x, y, <, >=) +#define AssertIntGE(x, y) AssertInt(x, y, >=, <) +#define AssertIntLE(x, y) AssertInt(x, y, <=, >) + +#define AssertStr(x, y, op, er) do { \ + const char* _x = x; \ + const char* _y = y; \ + int _z = strcmp(_x, _y); \ + \ + Assert(_z op 0, ("%s " #op " %s", #x, #y), \ + ("\"%s\" " #er " \"%s\"", _x, _y));\ +} while(0) + +#define AssertStrEQ(x, y) AssertStr(x, y, ==, !=) +#define AssertStrNE(x, y) AssertStr(x, y, !=, ==) +#define AssertStrGT(x, y) AssertStr(x, y, >, <=) +#define AssertStrLT(x, y) AssertStr(x, y, <, >=) +#define AssertStrGE(x, y) AssertStr(x, y, >=, <) +#define AssertStrLE(x, y) AssertStr(x, y, <=, >) + + +enum WS_TestEndpointTypes { + TEST_GOOD_ENDPOINT_SERVER = WOLFSSH_ENDPOINT_SERVER, + TEST_GOOD_ENDPOINT_CLIENT = WOLFSSH_ENDPOINT_CLIENT, + TEST_BAD_ENDPOINT_NEXT, + TEST_BAD_ENDPOINT_LAST = 255 +}; + +static void test_wolfSSH_CTX_new(void) +{ + WOLFSSH_CTX* ctx; + + AssertNull(ctx = wolfSSH_CTX_new(TEST_BAD_ENDPOINT_NEXT, NULL)); + wolfSSH_CTX_free(ctx); + + AssertNull(ctx = wolfSSH_CTX_new(TEST_BAD_ENDPOINT_LAST, NULL)); + wolfSSH_CTX_free(ctx); + + AssertNotNull(ctx = wolfSSH_CTX_new(TEST_GOOD_ENDPOINT_SERVER, NULL)); + wolfSSH_CTX_free(ctx); + + AssertNotNull(ctx = wolfSSH_CTX_new(TEST_GOOD_ENDPOINT_CLIENT, NULL)); + wolfSSH_CTX_free(ctx); +} + + +static void test_server_wolfSSH_new(void) { - int result; + WOLFSSH_CTX* ctx; + WOLFSSH* ssh; - printf(testingFmt, "wolfSSH_Init()"); - result = wolfSSH_Init(); - printf(resultFmt, result == WS_SUCCESS ? passed : failed); + AssertNotNull(ctx = wolfSSH_CTX_new(WOLFSSH_ENDPOINT_SERVER, NULL)); + AssertNotNull(ssh = wolfSSH_new(ctx)); - return result; + wolfSSH_free(ssh); + wolfSSH_CTX_free(ctx); } -static int test_wolfSSH_Cleanup(void) +static void test_client_wolfSSH_new(void) { - int result; + WOLFSSH_CTX* ctx; + WOLFSSH* ssh; - printf(testingFmt, "wolfSSH_Cleanup()"); - result = wolfSSH_Cleanup(); - printf(resultFmt, result == WS_SUCCESS ? passed : failed); + AssertNotNull(ctx = wolfSSH_CTX_new(WOLFSSH_ENDPOINT_CLIENT, NULL)); + AssertNotNull(ssh = wolfSSH_new(ctx)); - return result; + wolfSSH_free(ssh); + wolfSSH_CTX_free(ctx); } -int ApiTest(void) +int main(void) { - printf(" Begin API Tests\n"); - AssertIntEQ(test_wolfSSH_Init(), WS_SUCCESS); - AssertIntEQ(test_wolfSSH_Cleanup(), WS_SUCCESS); - printf(" End API Tests\n"); + AssertIntEQ(wolfSSH_Init(), WS_SUCCESS); + + test_wolfSSH_CTX_new(); + test_server_wolfSSH_new(); + test_client_wolfSSH_new(); + + AssertIntEQ(wolfSSH_Cleanup(), WS_SUCCESS); + return 0; } diff --git a/tests/include.am b/tests/include.am index 5e549e7a7..162b91be0 100644 --- a/tests/include.am +++ b/tests/include.am @@ -2,13 +2,17 @@ # included from Top Level Makefile.am # All paths should be given relative to the root -check_PROGRAMS += tests/unit.test -noinst_PROGRAMS += tests/unit.test - -tests_unit_test_SOURCES = tests/unit.c tests/api.c +check_PROGRAMS += tests/unit.test tests/api.test +noinst_PROGRAMS += tests/unit.test tests/api.test +tests_unit_test_SOURCES = tests/unit.c tests_unit_test_CFLAGS = -DNO_MAIN_DRIVER $(AM_CFLAGS) tests_unit_test_LDADD = src/libwolfssh.la $(LIB_STATIC_ADD) tests_unit_test_DEPENDENCIES = src/libwolfssh.la -DISTCLEANFILES+= tests/.libs/unit.test +tests_api_test_SOURCES = tests/api.c +tests_api_test_CFLAGS = -DNO_MAIN_DRIVER $(AM_CFLAGS) +tests_api_test_LDADD = src/libwolfssh.la $(LIB_STATIC_ADD) +tests_api_test_DEPENDENCIES = src/libwolfssh.la + +DISTCLEANFILES+= tests/.libs/unit.test tests/.libs/api.test diff --git a/tests/unit.c b/tests/unit.c index c9cb82134..d2a939994 100644 --- a/tests/unit.c +++ b/tests/unit.c @@ -23,7 +23,6 @@ #include #include #include -#include /* Utility functions */ @@ -394,10 +393,6 @@ int main(void) testResult = testResult || unitResult; #endif - printf("Running the API tests\n"); - unitResult = ApiTest(); - testResult = testResult || unitResult; - return (testResult ? 1 : 0); } diff --git a/tests/unit.h b/tests/unit.h deleted file mode 100644 index 1e43b9455..000000000 --- a/tests/unit.h +++ /dev/null @@ -1,99 +0,0 @@ -/* unit.h - * - * Copyright (C) 2014-2016 wolfSSL Inc. - * - * This file is part of wolfSSH. - * - * wolfSSH is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * wolfSSH is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with wolfSSH. If not, see . - */ - - -/* - * The unit module contains the main driver for the wolfSSH unit tests. - */ - - -#pragma once - -#ifndef WOLFSSH_TESTS_UNIT_H -#define WOLFSSH_TESTS_UNIT_H - - -#include - - -#ifdef __cplusplus -extern "C" { -#endif - - -#define Fail(description, result) do { \ - printf("\nERROR - %s line %d failed with:", __FILE__, __LINE__); \ - printf("\n expected: "); printf description; \ - printf("\n result: "); printf result; printf("\n\n"); \ - abort(); \ -} while(0) - -#define Assert(test, description, result) if (!(test)) Fail(description, result) - -#define AssertTrue(x) Assert( (x), ("%s is true", #x), (#x " => FALSE")) -#define AssertFalse(x) Assert(!(x), ("%s is false", #x), (#x " => TRUE")) -#define AssertNotNull(x) Assert( (x), ("%s is not null", #x), (#x " => NULL")) - -#define AssertNull(x) do { \ - void* _x = (void *) (x); \ - \ - Assert(!_x, ("%s is null", #x), (#x " => %p", _x)); \ -} while(0) - -#define AssertInt(x, y, op, er) do { \ - int _x = x; \ - int _y = y; \ - \ - Assert(_x op _y, ("%s " #op " %s", #x, #y), ("%d " #er " %d", _x, _y)); \ -} while(0) - -#define AssertIntEQ(x, y) AssertInt(x, y, ==, !=) -#define AssertIntNE(x, y) AssertInt(x, y, !=, ==) -#define AssertIntGT(x, y) AssertInt(x, y, >, <=) -#define AssertIntLT(x, y) AssertInt(x, y, <, >=) -#define AssertIntGE(x, y) AssertInt(x, y, >=, <) -#define AssertIntLE(x, y) AssertInt(x, y, <=, >) - -#define AssertStr(x, y, op, er) do { \ - const char* _x = x; \ - const char* _y = y; \ - int _z = strcmp(_x, _y); \ - \ - Assert(_z op 0, ("%s " #op " %s", #x, #y), \ - ("\"%s\" " #er " \"%s\"", _x, _y));\ -} while(0) - -#define AssertStrEQ(x, y) AssertStr(x, y, ==, !=) -#define AssertStrNE(x, y) AssertStr(x, y, !=, ==) -#define AssertStrGT(x, y) AssertStr(x, y, >, <=) -#define AssertStrLT(x, y) AssertStr(x, y, <, >=) -#define AssertStrGE(x, y) AssertStr(x, y, >=, <) -#define AssertStrLE(x, y) AssertStr(x, y, <=, >) - - -int ApiTest(void); - - -#ifdef __cplusplus -} -#endif - - -#endif