1515#include " google/cloud/internal/curl_wrappers.h"
1616#include " google/cloud/internal/curl_options.h"
1717#include < gmock/gmock.h>
18+ #include < ostream>
1819#include < string_view>
1920
2021namespace google {
@@ -23,6 +24,7 @@ namespace rest_internal {
2324GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
2425namespace {
2526
27+ using ::testing::Eq;
2628using ::testing::Pair;
2729using ::testing::UnorderedElementsAre;
2830
@@ -156,6 +158,211 @@ TEST(CurlWrappers, DebugInfo) {
156158 }
157159}
158160
161+ struct DebugHeaderCase {
162+ std::string name;
163+ std::string input;
164+ std::string expected;
165+ };
166+
167+ void PrintTo (DebugHeaderCase const & c, std::ostream* os) { *os << c.name ; }
168+
169+ class DebugSendHeaderEncryptionKeyHeadersTest
170+ : public ::testing::TestWithParam<DebugHeaderCase> {};
171+
172+ // / @test Verify DebugSendHeader formats encryption key header values as
173+ // / [censored] while preserving line endings, sibling headers, and Bearer
174+ // / truncation.
175+ TEST_P (DebugSendHeaderEncryptionKeyHeadersTest, FormatsLine) {
176+ DebugHeaderCase const & test = GetParam ();
177+ EXPECT_THAT (DebugSendHeader (test.input .data (), test.input .size ()),
178+ Eq (test.expected ));
179+ }
180+
181+ INSTANTIATE_TEST_SUITE_P (
182+ CurlWrappers, DebugSendHeaderEncryptionKeyHeadersTest,
183+ ::testing::Values (
184+ DebugHeaderCase{
185+ " EncryptionKeyNoNewline" ,
186+ " x-goog-encryption-key: "
187+ " MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI=" ,
188+ " >> curl(Send Header): x-goog-encryption-key: [censored]" },
189+ DebugHeaderCase{
190+ " EncryptionKeyLf" ,
191+ " x-goog-encryption-key: "
192+ " MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI=\n " ,
193+ " >> curl(Send Header): x-goog-encryption-key: [censored]\n " },
194+ DebugHeaderCase{
195+ " EncryptionKeyCrLf" ,
196+ " x-goog-encryption-key: "
197+ " MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI=\r\n " ,
198+ " >> curl(Send Header): x-goog-encryption-key: [censored]\r\n " },
199+ DebugHeaderCase{
200+ " CopySourceEncryptionKeyNoNewline" ,
201+ " x-goog-copy-source-encryption-key: "
202+ " QUJDRDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg=" ,
203+ " >> curl(Send Header): x-goog-copy-source-encryption-key: "
204+ " [censored]" },
205+ DebugHeaderCase{
206+ " CopySourceEncryptionKeyLf" ,
207+ " x-goog-copy-source-encryption-key: "
208+ " QUJDRDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg=\n " ,
209+ " >> curl(Send Header): x-goog-copy-source-encryption-key: "
210+ " [censored]\n " },
211+ DebugHeaderCase{
212+ " CopySourceEncryptionKeyCrLf" ,
213+ " x-goog-copy-source-encryption-key: "
214+ " QUJDRDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg=\r\n " ,
215+ " >> curl(Send Header): x-goog-copy-source-encryption-key: "
216+ " [censored]\r\n " },
217+ DebugHeaderCase{
218+ " MixedCaseHeaders" ,
219+ " X-Goog-Encryption-Key: value1\r\n "
220+ " X-Goog-Copy-Source-Encryption-Key: value2\r\n " ,
221+ " >> curl(Send Header): X-Goog-Encryption-Key: [censored]\r\n "
222+ " X-Goog-Copy-Source-Encryption-Key: [censored]\r\n " },
223+ DebugHeaderCase{
224+ " MultiLineWithBearerAndSiblings" ,
225+ " authorization: Bearer 012345678901234567890123456789123456\n "
226+ " x-goog-encryption-key: "
227+ " MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI=\n "
228+ " x-goog-encryption-key-sha256: key-hash\n "
229+ " x-goog-copy-source-encryption-key: "
230+ " QUJDRDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg=\n "
231+ " x-goog-copy-source-encryption-key-sha256: source-hash\n "
232+ " host: storage.googleapis.com\n " ,
233+ " >> curl(Send Header): authorization: Bearer "
234+ " 01234567890123456789012345678912...<truncated>...\n "
235+ " x-goog-encryption-key: [censored]\n "
236+ " x-goog-encryption-key-sha256: key-hash\n "
237+ " x-goog-copy-source-encryption-key: [censored]\n "
238+ " x-goog-copy-source-encryption-key-sha256: source-hash\n "
239+ " host: storage.googleapis.com\n " },
240+ DebugHeaderCase{
241+ " DuplicateKeyLines" ,
242+ " x-goog-encryption-key: first\r\n "
243+ " x-goog-encryption-key: second\r\n " ,
244+ " >> curl(Send Header): x-goog-encryption-key: [censored]\r\n "
245+ " x-goog-encryption-key: [censored]\r\n " },
246+ DebugHeaderCase{
247+ " NoSpaceAfterColon" , " x-goog-encryption-key:value" ,
248+ " >> curl(Send Header): x-goog-encryption-key: [censored]" },
249+ DebugHeaderCase{
250+ " EmptyValueWithSpace" , " x-goog-encryption-key: \r\n " ,
251+ " >> curl(Send Header): x-goog-encryption-key: [censored]\r\n " },
252+ DebugHeaderCase{
253+ " EmptyValueNoSpace" , " x-goog-encryption-key:\r\n " ,
254+ " >> curl(Send Header): x-goog-encryption-key: [censored]\r\n " },
255+ DebugHeaderCase{
256+ " ValueStartingWithBearer" ,
257+ " x-goog-encryption-key: Bearer "
258+ " 012345678901234567890123456789123456\r\n " ,
259+ " >> curl(Send Header): x-goog-encryption-key: [censored]\r\n " },
260+ DebugHeaderCase{
261+ " ValueStartingWithBearerBeforeAuthorization" ,
262+ " x-goog-encryption-key: Bearer "
263+ " 012345678901234567890123456789123456\n "
264+ " authorization: Bearer 012345678901234567890123456789123456\n " ,
265+ " >> curl(Send Header): x-goog-encryption-key: [censored]\n "
266+ " authorization: Bearer "
267+ " 01234567890123456789012345678912...<truncated>...\n " },
268+ DebugHeaderCase{
269+ " LeftmostHeaderWinsOnSameLine" ,
270+ " x-goog-copy-source-encryption-key: val1 x-goog-encryption-key: "
271+ " val2\r\n " ,
272+ " >> curl(Send Header): x-goog-copy-source-encryption-key: "
273+ " [censored]\r\n " },
274+ DebugHeaderCase{
275+ " TabPrecededHeader" , " \t x-goog-encryption-key: value\r\n " ,
276+ " >> curl(Send Header): \t x-goog-encryption-key: [censored]\r\n " },
277+ DebugHeaderCase{
278+ " BlankLinesPreservedLf" , " x-goog-encryption-key: value\n\n " ,
279+ " >> curl(Send Header): x-goog-encryption-key: [censored]\n\n " },
280+ DebugHeaderCase{" BlankLinesPreservedCrLf" ,
281+ " x-goog-encryption-key: value\r\n\r\n " ,
282+ " >> curl(Send Header): x-goog-encryption-key: "
283+ " [censored]\r\n\r\n " },
284+ DebugHeaderCase{
285+ " TrailingCarriageReturnOnly" , " x-goog-encryption-key: value\r " ,
286+ " >> curl(Send Header): x-goog-encryption-key: [censored]\r " },
287+ DebugHeaderCase{
288+ " UnbracketedValueEndingInClosingBracket" ,
289+ " x-goog-encryption-key: value]\r\n " ,
290+ " >> curl(Send Header): x-goog-encryption-key: [censored]\r\n " },
291+ DebugHeaderCase{" EmptyPayload" , " " , " >> curl(Send Header): " },
292+ DebugHeaderCase{
293+ " SiblingAndNearMissHeadersUnchanged" ,
294+ " x-goog-encryption-key-sha256: key-hash\r\n "
295+ " x-goog-copy-source-encryption-key-sha256: source-hash\r\n "
296+ " x-goog-encryption-algorithm: AES256\r\n "
297+ " my-x-goog-encryption-key: value\r\n " ,
298+ " >> curl(Send Header): x-goog-encryption-key-sha256: key-hash\r\n "
299+ " x-goog-copy-source-encryption-key-sha256: source-hash\r\n "
300+ " x-goog-encryption-algorithm: AES256\r\n "
301+ " my-x-goog-encryption-key: value\r\n " }),
302+ [](::testing::TestParamInfo<DebugHeaderCase> const & info) {
303+ return info.param .name ;
304+ });
305+
306+ class DebugInfoEncryptionKeyHeadersTest
307+ : public ::testing::TestWithParam<DebugHeaderCase> {};
308+
309+ // / @test Verify DebugInfo formats bracketed HTTP/2 and HTTP/3 encryption key
310+ // / header traces as [censored] while preserving non-matching headers.
311+ TEST_P (DebugInfoEncryptionKeyHeadersTest, FormatsLine) {
312+ DebugHeaderCase const & test = GetParam ();
313+ EXPECT_THAT (DebugInfo (test.input .data (), test.input .size ()),
314+ Eq (test.expected ));
315+ }
316+
317+ INSTANTIATE_TEST_SUITE_P (
318+ CurlWrappers, DebugInfoEncryptionKeyHeadersTest,
319+ ::testing::Values (
320+ DebugHeaderCase{" Http2EncryptionKey" ,
321+ " [HTTP/2] [1] [x-goog-encryption-key: "
322+ " MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI=]\n " ,
323+ " == curl(Info): [HTTP/2] [1] [x-goog-encryption-key: "
324+ " [censored]]\n " },
325+ DebugHeaderCase{
326+ " Http2CopySourceEncryptionKey" ,
327+ " [HTTP/2] [1] [x-goog-copy-source-encryption-key: "
328+ " QUJDRDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg=]\n " ,
329+ " == curl(Info): [HTTP/2] [1] [x-goog-copy-source-encryption-key: "
330+ " [censored]]\n " },
331+ DebugHeaderCase{" Http3EncryptionKey" ,
332+ " [HTTP/3] [0] [x-goog-encryption-key: "
333+ " MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI=]\n " ,
334+ " == curl(Info): [HTTP/3] [0] [x-goog-encryption-key: "
335+ " [censored]]\n " },
336+ DebugHeaderCase{
337+ " H2H3EncryptionKey" ,
338+ " h2h3 [x-goog-encryption-key: "
339+ " MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI=]\n " ,
340+ " == curl(Info): h2h3 [x-goog-encryption-key: [censored]]\n " },
341+ DebugHeaderCase{
342+ " SpacePrecededTraceHeader" ,
343+ " [1] header: x-goog-encryption-key: "
344+ " MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI=\n " ,
345+ " == curl(Info): [1] header: x-goog-encryption-key: [censored]\n " },
346+ DebugHeaderCase{
347+ " UnclosedBracketEncryptionKey" ,
348+ " [HTTP/2] [1] [x-goog-encryption-key: "
349+ " MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI=" ,
350+ " == curl(Info): [HTTP/2] [1] [x-goog-encryption-key: [censored]" },
351+ DebugHeaderCase{" ValueContainingClosingBracket" ,
352+ " [HTTP/2] [1] [x-goog-encryption-key: part1]part2]\n " ,
353+ " == curl(Info): [HTTP/2] [1] [x-goog-encryption-key: "
354+ " [censored]]\n " },
355+ DebugHeaderCase{
356+ " SiblingAndNearMissHeadersUnchanged" ,
357+ " [HTTP/2] [1] [x-goog-encryption-key-sha256: key-hash]\n "
358+ " [HTTP/2] [1] [my-x-goog-encryption-key: value]\n " ,
359+ " == curl(Info): [HTTP/2] [1] [x-goog-encryption-key-sha256: "
360+ " key-hash]\n "
361+ " [HTTP/2] [1] [my-x-goog-encryption-key: value]\n " }),
362+ [](::testing::TestParamInfo<DebugHeaderCase> const & info) {
363+ return info.param .name ;
364+ });
365+
159366TEST (CurlWrappers, CurlInitializeOptions) {
160367 auto defaults = CurlInitializeOptions ({});
161368 EXPECT_TRUE (defaults.get <EnableCurlSslLockingOption>());
0 commit comments