File: test_buffer.c

package info (click to toggle)
shadowsocks-libev 3.3.6%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,192 kB
  • sloc: ansic: 15,474; sh: 3,518; python: 452; makefile: 25
file content (103 lines) | stat: -rw-r--r-- 1,784 bytes parent folder | download
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
97
98
99
100
101
102
103
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <assert.h>
#include <string.h>
#include <stdlib.h>

int verbose = 0;

#include "crypto.h"

/* Provide nonce_cache symbol needed by crypto.c */
struct cache *nonce_cache = NULL;

static void
test_balloc(void)
{
    buffer_t buf;
    memset(&buf, 0, sizeof(buf));

    int ret = balloc(&buf, 100);
    assert(ret == 0);
    (void)ret;
    assert(buf.data != NULL);
    assert(buf.capacity >= 100);
    assert(buf.len == 0);
    assert(buf.idx == 0);

    bfree(&buf);
    assert(buf.data == NULL);
    assert(buf.capacity == 0);
}

static void
test_brealloc(void)
{
    buffer_t buf;
    memset(&buf, 0, sizeof(buf));

    balloc(&buf, 50);
    buf.len = 10;

    /* Grow the buffer */
    int ret = brealloc(&buf, 10, 200);
    assert(ret == 0);
    (void)ret;
    assert(buf.capacity >= 200);
    assert(buf.len == 10);

    bfree(&buf);
}

static void
test_bprepend(void)
{
    buffer_t dst, src;
    memset(&dst, 0, sizeof(dst));
    memset(&src, 0, sizeof(src));

    balloc(&dst, 100);
    balloc(&src, 100);

    /* Put some data in src */
    memcpy(src.data, "HEADER", 6);
    src.len = 6;

    /* Put some data in dst */
    memcpy(dst.data, "BODY", 4);
    dst.len = 4;

    int ret = bprepend(&dst, &src, 200);
    assert(ret == 0);
    (void)ret;
    assert(dst.len == 10);
    assert(memcmp(dst.data, "HEADERBODY", 10) == 0);

    bfree(&dst);
    bfree(&src);
}

static void
test_balloc_zero(void)
{
    buffer_t buf;
    memset(&buf, 0, sizeof(buf));

    int ret = balloc(&buf, 0);
    assert(ret == 0);
    (void)ret;
    /* A zero-capacity buffer should still succeed */
    bfree(&buf);
}

int
main(void)
{
    test_balloc();
    test_brealloc();
    test_bprepend();
    test_balloc_zero();
    return 0;
}