2 #include "mbedtls/memory_buffer_alloc.h"
3 #define TEST_SUITE_MEMORY_BUFFER_ALLOC
8 * depends_on:MBEDTLS_MEMORY_BUFFER_ALLOC_C
12 /* BEGIN_SUITE_HELPERS */
13 static int check_pointer( void *p )
18 if( (size_t) p % MBEDTLS_MEMORY_ALIGN_MULTIPLE != 0 )
23 /* END_SUITE_HELPERS */
25 /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
26 void mbedtls_memory_buffer_alloc_self_test( )
28 TEST_ASSERT( mbedtls_memory_buffer_alloc_self_test( 1 ) == 0 );
32 /* BEGIN_CASE depends_on:MBEDTLS_MEMORY_DEBUG */
33 void 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,
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;
42 size_t reported_blocks;
43 size_t allocated_bytes = 0, reported_bytes;
45 mbedtls_memory_buffer_alloc_init( buf, sizeof( buf ) );
47 mbedtls_memory_buffer_set_verify( MBEDTLS_MEMORY_VERIFY_ALWAYS );
51 ptr_a = mbedtls_calloc( a_bytes, sizeof(char) );
52 TEST_ASSERT( check_pointer( ptr_a ) == 0 );
54 allocated_bytes += a_bytes * sizeof(char);
59 ptr_b = mbedtls_calloc( b_bytes, sizeof(char) );
60 TEST_ASSERT( check_pointer( ptr_b ) == 0 );
62 allocated_bytes += b_bytes * sizeof(char);
67 ptr_c = mbedtls_calloc( c_bytes, sizeof(char) );
68 TEST_ASSERT( check_pointer( ptr_c ) == 0 );
70 allocated_bytes += c_bytes * sizeof(char);
75 ptr_d = mbedtls_calloc( d_bytes, sizeof(char) );
76 TEST_ASSERT( check_pointer( ptr_d ) == 0 );
78 allocated_bytes += d_bytes * sizeof(char);
81 mbedtls_memory_buffer_alloc_cur_get( &reported_bytes, &reported_blocks );
82 TEST_ASSERT( reported_bytes == allocated_bytes );
86 mbedtls_free( ptr_a );
88 TEST_ASSERT( mbedtls_memory_buffer_alloc_verify() == 0 );
90 allocated_bytes -= a_bytes * sizeof(char);
95 mbedtls_free( ptr_b );
97 TEST_ASSERT( mbedtls_memory_buffer_alloc_verify() == 0 );
99 allocated_bytes -= b_bytes * sizeof(char);
104 mbedtls_free( ptr_c );
106 TEST_ASSERT( mbedtls_memory_buffer_alloc_verify() == 0 );
108 allocated_bytes -= c_bytes * sizeof(char);
113 mbedtls_free( ptr_d );
115 TEST_ASSERT( mbedtls_memory_buffer_alloc_verify() == 0 );
117 allocated_bytes -= d_bytes * sizeof(char);
120 mbedtls_memory_buffer_alloc_cur_get( &reported_bytes, &reported_blocks );
121 TEST_ASSERT( reported_bytes == allocated_bytes );
125 ptr_e = mbedtls_calloc( e_bytes, sizeof(char) );
126 TEST_ASSERT( check_pointer( ptr_e ) == 0 );
131 ptr_f = mbedtls_calloc( f_bytes, sizeof(char) );
132 TEST_ASSERT( check_pointer( ptr_f ) == 0 );
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. */
142 mbedtls_free( ptr_a );
144 TEST_ASSERT( mbedtls_memory_buffer_alloc_verify() == 0 );
149 mbedtls_free( ptr_b );
151 TEST_ASSERT( mbedtls_memory_buffer_alloc_verify() == 0 );
156 mbedtls_free( ptr_c );
158 TEST_ASSERT( mbedtls_memory_buffer_alloc_verify() == 0 );
163 mbedtls_free( ptr_d );
165 TEST_ASSERT( mbedtls_memory_buffer_alloc_verify() == 0 );
170 mbedtls_free( ptr_e );
172 TEST_ASSERT( mbedtls_memory_buffer_alloc_verify() == 0 );
177 mbedtls_free( ptr_f );
181 mbedtls_memory_buffer_alloc_cur_get( &reported_bytes, &reported_blocks );
182 TEST_ASSERT( reported_bytes == 0 );
184 TEST_ASSERT( mbedtls_memory_buffer_alloc_verify() == 0 );
187 mbedtls_memory_buffer_alloc_free( );
191 /* BEGIN_CASE depends_on:MBEDTLS_MEMORY_DEBUG */
192 void memory_buffer_alloc_oom_test( )
194 unsigned char buf[1024];
195 unsigned char *ptr_a = NULL, *ptr_b = NULL, *ptr_c = NULL;
196 size_t reported_blocks, reported_bytes;
200 mbedtls_memory_buffer_alloc_init( buf, sizeof( buf ) );
202 mbedtls_memory_buffer_set_verify( MBEDTLS_MEMORY_VERIFY_ALWAYS );
204 ptr_a = mbedtls_calloc( 432, sizeof(char) );
205 TEST_ASSERT( check_pointer( ptr_a ) == 0 );
207 ptr_b = mbedtls_calloc( 432, sizeof(char) );
208 TEST_ASSERT( check_pointer( ptr_b ) == 0 );
210 ptr_c = mbedtls_calloc( 431, sizeof(char) );
211 TEST_ASSERT( ptr_c == NULL );
213 mbedtls_memory_buffer_alloc_cur_get( &reported_bytes, &reported_blocks );
214 TEST_ASSERT( reported_bytes >= 864 && reported_bytes <= sizeof(buf) );
216 mbedtls_free( ptr_a );
218 TEST_ASSERT( mbedtls_memory_buffer_alloc_verify() == 0 );
220 mbedtls_free( ptr_b );
222 TEST_ASSERT( mbedtls_memory_buffer_alloc_verify() == 0 );
224 mbedtls_memory_buffer_alloc_cur_get( &reported_bytes, &reported_blocks );
225 TEST_ASSERT( reported_bytes == 0 );
227 TEST_ASSERT( mbedtls_memory_buffer_alloc_verify() == 0 );
230 mbedtls_memory_buffer_alloc_free( );
234 /* BEGIN_CASE depends_on:MBEDTLS_MEMORY_DEBUG */
235 void memory_buffer_small_buffer( )
237 unsigned char buf[1];
239 mbedtls_memory_buffer_alloc_init( buf, sizeof( buf ) );
240 TEST_ASSERT( mbedtls_memory_buffer_alloc_verify() != 0 );
244 /* BEGIN_CASE depends_on:MBEDTLS_MEMORY_DEBUG */
245 void memory_buffer_underalloc( )
247 unsigned char buf[100];
250 mbedtls_memory_buffer_alloc_init( buf, sizeof( buf ) );
251 for( i = 1; i < MBEDTLS_MEMORY_ALIGN_MULTIPLE; i++ )
253 TEST_ASSERT( mbedtls_calloc( 1,
254 (size_t)-( MBEDTLS_MEMORY_ALIGN_MULTIPLE - i ) ) == NULL );
255 TEST_ASSERT( mbedtls_memory_buffer_alloc_verify() == 0 );
259 mbedtls_memory_buffer_alloc_free();