1 // Copyright 2024 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_TEST_WIN_FAKE_NETWORK_COST_MANAGER_H_ 6 #define NET_TEST_WIN_FAKE_NETWORK_COST_MANAGER_H_ 7 8 #include <windows.h> 9 10 #include <wrl/client.h> 11 12 #include <vector> 13 14 #include "base/synchronization/lock.h" 15 #include "base/thread_annotations.h" 16 #include "net/base/network_change_notifier.h" 17 18 namespace net { 19 class FakeNetworkCostManager; 20 21 // Each value represents a different Windows OS API that can fail when 22 // monitoring the cost of network connections. Use with 23 // `FakeNetworkCostManagerEnvironment::SimulateError()` to simulate Windows OS 24 // API failures that return error `HRESULT` codes. 25 enum class NetworkCostManagerStatus { 26 kOk, 27 kErrorCoCreateInstanceFailed, 28 kErrorQueryInterfaceFailed, 29 kErrorFindConnectionPointFailed, 30 kErrorAdviseFailed, 31 kErrorGetCostFailed, 32 }; 33 34 // Provides a fake implementation of the `INetworkCostManager` Windows OS API 35 // for `NetworkCostChangeNotifierWin`. Must be constructed before any 36 // `NetworkCostChangeNotifierWin` instances exist. Sets up the fake OS API in 37 // the constructor and restores the real OS API in the destructor. Tests should 38 // use this class to simulate different network costs, cost changed events and 39 // errors without depending on the actual OS APIs or current network 40 // environment. 41 class FakeNetworkCostManagerEnvironment { 42 public: 43 FakeNetworkCostManagerEnvironment(); 44 ~FakeNetworkCostManagerEnvironment(); 45 46 void SetCost(NetworkChangeNotifier::ConnectionCost value); 47 void SimulateError(NetworkCostManagerStatus error_status); 48 49 FakeNetworkCostManagerEnvironment(const FakeNetworkCostManagerEnvironment&) = 50 delete; 51 FakeNetworkCostManagerEnvironment& operator=( 52 const FakeNetworkCostManagerEnvironment&) = delete; 53 54 private: 55 // Creates a fake implementation of `INetworkCostManager`. 56 HRESULT FakeCoCreateInstance(REFCLSID class_id, 57 LPUNKNOWN outer_aggregate, 58 DWORD context_flags, 59 REFIID interface_id, 60 LPVOID* result); 61 62 // Members must be accessed while holding this lock to support the creation 63 // and use of `FakeNetworkCostManager` instances on any thread. 64 base::Lock member_lock_; 65 66 // The connection cost to simulate. 67 NetworkChangeNotifier::ConnectionCost connection_cost_ 68 GUARDED_BY(member_lock_) = 69 NetworkChangeNotifier::ConnectionCost::CONNECTION_COST_UNKNOWN; 70 71 // When `FakeNetworkCostManagerEnvironment` creates a new 72 // `FakeNetworkCostManager`, the new `FakeNetworkCostManager` will simulate 73 // this error. 74 NetworkCostManagerStatus error_status_ GUARDED_BY(member_lock_) = 75 NetworkCostManagerStatus::kOk; 76 77 // Holds the fake implementations of `INetworkCostManager` constructed through 78 // `FakeCoCreateInstance()`. 79 std::vector<Microsoft::WRL::ComPtr<FakeNetworkCostManager>> 80 fake_network_cost_managers_ GUARDED_BY(member_lock_); 81 }; 82 83 } // namespace net 84 85 #endif // NET_TEST_WIN_FAKE_NETWORK_COST_MANAGER_H_ 86