1
0
mirror of https://github.com/samba-team/samba.git synced 2024-12-22 13:34:15 +03:00

ctdb-tests: Separate testing code for basic data types

This will be used for testing other daemons' protocol code.

Signed-off-by: Amitay Isaacs <amitay@gmail.com>
Reviewed-by: Martin Schwenke <martin@meltin.net>
This commit is contained in:
Amitay Isaacs 2018-03-05 16:45:42 +11:00 committed by Martin Schwenke
parent cbf7e2f0f2
commit 5586e035f2
6 changed files with 415 additions and 348 deletions

View File

@ -20,10 +20,8 @@
#include <assert.h>
#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);

View File

@ -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<len; i++) {
ptr[i] = rand8();
}
}
void verify_buffer(void *p1, void *p2, size_t len)
{
if (len > 0) {
assert(memcmp(p1, p2, len) == 0);
}
}
static void fill_string(char *p, size_t len)
{
int i;
for (i=0; i<len-1; i++) {
p[i] = 'A' + rand_int(26);
}
p[len-1] = '\0';
}
static void verify_string(const char *p1, const char *p2)
{
assert(strlen(p1) == strlen(p2));
assert(strcmp(p1, p2) == 0);
}
void fill_ctdb_uint8(uint8_t *p)
{
*p = rand8();
}
void verify_ctdb_uint8(uint8_t *p1, uint8_t *p2)
{
assert(*p1 == *p2);
}
void fill_ctdb_uint16(uint16_t *p)
{
*p = rand16();
}
void verify_ctdb_uint16(uint16_t *p1, uint16_t *p2)
{
assert(*p1 == *p2);
}
void fill_ctdb_int32(int32_t *p)
{
*p = rand32i();
}
void verify_ctdb_int32(int32_t *p1, int32_t *p2)
{
assert(*p1 == *p2);
}
void fill_ctdb_uint32(uint32_t *p)
{
*p = rand32();
}
void verify_ctdb_uint32(uint32_t *p1, uint32_t *p2)
{
assert(*p1 == *p2);
}
void fill_ctdb_uint64(uint64_t *p)
{
*p = rand64();
}
void verify_ctdb_uint64(uint64_t *p1, uint64_t *p2)
{
assert(*p1 == *p2);
}
void fill_ctdb_double(double *p)
{
*p = rand_double();
}
void verify_ctdb_double(double *p1, double *p2)
{
assert(*p1 == *p2);
}
void fill_ctdb_bool(bool *p)
{
if (rand_int(2) == 0) {
*p = true;
} else {
*p = false;
}
}
void verify_ctdb_bool(bool *p1, bool *p2)
{
assert(*p1 == *p2);
}
void fill_ctdb_string(TALLOC_CTX *mem_ctx, const char **p)
{
char *str;
int len;
len = rand_int(1024) + 2;
str = talloc_size(mem_ctx, len+1);
assert(str != NULL);
fill_string(str, len);
*p = str;
}
void verify_ctdb_string(const char **p1, const char **p2)
{
if (*p1 == NULL || *p2 == NULL) {
assert(*p1 == *p2);
} else {
verify_string(*p1, *p2);
}
}
void fill_ctdb_stringn(TALLOC_CTX *mem_ctx, const char **p)
{
fill_ctdb_string(mem_ctx, p);
}
void verify_ctdb_stringn(const char **p1, const char **p2)
{
verify_ctdb_string(p1, p2);
}
void fill_ctdb_pid(pid_t *p)
{
*p = rand32();
}
void verify_ctdb_pid(pid_t *p1, pid_t *p2)
{
assert(*p1 == *p2);
}
void fill_ctdb_timeval(struct timeval *p)
{
p->tv_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;

View File

@ -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);

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#include "replace.h"
#include <assert.h>
#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<len; i++) {
ptr[i] = rand8();
}
}
void verify_buffer(void *p1, void *p2, size_t len)
{
if (len > 0) {
assert(memcmp(p1, p2, len) == 0);
}
}
void fill_string(char *p, size_t len)
{
int i;
for (i=0; i<len-1; i++) {
p[i] = 'A' + rand_int(26);
}
p[len-1] = '\0';
}
void verify_string(const char *p1, const char *p2)
{
assert(strlen(p1) == strlen(p2));
assert(strcmp(p1, p2) == 0);
}
void fill_ctdb_uint8(uint8_t *p)
{
*p = rand8();
}
void verify_ctdb_uint8(uint8_t *p1, uint8_t *p2)
{
assert(*p1 == *p2);
}
void fill_ctdb_uint16(uint16_t *p)
{
*p = rand16();
}
void verify_ctdb_uint16(uint16_t *p1, uint16_t *p2)
{
assert(*p1 == *p2);
}
void fill_ctdb_int32(int32_t *p)
{
*p = rand32i();
}
void verify_ctdb_int32(int32_t *p1, int32_t *p2)
{
assert(*p1 == *p2);
}
void fill_ctdb_uint32(uint32_t *p)
{
*p = rand32();
}
void verify_ctdb_uint32(uint32_t *p1, uint32_t *p2)
{
assert(*p1 == *p2);
}
void fill_ctdb_uint64(uint64_t *p)
{
*p = rand64();
}
void verify_ctdb_uint64(uint64_t *p1, uint64_t *p2)
{
assert(*p1 == *p2);
}
void fill_ctdb_double(double *p)
{
*p = rand_double();
}
void verify_ctdb_double(double *p1, double *p2)
{
assert(*p1 == *p2);
}
void fill_ctdb_bool(bool *p)
{
if (rand_int(2) == 0) {
*p = true;
} else {
*p = false;
}
}
void verify_ctdb_bool(bool *p1, bool *p2)
{
assert(*p1 == *p2);
}
void fill_ctdb_string(TALLOC_CTX *mem_ctx, const char **p)
{
char *str;
int len;
len = rand_int(1024) + 2;
str = talloc_size(mem_ctx, len+1);
assert(str != NULL);
fill_string(str, len);
*p = str;
}
void verify_ctdb_string(const char **p1, const char **p2)
{
if (*p1 == NULL || *p2 == NULL) {
assert(*p1 == *p2);
} else {
verify_string(*p1, *p2);
}
}
void fill_ctdb_stringn(TALLOC_CTX *mem_ctx, const char **p)
{
fill_ctdb_string(mem_ctx, p);
}
void verify_ctdb_stringn(const char **p1, const char **p2)
{
verify_ctdb_string(p1, p2);
}
void fill_ctdb_pid(pid_t *p)
{
*p = rand32();
}
void verify_ctdb_pid(pid_t *p1, pid_t *p2)
{
assert(*p1 == *p2);
}
void fill_ctdb_timeval(struct timeval *p)
{
p->tv_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);
}

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#ifndef __CTDB_PROTOCOL_COMMON_BASIC_H__
#define __CTDB_PROTOCOL_COMMON_BASIC_H__
#include "replace.h"
#include <talloc.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_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__ */

View File

@ -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',