test_net_log_util.h 4.0 KB

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