1/* BEGIN_HEADER */
2#include "mbedtls/memory_buffer_alloc.h"
3#define TEST_SUITE_MEMORY_BUFFER_ALLOC
4
5/* END_HEADER */
6
7/* BEGIN_DEPENDENCIES
8 * depends_on:MBEDTLS_MEMORY_BUFFER_ALLOC_C
9 * END_DEPENDENCIES
10 */
11
12/* BEGIN_SUITE_HELPERS */
13static int check_pointer( void *p )
14{
15    if( p == NULL )
16        return( -1 );
17
18    if( (size_t) p % MBEDTLS_MEMORY_ALIGN_MULTIPLE != 0 )
19        return( -1 );
20
21    return( 0 );
22}
23/* END_SUITE_HELPERS */
24
25/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
26void mbedtls_memory_buffer_alloc_self_test(  )
27{
28    TEST_ASSERT( mbedtls_memory_buffer_alloc_self_test( 1 ) == 0 );
29}
30/* END_CASE */
31
32/* BEGIN_CASE depends_on:MBEDTLS_MEMORY_DEBUG */
33void memory_buffer_alloc_free_alloc( int a_bytes, int b_bytes, int c_bytes,
34                                     int d_bytes, int free_a, int free_b,
35                                     int free_c, int free_d, int e_bytes,
36                                     int f_bytes )
37{
38    unsigned char buf[1024];
39    unsigned char *ptr_a = NULL, *ptr_b = NULL, *ptr_c = NULL, *ptr_d = NULL,
40                    *ptr_e = NULL, *ptr_f = NULL;
41
42    size_t reported_blocks;
43    size_t allocated_bytes = 0, reported_bytes;
44
45    mbedtls_memory_buffer_alloc_init( buf, sizeof( buf ) );
46
47    mbedtls_memory_buffer_set_verify( MBEDTLS_MEMORY_VERIFY_ALWAYS );
48
49    if( a_bytes > 0 )
50    {
51        ptr_a = mbedtls_calloc( a_bytes, sizeof(char) );
52        TEST_ASSERT( check_pointer( ptr_a ) == 0 );
53
54        allocated_bytes += a_bytes * sizeof(char);
55    }
56
57    if( b_bytes > 0 )
58    {
59        ptr_b = mbedtls_calloc( b_bytes, sizeof(char) );
60        TEST_ASSERT( check_pointer( ptr_b ) == 0 );
61
62        allocated_bytes += b_bytes * sizeof(char);
63    }
64
65    if( c_bytes > 0 )
66    {
67        ptr_c = mbedtls_calloc( c_bytes, sizeof(char) );
68        TEST_ASSERT( check_pointer( ptr_c ) == 0 );
69
70        allocated_bytes += c_bytes * sizeof(char);
71    }
72
73    if( d_bytes > 0 )
74    {
75        ptr_d = mbedtls_calloc( d_bytes, sizeof(char) );
76        TEST_ASSERT( check_pointer( ptr_d ) == 0 );
77
78        allocated_bytes += d_bytes * sizeof(char);
79    }
80
81    mbedtls_memory_buffer_alloc_cur_get( &reported_bytes, &reported_blocks );
82    TEST_ASSERT( reported_bytes == allocated_bytes );
83
84    if( free_a )
85    {
86        mbedtls_free( ptr_a );
87        ptr_a = NULL;
88        TEST_ASSERT( mbedtls_memory_buffer_alloc_verify() == 0 );
89
90        allocated_bytes -= a_bytes * sizeof(char);
91    }
92
93    if( free_b )
94    {
95        mbedtls_free( ptr_b );
96        ptr_b = NULL;
97        TEST_ASSERT( mbedtls_memory_buffer_alloc_verify() == 0 );
98
99        allocated_bytes -= b_bytes * sizeof(char);
100    }
101
102    if( free_c )
103    {
104        mbedtls_free( ptr_c );
105        ptr_c = NULL;
106        TEST_ASSERT( mbedtls_memory_buffer_alloc_verify() == 0 );
107
108        allocated_bytes -= c_bytes * sizeof(char);
109    }
110
111    if( free_d )
112    {
113        mbedtls_free( ptr_d );
114        ptr_d = NULL;
115        TEST_ASSERT( mbedtls_memory_buffer_alloc_verify() == 0 );
116
117        allocated_bytes -= d_bytes * sizeof(char);
118    }
119
120    mbedtls_memory_buffer_alloc_cur_get( &reported_bytes, &reported_blocks );
121    TEST_ASSERT( reported_bytes == allocated_bytes );
122
123    if( e_bytes > 0 )
124    {
125        ptr_e = mbedtls_calloc( e_bytes, sizeof(char) );
126        TEST_ASSERT( check_pointer( ptr_e ) == 0 );
127    }
128
129    if( f_bytes > 0 )
130    {
131        ptr_f = mbedtls_calloc( f_bytes, sizeof(char) );
132        TEST_ASSERT( check_pointer( ptr_f ) == 0 );
133    }
134
135    /* Once blocks are reallocated, the block allocated to the memory request
136     * may be bigger than the request itself, which is indicated by the reported
137     * bytes, and makes it hard to know what the reported size will be, so
138     * we don't check the size after blocks have been reallocated. */
139
140    if( ptr_a != NULL )
141    {
142        mbedtls_free( ptr_a );
143        ptr_a = NULL;
144        TEST_ASSERT( mbedtls_memory_buffer_alloc_verify() == 0 );
145    }
146
147    if( ptr_b != NULL )
148    {
149        mbedtls_free( ptr_b );
150        ptr_b = NULL;
151        TEST_ASSERT( mbedtls_memory_buffer_alloc_verify() == 0 );
152    }
153
154    if( ptr_c != NULL )
155    {
156        mbedtls_free( ptr_c );
157        ptr_c = NULL;
158        TEST_ASSERT( mbedtls_memory_buffer_alloc_verify() == 0 );
159    }
160
161    if( ptr_d != NULL )
162    {
163        mbedtls_free( ptr_d );
164        ptr_d = NULL;
165        TEST_ASSERT( mbedtls_memory_buffer_alloc_verify() == 0 );
166    }
167
168    if( ptr_e != NULL )
169    {
170        mbedtls_free( ptr_e );
171        ptr_e = NULL;
172        TEST_ASSERT( mbedtls_memory_buffer_alloc_verify() == 0 );
173    }
174
175    if( ptr_f != NULL )
176    {
177        mbedtls_free( ptr_f );
178        ptr_f = NULL;
179    }
180
181    mbedtls_memory_buffer_alloc_cur_get( &reported_bytes, &reported_blocks );
182    TEST_ASSERT( reported_bytes == 0 );
183
184    TEST_ASSERT( mbedtls_memory_buffer_alloc_verify() == 0 );
185
186exit:
187    mbedtls_memory_buffer_alloc_free( );
188}
189/* END_CASE */
190
191/* BEGIN_CASE depends_on:MBEDTLS_MEMORY_DEBUG */
192void memory_buffer_alloc_oom_test(  )
193{
194    unsigned char buf[1024];
195    unsigned char *ptr_a = NULL, *ptr_b = NULL, *ptr_c = NULL;
196    size_t reported_blocks, reported_bytes;
197
198    (void)ptr_c;
199
200    mbedtls_memory_buffer_alloc_init( buf, sizeof( buf ) );
201
202    mbedtls_memory_buffer_set_verify( MBEDTLS_MEMORY_VERIFY_ALWAYS );
203
204    ptr_a = mbedtls_calloc( 432, sizeof(char) );
205    TEST_ASSERT( check_pointer( ptr_a ) == 0 );
206
207    ptr_b = mbedtls_calloc( 432, sizeof(char) );
208    TEST_ASSERT( check_pointer( ptr_b ) == 0 );
209
210    ptr_c = mbedtls_calloc( 431, sizeof(char) );
211    TEST_ASSERT( ptr_c == NULL );
212
213    mbedtls_memory_buffer_alloc_cur_get( &reported_bytes, &reported_blocks );
214    TEST_ASSERT( reported_bytes >= 864 && reported_bytes <= sizeof(buf) );
215
216    mbedtls_free( ptr_a );
217    ptr_a = NULL;
218    TEST_ASSERT( mbedtls_memory_buffer_alloc_verify() == 0 );
219
220    mbedtls_free( ptr_b );
221    ptr_b = NULL;
222    TEST_ASSERT( mbedtls_memory_buffer_alloc_verify() == 0 );
223
224    mbedtls_memory_buffer_alloc_cur_get( &reported_bytes, &reported_blocks );
225    TEST_ASSERT( reported_bytes == 0 );
226
227    TEST_ASSERT( mbedtls_memory_buffer_alloc_verify() == 0 );
228
229exit:
230    mbedtls_memory_buffer_alloc_free( );
231}
232/* END_CASE */
233
234/* BEGIN_CASE depends_on:MBEDTLS_MEMORY_DEBUG */
235void memory_buffer_small_buffer( )
236{
237    unsigned char buf[1];
238
239    mbedtls_memory_buffer_alloc_init( buf, sizeof( buf ) );
240    TEST_ASSERT( mbedtls_memory_buffer_alloc_verify() != 0 );
241}
242/* END_CASE */
243
244/* BEGIN_CASE depends_on:MBEDTLS_MEMORY_DEBUG */
245void memory_buffer_underalloc( )
246{
247    unsigned char buf[100];
248    size_t i;
249
250    mbedtls_memory_buffer_alloc_init( buf, sizeof( buf ) );
251    for( i = 1; i < MBEDTLS_MEMORY_ALIGN_MULTIPLE; i++ )
252    {
253        TEST_ASSERT( mbedtls_calloc( 1,
254                     (size_t)-( MBEDTLS_MEMORY_ALIGN_MULTIPLE - i ) ) == NULL );
255        TEST_ASSERT( mbedtls_memory_buffer_alloc_verify() == 0 );
256    }
257
258exit:
259    mbedtls_memory_buffer_alloc_free();
260}
261/* END_CASE */
262