Branch data Line data Source code
1 : : /*
2 : : * Copyright (c) 2018, Citrix Systems, Inc.
3 : : *
4 : : * All rights reserved.
5 : : *
6 : : * Redistribution and use in source and binary forms, with or without
7 : : * modification, are permitted provided that the following conditions are met:
8 : : *
9 : : * 1. Redistributions of source code must retain the above copyright
10 : : * notice, this list of conditions and the following disclaimer.
11 : : * 2. Redistributions in binary form must reproduce the above copyright
12 : : * notice, this list of conditions and the following disclaimer in the
13 : : * documentation and/or other materials provided with the distribution.
14 : : * 3. Neither the name of the copyright holder nor the names of its
15 : : * contributors may be used to endorse or promote products derived from
16 : : * this software without specific prior written permission.
17 : : *
18 : : * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 : : * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 : : * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 : : * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
22 : : * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 : : * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 : : * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 : : * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 : : * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 : : * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 : : * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 : : */
30 : :
31 : : #include <stddef.h>
32 : : #include <stdarg.h>
33 : : #include <setjmp.h>
34 : : #include <cmocka.h>
35 : : #include <stdlib.h>
36 : :
37 : : #include "test-suites.h"
38 : :
39 : : #include "tapdisk-stats.h"
40 : :
41 : : #define TD_CTL_TEST_BUFSIZ ((size_t)64)
42 : :
43 : : /* Test that filling a normal stats buffer produces the
44 : : * expected output */
45 : : void
46 : 1 : test_stats_normal_buffer(void **state)
47 : : {
48 : : td_stats_t st;
49 : : char *buf;
50 : :
51 : 1 : buf = malloc(TD_CTL_TEST_BUFSIZ);
52 : 1 : assert_non_null(buf);
53 : :
54 : : tapdisk_stats_init(&st, buf, TD_CTL_TEST_BUFSIZ);
55 : :
56 : 1 : tapdisk_stats_enter(&st, '{');
57 : 1 : tapdisk_stats_val(&st, "d", 123456789);
58 : 1 : tapdisk_stats_leave(&st, '}');
59 : :
60 : 1 : assert_string_equal((char*)st.buf, "{ 123456789 }");
61 : :
62 : : /* tapdisk_stats_init() put buf into st->buf and in the general case
63 : : * it might have been realloc()'d so don't free buf here */
64 : 1 : free(st.buf);
65 : 1 : }
66 : :
67 : : /* Test that filling the stats buffer with more than the
68 : : * initial allocation correctly reallocates and extends
69 : : * the buffer
70 : : */
71 : : void
72 : 1 : test_stats_realloc_buffer(void **state)
73 : : {
74 : : td_stats_t st;
75 : : char *buf;
76 : : int i;
77 : :
78 : 1 : buf = malloc(TD_CTL_TEST_BUFSIZ);
79 : 1 : assert_non_null(buf);
80 : :
81 : : tapdisk_stats_init(&st, buf, TD_CTL_TEST_BUFSIZ);
82 : :
83 [ + + ]: 11 : for (i = 0; i < 10; i++) {
84 : 10 : tapdisk_stats_val(&st, "d", 1234567890);
85 : : }
86 : :
87 : 1 : assert_string_equal((char*)st.buf, "1234567890, 1234567890, 1234567890, "
88 : : "1234567890, 1234567890, 1234567890, 1234567890, 1234567890, "
89 : : "1234567890, 1234567890");
90 : :
91 : : /* tapdisk_stats_init() put buf into st->buf and in the general case
92 : : * it might have been realloc()'d so don't free buf here */
93 : 1 : free(st.buf);
94 : 1 : }
95 : :
96 : : /* Test that filling the stats buffer to exactly the
97 : : * initial buffer length not including the terminating
98 : : * NULL is handled correctly. Note if we change TD_CTL_TEST_BUFSIZ
99 : : * then this test has to change.
100 : : */
101 : : void
102 : 1 : test_stats_realloc_buffer_edgecase(void **state)
103 : : {
104 : : td_stats_t st;
105 : : char *buf;
106 : : int i;
107 : :
108 : 1 : buf = malloc(TD_CTL_TEST_BUFSIZ);
109 : 1 : assert_non_null(buf);
110 : :
111 : : tapdisk_stats_init(&st, buf, TD_CTL_TEST_BUFSIZ);
112 : :
113 : : /* Put in 8 characters initially */
114 : 1 : tapdisk_stats_val(&st, "d", 12345678);
115 : : /* Then 7 lots of 6 because the comma-space will pad it to 8 */
116 [ + + ]: 8 : for (i = 0; i < 7; i++) {
117 : 7 : tapdisk_stats_val(&st, "d", 123456);
118 : : }
119 : 1 : assert_string_equal((char*)st.buf, "12345678, 123456, 123456, "
120 : : "123456, 123456, 123456, 123456, 123456");
121 : :
122 : : /* tapdisk_stats_init() put buf into st->buf and in the general case
123 : : * it might have been realloc()'d so don't free buf here */
124 : 1 : free(st.buf);
125 : 1 : }
|