| 1 |
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 OR ISC |
| 3 |
|
| 4 |
#include <gtest/gtest.h> |
| 5 |
#include <signal.h> |
| 6 |
|
| 7 |
#include <openssl/base.h> |
| 8 |
|
| 9 |
#include "internal.h" |
| 10 |
#include "../test/test_util.h" |
| 11 |
|
| 12 |
#if !defined(OPENSSL_NO_TTY) |
| 13 |
|
| 14 |
#if defined(OPENSSL_WINDOWS) |
| 15 |
#include <io.h> |
| 16 |
#define dup _dup |
| 17 |
#define dup2 _dup2 |
| 18 |
#define fileno _fileno |
| 19 |
#define close _close |
| 20 |
#else |
| 21 |
#include <unistd.h> |
| 22 |
#endif |
| 23 |
|
| 24 |
|
| 25 |
#if !defined(OPENSSL_ANDROID) |
| 26 |
// On Android, when running from an APK, |tmpfile| does not work. See |
| 27 |
// b/36991167#comment8. |
| 28 |
|
| 29 |
// Consolidated password testing |
| 30 |
class PemPasswdTest : public testing::Test { |
| 31 |
protected: |
| 32 |
void SetUp() override { |
| 33 |
#if defined(OPENSSL_WINDOWS) |
| 34 |
_putenv_s("AWSLC_CONSOLE_NO_TTY_DETECT", "1"); |
| 35 |
#else |
| 36 |
setenv("AWSLC_CONSOLE_NO_TTY_DETECT", "1", 1); |
| 37 |
#endif |
| 38 |
|
| 39 |
// Save original file descriptors |
| 40 |
original_stdin = dup(fileno(stdin)); |
| 41 |
original_stderr = dup(fileno(stderr)); |
| 42 |
|
| 43 |
// Create temporary files |
| 44 |
stdin_file = createRawTempFILE(); |
| 45 |
stderr_file = createRawTempFILE(); |
| 46 |
ASSERT_TRUE(stdin_file != nullptr); |
| 47 |
ASSERT_TRUE(stderr_file != nullptr); |
| 48 |
|
| 49 |
// Redirect stdin/stderr to our temp files |
| 50 |
ASSERT_NE(-1, dup2(fileno(stdin_file), fileno(stdin))); |
| 51 |
ASSERT_NE(-1, dup2(fileno(stderr_file), fileno(stderr))); |
| 52 |
|
| 53 |
// Initialize console for each test |
| 54 |
openssl_console_acquire_mutex(); |
| 55 |
ASSERT_TRUE(openssl_console_open()); |
| 56 |
} |
| 57 |
|
| 58 |
void TearDown() override { |
| 59 |
#if defined(OPENSSL_WINDOWS) |
| 60 |
_putenv_s("AWSLC_CONSOLE_NO_TTY_DETECT", ""); |
| 61 |
#else |
| 62 |
unsetenv("AWSLC_CONSOLE_NO_TTY_DETECT"); |
| 63 |
#endif |
| 64 |
|
| 65 |
// Close console for each test |
| 66 |
ASSERT_TRUE(openssl_console_close()); |
| 67 |
openssl_console_release_mutex(); |
| 68 |
|
| 69 |
// Restore original streams |
| 70 |
ASSERT_NE(-1, dup2(original_stdin, fileno(stdin))); |
| 71 |
ASSERT_NE(-1, dup2(original_stderr, fileno(stderr))); |
| 72 |
|
| 73 |
// Close temp files |
| 74 |
if (stdin_file) { |
| 75 |
fclose(stdin_file); |
| 76 |
} |
| 77 |
if (stderr_file) { |
| 78 |
fclose(stderr_file); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
void MockStdinInput(const std::string& input) { |
| 83 |
ASSERT_GT(fwrite(input.c_str(), 1, input.length(), stdin_file), (size_t)0); |
| 84 |
rewind(stdin_file); |
| 85 |
} |
| 86 |
|
| 87 |
std::string GetStderrOutput() { |
| 88 |
std::string output; |
| 89 |
char buf[1024]; |
| 90 |
rewind(stderr_file); |
| 91 |
while (fgets(buf, sizeof(buf), stderr_file) != nullptr) { |
| 92 |
output += buf; |
| 93 |
} |
| 94 |
|
| 95 |
return output; |
| 96 |
} |
| 97 |
|
| 98 |
void ResetTempFiles() { |
| 99 |
fclose(stdin_file); |
| 100 |
fclose(stderr_file); |
| 101 |
|
| 102 |
stdin_file = tmpfile(); |
| 103 |
stderr_file = tmpfile(); |
| 104 |
ASSERT_TRUE(stdin_file != nullptr); |
| 105 |
ASSERT_TRUE(stderr_file != nullptr); |
| 106 |
|
| 107 |
// Redirect stdin/stderr to our NEW temp files |
| 108 |
ASSERT_NE(-1, dup2(fileno(stdin_file), fileno(stdin))); |
| 109 |
ASSERT_NE(-1, dup2(fileno(stderr_file), fileno(stderr))); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
FILE* stdin_file = nullptr; |
| 114 |
FILE* stderr_file = nullptr; |
| 115 |
int original_stdin = -1; |
| 116 |
int original_stderr = -1; |
| 117 |
const char* default_prompt = "Enter password:"; |
| 118 |
}; |
| 119 |
|
| 120 |
// Test basic password functionality with various inputs |
| 121 |
TEST_F(PemPasswdTest, PasswordInputVariations) { |
| 122 |
struct TestCase { |
| 123 |
std::string description; |
| 124 |
std::string input; |
| 125 |
int min_size; |
| 126 |
int expected_result; |
| 127 |
std::string expected_output; |
| 128 |
}; |
| 129 |
|
| 130 |
std::vector<TestCase> test_cases = { |
| 131 |
// Normal password |
| 132 |
{"Normal password", "test_password\n", 0, 0, "test_password"}, |
| 133 |
// |
| 134 |
// // Empty password |
| 135 |
{"Empty password allowed", "\n", 0, 0, ""}, |
| 136 |
{"Empty password rejected", "\n", 2, -1, ""}, |
| 137 |
|
| 138 |
// Length requirements |
| 139 |
{"Password too short", "short\n", 10, -1, "short"}, |
| 140 |
{"Password meets min length", "longenoughpass\n", 10, 0, "longenoughpass"}, |
| 141 |
|
| 142 |
// Special characters |
| 143 |
{"Special characters", "!@#$%^&*()\n", 0, 0, "!@#$%^&*()"}, |
| 144 |
{"Unicode characters", "パスワード\n", 0, 0, "パスワード"} |
| 145 |
}; |
| 146 |
|
| 147 |
for (const auto& tc : test_cases) { |
| 148 |
SCOPED_TRACE(tc.description); |
| 149 |
|
| 150 |
char buf[1024] = {0}; |
| 151 |
MockStdinInput(tc.input); |
| 152 |
|
| 153 |
ASSERT_TRUE(openssl_console_write(default_prompt)); |
| 154 |
|
| 155 |
ASSERT_EQ(openssl_console_read(buf, tc.min_size, sizeof(buf), 0), tc.expected_result); |
| 156 |
|
| 157 |
if (tc.expected_result == 0) { |
| 158 |
ASSERT_STREQ(buf, tc.expected_output.c_str()); |
| 159 |
} |
| 160 |
|
| 161 |
// Verify prompt was written |
| 162 |
std::string output = GetStderrOutput(); |
| 163 |
ASSERT_TRUE(output.find(default_prompt) != std::string::npos); |
| 164 |
|
| 165 |
ResetTempFiles(); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
// Test password verification flow (matching and non-matching) |
| 170 |
TEST_F(PemPasswdTest, PasswordVerification) { |
| 171 |
struct TestCase { |
| 172 |
std::string description; |
| 173 |
std::string first_password; |
| 174 |
std::string second_password; |
| 175 |
bool should_match; |
| 176 |
}; |
| 177 |
|
| 178 |
std::vector<TestCase> test_cases = { |
| 179 |
{"Matching passwords", "test_password\n", "test_password\n", true}, |
| 180 |
{"Non-matching passwords", "password1\n", "password2\n", false} |
| 181 |
}; |
| 182 |
|
| 183 |
for (const auto& tc : test_cases) { |
| 184 |
SCOPED_TRACE(tc.description); |
| 185 |
|
| 186 |
char buf1[1024] = {0}; |
| 187 |
char buf2[1024] = {0}; |
| 188 |
|
| 189 |
// Mock both password inputs |
| 190 |
std::string combined_input = tc.first_password + tc.second_password; |
| 191 |
MockStdinInput(combined_input); |
| 192 |
|
| 193 |
// First password entry |
| 194 |
ASSERT_TRUE(openssl_console_write(default_prompt)); |
| 195 |
ASSERT_EQ(0, openssl_console_read(buf1, 0, sizeof(buf1), 0)); |
| 196 |
|
| 197 |
// Verification prompt |
| 198 |
ASSERT_TRUE(openssl_console_write("Verifying - ")); |
| 199 |
ASSERT_TRUE(openssl_console_write(default_prompt)); |
| 200 |
ASSERT_EQ(0, openssl_console_read(buf2, 0, sizeof(buf2), 0)); |
| 201 |
|
| 202 |
// Verify match/mismatch as expected |
| 203 |
if (tc.should_match) { |
| 204 |
ASSERT_STREQ(buf1, buf2); |
| 205 |
} else { |
| 206 |
ASSERT_STRNE(buf1, buf2); |
| 207 |
} |
| 208 |
|
| 209 |
// Verify prompts were written |
| 210 |
std::string output = GetStderrOutput(); |
| 211 |
ASSERT_TRUE(output.find(default_prompt) != std::string::npos); |
| 212 |
ASSERT_TRUE(output.find("Verifying - ") != std::string::npos); |
| 213 |
|
| 214 |
ResetTempFiles(); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
// Test buffer handling (truncation of long passwords) |
| 219 |
TEST_F(PemPasswdTest, BufferHandling) { |
| 220 |
// Small buffer to test truncation |
| 221 |
char small_buf[16] = {0}; |
| 222 |
|
| 223 |
// Create a password longer than the buffer |
| 224 |
std::string long_password(32, 'a'); |
| 225 |
long_password += "\n"; |
| 226 |
|
| 227 |
MockStdinInput(long_password); |
| 228 |
ASSERT_TRUE(openssl_console_write(default_prompt)); |
| 229 |
ASSERT_EQ(0, openssl_console_read(small_buf, 0, sizeof(small_buf),0)); |
| 230 |
|
| 231 |
// Verify the password was truncated to fit the buffer (15 chars + null terminator) |
| 232 |
std::string expected(15, 'a'); |
| 233 |
ASSERT_STREQ(small_buf, expected.c_str()); |
| 234 |
} |
| 235 |
|
| 236 |
// Test echo modes |
| 237 |
TEST_F(PemPasswdTest, EchoModes) { |
| 238 |
const char* test_password = "test_password\n"; |
| 239 |
char buf_no_echo[1024] = {0}; |
| 240 |
char buf_with_echo[1024] = {0}; |
| 241 |
|
| 242 |
// Test with echo disabled |
| 243 |
MockStdinInput(test_password); |
| 244 |
ASSERT_TRUE(openssl_console_write(default_prompt)); |
| 245 |
ASSERT_EQ(0, openssl_console_read(buf_no_echo, 0, sizeof(buf_no_echo), 0)); |
| 246 |
|
| 247 |
// Test with echo enabled |
| 248 |
MockStdinInput(test_password); |
| 249 |
ASSERT_TRUE(openssl_console_write(default_prompt)); |
| 250 |
ASSERT_EQ(0, openssl_console_read(buf_with_echo, 0, sizeof(buf_with_echo), 1)); |
| 251 |
|
| 252 |
// Both should have the same result |
| 253 |
ASSERT_STREQ(buf_no_echo, "test_password"); |
| 254 |
ASSERT_STREQ(buf_with_echo, "test_password"); |
| 255 |
} |
| 256 |
#endif |
| 257 |
|
| 258 |
#endif // !OPENSSL_NO_TTY |