#include "config.h" #define LIBSSH_STATIC #include "torture.h" #define DEBUG_BUFFER #include "buffer.c" #define LIMIT (8*1024*1024) static int setup(void **state) { ssh_buffer buffer; buffer = ssh_buffer_new(); if (buffer == NULL) { return -1; } ssh_buffer_set_secure(buffer); *state = (void *) buffer; return 0; } static int teardown(void **state) { ssh_buffer_free(*state); return 0; } /* * Test if the continuously growing buffer size never exceeds 2 time its * real capacity */ static void torture_growing_buffer(void **state) { ssh_buffer buffer = *state; int i; for(i=0;iused >= 128){ if(ssh_buffer_get_len(buffer) * 2 < buffer->allocated){ assert_true(ssh_buffer_get_len(buffer) * 2 >= buffer->allocated); } } } } /* * Test if the continuously growing buffer size never exceeds 2 time its * real capacity, when we remove 1 byte after each call (sliding window) */ static void torture_growing_buffer_shifting(void **state) { ssh_buffer buffer = *state; int i; unsigned char c; for(i=0; i<1024;++i){ ssh_buffer_add_data(buffer,"S",1); } for(i=0;iused >= 128){ if(ssh_buffer_get_len(buffer) * 4 < buffer->allocated){ assert_true(ssh_buffer_get_len(buffer) * 4 >= buffer->allocated); return; } } } } /* * Test the behavior of ssh_buffer_prepend_data */ static void torture_buffer_prepend(void **state) { ssh_buffer buffer = *state; uint32_t v; ssh_buffer_add_data(buffer,"abcdef",6); ssh_buffer_prepend_data(buffer,"xyz",3); assert_int_equal(ssh_buffer_get_len(buffer),9); assert_memory_equal(ssh_buffer_get(buffer), "xyzabcdef", 9); /* Now remove 4 bytes and see if we can replace them */ ssh_buffer_get_u32(buffer,&v); assert_int_equal(ssh_buffer_get_len(buffer),5); assert_memory_equal(ssh_buffer_get(buffer), "bcdef", 5); ssh_buffer_prepend_data(buffer,"aris",4); assert_int_equal(ssh_buffer_get_len(buffer),9); assert_memory_equal(ssh_buffer_get(buffer), "arisbcdef", 9); /* same thing but we add 5 bytes now */ ssh_buffer_get_u32(buffer,&v); assert_int_equal(ssh_buffer_get_len(buffer),5); assert_memory_equal(ssh_buffer_get(buffer), "bcdef", 5); ssh_buffer_prepend_data(buffer,"12345",5); assert_int_equal(ssh_buffer_get_len(buffer),10); assert_memory_equal(ssh_buffer_get(buffer), "12345bcdef", 10); } /* * Test the behavior of ssh_buffer_get_ssh_string with invalid data */ static void torture_ssh_buffer_get_ssh_string(void **state) { ssh_buffer buffer; int i,j,k,l, rc; /* some values that can go wrong */ uint32_t values[] = {0xffffffff, 0xfffffffe, 0xfffffffc, 0xffffff00, 0x80000000, 0x80000004, 0x7fffffff}; char data[128]; (void)state; memset(data,'X',sizeof(data)); for(i=0; i < (int)(sizeof(values)/sizeof(values[0]));++i){ for(j=0; j< (int)sizeof(data);++j){ for(k=1;k<5;++k){ buffer = ssh_buffer_new(); assert_non_null(buffer); for(l=0;l