diff --git a/ctdb/tests/src/protocol_basic_test.c b/ctdb/tests/src/protocol_basic_test.c index 8954035677b..4bacb47ada3 100644 --- a/ctdb/tests/src/protocol_basic_test.c +++ b/ctdb/tests/src/protocol_basic_test.c @@ -20,10 +20,8 @@ #include #include "protocol/protocol_basic.c" -#include "protocol/protocol_types.c" - -#include "tests/src/protocol_common.h" +#include "tests/src/protocol_common_basic.h" PROTOCOL_TYPE1_TEST(uint8_t, ctdb_uint8); PROTOCOL_TYPE1_TEST(uint16_t, ctdb_uint16); diff --git a/ctdb/tests/src/protocol_common.c b/ctdb/tests/src/protocol_common.c index c06272db6cc..781947d13d1 100644 --- a/ctdb/tests/src/protocol_common.c +++ b/ctdb/tests/src/protocol_common.c @@ -24,214 +24,9 @@ #include "protocol/protocol_api.h" +#include "tests/src/protocol_common_basic.h" #include "tests/src/protocol_common.h" -uint8_t BUFFER[1024*1024]; - -/* - * Functions to generation random data - */ - -int rand_int(int max) -{ - return random() % max; -} - -uint8_t rand8(void) -{ - uint8_t val = rand_int(256) & 0xff; - return val; -} - -uint16_t rand16(void) -{ - uint16_t val = rand_int(0xffff) & 0xffff; - return val; -} - -int32_t rand32i(void) -{ - return INT_MIN + random(); -} - -uint32_t rand32(void) -{ - return random(); -} - -uint64_t rand64(void) -{ - uint64_t t = random(); - t = (t << 32) | random(); - return t; -} - -double rand_double(void) -{ - return 1.0 / rand64(); -} - -void fill_buffer(void *p, size_t len) -{ - int i; - uint8_t *ptr = p; - - for (i=0; i 0) { - assert(memcmp(p1, p2, len) == 0); - } -} - -static void fill_string(char *p, size_t len) -{ - int i; - - for (i=0; itv_sec = rand32(); - p->tv_usec = rand_int(1000000); -} - -void verify_ctdb_timeval(struct timeval *p1, struct timeval *p2) -{ - assert(p1->tv_sec == p2->tv_sec); - assert(p1->tv_usec == p2->tv_usec); -} - void fill_tdb_data_nonnull(TALLOC_CTX *mem_ctx, TDB_DATA *p) { p->dsize = rand_int(1024) + 1; diff --git a/ctdb/tests/src/protocol_common.h b/ctdb/tests/src/protocol_common.h index f1b957ae46c..ec00cf97b63 100644 --- a/ctdb/tests/src/protocol_common.h +++ b/ctdb/tests/src/protocol_common.h @@ -28,143 +28,7 @@ #include "protocol/protocol.h" -/* - * Generate test routines - */ - -#define TEST_FUNC(NAME) test_ ##NAME -#define FILL_FUNC(NAME) fill_ ##NAME -#define VERIFY_FUNC(NAME) verify_ ##NAME -#define LEN_FUNC(NAME) NAME## _len -#define PUSH_FUNC(NAME) NAME## _push -#define PULL_FUNC(NAME) NAME## _pull - -/* - * Test for basic data types that do not need memory allocation - * For example - int32_t, uint32_t, uint64_t - */ -#define PROTOCOL_TYPE1_TEST(TYPE, NAME) \ -static void TEST_FUNC(NAME)(void) \ -{ \ - TYPE p1; \ - TYPE p2; \ - size_t buflen, np = 0; \ - int ret; \ -\ - FILL_FUNC(NAME)(&p1); \ - buflen = LEN_FUNC(NAME)(&p1); \ - assert(buflen < sizeof(BUFFER)); \ - PUSH_FUNC(NAME)(&p1, BUFFER, &np); \ - assert(np == buflen); \ - np = 0; \ - ret = PULL_FUNC(NAME)(BUFFER, buflen, &p2, &np); \ - assert(ret == 0); \ - assert(np == buflen); \ - VERIFY_FUNC(NAME)(&p1, &p2); \ -} - -/* - * Test for container data types that need memory allocation for sub-elements - * For example - TDB_DATA - */ -#define PROTOCOL_TYPE2_TEST(TYPE, NAME) \ -static void TEST_FUNC(NAME)(void) \ -{ \ - TALLOC_CTX *mem_ctx; \ - TYPE p1; \ - TYPE p2; \ - size_t buflen, np = 0; \ - int ret; \ -\ - mem_ctx = talloc_new(NULL); \ - assert(mem_ctx != NULL); \ - FILL_FUNC(NAME)(mem_ctx, &p1); \ - buflen = LEN_FUNC(NAME)(&p1); \ - assert(buflen < sizeof(BUFFER)); \ - PUSH_FUNC(NAME)(&p1, BUFFER, &np); \ - assert(np == buflen); \ - np = 0; \ - ret = PULL_FUNC(NAME)(BUFFER, buflen, mem_ctx, &p2, &np); \ - assert(ret == 0); \ - assert(np == buflen); \ - VERIFY_FUNC(NAME)(&p1, &p2); \ - talloc_free(mem_ctx); \ -} - -/* - * Test for derived data types that need memory allocation - * For example - most ctdb structures - */ -#define PROTOCOL_TYPE3_TEST(TYPE, NAME) \ -static void TEST_FUNC(NAME)(void) \ -{ \ - TALLOC_CTX *mem_ctx; \ - TYPE *p1, *p2; \ - size_t buflen, np = 0; \ - int ret; \ -\ - mem_ctx = talloc_new(NULL); \ - assert(mem_ctx != NULL); \ - p1 = talloc_zero(mem_ctx, TYPE); \ - assert(p1 != NULL); \ - FILL_FUNC(NAME)(p1, p1); \ - buflen = LEN_FUNC(NAME)(p1); \ - assert(buflen < sizeof(BUFFER)); \ - PUSH_FUNC(NAME)(p1, BUFFER, &np); \ - assert(np == buflen); \ - np = 0; \ - ret = PULL_FUNC(NAME)(BUFFER, buflen, mem_ctx, &p2, &np); \ - assert(ret == 0); \ - assert(np == buflen); \ - VERIFY_FUNC(NAME)(p1, p2); \ - talloc_free(mem_ctx); \ -} - -extern uint8_t BUFFER[1024*1024]; - -int rand_int(int max); -uint8_t rand8(void); -uint16_t rand16(void); -int32_t rand32i(void); -uint32_t rand32(void); -uint64_t rand64(void); -double rand_double(void); - -void fill_buffer(void *p, size_t len); -void verify_buffer(void *p1, void *p2, size_t len); - -void fill_ctdb_uint8(uint8_t *p); -void verify_ctdb_uint8(uint8_t *p1, uint8_t *p2); - -void fill_ctdb_uint16(uint16_t *p); -void verify_ctdb_uint16(uint16_t *p1, uint16_t *p2); - -void fill_ctdb_int32(int32_t *p); -void verify_ctdb_int32(int32_t *p1, int32_t *p2); - -void fill_ctdb_uint32(uint32_t *p); -void verify_ctdb_uint32(uint32_t *p1, uint32_t *p2); - -void fill_ctdb_uint64(uint64_t *p); -void verify_ctdb_uint64(uint64_t *p1, uint64_t *p2); - -void fill_ctdb_double(double *p); -void verify_ctdb_double(double *p1, double *p2); - -void fill_ctdb_bool(bool *p); -void verify_ctdb_bool(bool *p1, bool *p2); - -void fill_ctdb_string(TALLOC_CTX *mem_ctx, const char **p); -void verify_ctdb_string(const char **p1, const char **p2); - -void fill_ctdb_stringn(TALLOC_CTX *mem_ctx, const char **p); -void verify_ctdb_stringn(const char **p1, const char **p2); - -void fill_ctdb_pid(pid_t *p); -void verify_ctdb_pid(pid_t *p1, pid_t *p2); - -void fill_ctdb_timeval(struct timeval *p); -void verify_ctdb_timeval(struct timeval *p1, struct timeval *p2); +#include "tests/src/protocol_common_basic.h" void fill_tdb_data_nonnull(TALLOC_CTX *mem_ctx, TDB_DATA *p); void fill_tdb_data(TALLOC_CTX *mem_ctx, TDB_DATA *p); diff --git a/ctdb/tests/src/protocol_common_basic.c b/ctdb/tests/src/protocol_common_basic.c new file mode 100644 index 00000000000..cbc59606629 --- /dev/null +++ b/ctdb/tests/src/protocol_common_basic.c @@ -0,0 +1,231 @@ +/* + protocol tests - common functions - basic types + + Copyright (C) Amitay Isaacs 2015-2017 + + 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 + +#include "tests/src/protocol_common_basic.h" + +uint8_t BUFFER[1024*1024]; + +/* + * Functions to generation random data + */ + +int rand_int(int max) +{ + return random() % max; +} + +uint8_t rand8(void) +{ + uint8_t val = rand_int(256) & 0xff; + return val; +} + +uint16_t rand16(void) +{ + uint16_t val = rand_int(0xffff) & 0xffff; + return val; +} + +int32_t rand32i(void) +{ + return INT_MIN + random(); +} + +uint32_t rand32(void) +{ + return random(); +} + +uint64_t rand64(void) +{ + uint64_t t = random(); + t = (t << 32) | random(); + return t; +} + +double rand_double(void) +{ + return 1.0 / rand64(); +} + +void fill_buffer(void *p, size_t len) +{ + int i; + uint8_t *ptr = p; + + for (i=0; i 0) { + assert(memcmp(p1, p2, len) == 0); + } +} + +void fill_string(char *p, size_t len) +{ + int i; + + for (i=0; itv_sec = rand32(); + p->tv_usec = rand_int(1000000); +} + +void verify_ctdb_timeval(struct timeval *p1, struct timeval *p2) +{ + assert(p1->tv_sec == p2->tv_sec); + assert(p1->tv_usec == p2->tv_usec); +} + diff --git a/ctdb/tests/src/protocol_common_basic.h b/ctdb/tests/src/protocol_common_basic.h new file mode 100644 index 00000000000..0f32a0813db --- /dev/null +++ b/ctdb/tests/src/protocol_common_basic.h @@ -0,0 +1,170 @@ +/* + protocol tests - common functions - basic types + + Copyright (C) Amitay Isaacs 2015-2017 + + 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 . +*/ + +#ifndef __CTDB_PROTOCOL_COMMON_BASIC_H__ +#define __CTDB_PROTOCOL_COMMON_BASIC_H__ + +#include "replace.h" + +#include + +/* + * Generate test routines + */ + +#define TEST_FUNC(NAME) test_ ##NAME +#define FILL_FUNC(NAME) fill_ ##NAME +#define VERIFY_FUNC(NAME) verify_ ##NAME +#define LEN_FUNC(NAME) NAME## _len +#define PUSH_FUNC(NAME) NAME## _push +#define PULL_FUNC(NAME) NAME## _pull + +/* + * Test for basic data types that do not need memory allocation + * For example - int32_t, uint32_t, uint64_t + */ +#define PROTOCOL_TYPE1_TEST(TYPE, NAME) \ +static void TEST_FUNC(NAME)(void) \ +{ \ + TYPE p1; \ + TYPE p2; \ + size_t buflen, np = 0; \ + int ret; \ +\ + FILL_FUNC(NAME)(&p1); \ + buflen = LEN_FUNC(NAME)(&p1); \ + assert(buflen < sizeof(BUFFER)); \ + PUSH_FUNC(NAME)(&p1, BUFFER, &np); \ + assert(np == buflen); \ + np = 0; \ + ret = PULL_FUNC(NAME)(BUFFER, buflen, &p2, &np); \ + assert(ret == 0); \ + assert(np == buflen); \ + VERIFY_FUNC(NAME)(&p1, &p2); \ +} + +/* + * Test for container data types that need memory allocation for sub-elements + * For example - TDB_DATA + */ +#define PROTOCOL_TYPE2_TEST(TYPE, NAME) \ +static void TEST_FUNC(NAME)(void) \ +{ \ + TALLOC_CTX *mem_ctx; \ + TYPE p1; \ + TYPE p2; \ + size_t buflen, np = 0; \ + int ret; \ +\ + mem_ctx = talloc_new(NULL); \ + assert(mem_ctx != NULL); \ + FILL_FUNC(NAME)(mem_ctx, &p1); \ + buflen = LEN_FUNC(NAME)(&p1); \ + assert(buflen < sizeof(BUFFER)); \ + PUSH_FUNC(NAME)(&p1, BUFFER, &np); \ + assert(np == buflen); \ + np = 0; \ + ret = PULL_FUNC(NAME)(BUFFER, buflen, mem_ctx, &p2, &np); \ + assert(ret == 0); \ + assert(np == buflen); \ + VERIFY_FUNC(NAME)(&p1, &p2); \ + talloc_free(mem_ctx); \ +} + +/* + * Test for derived data types that need memory allocation + * For example - most ctdb structures + */ +#define PROTOCOL_TYPE3_TEST(TYPE, NAME) \ +static void TEST_FUNC(NAME)(void) \ +{ \ + TALLOC_CTX *mem_ctx; \ + TYPE *p1, *p2; \ + size_t buflen, np = 0; \ + int ret; \ +\ + mem_ctx = talloc_new(NULL); \ + assert(mem_ctx != NULL); \ + p1 = talloc_zero(mem_ctx, TYPE); \ + assert(p1 != NULL); \ + FILL_FUNC(NAME)(p1, p1); \ + buflen = LEN_FUNC(NAME)(p1); \ + assert(buflen < sizeof(BUFFER)); \ + PUSH_FUNC(NAME)(p1, BUFFER, &np); \ + assert(np == buflen); \ + np = 0; \ + ret = PULL_FUNC(NAME)(BUFFER, buflen, mem_ctx, &p2, &np); \ + assert(ret == 0); \ + assert(np == buflen); \ + VERIFY_FUNC(NAME)(p1, p2); \ + talloc_free(mem_ctx); \ +} + +extern uint8_t BUFFER[1024*1024]; + +int rand_int(int max); +uint8_t rand8(void); +uint16_t rand16(void); +int32_t rand32i(void); +uint32_t rand32(void); +uint64_t rand64(void); +double rand_double(void); + +void fill_buffer(void *p, size_t len); +void verify_buffer(void *p1, void *p2, size_t len); + +void fill_string(char *p, size_t len); +void verify_string(const char *p1, const char *p2); + +void fill_ctdb_uint8(uint8_t *p); +void verify_ctdb_uint8(uint8_t *p1, uint8_t *p2); + +void fill_ctdb_uint16(uint16_t *p); +void verify_ctdb_uint16(uint16_t *p1, uint16_t *p2); + +void fill_ctdb_int32(int32_t *p); +void verify_ctdb_int32(int32_t *p1, int32_t *p2); + +void fill_ctdb_uint32(uint32_t *p); +void verify_ctdb_uint32(uint32_t *p1, uint32_t *p2); + +void fill_ctdb_uint64(uint64_t *p); +void verify_ctdb_uint64(uint64_t *p1, uint64_t *p2); + +void fill_ctdb_double(double *p); +void verify_ctdb_double(double *p1, double *p2); + +void fill_ctdb_bool(bool *p); +void verify_ctdb_bool(bool *p1, bool *p2); + +void fill_ctdb_string(TALLOC_CTX *mem_ctx, const char **p); +void verify_ctdb_string(const char **p1, const char **p2); + +void fill_ctdb_stringn(TALLOC_CTX *mem_ctx, const char **p); +void verify_ctdb_stringn(const char **p1, const char **p2); + +void fill_ctdb_pid(pid_t *p); +void verify_ctdb_pid(pid_t *p1, pid_t *p2); + +void fill_ctdb_timeval(struct timeval *p); +void verify_ctdb_timeval(struct timeval *p1, struct timeval *p2); + +#endif /* __CTDB_PROTOCOL_COMMON_BASIC_H__ */ + + diff --git a/ctdb/wscript b/ctdb/wscript index 2edf6d0f5b2..a2ed80251b6 100644 --- a/ctdb/wscript +++ b/ctdb/wscript @@ -837,6 +837,11 @@ def build(bld): deps='samba-util ctdb-system popt', install_path='${CTDB_TEST_LIBEXECDIR}') + bld.SAMBA_SUBSYSTEM('ctdb-protocol-tests-basic', + source=bld.SUBDIR('tests/src', + 'protocol_common_basic.c'), + deps='replace talloc') + bld.SAMBA_SUBSYSTEM('ctdb-protocol-tests-common', source=bld.SUBDIR('tests/src', '''protocol_common.c @@ -844,10 +849,14 @@ def build(bld): protocol_common_event.c '''), includes='include', - deps='replace popt talloc tevent tdb') + deps='ctdb-protocol-tests-basic replace talloc tdb') + + bld.SAMBA_BINARY('protocol_basic_test', + source=bld.SUBDIR('tests/src', 'protocol_basic_test.c'), + deps='ctdb-protocol-tests-basic talloc', + install_path='${CTDB_TEST_LIBEXECDIR}') ctdb_protocol_tests = [ - 'protocol_basic_test', 'protocol_types_test', 'protocol_ctdb_test', 'protocol_event_test',