OLD | NEW |
(Empty) | |
| 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 |
| 15 #include "util/misc/initialization_state_dcheck.h" |
| 16 |
| 17 #include "base/logging.h" |
| 18 #include "gtest/gtest.h" |
| 19 |
| 20 namespace { |
| 21 |
| 22 using namespace crashpad; |
| 23 |
| 24 TEST(InitializationStateDcheck, InitializationStateDcheck) { |
| 25 InitializationStateDcheck initialization_state_dcheck; |
| 26 INITIALIZATION_STATE_DCHECK_BEGIN(initialization_state_dcheck); |
| 27 INITIALIZATION_STATE_SET_VALID(initialization_state_dcheck); |
| 28 INITIALIZATION_STATE_DCHECK_VALID(initialization_state_dcheck); |
| 29 } |
| 30 |
| 31 TEST(InitializationStateDcheck, InitializationStateDcheck_DirectToValid) { |
| 32 // It’s OK for an object to go straight from the uninitialized to valid states |
| 33 // without transitioning through the initializing state. |
| 34 InitializationStateDcheck initialization_state_dcheck; |
| 35 INITIALIZATION_STATE_SET_VALID(initialization_state_dcheck); |
| 36 INITIALIZATION_STATE_DCHECK_VALID(initialization_state_dcheck); |
| 37 } |
| 38 |
| 39 #if DCHECK_IS_ON |
| 40 |
| 41 // InitializationStateDcheck only DCHECKs, so the death tests can only run |
| 42 // when DCHECKs are enabled. |
| 43 |
| 44 TEST(InitializationStateDcheckDeathTest, Uninitialized_NotValid) { |
| 45 // This tests that an attempt to use an uninitialized object as though it |
| 46 // were valid fails. |
| 47 InitializationStateDcheck initialization_state_dcheck; |
| 48 ASSERT_DEATH(INITIALIZATION_STATE_DCHECK_VALID(initialization_state_dcheck), |
| 49 "kStateValid"); |
| 50 } |
| 51 |
| 52 TEST(InitializationStateDcheckDeathTest, Invalid_NotValid) { |
| 53 // This tests that an attempt to use an initializing object as though it |
| 54 // were valid fails. |
| 55 InitializationStateDcheck initialization_state_dcheck; |
| 56 INITIALIZATION_STATE_DCHECK_BEGIN(initialization_state_dcheck); |
| 57 ASSERT_DEATH(INITIALIZATION_STATE_DCHECK_VALID(initialization_state_dcheck), |
| 58 "kStateValid"); |
| 59 } |
| 60 |
| 61 TEST(InitializationStateDcheckDeathTest, Invalid_NotUninitialized) { |
| 62 // This tests that an attempt to begin initializing an object on which |
| 63 // initialization was already attempted fails. |
| 64 InitializationStateDcheck initialization_state_dcheck; |
| 65 INITIALIZATION_STATE_DCHECK_BEGIN(initialization_state_dcheck); |
| 66 ASSERT_DEATH(INITIALIZATION_STATE_DCHECK_BEGIN(initialization_state_dcheck), |
| 67 "kStateUninitialized"); |
| 68 } |
| 69 |
| 70 TEST(InitializationStateDcheckDeathTest, Valid_NotUninitialized) { |
| 71 // This tests that an attempt to begin initializing an object that has already |
| 72 // been initialized fails. |
| 73 InitializationStateDcheck initialization_state_dcheck; |
| 74 INITIALIZATION_STATE_DCHECK_BEGIN(initialization_state_dcheck); |
| 75 INITIALIZATION_STATE_SET_VALID(initialization_state_dcheck); |
| 76 ASSERT_DEATH(INITIALIZATION_STATE_DCHECK_BEGIN(initialization_state_dcheck), |
| 77 "kStateUninitialized"); |
| 78 } |
| 79 |
| 80 TEST(InitializationStateDcheckDeathTest, Destroyed_NotValid) { |
| 81 // This tests that an attempt to use a destroyed object fails. See the |
| 82 // InitializationState.InitializationState test for an explanation of this |
| 83 // use-after-free test. |
| 84 InitializationStateDcheck* initialization_state_dcheck_pointer; |
| 85 { |
| 86 InitializationStateDcheck initialization_state_dcheck; |
| 87 initialization_state_dcheck_pointer = &initialization_state_dcheck; |
| 88 INITIALIZATION_STATE_DCHECK_BEGIN(initialization_state_dcheck); |
| 89 INITIALIZATION_STATE_SET_VALID(initialization_state_dcheck); |
| 90 INITIALIZATION_STATE_DCHECK_VALID(initialization_state_dcheck); |
| 91 } |
| 92 ASSERT_DEATH( |
| 93 INITIALIZATION_STATE_DCHECK_VALID(*initialization_state_dcheck_pointer), |
| 94 "kStateValid"); |
| 95 } |
| 96 |
| 97 TEST(InitializationStateDcheckDeathTest, Destroyed_NotUninitialized) { |
| 98 // This tests that an attempt to reinitialize a destroyed object fails. See |
| 99 // the InitializationState.InitializationState test for an explanation of this |
| 100 // use-after-free test. |
| 101 InitializationStateDcheck* initialization_state_dcheck_pointer; |
| 102 { |
| 103 InitializationStateDcheck initialization_state_dcheck; |
| 104 initialization_state_dcheck_pointer = &initialization_state_dcheck; |
| 105 INITIALIZATION_STATE_DCHECK_BEGIN(initialization_state_dcheck); |
| 106 INITIALIZATION_STATE_SET_VALID(initialization_state_dcheck); |
| 107 INITIALIZATION_STATE_DCHECK_VALID(initialization_state_dcheck); |
| 108 } |
| 109 ASSERT_DEATH( |
| 110 INITIALIZATION_STATE_DCHECK_BEGIN(*initialization_state_dcheck_pointer), |
| 111 "kStateUninitialized"); |
| 112 } |
| 113 |
| 114 #endif |
| 115 |
| 116 } // namespace |
OLD | NEW |