2 protocol types backward compatibility test
4 Copyright (C) Amitay Isaacs 2015
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.
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.
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/>.
21 #include "system/filesys.h"
25 #include "protocol/protocol_basic.c"
26 #include "protocol/protocol_types.c"
28 #include "tests/src/protocol_common.h"
30 #define COMPAT_TEST_FUNC(NAME) test_ ##NAME## _compat
31 #define OLD_LEN_FUNC(NAME) NAME## _len_old
32 #define OLD_PUSH_FUNC(NAME) NAME## _push_old
33 #define OLD_PULL_FUNC(NAME) NAME## _pull_old
35 #define COMPAT_TYPE1_TEST(TYPE, NAME) \
36 static void COMPAT_TEST_FUNC(NAME)(void) \
38 TALLOC_CTX *mem_ctx; \
39 uint8_t *buf1, *buf2; \
40 TYPE p = { 0 }, p1, p2; \
41 size_t buflen1, buflen2, np = 0; \
44 mem_ctx = talloc_new(NULL); \
45 assert(mem_ctx != NULL); \
46 FILL_FUNC(NAME)(&p); \
47 buflen1 = LEN_FUNC(NAME)(&p); \
48 buflen2 = OLD_LEN_FUNC(NAME)(&p); \
49 assert(buflen1 == buflen2); \
50 buf1 = talloc_zero_size(mem_ctx, buflen1); \
51 assert(buf1 != NULL); \
52 buf2 = talloc_zero_size(mem_ctx, buflen2); \
53 assert(buf2 != NULL); \
54 PUSH_FUNC(NAME)(&p, buf1, &np); \
55 OLD_PUSH_FUNC(NAME)(&p, buf2); \
56 assert(memcmp(buf1, buf2, buflen1) == 0); \
57 ret = PULL_FUNC(NAME)(buf1, buflen1, &p1, &np); \
59 ret = OLD_PULL_FUNC(NAME)(buf2, buflen2, &p2); \
60 VERIFY_FUNC(NAME)(&p1, &p2); \
61 talloc_free(mem_ctx); \
64 #define COMPAT_TYPE3_TEST(TYPE, NAME) \
65 static void COMPAT_TEST_FUNC(NAME)(void) \
67 TALLOC_CTX *mem_ctx; \
68 uint8_t *buf1, *buf2; \
70 size_t buflen1, buflen2, np = 0; \
73 mem_ctx = talloc_new(NULL); \
74 assert(mem_ctx != NULL); \
75 p = talloc_zero(mem_ctx, TYPE); \
77 FILL_FUNC(NAME)(p, p); \
78 buflen1 = LEN_FUNC(NAME)(p); \
79 buflen2 = OLD_LEN_FUNC(NAME)(p); \
80 assert(buflen1 == buflen2); \
81 buf1 = talloc_zero_size(mem_ctx, buflen1); \
82 assert(buf1 != NULL); \
83 buf2 = talloc_zero_size(mem_ctx, buflen2); \
84 assert(buf2 != NULL); \
85 PUSH_FUNC(NAME)(p, buf1, &np); \
86 OLD_PUSH_FUNC(NAME)(p, buf2); \
87 assert(memcmp(buf1, buf2, buflen1) == 0); \
88 ret = PULL_FUNC(NAME)(buf1, buflen1, mem_ctx, &p1, &np); \
90 ret = OLD_PULL_FUNC(NAME)(buf2, buflen2, mem_ctx, &p2); \
91 VERIFY_FUNC(NAME)(p1, p2); \
92 talloc_free(mem_ctx); \
96 static size_t ctdb_statistics_len_old(struct ctdb_statistics *in)
98 return sizeof(struct ctdb_statistics);
101 static void ctdb_statistics_push_old(struct ctdb_statistics *in, uint8_t *buf)
103 memcpy(buf, in, sizeof(struct ctdb_statistics));
106 static int ctdb_statistics_pull_old(uint8_t *buf, size_t buflen,
108 struct ctdb_statistics **out)
110 struct ctdb_statistics *val;
112 if (buflen < sizeof(struct ctdb_statistics)) {
116 val = talloc(mem_ctx, struct ctdb_statistics);
121 memcpy(val, buf, sizeof(struct ctdb_statistics));
127 struct ctdb_vnn_map_wire {
133 static size_t ctdb_vnn_map_len_old(struct ctdb_vnn_map *in)
135 return offsetof(struct ctdb_vnn_map, map) +
136 in->size * sizeof(uint32_t);
139 static void ctdb_vnn_map_push_old(struct ctdb_vnn_map *in, uint8_t *buf)
141 struct ctdb_vnn_map_wire *wire = (struct ctdb_vnn_map_wire *)buf;
143 memcpy(wire, in, offsetof(struct ctdb_vnn_map, map));
144 memcpy(wire->map, in->map, in->size * sizeof(uint32_t));
147 static int ctdb_vnn_map_pull_old(uint8_t *buf, size_t buflen,
149 struct ctdb_vnn_map **out)
151 struct ctdb_vnn_map *val;
152 struct ctdb_vnn_map_wire *wire = (struct ctdb_vnn_map_wire *)buf;
154 if (buflen < offsetof(struct ctdb_vnn_map_wire, map)) {
157 if (wire->size > buflen / sizeof(uint32_t)) {
160 if (offsetof(struct ctdb_vnn_map_wire, map) +
161 wire->size * sizeof(uint32_t) <
162 offsetof(struct ctdb_vnn_map_wire, map)) {
165 if (buflen < offsetof(struct ctdb_vnn_map_wire, map) +
166 wire->size * sizeof(uint32_t)) {
170 val = talloc(mem_ctx, struct ctdb_vnn_map);
175 memcpy(val, wire, offsetof(struct ctdb_vnn_map, map));
177 val->map = talloc_memdup(val, wire->map,
178 wire->size * sizeof(uint32_t));
179 if (val->map == NULL) {
188 struct ctdb_dbid_map_wire {
190 struct ctdb_dbid dbs[1];
193 static size_t ctdb_dbid_map_len_old(struct ctdb_dbid_map *in)
195 return sizeof(uint32_t) + in->num * sizeof(struct ctdb_dbid);
198 static void ctdb_dbid_map_push_old(struct ctdb_dbid_map *in, uint8_t *buf)
200 struct ctdb_dbid_map_wire *wire = (struct ctdb_dbid_map_wire *)buf;
203 memcpy(wire->dbs, in->dbs, in->num * sizeof(struct ctdb_dbid));
206 static int ctdb_dbid_map_pull_old(uint8_t *buf, size_t buflen,
208 struct ctdb_dbid_map **out)
210 struct ctdb_dbid_map *val;
211 struct ctdb_dbid_map_wire *wire = (struct ctdb_dbid_map_wire *)buf;
213 if (buflen < sizeof(uint32_t)) {
216 if (wire->num > buflen / sizeof(struct ctdb_dbid)) {
219 if (sizeof(uint32_t) + wire->num * sizeof(struct ctdb_dbid) <
223 if (buflen < sizeof(uint32_t) + wire->num * sizeof(struct ctdb_dbid)) {
227 val = talloc(mem_ctx, struct ctdb_dbid_map);
232 val->num = wire->num;
234 val->dbs = talloc_memdup(val, wire->dbs,
235 wire->num * sizeof(struct ctdb_dbid));
236 if (val->dbs == NULL) {
245 static size_t ctdb_pulldb_len_old(struct ctdb_pulldb *in)
247 return sizeof(struct ctdb_pulldb);
250 static void ctdb_pulldb_push_old(struct ctdb_pulldb *in, uint8_t *buf)
252 memcpy(buf, in, sizeof(struct ctdb_pulldb));
255 static int ctdb_pulldb_pull_old(uint8_t *buf, size_t buflen,
256 TALLOC_CTX *mem_ctx, struct ctdb_pulldb **out)
258 struct ctdb_pulldb *val;
260 if (buflen < sizeof(struct ctdb_pulldb)) {
264 val = talloc_memdup(mem_ctx, buf, sizeof(struct ctdb_pulldb));
274 COMPAT_TYPE3_TEST(struct ctdb_statistics, ctdb_statistics);
275 COMPAT_TYPE3_TEST(struct ctdb_vnn_map, ctdb_vnn_map);
276 COMPAT_TYPE3_TEST(struct ctdb_dbid_map, ctdb_dbid_map);
277 COMPAT_TYPE3_TEST(struct ctdb_pulldb, ctdb_pulldb);
279 int main(int argc, char *argv[])
282 int seed = atoi(argv[1]);
286 COMPAT_TEST_FUNC(ctdb_statistics)();
287 COMPAT_TEST_FUNC(ctdb_vnn_map)();
288 COMPAT_TEST_FUNC(ctdb_dbid_map)();
289 COMPAT_TEST_FUNC(ctdb_pulldb)();