xref: /aosp_15_r20/external/cronet/net/network_error_logging/network_error_logging_test_util.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2018 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef NET_NETWORK_ERROR_LOGGING_NETWORK_ERROR_LOGGING_TEST_UTIL_H_
6 #define NET_NETWORK_ERROR_LOGGING_NETWORK_ERROR_LOGGING_TEST_UTIL_H_
7 
8 #include <string>
9 #include <vector>
10 
11 #include "base/functional/callback.h"
12 #include "net/base/address_list.h"
13 #include "net/base/ip_address.h"
14 #include "net/network_error_logging/network_error_logging_service.h"
15 #include "url/origin.h"
16 
17 namespace net {
18 
19 class IPAddress;
20 
21 // A NetworkErrorLoggingService implementation that stashes all NEL headers and
22 // reports so that they can be easily verified in unit tests.
23 class TestNetworkErrorLoggingService : public NetworkErrorLoggingService {
24  public:
25   struct Header {
26     Header() = default;
27     ~Header() = default;
28 
29     // Returns whether the |received_ip_address| field matches any of the
30     // addresses in |address_list|.
31     bool MatchesAddressList(const AddressList& address_list) const;
32 
33     NetworkAnonymizationKey network_anonymization_key;
34     url::Origin origin;
35     IPAddress received_ip_address;
36     std::string value;
37   };
38 
39   TestNetworkErrorLoggingService();
40 
41   TestNetworkErrorLoggingService(const TestNetworkErrorLoggingService&) =
42       delete;
43   TestNetworkErrorLoggingService& operator=(
44       const TestNetworkErrorLoggingService&) = delete;
45 
46   ~TestNetworkErrorLoggingService() override;
47 
headers()48   const std::vector<Header>& headers() { return headers_; }
errors()49   const std::vector<RequestDetails>& errors() { return errors_; }
50 
51   // NetworkErrorLoggingService implementation
52   void OnHeader(const NetworkAnonymizationKey& network_anonymization_key,
53                 const url::Origin& origin,
54                 const IPAddress& received_ip_address,
55                 const std::string& value) override;
56   void OnRequest(RequestDetails details) override;
57   void QueueSignedExchangeReport(SignedExchangeReportDetails details) override;
58   void RemoveBrowsingData(
59       const base::RepeatingCallback<bool(const url::Origin&)>& origin_filter)
60       override;
61   void RemoveAllBrowsingData() override;
62 
63  private:
64   std::vector<Header> headers_;
65   std::vector<RequestDetails> errors_;
66 };
67 
68 }  // namespace net
69 
70 #endif  // NET_NETWORK_ERROR_LOGGING_NETWORK_ERROR_LOGGING_TEST_UTIL_H_
71