|
3 | 3 | #include <Common/maskURIPassword.h> |
4 | 4 | #include <Common/re2.h> |
5 | 5 |
|
| 6 | +#include <random> |
6 | 7 | #include <string> |
7 | 8 | #include <vector> |
8 | 9 |
|
@@ -77,6 +78,32 @@ TEST(MaskURIPassword, MatchesTheRegularExpressionItReplaced) |
77 | 78 | } |
78 | 79 | } |
79 | 80 |
|
| 81 | +TEST(MaskURIPassword, AgreesWithTheRegularExpressionOnRandomStrings) |
| 82 | +{ |
| 83 | + /// The fixed corpus above documents the interesting cases; this is the safety net for the ones |
| 84 | + /// nobody thought of. The alphabet is the characters the regular expression assigns meaning to. |
| 85 | + constexpr std::string_view alphabet = "ab:@/.\n"; |
| 86 | + std::mt19937_64 rng(20260731); /// NOLINT(cert-msc32-c,cert-msc51-cpp) deterministic seed, so a failure is reproducible |
| 87 | + std::uniform_int_distribution<size_t> length_dist(0, 32); |
| 88 | + std::uniform_int_distribution<size_t> char_dist(0, alphabet.size() - 1); |
| 89 | + |
| 90 | + for (size_t round = 0; round < 200000; ++round) |
| 91 | + { |
| 92 | + std::string input(length_dist(rng), ' '); |
| 93 | + for (auto & c : input) |
| 94 | + c = alphabet[char_dist(rng)]; |
| 95 | + |
| 96 | + std::string with_scan = input; |
| 97 | + std::string with_re2 = input; |
| 98 | + |
| 99 | + bool scanned = maskURIPassword(&with_scan); |
| 100 | + bool matched = maskURIPasswordWithRE2(&with_re2); |
| 101 | + |
| 102 | + ASSERT_EQ(scanned, matched) << "return value differs for: " << input; |
| 103 | + ASSERT_EQ(with_scan, with_re2) << "result differs for: " << input; |
| 104 | + } |
| 105 | +} |
| 106 | + |
80 | 107 | TEST(MaskURIPassword, MasksThePassword) |
81 | 108 | { |
82 | 109 | std::string uri = "https://user:[email protected]:443/db"; |
@@ -171,6 +198,38 @@ TEST(MaskS3URLCredentials, MatchTheRegularExpressionsTheyReplaced) |
171 | 198 | } |
172 | 199 | } |
173 | 200 |
|
| 201 | +TEST(MaskS3URLCredentials, AgreeWithTheRegularExpressionsOnRandomStrings) |
| 202 | +{ |
| 203 | + /// Random characters would almost never spell a presigned parameter name, so the strings are |
| 204 | + /// built from the tokens the two regular expressions care about. |
| 205 | + const std::vector<std::string> tokens = { |
| 206 | + "https://", "s3://", "1://", "-x://", ":/", "user", "pass", ":", "@", "/", "?", "&", "=", "#", |
| 207 | + "Signature", "AWSAccessKeyId", "Expires", "GoogleAccessId", "X-Amz-", "X-Goog-", "Credential", "_", "a", "\n", "", |
| 208 | + }; |
| 209 | + std::mt19937_64 rng(20260731); /// NOLINT(cert-msc32-c,cert-msc51-cpp) deterministic seed, so a failure is reproducible |
| 210 | + std::uniform_int_distribution<size_t> count_dist(0, 12); |
| 211 | + std::uniform_int_distribution<size_t> token_dist(0, tokens.size() - 1); |
| 212 | + |
| 213 | + /// Fewer rounds than for `maskURIPassword`: each round runs two scans and two regular |
| 214 | + /// expressions, and `RE2::GlobalReplace` dominates - sanitizer builds multiply that. |
| 215 | + for (size_t round = 0; round < 50000; ++round) |
| 216 | + { |
| 217 | + std::string input; |
| 218 | + for (size_t i = 0, count = count_dist(rng); i < count; ++i) |
| 219 | + input += tokens[token_dist(rng)]; |
| 220 | + |
| 221 | + std::string with_scan = input; |
| 222 | + std::string with_re2 = input; |
| 223 | + |
| 224 | + ASSERT_EQ(maskURIUserinfo(with_scan), maskURIUserinfoWithRE2(with_re2)) << "userinfo return value differs for: " << input; |
| 225 | + ASSERT_EQ(with_scan, with_re2) << "userinfo result differs for: " << input; |
| 226 | + |
| 227 | + ASSERT_EQ(maskPresignedURLParameters(with_scan), maskPresignedURLParametersWithRE2(with_re2)) |
| 228 | + << "presign return value differs for: " << input; |
| 229 | + ASSERT_EQ(with_scan, with_re2) << "presign result differs for: " << input; |
| 230 | + } |
| 231 | +} |
| 232 | + |
174 | 233 | TEST(MaskS3URLCredentials, MasksUserinfoAndPresignedParameters) |
175 | 234 | { |
176 | 235 | std::string url = "https://key:secret@bucket/path?X-Amz-Signature=abcdef&format=CSV"; |
|
0 commit comments