From 6e2be38ae2bc79623dc7412a3547788d1ea1f31f Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Sun, 7 Apr 2019 16:36:16 -0700 Subject: [PATCH] GUACAMOLE-637: Add path depth limits to generated paths in unit tests. --- src/common-ssh/tests/sftp/normalize_path.c | 21 +++++++++++++++------ src/protocols/rdp/tests/fs/normalize_path.c | 21 +++++++++++++++------ 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/src/common-ssh/tests/sftp/normalize_path.c b/src/common-ssh/tests/sftp/normalize_path.c index 23975252..79f4d426 100644 --- a/src/common-ssh/tests/sftp/normalize_path.c +++ b/src/common-ssh/tests/sftp/normalize_path.c @@ -166,19 +166,28 @@ void test_fs__normalize_relative_mixed() { * The number of bytes to include in the generated path, not counting the * null-terminator. * + * @param max_depth + * The maximum number of path components to include within the generated + * path. + * * @return * A dynamically-allocated path containing the given number of bytes, not * counting the null-terminator. This path must eventually be freed with a * call to free(). */ -static char* generate_path(int length) { +static char* generate_path(int length, int max_depth) { int i; char* input = malloc(length + 1); - /* Fill path with /x/x/x/x/x/x/x/x/x/x/... */ + /* Fill path with /x/x/x/x/x/x/x/x/x/x/.../xxxxxxxxx... */ for (i = 0; i < length; i++) { - input[i] = (i % 2 == 0) ? '/' : 'x'; + if (max_depth > 0 && i % 2 == 0) { + input[i] = '/'; + max_depth--; + } + else + input[i] = 'x'; } /* Add null terminator */ @@ -198,17 +207,17 @@ void test_fs__normalize_long() { char normalized[GUAC_COMMON_SSH_SFTP_MAX_PATH]; /* Exceeds maximum length by a factor of 2 */ - input = generate_path(GUAC_COMMON_SSH_SFTP_MAX_PATH*2); + input = generate_path(GUAC_COMMON_SSH_SFTP_MAX_PATH*2, GUAC_COMMON_SSH_SFTP_MAX_DEPTH); CU_ASSERT_EQUAL(guac_common_ssh_sftp_normalize_path(normalized, input), 0); free(input); /* Exceeds maximum length by one byte */ - input = generate_path(GUAC_COMMON_SSH_SFTP_MAX_PATH); + input = generate_path(GUAC_COMMON_SSH_SFTP_MAX_PATH, GUAC_COMMON_SSH_SFTP_MAX_DEPTH); CU_ASSERT_EQUAL(guac_common_ssh_sftp_normalize_path(normalized, input), 0); free(input); /* Exactly maximum length */ - input = generate_path(GUAC_COMMON_SSH_SFTP_MAX_PATH - 1); + input = generate_path(GUAC_COMMON_SSH_SFTP_MAX_PATH - 1, GUAC_COMMON_SSH_SFTP_MAX_DEPTH); CU_ASSERT_NOT_EQUAL(guac_common_ssh_sftp_normalize_path(normalized, input), 0); free(input); diff --git a/src/protocols/rdp/tests/fs/normalize_path.c b/src/protocols/rdp/tests/fs/normalize_path.c index 0d8f17fa..02fbc6a6 100644 --- a/src/protocols/rdp/tests/fs/normalize_path.c +++ b/src/protocols/rdp/tests/fs/normalize_path.c @@ -166,19 +166,28 @@ void test_fs__normalize_relative_mixed() { * The number of bytes to include in the generated path, not counting the * null-terminator. * + * @param max_depth + * The maximum number of path components to include within the generated + * path. + * * @return * A dynamically-allocated path containing the given number of bytes, not * counting the null-terminator. This path must eventually be freed with a * call to free(). */ -static char* generate_path(int length) { +static char* generate_path(int length, int max_depth) { int i; char* input = malloc(length + 1); - /* Fill path with \x\x\x\x\x\x\x\x\x\x\... */ + /* Fill path with \x\x\x\x\x\x\x\x\x\x\...\xxxxxxxxx... */ for (i = 0; i < length; i++) { - input[i] = (i % 2 == 0) ? '\\' : 'x'; + if (max_depth > 0 && i % 2 == 0) { + input[i] = '\\'; + max_depth--; + } + else + input[i] = 'x'; } /* Add null terminator */ @@ -198,17 +207,17 @@ void test_fs__normalize_long() { char normalized[GUAC_RDP_FS_MAX_PATH]; /* Exceeds maximum length by a factor of 2 */ - input = generate_path(GUAC_RDP_FS_MAX_PATH*2); + input = generate_path(GUAC_RDP_FS_MAX_PATH*2, GUAC_RDP_MAX_PATH_DEPTH); CU_ASSERT_NOT_EQUAL(guac_rdp_fs_normalize_path(input, normalized), 0); free(input); /* Exceeds maximum length by one byte */ - input = generate_path(GUAC_RDP_FS_MAX_PATH); + input = generate_path(GUAC_RDP_FS_MAX_PATH, GUAC_RDP_MAX_PATH_DEPTH); CU_ASSERT_NOT_EQUAL(guac_rdp_fs_normalize_path(input, normalized), 0); free(input); /* Exactly maximum length */ - input = generate_path(GUAC_RDP_FS_MAX_PATH - 1); + input = generate_path(GUAC_RDP_FS_MAX_PATH - 1, GUAC_RDP_MAX_PATH_DEPTH); CU_ASSERT_EQUAL(guac_rdp_fs_normalize_path(input, normalized), 0); free(input);