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