From a92bcdc074a5cccc6d6796273f8acc86b5b1c911 Mon Sep 17 00:00:00 2001 From: Jacob Barthelmeh Date: Wed, 6 Feb 2019 13:45:18 -0700 Subject: [PATCH] add test cases for more SFTP commands --- examples/sftpclient/sftpclient.c | 32 ++++++++++++--------- tests/sftp.c | 49 ++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 14 deletions(-) diff --git a/examples/sftpclient/sftpclient.c b/examples/sftpclient/sftpclient.c index f8566569..fa925a0a 100644 --- a/examples/sftpclient/sftpclient.c +++ b/examples/sftpclient/sftpclient.c @@ -39,6 +39,7 @@ static WOLFSSH* ssh = NULL; static char* workingDir; #define fin stdin #define fout stdout +#define MAX_CMD_SZ 7 static void myStatusCb(WOLFSSH* sshIn, long bytes, char* name) @@ -338,6 +339,7 @@ static INLINE char* SFTP_FGETS(func_args* args, char* msg, int msgSz) { char* ret = NULL; + WMEMSET(msg, 0, msgSz); if (args && args->sftp_cb) { if (args->sftp_cb(NULL, msg, msgSz) == 0) ret = msg; @@ -363,6 +365,8 @@ int doCmds(func_args* args) if (SFTP_FPUTS(args, "wolfSSH sftp> ") < 0) err_sys("fputs error"); + + WMEMSET(msg, 0, sizeof(msg)); if (SFTP_FGETS(args, msg, sizeof(msg) - 1) == NULL) err_sys("fgets error"); msg[WOLFSSH_MAX_FILENAME * 2 - 1] = '\0'; @@ -409,11 +413,11 @@ int doCmds(func_args* args) continue; } - if ((pt = WSTRNSTR(msg, "reget", sizeof(msg))) != NULL) { + if ((pt = WSTRNSTR(msg, "reget", MAX_CMD_SZ)) != NULL) { resume = 1; } - if ((pt = WSTRNSTR(msg, "get", sizeof(msg))) != NULL) { + if ((pt = WSTRNSTR(msg, "get", MAX_CMD_SZ)) != NULL) { int sz; char* f = NULL; char* to = NULL; @@ -495,11 +499,11 @@ int doCmds(func_args* args) } - if ((pt = WSTRNSTR(msg, "reput", sizeof(msg))) != NULL) { + if ((pt = WSTRNSTR(msg, "reput", MAX_CMD_SZ)) != NULL) { resume = 1; } - if ((pt = WSTRNSTR(msg, "put", sizeof(msg))) != NULL) { + if ((pt = WSTRNSTR(msg, "put", MAX_CMD_SZ)) != NULL) { int sz; char* f = NULL; char* to = NULL; @@ -578,7 +582,7 @@ int doCmds(func_args* args) continue; } - if ((pt = WSTRNSTR(msg, "cd", sizeof(msg))) != NULL) { + if ((pt = WSTRNSTR(msg, "cd", MAX_CMD_SZ)) != NULL) { WS_SFTP_FILEATRB atrb; int sz; char* f = NULL; @@ -631,7 +635,7 @@ int doCmds(func_args* args) continue; } - if ((pt = WSTRNSTR(msg, "chmod", sizeof(msg))) != NULL) { + if ((pt = WSTRNSTR(msg, "chmod", MAX_CMD_SZ)) != NULL) { int sz; char* f = NULL; char mode[WOLFSSH_MAX_OCTET_LEN]; @@ -692,7 +696,7 @@ int doCmds(func_args* args) continue; } - if ((pt = WSTRNSTR(msg, "rmdir", sizeof(msg))) != NULL) { + if ((pt = WSTRNSTR(msg, "rmdir", MAX_CMD_SZ)) != NULL) { int sz; char* f = NULL; @@ -737,7 +741,7 @@ int doCmds(func_args* args) } - if ((pt = WSTRNSTR(msg, "rm", sizeof(msg))) != NULL) { + if ((pt = WSTRNSTR(msg, "rm", MAX_CMD_SZ)) != NULL) { int sz; char* f = NULL; @@ -778,7 +782,7 @@ int doCmds(func_args* args) continue; } - if ((pt = WSTRNSTR(msg, "rename", sizeof(msg))) != NULL) { + if ((pt = WSTRNSTR(msg, "rename", MAX_CMD_SZ)) != NULL) { int sz; char* f = NULL; char* fTo = NULL; @@ -850,7 +854,7 @@ int doCmds(func_args* args) } - if ((pt = WSTRNSTR(msg, "ls", sizeof(msg))) != NULL) { + if ((pt = WSTRNSTR(msg, "ls", MAX_CMD_SZ)) != NULL) { WS_SFTPNAME* tmp; WS_SFTPNAME* current; @@ -872,24 +876,24 @@ int doCmds(func_args* args) } /* display current working directory */ - if ((pt = WSTRNSTR(msg, "pwd", sizeof(msg))) != NULL) { + if ((pt = WSTRNSTR(msg, "pwd", MAX_CMD_SZ)) != NULL) { if (SFTP_FPUTS(args, workingDir) < 0 || SFTP_FPUTS(args, "\n") < 0) err_sys("fputs error"); continue; } - if (WSTRNSTR(msg, "help", sizeof(msg)) != NULL) { + if (WSTRNSTR(msg, "help", MAX_CMD_SZ) != NULL) { ShowCommands(); continue; } - if (WSTRNSTR(msg, "quit", sizeof(msg)) != NULL) { + if (WSTRNSTR(msg, "quit", MAX_CMD_SZ) != NULL) { quit = 1; continue; } - if (WSTRNSTR(msg, "exit", sizeof(msg)) != NULL) { + if (WSTRNSTR(msg, "exit", MAX_CMD_SZ) != NULL) { quit = 1; continue; } diff --git a/tests/sftp.c b/tests/sftp.c index f2ab5102..eb3ab8cf 100644 --- a/tests/sftp.c +++ b/tests/sftp.c @@ -33,10 +33,54 @@ #if defined(WOLFSSH_SFTP) && !defined(SINGLE_THREADED) static const char* cmds[] = { + "mkdir a", + "cd a", + "pwd", "ls", + "put configure", + "ls", + "get configure test-get", + "rm configure", + "cd ../", + "ls", + "rename test-get test-get-2", + "rmdir a", + "ls", + "rm test-get-2", "exit" }; static int commandIdx = 0; +static char inBuf[1024] = {0}; + +/* return 0 on success */ +static int Expected(int command) +{ + switch (command) { + case 4: + { + char expt[] = ".\n..\nwolfSSH sftp> "; + return WMEMCMP(expt, inBuf, sizeof(expt)); + } + + case 6: + { + char expt[] = ".\n..\nconfigure\nwolfSSH sftp> "; + return WMEMCMP(expt, inBuf, sizeof(expt)); + } + + case 10: + return (WSTRNSTR(inBuf, "test-get", sizeof(inBuf)) == NULL); + + case 13: + return (WSTRNSTR(inBuf, "test-get-2", sizeof(inBuf)) == NULL); + + default: + break; + } + WMEMSET(inBuf, 0, sizeof(inBuf)); + return 0; +} + static int commandCb(const char* in, char* out, int outSz) { @@ -44,6 +88,7 @@ static int commandCb(const char* in, char* out, int outSz) if (in) { /* print out */ + WSTRNCAT(inBuf, in, sizeof(inBuf)); } /* get command input */ @@ -55,6 +100,10 @@ static int commandCb(const char* in, char* out, int outSz) else { WMEMCPY(out, cmds[commandIdx], sz); } + + if (Expected(commandIdx) != 0) { + exit(1); /* abort out */ + } commandIdx++; } return ret;