Skip to content

Commit 3aaa0d4

Browse files
committed
SFTP List
1. Add some more test cases to the RealPath() test. 2. Change the API for the function wolfSSH_RealPath(). It doesn't need the currentPath. Non-absolute paths are relative to defaultPath. 3. If defaultPath isn't present, use "/".
1 parent 61492ec commit 3aaa0d4

File tree

3 files changed

+58
-37
lines changed

3 files changed

+58
-37
lines changed

src/ssh.c

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2275,51 +2275,47 @@ int wolfSSH_ChannelGetEof(WOLFSSH_CHANNEL* channel)
22752275
#if (defined(WOLFSSH_SFTP) || defined(WOLFSSH_SCP)) && \
22762276
!defined(NO_WOLFSSH_SERVER)
22772277
/*
2278-
* Paths starting with a slash are absolute, rooted at root. Any path that
2279-
* doesn't have a starting slash is assumed to be relative to the current
2278+
* Paths starting with a slash are absolute, rooted at "/". Any path that
2279+
* doesn't have a starting slash is assumed to be relative to the default
22802280
* path. If the path is empty, return the default path.
22812281
*
22822282
* The path "/." is stripped out. The path "/.." strips out the previous
22832283
* path value. The root path, "/", is always present.
22842284
*
22852285
* Example: "/home/fred/frob/frizz/../../../barney/bar/baz/./././../.."
22862286
* will return "/home/barney". "/../.." will return "/". "." will return
2287-
* currentPath.
2287+
* the default path.
22882288
*
22892289
* Note, this function does not care about OS and filesystem issues. The
22902290
* SFTP protocol describes how paths are handled in SFTP. Specialized
22912291
* behaviors are handled when actually calling the OS functions. Paths
22922292
* are further massaged there. For example, the C: drive is treated as
22932293
* the path "/C:", and is a directory like any other.
22942294
*
2295-
* @param currentPath RealPath of the current working directory
22962295
* @param defaultPath RealPath of the default directory, usually user's
22972296
* @param in requested new path
22982297
* @param out output of real path cleanup
22992298
* @param outSz size in bytes of buffer 'out'
23002299
* @return WS_SUCCESS, WS_BAD_ARGUMENT, or WS_INVALID_PATH_E
23012300
*/
2302-
int wolfSSH_RealPath(const char* currentPath, const char* defaultPath,
2303-
char* in, char* out, word32 outSz)
2301+
int wolfSSH_RealPath(const char* defaultPath, char* in,
2302+
char* out, word32 outSz)
23042303
{
23052304
char* tail = NULL;
23062305
char* seg;
23072306
word32 inSz, segSz, curSz;
23082307

2309-
if (currentPath == NULL || defaultPath == NULL ||
2310-
in == NULL || out == NULL || outSz == 0)
2308+
if (in == NULL || out == NULL || outSz == 0) {
23112309
return WS_BAD_ARGUMENT;
2310+
}
23122311

23132312
WMEMSET(out, 0, outSz);
23142313
inSz = (word32)WSTRLEN(in);
2315-
if (inSz == 0) {
2314+
if ((inSz == 0 || in[0] != '/') && defaultPath != NULL){
23162315
WSTRNCPY(out, defaultPath, outSz);
23172316
}
2318-
else if (in[0] == '/') {
2319-
out[0] = '/';
2320-
}
23212317
else {
2322-
WSTRNCPY(out, currentPath, outSz);
2318+
out[0] = '/';
23232319
}
23242320
out[outSz - 1] = 0;
23252321
curSz = (word32)WSTRLEN(out);

tests/api.c

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,7 +1021,7 @@ struct RealPathTestCase {
10211021
const char* exp;
10221022
};
10231023

1024-
struct RealPathTestCase realPathTestCases[] = {
1024+
struct RealPathTestCase realPathDefault[] = {
10251025
{ ".", "/C:/Users/fred" },
10261026
{ "", "/C:/Users/fred" },
10271027
{ "/C:/Users/fred/..", "/C:/Users" },
@@ -1042,33 +1042,57 @@ struct RealPathTestCase realPathTestCases[] = {
10421042
{ "/home/C:/ok", "/home/C:/ok" },
10431043
{ "/home/fred/frob/frizz/../../../barney/bar/baz/./././../..",
10441044
"/home/barney" },
1045+
{ "/home/fred/sample.", "/home/fred/sample." },
1046+
{ "/home/fred/sample.jpg", "/home/fred/sample.jpg" },
1047+
{ "/home/fred/sample./other", "/home/fred/sample./other" },
1048+
{ "/home/fred/sample.dir/other", "/home/fred/sample.dir/other" },
1049+
{ "./sample.", "/C:/Users/fred/sample." },
1050+
{ "./sample.jpg", "/C:/Users/fred/sample.jpg" },
1051+
{ "./sample./other", "/C:/Users/fred/sample./other" },
1052+
{ "./sample.dir/other", "/C:/Users/fred/sample.dir/other" },
10451053
};
1046-
const char* defaultPath = "/C:/Users/fred";
10471054

1048-
static void test_wolfSSH_RealPath(void)
1055+
struct RealPathTestCase realPathNull[] = {
1056+
{ ".", "/" },
1057+
{ "", "/" },
1058+
{ "..", "/" },
1059+
{ "../barney", "/barney" },
1060+
};
1061+
1062+
static void DoRealPathTestCase(const char* path, struct RealPathTestCase* tc)
10491063
{
1050-
struct RealPathTestCase* tc;
10511064
char testPath[128];
10521065
char checkPath[128];
1053-
word32 testCount =
1054-
(sizeof realPathTestCases)/(sizeof(struct RealPathTestCase));
1055-
word32 i;
10561066
int err;
10571067

1058-
for (i = 0, tc = realPathTestCases; i < testCount; i++, tc++) {
1059-
WSTRNCPY(testPath, tc->in, sizeof(testPath) - 1);
1060-
testPath[sizeof(testPath) - 1] = 0;
1061-
WMEMSET(checkPath, 0, sizeof checkPath);
1062-
err = wolfSSH_RealPath(defaultPath, defaultPath, testPath,
1063-
checkPath, sizeof checkPath);
1064-
if (err || WSTRCMP(tc->exp, checkPath) != 0) {
1065-
printf("RealPath failure (case %u: %d)\n"
1066-
" defaultPath: %s\n"
1067-
" input: %s\n"
1068-
" expected: %s\n"
1069-
" output: %s\n", i, err,
1070-
defaultPath, tc->in, tc->exp, checkPath);
1071-
}
1068+
WSTRNCPY(testPath, tc->in, sizeof(testPath) - 1);
1069+
testPath[sizeof(testPath) - 1] = 0;
1070+
WMEMSET(checkPath, 0, sizeof checkPath);
1071+
err = wolfSSH_RealPath(path, testPath,
1072+
checkPath, sizeof checkPath);
1073+
if (err || WSTRCMP(tc->exp, checkPath) != 0) {
1074+
printf("RealPath failure (%d)\n"
1075+
" defaultPath: %s\n"
1076+
" input: %s\n"
1077+
" expected: %s\n"
1078+
" output: %s\n", err,
1079+
path, tc->in, tc->exp, checkPath);
1080+
}
1081+
}
1082+
1083+
static void test_wolfSSH_RealPath(void)
1084+
{
1085+
word32 testCount;
1086+
word32 i;
1087+
1088+
testCount = (sizeof realPathDefault)/(sizeof(struct RealPathTestCase));
1089+
for (i = 0; i < testCount; i++) {
1090+
DoRealPathTestCase("/C:/Users/fred", realPathDefault + i);
1091+
}
1092+
1093+
testCount = (sizeof realPathNull)/(sizeof(struct RealPathTestCase));
1094+
for (i = 0; i < testCount; i++) {
1095+
DoRealPathTestCase(NULL, realPathNull + i);
10721096
}
10731097
}
10741098
#else
@@ -1088,7 +1112,6 @@ int main(void)
10881112
test_wolfSSH_SetUsername();
10891113
test_wolfSSH_ConvertConsole();
10901114
test_wolfSSH_CTX_UsePrivateKey_buffer();
1091-
test_wolfSSH_RealPath();
10921115
test_wolfSSH_CTX_UseCert_buffer();
10931116
test_wolfSSH_CertMan();
10941117

@@ -1098,6 +1121,8 @@ int main(void)
10981121
/* SFTP tests */
10991122
test_wolfSSH_SFTP_SendReadPacket();
11001123

1124+
/* Either SCP or SFTP */
1125+
test_wolfSSH_RealPath();
11011126

11021127
AssertIntEQ(wolfSSH_Cleanup(), WS_SUCCESS);
11031128

wolfssh/ssh.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,8 @@ enum WS_DisconnectReasonCodes {
339339
};
340340

341341

342-
WOLFSSH_API int wolfSSH_RealPath(const char* currentPath,
343-
const char* defaultPath, char* in, char* out, word32 outSz);
342+
WOLFSSH_API int wolfSSH_RealPath(const char* defaultPath, char* in,
343+
char* out, word32 outSz);
344344

345345

346346
WOLFSSH_API void wolfSSH_ShowSizes(void);

0 commit comments

Comments
 (0)