summaryrefslogtreecommitdiffstats
path: root/ctdb/tests/src/protocol_common_basic.h
blob: 22a11b38de5241eb399cb4cc91b8c638bef89b6c (plain)
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
/*
   protocol tests - common functions - basic types

   Copyright (C) Amitay Isaacs  2015-2017

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, see <http://www.gnu.org/licenses/>.
*/

#ifndef __CTDB_PROTOCOL_COMMON_BASIC_H__
#define __CTDB_PROTOCOL_COMMON_BASIC_H__

#include "replace.h"

#include <talloc.h>

/*
 * Generate test routines
 */

#define TEST_FUNC(NAME)		test_ ##NAME
#define FILL_FUNC(NAME)		fill_ ##NAME
#define VERIFY_FUNC(NAME)	verify_ ##NAME
#define LEN_FUNC(NAME)		NAME## _len
#define PUSH_FUNC(NAME)		NAME## _push
#define PULL_FUNC(NAME)		NAME## _pull

/*
 * Test for basic data types that do not need memory allocation
 * For example - int32_t, uint32_t, uint64_t
 */
#define PROTOCOL_TYPE1_TEST(TYPE, NAME)	\
static void TEST_FUNC(NAME)(void) \
{ \
	TYPE p1; \
	TYPE p2; \
	size_t buflen, np = 0; \
	int ret; \
\
	FILL_FUNC(NAME)(&p1); \
	buflen = LEN_FUNC(NAME)(&p1); \
	assert(buflen < sizeof(BUFFER)); \
	PUSH_FUNC(NAME)(&p1, BUFFER, &np); \
	assert(np == buflen); \
	np = 0; \
	ret = PULL_FUNC(NAME)(BUFFER, buflen, &p2, &np); \
	assert(ret == 0); \
	assert(np == buflen); \
	VERIFY_FUNC(NAME)(&p1, &p2); \
}

/*
 * Test for container data types that need memory allocation for sub-elements
 * For example - TDB_DATA
 */
#define PROTOCOL_TYPE2_TEST(TYPE, NAME)	\
static void TEST_FUNC(NAME)(void) \
{ \
	TALLOC_CTX *mem_ctx; \
	TYPE p1; \
	TYPE p2; \
	size_t buflen, np = 0; \
	int ret; \
\
	mem_ctx = talloc_new(NULL); \
	assert(mem_ctx != NULL); \
	FILL_FUNC(NAME)(mem_ctx, &p1); \
	buflen = LEN_FUNC(NAME)(&p1); \
	assert(buflen < sizeof(BUFFER)); \
	PUSH_FUNC(NAME)(&p1, BUFFER, &np); \
	assert(np == buflen); \
	np = 0; \
	ret = PULL_FUNC(NAME)(BUFFER, buflen, mem_ctx, &p2, &np); \
	assert(ret == 0); \
	assert(np == buflen); \
	VERIFY_FUNC(NAME)(&p1, &p2); \
	talloc_free(mem_ctx); \
}

/*
 * Test for derived data types that need memory allocation
 * For example - most ctdb structures
 */
#define PROTOCOL_TYPE3_TEST(TYPE, NAME)	\
static void TEST_FUNC(NAME)(void) \
{ \
	TALLOC_CTX *mem_ctx; \
	TYPE *p1, *p2; \
	size_t buflen, np = 0; \
	int ret; \
\
	mem_ctx = talloc_new(NULL); \
	assert(mem_ctx != NULL); \
	p1 = talloc_zero(mem_ctx, TYPE); \
	assert(p1 != NULL); \
	FILL_FUNC(NAME)(p1, p1); \
	buflen = LEN_FUNC(NAME)(p1); \
	assert(buflen < sizeof(BUFFER)); \
	PUSH_FUNC(NAME)(p1, BUFFER, &np); \
	assert(np == buflen); \
	np = 0; \
	ret = PULL_FUNC(NAME)(BUFFER, buflen, mem_ctx, &p2, &np); \
	assert(ret == 0); \
	assert(np == buflen); \
	VERIFY_FUNC(NAME)(p1, p2); \
	talloc_free(mem_ctx); \
}

extern uint8_t BUFFER[1024*1024];

int rand_int(int max);
uint8_t rand8(void);
uint16_t rand16(void);
int32_t rand32i(void);
uint32_t rand32(void);
uint64_t rand64(void);
double rand_double(void);

void fill_buffer(void *p, size_t len);
void verify_buffer(void *p1, void *p2, size_t len);

void fill_string(char *p, size_t len);
void verify_string(const char *p1, const char *p2);

void fill_ctdb_uint8(uint8_t *p);
void verify_ctdb_uint8(uint8_t *p1, uint8_t *p2);

void fill_ctdb_uint16(uint16_t *p);
void verify_ctdb_uint16(uint16_t *p1, uint16_t *p2);

void fill_ctdb_int32(int32_t *p);
void verify_ctdb_int32(int32_t *p1, int32_t *p2);

void fill_ctdb_uint32(uint32_t *p);
void verify_ctdb_uint32(uint32_t *p1, uint32_t *p2);

void fill_ctdb_uint64(uint64_t *p);
void verify_ctdb_uint64(uint64_t *p1, uint64_t *p2);

void fill_ctdb_double(double *p);
void verify_ctdb_double(double *p1, double *p2);

void fill_ctdb_bool(bool *p);
void verify_ctdb_bool(bool *p1, bool *p2);

void fill_ctdb_string(TALLOC_CTX *mem_ctx, const char **p);
void verify_ctdb_string(const char **p1, const char **p2);

void fill_ctdb_stringn(TALLOC_CTX *mem_ctx, const char **p);
void verify_ctdb_stringn(const char **p1, const char **p2);

void fill_ctdb_pid(pid_t *p);
void verify_ctdb_pid(pid_t *p1, pid_t *p2);

void fill_ctdb_timeval(struct timeval *p);
void verify_ctdb_timeval(struct timeval *p1, struct timeval *p2);

void protocol_test_iterate_tag(const char *fmt, ...) PRINTF_ATTRIBUTE(1,0);
void protocol_test_iterate(int argc,
			    const char *argv[],
			   void (*test_func)(void));

#endif /* __CTDB_PROTOCOL_COMMON_BASIC_H__ */