summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Schwenke <martin@meltin.net>2016-05-23 11:53:26 +1000
committerKarolin Seeger <kseeger@samba.org>2016-12-20 12:20:16 +0100
commit94c3b8114efeffc8cd4f3ac56a3d7c12fe1c405f (patch)
treee095bb2060d1796fbf4a39e66e9bb91fbc053f69
parentedf48177b96cd04f212118a047041d0b3596673d (diff)
downloadsamba-94c3b8114efeffc8cd4f3ac56a3d7c12fe1c405f.tar.gz
ctdb-tests: Add unit test for protocol utilities
BUG: https://bugzilla.samba.org/show_bug.cgi?id=12470 Signed-off-by: Martin Schwenke <martin@meltin.net> Reviewed-by: Amitay Isaacs <amitay@gmail.com> (cherry picked from commit 3845ff6349421560bdcf9ba13467b2418205cd96)
-rwxr-xr-xctdb/tests/cunit/protocol_test_003.sh6
-rw-r--r--ctdb/tests/src/protocol_util_test.c82
-rw-r--r--ctdb/wscript6
3 files changed, 94 insertions, 0 deletions
diff --git a/ctdb/tests/cunit/protocol_test_003.sh b/ctdb/tests/cunit/protocol_test_003.sh
new file mode 100755
index 00000000000..012db9030b2
--- /dev/null
+++ b/ctdb/tests/cunit/protocol_test_003.sh
@@ -0,0 +1,6 @@
+#!/bin/sh
+
+. "${TEST_SCRIPTS_DIR}/unit.sh"
+
+ok_null
+unit_test protocol_util_test
diff --git a/ctdb/tests/src/protocol_util_test.c b/ctdb/tests/src/protocol_util_test.c
new file mode 100644
index 00000000000..752c437b573
--- /dev/null
+++ b/ctdb/tests/src/protocol_util_test.c
@@ -0,0 +1,82 @@
+/*
+ protocol utilities tests
+
+ Copyright (C) Martin Schwenke 2016
+
+ 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/network.h"
+
+#include <assert.h>
+
+#include <talloc.h>
+
+#include "protocol/protocol.h"
+#include "protocol/protocol_api.h"
+#include "common/system_util.c"
+
+/* Test parsing of IPs, conversion to string */
+static void test_sock_addr_to_string(const char *ip)
+{
+ ctdb_sock_addr sa;
+ const char *s;
+
+ assert(parse_ip(ip, NULL, 0, &sa));
+ s = ctdb_sock_addr_to_string(NULL, &sa);
+ assert(strcmp(ip, s) == 0);
+ talloc_free(discard_const(s));
+}
+
+static void test_sock_addr_cmp(const char *ip1, const char *ip2, int res)
+{
+ ctdb_sock_addr sa1, sa2;
+ int ret;
+
+ assert(parse_ip(ip1, NULL, 0, &sa1));
+ assert(parse_ip(ip2, NULL, 0, &sa2));
+ ret = ctdb_sock_addr_cmp(&sa1, &sa2);
+ if (ret < 0) {
+ ret = -1;
+ } else if (ret > 0) {
+ ret = 1;
+ }
+
+ assert(ret == res);
+}
+
+int main(int argc, char *argv[])
+{
+ test_sock_addr_to_string("0.0.0.0");
+ test_sock_addr_to_string("127.0.0.1");
+ test_sock_addr_to_string("::1");
+ test_sock_addr_to_string("192.168.2.1");
+ test_sock_addr_to_string("fe80::6af7:28ff:fefa:d136");
+
+ test_sock_addr_cmp("127.0.0.1", "127.0.0.1" , 0);
+ test_sock_addr_cmp("127.0.0.1", "127.0.0.2" , -1);
+ test_sock_addr_cmp("127.0.0.2", "127.0.0.1" , 1);
+ test_sock_addr_cmp("127.0.1.2", "127.0.2.1" , -1);
+ test_sock_addr_cmp("127.0.2.1", "127.0.1.2" , 1);
+ test_sock_addr_cmp("fe80::6af7:28ff:fefa:d136", "127.0.1.2" , 1);
+ test_sock_addr_cmp("fe80::6af7:28ff:fefa:d136",
+ "fe80::6af7:28ff:fefa:d136" , 0);
+ test_sock_addr_cmp("fe80::6af7:28ff:fefa:d136",
+ "fe80::6af7:28ff:fefa:d137" , -1);
+ test_sock_addr_cmp("fe80::6af7:28ff:fefa:d136",
+ "fe80:0000:0000:0000:6af7:28ff:fefa:d136" , 0);
+
+ return 0;
+}
diff --git a/ctdb/wscript b/ctdb/wscript
index c775cb5dc94..f61d087d5a9 100644
--- a/ctdb/wscript
+++ b/ctdb/wscript
@@ -697,6 +697,12 @@ def build(bld):
includes='include',
deps='replace popt talloc tevent tdb')
+ bld.SAMBA_BINARY('protocol_util_test',
+ source='tests/src/protocol_util_test.c',
+ deps='talloc ctdb-protocol samba-util',
+ install_path='${CTDB_TEST_LIBEXECDIR}')
+
+ # Test binaries
ctdb_tests = [
'g_lock_loop',
'message_ring',