@@ -1980,4 +1980,81 @@ TEST_F(TransferTests, TransferManager_TemplatesTest)
19801980 " text/plain" ,
19811981 Aws::Map<Aws::String, Aws::String>());
19821982}
1983+
1984+ class TestHelperTransferManager : public Aws ::Transfer::TransferManager
1985+ {
1986+ public:
1987+ static bool MakePublicIsWithinParentDirectory (Aws::String parentDirectory, Aws::String filePath)
1988+ {
1989+ return Aws::Transfer::TransferManager::IsWithinParentDirectory (parentDirectory, filePath);
1990+ }
1991+
1992+ struct TestCaseEntry
1993+ {
1994+ Aws::String parentDir;
1995+ Aws::String filePath;
1996+
1997+ bool expectedIsWithinParentDirectory;
1998+ };
1999+ };
2000+
2001+ TEST_F (TransferTests, TransferManager_TestRelativePrefix)
2002+ {
2003+
2004+ static const std::vector<TestHelperTransferManager::TestCaseEntry> TEST_CASES =
2005+ {
2006+ {R"( C:/temp/)" , R"( C:/temp/filename.bmp)" , true },
2007+ {R"( C:/temp)" , R"( C:/temp/filename.bmp)" , true },
2008+ {R"( C:/temp/)" , R"( C:/temp/../temp-1/filename.bmp)" , false },
2009+ {R"( C:/temp/)" , R"( C:/temp/temp/filename..bmp)" , true },
2010+ {R"( C:/temp/)" , R"( C:/temp/temp/filename/..bmp)" , true },
2011+ {R"( C:/temp/)" , R"( C:/temp/temp/filename/../bmp)" , true },
2012+ {R"( C:/temp/)" , R"( C:/temp/../temp-1/filename..bmp)" , false },
2013+ {R"( C:/temp/)" , R"( C:/temp/../temp/temp-1/filename..bmp)" , false },
2014+ {R"( C:/)" , R"( C:/..../foo.txt)" , true },
2015+ {R"( C:/)" , R"( C:/./foo.txt)" , true },
2016+ {R"( C:/)" , R"( C:/.../foo.txt)" , true },
2017+ {R"( C:/)" , R"( C:/.../../foo.txt)" , true },
2018+ {R"( C:/)" , R"( C:/.../.../../../foo.txt)" , true },
2019+ {R"( C:/)" , R"( C:/...////./foo.txt)" , true },
2020+
2021+ {R"( /home/user/my-intended-directory)" , R"( /home/user/my-intended-directory/foo.txt)" , true },
2022+ {R"( /home/user/my-intended-directory/)" , R"( /home/user/my-intended-directory/foo.txt)" , true },
2023+ {R"( /home/user/my-intended-directory/)" , R"( /home/user/my-intended-directory//////foo.txt)" , true },
2024+ {R"( /home/user/)" , R"( /home/user/down/../down/../down/../foo.txt)" , true },
2025+ {R"( /home/user/my-intended-directory)" , R"( /home/user/my-intended-directory/../my-intended-directory-2/foo.txt)" , false },
2026+ {R"( /home/user/)" , R"( /home/user/../../root/foo.txt)" , false },
2027+ {R"( /home/user)" , R"( /home/user/../../root/foo.txt)" , false },
2028+
2029+ {R"( /home/user/)" , R"( /home/user/.. )" , true },
2030+ {R"( /home/user/)" , R"( /home/user/ .. )" , true },
2031+ {R"( /home/user/)" , R"( /home/user/ ..)" , true },
2032+ {R"( /home/user/)" , R"( /home/user/./foo)" , true },
2033+ {R"( /home/user)" , R"( /home/user/./foo)" , true },
2034+ {R"( /home/user/)" , R"( /home/user/./././././foo)" , true },
2035+ {R"( /home/user/)" , R"( /home/user/./././.././foo)" , false },
2036+
2037+ {R"( ./)" , R"( ./foo)" , true },
2038+ {R"( ./)" , R"( ./..foo)" , true },
2039+ {R"( ./)" , R"( ./../foo)" , false },
2040+ {R"( ./)" , R"( ./.../foo)" , true },
2041+ {R"( ./)" , R"( ./.../../foo)" , true },
2042+ {R"( ./)" , R"( .//.../../foo)" , true },
2043+ };
2044+
2045+ for (const TestHelperTransferManager::TestCaseEntry& TC_ENTRY : TEST_CASES)
2046+ {
2047+ char delimiter[] = { Aws::FileSystem::PATH_DELIM, 0 };
2048+ Aws::String osNormalizedParentDir = TC_ENTRY.parentDir ;
2049+ Aws::Utils::StringUtils::Replace (osNormalizedParentDir, " /" , delimiter);
2050+ Aws::String osNormalizedFilePath = TC_ENTRY.filePath ;
2051+ Aws::Utils::StringUtils::Replace (osNormalizedFilePath, " /" , delimiter);
2052+
2053+ bool actualResult = TestHelperTransferManager::MakePublicIsWithinParentDirectory (osNormalizedParentDir, osNormalizedFilePath);
2054+
2055+ ASSERT_EQ (TC_ENTRY.expectedIsWithinParentDirectory , actualResult)
2056+ << (TC_ENTRY.expectedIsWithinParentDirectory ? " EXPECTED \" " : " NOT EXPECTED \" " ) << osNormalizedFilePath << " \" to be found in \" " << osNormalizedParentDir << " \" " ;
2057+ }
2058+ }
2059+
19832060}
0 commit comments