Browse Source

make unit tests self contained

master
Felix von Leitner 12 months ago
parent
commit
5ce2f8743c
  1. 28
      buffer/bs_capacityassert.c
  2. 13
      buffer/bs_get.c
  3. 1
      buffer/bs_init_bstream_size.c
  4. 7
      buffer/bs_init_membuf.c
  5. 11
      buffer/bs_peek.c
  6. 9
      buffer/buffer_init_staticcontents.c
  7. 2
      buffer/buffer_put.c
  8. 2
      buffer/buffer_stubborn.c
  9. 1
      buffer/prs_asciiz.c
  10. 1
      buffer/prs_asciiz_fixedlen.c
  11. 45
      buffer/prs_readblob.c
  12. 7
      buffer/prs_u16.c
  13. 7
      buffer/prs_u16_big.c
  14. 7
      buffer/prs_u32.c
  15. 7
      buffer/prs_u32_big.c
  16. 7
      buffer/prs_u64.c
  17. 7
      buffer/prs_u64_big.c
  18. 1
      parse.h

28
buffer/bs_capacityassert.c

@ -11,3 +11,31 @@ int bs_capacityassert(struct bytestream* bs,size_t capacity) {
}
return 1;
}
#ifdef UNITTEST
#include <assert.h>
#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

13
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 <assert.h>
#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;

1
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"

7
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 <assert.h>
#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);

11
buffer/bs_peek.c

@ -40,6 +40,17 @@ unsigned char bs_peek(struct bytestream* bs) {
#ifdef UNITTEST
#include <assert.h>
#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);

9
buffer/buffer_init_staticcontents.c

@ -16,6 +16,15 @@ void buffer_init_staticcontents(buffer* b, char* y, size_t len) {
#ifdef UNITTEST
#include <assert.h>
#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);

2
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__

2
buffer/buffer_stubborn.c

@ -1,7 +1,7 @@
#include <errno.h>
#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) {

1
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

1
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

45
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 <assert.h>
#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

7
buffer/prs_u16.c

@ -7,6 +7,13 @@ uint16_t prs_u16(struct bytestream* bs) {
#ifdef UNITTEST
#include <assert.h>
#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);

7
buffer/prs_u16_big.c

@ -7,6 +7,13 @@ uint16_t prs_u16_big(struct bytestream* bs) {
#ifdef UNITTEST
#include <assert.h>
#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);

7
buffer/prs_u32.c

@ -7,6 +7,13 @@ uint32_t prs_u32(struct bytestream* bs) {
#ifdef UNITTEST
#include <assert.h>
#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);

7
buffer/prs_u32_big.c

@ -7,6 +7,13 @@ uint32_t prs_u32_big(struct bytestream* bs) {
#ifdef UNITTEST
#include <assert.h>
#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);

7
buffer/prs_u64.c

@ -11,6 +11,13 @@ uint64_t prs_u64(struct bytestream* bs) {
#ifdef UNITTEST
#include <assert.h>
#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);

7
buffer/prs_u64_big.c

@ -11,6 +11,13 @@ uint64_t prs_u64_big(struct bytestream* bs) {
#ifdef UNITTEST
#include <assert.h>
#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);

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

Loading…
Cancel
Save