File: network_annotation_monitor_browsertest.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (108 lines) | stat: -rw-r--r-- 4,144 bytes parent folder | download | duplicates (4)
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
// Copyright 2024 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/net/network_annotation_monitor.h"

#include "base/test/metrics/histogram_tester.h"
#include "chrome/browser/browser_features.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/net/system_network_context_manager.h"
#include "chrome/browser/policy/policy_test_utils.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chromeos/dbus/regmon/regmon_client.h"
#include "components/policy/policy_constants.h"
#include "content/public/browser/network_service_instance.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/simple_url_loader_test_helper.h"
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/mojom/network_service.mojom.h"

namespace {

enum class TestCase {
  kEnabled,
  kDisabled,
};

// Network annotation that can trigger a violation.
constexpr net::NetworkTrafficAnnotationTag kTestDisabledAnnotation =
    net::DefineNetworkTrafficAnnotation("autofill_query", "");
// Policy that controls the above annotation. When set to false, any occurrences
// of the above annotation should trigger a violation.
constexpr char kTestPolicy[] = "PasswordManagerEnabled";

class NetworkAnnotationMonitorBrowserTest
    : public policy::PolicyTest,
      public ::testing::WithParamInterface<TestCase> {
 public:
  NetworkAnnotationMonitorBrowserTest() {
    if (GetParam() == TestCase::kEnabled) {
      feature_list_.InitAndEnableFeature(
          features::kNetworkAnnotationMonitoring);
    } else {
      feature_list_.InitAndDisableFeature(
          features::kNetworkAnnotationMonitoring);
    }
  }

  void SendFakeNetworkRequest(net::NetworkTrafficAnnotationTag annotation) {
    auto request = std::make_unique<network::ResourceRequest>();

    content::SimpleURLLoaderTestHelper simple_loader_helper;
    std::unique_ptr<network::SimpleURLLoader> simple_loader =
        network::SimpleURLLoader::Create(std::move(request), annotation);
    simple_loader->DownloadToStringOfUnboundedSizeUntilCrashAndDie(
        browser()
            ->profile()
            ->GetDefaultStoragePartition()
            ->GetURLLoaderFactoryForBrowserProcess()
            .get(),
        simple_loader_helper.GetCallback());
    simple_loader_helper.WaitForCallback();
  }

 private:
  base::test::ScopedFeatureList feature_list_;
};

IN_PROC_BROWSER_TEST_P(NetworkAnnotationMonitorBrowserTest, FeatureTest) {
  policy::PolicyMap policies;
  SetPolicy(&policies, kTestPolicy, base::Value(false));
  provider_.UpdateChromePolicy(policies);

  // Send network request with arbitrary network annotation that will never
  // trigger a violation.
  SendFakeNetworkRequest(TRAFFIC_ANNOTATION_FOR_TESTS);
  // Send network request with a network annotation that will trigger a
  // violation.
  SendFakeNetworkRequest(kTestDisabledAnnotation);

  g_browser_process->system_network_context_manager()
      ->FlushNetworkAnnotationMonitorForTesting();

  // Disabled hash codes should trigger a violation only if the feature is
  // enabled.
  chromeos::RegmonClient* regmon_client = chromeos::RegmonClient::Get();
  if (GetParam() == TestCase::kEnabled) {
    std::list<int32_t> expected_hash_codes{
        kTestDisabledAnnotation.unique_id_hash_code};
    EXPECT_EQ(regmon_client->GetTestInterface()->GetReportedHashCodes(),
              expected_hash_codes);
  } else {
    std::list<int32_t> expected_hash_codes{};
    EXPECT_EQ(regmon_client->GetTestInterface()->GetReportedHashCodes(),
              expected_hash_codes);
  }
}

INSTANTIATE_TEST_SUITE_P(All,
                         NetworkAnnotationMonitorBrowserTest,
                         ::testing::Values(TestCase::kDisabled,
                                           TestCase::kEnabled));

}  // namespace