|
7 | 7 | #include "pal/PseudoRandomGenerator.hpp"
|
8 | 8 | #include "Version.hpp"
|
9 | 9 |
|
| 10 | +#ifdef HAVE_MAT_LOGGING |
| 11 | +#include "pal/PAL.hpp" |
| 12 | +#include <gtest/gtest.h> |
| 13 | +#include <fstream> |
| 14 | +#include <memory> |
| 15 | +#include <string> |
| 16 | +#include <cstdio> |
| 17 | +#if defined(_WIN32) || defined(_WIN64) |
| 18 | +#include <windows.h> |
| 19 | +#else |
| 20 | +#include <unistd.h> |
| 21 | +#endif |
| 22 | +using namespace PAL::detail; |
| 23 | +#endif |
| 24 | + |
10 | 25 | using namespace testing;
|
11 | 26 |
|
12 | 27 | class PalTests : public Test {};
|
@@ -127,3 +142,88 @@ TEST_F(PalTests, SdkVersion)
|
127 | 142 |
|
128 | 143 | EXPECT_THAT(PAL::getSdkVersion(), Eq(v));
|
129 | 144 | }
|
| 145 | + |
| 146 | +#ifdef HAVE_MAT_LOGGING |
| 147 | +class LogInitTest : public Test |
| 148 | +{ |
| 149 | + protected: |
| 150 | + const std::string validPath = "valid/path/"; |
| 151 | + |
| 152 | + void SetUp() override |
| 153 | + { |
| 154 | + // Create the valid path directory and any intermediate directories |
| 155 | + #if defined(_WIN32) || defined(_WIN64) |
| 156 | + CreateDirectoryA("valid", NULL); |
| 157 | + CreateDirectoryA(validPath.c_str(), NULL); |
| 158 | + #else |
| 159 | + mkdir("valid", 0777); |
| 160 | + mkdir(validPath.c_str(), 0777); |
| 161 | + #endif |
| 162 | + } |
| 163 | + |
| 164 | + void TearDown() override |
| 165 | + { |
| 166 | + PAL::detail::log_done(); |
| 167 | + if (!PAL::detail::getDebugLogPath().empty()) |
| 168 | + { |
| 169 | + std::remove(PAL::detail::getDebugLogPath().c_str()); |
| 170 | + } |
| 171 | + |
| 172 | + // Remove the valid path directory |
| 173 | + #if defined(_WIN32) || defined(_WIN64) |
| 174 | + RemoveDirectoryA(validPath.c_str()); |
| 175 | + RemoveDirectoryA("valid"); |
| 176 | + #else |
| 177 | + rmdir(validPath.c_str()); |
| 178 | + rmdir("valid"); |
| 179 | + #endif |
| 180 | + } |
| 181 | +}; |
| 182 | + |
| 183 | +TEST_F(LogInitTest, LogInitDisabled) |
| 184 | +{ |
| 185 | + EXPECT_FALSE(log_init(false, validPath)); |
| 186 | +} |
| 187 | + |
| 188 | +TEST_F(LogInitTest, LogInitValidPath) |
| 189 | +{ |
| 190 | + EXPECT_TRUE(PAL::detail::log_init(true, validPath)); |
| 191 | + EXPECT_TRUE(PAL::detail::getDebugLogStream()->is_open()); |
| 192 | +} |
| 193 | + |
| 194 | +TEST_F(LogInitTest, LogInitParentDirectoryInvalidPath) |
| 195 | +{ |
| 196 | + EXPECT_FALSE(PAL::detail::log_init(true, "invalid/../path/")); |
| 197 | +} |
| 198 | + |
| 199 | +TEST_F(LogInitTest, LogInitPathDoesNotExist) |
| 200 | +{ |
| 201 | + EXPECT_FALSE(PAL::detail::log_init(true, "nonexistent/path/")); |
| 202 | +} |
| 203 | + |
| 204 | +TEST_F(LogInitTest, LogInitAlreadyInitialized) |
| 205 | +{ |
| 206 | + EXPECT_TRUE(PAL::detail::log_init(true, validPath)); |
| 207 | + EXPECT_TRUE(PAL::detail::getDebugLogStream()->is_open()); |
| 208 | + EXPECT_TRUE(PAL::detail::log_init(true, validPath)); // Should return true as it's already initialized |
| 209 | +} |
| 210 | + |
| 211 | +TEST_F(LogInitTest, LogInitPathWithoutTrailingSlash) |
| 212 | +{ |
| 213 | + std::string pathWithoutSlash = "valid/path"; |
| 214 | + EXPECT_TRUE(PAL::detail::log_init(true, pathWithoutSlash)); |
| 215 | + EXPECT_TRUE(PAL::detail::getDebugLogStream()->is_open()); |
| 216 | +} |
| 217 | + |
| 218 | +TEST_F(LogInitTest, LogInitPathWithoutTrailingBackslash) |
| 219 | +{ |
| 220 | +#if defined(_WIN32) || defined(_WIN64) |
| 221 | + std::string pathWithoutBackslash = "valid\\path"; |
| 222 | +#else |
| 223 | + std::string pathWithoutBackslash = "valid//path"; |
| 224 | +#endif |
| 225 | + EXPECT_TRUE(PAL::detail::log_init(true, pathWithoutBackslash)); |
| 226 | + EXPECT_TRUE(PAL::detail::getDebugLogStream()->is_open()); |
| 227 | +} |
| 228 | + |
| 229 | +#endif |
0 commit comments