Исходный код СУБД 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
/***********************************************************************************************************************************
Execute Process
***********************************************************************************************************************************/
#include "harness/fork.h"
 
/***********************************************************************************************************************************
Test Run
***********************************************************************************************************************************/
static void
testRun(void)
{
    FUNCTION_HARNESS_VOID();
 
    // *****************************************************************************************************************************
    if (testBegin("Exec"))
    {
        Exec *exec = NULL;
 
        TEST_ASSIGN(exec, execNew(STRDEF("catt"), NULL, STRDEF("cat"), 1000), "invalid exec");
        TEST_RESULT_VOID(execOpen(exec), "open invalid exec");
        TEST_RESULT_VOID(ioWriteStrLine(execIoWrite(exec), EMPTY_STR), "write invalid exec");
        sleep(1);
        TEST_ERROR(
            ioWriteFlush(execIoWrite(exec)), ExecuteError,
            "cat terminated unexpectedly [102]: unable to execute 'catt': [2] No such file or directory");
        TEST_RESULT_VOID(execFree(exec), "free exec");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_ASSIGN(exec, execNew(STRDEF("cat"), NULL, STRDEF("cat"), 1000), "new cat exec");
        TEST_RESULT_PTR(execMemContext(exec), objMemContext(exec), "get mem context");
        TEST_RESULT_INT(execFdRead(exec), exec->fdRead, "check read file descriptor");
        TEST_RESULT_VOID(execOpen(exec), "open cat exec");
 
        const String *message = STRDEF("ACKBYACK");
        TEST_RESULT_VOID(ioWriteStrLine(execIoWrite(exec), message), "write cat exec");
        ioWriteFlush(execIoWrite(exec));
        TEST_RESULT_STR(ioReadLine(execIoRead(exec)), message, "read cat exec");
        TEST_RESULT_VOID(execFree(exec), "free exec");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_ASSIGN(exec, execNew(STRDEF("cat"), NULL, STRDEF("cat"), 1000), "new cat exec");
        TEST_RESULT_VOID(execOpen(exec), "open cat exec");
        close(exec->fdWrite);
 
        TEST_ERROR(strZ(ioReadLine(execIoRead(exec))), UnknownError, "cat terminated unexpectedly [0]");
        TEST_RESULT_VOID(execFree(exec), "free exec");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_ASSIGN(exec, execNew(STRDEF("cat"), NULL, STRDEF("cat"), 1000), "new cat exec");
        TEST_RESULT_VOID(execOpen(exec), "open cat exec");
        kill(exec->processId, SIGKILL);
 
        TEST_ERROR(strZ(ioReadLine(execIoRead(exec))), ExecuteError, "cat terminated unexpectedly on signal 9");
        TEST_RESULT_VOID(execFree(exec), "free exec");
 
        // -------------------------------------------------------------------------------------------------------------------------
        StringList *option = strLstNew();
        strLstAddZ(option, "-b");
 
        TEST_ASSIGN(exec, execNew(STRDEF("cat"), option, STRDEF("cat"), 1000), "new cat exec");
        TEST_RESULT_VOID(execOpen(exec), "open cat exec");
 
        TEST_RESULT_VOID(ioWriteStrLine(execIoWrite(exec), message), "write cat exec");
        ioWriteFlush(execIoWrite(exec));
        TEST_RESULT_STR_Z(ioReadLine(execIoRead(exec)), "     1\tACKBYACK", "read cat exec");
        TEST_RESULT_VOID(execFree(exec), "free exec");
 
        // Run the same test as above but close all file descriptors first to ensure we don't accidentally close a required
        // descriptor while running dup2()/close() between the fork() and the exec().
        // -------------------------------------------------------------------------------------------------------------------------
        HRN_FORK_BEGIN()
        {
            HRN_FORK_CHILD_BEGIN()
            {
                // This is not really fd max but for the purposes of testing is fine -- we won't have more than 64 fds open
                for (int fd = 0; fd < 64; fd++)
                    close(fd);
 
                StringList *option = strLstNew();
                strLstAddZ(option, "-b");
 
                TEST_ASSIGN(exec, execNew(STRDEF("cat"), option, STRDEF("cat"), 1000), "new cat exec");
                TEST_RESULT_VOID(execOpen(exec), "open cat exec");
 
                TEST_RESULT_VOID(ioWriteStrLine(execIoWrite(exec), message), "write cat exec");
                ioWriteFlush(execIoWrite(exec));
                TEST_RESULT_STR_Z(ioReadLine(execIoRead(exec)), "     1\tACKBYACK", "read cat exec");
                TEST_RESULT_VOID(execFree(exec), "free exec");
            }
            HRN_FORK_CHILD_END();
        }
        HRN_FORK_END();
 
        // -------------------------------------------------------------------------------------------------------------------------
        option = strLstNew();
        strLstAddZ(option, "2");
 
        TEST_ASSIGN(exec, execNew(STRDEF("sleep"), option, STRDEF("sleep"), 1500), "new sleep exec");
        TEST_RESULT_VOID(execOpen(exec), "open cat exec");
 
        TEST_ERROR(execFreeResource(exec), ExecuteError, "sleep did not exit when expected");
 
        TEST_ERROR(ioReadLine(execIoRead(exec)), FileReadError, "unable to read from sleep read: [9] Bad file descriptor");
        ioWriteStrLine(execIoWrite(exec), strNew());
        TEST_ERROR(
            ioWriteFlush(execIoWrite(exec)), FileWriteError,
            "unable to finish write to sleep write (wrote 0/1 bytes): [9] Bad file descriptor");
 
        sleepMSec(500);
        TEST_RESULT_VOID(execFree(exec), "sleep exited as expected");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("exec one with output");
 
        TEST_RESULT_STR_Z(execOne(strLstNewSplitZ(STRDEF("echo output"), " ")), "output", "exec echo");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("exec one exits with error");
 
        StringList *command = strLstNew();
        strLstAddZ(command, "sh");
        strLstAddZ(command, "-c");
        strLstAddZ(command, "echo error; exit 1");
 
        TEST_ERROR(execOne(command), UnknownError, "sh terminated unexpectedly [1]: error");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("exec one exits from signal");
 
        command = strLstNew();
        strLstAddZ(command, "sh");
        strLstAddZ(command, "-c");
        strLstAddZ(command, "kill -9 $$");
 
        TEST_ERROR(execOne(command), ExecuteError, "sh terminated unexpectedly on signal 9");
    }
 
    // *****************************************************************************************************************************
    if (testBegin("hrnExecOneExpectP()"))
    {
        Exec *exec = NULL;
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("exec without output");
 
        TEST_RESULT_STR_Z(hrnExecOneExpectP(STRDEF("ls " TEST_PATH)), "", "exec ls");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("touch file");
 
        TEST_RESULT_STR_Z(hrnExecOneExpectP(STRDEF("touch " TEST_PATH "/file")), "", "exec touch");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("exec with custom shell and output");
 
        TEST_RESULT_STR_Z(hrnExecOneExpectP(STRDEF("ls " TEST_PATH), .shell = STRDEF("sh -c")), "file\n", "exec ls");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("exec with custom timeout");
 
        TEST_RESULT_STR_Z(hrnExecOneExpectP(STRDEF("echo timeout"), .timeout = 60000), "timeout\n", "exec with timeout");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("exec exits from signal");
 
        TEST_ASSIGN(exec, execNew(STRDEF("cat"), NULL, STRDEF("cat"), 1000), "new cat exec");
        TEST_RESULT_VOID(execOpen(exec), "open cat exec");
        kill(exec->processId, SIGKILL);
 
        TEST_ERROR(execProcess(exec, (HrnExecOneExpectParam){0}), ExecuteError, "cat terminated unexpectedly on signal 9");
        TEST_RESULT_VOID(execFree(exec), "free exec");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("exec exits with error");
 
        TEST_ERROR_MULTI(
            hrnExecOneExpectP(STRDEF("cat missing.txt")), UnknownError,
            // glibc
            "cat missing.txt terminated unexpectedly [1]: cat: missing.txt: No such file or directory",
            // musl libc
            "cat missing.txt terminated unexpectedly [1]: cat: can't open 'missing.txt': No such file or directory");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("exec ignores error");
 
        TRY_BEGIN()
        {
            TEST_RESULT_STR_Z(
                hrnExecOneExpectP(STRDEF("cat missing.txt"), .resultExpect = 1), "cat: missing.txt: No such file or directory\n",
                "ignore error");
        }
        CATCH_ANY()
        {
            hrnTestResultEnd();
 
            TEST_RESULT_STR_Z(
                hrnExecOneExpectP(STRDEF("cat missing.txt"), .resultExpect = 1),
                "cat: can't open 'missing.txt': No such file or directory\n", "ignore error");
        }
        TRY_END();
    }
 
    FUNCTION_HARNESS_RETURN_VOID();
}