-
Notifications
You must be signed in to change notification settings - Fork 6.6k
/
companion_url_builder_unittest.cc
265 lines (211 loc) · 9.56 KB
/
companion_url_builder_unittest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/companion/core/companion_url_builder.h"
#include "base/base64.h"
#include "base/logging.h"
#include "chrome/browser/companion/core/constants.h"
#include "chrome/browser/companion/core/promo_handler.h"
#include "chrome/browser/companion/core/proto/companion_url_params.pb.h"
#include "chrome/browser/companion/core/signin_delegate.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/testing_pref_service.h"
#include "components/unified_consent/pref_names.h"
#include "net/base/url_util.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using ::testing::ContainsRegex;
using ::testing::MatchesRegex;
namespace companion {
namespace {
constexpr char kValidUrl[] = "https://foo.com/";
constexpr char kTextQuery[] = "Apples";
constexpr char kOrigin[] = "chrome-untrusted://companion-side-panel.top-chrome";
class MockSigninDelegate : public SigninDelegate {
public:
MOCK_METHOD0(AllowedSignin, bool());
MOCK_METHOD0(IsSignedIn, bool());
MOCK_METHOD0(StartSigninFlow, void());
MOCK_METHOD1(EnableMsbb, void(bool));
MOCK_METHOD1(LoadUrlInNewTab, void(const GURL&));
};
} // namespace
class CompanionUrlBuilderTest : public testing::Test {
public:
CompanionUrlBuilderTest() = default;
~CompanionUrlBuilderTest() override = default;
void SetUp() override {
pref_service_.registry()->RegisterBooleanPref(
unified_consent::prefs::kUrlKeyedAnonymizedDataCollectionEnabled,
false);
PromoHandler::RegisterProfilePrefs(pref_service_.registry());
pref_service_.SetUserPref(kSigninPromoDeclinedCountPref, base::Value(1));
SetSignInAndMsbbExpectations(/*is_sign_in_allowed=*/true,
/*is_signed_in=*/true,
/*msbb_pref_enabled=*/true);
url_builder_ = std::make_unique<CompanionUrlBuilder>(&pref_service_,
&signin_delegate_);
}
protected:
void VerifyPageUrlSent(GURL page_url, bool expect_was_sent) {
GURL companion_url = url_builder_->BuildCompanionURL(page_url);
std::string companion_query_param;
EXPECT_TRUE(net::GetValueForKeyInQuery(companion_url, "companion_query",
&companion_query_param));
// Deserialize the query param into protobuf.
companion::proto::CompanionUrlParams proto =
DeserializeCompanionRequest(companion_query_param);
if (expect_was_sent) {
EXPECT_EQ(proto.page_url(), page_url.spec());
} else {
EXPECT_EQ(proto.page_url(), std::string());
}
EXPECT_TRUE(proto.has_msbb_enabled());
}
// Deserialize the query param into proto::CompanionUrlParams.
proto::CompanionUrlParams DeserializeCompanionRequest(
const std::string& companion_url_param) {
companion::proto::CompanionUrlParams proto;
auto base64_decoded = base::Base64Decode(companion_url_param);
auto serialized_proto = std::string(base64_decoded.value().begin(),
base64_decoded.value().end());
EXPECT_TRUE(proto.ParseFromString(serialized_proto));
return proto;
}
void SetSignInAndMsbbExpectations(bool is_sign_in_allowed,
bool is_signed_in,
bool msbb_pref_enabled) {
EXPECT_CALL(signin_delegate_, AllowedSignin())
.WillRepeatedly(testing::Return(is_sign_in_allowed));
EXPECT_CALL(signin_delegate_, IsSignedIn())
.WillRepeatedly(testing::Return(is_signed_in));
pref_service_.SetUserPref(
unified_consent::prefs::kUrlKeyedAnonymizedDataCollectionEnabled,
base::Value(msbb_pref_enabled));
}
TestingPrefServiceSimple pref_service_;
MockSigninDelegate signin_delegate_;
std::unique_ptr<CompanionUrlBuilder> url_builder_;
};
TEST_F(CompanionUrlBuilderTest, SignIn) {
GURL page_url(kValidUrl);
// Not signed in, no msbb.
SetSignInAndMsbbExpectations(/*is_sign_in_allowed=*/false,
/*is_signed_in=*/false,
/*msbb_pref_enabled=*/false);
std::string encoded_proto =
url_builder_->BuildCompanionUrlParamProto(page_url);
companion::proto::CompanionUrlParams proto =
DeserializeCompanionRequest(encoded_proto);
EXPECT_EQ(proto.page_url(), std::string());
EXPECT_FALSE(proto.is_sign_in_allowed());
EXPECT_FALSE(proto.is_signed_in());
EXPECT_FALSE(proto.has_msbb_enabled());
// Allowed to sign-in, but not signed in, no msbb.
SetSignInAndMsbbExpectations(/*is_sign_in_allowed=*/true,
/*is_signed_in=*/false,
/*msbb_pref_enabled=*/false);
encoded_proto = url_builder_->BuildCompanionUrlParamProto(page_url);
proto = DeserializeCompanionRequest(encoded_proto);
EXPECT_EQ(proto.page_url(), std::string());
EXPECT_TRUE(proto.is_sign_in_allowed());
EXPECT_FALSE(proto.is_signed_in());
EXPECT_FALSE(proto.has_msbb_enabled());
}
TEST_F(CompanionUrlBuilderTest, MsbbOff) {
pref_service_.SetUserPref(kSigninPromoDeclinedCountPref, base::Value(1));
SetSignInAndMsbbExpectations(/*is_sign_in_allowed=*/true,
/*is_signed_in=*/true,
/*msbb_pref_enabled=*/false);
pref_service_.SetUserPref(kSigninPromoDeclinedCountPref, base::Value(1));
GURL page_url(kValidUrl);
GURL companion_url = url_builder_->BuildCompanionURL(page_url);
std::string value;
EXPECT_FALSE(net::GetValueForKeyInQuery(companion_url, "url", &value));
EXPECT_TRUE(net::GetValueForKeyInQuery(companion_url, "origin", &value));
EXPECT_EQ(value, kOrigin);
std::string companion_url_param;
EXPECT_TRUE(net::GetValueForKeyInQuery(companion_url, "companion_query",
&companion_url_param));
// Verify that both helper methods generate the same proto.
std::string encoded_proto =
url_builder_->BuildCompanionUrlParamProto(page_url);
EXPECT_EQ(encoded_proto, companion_url_param);
// Deserialize the query param into protobuf.
companion::proto::CompanionUrlParams proto =
DeserializeCompanionRequest(companion_url_param);
// URL shouldn't be sent when MSBB is off.
EXPECT_EQ(proto.page_url(), std::string());
EXPECT_TRUE(proto.is_signed_in());
EXPECT_TRUE(proto.is_sign_in_allowed());
EXPECT_FALSE(proto.has_msbb_enabled());
}
TEST_F(CompanionUrlBuilderTest, MsbbOn) {
EXPECT_CALL(signin_delegate_, IsSignedIn())
.WillRepeatedly(testing::Return(true));
pref_service_.SetUserPref(kExpsPromoShownCountPref, base::Value(2));
GURL page_url(kValidUrl);
GURL companion_url = url_builder_->BuildCompanionURL(page_url);
std::string value;
EXPECT_TRUE(net::GetValueForKeyInQuery(companion_url, "url", &value));
EXPECT_EQ(value, page_url.spec());
EXPECT_TRUE(net::GetValueForKeyInQuery(companion_url, "origin", &value));
EXPECT_EQ(value, kOrigin);
std::string companion_url_param;
EXPECT_TRUE(net::GetValueForKeyInQuery(companion_url, "companion_query",
&companion_url_param));
// Verify that both helper methods generate the same proto.
std::string encoded_proto =
url_builder_->BuildCompanionUrlParamProto(page_url);
EXPECT_EQ(encoded_proto, companion_url_param);
// Deserialize the query param into protobuf.
companion::proto::CompanionUrlParams proto =
DeserializeCompanionRequest(companion_url_param);
// Verify fields inside protobuf.
EXPECT_EQ(proto.page_url(), page_url.spec());
EXPECT_TRUE(proto.has_msbb_enabled());
EXPECT_TRUE(proto.is_signed_in());
// Verify promo state.
EXPECT_TRUE(proto.has_promo_state());
EXPECT_EQ(1, proto.promo_state().signin_promo_denial_count());
EXPECT_EQ(0, proto.promo_state().msbb_promo_denial_count());
EXPECT_EQ(0, proto.promo_state().exps_promo_denial_count());
EXPECT_EQ(2, proto.promo_state().exps_promo_shown_count());
}
TEST_F(CompanionUrlBuilderTest, NonProtobufParams) {
GURL page_url(kValidUrl);
GURL companion_url = url_builder_->BuildCompanionURL(page_url);
std::string value;
EXPECT_TRUE(net::GetValueForKeyInQuery(companion_url, "url", &value));
EXPECT_EQ(value, page_url.spec());
EXPECT_TRUE(net::GetValueForKeyInQuery(companion_url, "origin", &value));
EXPECT_EQ(value, kOrigin);
}
TEST_F(CompanionUrlBuilderTest, ValidPageUrls) {
VerifyPageUrlSent(GURL(kValidUrl), true);
VerifyPageUrlSent(GURL("chrome://new-tab"), false);
VerifyPageUrlSent(GURL("https://192.168.0.1"), false);
VerifyPageUrlSent(GURL("https://localhost:8888"), false);
}
TEST_F(CompanionUrlBuilderTest, WithTextQuery) {
GURL page_url(kValidUrl);
GURL companion_url = url_builder_->BuildCompanionURL(page_url, kTextQuery);
std::string value;
EXPECT_TRUE(net::GetValueForKeyInQuery(companion_url, "url", &value));
EXPECT_EQ(value, page_url.spec());
EXPECT_TRUE(net::GetValueForKeyInQuery(companion_url, "q", &value));
EXPECT_EQ(value, kTextQuery);
EXPECT_TRUE(net::GetValueForKeyInQuery(companion_url, "origin", &value));
EXPECT_EQ(value, kOrigin);
}
TEST_F(CompanionUrlBuilderTest, WithoutTextQuery) {
GURL page_url(kValidUrl);
GURL companion_url = url_builder_->BuildCompanionURL(page_url);
std::string value;
EXPECT_TRUE(net::GetValueForKeyInQuery(companion_url, "url", &value));
EXPECT_EQ(value, page_url.spec());
EXPECT_FALSE(net::GetValueForKeyInQuery(companion_url, "q", &value));
EXPECT_TRUE(net::GetValueForKeyInQuery(companion_url, "origin", &value));
EXPECT_EQ(value, kOrigin);
}
} // namespace companion