Skip to content

Commit

Permalink
UnitTests/FS: Add path validity and splitting tests
Browse files Browse the repository at this point in the history
  • Loading branch information
leoetlino committed Jan 25, 2020
1 parent 484cfb9 commit d4ba0ac
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Source/Core/Core/IOS/FS/FileSystem.h
Expand Up @@ -136,6 +136,19 @@ struct SplitPathResult
std::string parent;
std::string file_name;
};
inline bool operator==(const SplitPathResult& lhs, const SplitPathResult& rhs)
{
const auto fields = [](const SplitPathResult& obj) {
return std::tie(obj.parent, obj.file_name);
};
return fields(lhs) == fields(rhs);
}

inline bool operator!=(const SplitPathResult& lhs, const SplitPathResult& rhs)
{
return !(lhs == rhs);
}

/// Split a path into a parent path and the file name. Takes a *valid non-root* path.
///
/// Example: /shared2/sys/SYSCONF => {/shared2/sys, SYSCONF}
Expand Down
46 changes: 46 additions & 0 deletions Source/UnitTests/Core/IOS/FS/FileSystemTest.cpp
Expand Up @@ -39,6 +39,41 @@ class FileSystemTest : public testing::Test
std::string m_profile_path;
};

TEST(FileSystem, BasicPathValidity)
{
EXPECT_TRUE(IsValidPath("/"));
EXPECT_FALSE(IsValidNonRootPath("/"));

EXPECT_TRUE(IsValidNonRootPath("/shared2/sys/SYSCONF"));
EXPECT_TRUE(IsValidNonRootPath("/shared2/sys"));
EXPECT_TRUE(IsValidNonRootPath("/shared2"));

// Paths must start with /.
EXPECT_FALSE(IsValidNonRootPath("\\test"));
// Paths must not end with /.
EXPECT_FALSE(IsValidNonRootPath("/shared2/sys/"));
// Paths must not be longer than 64 characters.
EXPECT_FALSE(IsValidPath(
"/abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"));
}

TEST(FileSystem, PathSplitting)
{
SplitPathResult result;

result = {"/shared1", "00000042.app"};
EXPECT_EQ(SplitPathAndBasename("/shared1/00000042.app"), result);

result = {"/shared2/sys", "SYSCONF"};
EXPECT_EQ(SplitPathAndBasename("/shared2/sys/SYSCONF"), result);

result = {"/shared2", "sys"};
EXPECT_EQ(SplitPathAndBasename("/shared2/sys"), result);

result = {"/", "shared2"};
EXPECT_EQ(SplitPathAndBasename("/shared2"), result);
}

TEST_F(FileSystemTest, EssentialDirectories)
{
for (const std::string& path :
Expand Down Expand Up @@ -70,6 +105,13 @@ TEST_F(FileSystemTest, CreateFile)
const Result<std::vector<std::string>> tmp_files = m_fs->ReadDirectory(Uid{0}, Gid{0}, "/tmp");
ASSERT_TRUE(tmp_files.Succeeded());
EXPECT_EQ(std::count(tmp_files->begin(), tmp_files->end(), "f"), 1u);

// Test invalid paths
// Unprintable characters
EXPECT_EQ(m_fs->CreateFile(Uid{0}, Gid{0}, "/tmp/tes\1t", 0, modes), ResultCode::Invalid);
EXPECT_EQ(m_fs->CreateFile(Uid{0}, Gid{0}, "/tmp/te\x7fst", 0, modes), ResultCode::Invalid);
// Paths with too many components are not rejected for files.
EXPECT_EQ(m_fs->CreateFile(Uid{0}, Gid{0}, "/1/2/3/4/5/6/7/8/9", 0, modes), ResultCode::NotFound);
}

TEST_F(FileSystemTest, CreateDirectory)
Expand All @@ -94,6 +136,10 @@ TEST_F(FileSystemTest, CreateDirectory)
EXPECT_TRUE(children->empty());

EXPECT_EQ(m_fs->CreateDirectory(Uid{0}, Gid{0}, PATH, 0, modes), ResultCode::AlreadyExists);

// Paths with too many components should be rejected.
EXPECT_EQ(m_fs->CreateDirectory(Uid{0}, Gid{0}, "/1/2/3/4/5/6/7/8/9", 0, modes),
ResultCode::TooManyPathComponents);
}

TEST_F(FileSystemTest, Delete)
Expand Down

0 comments on commit d4ba0ac

Please sign in to comment.