77#include < openssl/rsa.h>
88#include < stdio.h>
99#include < string.h>
10+ #if defined(OPENSSL_WINDOWS)
11+ #include < direct.h>
12+ #include < io.h>
13+ #else
14+ #include < unistd.h>
15+ #include < sys/stat.h>
16+ #endif
1017#include " ../tool/internal.h"
1118
1219#include < gtest/gtest.h>
@@ -25,6 +32,17 @@ class ReqTest : public ::testing::Test {
2532 ASSERT_GT (createTempFILEpath (config_path), 0u );
2633 ASSERT_GT (createTempFILEpath (protected_key_path), 0u );
2734
35+ // Create a temporary directory and change to it
36+ // This allows testing the default "privkey.pem" behavior in an isolated location
37+ ASSERT_GT (createTempDirPath (temp_dir_path), 0u );
38+ #if defined(OPENSSL_WINDOWS)
39+ ASSERT_TRUE (_getcwd (original_dir, PATH_MAX) != nullptr );
40+ ASSERT_EQ (_chdir (temp_dir_path), 0 );
41+ #else
42+ ASSERT_TRUE (getcwd (original_dir, PATH_MAX) != nullptr );
43+ ASSERT_EQ (chdir (temp_dir_path), 0 );
44+ #endif
45+
2846 // Create a test private key
2947 bssl::UniquePtr<EVP_PKEY> pkey (EVP_PKEY_new ());
3048 ASSERT_TRUE (pkey);
@@ -50,13 +68,37 @@ class ReqTest : public ::testing::Test {
5068 }
5169
5270 void TearDown () override {
71+ // Change back to original directory before cleanup
72+ if (original_dir[0 ] != ' \0 ' ) {
73+ #if defined(OPENSSL_WINDOWS)
74+ (void )_chdir (original_dir);
75+ #else
76+ (void )chdir (original_dir);
77+ #endif
78+ }
79+
5380 RemoveFile (input_key_path);
5481 RemoveFile (output_key_path);
5582 RemoveFile (csr_path);
5683 RemoveFile (cert_path);
5784 RemoveFile (config_path);
5885 RemoveFile (protected_key_path);
59- RemoveFile (" privkey.pem" );
86+
87+ // Clean up privkey.pem in the temp directory
88+ if (temp_dir_path[0 ] != ' \0 ' ) {
89+ size_t temp_dir_len = strlen (temp_dir_path);
90+ // Check if path would fit (temp_dir + "/" + "privkey.pem" + null terminator)
91+ if (temp_dir_len + 13 < PATH_MAX) {
92+ char privkey_path[PATH_MAX];
93+ snprintf (privkey_path, PATH_MAX, " %s/privkey.pem" , temp_dir_path);
94+ RemoveFile (privkey_path);
95+ }
96+ #if defined(OPENSSL_WINDOWS)
97+ _rmdir (temp_dir_path);
98+ #else
99+ rmdir (temp_dir_path);
100+ #endif
101+ }
60102 }
61103
62104 char input_key_path[PATH_MAX]; // For -key (input)
@@ -65,6 +107,8 @@ class ReqTest : public ::testing::Test {
65107 char cert_path[PATH_MAX];
66108 char config_path[PATH_MAX];
67109 char protected_key_path[PATH_MAX];
110+ char temp_dir_path[PATH_MAX]; // Temporary directory for test isolation
111+ char original_dir[PATH_MAX]; // Original working directory
68112};
69113
70114TEST_F (ReqTest, GenerateRSAKey) {
0 commit comments