A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
system-path-test-suite.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2026 Centre Tecnologic de Telecomunicacions de Catalunya (CTTC)
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 */
6
7#include "ns3/system-path.h"
8#include "ns3/test.h"
9
10#include <filesystem>
11#include <fstream>
12
13using namespace ns3;
14using namespace ns3::SystemPath;
15
16/**
17 * @ingroup core-tests
18 * @defgroup system-path-tests SystemPath test suite
19 */
20
21/**
22 * @ingroup system-path-tests
23 *
24 * @brief Tests for SystemPath::Exists().
25 *
26 * Regression test for issue #781: Exists() used to derive the directory part
27 * of the path and search it for the final component, which returned a wrong
28 * result for a bare relative filename (the derived directory was the empty
29 * string) and mishandled trailing separators. It now delegates to
30 * std::filesystem and must agree with it.
31 */
33{
34 public:
36 : TestCase("SystemPath::Exists matches std::filesystem::exists")
37 {
38 }
39
40 private:
41 void DoRun() override;
42};
43
44void
46{
47 namespace fs = std::filesystem;
48
49 // RAII guards so that a failing assertion cannot leak the temporary
50 // directory or leave the process working directory changed for later test
51 // suites: every NS_TEST_ASSERT_MSG_EQ can return early from DoRun().
52 struct DirRemover
53 {
54 std::string path; ///< Directory removed on scope exit.
55
56 ~DirRemover()
57 {
58 std::error_code ec;
59 std::filesystem::remove_all(path, ec);
60 }
61 };
62
63 struct CwdRestorer
64 {
65 std::filesystem::path original; ///< Working directory restored on scope exit.
66
67 ~CwdRestorer()
68 {
69 std::error_code ec;
70 std::filesystem::current_path(original, ec);
71 }
72 };
73
74 // Create a unique temporary directory and a file inside it.
75 std::string tempDir = MakeTemporaryDirectoryName();
76 MakeDirectories(tempDir);
77 DirRemover dirRemover{tempDir};
78 NS_TEST_ASSERT_MSG_EQ(Exists(tempDir), true, "Created directory should exist");
79 // A trailing separator must still resolve to the existing directory. The
80 // literal '/' is intentional: std::filesystem accepts it as a separator on
81 // every platform, including Windows where SYSTEM_PATH_SEP is '\\'.
82 NS_TEST_ASSERT_MSG_EQ(Exists(tempDir + "/"),
83 true,
84 "Directory with trailing slash should exist");
85
86 std::string filePath = Append(tempDir, "a-file.txt");
87 {
88 std::ofstream f(filePath);
89 f << "ns-3";
90 }
91 NS_TEST_ASSERT_MSG_EQ(Exists(filePath), true, "Created file should exist");
92 NS_TEST_ASSERT_MSG_EQ(Exists(Append(tempDir, "missing.txt")),
93 false,
94 "Non-existent file must not exist");
95 NS_TEST_ASSERT_MSG_EQ(Exists(Append(tempDir, "no/such/dir")),
96 false,
97 "Non-existent nested path must not exist");
98
99 // Regression for issue #781: a bare relative filename (no separator) that
100 // exists in the current working directory must be reported as existing.
101 CwdRestorer cwdRestorer{fs::current_path()};
102 fs::current_path(tempDir);
103 NS_TEST_ASSERT_MSG_EQ(Exists("a-file.txt"),
104 true,
105 "Bare filename present in the current directory must exist");
106 NS_TEST_ASSERT_MSG_EQ(Exists("definitely-not-here.txt"),
107 false,
108 "Bare filename absent from the current directory must not exist");
109
110 // dirRemover and cwdRestorer clean up on scope exit (cwd restored first,
111 // then the directory removed), including when an assertion fails early.
112}
113
114/**
115 * @ingroup system-path-tests
116 *
117 * @brief SystemPath test suite.
118 */
120{
121 public:
127};
128
129static SystemPathTestSuite g_systemPathTestSuite; //!< Static variable for test initialization
Tests for SystemPath::Exists().
void DoRun() override
Implementation to actually run this TestCase.
SystemPath test suite.
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:296
@ QUICK
Fast test.
Definition test.h:1057
TestCase(const TestCase &)=delete
Caller graph was not generated because of its size.
Type
Type of test.
Definition test.h:1271
@ UNIT
This test suite implements a Unit Test.
Definition test.h:1273
TestSuite(std::string name, Type type=Type::UNIT)
Construct a new test suite.
Definition test.cc:494
bool Exists(const std::string path)
Check if a path exists.
void MakeDirectories(std::string path)
Create all the directories leading to path.
std::string MakeTemporaryDirectoryName()
Get the name of a temporary directory.
std::string Append(std::string left, std::string right)
Join two file system path elements.
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition test.h:133
Namespace for various file and directory path functions.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static SystemPathTestSuite g_systemPathTestSuite
Static variable for test initialization.