Add more tests for the entropy module
diff --git a/include/polarssl/entropy.h b/include/polarssl/entropy.h
index 3b5d692..f5fa928 100644
--- a/include/polarssl/entropy.h
+++ b/include/polarssl/entropy.h
@@ -183,7 +183,7 @@
*
* \param data Entropy context
* \param output Buffer to fill
- * \param len Length of buffer
+ * \param len Number of bytes desired, must be at most ENTROPY_BLOCK_SIZE
*
* \return 0 if successful, or POLARSSL_ERR_ENTROPY_SOURCE_FAILED
*/
diff --git a/tests/suites/test_suite_entropy.data b/tests/suites/test_suite_entropy.data
index 5a43ffd..d81061c 100644
--- a/tests/suites/test_suite_entropy.data
+++ b/tests/suites/test_suite_entropy.data
@@ -4,5 +4,38 @@
Entropy write/update seed file
entropy_seed_file:"no_such_dir/file":POLARSSL_ERR_ENTROPY_FILE_IO_ERROR
+Entropy too many sources
+entropy_too_many_sources:
+
+Entropy output length #1
+entropy_func_len:0:0
+
+Entropy output length #2
+entropy_func_len:1:0
+
+Entropy output length #3
+entropy_func_len:2:0
+
+Entropy output length #4
+entropy_func_len:31:0
+
+Entropy output length #5
+entropy_func_len:65:POLARSSL_ERR_ENTROPY_SOURCE_FAILED
+
+Entropy failing source
+entropy_source_fail:"data_files/entropy_seed"
+
+Entropy threshold #1
+entropy_threshold:16:2:8
+
+Entropy threshold #2
+entropy_threshold:32:1:32
+
+Entropy thershold #3
+entropy_threshold:16:0:POLARSSL_ERR_ENTROPY_SOURCE_FAILED
+
+Entropy thershold #4
+entropy_threshold:1024:1:POLARSSL_ERR_ENTROPY_SOURCE_FAILED
+
Entropy self test
entropy_selftest:
diff --git a/tests/suites/test_suite_entropy.function b/tests/suites/test_suite_entropy.function
index 31286ba..844eb96 100644
--- a/tests/suites/test_suite_entropy.function
+++ b/tests/suites/test_suite_entropy.function
@@ -1,5 +1,38 @@
/* BEGIN_HEADER */
#include <polarssl/entropy.h>
+
+/*
+ * Number of calls made to entropy_dummy_source()
+ */
+static size_t entropy_dummy_calls;
+
+/*
+ * Dummy entropy source
+ *
+ * If data is NULL, write exactly the requested length.
+ * Otherwise, write the length indicated by data or error if negative
+ */
+static int entropy_dummy_source( void *data, unsigned char *output,
+ size_t len, size_t *olen )
+{
+ entropy_dummy_calls++;
+
+ if( data == NULL )
+ *olen = len;
+ else
+ {
+ int *d = (int *) data;
+
+ if( *d < 0 )
+ return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED );
+ else
+ *olen = *d;
+ }
+
+ memset( output, 0x2a, *olen );
+
+ return( 0 );
+}
/* END_HEADER */
/* BEGIN_DEPENDENCIES
@@ -21,6 +54,115 @@
}
/* END_CASE */
+/* BEGIN_CASE */
+void entropy_too_many_sources( )
+{
+ entropy_context ctx;
+ size_t i;
+
+ entropy_init( &ctx );
+
+ /*
+ * It's hard to tell precisely when the error will occur,
+ * since we don't know how many sources were automatically added.
+ */
+ for( i = 0; i < ENTROPY_MAX_SOURCES; i++ )
+ (void) entropy_add_source( &ctx, entropy_dummy_source, NULL, 16 );
+
+ TEST_ASSERT( entropy_add_source( &ctx, entropy_dummy_source, NULL, 16 )
+ == POLARSSL_ERR_ENTROPY_MAX_SOURCES );
+
+ entropy_free( &ctx );
+}
+/* END_CASE */
+
+/* BEGIN_CASE */
+void entropy_func_len( int len, int ret )
+{
+ entropy_context ctx;
+ unsigned char buf[ENTROPY_BLOCK_SIZE + 10] = { 0 };
+ unsigned char acc[ENTROPY_BLOCK_SIZE + 10] = { 0 };
+ size_t i, j;
+
+ entropy_init( &ctx );
+
+ /*
+ * See comments in entropy_self_test()
+ */
+ for( i = 0; i < 8; i++ )
+ {
+ TEST_ASSERT( entropy_func( &ctx, buf, len ) == ret );
+ for( j = 0; j < sizeof( buf ); j++ )
+ acc[j] |= buf[j];
+ }
+
+ if( ret == 0 )
+ for( j = 0; j < (size_t) len; j++ )
+ TEST_ASSERT( acc[j] != 0 );
+
+ for( j = len; j < sizeof( buf ); j++ )
+ TEST_ASSERT( acc[j] == 0 );
+}
+/* END_CASE */
+
+/* BEGIN_CASE */
+void entropy_source_fail( char *path )
+{
+ entropy_context ctx;
+ int fail = -1;
+ unsigned char buf[16];
+
+ entropy_init( &ctx );
+
+ TEST_ASSERT( entropy_add_source( &ctx, entropy_dummy_source, &fail, 16 )
+ == 0 );
+
+ TEST_ASSERT( entropy_func( &ctx, buf, sizeof( buf ) )
+ == POLARSSL_ERR_ENTROPY_SOURCE_FAILED );
+ TEST_ASSERT( entropy_gather( &ctx )
+ == POLARSSL_ERR_ENTROPY_SOURCE_FAILED );
+#if defined(POLARSSL_FS_IO)
+ TEST_ASSERT( entropy_write_seed_file( &ctx, path )
+ == POLARSSL_ERR_ENTROPY_SOURCE_FAILED );
+ TEST_ASSERT( entropy_update_seed_file( &ctx, path )
+ == POLARSSL_ERR_ENTROPY_SOURCE_FAILED );
+#else
+ ((void) path);
+#endif
+
+ entropy_free( &ctx );
+}
+/* END_CASE */
+
+/* BEGIN_CASE */
+void entropy_threshold( int threshold, int chunk_size, int result )
+{
+ entropy_context ctx;
+ unsigned char buf[ENTROPY_BLOCK_SIZE] = { 0 };
+ int ret;
+
+ entropy_init( &ctx );
+
+ TEST_ASSERT( entropy_add_source( &ctx, entropy_dummy_source,
+ &chunk_size, threshold ) == 0 );
+
+ entropy_dummy_calls = 0;
+ ret = entropy_func( &ctx, buf, sizeof( buf ) );
+
+ if( result >= 0 )
+ {
+ TEST_ASSERT( ret == 0 );
+ TEST_ASSERT( entropy_dummy_calls == (size_t) result );
+ }
+ else
+ {
+ TEST_ASSERT( ret == result );
+ }
+
+ entropy_free( &ctx );
+}
+/* END_CASE */
+
/* BEGIN_CASE depends_on:POLARSSL_SELF_TEST */
void entropy_selftest( )
{