From 8cdd02877feaa61587366f8cd42403a9c23e4771 Mon Sep 17 00:00:00 2001 From: Victor Julien Date: Sun, 27 Jun 2010 10:41:37 +0200 Subject: [PATCH] Add unittests for ringbuffer. --- src/suricata.c | 3 + src/util-ringbuffer.c | 287 ++++++++++++++++++++++++++++++++++++++++++ src/util-ringbuffer.h | 2 + 3 files changed, 292 insertions(+) diff --git a/src/suricata.c b/src/suricata.c index 9386393e7a..1e1f7c1919 100644 --- a/src/suricata.c +++ b/src/suricata.c @@ -139,6 +139,8 @@ #include "tmqh-packetpool.h" +#include "util-ringbuffer.h" + /* * we put this here, because we only use it here in main. */ @@ -886,6 +888,7 @@ int main(int argc, char **argv) SCProfilingRegisterTests(); #endif DeStateRegisterTests(); + DetectRingBufferRegisterTests(); if (list_unittests) { UtListTests(regex_arg); } diff --git a/src/util-ringbuffer.c b/src/util-ringbuffer.c index 54d4b48cb0..4df1adc274 100644 --- a/src/util-ringbuffer.c +++ b/src/util-ringbuffer.c @@ -37,6 +37,7 @@ #include "suricata.h" #include "util-ringbuffer.h" #include "util-atomic.h" +#include "util-unittest.h" #define USLEEP_TIME 5 @@ -99,6 +100,36 @@ void RingBuffer8Shutdown(RingBuffer8 *rb) { #endif } +/** \brief check the ringbuffer is empty (no data in it) + * + * \param rb ringbuffer + * + * \retval 1 empty + * \retval 0 not empty + */ +int RingBuffer8IsEmpty(RingBuffer8 *rb) { + if (SC_ATOMIC_GET(rb->write) == SC_ATOMIC_GET(rb->read)) { + return 1; + } + + return 0; +} + +/** \brief check the ringbuffer is full (no more data will fit) + * + * \param rb ringbuffer + * + * \retval 1 empty + * \retval 0 not empty + */ +int RingBuffer8IsFull(RingBuffer8 *rb) { + if ((unsigned char)(SC_ATOMIC_GET(rb->write) + 1) == SC_ATOMIC_GET(rb->read)) { + return 1; + } + + return 0; +} + /** \brief tell the ringbuffer to shut down * * \param rb ringbuffer @@ -764,3 +795,259 @@ retry: return 0; } +#ifdef UNITTESTS +static int RingBuffer8SrSwInit01 (void) { + int result = 0; + + RingBuffer8 *rb = NULL; + + rb = RingBuffer8Init(); + if (rb == NULL) { + printf("rb == NULL: "); + goto end; + } + + int r = SCSpinLock(&rb->spin); + if (r != 0) { + printf("r = %d, expected %d: ", r, 0); + goto end; + } + SCSpinUnlock(&rb->spin); + + if (SC_ATOMIC_GET(rb->read) != 0) { + printf("read %u, expected 0: ", SC_ATOMIC_GET(rb->read)); + goto end; + } + + if (SC_ATOMIC_GET(rb->write) != 0) { + printf("write %u, expected 0: ", SC_ATOMIC_GET(rb->write)); + goto end; + } + + result = 1; +end: + if (rb != NULL) { + RingBuffer8Destroy(rb); + } + return result; +} + +static int RingBuffer8SrSwPut01 (void) { + int result = 0; + + RingBuffer8 *rb = NULL; + + rb = RingBuffer8Init(); + if (rb == NULL) { + printf("rb == NULL: "); + goto end; + } + + if (SC_ATOMIC_GET(rb->read) != 0) { + printf("read %u, expected 0: ", SC_ATOMIC_GET(rb->read)); + goto end; + } + + if (SC_ATOMIC_GET(rb->write) != 0) { + printf("write %u, expected 0: ", SC_ATOMIC_GET(rb->write)); + goto end; + } + + void *ptr = &result; + + RingBufferSrSw8Put(rb, ptr); + + if (SC_ATOMIC_GET(rb->read) != 0) { + printf("read %u, expected 0: ", SC_ATOMIC_GET(rb->read)); + goto end; + } + + if (SC_ATOMIC_GET(rb->write) != 1) { + printf("write %u, expected 1: ", SC_ATOMIC_GET(rb->write)); + goto end; + } + + if (rb->array[0] != ptr) { + printf("ptr is %p, expected %p: ", rb->array[0], ptr); + goto end; + } + + result = 1; +end: + if (rb != NULL) { + RingBuffer8Destroy(rb); + } + return result; +} + +static int RingBuffer8SrSwPut02 (void) { + int result = 0; + RingBuffer8 *rb = NULL; + + int array[255]; + int cnt = 0; + for (cnt = 0; cnt < 255; cnt++) { + array[cnt] = cnt; + } + + rb = RingBuffer8Init(); + if (rb == NULL) { + printf("rb == NULL: "); + goto end; + } + + for (cnt = 0; cnt < 255; cnt++) { + RingBufferSrSw8Put(rb, (void *)&array[cnt]); + + if (SC_ATOMIC_GET(rb->read) != 0) { + printf("read %u, expected 0: ", SC_ATOMIC_GET(rb->read)); + goto end; + } + + if (SC_ATOMIC_GET(rb->write) != (unsigned char)(cnt+1)) { + printf("write %u, expected %u: ", SC_ATOMIC_GET(rb->write), (unsigned char)(cnt+1)); + goto end; + } + + if (rb->array[cnt] != (void *)&array[cnt]) { + printf("ptr is %p, expected %p: ", rb->array[cnt], (void *)&array[cnt]); + goto end; + } + } + + if (!(RingBuffer8IsFull(rb))) { + printf("ringbuffer should be full, isn't: "); + goto end; + } + + result = 1; +end: + if (rb != NULL) { + RingBuffer8Destroy(rb); + } + return result; +} + +static int RingBuffer8SrSwGet01 (void) { + int result = 0; + + RingBuffer8 *rb = NULL; + + rb = RingBuffer8Init(); + if (rb == NULL) { + printf("rb == NULL: "); + goto end; + } + + void *ptr = &result; + + RingBufferSrSw8Put(rb, ptr); + void *ptr2 = RingBufferSrSw8Get(rb); + + if (ptr != ptr2) { + printf("ptr %p != ptr2 %p: ", ptr, ptr2); + goto end; + } + + if (SC_ATOMIC_GET(rb->read) != 1) { + printf("read %u, expected 1: ", SC_ATOMIC_GET(rb->read)); + goto end; + } + + if (SC_ATOMIC_GET(rb->write) != 1) { + printf("write %u, expected 1: ", SC_ATOMIC_GET(rb->write)); + goto end; + } + + result = 1; +end: + if (rb != NULL) { + RingBuffer8Destroy(rb); + } + return result; +} + +static int RingBuffer8SrSwGet02 (void) { + int result = 0; + RingBuffer8 *rb = NULL; + + int array[255]; + int cnt = 0; + for (cnt = 0; cnt < 255; cnt++) { + array[cnt] = cnt; + } + + rb = RingBuffer8Init(); + if (rb == NULL) { + printf("rb == NULL: "); + goto end; + } + + for (cnt = 0; cnt < 255; cnt++) { + RingBufferSrSw8Put(rb, (void *)&array[cnt]); + + if (SC_ATOMIC_GET(rb->read) != 0) { + printf("read %u, expected 0: ", SC_ATOMIC_GET(rb->read)); + goto end; + } + + if (SC_ATOMIC_GET(rb->write) != (unsigned char)(cnt+1)) { + printf("write %u, expected %u: ", SC_ATOMIC_GET(rb->write), (unsigned char)(cnt+1)); + goto end; + } + + if (rb->array[cnt] != (void *)&array[cnt]) { + printf("ptr is %p, expected %p: ", rb->array[cnt], (void *)&array[cnt]); + goto end; + } + } + + if (!(RingBuffer8IsFull(rb))) { + printf("ringbuffer should be full, isn't: "); + goto end; + } + + for (cnt = 0; cnt < 255; cnt++) { + void *ptr = RingBufferSrSw8Get(rb); + + if (SC_ATOMIC_GET(rb->read) != (unsigned char)(cnt+1)) { + printf("read %u, expected %u: ", SC_ATOMIC_GET(rb->read), (unsigned char)(cnt+1)); + goto end; + } + + if (SC_ATOMIC_GET(rb->write) != 255) { + printf("write %u, expected %u: ", SC_ATOMIC_GET(rb->write), 255); + goto end; + } + + if (ptr != (void *)&array[cnt]) { + printf("ptr is %p, expected %p: ", ptr, (void *)&array[cnt]); + goto end; + } + } + + if (!(RingBuffer8IsEmpty(rb))) { + printf("ringbuffer should be empty, isn't: "); + goto end; + } + + result = 1; +end: + if (rb != NULL) { + RingBuffer8Destroy(rb); + } + return result; +} + +#endif /* UNITTESTS */ + +void DetectRingBufferRegisterTests(void) { +#ifdef UNITTESTS /* UNITTESTS */ + UtRegisterTest("RingBuffer8SrSwInit01", RingBuffer8SrSwInit01, 1); + UtRegisterTest("RingBuffer8SrSwPut01", RingBuffer8SrSwPut01, 1); + UtRegisterTest("RingBuffer8SrSwPut02", RingBuffer8SrSwPut02, 1); + UtRegisterTest("RingBuffer8SrSwGet01", RingBuffer8SrSwGet01, 1); + UtRegisterTest("RingBuffer8SrSwGet02", RingBuffer8SrSwGet02, 1); +#endif /* UNITTESTS */ +} + diff --git a/src/util-ringbuffer.h b/src/util-ringbuffer.h index 4de93edb27..ad7c0c31d0 100644 --- a/src/util-ringbuffer.h +++ b/src/util-ringbuffer.h @@ -130,5 +130,7 @@ int RingBufferMrMwPut(RingBuffer16 *, void *); void *RingBufferSrMw8Get(RingBuffer8 *); int RingBufferSrMw8Put(RingBuffer8 *, void *); +void DetectRingBufferRegisterTests(void); + #endif /* __UTIL_RINGBUFFER_H__ */