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 */
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#if defined(MBEDTLS_MEMORY_DEBUG)
43    size_t reported_blocks;
44    size_t reported_bytes;
45#endif
46    size_t allocated_bytes = 0;
47
48    mbedtls_memory_buffer_alloc_init( buf, sizeof( buf ) );
49
50    mbedtls_memory_buffer_set_verify( MBEDTLS_MEMORY_VERIFY_ALWAYS );
51
52    if( a_bytes > 0 )
53    {
54        ptr_a = mbedtls_calloc( a_bytes, sizeof(char) );
55        TEST_ASSERT( check_pointer( ptr_a ) == 0 );
56
57        allocated_bytes += a_bytes * sizeof(char);
58    }
59
60    if( b_bytes > 0 )
61    {
62        ptr_b = mbedtls_calloc( b_bytes, sizeof(char) );
63        TEST_ASSERT( check_pointer( ptr_b ) == 0 );
64
65        allocated_bytes += b_bytes * sizeof(char);
66    }
67
68    if( c_bytes > 0 )
69    {
70        ptr_c = mbedtls_calloc( c_bytes, sizeof(char) );
71        TEST_ASSERT( check_pointer( ptr_c ) == 0 );
72
73        allocated_bytes += c_bytes * sizeof(char);
74    }
75
76    if( d_bytes > 0 )
77    {
78        ptr_d = mbedtls_calloc( d_bytes, sizeof(char) );
79        TEST_ASSERT( check_pointer( ptr_d ) == 0 );
80
81        allocated_bytes += d_bytes * sizeof(char);
82    }
83
84#if defined(MBEDTLS_MEMORY_DEBUG)
85    mbedtls_memory_buffer_alloc_cur_get( &reported_bytes, &reported_blocks );
86    TEST_ASSERT( reported_bytes == allocated_bytes );
87#endif
88
89    if( free_a )
90    {
91        mbedtls_free( ptr_a );
92        ptr_a = NULL;
93        TEST_ASSERT( mbedtls_memory_buffer_alloc_verify() == 0 );
94
95        allocated_bytes -= a_bytes * sizeof(char);
96    }
97
98    if( free_b )
99    {
100        mbedtls_free( ptr_b );
101        ptr_b = NULL;
102        TEST_ASSERT( mbedtls_memory_buffer_alloc_verify() == 0 );
103
104        allocated_bytes -= b_bytes * sizeof(char);
105    }
106
107    if( free_c )
108    {
109        mbedtls_free( ptr_c );
110        ptr_c = NULL;
111        TEST_ASSERT( mbedtls_memory_buffer_alloc_verify() == 0 );
112
113        allocated_bytes -= c_bytes * sizeof(char);
114    }
115
116    if( free_d )
117    {
118        mbedtls_free( ptr_d );
119        ptr_d = NULL;
120        TEST_ASSERT( mbedtls_memory_buffer_alloc_verify() == 0 );
121
122        allocated_bytes -= d_bytes * sizeof(char);
123    }
124
125#if defined(MBEDTLS_MEMORY_DEBUG)
126    mbedtls_memory_buffer_alloc_cur_get( &reported_bytes, &reported_blocks );
127    TEST_ASSERT( reported_bytes == allocated_bytes );
128#endif
129
130    if( e_bytes > 0 )
131    {
132        ptr_e = mbedtls_calloc( e_bytes, sizeof(char) );
133        TEST_ASSERT( check_pointer( ptr_e ) == 0 );
134    }
135
136    if( f_bytes > 0 )
137    {
138        ptr_f = mbedtls_calloc( f_bytes, sizeof(char) );
139        TEST_ASSERT( check_pointer( ptr_f ) == 0 );
140    }
141
142    /* Once blocks are reallocated, the block allocated to the memory request
143     * may be bigger than the request itself, which is indicated by the reported
144     * bytes, and makes it hard to know what the reported size will be, so
145     * we don't check the size after blocks have been reallocated. */
146
147    if( ptr_a != NULL )
148    {
149        mbedtls_free( ptr_a );
150        ptr_a = NULL;
151        TEST_ASSERT( mbedtls_memory_buffer_alloc_verify() == 0 );
152    }
153
154    if( ptr_b != NULL )
155    {
156        mbedtls_free( ptr_b );
157        ptr_b = NULL;
158        TEST_ASSERT( mbedtls_memory_buffer_alloc_verify() == 0 );
159    }
160
161    if( ptr_c != NULL )
162    {
163        mbedtls_free( ptr_c );
164        ptr_c = NULL;
165        TEST_ASSERT( mbedtls_memory_buffer_alloc_verify() == 0 );
166    }
167
168    if( ptr_d != NULL )
169    {
170        mbedtls_free( ptr_d );
171        ptr_d = NULL;
172        TEST_ASSERT( mbedtls_memory_buffer_alloc_verify() == 0 );
173    }
174
175    if( ptr_e != NULL )
176    {
177        mbedtls_free( ptr_e );
178        ptr_e = NULL;
179        TEST_ASSERT( mbedtls_memory_buffer_alloc_verify() == 0 );
180    }
181
182    if( ptr_f != NULL )
183    {
184        mbedtls_free( ptr_f );
185        ptr_f = NULL;
186    }
187
188#if defined(MBEDTLS_MEMORY_DEBUG)
189    mbedtls_memory_buffer_alloc_cur_get( &reported_bytes, &reported_blocks );
190    TEST_ASSERT( reported_bytes == 0 );
191#endif
192
193    TEST_ASSERT( mbedtls_memory_buffer_alloc_verify() == 0 );
194
195exit:
196    mbedtls_memory_buffer_alloc_free( );
197}
198/* END_CASE */
199
200/* BEGIN_CASE */
201void memory_buffer_alloc_oom_test(  )
202{
203    unsigned char buf[1024];
204    unsigned char *ptr_a = NULL, *ptr_b = NULL, *ptr_c = NULL;
205#if defined(MBEDTLS_MEMORY_DEBUG)
206    size_t reported_blocks, reported_bytes;
207#endif
208
209    (void)ptr_c;
210
211    mbedtls_memory_buffer_alloc_init( buf, sizeof( buf ) );
212
213    mbedtls_memory_buffer_set_verify( MBEDTLS_MEMORY_VERIFY_ALWAYS );
214
215    ptr_a = mbedtls_calloc( 432, sizeof(char) );
216    TEST_ASSERT( check_pointer( ptr_a ) == 0 );
217
218    ptr_b = mbedtls_calloc( 432, sizeof(char) );
219    TEST_ASSERT( check_pointer( ptr_b ) == 0 );
220
221    ptr_c = mbedtls_calloc( 431, sizeof(char) );
222    TEST_ASSERT( ptr_c == NULL );
223
224#if defined(MBEDTLS_MEMORY_DEBUG)
225    mbedtls_memory_buffer_alloc_cur_get( &reported_bytes, &reported_blocks );
226    TEST_ASSERT( reported_bytes >= 864 && reported_bytes <= sizeof(buf) );
227#endif
228
229    mbedtls_free( ptr_a );
230    ptr_a = NULL;
231    TEST_ASSERT( mbedtls_memory_buffer_alloc_verify() == 0 );
232
233    mbedtls_free( ptr_b );
234    ptr_b = NULL;
235    TEST_ASSERT( mbedtls_memory_buffer_alloc_verify() == 0 );
236
237#if defined(MBEDTLS_MEMORY_DEBUG)
238    mbedtls_memory_buffer_alloc_cur_get( &reported_bytes, &reported_blocks );
239    TEST_ASSERT( reported_bytes == 0 );
240#endif
241
242    TEST_ASSERT( mbedtls_memory_buffer_alloc_verify() == 0 );
243
244exit:
245    mbedtls_memory_buffer_alloc_free( );
246}
247/* END_CASE */
248
249/* BEGIN_CASE */
250void memory_buffer_heap_too_small( )
251{
252    unsigned char buf[1];
253
254    mbedtls_memory_buffer_alloc_init( buf, sizeof( buf ) );
255    /* With MBEDTLS_MEMORY_DEBUG enabled, this prints a message
256     * "FATAL: verification of first header failed".
257     */
258    TEST_ASSERT( mbedtls_memory_buffer_alloc_verify() != 0 );
259}
260/* END_CASE */
261
262/* BEGIN_CASE */
263void memory_buffer_underalloc( )
264{
265    unsigned char buf[100];
266    size_t i;
267
268    mbedtls_memory_buffer_alloc_init( buf, sizeof( buf ) );
269    for( i = 1; i < MBEDTLS_MEMORY_ALIGN_MULTIPLE; i++ )
270    {
271        TEST_ASSERT( mbedtls_calloc( 1,
272                     (size_t)-( MBEDTLS_MEMORY_ALIGN_MULTIPLE - i ) ) == NULL );
273        TEST_ASSERT( mbedtls_memory_buffer_alloc_verify() == 0 );
274    }
275
276exit:
277    mbedtls_memory_buffer_alloc_free();
278}
279/* END_CASE */
280