1 //
2 // Copyright (C) 2023 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15
16 #include "host/libs/web/http_client/http_client_util.h"
17
18 #include <gtest/gtest.h>
19
20 namespace cuttlefish {
21 namespace http_client {
22
TEST(HttpClientUtilTest,ScrubSecretsAuthorizationMatch)23 TEST(HttpClientUtilTest, ScrubSecretsAuthorizationMatch) {
24 EXPECT_EQ(ScrubSecrets("Authorization: Bearer 123456"),
25 "Authorization: Bearer 123456...");
26 EXPECT_EQ(ScrubSecrets("Authorization: Bearer 1234567890"),
27 "Authorization: Bearer 123456...");
28 EXPECT_EQ(ScrubSecrets("Authorization: Basic 1234567890"),
29 "Authorization: Basic 123456...");
30 EXPECT_EQ(ScrubSecrets("text\nAuthorization: Bearer 1234567890"),
31 "text\nAuthorization: Bearer 123456...");
32 EXPECT_EQ(ScrubSecrets("Authorization: Bearer 1234567890\nnext_line"),
33 "Authorization: Bearer 123456...\nnext_line");
34 EXPECT_EQ(ScrubSecrets("Authorization: Bearer 1234567890 \nnext_line"),
35 "Authorization: Bearer 123456... \nnext_line");
36 EXPECT_EQ(ScrubSecrets("Authorization: Bearer 1234567890 \nnext_line"),
37 "Authorization: Bearer 123456... \nnext_line");
38 }
39
TEST(HttpClientUtilTest,ScrubSecretsAuthorizationNoMatch)40 TEST(HttpClientUtilTest, ScrubSecretsAuthorizationNoMatch) {
41 EXPECT_EQ(ScrubSecrets("hello world"), "hello world");
42 EXPECT_EQ(ScrubSecrets("Authorization: Bearer 12345"),
43 "Authorization: Bearer 12345");
44 EXPECT_EQ(ScrubSecrets("Authorization Bearer 1234567890"),
45 "Authorization Bearer 1234567890");
46 EXPECT_EQ(ScrubSecrets("Authorization: 1234567890"),
47 "Authorization: 1234567890");
48 }
49
TEST(HttpClientUtilTest,ScrubSecretsClientSecretMatch)50 TEST(HttpClientUtilTest, ScrubSecretsClientSecretMatch) {
51 EXPECT_EQ(ScrubSecrets("client_secret=123456"), "client_secret=123456...");
52 EXPECT_EQ(ScrubSecrets("client_secret=1234567890"),
53 "client_secret=123456...");
54 EXPECT_EQ(ScrubSecrets("text\nclient_secret=1234567890"),
55 "text\nclient_secret=123456...");
56 EXPECT_EQ(ScrubSecrets("client_id=abc&client_secret=1234567890"),
57 "client_id=abc&client_secret=123456...");
58 EXPECT_EQ(ScrubSecrets("client_secret=1234567890\nnext_line"),
59 "client_secret=123456...\nnext_line");
60 EXPECT_EQ(ScrubSecrets("client_secret=1234567890 \nnext_line"),
61 "client_secret=123456... \nnext_line");
62 EXPECT_EQ(ScrubSecrets("client_secret=1234567890 \nnext_line"),
63 "client_secret=123456... \nnext_line");
64 EXPECT_EQ(ScrubSecrets("client_secret=1234567890&client_id=abc"),
65 "client_secret=123456...&client_id=abc");
66 }
67
TEST(HttpClientUtilTest,ScrubSecretsClientSecretNoMatch)68 TEST(HttpClientUtilTest, ScrubSecretsClientSecretNoMatch) {
69 EXPECT_EQ(ScrubSecrets("hello world"), "hello world");
70 EXPECT_EQ(ScrubSecrets("client_secret=12345"), "client_secret=12345");
71 EXPECT_EQ(ScrubSecrets("client_id=1234567890"), "client_id=1234567890");
72 }
73
74 } // namespace http_client
75 } // namespace cuttlefish
76