File: profile_management_navigation_throttle_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (381 lines) | stat: -rw-r--r-- 15,564 bytes parent folder | download | duplicates (5)
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// 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/enterprise/profile_management/profile_management_navigation_throttle.h"

#include "base/base64.h"
#include "base/run_loop.h"
#include "base/strings/strcat.h"
#include "base/strings/stringprintf.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/enterprise/profile_management/profile_management_features.h"
#include "chrome/browser/enterprise/profile_management/saml_response_parser.h"
#include "chrome/browser/profiles/profiles_state.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/browser_with_test_window_test.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/mock_navigation_handle.h"
#include "content/public/test/mock_navigation_throttle_registry.h"
#include "services/data_decoder/public/cpp/test_support/in_process_data_decoder.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace profile_management {

using testing::_;

namespace {

constexpr char kSupportedDomain[] = "https://supported.test";
constexpr char kSwitchDomain[] = "switch.test";
constexpr char kGoogleServiceLoginUrl[] = "www.google.com/a/%s/ServiceLogin";
constexpr char kTokenUrl[] = "token.test/";
constexpr char kUnmanagedUrl[] = "unmanaged.test/";
constexpr char kTestUrl[] = "foo/1";

constexpr char kValidDomain[] = "host.test";
constexpr char kInvalidDomain[] = "host";
constexpr char kValidEmail[] = "user@host.test";
constexpr char kInvalidEmail[] = "user@host";

constexpr char kJSONAttributesTemplate[] = R"({"%s":{"name":"placeholderName",
"domain":"placeholderDomain","token":"placeholderToken"}})";

constexpr char kHTMLTemplate[] = R"(<html><body><form>
      <input name="SAMLResponse" value="%s"/></form></body></html>)";

constexpr char kSAMLResponse[] = R"(
<samlp:Response>
  <Assertion>
    <AttributeStatement>
      <Attribute Name="placeholderDomain">
        <AttributeValue>%s</AttributeValue>
      </Attribute>
      <Attribute Name="placeholderToken">
        <AttributeValue>%s</AttributeValue>
      </Attribute>
    </AttributeStatement>
  </Assertion>
</samlp:Response>)";

std::string BuildSAMLResponse(const std::string& domain,
                              const std::string& token = "token-value") {
  std::string encoded_saml_response = base::Base64Encode(
      base::StringPrintf(kSAMLResponse, domain.c_str(), token.c_str()));
  return base::StringPrintf(kHTMLTemplate, encoded_saml_response.c_str());
}

}  // namespace

class ProfileManagementNavigationThrottleTest
    : public BrowserWithTestWindowTest {
 public:
  ProfileManagementNavigationThrottleTest() = default;

  ~ProfileManagementNavigationThrottleTest() override = default;

  void SetUp() override {
    BrowserWithTestWindowTest::SetUp();
    // Create the first tab so that web_contents() exists.
    AddTab(browser(), GURL(base::StrCat({"http://", kTestUrl})));
  }

  std::unique_ptr<ProfileManagementNavigationThrottle> GetNavigationThrottle(
      content::NavigationThrottleRegistry& registry) {
    auto throttle =
        std::make_unique<ProfileManagementNavigationThrottle>(registry);
    throttle->ClearAttributeMapForTesting();
    return throttle;
  }

  content::WebContents* web_contents() const {
    return browser()->tab_strip_model()->GetActiveWebContents();
  }

  content::RenderFrameHost* main_frame() const {
    return web_contents()->GetPrimaryMainFrame();
  }
};

TEST_F(ProfileManagementNavigationThrottleTest, FeaturesDisabled) {
  base::test::ScopedFeatureList features;
  features.InitWithFeatures(/*enabled_features=*/{}, /*disabled_features=*/{
                                features::kEnableProfileTokenManagement,
                                features::kThirdPartyProfileManagement});

  content::MockNavigationHandle navigation_handle(
      GURL("https://www.example.test/"), main_frame());
  content::MockNavigationThrottleRegistry registry(
      &navigation_handle,
      content::MockNavigationThrottleRegistry::RegistrationMode::kHold);

  ProfileManagementNavigationThrottle::MaybeCreateAndAdd(registry);
  ASSERT_EQ(0u, registry.throttles().size());
}

TEST_F(ProfileManagementNavigationThrottleTest, ProfileCreationDisallowed) {
  base::test::ScopedFeatureList features(
      features::kEnableProfileTokenManagement);
  g_browser_process->local_state()->SetBoolean(prefs::kBrowserAddPersonEnabled,
                                               false);
  ASSERT_FALSE(profiles::IsProfileCreationAllowed());

  content::MockNavigationHandle navigation_handle(
      GURL("https://www.example.test/"), main_frame());
  content::MockNavigationThrottleRegistry registry(
      &navigation_handle,
      content::MockNavigationThrottleRegistry::RegistrationMode::kHold);

  ProfileManagementNavigationThrottle::MaybeCreateAndAdd(registry);
  ASSERT_EQ(0u, registry.throttles().size());
}

TEST_F(ProfileManagementNavigationThrottleTest, UnsupportedHost) {
  base::test::ScopedFeatureList features(
      features::kEnableProfileTokenManagement);
  content::MockNavigationHandle navigation_handle(
      GURL("https://unsupported.host/"), main_frame());
  EXPECT_CALL(navigation_handle, GetResponseBody(_)).Times(0);

  content::MockNavigationThrottleRegistry registry(
      &navigation_handle,
      content::MockNavigationThrottleRegistry::RegistrationMode::kHold);
  auto throttle = GetNavigationThrottle(registry);
  EXPECT_EQ(content::NavigationThrottle::PROCEED,
            throttle->WillProcessResponse().action());
}

TEST_F(ProfileManagementNavigationThrottleTest, Switch_InvalidJSON) {
  base::test::ScopedFeatureList features(
      features::kEnableProfileTokenManagement);
  // Set the command line switch to an invalid JSON string.
  base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  command_line->AppendSwitchASCII(
      switches::kProfileManagementAttributes,
      base::StringPrintf(kJSONAttributesTemplate,
                         base::StrCat({kSwitchDomain, "\""}).c_str()));

  content::MockNavigationHandle navigation_handle(
      GURL(base::StrCat({"https://", kSwitchDomain})), main_frame());
  EXPECT_CALL(navigation_handle, GetResponseBody(_)).Times(0);
  content::MockNavigationThrottleRegistry registry(
      &navigation_handle,
      content::MockNavigationThrottleRegistry::RegistrationMode::kHold);

  auto throttle = GetNavigationThrottle(registry);
  EXPECT_EQ(content::NavigationThrottle::PROCEED,
            throttle->WillProcessResponse().action());
}

TEST_F(ProfileManagementNavigationThrottleTest, Switch_InvalidAttributes) {
  base::test::ScopedFeatureList features(
      features::kEnableProfileTokenManagement);
  // Set the command line switch attributes as a string instead of JSON object.
  base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  command_line->AppendSwitchASCII(
      switches::kProfileManagementAttributes,
      base::StringPrintf(R"({"%s": "attribute_value"})", kSwitchDomain));

  content::MockNavigationHandle navigation_handle(
      GURL(base::StrCat({"https://", kSwitchDomain})), main_frame());
  EXPECT_CALL(navigation_handle, GetResponseBody(_)).Times(0);

  content::MockNavigationThrottleRegistry registry(
      &navigation_handle,
      content::MockNavigationThrottleRegistry::RegistrationMode::kHold);

  auto throttle = GetNavigationThrottle(registry);
  EXPECT_EQ(content::NavigationThrottle::PROCEED,
            throttle->WillProcessResponse().action());
}

class ProfileManagementNavigationThrottleRedirectTest
    : public ProfileManagementNavigationThrottleTest {
 public:
  ProfileManagementNavigationThrottleRedirectTest() = default;
  ~ProfileManagementNavigationThrottleRedirectTest() override = default;

  void SetUp() override {
    ProfileManagementNavigationThrottleTest::SetUp();
    features_.InitWithFeatures(
        /*enabled_features=*/{features::kEnableProfileTokenManagement,
                              features::kThirdPartyProfileManagement},
        /*disabled_features=*/{});
  }

  void SetNavigationHandleExpectations(content::MockNavigationHandle& handle,
                                       const std::string& response_body) {
    ON_CALL(handle, GetResponseBody(_))
        .WillByDefault(
            [response_body](
                content::MockNavigationHandle::ResponseBodyCallback callback) {
              std::move(callback).Run(response_body);
            });
    EXPECT_CALL(handle, GetResponseBody(_)).Times(1);
  }

  std::unique_ptr<ProfileManagementNavigationThrottle> GetNavigationThrottle(
      content::NavigationThrottleRegistry& registry) {
    auto throttle =
        std::make_unique<ProfileManagementNavigationThrottle>(registry);
    throttle->ClearAttributeMapForTesting();
    throttle->SetURLsForTesting(base::StrCat({"https://", kTokenUrl}),
                                base::StrCat({"https://", kUnmanagedUrl}));
    return throttle;
  }

 protected:
  base::RunLoop loop_;

 private:
  base::test::ScopedFeatureList features_;
  data_decoder::test::InProcessDataDecoder in_process_data_decoder_;
};

TEST_F(ProfileManagementNavigationThrottleRedirectTest, InvalidEmail) {
  content::MockNavigationHandle navigation_handle(
      GURL(std::string(kSupportedDomain)), main_frame());
  SetNavigationHandleExpectations(navigation_handle,
                                  BuildSAMLResponse(kInvalidEmail));

  content::MockNavigationThrottleRegistry registry(
      &navigation_handle,
      content::MockNavigationThrottleRegistry::RegistrationMode::kHold);
  auto throttle = GetNavigationThrottle(registry);
  EXPECT_EQ(content::NavigationThrottle::DEFER,
            throttle->WillProcessResponse().action());
  loop_.RunUntilIdle();

  // The throttle does not navigate away from its current URL if the provided
  // domain is invalid.
  EXPECT_EQ(kTestUrl, web_contents()->GetURL().GetContent());
}

TEST_F(ProfileManagementNavigationThrottleRedirectTest, ValidEmail) {
  content::MockNavigationHandle navigation_handle(
      GURL(std::string(kSupportedDomain)), main_frame());
  SetNavigationHandleExpectations(navigation_handle,
                                  BuildSAMLResponse(kValidEmail));

  content::MockNavigationThrottleRegistry registry(
      &navigation_handle,
      content::MockNavigationThrottleRegistry::RegistrationMode::kHold);
  auto throttle = GetNavigationThrottle(registry);
  EXPECT_EQ(content::NavigationThrottle::DEFER,
            throttle->WillProcessResponse().action());
  loop_.RunUntilIdle();

  // The throttle navigates to the Google sign-in URL for the domain
  // corresponding to the parsed email address.
  EXPECT_EQ(base::StringPrintf(kGoogleServiceLoginUrl, kValidDomain),
            web_contents()->GetURL().GetContent());
}

TEST_F(ProfileManagementNavigationThrottleRedirectTest, EmptyDomain) {
  content::MockNavigationHandle navigation_handle(
      GURL(std::string(kSupportedDomain)), main_frame());
  SetNavigationHandleExpectations(navigation_handle,
                                  BuildSAMLResponse(std::string()));

  content::MockNavigationThrottleRegistry registry(
      &navigation_handle,
      content::MockNavigationThrottleRegistry::RegistrationMode::kHold);
  auto throttle = GetNavigationThrottle(registry);
  EXPECT_EQ(content::NavigationThrottle::DEFER,
            throttle->WillProcessResponse().action());
  loop_.RunUntilIdle();

  // The throttle navigates to the token-based management test URL when a token
  // is received but a domain is not.
  EXPECT_EQ(kTokenUrl, web_contents()->GetURL().GetContent());
}

TEST_F(ProfileManagementNavigationThrottleRedirectTest, EmptyDomainAndToken) {
  content::MockNavigationHandle navigation_handle(
      GURL(std::string(kSupportedDomain)), main_frame());
  SetNavigationHandleExpectations(
      navigation_handle, BuildSAMLResponse(std::string(), std::string()));

  content::MockNavigationThrottleRegistry registry(
      &navigation_handle,
      content::MockNavigationThrottleRegistry::RegistrationMode::kHold);
  auto throttle = GetNavigationThrottle(registry);
  EXPECT_EQ(content::NavigationThrottle::DEFER,
            throttle->WillProcessResponse().action());
  loop_.RunUntilIdle();

  // The throttle navigates to the unmanaged test URL when neither a domain nor
  // a token are received.
  EXPECT_EQ(kUnmanagedUrl, web_contents()->GetURL().GetContent());
}

TEST_F(ProfileManagementNavigationThrottleRedirectTest, InvalidDomain) {
  content::MockNavigationHandle navigation_handle(
      GURL(std::string(kSupportedDomain)), main_frame());
  SetNavigationHandleExpectations(navigation_handle,
                                  BuildSAMLResponse(kInvalidDomain));

  content::MockNavigationThrottleRegistry registry(
      &navigation_handle,
      content::MockNavigationThrottleRegistry::RegistrationMode::kHold);
  auto throttle = GetNavigationThrottle(registry);
  EXPECT_EQ(content::NavigationThrottle::DEFER,
            throttle->WillProcessResponse().action());
  loop_.RunUntilIdle();

  // The throttle does not navigate away from its current URL if the provided
  // domain is invalid.
  EXPECT_EQ(kTestUrl, web_contents()->GetURL().GetContent());
}

TEST_F(ProfileManagementNavigationThrottleRedirectTest, ValidDomain) {
  content::MockNavigationHandle navigation_handle(
      GURL(std::string(kSupportedDomain)), main_frame());
  SetNavigationHandleExpectations(navigation_handle,
                                  BuildSAMLResponse(kValidDomain));

  content::MockNavigationThrottleRegistry registry(
      &navigation_handle,
      content::MockNavigationThrottleRegistry::RegistrationMode::kHold);
  auto throttle = GetNavigationThrottle(registry);
  EXPECT_EQ(content::NavigationThrottle::DEFER,
            throttle->WillProcessResponse().action());
  loop_.RunUntilIdle();

  // The throttle navigates to the Google sign-in URL corresponding to the
  // parsed domain when the domain is valid.
  EXPECT_EQ(base::StringPrintf(kGoogleServiceLoginUrl, kValidDomain),
            web_contents()->GetURL().GetContent());
}

TEST_F(ProfileManagementNavigationThrottleRedirectTest, Switch_ValidDomain) {
  // Set a new supported domain using the command line switch.
  base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  command_line->AppendSwitchASCII(
      switches::kProfileManagementAttributes,
      base::StringPrintf(kJSONAttributesTemplate, kSwitchDomain));

  content::MockNavigationHandle navigation_handle(
      GURL(base::StrCat({"https://", kSwitchDomain})), main_frame());
  SetNavigationHandleExpectations(navigation_handle,
                                  BuildSAMLResponse(kValidDomain));

  content::MockNavigationThrottleRegistry registry(
      &navigation_handle,
      content::MockNavigationThrottleRegistry::RegistrationMode::kHold);
  auto throttle = GetNavigationThrottle(registry);
  EXPECT_EQ(content::NavigationThrottle::DEFER,
            throttle->WillProcessResponse().action());
  loop_.RunUntilIdle();

  // The throttle navigates to the Google sign-in URL corresponding to the
  // parsed domain when the domain is valid.
  EXPECT_EQ(base::StringPrintf(kGoogleServiceLoginUrl, kValidDomain),
            web_contents()->GetURL().GetContent());
}

}  // namespace profile_management