diff --git a/src/mavsdk/core/fs_test.cpp b/src/mavsdk/core/fs_test.cpp index 4a68de5861..4d3eca535a 100644 --- a/src/mavsdk/core/fs_test.cpp +++ b/src/mavsdk/core/fs_test.cpp @@ -1,9 +1,80 @@ #include +#include #include "fs.h" -TEST(Filesystem, SimpleCanonicalPathIsCorrect) +#if defined(WINDOWS) +#else +#define PATH_MAX 4096 + +TEST(Filesystem, AbsolutePathUnchanged) +{ + const std::string path = "/sbin/init"; + ASSERT_EQ(path, mavsdk::fs_canonical(path)); +} + +TEST(Filesystem, RemoveFinalSlash) +{ + const std::string path = "/usr/local/include/mavsdk/"; + const std::string canonical_path = "/usr/local/include/mavsdk"; + ASSERT_EQ(canonical_path, mavsdk::fs_canonical(path)); +} + +TEST(Filesystem, RemoveDuplicateSlashes) +{ + const std::string path = "//////opt/////ros////foxy"; + const std::string canonical_path = "/opt/ros/foxy"; + ASSERT_EQ(canonical_path, mavsdk::fs_canonical(path)); +} + +TEST(Filesystem, RemoveInternalDots) +{ + const std::string path = "/sys/./class/./input"; + const std::string canonical_path = "/sys/class/input"; + ASSERT_EQ(canonical_path, mavsdk::fs_canonical(path)); +} + +TEST(Filesystem, ResolveDoubleDots) { - const auto path = "/bin/path"; - const auto canonical_path = mavsdk::fs_canonical(path); - ASSERT_EQ("blah", canonical_path); + const std::string path = "/bin/../dev/../etc/crontab/../shadow"; + const std::string canonical_path = "/etc/shadow"; + ASSERT_EQ(canonical_path, mavsdk::fs_canonical(path)); } + +TEST(Filesystem, RelativePathEmpty) +{ + char cwd[PATH_MAX]; + getcwd(cwd, PATH_MAX); + const std::string path = ""; + const std::string canonical_path = std::string(cwd); + ASSERT_EQ(canonical_path, mavsdk::fs_canonical(path)); +} + +TEST(Filesystem, RelativePathDot) +{ + char cwd[PATH_MAX]; + getcwd(cwd, PATH_MAX); + const std::string path = "."; + const std::string canonical_path = std::string(cwd); + ASSERT_EQ(canonical_path, mavsdk::fs_canonical(path)); +} + +TEST(Filesystem, RelativePathBare) +{ + char cwd[PATH_MAX]; + getcwd(cwd, PATH_MAX); + const std::string path = "src/mavsdk/core/fs_test.cpp"; + const std::string canonical_path = std::string(cwd) + mavsdk::path_separator + path; + ASSERT_EQ(canonical_path, mavsdk::fs_canonical(path)); +} + +TEST(Filesystem, RelativePathDotSlash) +{ + char cwd[PATH_MAX]; + getcwd(cwd, PATH_MAX); + const std::string bare_path = "src/mavsdk/plugins/mavlink_passthrough"; + const std::string dotslash_path = "./" + bare_path; + const std::string canonical_path = std::string(cwd) + mavsdk::path_separator + bare_path; + ASSERT_EQ(canonical_path, mavsdk::fs_canonical(dotslash_path)); +} + +#endif