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