A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
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
13
using namespace
ns3
;
14
using 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
*/
32
class
SystemPathExistsTestCaseBug781
:
public
TestCase
33
{
34
public
:
35
SystemPathExistsTestCaseBug781
()
36
:
TestCase
(
"SystemPath::Exists matches std::filesystem::exists"
)
37
{
38
}
39
40
private
:
41
void
DoRun
()
override
;
42
};
43
44
void
45
SystemPathExistsTestCaseBug781::DoRun
()
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
*/
119
class
SystemPathTestSuite
:
public
TestSuite
120
{
121
public
:
122
SystemPathTestSuite
()
123
:
TestSuite
(
"system-path"
,
Type
::
UNIT
)
124
{
125
AddTestCase
(
new
SystemPathExistsTestCaseBug781
,
TestCase::Duration::QUICK
);
126
}
127
};
128
129
static
SystemPathTestSuite
g_systemPathTestSuite
;
//!< Static variable for test initialization
SystemPathExistsTestCaseBug781
Tests for SystemPath::Exists().
Definition
system-path-test-suite.cc:33
SystemPathExistsTestCaseBug781::SystemPathExistsTestCaseBug781
SystemPathExistsTestCaseBug781()
Definition
system-path-test-suite.cc:35
SystemPathExistsTestCaseBug781::DoRun
void DoRun() override
Implementation to actually run this TestCase.
Definition
system-path-test-suite.cc:45
SystemPathTestSuite
SystemPath test suite.
Definition
system-path-test-suite.cc:120
SystemPathTestSuite::SystemPathTestSuite
SystemPathTestSuite()
Definition
system-path-test-suite.cc:122
ns3::TestCase::AddTestCase
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition
test.cc:296
ns3::TestCase::Duration::QUICK
@ QUICK
Fast test.
Definition
test.h:1057
ns3::TestCase::TestCase
TestCase(const TestCase &)=delete
Caller graph was not generated because of its size.
ns3::TestSuite::Type
Type
Type of test.
Definition
test.h:1271
ns3::TestSuite::Type::UNIT
@ UNIT
This test suite implements a Unit Test.
Definition
test.h:1273
ns3::TestSuite::TestSuite
TestSuite(std::string name, Type type=Type::UNIT)
Construct a new test suite.
Definition
test.cc:494
ns3::SystemPath::Exists
bool Exists(const std::string path)
Check if a path exists.
Definition
system-path.cc:345
ns3::SystemPath::MakeDirectories
void MakeDirectories(std::string path)
Create all the directories leading to path.
Definition
system-path.cc:328
ns3::SystemPath::MakeTemporaryDirectoryName
std::string MakeTemporaryDirectoryName()
Get the name of a temporary directory.
Definition
system-path.cc:283
ns3::SystemPath::Append
std::string Append(std::string left, std::string right)
Join two file system path elements.
Definition
system-path.cc:218
NS_TEST_ASSERT_MSG_EQ
#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
ns3::SystemPath
Namespace for various file and directory path functions.
Definition
system-path.cc:98
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
g_systemPathTestSuite
static SystemPathTestSuite g_systemPathTestSuite
Static variable for test initialization.
Definition
system-path-test-suite.cc:129
src
core
test
system-path-test-suite.cc
Generated on
for ns-3 by
1.16.1