From 3845ff6349421560bdcf9ba13467b2418205cd96 Mon Sep 17 00:00:00 2001 From: Martin Schwenke Date: Mon, 23 May 2016 11:53:26 +1000 Subject: [PATCH] ctdb-tests: Add unit test for protocol utilities BUG: https://bugzilla.samba.org/show_bug.cgi?id=12470 Signed-off-by: Martin Schwenke Reviewed-by: Amitay Isaacs --- ctdb/tests/cunit/protocol_test_003.sh | 6 ++ ctdb/tests/src/protocol_util_test.c | 82 +++++++++++++++++++++++++++ ctdb/wscript | 6 ++ 3 files changed, 94 insertions(+) create mode 100755 ctdb/tests/cunit/protocol_test_003.sh create mode 100644 ctdb/tests/src/protocol_util_test.c 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 . +*/ + +#include "replace.h" +#include "system/network.h" + +#include + +#include + +#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 00a475f75cd..e19fa713ded 100644 --- a/ctdb/wscript +++ b/ctdb/wscript @@ -752,6 +752,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', -- 2.34.1