1 
2 /**
3  *    Copyright (C) 2018-present MongoDB, Inc.
4  *
5  *    This program is free software: you can redistribute it and/or modify
6  *    it under the terms of the Server Side Public License, version 1,
7  *    as published by MongoDB, Inc.
8  *
9  *    This program is distributed in the hope that it will be useful,
10  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *    Server Side Public License for more details.
13  *
14  *    You should have received a copy of the Server Side Public License
15  *    along with this program. If not, see
16  *    <http://www.mongodb.com/licensing/server-side-public-license>.
17  *
18  *    As a special exception, the copyright holders give permission to link the
19  *    code of portions of this program with the OpenSSL library under certain
20  *    conditions as described in each individual source file and distribute
21  *    linked combinations including the program with the OpenSSL library. You
22  *    must comply with the Server Side Public License in all respects for
23  *    all of the code used other than as permitted herein. If you modify file(s)
24  *    with this exception, you may extend this exception to your version of the
25  *    file(s), but you are not obligated to do so. If you do not wish to do so,
26  *    delete this exception statement from your version. If you delete this
27  *    exception statement from all source files in the program, then also delete
28  *    it in the license file.
29  */
30 
31 #pragma once
32 
33 #include <memory>
34 #include <string>
35 
36 #include "mongo/base/disallow_copying.h"
37 #include "mongo/stdx/memory.h"
38 #include "mongo/unittest/unittest.h"
39 
40 /**
41  * Constructs a single death test named "TEST_NAME" within the test case "CASE_NAME".
42  *
43  * The test only succeeds if the process terminates abnormally, e.g. through an fassert
44  * failure or deadly signal.
45  *
46  * Death tests are incompatible with already-running threads. If you need multiple threads
47  * in your death test, start them in the test body, or use DEATH_TEST_F and start them
48  * in the setUp() method of the fixture.
49  */
50 #define DEATH_TEST(CASE_NAME, TEST_NAME, MATCH_EXPR)                               \
51     class _TEST_TYPE_NAME(CASE_NAME, TEST_NAME) : public ::mongo::unittest::Test { \
52     private:                                                                       \
53         virtual void _doTest();                                                    \
54                                                                                    \
55         static const RegistrationAgent<                                            \
56             ::mongo::unittest::DeathTest<_TEST_TYPE_NAME(CASE_NAME, TEST_NAME)>>   \
57             _agent;                                                                \
58     };                                                                             \
59     const ::mongo::unittest::Test::RegistrationAgent<                              \
60         ::mongo::unittest::DeathTest<_TEST_TYPE_NAME(CASE_NAME, TEST_NAME)>>       \
61         _TEST_TYPE_NAME(CASE_NAME, TEST_NAME)::_agent(#CASE_NAME, #TEST_NAME);     \
62     std::string getDeathTestPattern(_TEST_TYPE_NAME(CASE_NAME, TEST_NAME)*) {      \
63         return MATCH_EXPR;                                                         \
64     }                                                                              \
65     void _TEST_TYPE_NAME(CASE_NAME, TEST_NAME)::_doTest()
66 
67 /**
68  * Constructs a single test named TEST_NAME that has access to a common fixture
69  * named "FIXTURE_NAME".
70  *
71  * See description of DEATH_TEST for more details on death tests.
72  */
73 #define DEATH_TEST_F(FIXTURE_NAME, TEST_NAME, MATCH_EXPR)                            \
74     class _TEST_TYPE_NAME(FIXTURE_NAME, TEST_NAME) : public FIXTURE_NAME {           \
75     private:                                                                         \
76         virtual void _doTest();                                                      \
77                                                                                      \
78         static const RegistrationAgent<                                              \
79             ::mongo::unittest::DeathTest<_TEST_TYPE_NAME(FIXTURE_NAME, TEST_NAME)>>  \
80             _agent;                                                                  \
81     };                                                                               \
82     const ::mongo::unittest::Test::RegistrationAgent<                                \
83         ::mongo::unittest::DeathTest<_TEST_TYPE_NAME(FIXTURE_NAME, TEST_NAME)>>      \
84         _TEST_TYPE_NAME(FIXTURE_NAME, TEST_NAME)::_agent(#FIXTURE_NAME, #TEST_NAME); \
85     std::string getDeathTestPattern(_TEST_TYPE_NAME(FIXTURE_NAME, TEST_NAME)*) {     \
86         return MATCH_EXPR;                                                           \
87     }                                                                                \
88     void _TEST_TYPE_NAME(FIXTURE_NAME, TEST_NAME)::_doTest()
89 
90 namespace mongo {
91 namespace unittest {
92 
93 class DeathTestImpl : public Test {
94     MONGO_DISALLOW_COPYING(DeathTestImpl);
95 
96 protected:
97     DeathTestImpl(std::unique_ptr<Test> test);
98 
99 private:
100     void _doTest() override;
101     virtual std::string getPattern() = 0;
102     std::unique_ptr<Test> _test;
103 };
104 
105 template <typename T>
106 class DeathTest : public DeathTestImpl {
107 public:
108     static const std::string pattern;
109 
110     template <typename... Args>
DeathTest(Args &&...args)111     DeathTest(Args&&... args) : DeathTestImpl(stdx::make_unique<T>(std::forward<Args>(args)...)) {}
112 
113 private:
getPattern()114     std::string getPattern() override {
115         return getDeathTestPattern(static_cast<T*>(nullptr));
116     }
117 };
118 
119 }  // namespace unittest
120 }  // namespace mongo
121