|
| 1 | +/* api.c |
| 2 | + * |
| 3 | + * Copyright (C) 2014-2017 wolfSSL Inc. |
| 4 | + * |
| 5 | + * This file is part of wolfSSH. |
| 6 | + * |
| 7 | + * wolfSSH is free software; you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation; either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * wolfSSH is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with wolfSSH. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | + |
| 21 | + |
| 22 | +#include <stdio.h> |
| 23 | +#include <wolfssh/ssh.h> |
| 24 | + |
| 25 | + |
| 26 | +#define Fail(description, result) do { \ |
| 27 | + printf("\nERROR - %s line %d failed with:", __FILE__, __LINE__); \ |
| 28 | + printf("\n expected: "); printf description; \ |
| 29 | + printf("\n result: "); printf result; printf("\n\n"); \ |
| 30 | + abort(); \ |
| 31 | +} while(0) |
| 32 | + |
| 33 | +#define Assert(test, description, result) if (!(test)) Fail(description, result) |
| 34 | + |
| 35 | +#define AssertTrue(x) Assert( (x), ("%s is true", #x), (#x " => FALSE")) |
| 36 | +#define AssertFalse(x) Assert(!(x), ("%s is false", #x), (#x " => TRUE")) |
| 37 | +#define AssertNotNull(x) Assert( (x), ("%s is not null", #x), (#x " => NULL")) |
| 38 | + |
| 39 | +#define AssertNull(x) do { \ |
| 40 | + void* _x = (void *) (x); \ |
| 41 | + \ |
| 42 | + Assert(!_x, ("%s is null", #x), (#x " => %p", _x)); \ |
| 43 | +} while(0) |
| 44 | + |
| 45 | +#define AssertInt(x, y, op, er) do { \ |
| 46 | + int _x = x; \ |
| 47 | + int _y = y; \ |
| 48 | + \ |
| 49 | + Assert(_x op _y, ("%s " #op " %s", #x, #y), ("%d " #er " %d", _x, _y)); \ |
| 50 | +} while(0) |
| 51 | + |
| 52 | +#define AssertIntEQ(x, y) AssertInt(x, y, ==, !=) |
| 53 | +#define AssertIntNE(x, y) AssertInt(x, y, !=, ==) |
| 54 | +#define AssertIntGT(x, y) AssertInt(x, y, >, <=) |
| 55 | +#define AssertIntLT(x, y) AssertInt(x, y, <, >=) |
| 56 | +#define AssertIntGE(x, y) AssertInt(x, y, >=, <) |
| 57 | +#define AssertIntLE(x, y) AssertInt(x, y, <=, >) |
| 58 | + |
| 59 | +#define AssertStr(x, y, op, er) do { \ |
| 60 | + const char* _x = x; \ |
| 61 | + const char* _y = y; \ |
| 62 | + int _z = strcmp(_x, _y); \ |
| 63 | + \ |
| 64 | + Assert(_z op 0, ("%s " #op " %s", #x, #y), \ |
| 65 | + ("\"%s\" " #er " \"%s\"", _x, _y));\ |
| 66 | +} while(0) |
| 67 | + |
| 68 | +#define AssertStrEQ(x, y) AssertStr(x, y, ==, !=) |
| 69 | +#define AssertStrNE(x, y) AssertStr(x, y, !=, ==) |
| 70 | +#define AssertStrGT(x, y) AssertStr(x, y, >, <=) |
| 71 | +#define AssertStrLT(x, y) AssertStr(x, y, <, >=) |
| 72 | +#define AssertStrGE(x, y) AssertStr(x, y, >=, <) |
| 73 | +#define AssertStrLE(x, y) AssertStr(x, y, <=, >) |
| 74 | + |
| 75 | + |
| 76 | +enum WS_TestEndpointTypes { |
| 77 | + TEST_GOOD_ENDPOINT_SERVER = WOLFSSH_ENDPOINT_SERVER, |
| 78 | + TEST_GOOD_ENDPOINT_CLIENT = WOLFSSH_ENDPOINT_CLIENT, |
| 79 | + TEST_BAD_ENDPOINT_NEXT, |
| 80 | + TEST_BAD_ENDPOINT_LAST = 255 |
| 81 | +}; |
| 82 | + |
| 83 | +static void test_wolfSSH_CTX_new(void) |
| 84 | +{ |
| 85 | + WOLFSSH_CTX* ctx; |
| 86 | + |
| 87 | + AssertNull(ctx = wolfSSH_CTX_new(TEST_BAD_ENDPOINT_NEXT, NULL)); |
| 88 | + wolfSSH_CTX_free(ctx); |
| 89 | + |
| 90 | + AssertNull(ctx = wolfSSH_CTX_new(TEST_BAD_ENDPOINT_LAST, NULL)); |
| 91 | + wolfSSH_CTX_free(ctx); |
| 92 | + |
| 93 | + AssertNotNull(ctx = wolfSSH_CTX_new(TEST_GOOD_ENDPOINT_SERVER, NULL)); |
| 94 | + wolfSSH_CTX_free(ctx); |
| 95 | + |
| 96 | + AssertNotNull(ctx = wolfSSH_CTX_new(TEST_GOOD_ENDPOINT_CLIENT, NULL)); |
| 97 | + wolfSSH_CTX_free(ctx); |
| 98 | +} |
| 99 | + |
| 100 | + |
| 101 | +static void test_server_wolfSSH_new(void) |
| 102 | +{ |
| 103 | + WOLFSSH_CTX* ctx; |
| 104 | + WOLFSSH* ssh; |
| 105 | + |
| 106 | + AssertNotNull(ctx = wolfSSH_CTX_new(WOLFSSH_ENDPOINT_SERVER, NULL)); |
| 107 | + AssertNotNull(ssh = wolfSSH_new(ctx)); |
| 108 | + |
| 109 | + wolfSSH_free(ssh); |
| 110 | + wolfSSH_CTX_free(ctx); |
| 111 | +} |
| 112 | + |
| 113 | + |
| 114 | +static void test_client_wolfSSH_new(void) |
| 115 | +{ |
| 116 | + WOLFSSH_CTX* ctx; |
| 117 | + WOLFSSH* ssh; |
| 118 | + |
| 119 | + AssertNotNull(ctx = wolfSSH_CTX_new(WOLFSSH_ENDPOINT_CLIENT, NULL)); |
| 120 | + AssertNotNull(ssh = wolfSSH_new(ctx)); |
| 121 | + |
| 122 | + wolfSSH_free(ssh); |
| 123 | + wolfSSH_CTX_free(ctx); |
| 124 | +} |
| 125 | + |
| 126 | + |
| 127 | +int main(void) |
| 128 | +{ |
| 129 | + AssertIntEQ(wolfSSH_Init(), WS_SUCCESS); |
| 130 | + |
| 131 | + test_wolfSSH_CTX_new(); |
| 132 | + test_server_wolfSSH_new(); |
| 133 | + test_client_wolfSSH_new(); |
| 134 | + |
| 135 | + AssertIntEQ(wolfSSH_Cleanup(), WS_SUCCESS); |
| 136 | + |
| 137 | + return 0; |
| 138 | +} |
0 commit comments