Debian Codegraph @

test/lib/client.h

raw
1 /* Setup a test cowsql client. */
2
3 #include "endpoint.h"
4
5 #ifndef TEST_CLIENT_H
6 #define TEST_CLIENT_H
7
8 #define FIXTURE_CLIENT \
9 struct client_proto client; \
10 struct test_endpoint endpoint; \
11 int server
12
13 #define SETUP_CLIENT \
14 { \
15 int _rv; \
16 int _client; \
17 test_endpoint_setup(&f->endpoint, params); \
18 _rv = listen(f->endpoint.fd, 16); \
19 munit_assert_int(_rv, ==, 0); \
20 test_endpoint_pair(&f->endpoint, &f->server, &_client); \
21 memset(&f->client, 0, sizeof f->client); \
22 buffer__init(&f->client.read); \
23 buffer__init(&f->client.write); \
24 f->client.fd = _client; \
25 }
26
27 #define TEAR_DOWN_CLIENT \
28 clientClose(&f->client); \
29 test_endpoint_tear_down(&f->endpoint)
30
31 /******************************************************************************
32 *
33 * Helper macros.
34 *
35 ******************************************************************************/
36
37 /* Send the initial client handshake. */
38 #define HANDSHAKE \
39 { \
40 int rv_; \
41 rv_ = clientSendHandshake(f->client, NULL); \
42 munit_assert_int(rv_, ==, 0); \
43 }
44
45 /* Send the initial client handshake for a specific client. */
46 #define HANDSHAKE_C(CLIENT) \
47 { \
48 int rv_; \
49 rv_ = clientSendHandshake(CLIENT, NULL); \
50 munit_assert_int(rv_, ==, 0); \
51 }
52
53 /* Send an add request. */
54 #define ADD(ID, ADDRESS) \
55 { \
56 int rv_; \
57 rv_ = clientSendAdd(f->client, ID, ADDRESS, NULL); \
58 munit_assert_int(rv_, ==, 0); \
59 rv_ = clientRecvEmpty(f->client, NULL); \
60 munit_assert_int(rv_, ==, 0); \
61 }
62
63 /* Send an assign role request. */
64 #define ASSIGN(ID, ROLE) \
65 { \
66 int rv_; \
67 rv_ = clientSendAssign(f->client, ID, ROLE, NULL); \
68 munit_assert_int(rv_, ==, 0); \
69 rv_ = clientRecvEmpty(f->client, NULL); \
70 munit_assert_int(rv_, ==, 0); \
71 }
72
73 /* Send a remove request. */
74 #define REMOVE(ID) \
75 { \
76 int rv_; \
77 rv_ = clientSendRemove(f->client, ID, NULL); \
78 munit_assert_int(rv_, ==, 0); \
79 rv_ = clientRecvEmpty(f->client, NULL); \
80 munit_assert_int(rv_, ==, 0); \
81 }
82
83 /* Send a transfer request. */
84 #define TRANSFER(ID, CLIENT) \
85 { \
86 int rv_; \
87 rv_ = clientSendTransfer(CLIENT, ID, NULL); \
88 munit_assert_int(rv_, ==, 0); \
89 rv_ = clientRecvEmpty(CLIENT, NULL); \
90 munit_assert_int(rv_, ==, 0); \
91 }
92
93 /* Open a test database. */
94 #define OPEN \
95 { \
96 int rv_; \
97 rv_ = clientSendOpen(f->client, "test", NULL); \
98 munit_assert_int(rv_, ==, 0); \
99 rv_ = clientRecvDb(f->client, NULL); \
100 munit_assert_int(rv_, ==, 0); \
101 }
102
103 /* Open a test database with a specific name. */
104 #define OPEN_NAME(NAME) \
105 { \
106 int rv_; \
107 rv_ = clientSendOpen(f->client, NAME, NULL); \
108 munit_assert_int(rv_, ==, 0); \
109 rv_ = clientRecvDb(f->client, NULL); \
110 munit_assert_int(rv_, ==, 0); \
111 }
112
113 /* Prepare a statement. */
114 #define PREPARE(SQL, STMT_ID) \
115 { \
116 int rv_; \
117 rv_ = clientSendPrepare(f->client, SQL, NULL); \
118 munit_assert_int(rv_, ==, 0); \
119 rv_ = clientRecvStmt(f->client, STMT_ID, NULL, NULL, NULL); \
120 munit_assert_int(rv_, ==, 0); \
121 }
122
123 #define PREPARE_FAIL(SQL, STMT_ID, RV, MSG) \
124 { \
125 int rv_; \
126 rv_ = clientSendPrepare(f->client, SQL, NULL); \
127 munit_assert_int(rv_, ==, 0); \
128 rv_ = clientRecvFailure(f->client, RV, MSG, NULL); \
129 munit_assert_int(rv_, ==, 0); \
130 }
131
132 /* Execute a statement. */
133 #define EXEC(STMT_ID, LAST_INSERT_ID, ROWS_AFFECTED) \
134 { \
135 int rv_; \
136 rv_ = clientSendExec(f->client, STMT_ID, NULL, 0, NULL); \
137 munit_assert_int(rv_, ==, 0); \
138 rv_ = clientRecvResult(f->client, LAST_INSERT_ID, \
139 ROWS_AFFECTED, NULL); \
140 munit_assert_int(rv_, ==, 0); \
141 }
142
143 #define EXEC_SQL(SQL, LAST_INSERT_ID, ROWS_AFFECTED) \
144 { \
145 int rv_; \
146 rv_ = clientSendExecSQL(f->client, SQL, NULL, 0, NULL); \
147 munit_assert_int(rv_, ==, 0); \
148 rv_ = clientRecvResult(f->client, LAST_INSERT_ID, \
149 ROWS_AFFECTED, NULL); \
150 munit_assert_int(rv_, ==, 0); \
151 }
152
153 /* Perform a query. */
154 #define QUERY(STMT_ID, ROWS) \
155 { \
156 int rv_; \
157 rv_ = clientSendQuery(f->client, STMT_ID, NULL, 0, NULL); \
158 munit_assert_int(rv_, ==, 0); \
159 rv_ = clientRecvRows(f->client, ROWS, NULL, NULL); \
160 munit_assert_int(rv_, ==, 0); \
161 }
162
163 #define QUERY_SQL(SQL, ROWS) \
164 { \
165 int rv_; \
166 rv_ = clientSendQuerySQL(f->client, SQL, NULL, 0, NULL); \
167 munit_assert_int(rv_, ==, 0); \
168 rv_ = clientRecvRows(f->client, ROWS, NULL, NULL); \
169 munit_assert_int(rv_, ==, 0); \
170 }
171
172 #endif /* TEST_CLIENT_H */