initialization_state_dcheck_test.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Copyright 2014 The Crashpad Authors. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "util/misc/initialization_state_dcheck.h"
  15. #include <stdlib.h>
  16. #include <memory>
  17. #include "base/logging.h"
  18. #include "base/memory/free_deleter.h"
  19. #include "gtest/gtest.h"
  20. #include "test/gtest_death_check.h"
  21. namespace crashpad {
  22. namespace test {
  23. namespace {
  24. TEST(InitializationStateDcheck, InitializationStateDcheck) {
  25. InitializationStateDcheck initialization_state_dcheck;
  26. INITIALIZATION_STATE_SET_INITIALIZING(initialization_state_dcheck);
  27. INITIALIZATION_STATE_SET_VALID(initialization_state_dcheck);
  28. INITIALIZATION_STATE_DCHECK_VALID(initialization_state_dcheck);
  29. }
  30. #if DCHECK_IS_ON()
  31. // InitializationStateDcheck only DCHECKs, so the death tests can only run
  32. // when DCHECKs are enabled.
  33. TEST(InitializationStateDcheckDeathTest, Uninitialized_NotInvalid) {
  34. // This tests that an attempt to set an uninitialized object as valid without
  35. // transitioning through the initializing (invalid) state fails.
  36. InitializationStateDcheck initialization_state_dcheck;
  37. ASSERT_DEATH_CHECK(
  38. INITIALIZATION_STATE_SET_VALID(initialization_state_dcheck),
  39. "kStateInvalid");
  40. }
  41. TEST(InitializationStateDcheckDeathTest, Uninitialized_NotValid) {
  42. // This tests that an attempt to use an uninitialized object as though it
  43. // were valid fails.
  44. InitializationStateDcheck initialization_state_dcheck;
  45. ASSERT_DEATH_CHECK(
  46. INITIALIZATION_STATE_DCHECK_VALID(initialization_state_dcheck),
  47. "kStateValid");
  48. }
  49. TEST(InitializationStateDcheckDeathTest, Invalid_NotUninitialized) {
  50. // This tests that an attempt to begin initializing an object on which
  51. // initialization was already attempted fails.
  52. InitializationStateDcheck initialization_state_dcheck;
  53. INITIALIZATION_STATE_SET_INITIALIZING(initialization_state_dcheck);
  54. ASSERT_DEATH_CHECK(
  55. INITIALIZATION_STATE_SET_INITIALIZING(initialization_state_dcheck),
  56. "kStateUninitialized");
  57. }
  58. TEST(InitializationStateDcheckDeathTest, Invalid_NotValid) {
  59. // This tests that an attempt to use an initializing object as though it
  60. // were valid fails.
  61. InitializationStateDcheck initialization_state_dcheck;
  62. INITIALIZATION_STATE_SET_INITIALIZING(initialization_state_dcheck);
  63. ASSERT_DEATH_CHECK(
  64. INITIALIZATION_STATE_DCHECK_VALID(initialization_state_dcheck),
  65. "kStateValid");
  66. }
  67. TEST(InitializationStateDcheckDeathTest, Valid_NotUninitialized) {
  68. // This tests that an attempt to begin initializing an object that has already
  69. // been initialized fails.
  70. InitializationStateDcheck initialization_state_dcheck;
  71. INITIALIZATION_STATE_SET_INITIALIZING(initialization_state_dcheck);
  72. INITIALIZATION_STATE_SET_VALID(initialization_state_dcheck);
  73. ASSERT_DEATH_CHECK(
  74. INITIALIZATION_STATE_SET_INITIALIZING(initialization_state_dcheck),
  75. "kStateUninitialized");
  76. }
  77. TEST(InitializationStateDcheckDeathTest, Valid_NotInvalid) {
  78. // This tests that an attempt to set a valid object as valid a second time
  79. // fails.
  80. InitializationStateDcheck initialization_state_dcheck;
  81. INITIALIZATION_STATE_SET_INITIALIZING(initialization_state_dcheck);
  82. INITIALIZATION_STATE_SET_VALID(initialization_state_dcheck);
  83. ASSERT_DEATH_CHECK(
  84. INITIALIZATION_STATE_SET_VALID(initialization_state_dcheck),
  85. "kStateInvalid");
  86. }
  87. TEST(InitializationStateDcheckDeathTest, Destroyed_NotUninitialized) {
  88. // This tests that an attempt to reinitialize a destroyed object fails. See
  89. // the InitializationState.InitializationState test for an explanation of this
  90. // use-after-free test.
  91. std::unique_ptr<InitializationStateDcheck, base::FreeDeleter>
  92. initialization_state_dcheck_buffer(static_cast<InitializationStateDcheck*>(
  93. malloc(sizeof(InitializationStateDcheck))));
  94. InitializationStateDcheck* initialization_state_dcheck =
  95. new (initialization_state_dcheck_buffer.get())
  96. InitializationStateDcheck();
  97. INITIALIZATION_STATE_SET_INITIALIZING(*initialization_state_dcheck);
  98. INITIALIZATION_STATE_SET_VALID(*initialization_state_dcheck);
  99. INITIALIZATION_STATE_DCHECK_VALID(*initialization_state_dcheck);
  100. initialization_state_dcheck->~InitializationStateDcheck();
  101. ASSERT_DEATH_CHECK(
  102. INITIALIZATION_STATE_SET_INITIALIZING(*initialization_state_dcheck),
  103. "kStateUninitialized");
  104. }
  105. TEST(InitializationStateDcheckDeathTest, Destroyed_NotValid) {
  106. // This tests that an attempt to use a destroyed object fails. See the
  107. // InitializationState.InitializationState test for an explanation of this
  108. // use-after-free test.
  109. std::unique_ptr<InitializationStateDcheck, base::FreeDeleter>
  110. initialization_state_dcheck_buffer(static_cast<InitializationStateDcheck*>(
  111. malloc(sizeof(InitializationStateDcheck))));
  112. InitializationStateDcheck* initialization_state_dcheck =
  113. new (initialization_state_dcheck_buffer.get())
  114. InitializationStateDcheck();
  115. INITIALIZATION_STATE_SET_INITIALIZING(*initialization_state_dcheck);
  116. INITIALIZATION_STATE_SET_VALID(*initialization_state_dcheck);
  117. INITIALIZATION_STATE_DCHECK_VALID(*initialization_state_dcheck);
  118. initialization_state_dcheck->~InitializationStateDcheck();
  119. ASSERT_DEATH_CHECK(
  120. INITIALIZATION_STATE_DCHECK_VALID(*initialization_state_dcheck),
  121. "kStateValid");
  122. }
  123. #endif
  124. } // namespace
  125. } // namespace test
  126. } // namespace crashpad