From 45f449baddf60dac7ff50793642e45a35c72f1cc Mon Sep 17 00:00:00 2001 From: Yingchun Lai Date: Wed, 11 Oct 2023 12:28:16 +0800 Subject: [PATCH] fix ut --- src/replica/test/mutation_log_test.cpp | 2 +- src/test_util/test_util.cpp | 10 +- src/test_util/test_util.h | 17 +- src/utils/alloc.h | 2 +- src/utils/test/env.cpp | 23 +- src/utils/test/file_utils.cpp | 1217 ++++++++++++------------ 6 files changed, 642 insertions(+), 629 deletions(-) diff --git a/src/replica/test/mutation_log_test.cpp b/src/replica/test/mutation_log_test.cpp index 70f8a8c695..1cdaec67f1 100644 --- a/src/replica/test/mutation_log_test.cpp +++ b/src/replica/test/mutation_log_test.cpp @@ -686,7 +686,7 @@ TEST_P(mutation_log_test, reset_from_while_writing) ASSERT_EQ(actual.size(), expected.size()); } -TEST_F(mutation_log_test, gc_slog) +TEST_P(mutation_log_test, gc_slog) { // Remove the slog dir and create a new one. const std::string slog_dir("./slog_test"); diff --git a/src/test_util/test_util.cpp b/src/test_util/test_util.cpp index 0789c4678b..a4dbf41deb 100644 --- a/src/test_util/test_util.cpp +++ b/src/test_util/test_util.cpp @@ -18,7 +18,6 @@ #include "test_util.h" #include -#include #include #include #include @@ -42,10 +41,11 @@ namespace pegasus { void create_local_test_file(const std::string &full_name, dsn::replication::file_meta *fm) { ASSERT_NE(fm, nullptr); - auto s = rocksdb::WriteStringToFile(rocksdb::Env::Default(), - rocksdb::Slice("write some data."), - full_name, - /* should_sync */ true); + auto s = + rocksdb::WriteStringToFile(dsn::utils::PegasusEnv(dsn::utils::FileDataType::kSensitive), + rocksdb::Slice("write some data."), + full_name, + /* should_sync */ true); ASSERT_TRUE(s.ok()) << s.ToString(); fm->name = full_name; ASSERT_EQ(dsn::ERR_OK, dsn::utils::filesystem::md5sum(full_name, fm->md5)); diff --git a/src/test_util/test_util.h b/src/test_util/test_util.h index 762d154367..217d639f0c 100644 --- a/src/test_util/test_util.h +++ b/src/test_util/test_util.h @@ -19,10 +19,12 @@ #pragma once +#include #include #include #include +#include "utils/env.h" #include "utils/flags.h" #include "utils/test_macros.h" @@ -40,7 +42,20 @@ namespace pegasus { class encrypt_data_test_base : public testing::TestWithParam { public: - encrypt_data_test_base() { FLAGS_encrypt_data_at_rest = GetParam(); } + encrypt_data_test_base() + { + FLAGS_encrypt_data_at_rest = GetParam(); + // The size of an actual encrypted file should plus kEncryptionHeaderkSize bytes if consider + // it as kNonSensitive. + if (FLAGS_encrypt_data_at_rest) { + _extra_encrypted_file_size = dsn::utils::kEncryptionHeaderkSize; + } + } + + uint64_t extra_encrypted_file_size() const { return _extra_encrypted_file_size; } + +private: + uint64_t _extra_encrypted_file_size = 0; }; void create_local_test_file(const std::string &full_name, dsn::replication::file_meta *fm); diff --git a/src/utils/alloc.h b/src/utils/alloc.h index 8541d98e8a..16045b3b5b 100644 --- a/src/utils/alloc.h +++ b/src/utils/alloc.h @@ -17,7 +17,7 @@ #pragma once -#include +#include // IWYU pragma: keep #include #include #include diff --git a/src/utils/test/env.cpp b/src/utils/test/env.cpp index 5d50951b7b..c29879ca97 100644 --- a/src/utils/test/env.cpp +++ b/src/utils/test/env.cpp @@ -47,7 +47,6 @@ #include #include "test_util/test_util.h" -#include "utils/enum_helper.h" #include "utils/env.h" #include "utils/filesystem.h" #include "utils/flags.h" @@ -87,16 +86,6 @@ TEST(env_test, get_env) class env_file_test : public pegasus::encrypt_data_test_base { -public: - env_file_test() : pegasus::encrypt_data_test_base() - { - // The size of an actual encrypted file should plus kEncryptionHeaderkSize bytes if consider - // it as kNonSensitive. - if (FLAGS_encrypt_data_at_rest) { - _extra_size = dsn::utils::kEncryptionHeaderkSize; - } - } - uint64_t _extra_size = 0; }; INSTANTIATE_TEST_CASE_P(, env_file_test, ::testing::Values(false, true)); @@ -134,7 +123,7 @@ TEST_P(env_file_test, encrypt_file_2_files) ASSERT_EQ(kFileContentSize, wfile_size); ASSERT_TRUE(dsn::utils::filesystem::file_size( kEncryptedFileName, dsn::utils::FileDataType::kNonSensitive, wfile_size)); - ASSERT_EQ(kFileContentSize + _extra_size, wfile_size); + ASSERT_EQ(kFileContentSize + extra_encrypted_file_size(), wfile_size); // Check file content. std::string data; s = rocksdb::ReadFileToString(dsn::utils::PegasusEnv(dsn::utils::FileDataType::kSensitive), @@ -174,7 +163,7 @@ TEST_P(env_file_test, encrypt_file_1_file) ASSERT_EQ(kFileContentSize, wfile_size); ASSERT_TRUE(dsn::utils::filesystem::file_size( kFileName, dsn::utils::FileDataType::kNonSensitive, wfile_size)); - ASSERT_EQ(kFileContentSize + _extra_size, wfile_size); + ASSERT_EQ(kFileContentSize + extra_encrypted_file_size(), wfile_size); // Check file content. std::string data; s = rocksdb::ReadFileToString( @@ -204,7 +193,7 @@ TEST_P(env_file_test, copy_file) ASSERT_EQ(kFileContentSize, wfile_size); ASSERT_TRUE(dsn::utils::filesystem::file_size( kFileName, dsn::utils::FileDataType::kNonSensitive, wfile_size)); - ASSERT_EQ(kFileContentSize + _extra_size, wfile_size); + ASSERT_EQ(kFileContentSize + extra_encrypted_file_size(), wfile_size); // Check copy_file(src_fname, dst_fname, total_size). // Loop twice to check overwrite. @@ -218,7 +207,7 @@ TEST_P(env_file_test, copy_file) ASSERT_EQ(kFileContentSize, wfile_size); ASSERT_TRUE(dsn::utils::filesystem::file_size( kCopyFileName, dsn::utils::FileDataType::kNonSensitive, wfile_size)); - ASSERT_EQ(kFileContentSize + _extra_size, wfile_size); + ASSERT_EQ(kFileContentSize + extra_encrypted_file_size(), wfile_size); // Check file content. std::string data; s = rocksdb::ReadFileToString( @@ -249,7 +238,7 @@ TEST_P(env_file_test, copy_file_by_size) ASSERT_EQ(kFileContentSize, wfile_size); ASSERT_TRUE(dsn::utils::filesystem::file_size( kFileName, dsn::utils::FileDataType::kNonSensitive, wfile_size)); - ASSERT_EQ(kFileContentSize + _extra_size, wfile_size); + ASSERT_EQ(kFileContentSize + extra_encrypted_file_size(), wfile_size); // Check copy_file_by_size(src_fname, dst_fname, limit_size). struct test_case @@ -271,7 +260,7 @@ TEST_P(env_file_test, copy_file_by_size) ASSERT_EQ(test.expect_size, actual_size); ASSERT_TRUE(dsn::utils::filesystem::file_size( kCopyFileName, dsn::utils::FileDataType::kNonSensitive, wfile_size)); - ASSERT_EQ(test.expect_size + _extra_size, wfile_size); + ASSERT_EQ(test.expect_size + extra_encrypted_file_size(), wfile_size); // Check file content. std::string data; s = rocksdb::ReadFileToString( diff --git a/src/utils/test/file_utils.cpp b/src/utils/test/file_utils.cpp index 0cc2173e8e..cd8ed424a6 100644 --- a/src/utils/test/file_utils.cpp +++ b/src/utils/test/file_utils.cpp @@ -25,6 +25,7 @@ */ // IWYU pragma: no_include +// IWYU pragma: no_include // IWYU pragma: no_include #include #include @@ -33,869 +34,877 @@ #include #include +#include "test_util/test_util.h" #include "utils/env.h" #include "utils/error_code.h" #include "utils/filesystem.h" -static void file_utils_test_setup() +class file_utils : public pegasus::encrypt_data_test_base { - std::string path; - bool ret; - - path = "./file_utils_temp.txt"; - ret = dsn::utils::filesystem::remove_path(path); - EXPECT_TRUE(ret); - ret = dsn::utils::filesystem::file_exists(path); - EXPECT_FALSE(ret); - - path = "./file_utils_temp"; - ret = dsn::utils::filesystem::remove_path(path); - EXPECT_TRUE(ret); - ret = dsn::utils::filesystem::directory_exists(path); - EXPECT_FALSE(ret); -} - -static void file_utils_test_get_process_image_path() -{ - std::string path; - std::string imagepath; - dsn::error_code ret; - // int pid; - - if (!dsn::utils::filesystem::get_current_directory(imagepath)) { - EXPECT_TRUE(false); +public: + void file_utils_test_setup() + { + std::string path; + bool ret; + + path = "./file_utils_temp.txt"; + ret = dsn::utils::filesystem::remove_path(path); + EXPECT_TRUE(ret); + ret = dsn::utils::filesystem::file_exists(path); + EXPECT_FALSE(ret); + + path = "./file_utils_temp"; + ret = dsn::utils::filesystem::remove_path(path); + EXPECT_TRUE(ret); + ret = dsn::utils::filesystem::directory_exists(path); + EXPECT_FALSE(ret); } - imagepath = dsn::utils::filesystem::path_combine(imagepath, "dsn_utils_tests"); - ret = dsn::utils::filesystem::get_current_process_image_path(path); - EXPECT_TRUE(ret == dsn::ERR_OK); - // TODO: not always true when running dir is not where the test resides - // EXPECT_TRUE(path == imagepath); // e: vs E: -} + void file_utils_test_get_process_image_path() + { + std::string path; + std::string imagepath; + dsn::error_code ret; + // int pid; + + if (!dsn::utils::filesystem::get_current_directory(imagepath)) { + EXPECT_TRUE(false); + } + imagepath = dsn::utils::filesystem::path_combine(imagepath, "dsn_utils_tests"); + + ret = dsn::utils::filesystem::get_current_process_image_path(path); + EXPECT_TRUE(ret == dsn::ERR_OK); + // TODO: not always true when running dir is not where the test resides + // EXPECT_TRUE(path == imagepath); // e: vs E: + } -static void file_utils_test_get_normalized_path() -{ - int ret; - std::string path; - std::string npath; + void file_utils_test_get_normalized_path() + { + int ret; + std::string path; + std::string npath; - path = "\\\\?\\"; - ret = dsn::utils::filesystem::get_normalized_path(path, npath); - EXPECT_TRUE(ret == 0); - EXPECT_TRUE(npath == path); + path = "\\\\?\\"; + ret = dsn::utils::filesystem::get_normalized_path(path, npath); + EXPECT_TRUE(ret == 0); + EXPECT_TRUE(npath == path); - path = "c:\\"; - ret = dsn::utils::filesystem::get_normalized_path(path, npath); - EXPECT_TRUE(ret == 0); - EXPECT_TRUE(npath == path); + path = "c:\\"; + ret = dsn::utils::filesystem::get_normalized_path(path, npath); + EXPECT_TRUE(ret == 0); + EXPECT_TRUE(npath == path); - path = "c:"; - ret = dsn::utils::filesystem::get_normalized_path(path, npath); - EXPECT_TRUE(ret == 0); - EXPECT_TRUE(npath == path); + path = "c:"; + ret = dsn::utils::filesystem::get_normalized_path(path, npath); + EXPECT_TRUE(ret == 0); + EXPECT_TRUE(npath == path); - path = "\\\\?\\c:\\"; - ret = dsn::utils::filesystem::get_normalized_path(path, npath); - EXPECT_TRUE(ret == 0); - EXPECT_TRUE(npath == path); + path = "\\\\?\\c:\\"; + ret = dsn::utils::filesystem::get_normalized_path(path, npath); + EXPECT_TRUE(ret == 0); + EXPECT_TRUE(npath == path); - path = "\\\\?\\c:"; - ret = dsn::utils::filesystem::get_normalized_path(path, npath); - EXPECT_TRUE(ret == 0); - EXPECT_TRUE(npath == path); + path = "\\\\?\\c:"; + ret = dsn::utils::filesystem::get_normalized_path(path, npath); + EXPECT_TRUE(ret == 0); + EXPECT_TRUE(npath == path); - path = "c:\\a"; - ret = dsn::utils::filesystem::get_normalized_path(path, npath); - EXPECT_TRUE(ret == 0); - EXPECT_TRUE(npath == path); + path = "c:\\a"; + ret = dsn::utils::filesystem::get_normalized_path(path, npath); + EXPECT_TRUE(ret == 0); + EXPECT_TRUE(npath == path); - path = "c:\\\\a"; - ret = dsn::utils::filesystem::get_normalized_path(path, npath); - EXPECT_TRUE(ret == 0); + path = "c:\\\\a"; + ret = dsn::utils::filesystem::get_normalized_path(path, npath); + EXPECT_TRUE(ret == 0); #ifdef _WIN32 - EXPECT_TRUE(npath == "c:\\a"); + EXPECT_TRUE(npath == "c:\\a"); #else - EXPECT_TRUE(npath == path); + EXPECT_TRUE(npath == path); #endif - path = "c:\\\\a\\"; - ret = dsn::utils::filesystem::get_normalized_path(path, npath); - EXPECT_TRUE(ret == 0); + path = "c:\\\\a\\"; + ret = dsn::utils::filesystem::get_normalized_path(path, npath); + EXPECT_TRUE(ret == 0); #ifdef _WIN32 - EXPECT_TRUE(npath == "c:\\a"); + EXPECT_TRUE(npath == "c:\\a"); #else - EXPECT_TRUE(npath == path); + EXPECT_TRUE(npath == path); #endif - path = "c:\\\\a\\\\"; - ret = dsn::utils::filesystem::get_normalized_path(path, npath); - EXPECT_TRUE(ret == 0); + path = "c:\\\\a\\\\"; + ret = dsn::utils::filesystem::get_normalized_path(path, npath); + EXPECT_TRUE(ret == 0); #ifdef _WIN32 - EXPECT_TRUE(npath == "c:\\a"); + EXPECT_TRUE(npath == "c:\\a"); #else - EXPECT_TRUE(npath == path); + EXPECT_TRUE(npath == path); #endif - path = "\\\\?\\c:\\a"; - ret = dsn::utils::filesystem::get_normalized_path(path, npath); - EXPECT_TRUE(ret == 0); - EXPECT_TRUE(npath == path); + path = "\\\\?\\c:\\a"; + ret = dsn::utils::filesystem::get_normalized_path(path, npath); + EXPECT_TRUE(ret == 0); + EXPECT_TRUE(npath == path); - path = "\\\\?\\c:\\\\a"; - ret = dsn::utils::filesystem::get_normalized_path(path, npath); - EXPECT_TRUE(ret == 0); + path = "\\\\?\\c:\\\\a"; + ret = dsn::utils::filesystem::get_normalized_path(path, npath); + EXPECT_TRUE(ret == 0); #ifdef _WIN32 - EXPECT_TRUE(npath == "\\\\?\\c:\\a"); + EXPECT_TRUE(npath == "\\\\?\\c:\\a"); #else - EXPECT_TRUE(npath == path); + EXPECT_TRUE(npath == path); #endif - path = "\\\\?\\c:\\\\a\\"; - ret = dsn::utils::filesystem::get_normalized_path(path, npath); - EXPECT_TRUE(ret == 0); + path = "\\\\?\\c:\\\\a\\"; + ret = dsn::utils::filesystem::get_normalized_path(path, npath); + EXPECT_TRUE(ret == 0); #ifdef _WIN32 - EXPECT_TRUE(npath == "\\\\?\\c:\\a"); + EXPECT_TRUE(npath == "\\\\?\\c:\\a"); #else - EXPECT_TRUE(npath == path); + EXPECT_TRUE(npath == path); #endif - path = "\\\\?\\c:\\\\a\\\\"; - ret = dsn::utils::filesystem::get_normalized_path(path, npath); - EXPECT_TRUE(ret == 0); + path = "\\\\?\\c:\\\\a\\\\"; + ret = dsn::utils::filesystem::get_normalized_path(path, npath); + EXPECT_TRUE(ret == 0); #ifdef _WIN32 - EXPECT_TRUE(npath == "\\\\?\\c:\\a"); + EXPECT_TRUE(npath == "\\\\?\\c:\\a"); #else - EXPECT_TRUE(npath == path); + EXPECT_TRUE(npath == path); #endif - path = "\\"; - ret = dsn::utils::filesystem::get_normalized_path(path, npath); - EXPECT_TRUE(ret == 0); - EXPECT_TRUE(npath == path); + path = "\\"; + ret = dsn::utils::filesystem::get_normalized_path(path, npath); + EXPECT_TRUE(ret == 0); + EXPECT_TRUE(npath == path); - path = "\\\\"; - ret = dsn::utils::filesystem::get_normalized_path(path, npath); - EXPECT_TRUE(ret == 0); - EXPECT_TRUE(npath == path); + path = "\\\\"; + ret = dsn::utils::filesystem::get_normalized_path(path, npath); + EXPECT_TRUE(ret == 0); + EXPECT_TRUE(npath == path); - path = "\\\\\\"; - ret = dsn::utils::filesystem::get_normalized_path(path, npath); - EXPECT_TRUE(ret == 0); + path = "\\\\\\"; + ret = dsn::utils::filesystem::get_normalized_path(path, npath); + EXPECT_TRUE(ret == 0); #ifdef _WIN32 - EXPECT_TRUE(npath == "\\\\"); + EXPECT_TRUE(npath == "\\\\"); #else - EXPECT_TRUE(npath == path); + EXPECT_TRUE(npath == path); #endif - path = "\\\\a"; - ret = dsn::utils::filesystem::get_normalized_path(path, npath); - EXPECT_TRUE(ret == 0); - EXPECT_TRUE(npath == path); + path = "\\\\a"; + ret = dsn::utils::filesystem::get_normalized_path(path, npath); + EXPECT_TRUE(ret == 0); + EXPECT_TRUE(npath == path); - path = "\\\\\\a"; - ret = dsn::utils::filesystem::get_normalized_path(path, npath); - EXPECT_TRUE(ret == 0); + path = "\\\\\\a"; + ret = dsn::utils::filesystem::get_normalized_path(path, npath); + EXPECT_TRUE(ret == 0); #ifdef _WIN32 - EXPECT_TRUE(npath == "\\\\a"); + EXPECT_TRUE(npath == "\\\\a"); #else - EXPECT_TRUE(npath == path); + EXPECT_TRUE(npath == path); #endif - path = "\\\\a\\"; - ret = dsn::utils::filesystem::get_normalized_path(path, npath); - EXPECT_TRUE(ret == 0); + path = "\\\\a\\"; + ret = dsn::utils::filesystem::get_normalized_path(path, npath); + EXPECT_TRUE(ret == 0); #ifdef _WIN32 - EXPECT_TRUE(npath == "\\\\a"); + EXPECT_TRUE(npath == "\\\\a"); #else - EXPECT_TRUE(npath == path); + EXPECT_TRUE(npath == path); #endif - path = "\\\\\\a\\"; - ret = dsn::utils::filesystem::get_normalized_path(path, npath); - EXPECT_TRUE(ret == 0); + path = "\\\\\\a\\"; + ret = dsn::utils::filesystem::get_normalized_path(path, npath); + EXPECT_TRUE(ret == 0); #ifdef _WIN32 - EXPECT_TRUE(npath == "\\\\a"); + EXPECT_TRUE(npath == "\\\\a"); #else - EXPECT_TRUE(npath == path); + EXPECT_TRUE(npath == path); #endif - path = "\\\\\\a\\\\"; - ret = dsn::utils::filesystem::get_normalized_path(path, npath); - EXPECT_TRUE(ret == 0); + path = "\\\\\\a\\\\"; + ret = dsn::utils::filesystem::get_normalized_path(path, npath); + EXPECT_TRUE(ret == 0); #ifdef _WIN32 - EXPECT_TRUE(npath == "\\\\a"); + EXPECT_TRUE(npath == "\\\\a"); #else - EXPECT_TRUE(npath == path); + EXPECT_TRUE(npath == path); #endif - path = "/"; - ret = dsn::utils::filesystem::get_normalized_path(path, npath); - EXPECT_TRUE(ret == 0); + path = "/"; + ret = dsn::utils::filesystem::get_normalized_path(path, npath); + EXPECT_TRUE(ret == 0); #ifdef _WIN32 - EXPECT_TRUE(npath == "\\"); + EXPECT_TRUE(npath == "\\"); #else - EXPECT_TRUE(npath == path); + EXPECT_TRUE(npath == path); #endif - path = "//"; - ret = dsn::utils::filesystem::get_normalized_path(path, npath); - EXPECT_TRUE(ret == 0); + path = "//"; + ret = dsn::utils::filesystem::get_normalized_path(path, npath); + EXPECT_TRUE(ret == 0); #ifdef _WIN32 - EXPECT_TRUE(npath == "\\\\"); + EXPECT_TRUE(npath == "\\\\"); #else - EXPECT_TRUE(npath == "/"); + EXPECT_TRUE(npath == "/"); #endif - path = "//?/"; - ret = dsn::utils::filesystem::get_normalized_path(path, npath); - EXPECT_TRUE(ret == 0); + path = "//?/"; + ret = dsn::utils::filesystem::get_normalized_path(path, npath); + EXPECT_TRUE(ret == 0); #ifdef _WIN32 - EXPECT_TRUE(npath == "\\\\?\\"); + EXPECT_TRUE(npath == "\\\\?\\"); #else - EXPECT_TRUE(npath == "/?"); + EXPECT_TRUE(npath == "/?"); #endif - path = "//a"; - ret = dsn::utils::filesystem::get_normalized_path(path, npath); - EXPECT_TRUE(ret == 0); + path = "//a"; + ret = dsn::utils::filesystem::get_normalized_path(path, npath); + EXPECT_TRUE(ret == 0); #ifdef _WIN32 - EXPECT_TRUE(npath == "\\\\a"); + EXPECT_TRUE(npath == "\\\\a"); #else - EXPECT_TRUE(npath == "/a"); + EXPECT_TRUE(npath == "/a"); #endif - path = "//a/"; - ret = dsn::utils::filesystem::get_normalized_path(path, npath); - EXPECT_TRUE(ret == 0); + path = "//a/"; + ret = dsn::utils::filesystem::get_normalized_path(path, npath); + EXPECT_TRUE(ret == 0); #ifdef _WIN32 - EXPECT_TRUE(npath == "\\\\a"); + EXPECT_TRUE(npath == "\\\\a"); #else - EXPECT_TRUE(npath == "/a"); + EXPECT_TRUE(npath == "/a"); #endif - path = "//a//"; - ret = dsn::utils::filesystem::get_normalized_path(path, npath); - EXPECT_TRUE(ret == 0); + path = "//a//"; + ret = dsn::utils::filesystem::get_normalized_path(path, npath); + EXPECT_TRUE(ret == 0); #ifdef _WIN32 - EXPECT_TRUE(npath == "\\\\a"); + EXPECT_TRUE(npath == "\\\\a"); #else - EXPECT_TRUE(npath == "/a"); + EXPECT_TRUE(npath == "/a"); #endif - path = "c:/"; - ret = dsn::utils::filesystem::get_normalized_path(path, npath); - EXPECT_TRUE(ret == 0); + path = "c:/"; + ret = dsn::utils::filesystem::get_normalized_path(path, npath); + EXPECT_TRUE(ret == 0); #ifdef _WIN32 - EXPECT_TRUE(npath == "c:\\"); + EXPECT_TRUE(npath == "c:\\"); #else - EXPECT_TRUE(npath == "c:"); + EXPECT_TRUE(npath == "c:"); #endif - path = "c://"; - ret = dsn::utils::filesystem::get_normalized_path(path, npath); - EXPECT_TRUE(ret == 0); + path = "c://"; + ret = dsn::utils::filesystem::get_normalized_path(path, npath); + EXPECT_TRUE(ret == 0); #ifdef _WIN32 - EXPECT_TRUE(npath == "c:\\"); + EXPECT_TRUE(npath == "c:\\"); #else - EXPECT_TRUE(npath == "c:"); + EXPECT_TRUE(npath == "c:"); #endif - path = "c:/a"; - ret = dsn::utils::filesystem::get_normalized_path(path, npath); - EXPECT_TRUE(ret == 0); + path = "c:/a"; + ret = dsn::utils::filesystem::get_normalized_path(path, npath); + EXPECT_TRUE(ret == 0); #ifdef _WIN32 - EXPECT_TRUE(npath == "c:\\a"); + EXPECT_TRUE(npath == "c:\\a"); #else - EXPECT_TRUE(npath == path); + EXPECT_TRUE(npath == path); #endif - path = "c:/a/"; - ret = dsn::utils::filesystem::get_normalized_path(path, npath); - EXPECT_TRUE(ret == 0); + path = "c:/a/"; + ret = dsn::utils::filesystem::get_normalized_path(path, npath); + EXPECT_TRUE(ret == 0); #ifdef _WIN32 - EXPECT_TRUE(npath == "c:\\a"); + EXPECT_TRUE(npath == "c:\\a"); #else - EXPECT_TRUE(npath == "c:/a"); + EXPECT_TRUE(npath == "c:/a"); #endif - path = "c://a/"; - ret = dsn::utils::filesystem::get_normalized_path(path, npath); - EXPECT_TRUE(ret == 0); + path = "c://a/"; + ret = dsn::utils::filesystem::get_normalized_path(path, npath); + EXPECT_TRUE(ret == 0); #ifdef _WIN32 - EXPECT_TRUE(npath == "c:\\a"); + EXPECT_TRUE(npath == "c:\\a"); #else - EXPECT_TRUE(npath == "c:/a"); + EXPECT_TRUE(npath == "c:/a"); #endif - path = "c://a//"; - ret = dsn::utils::filesystem::get_normalized_path(path, npath); - EXPECT_TRUE(ret == 0); + path = "c://a//"; + ret = dsn::utils::filesystem::get_normalized_path(path, npath); + EXPECT_TRUE(ret == 0); #ifdef _WIN32 - EXPECT_TRUE(npath == "c:\\a"); + EXPECT_TRUE(npath == "c:\\a"); #else - EXPECT_TRUE(npath == "c:/a"); + EXPECT_TRUE(npath == "c:/a"); #endif - path = "/////////////////////////////////////////////////////////////////"; - ret = dsn::utils::filesystem::get_normalized_path(path, npath); - EXPECT_TRUE(ret == 0); + path = "/////////////////////////////////////////////////////////////////"; + ret = dsn::utils::filesystem::get_normalized_path(path, npath); + EXPECT_TRUE(ret == 0); #ifdef _WIN32 - EXPECT_TRUE(npath == "\\\\"); + EXPECT_TRUE(npath == "\\\\"); #else - EXPECT_TRUE(npath == "/"); + EXPECT_TRUE(npath == "/"); #endif - path = "/////////////////////////////////////////////////////////////////a/////////////////b///" - "////////"; - ret = dsn::utils::filesystem::get_normalized_path(path, npath); - EXPECT_TRUE(ret == 0); + path = "/////////////////////////////////////////////////////////////////a/////////////////" + "b///" + "////////"; + ret = dsn::utils::filesystem::get_normalized_path(path, npath); + EXPECT_TRUE(ret == 0); #ifdef _WIN32 - EXPECT_TRUE(npath == "\\\\a\\b"); + EXPECT_TRUE(npath == "\\\\a\\b"); #else - EXPECT_TRUE(npath == "/a/b"); + EXPECT_TRUE(npath == "/a/b"); #endif - path = "."; - ret = dsn::utils::filesystem::get_normalized_path(path, npath); - EXPECT_TRUE(ret == 0); - EXPECT_TRUE(npath == path); + path = "."; + ret = dsn::utils::filesystem::get_normalized_path(path, npath); + EXPECT_TRUE(ret == 0); + EXPECT_TRUE(npath == path); - path = "./"; - ret = dsn::utils::filesystem::get_normalized_path(path, npath); - EXPECT_TRUE(ret == 0); - EXPECT_TRUE(npath == "."); + path = "./"; + ret = dsn::utils::filesystem::get_normalized_path(path, npath); + EXPECT_TRUE(ret == 0); + EXPECT_TRUE(npath == "."); - path = "./a"; - ret = dsn::utils::filesystem::get_normalized_path(path, npath); - EXPECT_TRUE(ret == 0); + path = "./a"; + ret = dsn::utils::filesystem::get_normalized_path(path, npath); + EXPECT_TRUE(ret == 0); #ifdef _WIN32 - EXPECT_TRUE(npath == ".\\a"); + EXPECT_TRUE(npath == ".\\a"); #else - EXPECT_TRUE(npath == path); + EXPECT_TRUE(npath == path); #endif - path = ".//a"; - ret = dsn::utils::filesystem::get_normalized_path(path, npath); - EXPECT_TRUE(ret == 0); + path = ".//a"; + ret = dsn::utils::filesystem::get_normalized_path(path, npath); + EXPECT_TRUE(ret == 0); #ifdef _WIN32 - EXPECT_TRUE(npath == ".\\a"); + EXPECT_TRUE(npath == ".\\a"); #else - EXPECT_TRUE(npath == "./a"); + EXPECT_TRUE(npath == "./a"); #endif - path = "./a/"; - ret = dsn::utils::filesystem::get_normalized_path(path, npath); - EXPECT_TRUE(ret == 0); + path = "./a/"; + ret = dsn::utils::filesystem::get_normalized_path(path, npath); + EXPECT_TRUE(ret == 0); #ifdef _WIN32 - EXPECT_TRUE(npath == ".\\a"); + EXPECT_TRUE(npath == ".\\a"); #else - EXPECT_TRUE(npath == "./a"); + EXPECT_TRUE(npath == "./a"); #endif - path = "./a/b"; - ret = dsn::utils::filesystem::get_normalized_path(path, npath); - EXPECT_TRUE(ret == 0); + path = "./a/b"; + ret = dsn::utils::filesystem::get_normalized_path(path, npath); + EXPECT_TRUE(ret == 0); #ifdef _WIN32 - EXPECT_TRUE(npath == ".\\a\\b"); + EXPECT_TRUE(npath == ".\\a\\b"); #else - EXPECT_TRUE(npath == path); + EXPECT_TRUE(npath == path); #endif - path = "./a/b/"; - ret = dsn::utils::filesystem::get_normalized_path(path, npath); - EXPECT_TRUE(ret == 0); + path = "./a/b/"; + ret = dsn::utils::filesystem::get_normalized_path(path, npath); + EXPECT_TRUE(ret == 0); #ifdef _WIN32 - EXPECT_TRUE(npath == ".\\a\\b"); + EXPECT_TRUE(npath == ".\\a\\b"); #else - EXPECT_TRUE(npath == "./a/b"); + EXPECT_TRUE(npath == "./a/b"); #endif - path = ".///a////b///"; - ret = dsn::utils::filesystem::get_normalized_path(path, npath); - EXPECT_TRUE(ret == 0); + path = ".///a////b///"; + ret = dsn::utils::filesystem::get_normalized_path(path, npath); + EXPECT_TRUE(ret == 0); #ifdef _WIN32 - EXPECT_TRUE(npath == ".\\a\\b"); + EXPECT_TRUE(npath == ".\\a\\b"); #else - EXPECT_TRUE(npath == "./a/b"); + EXPECT_TRUE(npath == "./a/b"); #endif - path = ".."; - ret = dsn::utils::filesystem::get_normalized_path(path, npath); - EXPECT_TRUE(ret == 0); - EXPECT_TRUE(npath == path); + path = ".."; + ret = dsn::utils::filesystem::get_normalized_path(path, npath); + EXPECT_TRUE(ret == 0); + EXPECT_TRUE(npath == path); - path = "../"; - ret = dsn::utils::filesystem::get_normalized_path(path, npath); - EXPECT_TRUE(ret == 0); - EXPECT_TRUE(npath == ".."); + path = "../"; + ret = dsn::utils::filesystem::get_normalized_path(path, npath); + EXPECT_TRUE(ret == 0); + EXPECT_TRUE(npath == ".."); - path = "../a"; - ret = dsn::utils::filesystem::get_normalized_path(path, npath); - EXPECT_TRUE(ret == 0); + path = "../a"; + ret = dsn::utils::filesystem::get_normalized_path(path, npath); + EXPECT_TRUE(ret == 0); #ifdef _WIN32 - EXPECT_TRUE(npath == "..\\a"); + EXPECT_TRUE(npath == "..\\a"); #else - EXPECT_TRUE(npath == path); + EXPECT_TRUE(npath == path); #endif - path = "..//a"; - ret = dsn::utils::filesystem::get_normalized_path(path, npath); - EXPECT_TRUE(ret == 0); + path = "..//a"; + ret = dsn::utils::filesystem::get_normalized_path(path, npath); + EXPECT_TRUE(ret == 0); #ifdef _WIN32 - EXPECT_TRUE(npath == "..\\a"); + EXPECT_TRUE(npath == "..\\a"); #else - EXPECT_TRUE(npath == "../a"); + EXPECT_TRUE(npath == "../a"); #endif - path = "../a/"; - ret = dsn::utils::filesystem::get_normalized_path(path, npath); - EXPECT_TRUE(ret == 0); + path = "../a/"; + ret = dsn::utils::filesystem::get_normalized_path(path, npath); + EXPECT_TRUE(ret == 0); #ifdef _WIN32 - EXPECT_TRUE(npath == "..\\a"); + EXPECT_TRUE(npath == "..\\a"); #else - EXPECT_TRUE(npath == "../a"); + EXPECT_TRUE(npath == "../a"); #endif - path = "../a/b"; - ret = dsn::utils::filesystem::get_normalized_path(path, npath); - EXPECT_TRUE(ret == 0); + path = "../a/b"; + ret = dsn::utils::filesystem::get_normalized_path(path, npath); + EXPECT_TRUE(ret == 0); #ifdef _WIN32 - EXPECT_TRUE(npath == "..\\a\\b"); + EXPECT_TRUE(npath == "..\\a\\b"); #else - EXPECT_TRUE(npath == path); + EXPECT_TRUE(npath == path); #endif - path = "../a/b/"; - ret = dsn::utils::filesystem::get_normalized_path(path, npath); - EXPECT_TRUE(ret == 0); + path = "../a/b/"; + ret = dsn::utils::filesystem::get_normalized_path(path, npath); + EXPECT_TRUE(ret == 0); #ifdef _WIN32 - EXPECT_TRUE(npath == "..\\a\\b"); + EXPECT_TRUE(npath == "..\\a\\b"); #else - EXPECT_TRUE(npath == "../a/b"); + EXPECT_TRUE(npath == "../a/b"); #endif - path = "..///a////b///"; - ret = dsn::utils::filesystem::get_normalized_path(path, npath); - EXPECT_TRUE(ret == 0); + path = "..///a////b///"; + ret = dsn::utils::filesystem::get_normalized_path(path, npath); + EXPECT_TRUE(ret == 0); #ifdef _WIN32 - EXPECT_TRUE(npath == "..\\a\\b"); + EXPECT_TRUE(npath == "..\\a\\b"); #else - EXPECT_TRUE(npath == "../a/b"); + EXPECT_TRUE(npath == "../a/b"); #endif -} + } -static void file_utils_test_get_current_directory() -{ - std::string path; - bool ret; + void file_utils_test_get_current_directory() + { + std::string path; + bool ret; - path = ""; - ret = dsn::utils::filesystem::get_current_directory(path); - EXPECT_TRUE(ret); - EXPECT_TRUE(!path.empty()); -} + path = ""; + ret = dsn::utils::filesystem::get_current_directory(path); + EXPECT_TRUE(ret); + EXPECT_TRUE(!path.empty()); + } -static void file_utils_test_path_combine() -{ - std::string path; - std::string path1; - std::string path2; + void file_utils_test_path_combine() + { + std::string path; + std::string path1; + std::string path2; - path1 = ""; - path2 = ""; - path = dsn::utils::filesystem::path_combine(path1, path2); - EXPECT_TRUE(path == ""); + path1 = ""; + path2 = ""; + path = dsn::utils::filesystem::path_combine(path1, path2); + EXPECT_TRUE(path == ""); - path1 = "c:"; - path2 = "Windows\\explorer.exe"; - path = dsn::utils::filesystem::path_combine(path1, path2); + path1 = "c:"; + path2 = "Windows\\explorer.exe"; + path = dsn::utils::filesystem::path_combine(path1, path2); #ifdef _WIN32 - EXPECT_TRUE(path == "c:Windows\\explorer.exe"); + EXPECT_TRUE(path == "c:Windows\\explorer.exe"); #else - EXPECT_TRUE(path == "c:/Windows\\explorer.exe"); + EXPECT_TRUE(path == "c:/Windows\\explorer.exe"); #endif - path1 = "c:"; - path2 = "\\Windows\\explorer.exe"; - path = dsn::utils::filesystem::path_combine(path1, path2); + path1 = "c:"; + path2 = "\\Windows\\explorer.exe"; + path = dsn::utils::filesystem::path_combine(path1, path2); #ifdef _WIN32 - EXPECT_TRUE(path == "c:\\Windows\\explorer.exe"); + EXPECT_TRUE(path == "c:\\Windows\\explorer.exe"); #else - EXPECT_TRUE(path == "c:/Windows\\explorer.exe"); + EXPECT_TRUE(path == "c:/Windows\\explorer.exe"); #endif - path1 = "c:\\"; - path2 = "\\Windows\\explorer.exe"; - path = dsn::utils::filesystem::path_combine(path1, path2); + path1 = "c:\\"; + path2 = "\\Windows\\explorer.exe"; + path = dsn::utils::filesystem::path_combine(path1, path2); #ifdef _WIN32 - EXPECT_TRUE(path == "c:\\Windows\\explorer.exe"); + EXPECT_TRUE(path == "c:\\Windows\\explorer.exe"); #else - EXPECT_TRUE(path == "c:\\/Windows\\explorer.exe"); + EXPECT_TRUE(path == "c:\\/Windows\\explorer.exe"); #endif - path1 = "/bin"; - path2 = "ls"; - path = dsn::utils::filesystem::path_combine(path1, path2); + path1 = "/bin"; + path2 = "ls"; + path = dsn::utils::filesystem::path_combine(path1, path2); #ifdef _WIN32 - EXPECT_TRUE(path == "\\bin\\ls"); + EXPECT_TRUE(path == "\\bin\\ls"); #else - EXPECT_TRUE(path == "/bin/ls"); + EXPECT_TRUE(path == "/bin/ls"); #endif - path1 = "/bin/"; - path2 = "ls"; - path = dsn::utils::filesystem::path_combine(path1, path2); + path1 = "/bin/"; + path2 = "ls"; + path = dsn::utils::filesystem::path_combine(path1, path2); #ifdef _WIN32 - EXPECT_TRUE(path == "\\bin\\ls"); + EXPECT_TRUE(path == "\\bin\\ls"); #else - EXPECT_TRUE(path == "/bin/ls"); + EXPECT_TRUE(path == "/bin/ls"); #endif - path1 = "/bin"; - path2 = "/ls"; - path = dsn::utils::filesystem::path_combine(path1, path2); + path1 = "/bin"; + path2 = "/ls"; + path = dsn::utils::filesystem::path_combine(path1, path2); #ifdef _WIN32 - EXPECT_TRUE(path == "\\bin\\ls"); + EXPECT_TRUE(path == "\\bin\\ls"); #else - EXPECT_TRUE(path == "/bin/ls"); + EXPECT_TRUE(path == "/bin/ls"); #endif - path1 = "/bin/"; - path2 = "/ls"; - path = dsn::utils::filesystem::path_combine(path1, path2); + path1 = "/bin/"; + path2 = "/ls"; + path = dsn::utils::filesystem::path_combine(path1, path2); #ifdef _WIN32 - EXPECT_TRUE(path == "\\bin\\ls"); + EXPECT_TRUE(path == "\\bin\\ls"); #else - EXPECT_TRUE(path == "/bin/ls"); + EXPECT_TRUE(path == "/bin/ls"); #endif -} + } -static void file_utils_test_get_file_name() -{ - std::string path1; - std::string path2; + void file_utils_test_get_file_name() + { + std::string path1; + std::string path2; - path1 = ""; - path2 = dsn::utils::filesystem::get_file_name(path1); - EXPECT_TRUE(path2 == ""); + path1 = ""; + path2 = dsn::utils::filesystem::get_file_name(path1); + EXPECT_TRUE(path2 == ""); - path1 = "c:"; - path2 = dsn::utils::filesystem::get_file_name(path1); + path1 = "c:"; + path2 = dsn::utils::filesystem::get_file_name(path1); #ifdef _WIN32 - EXPECT_TRUE(path2 == ""); + EXPECT_TRUE(path2 == ""); #else - EXPECT_TRUE(path2 == "c:"); + EXPECT_TRUE(path2 == "c:"); #endif - path1 = "c:\\"; - path2 = dsn::utils::filesystem::get_file_name(path1); - EXPECT_TRUE(path2 == ""); + path1 = "c:\\"; + path2 = dsn::utils::filesystem::get_file_name(path1); + EXPECT_TRUE(path2 == ""); - path1 = "c:1.txt"; - path2 = dsn::utils::filesystem::get_file_name(path1); + path1 = "c:1.txt"; + path2 = dsn::utils::filesystem::get_file_name(path1); #ifdef _WIN32 - EXPECT_TRUE(path2 == "1.txt"); + EXPECT_TRUE(path2 == "1.txt"); #else - EXPECT_TRUE(path2 == "c:1.txt"); + EXPECT_TRUE(path2 == "c:1.txt"); #endif - path1 = "c:\\1.txt"; - path2 = dsn::utils::filesystem::get_file_name(path1); - EXPECT_TRUE(path2 == "1.txt"); + path1 = "c:\\1.txt"; + path2 = dsn::utils::filesystem::get_file_name(path1); + EXPECT_TRUE(path2 == "1.txt"); - path1 = "c:\\Windows\\1.txt"; - path2 = dsn::utils::filesystem::get_file_name(path1); - EXPECT_TRUE(path2 == "1.txt"); + path1 = "c:\\Windows\\1.txt"; + path2 = dsn::utils::filesystem::get_file_name(path1); + EXPECT_TRUE(path2 == "1.txt"); - path1 = "/bin/"; - path2 = dsn::utils::filesystem::get_file_name(path1); - EXPECT_TRUE(path2 == ""); + path1 = "/bin/"; + path2 = dsn::utils::filesystem::get_file_name(path1); + EXPECT_TRUE(path2 == ""); - path1 = "/bin/ls"; - path2 = dsn::utils::filesystem::get_file_name(path1); - EXPECT_TRUE(path2 == "ls"); -} + path1 = "/bin/ls"; + path2 = dsn::utils::filesystem::get_file_name(path1); + EXPECT_TRUE(path2 == "ls"); + } -static void file_utils_test_create() -{ - std::string path; - bool ret; - - path = "./file_utils_temp.txt"; - ret = dsn::utils::filesystem::create_file(path); - EXPECT_TRUE(ret); - ret = dsn::utils::filesystem::file_exists(path); - EXPECT_TRUE(ret); - - time_t current_time = ::time(nullptr); - EXPECT_TRUE(current_time != 1); - - std::ofstream myfile(path.c_str(), std::ios::out | std::ios::app | std::ios::binary); - EXPECT_TRUE(myfile.is_open()); - myfile << "Hello world!"; - myfile.close(); - - time_t last_write_time; - ret = dsn::utils::filesystem::last_write_time(path, last_write_time); - EXPECT_TRUE(ret); - EXPECT_TRUE((last_write_time != -1) && (last_write_time >= current_time)); - - path = "./file_utils_temp"; - ret = dsn::utils::filesystem::create_directory(path); - EXPECT_TRUE(ret); - ret = dsn::utils::filesystem::directory_exists(path); - EXPECT_TRUE(ret); - - path = "./file_utils_temp/a/b/c/d//"; - ret = dsn::utils::filesystem::create_directory(path); - EXPECT_TRUE(ret); - ret = dsn::utils::filesystem::directory_exists(path); - EXPECT_TRUE(ret); - - path = "./file_utils_temp/a/1.txt"; - ret = dsn::utils::filesystem::create_file(path); - EXPECT_TRUE(ret); - ret = dsn::utils::filesystem::file_exists(path); - EXPECT_TRUE(ret); - - path = "./file_utils_temp/a/1.txt"; - ret = dsn::utils::filesystem::create_file(path); - EXPECT_TRUE(ret); - ret = dsn::utils::filesystem::file_exists(path); - EXPECT_TRUE(ret); - - path = "./file_utils_temp/a/2.txt"; - ret = dsn::utils::filesystem::create_file(path); - EXPECT_TRUE(ret); - ret = dsn::utils::filesystem::file_exists(path); - EXPECT_TRUE(ret); - - path = "./file_utils_temp/b/c/d/1.txt"; - ret = dsn::utils::filesystem::create_file(path); - EXPECT_TRUE(ret); - ret = dsn::utils::filesystem::file_exists(path); - EXPECT_TRUE(ret); -} + void file_utils_test_create() + { + std::string path; + bool ret; + + path = "./file_utils_temp.txt"; + ret = dsn::utils::filesystem::create_file(path); + EXPECT_TRUE(ret); + ret = dsn::utils::filesystem::file_exists(path); + EXPECT_TRUE(ret); + + time_t current_time = ::time(nullptr); + EXPECT_TRUE(current_time != 1); + + std::ofstream myfile(path.c_str(), std::ios::out | std::ios::app | std::ios::binary); + EXPECT_TRUE(myfile.is_open()); + myfile << "Hello world!"; + myfile.close(); + + time_t last_write_time; + ret = dsn::utils::filesystem::last_write_time(path, last_write_time); + EXPECT_TRUE(ret); + EXPECT_TRUE((last_write_time != -1) && (last_write_time >= current_time)); + + path = "./file_utils_temp"; + ret = dsn::utils::filesystem::create_directory(path); + EXPECT_TRUE(ret); + ret = dsn::utils::filesystem::directory_exists(path); + EXPECT_TRUE(ret); + + path = "./file_utils_temp/a/b/c/d//"; + ret = dsn::utils::filesystem::create_directory(path); + EXPECT_TRUE(ret); + ret = dsn::utils::filesystem::directory_exists(path); + EXPECT_TRUE(ret); + + path = "./file_utils_temp/a/1.txt"; + ret = dsn::utils::filesystem::create_file(path); + EXPECT_TRUE(ret); + ret = dsn::utils::filesystem::file_exists(path); + EXPECT_TRUE(ret); + + path = "./file_utils_temp/a/1.txt"; + ret = dsn::utils::filesystem::create_file(path); + EXPECT_TRUE(ret); + ret = dsn::utils::filesystem::file_exists(path); + EXPECT_TRUE(ret); + + path = "./file_utils_temp/a/2.txt"; + ret = dsn::utils::filesystem::create_file(path); + EXPECT_TRUE(ret); + ret = dsn::utils::filesystem::file_exists(path); + EXPECT_TRUE(ret); + + path = "./file_utils_temp/b/c/d/1.txt"; + ret = dsn::utils::filesystem::create_file(path); + EXPECT_TRUE(ret); + ret = dsn::utils::filesystem::file_exists(path); + EXPECT_TRUE(ret); + } -static void file_utils_test_file_size() -{ - std::string path; - int64_t sz; - bool ret; - - path = "./file_utils_temp.txt"; - ret = dsn::utils::filesystem::file_size(path, dsn::utils::FileDataType::kNonSensitive, sz); - ASSERT_TRUE(ret); - ASSERT_EQ(12, sz); - - path = "./file_utils_temp2.txt"; - ret = dsn::utils::filesystem::file_size(path, dsn::utils::FileDataType::kNonSensitive, sz); - EXPECT_FALSE(ret); -} + void file_utils_test_file_size() + { + std::string path; + int64_t sz; + bool ret; -static void file_utils_test_path_exists() -{ - std::string path; - bool ret; + path = "./file_utils_temp.txt"; + ret = dsn::utils::filesystem::file_size(path, dsn::utils::FileDataType::kNonSensitive, sz); + ASSERT_TRUE(ret); + ASSERT_EQ(12 + extra_encrypted_file_size(), sz); + + path = "./file_utils_temp2.txt"; + ret = dsn::utils::filesystem::file_size(path, dsn::utils::FileDataType::kNonSensitive, sz); + EXPECT_FALSE(ret); + } + + void file_utils_test_path_exists() + { + std::string path; + bool ret; - path = "c:\\"; - ret = dsn::utils::filesystem::path_exists(path); + path = "c:\\"; + ret = dsn::utils::filesystem::path_exists(path); #ifdef _WIN32 - EXPECT_TRUE(ret); + EXPECT_TRUE(ret); #else - EXPECT_FALSE(ret); + EXPECT_FALSE(ret); #endif - path = "c:\\"; - ret = dsn::utils::filesystem::directory_exists(path); + path = "c:\\"; + ret = dsn::utils::filesystem::directory_exists(path); #ifdef _WIN32 - EXPECT_TRUE(ret); + EXPECT_TRUE(ret); #else - EXPECT_FALSE(ret); + EXPECT_FALSE(ret); #endif - path = "c:\\"; - ret = dsn::utils::filesystem::file_exists(path); + path = "c:\\"; + ret = dsn::utils::filesystem::file_exists(path); #ifdef _WIN32 - EXPECT_FALSE(ret); + EXPECT_FALSE(ret); #else - EXPECT_FALSE(ret); + EXPECT_FALSE(ret); #endif - path = "/"; - ret = dsn::utils::filesystem::path_exists(path); - EXPECT_TRUE(ret); + path = "/"; + ret = dsn::utils::filesystem::path_exists(path); + EXPECT_TRUE(ret); - path = "/"; - ret = dsn::utils::filesystem::directory_exists(path); - EXPECT_TRUE(ret); + path = "/"; + ret = dsn::utils::filesystem::directory_exists(path); + EXPECT_TRUE(ret); - path = "/"; - ret = dsn::utils::filesystem::file_exists(path); - EXPECT_FALSE(ret); + path = "/"; + ret = dsn::utils::filesystem::file_exists(path); + EXPECT_FALSE(ret); - path = "./not_exists_not_exists"; - ret = dsn::utils::filesystem::path_exists(path); - EXPECT_FALSE(ret); + path = "./not_exists_not_exists"; + ret = dsn::utils::filesystem::path_exists(path); + EXPECT_FALSE(ret); - path = "c:\\Windows\\System32\\notepad.exe"; - ret = dsn::utils::filesystem::path_exists(path); + path = "c:\\Windows\\System32\\notepad.exe"; + ret = dsn::utils::filesystem::path_exists(path); #ifdef _WIN32 - EXPECT_TRUE(ret); + EXPECT_TRUE(ret); #else - EXPECT_FALSE(ret); + EXPECT_FALSE(ret); #endif - path = "c:\\Windows\\System32\\notepad.exe"; - ret = dsn::utils::filesystem::directory_exists(path); - EXPECT_FALSE(ret); + path = "c:\\Windows\\System32\\notepad.exe"; + ret = dsn::utils::filesystem::directory_exists(path); + EXPECT_FALSE(ret); - path = "c:\\Windows\\System32\\notepad.exe"; - ret = dsn::utils::filesystem::file_exists(path); + path = "c:\\Windows\\System32\\notepad.exe"; + ret = dsn::utils::filesystem::file_exists(path); #ifdef _WIN32 - EXPECT_TRUE(ret); + EXPECT_TRUE(ret); #else - EXPECT_FALSE(ret); + EXPECT_FALSE(ret); #endif - path = "/bin/ls"; - ret = dsn::utils::filesystem::path_exists(path); + path = "/bin/ls"; + ret = dsn::utils::filesystem::path_exists(path); #ifdef _WIN32 - EXPECT_FALSE(ret); + EXPECT_FALSE(ret); #else - EXPECT_TRUE(ret); + EXPECT_TRUE(ret); #endif - path = "/bin/ls"; - ret = dsn::utils::filesystem::directory_exists(path); - EXPECT_FALSE(ret); + path = "/bin/ls"; + ret = dsn::utils::filesystem::directory_exists(path); + EXPECT_FALSE(ret); - path = "/bin/ls"; - ret = dsn::utils::filesystem::file_exists(path); + path = "/bin/ls"; + ret = dsn::utils::filesystem::file_exists(path); #ifdef _WIN32 - EXPECT_FALSE(ret); + EXPECT_FALSE(ret); #else - EXPECT_TRUE(ret); + EXPECT_TRUE(ret); #endif -} + } -static void file_utils_test_get_paths() -{ - std::string path; - bool ret; - std::vector file_list; - - path = "."; - ret = dsn::utils::filesystem::get_subfiles(path, file_list, false); - EXPECT_TRUE(ret); -#ifdef _WIN32 - EXPECT_TRUE(file_list.size() >= 3); -#else - EXPECT_TRUE(file_list.size() >= 2); -#endif - file_list.clear(); - - path = "."; - ret = dsn::utils::filesystem::get_subfiles(path, file_list, true); - EXPECT_TRUE(ret); - EXPECT_TRUE(file_list.size() >= 3); - file_list.clear(); - - path = "../../"; - ret = dsn::utils::filesystem::get_subfiles(path, file_list, true); - EXPECT_TRUE(ret); - EXPECT_TRUE(file_list.size() >= 3); - file_list.clear(); - - path = "./file_utils_temp/"; - ret = dsn::utils::filesystem::get_subfiles(path, file_list, true); - EXPECT_TRUE(ret); - EXPECT_TRUE(file_list.size() == 3); - file_list.clear(); - - path = "./file_utils_temp/"; - ret = dsn::utils::filesystem::get_subdirectories(path, file_list, true); - EXPECT_TRUE(ret); - EXPECT_TRUE(file_list.size() == 7); - file_list.clear(); - - path = "./file_utils_temp/"; - ret = dsn::utils::filesystem::get_subdirectories(path, file_list, false); - EXPECT_TRUE(ret); - EXPECT_TRUE(file_list.size() == 2); - file_list.clear(); - - path = "./file_utils_temp/"; - ret = dsn::utils::filesystem::get_subpaths(path, file_list, true); - EXPECT_TRUE(ret); - EXPECT_TRUE(file_list.size() == 10); - file_list.clear(); - - path = "./file_utils_temp/"; - ret = dsn::utils::filesystem::get_subpaths(path, file_list, false); - EXPECT_TRUE(ret); - EXPECT_TRUE(file_list.size() == 2); - file_list.clear(); - - path = "./file_utils_temp/a/"; - ret = dsn::utils::filesystem::get_subfiles(path, file_list, false); - EXPECT_TRUE(ret); - EXPECT_TRUE(file_list.size() == 2); - file_list.clear(); - - path = "./file_utils_temp/a/"; - ret = dsn::utils::filesystem::get_subpaths(path, file_list, false); - EXPECT_TRUE(ret); - EXPECT_TRUE(file_list.size() == 3); - file_list.clear(); -} + void file_utils_test_get_paths() + { + std::string path; + bool ret; + std::vector file_list; + + path = "."; + ret = dsn::utils::filesystem::get_subfiles(path, file_list, false); + EXPECT_TRUE(ret); +#ifdef _WIN32 + EXPECT_TRUE(file_list.size() >= 3); +#else + EXPECT_TRUE(file_list.size() >= 2); +#endif + file_list.clear(); + + path = "."; + ret = dsn::utils::filesystem::get_subfiles(path, file_list, true); + EXPECT_TRUE(ret); + EXPECT_TRUE(file_list.size() >= 3); + file_list.clear(); + + path = "../../"; + ret = dsn::utils::filesystem::get_subfiles(path, file_list, true); + EXPECT_TRUE(ret); + EXPECT_TRUE(file_list.size() >= 3); + file_list.clear(); + + path = "./file_utils_temp/"; + ret = dsn::utils::filesystem::get_subfiles(path, file_list, true); + EXPECT_TRUE(ret); + EXPECT_TRUE(file_list.size() == 3); + file_list.clear(); + + path = "./file_utils_temp/"; + ret = dsn::utils::filesystem::get_subdirectories(path, file_list, true); + EXPECT_TRUE(ret); + EXPECT_TRUE(file_list.size() == 7); + file_list.clear(); + + path = "./file_utils_temp/"; + ret = dsn::utils::filesystem::get_subdirectories(path, file_list, false); + EXPECT_TRUE(ret); + EXPECT_TRUE(file_list.size() == 2); + file_list.clear(); + + path = "./file_utils_temp/"; + ret = dsn::utils::filesystem::get_subpaths(path, file_list, true); + EXPECT_TRUE(ret); + EXPECT_TRUE(file_list.size() == 10); + file_list.clear(); + + path = "./file_utils_temp/"; + ret = dsn::utils::filesystem::get_subpaths(path, file_list, false); + EXPECT_TRUE(ret); + EXPECT_TRUE(file_list.size() == 2); + file_list.clear(); + + path = "./file_utils_temp/a/"; + ret = dsn::utils::filesystem::get_subfiles(path, file_list, false); + EXPECT_TRUE(ret); + EXPECT_TRUE(file_list.size() == 2); + file_list.clear(); + + path = "./file_utils_temp/a/"; + ret = dsn::utils::filesystem::get_subpaths(path, file_list, false); + EXPECT_TRUE(ret); + EXPECT_TRUE(file_list.size() == 3); + file_list.clear(); + } -static void file_utils_test_rename() -{ - std::string path; - std::string path2; - bool ret; - - path = "./file_utils_temp/b/c/d/1.txt"; - path2 = "./file_utils_temp/b/c/d/2.txt"; - ret = dsn::utils::filesystem::rename_path(path, path2); - EXPECT_TRUE(ret); - ret = dsn::utils::filesystem::file_exists(path); - EXPECT_FALSE(ret); - ret = dsn::utils::filesystem::file_exists(path2); - EXPECT_TRUE(ret); - ret = dsn::utils::filesystem::rename_path(path, path2); - EXPECT_FALSE(ret); -} + void file_utils_test_rename() + { + std::string path; + std::string path2; + bool ret; + + path = "./file_utils_temp/b/c/d/1.txt"; + path2 = "./file_utils_temp/b/c/d/2.txt"; + ret = dsn::utils::filesystem::rename_path(path, path2); + EXPECT_TRUE(ret); + ret = dsn::utils::filesystem::file_exists(path); + EXPECT_FALSE(ret); + ret = dsn::utils::filesystem::file_exists(path2); + EXPECT_TRUE(ret); + ret = dsn::utils::filesystem::rename_path(path, path2); + EXPECT_FALSE(ret); + } -static void file_utils_test_remove() -{ - std::string path; - std::vector file_list; - bool ret; - - path = "./file_utils_temp.txt"; - ret = dsn::utils::filesystem::remove_path(path); - EXPECT_TRUE(ret); - ret = dsn::utils::filesystem::file_exists(path); - EXPECT_FALSE(ret); - - path = "./file_utils_temp/a/2.txt"; - ret = dsn::utils::filesystem::remove_path(path); - EXPECT_TRUE(ret); - ret = dsn::utils::filesystem::remove_path(path); - EXPECT_TRUE(ret); - - path = "./file_utils_temp/"; - ret = dsn::utils::filesystem::remove_path(path); - EXPECT_TRUE(ret); - ret = dsn::utils::filesystem::directory_exists(path); - EXPECT_FALSE(ret); -} + void file_utils_test_remove() + { + std::string path; + std::vector file_list; + bool ret; + + path = "./file_utils_temp.txt"; + ret = dsn::utils::filesystem::remove_path(path); + EXPECT_TRUE(ret); + ret = dsn::utils::filesystem::file_exists(path); + EXPECT_FALSE(ret); + + path = "./file_utils_temp/a/2.txt"; + ret = dsn::utils::filesystem::remove_path(path); + EXPECT_TRUE(ret); + ret = dsn::utils::filesystem::remove_path(path); + EXPECT_TRUE(ret); + + path = "./file_utils_temp/"; + ret = dsn::utils::filesystem::remove_path(path); + EXPECT_TRUE(ret); + ret = dsn::utils::filesystem::directory_exists(path); + EXPECT_FALSE(ret); + } + + void file_utils_test_cleanup() {} +}; -static void file_utils_test_cleanup() {} +INSTANTIATE_TEST_CASE_P(, file_utils, ::testing::Values(false, true)); -TEST(core, file_utils) +TEST_P(file_utils, basic) { file_utils_test_setup(); file_utils_test_get_process_image_path();