source: box/trunk/lib/common/Test.h @ 3060

Revision 3060, 4.9 KB checked in by chris, 4 months ago (diff)

Log disabling of exception logging to help understand test output.

Allow std::ostringstream formatting in expected/actual output comparison.

  • Property svn:eol-style set to native
Line 
1// --------------------------------------------------------------------------
2//
3// File
4//              Name:    Test.h
5//              Purpose: Useful stuff for tests
6//              Created: 2003/07/11
7//
8// --------------------------------------------------------------------------
9
10#ifndef TEST__H
11#define TEST__H
12
13#include <cstring>
14
15#ifdef WIN32
16#define BBACKUPCTL      "..\\..\\bin\\bbackupctl\\bbackupctl.exe"
17#define BBACKUPD        "..\\..\\bin\\bbackupd\\bbackupd.exe"
18#define BBSTORED        "..\\..\\bin\\bbstored\\bbstored.exe"
19#define BBACKUPQUERY    "..\\..\\bin\\bbackupquery\\bbackupquery.exe"
20#define BBSTOREACCOUNTS "..\\..\\bin\\bbstoreaccounts\\bbstoreaccounts.exe"
21#define TEST_RETURN(actual, expected) TEST_EQUAL(expected, actual);
22#else
23#define BBACKUPCTL      "../../bin/bbackupctl/bbackupctl"
24#define BBACKUPD        "../../bin/bbackupd/bbackupd"
25#define BBSTORED        "../../bin/bbstored/bbstored"
26#define BBACKUPQUERY    "../../bin/bbackupquery/bbackupquery"
27#define BBSTOREACCOUNTS "../../bin/bbstoreaccounts/bbstoreaccounts"
28#define TEST_RETURN(actual, expected) TEST_EQUAL((expected << 8), actual);
29#endif
30
31extern int failures;
32extern int first_fail_line;
33extern std::string first_fail_file;
34extern std::string bbackupd_args, bbstored_args, bbackupquery_args, test_args;
35
36#define TEST_FAIL_WITH_MESSAGE(msg) \
37{ \
38        if (failures == 0) \
39        { \
40                first_fail_file = __FILE__; \
41                first_fail_line = __LINE__; \
42        } \
43        failures++; \
44        BOX_ERROR("**** TEST FAILURE: " << msg << " at " << __FILE__ << \
45                ":" << __LINE__); \
46}
47
48#define TEST_ABORT_WITH_MESSAGE(msg) {TEST_FAIL_WITH_MESSAGE(msg); return 1;}
49
50#define TEST_THAT(condition) {if(!(condition)) TEST_FAIL_WITH_MESSAGE("Condition [" #condition "] failed")}
51#define TEST_THAT_ABORTONFAIL(condition) {if(!(condition)) TEST_ABORT_WITH_MESSAGE("Condition [" #condition "] failed")}
52
53// NOTE: The 0- bit is to allow this to work with stuff which has negative constants for flags (eg ConnectionException)
54#define TEST_CHECK_THROWS(statement, excepttype, subtype) \
55        { \
56                bool didthrow = false; \
57                HideExceptionMessageGuard hide; \
58                BOX_TRACE("Exception logging disabled at " __FILE__ ":" \
59                        << __LINE__); \
60                try \
61                { \
62                        statement; \
63                } \
64                catch(excepttype &e) \
65                { \
66                        if(e.GetSubType() != ((unsigned int)excepttype::subtype) \
67                                        && e.GetSubType() != (unsigned int)(0-excepttype::subtype)) \
68                        { \
69                                throw; \
70                        } \
71                        didthrow = true; \
72                } \
73                catch(...) \
74                { \
75                        throw; \
76                } \
77                if(!didthrow) \
78                { \
79                        TEST_FAIL_WITH_MESSAGE("Didn't throw exception " #excepttype "(" #subtype ")") \
80                } \
81        }
82
83// utility macro for comparing two strings in a line
84#define TEST_EQUAL(_expected, _found) \
85{ \
86        std::ostringstream _oss1; \
87        _oss1 << _expected; \
88        std::string _exp_str = _oss1.str(); \
89        \
90        std::ostringstream _oss2; \
91        _oss2 << _found; \
92        std::string _found_str = _oss2.str(); \
93        \
94        if(_exp_str != _found_str) \
95        { \
96                BOX_ERROR("Expected <" << _exp_str << "> but found <" << \
97                        _found_str << ">"); \
98                \
99                std::ostringstream _oss3; \
100                _oss3 << #_found << " != " << #_expected; \
101                \
102                TEST_FAIL_WITH_MESSAGE(_oss3.str().c_str()); \
103        } \
104}
105
106// utility macro for comparing two strings in a line
107#define TEST_EQUAL_LINE(_expected, _found, _line) \
108{ \
109        std::ostringstream _oss1; \
110        _oss1 << _expected; \
111        std::string _exp_str = _oss1.str(); \
112        \
113        std::ostringstream _oss2; \
114        _oss2 << _found; \
115        std::string _found_str = _oss2.str(); \
116        \
117        if(_exp_str != _found_str) \
118        { \
119                std::ostringstream _ossl; \
120                _ossl << _line; \
121                std::string _line_str = _ossl.str(); \
122                printf("Expected <%s> but found <%s> in <%s>\n", \
123                        _exp_str.c_str(), _found_str.c_str(), _line_str.c_str()); \
124                \
125                std::ostringstream _oss3; \
126                _oss3 << #_found << " != " << #_expected << " in " << _line; \
127                \
128                TEST_FAIL_WITH_MESSAGE(_oss3.str().c_str()); \
129        } \
130}
131
132// utility macro for testing a line
133#define TEST_LINE(_condition, _line) \
134        TEST_THAT(_condition); \
135        if (!(_condition)) \
136        { \
137                std::ostringstream _ossl; \
138                _ossl << _line; \
139                std::string _line_str = _ossl.str(); \
140                printf("Test failed on <%s>\n", _line_str.c_str()); \
141        }
142
143bool TestFileExists(const char *Filename);
144bool TestDirExists(const char *Filename);
145
146// -1 if doesn't exist
147int TestGetFileSize(const std::string& Filename);
148std::string ConvertPaths(const std::string& rOriginal);
149int RunCommand(const std::string& rCommandLine);
150bool ServerIsAlive(int pid);
151int ReadPidFile(const char *pidFile);
152int LaunchServer(const std::string& rCommandLine, const char *pidFile);
153int WaitForServerStartup(const char *pidFile, int pidIfKnown);
154
155#define TestRemoteProcessMemLeaks(filename) \
156        TestRemoteProcessMemLeaksFunc(filename, __FILE__, __LINE__)
157
158void TestRemoteProcessMemLeaksFunc(const char *filename,
159        const char* file, int line);
160
161void force_sync();
162void wait_for_sync_start();
163void wait_for_sync_end();
164void sync_and_wait();
165void terminate_bbackupd(int pid);
166
167// Wait a given number of seconds for something to complete
168void wait_for_operation(int seconds, const char* message);
169void safe_sleep(int seconds);
170
171#endif // TEST__H
Note: See TracBrowser for help on using the repository browser.