From 3128a340acb67737e57078fd9baa05ea881a31e4 Mon Sep 17 00:00:00 2001 From: leitner Date: Fri, 30 Oct 2020 14:36:05 +0000 Subject: [PATCH] make unit tests self contained --- buffer/bs_capacityassert.c | 28 ++++++++++++++++++ buffer/bs_get.c | 13 ++++++--- buffer/bs_init_bstream_size.c | 1 + buffer/bs_init_membuf.c | 7 +++++ buffer/bs_peek.c | 11 +++++++ buffer/buffer_init_staticcontents.c | 9 ++++++ buffer/buffer_put.c | 2 +- buffer/buffer_stubborn.c | 2 +- buffer/prs_asciiz.c | 1 + buffer/prs_asciiz_fixedlen.c | 1 + buffer/prs_readblob.c | 45 +++++++++++++++++++++++++++++ buffer/prs_u16.c | 7 +++++ buffer/prs_u16_big.c | 7 +++++ buffer/prs_u32.c | 7 +++++ buffer/prs_u32_big.c | 7 +++++ buffer/prs_u64.c | 7 +++++ buffer/prs_u64_big.c | 7 +++++ parse.h | 1 + 18 files changed, 157 insertions(+), 6 deletions(-) diff --git a/buffer/bs_capacityassert.c b/buffer/bs_capacityassert.c index fbb0568..1e2ca93 100644 --- a/buffer/bs_capacityassert.c +++ b/buffer/bs_capacityassert.c @@ -11,3 +11,31 @@ int bs_capacityassert(struct bytestream* bs,size_t capacity) { } return 1; } + +#ifdef UNITTEST +#include + +#undef UNITTEST +#include "buffer/bs_err.c" + +int main() { + struct bytestream bs; + // test basic functionality + bs.cur=0; bs.max=100; + assert(bs_capacityassert(&bs, 100) == 1); + assert(bs_err(&bs) == 0); + + bs.cur=50; bs.max=100; + assert(bs_capacityassert(&bs, 50) == 1); + assert(bs_err(&bs) == 0); + + assert(bs_capacityassert(&bs, 51) == 0); + assert(bs_err(&bs)); + + // try to provoke a numeric overflow + bs.cur=100; bs.max=(size_t)-1; + assert(bs_capacityassert(&bs, (size_t)-50) == 0); + assert(bs_err(&bs)); +} + +#endif diff --git a/buffer/bs_get.c b/buffer/bs_get.c index 1b9eb9a..ef29e98 100644 --- a/buffer/bs_get.c +++ b/buffer/bs_get.c @@ -38,13 +38,18 @@ unsigned char bs_get(struct bytestream* bs) { return r; } -int bs_err(struct bytestream* bs) { - return (bs->cur > bs->max); -} - #ifdef UNITTEST #include +#undef UNITTEST +#include "buffer/bs_err.c" +#include "buffer/buffer_init_staticcontents.c" +#include "buffer/bs_init_iobuf.c" +#include "buffer/bs_init_iobuf_size.c" +#include "buffer/buffer_getc.c" +#include "buffer/buffer_feed.c" +#include "buffer/buffer_stubborn2.c" + int main() { struct bytestream bs = BS_FROM_MEMBUF("fnord\nx", 6); int i; diff --git a/buffer/bs_init_bstream_size.c b/buffer/bs_init_bstream_size.c index 7033360..cf9fa8e 100644 --- a/buffer/bs_init_bstream_size.c +++ b/buffer/bs_init_bstream_size.c @@ -19,6 +19,7 @@ void bs_init_bstream_size(struct bytestream* bs,struct bytestream* other,size_t #undef UNITTEST #include "buffer/bs_get.c" +#include "buffer/bs_err.c" #include "buffer/bs_capacityassert.c" #include "buffer/bs_init_membuf.c" diff --git a/buffer/bs_init_membuf.c b/buffer/bs_init_membuf.c index e45b251..fd1566f 100644 --- a/buffer/bs_init_membuf.c +++ b/buffer/bs_init_membuf.c @@ -9,6 +9,13 @@ void bs_init_membuf(struct bytestream* bs,const unsigned char* membuf,size_t len #ifdef UNITTEST #include + +#undef UNITTEST +#include "buffer/bs_get.c" +#include "buffer/bs_err.c" + +ssize_t buffer_getc(buffer* b,char* c) { return -1; } + int main() { static struct bytestream bs; bs_init_membuf(&bs, "fnord\n", 6); diff --git a/buffer/bs_peek.c b/buffer/bs_peek.c index a0c784b..3fc633a 100644 --- a/buffer/bs_peek.c +++ b/buffer/bs_peek.c @@ -40,6 +40,17 @@ unsigned char bs_peek(struct bytestream* bs) { #ifdef UNITTEST #include +#undef UNITTEST +#include "buffer/bs_err.c" +#include "buffer/bs_get.c" +#include "buffer/buffer_init_staticcontents.c" +#include "buffer/bs_init_iobuf.c" +#include "buffer/bs_init_iobuf_size.c" +#include "buffer/buffer_peekc.c" +#include "buffer/buffer_getc.c" +#include "buffer/buffer_feed.c" +#include "buffer/buffer_stubborn2.c" + int main() { struct bytestream bs = BS_FROM_MEMBUF("fx", 1); diff --git a/buffer/buffer_init_staticcontents.c b/buffer/buffer_init_staticcontents.c index deee53e..063871e 100644 --- a/buffer/buffer_init_staticcontents.c +++ b/buffer/buffer_init_staticcontents.c @@ -16,6 +16,15 @@ void buffer_init_staticcontents(buffer* b, char* y, size_t len) { #ifdef UNITTEST #include +#undef UNITTEST +#include "byte/byte_copy.c" +#include "buffer/buffer_get.c" +#include "buffer/buffer_put.c" +#include "buffer/buffer_stubborn.c" +#include "buffer/buffer_stubborn2.c" +#include "buffer/buffer_flush.c" +#include "buffer/buffer_feed.c" + int main() { buffer b; buffer_init_staticcontents(&b, "fnord", 5); diff --git a/buffer/buffer_put.c b/buffer/buffer_put.c index 9ee9a0d..2082da5 100644 --- a/buffer/buffer_put.c +++ b/buffer/buffer_put.c @@ -5,7 +5,7 @@ #endif #include "buffer.h" -extern int buffer_stubborn(ssize_t (*op)(),int fd,const char* buf, size_t len,void* cookie); +extern ssize_t buffer_stubborn(ssize_t (*op)(),int fd,const char* buf, size_t len,void* cookie); #ifndef __unlikely #ifdef __GNUC__ diff --git a/buffer/buffer_stubborn.c b/buffer/buffer_stubborn.c index 2e00418..8008878 100644 --- a/buffer/buffer_stubborn.c +++ b/buffer/buffer_stubborn.c @@ -1,7 +1,7 @@ #include #include "buffer.h" -int buffer_stubborn(ssize_t (*op)(),int fd,const char* buf, size_t len,void* cookie) { +ssize_t buffer_stubborn(ssize_t (*op)(),int fd,const char* buf, size_t len,void* cookie) { ssize_t w; errno=0; while (len) { diff --git a/buffer/prs_asciiz.c b/buffer/prs_asciiz.c index 9dce998..687c55b 100644 --- a/buffer/prs_asciiz.c +++ b/buffer/prs_asciiz.c @@ -45,6 +45,7 @@ ssize_t prs_asciiz(struct bytestream* bs, char* dest, size_t len) { #undef UNITTEST #include "buffer/bs_init_membuf.c" #include "buffer/bs_get.c" +#include "buffer/bs_err.c" #include "buffer/bs_peek.c" // we use membuf here, mock buffer stuff away diff --git a/buffer/prs_asciiz_fixedlen.c b/buffer/prs_asciiz_fixedlen.c index cf69472..256f857 100644 --- a/buffer/prs_asciiz_fixedlen.c +++ b/buffer/prs_asciiz_fixedlen.c @@ -36,6 +36,7 @@ ssize_t prs_asciiz_fixedlen(struct bytestream* bs, char* dest, size_t len) { #undef UNITTEST #include "buffer/bs_init_membuf.c" #include "buffer/bs_get.c" +#include "buffer/bs_err.c" #include "buffer/bs_peek.c" // we use membuf here, mock buffer stuff away diff --git a/buffer/prs_readblob.c b/buffer/prs_readblob.c index 4da4ba6..99d60e9 100644 --- a/buffer/prs_readblob.c +++ b/buffer/prs_readblob.c @@ -36,3 +36,48 @@ ssize_t prs_readblob(struct bytestream* bs,unsigned char* dest,size_t destlen) { return r; } + +#ifdef UNITTEST +#include + +#undef UNITTEST +#include "buffer/bs_get.c" +#include "buffer/bs_err.c" +#include "buffer/bs_capacityassert.c" +#include "buffer/bs_init_iobuf_size.c" +#include "buffer/bs_init_membuf.c" + +#include "buffer/buffer_get.c" +#include "buffer/buffer_getc.c" +#include "buffer/buffer_feed.c" +#include "buffer/buffer_stubborn2.c" +#include "byte/byte_copy.c" +#include "open/open_read.c" + +int main() { + struct bytestream bs = BS_FROM_MEMBUF("\x34\x12", 2); + char buf[42]; + assert(prs_readblob(&bs, buf, 2) == 2); + assert(bs_err(&bs) == 0); + assert(!memcmp(buf,"\x34\x12",2)); + + assert(prs_readblob(&bs, buf, 1) == -1); + assert(bs_err(&bs)); + + // now try to provoke integer overflow + bs_init_membuf(&bs, "\x34\x12", 2); + assert(bs_get(&bs) == 0x34); + assert(prs_readblob(&bs, buf, (size_t)-1) == -1); + // a bad implementation would add 0xffffffff to the 1 we already read and wrap to 0 + // our code makes sure not to wrap and also limits the length of the + // blob to max_ssize_t (0x7fffffff). + assert(bs_err(&bs)); + + int fd = open_read("Makefile"); + assert(fd!=-1); + buffer b = BUFFER_INIT(read, fd, buf+32, 10); + bs_init_iobuf_size(&bs, &b, 100); + assert(prs_readblob(&bs, buf, 20) == 20); // make sure we can read a 20 byte blob at once even if the iobuf buffer size is smaller + // strace will show two read calls here +} +#endif diff --git a/buffer/prs_u16.c b/buffer/prs_u16.c index 547db1d..4e17e9d 100644 --- a/buffer/prs_u16.c +++ b/buffer/prs_u16.c @@ -7,6 +7,13 @@ uint16_t prs_u16(struct bytestream* bs) { #ifdef UNITTEST #include +#undef UNITTEST +#include "buffer/bs_get.c" +#include "buffer/bs_err.c" + +// mock this +ssize_t buffer_getc(buffer* b,char* c) { return 0; } + int main() { struct bytestream bs = BS_FROM_MEMBUF("\x34\x12",2); assert(prs_u16(&bs) == 0x1234); diff --git a/buffer/prs_u16_big.c b/buffer/prs_u16_big.c index 45bce8e..e02ead2 100644 --- a/buffer/prs_u16_big.c +++ b/buffer/prs_u16_big.c @@ -7,6 +7,13 @@ uint16_t prs_u16_big(struct bytestream* bs) { #ifdef UNITTEST #include +#undef UNITTEST +#include "buffer/bs_get.c" +#include "buffer/bs_err.c" + +// mock this +ssize_t buffer_getc(buffer* b,char* c) { return 0; } + int main() { struct bytestream bs = BS_FROM_MEMBUF("\x12\x34",2); assert(prs_u16_big(&bs) == 0x1234); diff --git a/buffer/prs_u32.c b/buffer/prs_u32.c index 354693c..555bb14 100644 --- a/buffer/prs_u32.c +++ b/buffer/prs_u32.c @@ -7,6 +7,13 @@ uint32_t prs_u32(struct bytestream* bs) { #ifdef UNITTEST #include +#undef UNITTEST +#include "buffer/bs_get.c" +#include "buffer/bs_err.c" + +// mock this +ssize_t buffer_getc(buffer* b,char* c) { return 0; } + int main() { struct bytestream bs = BS_FROM_MEMBUF("\x78\x56\x34\x12",4); assert(prs_u32(&bs) == 0x12345678); diff --git a/buffer/prs_u32_big.c b/buffer/prs_u32_big.c index 2aad0b3..900071e 100644 --- a/buffer/prs_u32_big.c +++ b/buffer/prs_u32_big.c @@ -7,6 +7,13 @@ uint32_t prs_u32_big(struct bytestream* bs) { #ifdef UNITTEST #include +#undef UNITTEST +#include "buffer/bs_get.c" +#include "buffer/bs_err.c" + +// mock this +ssize_t buffer_getc(buffer* b,char* c) { return 0; } + int main() { struct bytestream bs = BS_FROM_MEMBUF("\x12\x34\x56\x78",4); assert(prs_u32_big(&bs) == 0x12345678); diff --git a/buffer/prs_u64.c b/buffer/prs_u64.c index e139004..1103a25 100644 --- a/buffer/prs_u64.c +++ b/buffer/prs_u64.c @@ -11,6 +11,13 @@ uint64_t prs_u64(struct bytestream* bs) { #ifdef UNITTEST #include +#undef UNITTEST +#include "buffer/bs_get.c" +#include "buffer/bs_err.c" + +// mock this +ssize_t buffer_getc(buffer* b,char* c) { return 0; } + int main() { struct bytestream bs = BS_FROM_MEMBUF("\x78\x56\x34\x12\xef\xbe\xad\xde",8); assert(prs_u64(&bs) == 0xdeadbeef12345678); diff --git a/buffer/prs_u64_big.c b/buffer/prs_u64_big.c index 4b9edda..9d8ec9d 100644 --- a/buffer/prs_u64_big.c +++ b/buffer/prs_u64_big.c @@ -11,6 +11,13 @@ uint64_t prs_u64_big(struct bytestream* bs) { #ifdef UNITTEST #include +#undef UNITTEST +#include "buffer/bs_get.c" +#include "buffer/bs_err.c" + +// mock this +ssize_t buffer_getc(buffer* b,char* c) { return 0; } + int main() { struct bytestream bs = BS_FROM_MEMBUF("\xde\xad\xbe\xef\x12\x34\x56\x78",8); assert(prs_u64_big(&bs) == 0xdeadbeef12345678); diff --git a/parse.h b/parse.h index 3fec6fe..cc94446 100644 --- a/parse.h +++ b/parse.h @@ -84,6 +84,7 @@ unsigned char bs_peek(struct bytestream* bs); int bs_err(struct bytestream* bs); /* Can we read this much more bytes from the bytestream? */ +/* returns 1 for yes, 0 for no */ int bs_capacitycheck(struct bytestream* bs,size_t capacity); /* Like bs_capacitycheck but will set stream to error state on fail */ int bs_capacityassert(struct bytestream* bs,size_t capacity);