aboutsummaryrefslogtreecommitdiff
path: root/tests/unittests/torture_buffer.c
blob: 5653c4db55b4350f22295995406277b54ba9975b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#define LIBSSH_STATIC

#include "torture.h"
#include "libssh/buffer.h"

ssh_buffer buffer;
#define LIMIT (8*1024*1024)

static void setup(void) {
    buffer=ssh_buffer_new();
}

static void teardown(void) {
  ssh_buffer_free(buffer);
}

/* test if the continuously growing buffer size never exceeds 2 time its
 * real capacity
 */
START_TEST (torture_growing_buffer)
{
  int i;
  for(i=0;i<LIMIT;++i){
    buffer_add_data(buffer,"A",1);
    if(buffer->used >= 128){
      if(buffer_get_rest_len(buffer) * 2 < buffer->allocated){
        ck_assert(buffer_get_rest_len(buffer) * 2 >= buffer->allocated);
      }
    }
  }
}
END_TEST

/* test if the continuously growing buffer size never exceeds 2 time its
 * real capacity, when we remove 1 byte after each call (sliding window)
 */
START_TEST (torture_growing_buffer_shifting)
{
  int i;
  unsigned char c;
  for(i=0; i<1024;++i){
    buffer_add_data(buffer,"S",1);
  }
  for(i=0;i<LIMIT;++i){
    buffer_get_u8(buffer,&c);
    buffer_add_data(buffer,"A",1);
    if(buffer->used >= 128){
      if(buffer_get_rest_len(buffer) * 4 < buffer->allocated){
        ck_assert(buffer_get_rest_len(buffer) * 4 >= buffer->allocated);
        return;
      }
    }
  }
}
END_TEST

/* test the behavior of buffer_prepend_data
 */
START_TEST (torture_buffer_prepend)
{
  uint32_t v;
  buffer_add_data(buffer,"abcdef",6);
  buffer_prepend_data(buffer,"xyz",3);
  ck_assert_int_eq(buffer_get_rest_len(buffer),9);
  ck_assert_int_eq(memcmp(buffer_get_rest(buffer), "xyzabcdef", 9), 0);
// Now remove 4 bytes and see if we can replace them
  buffer_get_u32(buffer,&v);
  ck_assert_int_eq(buffer_get_rest_len(buffer),5);
  ck_assert_int_eq(memcmp(buffer_get_rest(buffer), "bcdef", 5), 0);
  buffer_prepend_data(buffer,"aris",4);
  ck_assert_int_eq(buffer_get_rest_len(buffer),9);
  ck_assert_int_eq(memcmp(buffer_get_rest(buffer), "arisbcdef", 9), 0);
  /* same thing but we add 5 bytes now */
  buffer_get_u32(buffer,&v);
  ck_assert_int_eq(buffer_get_rest_len(buffer),5);
  ck_assert_int_eq(memcmp(buffer_get_rest(buffer), "bcdef", 5), 0);
  buffer_prepend_data(buffer,"12345",5);
  ck_assert_int_eq(buffer_get_rest_len(buffer),10);
  ck_assert_int_eq(memcmp(buffer_get_rest(buffer), "12345bcdef", 10), 0);

}
END_TEST


Suite *torture_make_suite(void) {
  Suite *s = suite_create("libssh_buffer");

  torture_create_case_fixture(s, "torture_growing_buffer",
            torture_growing_buffer, setup, teardown);
  torture_create_case_fixture(s, "torture_growing_buffer_shifting",
            torture_growing_buffer_shifting, setup, teardown);
  torture_create_case_fixture(s, "torture_buffer_prepend",
            torture_buffer_prepend, setup, teardown);
  return s;
}