test_net_log_util.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. #include "net/log/test_net_log_util.h"
  5. #include <cstddef>
  6. #include "net/log/net_log_entry.h"
  7. namespace net {
  8. namespace {
  9. // Takes the list of entries and an offset, and returns an index into the array.
  10. // If |offset| is positive, just returns |offset|. If it's negative, it
  11. // indicates a position relative to the end of the array.
  12. size_t GetIndex(const std::vector<NetLogEntry>& entries, int offset) {
  13. if (offset >= 0)
  14. return static_cast<size_t>(offset);
  15. size_t abs_offset = static_cast<size_t>(-offset);
  16. // If offset indicates a position before the start of the array, just return
  17. // the end of the list.
  18. if (abs_offset > entries.size())
  19. return entries.size();
  20. return entries.size() - abs_offset;
  21. }
  22. } // namespace
  23. ::testing::AssertionResult LogContainsEvent(
  24. const std::vector<NetLogEntry>& entries,
  25. int offset,
  26. NetLogEventType expected_event,
  27. NetLogEventPhase expected_phase) {
  28. size_t index = GetIndex(entries, offset);
  29. if (index >= entries.size())
  30. return ::testing::AssertionFailure() << index << " is out of bounds.";
  31. const NetLogEntry& entry = entries[index];
  32. if (expected_event != entry.type) {
  33. return ::testing::AssertionFailure()
  34. << "Actual event: " << NetLogEventTypeToString(entry.type)
  35. << ". Expected event: " << NetLogEventTypeToString(expected_event)
  36. << ".";
  37. }
  38. if (expected_phase != entry.phase) {
  39. return ::testing::AssertionFailure()
  40. << "Actual phase: " << static_cast<int>(entry.phase)
  41. << ". Expected phase: " << static_cast<int>(expected_phase) << ".";
  42. }
  43. return ::testing::AssertionSuccess();
  44. }
  45. ::testing::AssertionResult LogContainsBeginEvent(
  46. const std::vector<NetLogEntry>& entries,
  47. int offset,
  48. NetLogEventType expected_event) {
  49. return LogContainsEvent(entries, offset, expected_event,
  50. NetLogEventPhase::BEGIN);
  51. }
  52. ::testing::AssertionResult LogContainsEndEvent(
  53. const std::vector<NetLogEntry>& entries,
  54. int offset,
  55. NetLogEventType expected_event) {
  56. return LogContainsEvent(entries, offset, expected_event,
  57. NetLogEventPhase::END);
  58. }
  59. ::testing::AssertionResult LogContainsEntryWithType(
  60. const std::vector<NetLogEntry>& entries,
  61. int offset,
  62. NetLogEventType type) {
  63. size_t index = GetIndex(entries, offset);
  64. if (index >= entries.size())
  65. return ::testing::AssertionFailure() << index << " is out of bounds.";
  66. const NetLogEntry& entry = entries[index];
  67. if (entry.type != type)
  68. return ::testing::AssertionFailure() << "Type does not match.";
  69. return ::testing::AssertionSuccess();
  70. }
  71. ::testing::AssertionResult LogContainsEntryWithTypeAfter(
  72. const std::vector<NetLogEntry>& entries,
  73. int start_offset,
  74. NetLogEventType type) {
  75. for (size_t i = GetIndex(entries, start_offset); i < entries.size(); ++i) {
  76. const NetLogEntry& entry = entries[i];
  77. if (entry.type == type)
  78. return ::testing::AssertionSuccess();
  79. }
  80. return ::testing::AssertionFailure();
  81. }
  82. size_t ExpectLogContainsSomewhere(const std::vector<NetLogEntry>& entries,
  83. size_t min_offset,
  84. NetLogEventType expected_event,
  85. NetLogEventPhase expected_phase) {
  86. size_t min_index = GetIndex(entries, min_offset);
  87. size_t i = 0;
  88. for (; i < entries.size(); ++i) {
  89. const NetLogEntry& entry = entries[i];
  90. if (entry.type == expected_event && entry.phase == expected_phase)
  91. break;
  92. }
  93. EXPECT_LT(i, entries.size());
  94. EXPECT_GE(i, min_index);
  95. return i;
  96. }
  97. size_t ExpectLogContainsSomewhereAfter(const std::vector<NetLogEntry>& entries,
  98. size_t start_offset,
  99. NetLogEventType expected_event,
  100. NetLogEventPhase expected_phase) {
  101. size_t i = GetIndex(entries, start_offset);
  102. for (; i < entries.size(); ++i) {
  103. const NetLogEntry& entry = entries[i];
  104. if (entry.type == expected_event && entry.phase == expected_phase)
  105. break;
  106. }
  107. EXPECT_LT(i, entries.size());
  108. return i;
  109. }
  110. absl::optional<std::string> GetOptionalStringValueFromParams(
  111. const NetLogEntry& entry,
  112. base::StringPiece path) {
  113. if (!entry.params.is_dict())
  114. return absl::nullopt;
  115. const std::string* result =
  116. entry.params.GetDict().FindStringByDottedPath(path);
  117. if (!result)
  118. return absl::nullopt;
  119. return *result;
  120. }
  121. absl::optional<bool> GetOptionalBooleanValueFromParams(const NetLogEntry& entry,
  122. base::StringPiece path) {
  123. if (!entry.params.is_dict())
  124. return absl::nullopt;
  125. return entry.params.GetDict().FindBoolByDottedPath(path);
  126. }
  127. absl::optional<int> GetOptionalIntegerValueFromParams(const NetLogEntry& entry,
  128. base::StringPiece path) {
  129. if (!entry.params.is_dict())
  130. return absl::nullopt;
  131. return entry.params.GetDict().FindIntByDottedPath(path);
  132. }
  133. absl::optional<int> GetOptionalNetErrorCodeFromParams(
  134. const NetLogEntry& entry) {
  135. return GetOptionalIntegerValueFromParams(entry, "net_error");
  136. }
  137. std::string GetStringValueFromParams(const NetLogEntry& entry,
  138. base::StringPiece path) {
  139. auto result = GetOptionalStringValueFromParams(entry, path);
  140. if (!result) {
  141. ADD_FAILURE() << "No string parameter " << path;
  142. return "";
  143. }
  144. return *result;
  145. }
  146. int GetIntegerValueFromParams(const NetLogEntry& entry,
  147. base::StringPiece path) {
  148. auto result = GetOptionalIntegerValueFromParams(entry, path);
  149. if (!result) {
  150. ADD_FAILURE() << "No int parameter " << path;
  151. return -1;
  152. }
  153. return *result;
  154. }
  155. bool GetBooleanValueFromParams(const NetLogEntry& entry,
  156. base::StringPiece path) {
  157. auto result = GetOptionalBooleanValueFromParams(entry, path);
  158. if (!result) {
  159. ADD_FAILURE() << "No bool parameter " << path;
  160. return -1;
  161. }
  162. return *result;
  163. }
  164. int GetNetErrorCodeFromParams(const NetLogEntry& entry) {
  165. auto result = GetOptionalNetErrorCodeFromParams(entry);
  166. if (!result) {
  167. ADD_FAILURE() << "No net_error parameter";
  168. return -1;
  169. }
  170. return *result;
  171. }
  172. } // namespace net