1 // Copyright 2012 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_LOG_TEST_NET_LOG_UTIL_H_ 6 #define NET_LOG_TEST_NET_LOG_UTIL_H_ 7 8 #include <stddef.h> 9 10 #include <optional> 11 #include <string> 12 #include <string_view> 13 #include <vector> 14 15 #include "net/log/net_log_event_type.h" 16 #include "testing/gtest/include/gtest/gtest.h" 17 18 namespace net { 19 20 struct NetLogEntry; 21 22 // Checks that the element of |entries| at |offset| has the provided values. 23 // A negative |offset| indicates a position relative to the end of |entries|. 24 // Checks to make sure |offset| is within bounds, and fails gracefully if it 25 // isn't. 26 ::testing::AssertionResult LogContainsEvent( 27 const std::vector<NetLogEntry>& entries, 28 int offset, 29 NetLogEventType expected_event, 30 NetLogEventPhase expected_phase); 31 32 // Just like LogContainsEvent, but always checks for an EventPhase of 33 // PHASE_BEGIN. 34 ::testing::AssertionResult LogContainsBeginEvent( 35 const std::vector<NetLogEntry>& entries, 36 int offset, 37 NetLogEventType expected_event); 38 39 // Just like LogContainsEvent, but always checks for an EventPhase of PHASE_END. 40 ::testing::AssertionResult LogContainsEndEvent( 41 const std::vector<NetLogEntry>& entries, 42 int offset, 43 NetLogEventType expected_event); 44 45 // Just like LogContainsEvent, but does not check phase. 46 ::testing::AssertionResult LogContainsEntryWithType( 47 const std::vector<NetLogEntry>& entries, 48 int offset, 49 NetLogEventType type); 50 51 // Check if the log contains an entry of the given type at |start_offset| or 52 // after. It is not a failure if there's an earlier matching entry. Negative 53 // offsets are relative to the end of the array. 54 ::testing::AssertionResult LogContainsEntryWithTypeAfter( 55 const std::vector<NetLogEntry>& entries, 56 int start_offset, 57 NetLogEventType type); 58 59 // Check if the first entry with the specified values is at |start_offset| or 60 // after. It is a failure if there's an earlier matching entry. Negative 61 // offsets are relative to the end of the array. 62 size_t ExpectLogContainsSomewhere(const std::vector<NetLogEntry>& entries, 63 size_t min_offset, 64 NetLogEventType expected_event, 65 NetLogEventPhase expected_phase); 66 67 // Check if the log contains an entry with the given values at |start_offset| 68 // or after. It is not a failure if there's an earlier matching entry. 69 // Negative offsets are relative to the end of the array. 70 size_t ExpectLogContainsSomewhereAfter(const std::vector<NetLogEntry>& entries, 71 size_t start_offset, 72 NetLogEventType expected_event, 73 NetLogEventPhase expected_phase); 74 75 // The following methods return a parameter of the given type at the given path, 76 // or nullopt if there is none. 77 std::optional<std::string> GetOptionalStringValueFromParams( 78 const NetLogEntry& entry, 79 std::string_view path); 80 std::optional<bool> GetOptionalBooleanValueFromParams(const NetLogEntry& entry, 81 std::string_view path); 82 std::optional<int> GetOptionalIntegerValueFromParams(const NetLogEntry& entry, 83 std::string_view path); 84 std::optional<int> GetOptionalNetErrorCodeFromParams(const NetLogEntry& entry); 85 86 // Same as the *Optional* versions above, except will add a Gtest failure if the 87 // value was not present, and then return some default. 88 std::string GetStringValueFromParams(const NetLogEntry& entry, 89 std::string_view path); 90 int GetIntegerValueFromParams(const NetLogEntry& entry, std::string_view path); 91 bool GetBooleanValueFromParams(const NetLogEntry& entry, std::string_view path); 92 int GetNetErrorCodeFromParams(const NetLogEntry& entry); 93 94 } // namespace net 95 96 #endif // NET_LOG_TEST_NET_LOG_UTIL_H_ 97