PageRenderTime 107ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/components/history/content/browser/content_history_backend_db_unittest.cc

https://github.com/chromium/chromium
C++ | 134 lines | 93 code | 14 blank | 27 comment | 6 complexity | 7916633d0c1e2203bf9a90ec0ca02029 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, Apache-2.0, BSD-3-Clause
  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. // History unit tests come in two flavors:
  5. //
  6. // 1. The more complicated style is that the unit test creates a full history
  7. // service. This spawns a background thread for the history backend, and
  8. // all communication is asynchronous. This is useful for testing more
  9. // complicated things or end-to-end behavior.
  10. //
  11. // 2. The simpler style is to create a history backend on this thread and
  12. // access it directly without a HistoryService object. This is much simpler
  13. // because communication is synchronous. Generally, sets should go through
  14. // the history backend (since there is a lot of logic) but gets can come
  15. // directly from the HistoryDatabase. This is because the backend generally
  16. // has no logic in the getter except threading stuff, which we don't want
  17. // to run.
  18. #include <stddef.h>
  19. #include "components/history/core/browser/history_backend.h"
  20. #include "components/history/core/test/history_backend_db_base_test.h"
  21. namespace history {
  22. namespace {
  23. // This must be outside the anonymous namespace for the friend statement in
  24. // HistoryBackend to work.
  25. class ContentHistoryBackendDBTest : public HistoryBackendDBBaseTest {
  26. public:
  27. ContentHistoryBackendDBTest() {}
  28. ~ContentHistoryBackendDBTest() override {}
  29. };
  30. struct InterruptReasonAssociation {
  31. std::string name;
  32. int value;
  33. };
  34. // Test is dependent on interrupt reasons being listed in header file
  35. // in order.
  36. const InterruptReasonAssociation current_reasons[] = {
  37. #define INTERRUPT_REASON(a, b) { #a, b },
  38. #include "components/download/public/common/download_interrupt_reason_values.h"
  39. #undef INTERRUPT_REASON
  40. };
  41. // This represents a list of all reasons we've previously used;
  42. // Do Not Remove Any Entries From This List.
  43. const InterruptReasonAssociation historical_reasons[] = {
  44. {"FILE_FAILED", 1},
  45. {"FILE_ACCESS_DENIED", 2},
  46. {"FILE_NO_SPACE", 3},
  47. {"FILE_NAME_TOO_LONG", 5},
  48. {"FILE_TOO_LARGE", 6},
  49. {"FILE_VIRUS_INFECTED", 7},
  50. {"FILE_TRANSIENT_ERROR", 10},
  51. {"FILE_BLOCKED", 11},
  52. {"FILE_SECURITY_CHECK_FAILED", 12},
  53. {"FILE_TOO_SHORT", 13},
  54. {"FILE_HASH_MISMATCH", 14},
  55. {"FILE_SAME_AS_SOURCE", 15},
  56. {"NETWORK_FAILED", 20},
  57. {"NETWORK_TIMEOUT", 21},
  58. {"NETWORK_DISCONNECTED", 22},
  59. {"NETWORK_SERVER_DOWN", 23},
  60. {"NETWORK_INVALID_REQUEST", 24},
  61. {"SERVER_FAILED", 30},
  62. {"SERVER_NO_RANGE", 31},
  63. {"SERVER_PRECONDITION", 32},
  64. {"SERVER_BAD_CONTENT", 33},
  65. {"SERVER_UNAUTHORIZED", 34},
  66. {"SERVER_CERT_PROBLEM", 35},
  67. {"SERVER_FORBIDDEN", 36},
  68. {"SERVER_UNREACHABLE", 37},
  69. {"SERVER_CONTENT_LENGTH_MISMATCH", 38},
  70. {"SERVER_CROSS_ORIGIN_REDIRECT", 39},
  71. {"USER_CANCELED", 40},
  72. {"USER_SHUTDOWN", 41},
  73. {"CRASH", 50},
  74. };
  75. // Make sure no one has changed a DownloadInterruptReason we've previously
  76. // persisted.
  77. TEST_F(ContentHistoryBackendDBTest,
  78. ConfirmDownloadInterruptReasonBackwardsCompatible) {
  79. // Are there any cases in which a historical number has been repurposed
  80. // for an error other than it's original?
  81. for (size_t i = 0; i < std::size(current_reasons); i++) {
  82. const InterruptReasonAssociation& cur_reason(current_reasons[i]);
  83. bool found = false;
  84. for (size_t j = 0; j < std::size(historical_reasons); ++j) {
  85. const InterruptReasonAssociation& hist_reason(historical_reasons[j]);
  86. if (hist_reason.value == cur_reason.value) {
  87. EXPECT_EQ(cur_reason.name, hist_reason.name)
  88. << "Same integer value used for old error \""
  89. << hist_reason.name
  90. << "\" as for new error \""
  91. << cur_reason.name
  92. << "\"." << std::endl
  93. << "**This will cause database conflicts with persisted values**"
  94. << std::endl
  95. << "Please assign a new, non-conflicting value for the new error.";
  96. }
  97. if (hist_reason.name == cur_reason.name) {
  98. EXPECT_EQ(cur_reason.value, hist_reason.value)
  99. << "Same name (\"" << hist_reason.name
  100. << "\") maps to a different value historically ("
  101. << hist_reason.value << ") and currently ("
  102. << cur_reason.value << ")" << std::endl
  103. << "This may cause database conflicts with persisted values"
  104. << std::endl
  105. << "If this error is the same as the old one, you should"
  106. << std::endl
  107. << "use the old value, and if it is different, you should"
  108. << std::endl
  109. << "use a new name.";
  110. found = true;
  111. }
  112. }
  113. EXPECT_TRUE(found)
  114. << "Error \"" << cur_reason.name << "\" not found in historical list."
  115. << std::endl
  116. << "Please add it.";
  117. }
  118. }
  119. } // namespace
  120. } // namespace history