66#include < iostream>
77#include < sstream>
88#include < vector>
9+ #include < random>
10+ #include < chrono>
911
1012// Helper functions signatures
11- std::string createTestDirectory ();
13+ std::string createTestDirectory (const std::string &test_name );
1214std::string createTestConfig (const std::string &export_path,
1315 const std::string &suffix = " " );
14- void cleanupTestFiles (const std::string &pattern );
16+ void cleanupTestFiles (const std::string &directory );
1517
1618Test (Exporter, empty_results_should_skip_json_export) {
1719 Exporter exporter;
@@ -39,7 +41,7 @@ Test(Exporter, empty_results_should_skip_text_export) {
3941}
4042
4143Test (Exporter, should_export_valid_json_with_results) {
42- std::string test_dir = createTestDirectory ();
44+ std::string test_dir = createTestDirectory (" json_test " );
4345 std::string config_path = createTestConfig (test_dir, " _json" );
4446
4547 std::vector<PortResult> results = {{80 , " http" }, {443 , " https" }, {22 , " ssh" }};
@@ -79,13 +81,12 @@ Test(Exporter, should_export_valid_json_with_results) {
7981 cr_assert (json_content.find (" \" service\" : \" http\" " ) != std::string::npos,
8082 " JSON should contain http service" );
8183
82- cleanupTestFiles (test_dir);
8384 std::filesystem::remove (config_path);
85+ std::filesystem::remove_all (test_dir);
8486}
8587
8688Test (Exporter, should_export_valid_text_with_results) {
87- std::string test_dir = createTestDirectory ();
88- cleanupTestFiles (test_dir);
89+ std::string test_dir = createTestDirectory (" txt_test" );
8990 std::string config_path = createTestConfig (test_dir, " _txt" );
9091
9192 std::vector<PortResult> results = {{80 , " http" }, {443 , " https" }};
@@ -126,12 +127,12 @@ Test(Exporter, should_export_valid_text_with_results) {
126127 cr_assert (txt_content.find (" Service: http" ) != std::string::npos,
127128 " TXT should contain http service" );
128129
129- cleanupTestFiles (test_dir);
130130 std::filesystem::remove (config_path);
131+ std::filesystem::remove_all (test_dir);
131132}
132133
133134Test (Exporter, getExportPath_should_read_from_config) {
134- std::string test_dir = " /tmp/custom_export_path/ " ;
135+ std::string test_dir = createTestDirectory ( " getpath_test " ) ;
135136 std::string config_path = createTestConfig (test_dir, " _getpath" );
136137
137138 Exporter exporter (config_path);
@@ -143,26 +144,50 @@ Test(Exporter, getExportPath_should_read_from_config) {
143144 result.c_str (), test_dir.c_str ());
144145
145146 std::filesystem::remove (config_path);
147+ std::filesystem::remove_all (test_dir);
146148}
147149
148- std::string createTestDirectory () {
149- std::string test_dir = " /tmp/xpscan_test/" ;
150+ std::string createTestDirectory (const std::string &test_name) {
151+ // Create unique directory using timestamp and random number to avoid collisions
152+ auto now = std::chrono::high_resolution_clock::now ();
153+ auto timestamp = std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch ()).count ();
154+
155+ std::random_device rd;
156+ std::mt19937 gen (rd ());
157+ std::uniform_int_distribution<> dis (1000 , 9999 );
158+ int random_id = dis (gen);
159+
160+ std::string test_dir = " /tmp/xpscan_test_" + test_name + " _" +
161+ std::to_string (timestamp) + " _" +
162+ std::to_string (random_id) + " /" ;
150163 std::filesystem::create_directories (test_dir);
151164 return test_dir;
152165}
153166
154167std::string createTestConfig (const std::string &export_path,
155168 const std::string &suffix) {
156- std::string config_path = " /tmp/test_config" + suffix + " .conf" ;
169+ // Create unique config file using timestamp and random number
170+ auto now = std::chrono::high_resolution_clock::now ();
171+ auto timestamp = std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch ()).count ();
172+
173+ std::random_device rd;
174+ std::mt19937 gen (rd ());
175+ std::uniform_int_distribution<> dis (1000 , 9999 );
176+ int random_id = dis (gen);
177+
178+ std::string config_path = " /tmp/test_config" + suffix + " _" +
179+ std::to_string (timestamp) + " _" +
180+ std::to_string (random_id) + " .conf" ;
157181 std::ofstream config_file (config_path);
158182 config_file << export_path;
159183 config_file.close ();
160184 return config_path;
161185}
162186
163- void cleanupTestFiles (const std::string &pattern) {
164- for (const auto &entry :
165- std::filesystem::directory_iterator (" /tmp/xpscan_test/" )) {
166- std::filesystem::remove (entry.path ());
187+ void cleanupTestFiles (const std::string &directory) {
188+ if (std::filesystem::exists (directory)) {
189+ for (const auto &entry : std::filesystem::directory_iterator (directory)) {
190+ std::filesystem::remove (entry.path ());
191+ }
167192 }
168193}
0 commit comments