Fix minor style issues in test framework
diff --git a/tests/suites/helpers.function b/tests/suites/helpers.function
index 2475a3c..5938447 100644
--- a/tests/suites/helpers.function
+++ b/tests/suites/helpers.function
@@ -34,6 +34,9 @@
 
 #include <string.h>
 
+#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
+#include <unistd.h>
+#endif
 
 /*----------------------------------------------------------------------------*/
 /* Constants */
@@ -102,6 +105,43 @@
 /*----------------------------------------------------------------------------*/
 /* Helper Functions */
 
+#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
+static int redirect_output( FILE** out_stream, const char* path )
+{
+    int stdout_fd = dup( fileno( *out_stream ) );
+
+    if( stdout_fd == -1 )
+    {
+        return -1;
+    }
+
+    fflush( *out_stream );
+    fclose( *out_stream );
+    *out_stream = fopen( path, "w" );
+
+    if( *out_stream == NULL )
+    {
+        return -1;
+    }
+
+    return stdout_fd;
+}
+
+static int restore_output( FILE** out_stream, int old_fd )
+{
+    fflush( *out_stream );
+    fclose( *out_stream );
+
+    *out_stream = fdopen( old_fd, "w" );
+    if( *out_stream == NULL )
+    {
+        return -1;
+    }
+
+    return 0;
+}
+#endif /* __unix__ || __APPLE__ __MACH__ */
+
 static int unhexify( unsigned char *obuf, const char *ibuf )
 {
     unsigned char c, c2;
diff --git a/tests/suites/main_test.function b/tests/suites/main_test.function
index ed5d414..14209a5 100644
--- a/tests/suites/main_test.function
+++ b/tests/suites/main_test.function
@@ -79,12 +79,6 @@
 /*----------------------------------------------------------------------------*/
 /* Test dispatch code */
 
-#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
-#include <unistd.h>
-#endif
-#include <stdio.h>
-
-
 int dep_check( char *str )
 {
     if( str == NULL )
@@ -257,7 +251,6 @@
     const char **test_files = NULL;
     int testfile_count = 0;
     int option_verbose = 0;
-    int tests_stdout;
 
     /* Other Local variables */
     int arg_index = 1;
@@ -268,6 +261,7 @@
     char buf[5000];
     char *params[50];
     void *pointer;
+    int stdout_fd = 0;
 
 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \
     !defined(TEST_SUITE_MEMORY_BUFFER_ALLOC)
@@ -353,7 +347,7 @@
             if( unmet_dep_count > 0 )
             {
                 mbedtls_fprintf( stderr,
-                    "FATAL: Dep count larger than zero at start of loop\n");
+                    "FATAL: Dep count larger than zero at start of loop\n" );
                 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
             }
             unmet_dep_count = 0;
@@ -389,7 +383,7 @@
                         unmet_dependencies[ unmet_dep_count ] = strdup(params[i]);
                         if(  unmet_dependencies[ unmet_dep_count ] == NULL )
                         {
-                            mbedtls_fprintf( stderr, "FATAL: Out of memory\n");
+                            mbedtls_fprintf( stderr, "FATAL: Out of memory\n" );
                             mbedtls_exit( MBEDTLS_EXIT_FAILURE );
                         }
                         unmet_dep_count++;
@@ -412,21 +406,11 @@
                  */
                 if( !option_verbose )
                 {
-                    /* Redirect all stdout output to /dev/null */
-                    tests_stdout = dup( fileno(stdout) );
-                    if( tests_stdout == -1 )
+                    stdout_fd = redirect_output( &stdout, "/dev/null" );
+                    if( stdout_fd == -1 )
                     {
                         /* Redirection has failed with no stdout so exit */
-                        exit(1);
-                    }
-
-                    fflush( stdout );
-                    fclose( stdout );
-                    stdout = fopen("/dev/null", "w" );
-                    if( stdout == NULL )
-                    {
-                        /* Redirection has failed with no stdout so exit */
-                        exit(1);
+                        exit( 1 );
                     }
                 }
 #endif /* __unix__ || __APPLE__ __MACH__ */
@@ -434,18 +418,10 @@
                 ret = dispatch_test( cnt, params );
 
 #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
-                if( !option_verbose )
+                if( !option_verbose && restore_output( &stdout, stdout_fd ) )
                 {
-                    /* Restore stdout */
-                    fflush( stdout );
-                    fclose( stdout );
-
-                    stdout = fdopen ( tests_stdout, "w");
-                    if( stdout == NULL )
-                    {
                         /* Redirection has failed with no stdout so exit */
-                        exit(1);
-                    }
+                        exit( 1 );
                 }
 #endif /* __unix__ || __APPLE__ __MACH__ */