119 lines
3.9 KiB
C++
119 lines
3.9 KiB
C++
// Giai duong dan ban do: tuong doi -> MapDir(), `{stamp}` -> dau thoi gian,
|
|
// va mkdir -p cho thu muc cha.
|
|
//
|
|
// Cac test nay TU dat $VSLAM_RESOURCE_DIR trong than test thay vi nho harness:
|
|
// ResolveMapPath doc env o MOI lan goi, nen dat tai cho la du, va khong ep
|
|
// moi test khac trong cay phai song chung mot bien moi truong.
|
|
|
|
#include "vslam/node_constants.h"
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <sys/stat.h>
|
|
#include <unistd.h>
|
|
|
|
#include <cstdlib>
|
|
#include <fstream>
|
|
#include <string>
|
|
|
|
using namespace vslam;
|
|
|
|
namespace {
|
|
|
|
// Thu muc tam rieng cho tung test, duoi $TMPDIR. Kem pid de hai lan chay song
|
|
// song khong dam nhau.
|
|
std::string TempRoot(const std::string& leaf) {
|
|
const char* t = std::getenv("TMPDIR");
|
|
const std::string base = (t != nullptr && t[0] != '\0') ? std::string(t) : "/tmp";
|
|
return base + "/vslam_test_" + leaf + "_" + std::to_string(::getpid());
|
|
}
|
|
|
|
void SetResourceDir(const std::string& dir) {
|
|
::setenv(kResourceDirEnv, dir.c_str(), 1);
|
|
}
|
|
|
|
} // namespace
|
|
|
|
TEST(ResolveMapPath, RongThiGiuRong) {
|
|
// Rong = DB trong RAM. Bien no thanh mot duong dan la doi han y nghia cua
|
|
// `--slam-db ""`.
|
|
SetResourceDir("/tmp/khong-duoc-dung");
|
|
EXPECT_EQ(ResolveMapPath(""), "");
|
|
}
|
|
|
|
TEST(ResolveMapPath, TuyetDoiGiuNguyen) {
|
|
SetResourceDir("/tmp/khong-duoc-dung");
|
|
EXPECT_EQ(ResolveMapPath("/abs/duong/map.db"), "/abs/duong/map.db");
|
|
}
|
|
|
|
TEST(ResolveMapPath, TuongDoiGiaiTheoMapDir) {
|
|
SetResourceDir("/tmp/vslam_res");
|
|
EXPECT_EQ(MapDir(), "/tmp/vslam_res/maps");
|
|
EXPECT_EQ(ResolveMapPath("map.db"), "/tmp/vslam_res/maps/map.db");
|
|
EXPECT_EQ(ResolveMapPath("tang3/map.db"), "/tmp/vslam_res/maps/tang3/map.db");
|
|
}
|
|
|
|
TEST(ResolveMapPath, StampNoRaVaDongNhatTrongCaChuoi) {
|
|
SetResourceDir("/tmp/vslam_res");
|
|
const std::string out = ResolveMapPath("{stamp}/map_{stamp}.db");
|
|
|
|
// Khong con token nao sot lai.
|
|
EXPECT_EQ(out.find('{'), std::string::npos);
|
|
|
|
// Hai lan xuat hien phai ra CUNG mot gia tri -- neu moi lan goi NowStamp()
|
|
// rieng thi ten thu muc va ten file lech nhau mot giay.
|
|
const std::string prefix = "/tmp/vslam_res/maps/";
|
|
ASSERT_EQ(out.compare(0, prefix.size(), prefix), 0) << out;
|
|
const std::string rest = out.substr(prefix.size());
|
|
const size_t slash = rest.find('/');
|
|
ASSERT_NE(slash, std::string::npos) << out;
|
|
const std::string dir_stamp = rest.substr(0, slash);
|
|
EXPECT_EQ(dir_stamp.size(), 15u); // %Y%m%d_%H%M%S
|
|
EXPECT_EQ(rest.substr(slash + 1), "map_" + dir_stamp + ".db");
|
|
|
|
for (char c : dir_stamp) {
|
|
EXPECT_TRUE((c >= '0' && c <= '9') || c == '_') << dir_stamp;
|
|
}
|
|
}
|
|
|
|
TEST(ResolveMapPath, Idempotent) {
|
|
SetResourceDir("/tmp/vslam_res");
|
|
const std::string once = ResolveMapPath("map_{stamp}.db");
|
|
EXPECT_EQ(ResolveMapPath(once), once);
|
|
}
|
|
|
|
TEST(EnsureParentDir, TaoLongNhau) {
|
|
const std::string root = TempRoot("mkdir");
|
|
const std::string db = root + "/a/b/c/map.db";
|
|
const StatusResponse st = EnsureParentDir(db);
|
|
ASSERT_TRUE(st.ok()) << st.message;
|
|
|
|
struct stat sb {};
|
|
ASSERT_EQ(::stat((root + "/a/b/c").c_str(), &sb), 0);
|
|
EXPECT_TRUE(S_ISDIR(sb.st_mode));
|
|
EXPECT_FALSE(::access(db.c_str(), F_OK) == 0) << "chi tao thu muc, khong tao file";
|
|
|
|
// Goi lai tren thu muc da co phai Ok, khong phai EEXIST.
|
|
EXPECT_TRUE(EnsureParentDir(db).ok());
|
|
}
|
|
|
|
TEST(EnsureParentDir, LoiKhiChaLaFileThuong) {
|
|
const std::string root = TempRoot("notdir");
|
|
ASSERT_TRUE(EnsureParentDir(root + "/x").ok());
|
|
{
|
|
std::ofstream f(root + "/tap-tin");
|
|
ASSERT_TRUE(f.good());
|
|
f << "khong phai thu muc\n";
|
|
}
|
|
const StatusResponse st = EnsureParentDir(root + "/tap-tin/map.db");
|
|
EXPECT_FALSE(st.ok());
|
|
EXPECT_NE(st.message.find("khong phai thu muc"), std::string::npos) << st.message;
|
|
}
|
|
|
|
TEST(EnsureParentDir, KhongCoThuMucChaThiBoQua) {
|
|
// "map.db" tran = thu muc hien tai; "/map.db" = ngay duoi root. Ca hai deu
|
|
// khong co gi de tao, va KHONG duoc di mkdir("/").
|
|
EXPECT_TRUE(EnsureParentDir("map.db").ok());
|
|
EXPECT_TRUE(EnsureParentDir("/map.db").ok());
|
|
}
|