Skip to content

Commit

Permalink
fs_canonical tests
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeTriangle committed Jun 29, 2022
1 parent a6560a0 commit 7b0eace
Showing 1 changed file with 75 additions and 4 deletions.
79 changes: 75 additions & 4 deletions src/mavsdk/core/fs_test.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,80 @@
#include <gtest/gtest.h>
#include <unistd.h>
#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

0 comments on commit 7b0eace

Please sign in to comment.