ctdb-tests: Add compatibility test for protocol data types
authorAmitay Isaacs <amitay@gmail.com>
Mon, 14 Aug 2017 06:28:16 +0000 (16:28 +1000)
committerMartin Schwenke <martins@samba.org>
Wed, 30 Aug 2017 12:59:22 +0000 (14:59 +0200)
This patch prepares for testing old and new marshalling codes for
various data types to ensure backward compatibility.

Signed-off-by: Amitay Isaacs <amitay@gmail.com>
Reviewed-by: Martin Schwenke <martin@meltin.net>
ctdb/tests/cunit/protocol_test_012.sh [new file with mode: 0755]
ctdb/tests/src/protocol_types_compat_test.c [new file with mode: 0644]
ctdb/wscript

diff --git a/ctdb/tests/cunit/protocol_test_012.sh b/ctdb/tests/cunit/protocol_test_012.sh
new file mode 100755 (executable)
index 0000000..6137ac5
--- /dev/null
@@ -0,0 +1,9 @@
+#!/bin/sh
+
+. "${TEST_SCRIPTS_DIR}/unit.sh"
+
+ok_null
+
+for i in $(seq 1 1000) ; do
+    unit_test protocol_types_compat_test $i
+done
diff --git a/ctdb/tests/src/protocol_types_compat_test.c b/ctdb/tests/src/protocol_types_compat_test.c
new file mode 100644 (file)
index 0000000..c7d9665
--- /dev/null
@@ -0,0 +1,104 @@
+/*
+   protocol types backward compatibility test
+
+   Copyright (C) Amitay Isaacs  2015
+
+   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/>.
+*/
+
+#include "replace.h"
+#include "system/filesys.h"
+
+#include <assert.h>
+
+#include "protocol/protocol_basic.c"
+#include "protocol/protocol_types.c"
+
+#include "tests/src/protocol_common.h"
+
+#define COMPAT_TEST_FUNC(NAME)         test_ ##NAME## _compat
+#define OLD_LEN_FUNC(NAME)             NAME## _len_old
+#define OLD_PUSH_FUNC(NAME)            NAME## _push_old
+#define OLD_PULL_FUNC(NAME)            NAME## _pull_old
+
+#define COMPAT_TYPE1_TEST(TYPE, NAME)  \
+static void COMPAT_TEST_FUNC(NAME)(void) \
+{ \
+       TALLOC_CTX *mem_ctx; \
+       uint8_t *buf1, *buf2; \
+       TYPE p = { 0 }, p1, p2; \
+       size_t buflen1, buflen2, np = 0; \
+       int ret; \
+\
+       mem_ctx = talloc_new(NULL); \
+       assert(mem_ctx != NULL); \
+       FILL_FUNC(NAME)(&p); \
+       buflen1 = LEN_FUNC(NAME)(&p); \
+       buflen2 = OLD_LEN_FUNC(NAME)(&p); \
+       assert(buflen1 == buflen2); \
+       buf1 = talloc_zero_size(mem_ctx, buflen1); \
+       assert(buf1 != NULL); \
+       buf2 = talloc_zero_size(mem_ctx, buflen2); \
+       assert(buf2 != NULL); \
+       PUSH_FUNC(NAME)(&p, buf1, &np); \
+       OLD_PUSH_FUNC(NAME)(&p, buf2); \
+       assert(memcmp(buf1, buf2, buflen1) == 0); \
+       ret = PULL_FUNC(NAME)(buf1, buflen1, &p1, &np); \
+       assert(ret == 0); \
+       ret = OLD_PULL_FUNC(NAME)(buf2, buflen2, &p2); \
+       VERIFY_FUNC(NAME)(&p1, &p2); \
+       talloc_free(mem_ctx); \
+}
+
+#define COMPAT_TYPE3_TEST(TYPE, NAME)  \
+static void COMPAT_TEST_FUNC(NAME)(void) \
+{ \
+       TALLOC_CTX *mem_ctx; \
+       uint8_t *buf1, *buf2; \
+       TYPE *p, *p1, *p2; \
+       size_t buflen1, buflen2, np = 0; \
+       int ret; \
+\
+       mem_ctx = talloc_new(NULL); \
+       assert(mem_ctx != NULL); \
+       p = talloc_zero(mem_ctx, TYPE); \
+       assert(p != NULL); \
+       FILL_FUNC(NAME)(p, p); \
+       buflen1 = LEN_FUNC(NAME)(p); \
+       buflen2 = OLD_LEN_FUNC(NAME)(p); \
+       assert(buflen1 == buflen2); \
+       buf1 = talloc_zero_size(mem_ctx, buflen1); \
+       assert(buf1 != NULL); \
+       buf2 = talloc_zero_size(mem_ctx, buflen2); \
+       assert(buf2 != NULL); \
+       PUSH_FUNC(NAME)(p, buf1, &np); \
+       OLD_PUSH_FUNC(NAME)(p, buf2); \
+       assert(memcmp(buf1, buf2, buflen1) == 0); \
+       ret = PULL_FUNC(NAME)(buf1, buflen1, mem_ctx, &p1, &np); \
+       assert(ret == 0); \
+       ret = OLD_PULL_FUNC(NAME)(buf2, buflen2, mem_ctx, &p2); \
+       VERIFY_FUNC(NAME)(p1, p2); \
+       talloc_free(mem_ctx); \
+}
+
+
+int main(int argc, char *argv[])
+{
+       if (argc == 2) {
+               int seed = atoi(argv[1]);
+               srandom(seed);
+       }
+
+       return 0;
+}
index c23e840f4e7b7a20463243cb444d2eac2c9fb3b3..071fcb56956c180fa8f0ddeb9a262a47f0672c9e 100644 (file)
@@ -780,6 +780,7 @@ def build(bld):
             'protocol_ctdb_test',
             'protocol_event_test',
             'protocol_util_test',
+            'protocol_types_compat_test',
     ]
 
     for target in ctdb_protocol_tests: