1 /*
2 protocol tests - common functions - basic types
3
4 Copyright (C) Amitay Isaacs 2015-2017
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #ifndef __CTDB_PROTOCOL_COMMON_BASIC_H__
21 #define __CTDB_PROTOCOL_COMMON_BASIC_H__
22
23 #include "replace.h"
24
25 #include <talloc.h>
26
27 /*
28 * Generate test routines
29 */
30
31 #define TEST_FUNC(NAME) test_ ##NAME
32 #define FILL_FUNC(NAME) fill_ ##NAME
33 #define VERIFY_FUNC(NAME) verify_ ##NAME
34 #define LEN_FUNC(NAME) NAME## _len
35 #define PUSH_FUNC(NAME) NAME## _push
36 #define PULL_FUNC(NAME) NAME## _pull
37
38 /*
39 * Test for basic data types that do not need memory allocation
40 * For example - int32_t, uint32_t, uint64_t
41 */
42 #define PROTOCOL_TYPE1_TEST(TYPE, NAME) \
43 static void TEST_FUNC(NAME)(void) \
44 { \
45 TYPE p1; \
46 TYPE p2; \
47 size_t buflen, np = 0; \
48 int ret; \
49 \
50 FILL_FUNC(NAME)(&p1); \
BOOST_FUSION_FOLD_IMPL_ENABLER(T)51 buflen = LEN_FUNC(NAME)(&p1); \
52 assert(buflen < sizeof(BUFFER)); \
53 PUSH_FUNC(NAME)(&p1, BUFFER, &np); \
54 assert(np == buflen); \
55 np = 0; \
56 ret = PULL_FUNC(NAME)(BUFFER, buflen, &p2, &np); \
57 assert(ret == 0); \
58 assert(np == buflen); \
59 VERIFY_FUNC(NAME)(&p1, &p2); \
60 }
61
62 /*
63 * Test for container data types that need memory allocation for sub-elements
64 * For example - TDB_DATA
65 */
66 #define PROTOCOL_TYPE2_TEST(TYPE, NAME) \
67 static void TEST_FUNC(NAME)(void) \
68 { \
69 TALLOC_CTX *mem_ctx; \
70 TYPE p1; \
71 TYPE p2; \
72 size_t buflen, np = 0; \
73 int ret; \
74 \
75 mem_ctx = talloc_new(NULL); \
76 assert(mem_ctx != NULL); \
77 FILL_FUNC(NAME)(mem_ctx, &p1); \
BOOST_PP_CAT(result_of_it_,BOOST_FUSION_FOLD_NAME)78 buflen = LEN_FUNC(NAME)(&p1); \
79 assert(buflen < sizeof(BUFFER)); \
80 PUSH_FUNC(NAME)(&p1, BUFFER, &np); \
81 assert(np == buflen); \
82 np = 0; \
83 ret = PULL_FUNC(NAME)(BUFFER, buflen, mem_ctx, &p2, &np); \
84 assert(ret == 0); \
85 assert(np == buflen); \
86 VERIFY_FUNC(NAME)(&p1, &p2); \
87 talloc_free(mem_ctx); \
88 }
89
90 /*
91 * Test for derived data types that need memory allocation
92 * For example - most ctdb structures
93 */
94 #define PROTOCOL_TYPE3_TEST(TYPE, NAME) \
95 static void TEST_FUNC(NAME)(void) \
96 { \
97 TALLOC_CTX *mem_ctx; \
98 TYPE *p1, *p2; \
99 size_t buflen, np = 0; \
100 int ret; \
101 \
102 mem_ctx = talloc_new(NULL); \
103 assert(mem_ctx != NULL); \
104 p1 = talloc_zero(mem_ctx, TYPE); \
105 assert(p1 != NULL); \
106 FILL_FUNC(NAME)(p1, p1); \
107 buflen = LEN_FUNC(NAME)(p1); \
108 assert(buflen < sizeof(BUFFER)); \
109 PUSH_FUNC(NAME)(p1, BUFFER, &np); \
110 assert(np == buflen); \
111 np = 0; \
112 ret = PULL_FUNC(NAME)(BUFFER, buflen, mem_ctx, &p2, &np); \
113 assert(ret == 0); \
114 assert(np == buflen); \
115 VERIFY_FUNC(NAME)(p1, p2); \
116 talloc_free(mem_ctx); \
117 }
118
119 extern uint8_t BUFFER[1024*1024];
120
121 int rand_int(int max);
122 uint8_t rand8(void);
123 uint16_t rand16(void);
124 int32_t rand32i(void);
125 uint32_t rand32(void);
126 uint64_t rand64(void);
127 double rand_double(void);
128
129 void fill_buffer(void *p, size_t len);
130 void verify_buffer(void *p1, void *p2, size_t len);
131
132 void fill_string(char *p, size_t len);
133 void verify_string(const char *p1, const char *p2);
134
135 void fill_ctdb_uint8(uint8_t *p);
136 void verify_ctdb_uint8(uint8_t *p1, uint8_t *p2);
137
138 void fill_ctdb_uint16(uint16_t *p);
139 void verify_ctdb_uint16(uint16_t *p1, uint16_t *p2);
140
141 void fill_ctdb_int32(int32_t *p);
142 void verify_ctdb_int32(int32_t *p1, int32_t *p2);
143
144 void fill_ctdb_uint32(uint32_t *p);
145 void verify_ctdb_uint32(uint32_t *p1, uint32_t *p2);
146
147 void fill_ctdb_uint64(uint64_t *p);
BOOST_PP_CAT(result_of_it_,BOOST_FUSION_FOLD_NAME)148 void verify_ctdb_uint64(uint64_t *p1, uint64_t *p2);
149
150 void fill_ctdb_double(double *p);
151 void verify_ctdb_double(double *p1, double *p2);
152
153 void fill_ctdb_bool(bool *p);
154 void verify_ctdb_bool(bool *p1, bool *p2);
155
156 void fill_ctdb_string(TALLOC_CTX *mem_ctx, const char **p);
157 void verify_ctdb_string(const char **p1, const char **p2);
158
159 void fill_ctdb_stringn(TALLOC_CTX *mem_ctx, const char **p);
160 void verify_ctdb_stringn(const char **p1, const char **p2);
161
162 void fill_ctdb_pid(pid_t *p);
163 void verify_ctdb_pid(pid_t *p1, pid_t *p2);
164
165 void fill_ctdb_timeval(struct timeval *p);
166 void verify_ctdb_timeval(struct timeval *p1, struct timeval *p2);
167
168 #endif /* __CTDB_PROTOCOL_COMMON_BASIC_H__ */
169
BOOST_PP_CAT(it_,BOOST_FUSION_FOLD_NAME)170
171