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