File: client.h

package info (click to toggle)
cowsql 1.15.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,704 kB
  • sloc: ansic: 23,583; makefile: 137; python: 11
file content (172 lines) | stat: -rw-r--r-- 7,264 bytes parent folder | download | duplicates (3)
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
/* Setup a test cowsql client. */

#include "endpoint.h"

#ifndef TEST_CLIENT_H
#define TEST_CLIENT_H

#define FIXTURE_CLIENT                 \
	struct client_proto client;    \
	struct test_endpoint endpoint; \
	int server

#define SETUP_CLIENT                                                    \
	{                                                               \
		int _rv;                                                \
		int _client;                                            \
		test_endpoint_setup(&f->endpoint, params);              \
		_rv = listen(f->endpoint.fd, 16);                       \
		munit_assert_int(_rv, ==, 0);                           \
		test_endpoint_pair(&f->endpoint, &f->server, &_client); \
		memset(&f->client, 0, sizeof f->client);                \
		buffer__init(&f->client.read);                          \
		buffer__init(&f->client.write);                         \
		f->client.fd = _client;                                 \
	}

#define TEAR_DOWN_CLIENT         \
	clientClose(&f->client); \
	test_endpoint_tear_down(&f->endpoint)

/******************************************************************************
 *
 * Helper macros.
 *
 ******************************************************************************/

/* Send the initial client handshake. */
#define HANDSHAKE                                           \
	{                                                   \
		int rv_;                                    \
		rv_ = clientSendHandshake(f->client, NULL); \
		munit_assert_int(rv_, ==, 0);               \
	}

/* Send the initial client handshake for a specific client. */
#define HANDSHAKE_C(CLIENT)                              \
	{                                                \
		int rv_;                                 \
		rv_ = clientSendHandshake(CLIENT, NULL); \
		munit_assert_int(rv_, ==, 0);            \
	}

/* Send an add request. */
#define ADD(ID, ADDRESS)                                           \
	{                                                          \
		int rv_;                                           \
		rv_ = clientSendAdd(f->client, ID, ADDRESS, NULL); \
		munit_assert_int(rv_, ==, 0);                      \
		rv_ = clientRecvEmpty(f->client, NULL);            \
		munit_assert_int(rv_, ==, 0);                      \
	}

/* Send an assign role request. */
#define ASSIGN(ID, ROLE)                                           \
	{                                                          \
		int rv_;                                           \
		rv_ = clientSendAssign(f->client, ID, ROLE, NULL); \
		munit_assert_int(rv_, ==, 0);                      \
		rv_ = clientRecvEmpty(f->client, NULL);            \
		munit_assert_int(rv_, ==, 0);                      \
	}

/* Send a remove request. */
#define REMOVE(ID)                                           \
	{                                                    \
		int rv_;                                     \
		rv_ = clientSendRemove(f->client, ID, NULL); \
		munit_assert_int(rv_, ==, 0);                \
		rv_ = clientRecvEmpty(f->client, NULL);      \
		munit_assert_int(rv_, ==, 0);                \
	}

/* Send a transfer request. */
#define TRANSFER(ID, CLIENT)                                \
	{                                                   \
		int rv_;                                    \
		rv_ = clientSendTransfer(CLIENT, ID, NULL); \
		munit_assert_int(rv_, ==, 0);               \
		rv_ = clientRecvEmpty(CLIENT, NULL);        \
		munit_assert_int(rv_, ==, 0);               \
	}

/* Open a test database. */
#define OPEN                                                   \
	{                                                      \
		int rv_;                                       \
		rv_ = clientSendOpen(f->client, "test", NULL); \
		munit_assert_int(rv_, ==, 0);                  \
		rv_ = clientRecvDb(f->client, NULL);           \
		munit_assert_int(rv_, ==, 0);                  \
	}

/* Open a test database with a specific name. */
#define OPEN_NAME(NAME)                                      \
	{                                                    \
		int rv_;                                     \
		rv_ = clientSendOpen(f->client, NAME, NULL); \
		munit_assert_int(rv_, ==, 0);                \
		rv_ = clientRecvDb(f->client, NULL);         \
		munit_assert_int(rv_, ==, 0);                \
	}

/* Prepare a statement. */
#define PREPARE(SQL, STMT_ID)                                               \
	{                                                                   \
		int rv_;                                                    \
		rv_ = clientSendPrepare(f->client, SQL, NULL);              \
		munit_assert_int(rv_, ==, 0);                               \
		rv_ = clientRecvStmt(f->client, STMT_ID, NULL, NULL, NULL); \
		munit_assert_int(rv_, ==, 0);                               \
	}

#define PREPARE_FAIL(SQL, STMT_ID, RV, MSG)                        \
	{                                                          \
		int rv_;                                           \
		rv_ = clientSendPrepare(f->client, SQL, NULL);     \
		munit_assert_int(rv_, ==, 0);                      \
		rv_ = clientRecvFailure(f->client, RV, MSG, NULL); \
		munit_assert_int(rv_, ==, 0);                      \
	}

/* Execute a statement. */
#define EXEC(STMT_ID, LAST_INSERT_ID, ROWS_AFFECTED)                     \
	{                                                                \
		int rv_;                                                 \
		rv_ = clientSendExec(f->client, STMT_ID, NULL, 0, NULL); \
		munit_assert_int(rv_, ==, 0);                            \
		rv_ = clientRecvResult(f->client, LAST_INSERT_ID,        \
				       ROWS_AFFECTED, NULL);             \
		munit_assert_int(rv_, ==, 0);                            \
	}

#define EXEC_SQL(SQL, LAST_INSERT_ID, ROWS_AFFECTED)                    \
	{                                                               \
		int rv_;                                                \
		rv_ = clientSendExecSQL(f->client, SQL, NULL, 0, NULL); \
		munit_assert_int(rv_, ==, 0);                           \
		rv_ = clientRecvResult(f->client, LAST_INSERT_ID,       \
				       ROWS_AFFECTED, NULL);            \
		munit_assert_int(rv_, ==, 0);                           \
	}

/* Perform a query. */
#define QUERY(STMT_ID, ROWS)                                              \
	{                                                                 \
		int rv_;                                                  \
		rv_ = clientSendQuery(f->client, STMT_ID, NULL, 0, NULL); \
		munit_assert_int(rv_, ==, 0);                             \
		rv_ = clientRecvRows(f->client, ROWS, NULL, NULL);        \
		munit_assert_int(rv_, ==, 0);                             \
	}

#define QUERY_SQL(SQL, ROWS)                                             \
	{                                                                \
		int rv_;                                                 \
		rv_ = clientSendQuerySQL(f->client, SQL, NULL, 0, NULL); \
		munit_assert_int(rv_, ==, 0);                            \
		rv_ = clientRecvRows(f->client, ROWS, NULL, NULL);       \
		munit_assert_int(rv_, ==, 0);                            \
	}

#endif /* TEST_CLIENT_H */