GUACAMOLE-637: Add path depth limits to generated paths in unit tests.

This commit is contained in:
Michael Jumper 2019-04-07 16:36:16 -07:00
parent 986f7f5d64
commit 6e2be38ae2
2 changed files with 30 additions and 12 deletions

View File

@ -166,19 +166,28 @@ void test_fs__normalize_relative_mixed() {
* The number of bytes to include in the generated path, not counting the * The number of bytes to include in the generated path, not counting the
* null-terminator. * null-terminator.
* *
* @param max_depth
* The maximum number of path components to include within the generated
* path.
*
* @return * @return
* A dynamically-allocated path containing the given number of bytes, not * A dynamically-allocated path containing the given number of bytes, not
* counting the null-terminator. This path must eventually be freed with a * counting the null-terminator. This path must eventually be freed with a
* call to free(). * call to free().
*/ */
static char* generate_path(int length) { static char* generate_path(int length, int max_depth) {
int i; int i;
char* input = malloc(length + 1); 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++) { 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 */ /* Add null terminator */
@ -198,17 +207,17 @@ void test_fs__normalize_long() {
char normalized[GUAC_COMMON_SSH_SFTP_MAX_PATH]; char normalized[GUAC_COMMON_SSH_SFTP_MAX_PATH];
/* Exceeds maximum length by a factor of 2 */ /* 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); CU_ASSERT_EQUAL(guac_common_ssh_sftp_normalize_path(normalized, input), 0);
free(input); free(input);
/* Exceeds maximum length by one byte */ /* 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); CU_ASSERT_EQUAL(guac_common_ssh_sftp_normalize_path(normalized, input), 0);
free(input); free(input);
/* Exactly maximum length */ /* 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); CU_ASSERT_NOT_EQUAL(guac_common_ssh_sftp_normalize_path(normalized, input), 0);
free(input); free(input);

View File

@ -166,19 +166,28 @@ void test_fs__normalize_relative_mixed() {
* The number of bytes to include in the generated path, not counting the * The number of bytes to include in the generated path, not counting the
* null-terminator. * null-terminator.
* *
* @param max_depth
* The maximum number of path components to include within the generated
* path.
*
* @return * @return
* A dynamically-allocated path containing the given number of bytes, not * A dynamically-allocated path containing the given number of bytes, not
* counting the null-terminator. This path must eventually be freed with a * counting the null-terminator. This path must eventually be freed with a
* call to free(). * call to free().
*/ */
static char* generate_path(int length) { static char* generate_path(int length, int max_depth) {
int i; int i;
char* input = malloc(length + 1); 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++) { 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 */ /* Add null terminator */
@ -198,17 +207,17 @@ void test_fs__normalize_long() {
char normalized[GUAC_RDP_FS_MAX_PATH]; char normalized[GUAC_RDP_FS_MAX_PATH];
/* Exceeds maximum length by a factor of 2 */ /* 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); CU_ASSERT_NOT_EQUAL(guac_rdp_fs_normalize_path(input, normalized), 0);
free(input); free(input);
/* Exceeds maximum length by one byte */ /* 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); CU_ASSERT_NOT_EQUAL(guac_rdp_fs_normalize_path(input, normalized), 0);
free(input); free(input);
/* Exactly maximum length */ /* 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); CU_ASSERT_EQUAL(guac_rdp_fs_normalize_path(input, normalized), 0);
free(input); free(input);