Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/***********************************************************************************************************************************
C Test Wrapper
 
This wrapper runs the C unit tests.
***********************************************************************************************************************************/
#include <build.h>
 
// Enable FUNCTION_TEST*() macros for enhanced debugging
{[C_TEST_DEBUG_TEST_TRACE]}
 
// Enable memory debugging
#if defined(HRN_FEATURE_MEMCONTEXT) && defined(DEBUG)
#define DEBUG_MEM
#endif
 
/***********************************************************************************************************************************
C files to be tested
 
The files are included directly so the test can see and manipulate variables and functions in the module without the need to extern.
If a .c file does not exist for a module then the header file will be included instead. They are included first so they won't see
the includes which are required for the test code.
***********************************************************************************************************************************/
{[C_INCLUDE]}
 
/***********************************************************************************************************************************
The test code is included directly so it can freely interact with the included C files
***********************************************************************************************************************************/
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
 
#ifdef HRN_FEATURE_ERROR
    #include "common/error/error.h"
    #include "common/macro.h"
#endif
 
#ifdef HRN_FEATURE_DEBUG
    #include "common/debug.h"
#endif
 
#ifdef HRN_FEATURE_STRING
    #include "common/type/string.h"
#endif
 
// Name of the project exe
#define TEST_PROJECT_EXE                                            "{[C_TEST_PROJECT_EXE]}"
 
#ifdef HRN_FEATURE_STRING
STRING_DECLARE(TEST_PROJECT_EXE_STR);
STRING_EXTERN(TEST_PROJECT_EXE_STR, TEST_PROJECT_EXE);
#endif
 
// Path where the test is running
#define TEST_PATH                                                   "{[C_TEST_PATH]}"
 
#ifdef HRN_FEATURE_STRING
STRING_DECLARE(TEST_PATH_STR);
STRING_EXTERN(TEST_PATH_STR, TEST_PATH);
#endif
 
// Path to the source repository
#define HRN_PATH_REPO                                               "{[C_HRN_PATH_REPO]}"
 
#ifdef HRN_FEATURE_STRING
STRING_DECLARE(HRN_PATH_REPO_STR);
STRING_EXTERN(HRN_PATH_REPO_STR, HRN_PATH_REPO);
#endif
 
// Path where the harness can store data without interfering with the test
#define HRN_PATH                                                    "{[C_HRN_PATH]}"
 
#ifdef HRN_FEATURE_STRING
STRING_DECLARE(HRN_PATH_STR);
STRING_EXTERN(HRN_PATH_STR, HRN_PATH);
#endif
 
// User running the test
#define TEST_USER                                                   "{[C_TEST_USER]}"
#define TEST_USER_ID                                                {[C_TEST_USER_ID]}
#define TEST_USER_ID_Z                                              "{[C_TEST_USER_ID]}"
#define TEST_USER_LEN                                               "{[C_TEST_USER_LEN]}"
 
// VM for integration testing
#define TEST_VM                                                     "{[C_TEST_VM]}"
 
// PostgreSQL version for integration testing
#define TEST_PG_VERSION                                             "{[C_TEST_PG_VERSION]}"
 
#ifdef HRN_FEATURE_STRING
STRING_DECLARE(TEST_USER_STR);
STRING_EXTERN(TEST_USER_STR, TEST_USER);
#endif
 
// Group running the test
#define TEST_GROUP                                                  "{[C_TEST_GROUP]}"
#define TEST_GROUP_ID                                               {[C_TEST_GROUP_ID]}
#define TEST_GROUP_ID_Z                                             "{[C_TEST_GROUP_ID]}"
 
#ifdef HRN_FEATURE_STRING
STRING_DECLARE(TEST_GROUP_STR);
STRING_EXTERN(TEST_GROUP_STR, TEST_GROUP);
#endif
 
// Scaling factor for performance tests
#define TEST_SCALE                                                  {[C_TEST_SCALE]}
 
// Is this test running in a container?
#define TEST_IN_CONTAINER                                           {[C_TEST_CONTAINER]}
 
// Architecture of host
#define TEST_ARCHITECTURE                                           "{[C_TEST_ARCHITECTURE]}"
 
// Path to source -- used to construct __FILENAME__ tests
#define TEST_PGB_PATH                                               "{[C_TEST_PGB_PATH]}"
 
#include "harness/debug.h"
#include "harness/test.intern.h"
 
// Is log expect testing enabled?
#define TEST_LOG_EXPECT                                             {[C_TEST_LOG_EXPECT]}
 
#ifdef HRN_FEATURE_MEMCONTEXT
#include "common/memContext.h"
#endif
 
#ifdef HRN_FEATURE_LOG
#include "harness/log.h"
#endif
 
{[C_TEST_INCLUDE]}
 
/***********************************************************************************************************************************
Includes that are not generally used by tests
***********************************************************************************************************************************/
#include <assert.h>
 
#if defined(HRN_INTEST_SOCKET) || defined(HRN_FEATURE_SOCKET)
#include "harness/server.h"
#include "common/io/socket/common.h"
#endif
 
#ifdef HRN_FEATURE_STAT
    #include "common/stat.h"
#endif
 
#ifdef HRN_IN_STACKTRACE
    #include "common/stackTrace.h"
#endif
 
/***********************************************************************************************************************************
main - run the tests
***********************************************************************************************************************************/
int
main(int argListSize, const char *argList[])
{
    // Basic sanity test on input parameters
    if (argListSize == 0 || argList[0] == NULL)
    {
        fprintf(stderr, "at least one argument expected");
        fflush(stderr);
        exit(25);
    }
 
    FUNCTION_HARNESS_BEGIN();
        FUNCTION_HARNESS_PARAM(INT, argListSize);
        FUNCTION_HARNESS_PARAM(CHARPY, argList);
    FUNCTION_HARNESS_END();
 
    int result = 0;
 
#ifdef HRN_FEATURE_ERROR
    static const ErrorHandlerFunction handlerList[] =
    {
#if defined(HRN_INTEST_STACKTRACE) || defined(HRN_FEATURE_STACKTRACE)
        stackTraceClean,
#endif
#if defined(HRN_INTEST_MEMCONTEXT) || defined(HRN_FEATURE_MEMCONTEXT)
        memContextClean,
#endif
    };
 
    errorHandlerSet(handlerList, LENGTH_OF(handlerList));
#endif
 
    // Initialize statistics
#if defined(HRN_INTEST_STAT) || defined(HRN_FEATURE_STAT)
    MEM_CONTEXT_TEMP_BEGIN()
    {
        statInit();
    }
    MEM_CONTEXT_TEMP_END();
#endif
 
    // Set neutral umask for testing
    umask(0000);
 
    // Set timezone if specified
    {[C_TEST_TZ]}
 
    // Ignore SIGPIPE and check for EPIPE errors on write() instead
    signal(SIGPIPE, SIG_IGN);
 
    // Set globals
    hrnInit(
        argList[0],                 // Test exe
        TEST_PROJECT_EXE,           // Project exe
        TEST_IN_CONTAINER,          // Is this test running in a container?
        TEST_LOG_EXPECT,            // Is log expect testing enabled?
        {[C_TEST_IDX]},             // The 0-based index of this test
        {[C_TEST_TIMING]},          // Is timing enabled (may be disabled for reproducible documentation)
        TEST_ARCHITECTURE,          // Test architecture
        TEST_PATH,                  // Path where tests write data
        TEST_USER,                  // User running the test
        TEST_VM,                    // VM for integration testing
        TEST_PG_VERSION,            // PostgreSQL version for integration testing
        HRN_PATH,                   // Path where the harness stores temp files (expect, diff, etc.)
        HRN_PATH_REPO);             // Path with a copy of the repository
 
    // Set default test log level
#ifdef HRN_FEATURE_LOG
    harnessLogLevelDefaultSet({[C_LOG_LEVEL_TEST]});
#endif
 
    // Use aggressive keep-alive settings for testing
#if defined(HRN_INTEST_SOCKET) || defined(HRN_FEATURE_SOCKET)
    sckInit(false, true, 2, 5, 5);
    hrnServerInit();
#endif
 
    // Initialize tests
    //     run, selected
    {[C_TEST_LIST]}
 
#ifdef HRN_FEATURE_ERROR
    TRY_BEGIN()
    {
        TRY_BEGIN()
        {
#endif
 
#ifdef HRN_FEATURE_MEMCONTEXT
            MEM_CONTEXT_TEMP_BEGIN()
            {
#endif
                // Run the tests
                testRun();
#ifdef HRN_FEATURE_MEMCONTEXT
            }
            MEM_CONTEXT_TEMP_END();
#endif
 
#ifdef HRN_FEATURE_ERROR
        }
        CATCH_FATAL()
        {
            // If a test was running then throw a detailed result exception
#ifdef DEBUG
            if (!errorInstanceOf(&TestError))
#endif
                hrnTestResultException();
 
            // Else rethrow the original error
            RETHROW();
        }
        TRY_END();
#endif
 
        // End test run and make sure all tests completed
        hrnComplete();
 
        printf("\nTESTS COMPLETED SUCCESSFULLY\n");
        fflush(stdout);
#ifdef HRN_FEATURE_ERROR
    }
    CATCH_FATAL()
    {
        // Make the error really obvious
        fprintf(
            stderr,
            "\nTEST FAILED WITH %s:\n\n"
                "--------------------------------------------------------------------------------\n"
                "%s\n"
                "--------------------------------------------------------------------------------\n",
            errorName(), errorMessage());
 
        // If not a TestError (which is detailed) then also print the stack trace
#ifdef DEBUG
        if (!errorInstanceOf(&TestError))
#endif
            fprintf(stderr, "\nTHROWN AT:\n%s\n", errorStackTrace());
 
        fflush(stderr);
        result = errorCode();
    }
#ifdef HRN_FEATURE_MEMCONTEXT
    FINALLY()
    {
        memContextFree(memContextTop());
    }
#endif
    TRY_END();
#endif
 
    // Switch to build path when profiling so profile data gets written to a predictable location
#if {[C_TEST_PROFILE]}
    if (chdir("{[C_TEST_PATH_BUILD]}") != 0)
    {
        fprintf(stderr, "unable to chdir to '{[C_TEST_PATH_BUILD]}'");
        fflush(stderr);
        result = 25;
    }
#endif
 
    FUNCTION_HARNESS_RETURN(INT, result);
}