early-access version 1503
This commit is contained in:
119
externals/mbedtls/programs/test/selftest.c
vendored
119
externals/mbedtls/programs/test/selftest.c
vendored
@@ -1,8 +1,31 @@
|
||||
/*
|
||||
* Self-test demonstration program
|
||||
*
|
||||
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
||||
* SPDX-License-Identifier: GPL-2.0
|
||||
* Copyright The Mbed TLS Contributors
|
||||
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
||||
*
|
||||
* This file is provided under the Apache License 2.0, or the
|
||||
* GNU General Public License v2.0 or later.
|
||||
*
|
||||
* **********
|
||||
* Apache License 2.0:
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* **********
|
||||
*
|
||||
* **********
|
||||
* GNU General Public License v2.0 or later:
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -18,7 +41,7 @@
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
* **********
|
||||
*/
|
||||
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
@@ -68,6 +91,8 @@
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_calloc calloc
|
||||
#define mbedtls_free free
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_snprintf snprintf
|
||||
#define mbedtls_exit exit
|
||||
@@ -79,7 +104,88 @@
|
||||
#include "mbedtls/memory_buffer_alloc.h"
|
||||
#endif
|
||||
|
||||
static int test_snprintf( size_t n, const char ref_buf[10], int ref_ret )
|
||||
|
||||
#if defined MBEDTLS_SELF_TEST
|
||||
/* Sanity check for malloc. This is not expected to fail, and is rather
|
||||
* intended to display potentially useful information about the platform,
|
||||
* in particular the behavior of malloc(0). */
|
||||
static int calloc_self_test( int verbose )
|
||||
{
|
||||
int failures = 0;
|
||||
void *empty1 = mbedtls_calloc( 0, 1 );
|
||||
void *empty2 = mbedtls_calloc( 0, 1 );
|
||||
void *buffer1 = mbedtls_calloc( 1, 1 );
|
||||
void *buffer2 = mbedtls_calloc( 1, 1 );
|
||||
uintptr_t old_buffer1;
|
||||
|
||||
if( empty1 == NULL && empty2 == NULL )
|
||||
{
|
||||
if( verbose )
|
||||
mbedtls_printf( " CALLOC(0): passed (NULL)\n" );
|
||||
}
|
||||
else if( empty1 == NULL || empty2 == NULL )
|
||||
{
|
||||
if( verbose )
|
||||
mbedtls_printf( " CALLOC(0): failed (mix of NULL and non-NULL)\n" );
|
||||
++failures;
|
||||
}
|
||||
else if( empty1 == empty2 )
|
||||
{
|
||||
if( verbose )
|
||||
mbedtls_printf( " CALLOC(0): passed (same non-null)\n" );
|
||||
}
|
||||
else
|
||||
{
|
||||
if( verbose )
|
||||
mbedtls_printf( " CALLOC(0): passed (distinct non-null)\n" );
|
||||
}
|
||||
|
||||
if( buffer1 == NULL || buffer2 == NULL )
|
||||
{
|
||||
if( verbose )
|
||||
mbedtls_printf( " CALLOC(1): failed (NULL)\n" );
|
||||
++failures;
|
||||
}
|
||||
else if( buffer1 == buffer2 )
|
||||
{
|
||||
if( verbose )
|
||||
mbedtls_printf( " CALLOC(1): failed (same buffer twice)\n" );
|
||||
++failures;
|
||||
}
|
||||
else
|
||||
{
|
||||
if( verbose )
|
||||
mbedtls_printf( " CALLOC(1): passed\n" );
|
||||
}
|
||||
|
||||
old_buffer1 = (uintptr_t) buffer1;
|
||||
mbedtls_free( buffer1 );
|
||||
buffer1 = mbedtls_calloc( 1, 1 );
|
||||
if( buffer1 == NULL )
|
||||
{
|
||||
if( verbose )
|
||||
mbedtls_printf( " CALLOC(1 again): failed (NULL)\n" );
|
||||
++failures;
|
||||
}
|
||||
else
|
||||
{
|
||||
if( verbose )
|
||||
mbedtls_printf( " CALLOC(1 again): passed (%s address)\n",
|
||||
(uintptr_t) old_buffer1 == (uintptr_t) buffer1 ?
|
||||
"same" : "different" );
|
||||
}
|
||||
|
||||
if( verbose )
|
||||
mbedtls_printf( "\n" );
|
||||
mbedtls_free( empty1 );
|
||||
mbedtls_free( empty2 );
|
||||
mbedtls_free( buffer1 );
|
||||
mbedtls_free( buffer2 );
|
||||
return( failures );
|
||||
}
|
||||
#endif /* MBEDTLS_SELF_TEST */
|
||||
|
||||
static int test_snprintf( size_t n, const char *ref_buf, int ref_ret )
|
||||
{
|
||||
int ret;
|
||||
char buf[10] = "xxxxxxxxx";
|
||||
@@ -175,6 +281,7 @@ typedef struct
|
||||
|
||||
const selftest_t selftests[] =
|
||||
{
|
||||
{"calloc", calloc_self_test},
|
||||
#if defined(MBEDTLS_MD2_C)
|
||||
{"md2", mbedtls_md2_self_test},
|
||||
#endif
|
||||
@@ -418,7 +525,5 @@ int main( int argc, char *argv[] )
|
||||
if( suites_failed > 0)
|
||||
mbedtls_exit( MBEDTLS_EXIT_FAILURE );
|
||||
|
||||
/* return() is here to prevent compiler warnings */
|
||||
return( MBEDTLS_EXIT_SUCCESS );
|
||||
mbedtls_exit( MBEDTLS_EXIT_SUCCESS );
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user