コード例 #1
0
void main( int argc, char *argv[] )
{
    int                 exitcode;

    /*** Initialize ***/
    #ifdef __SW_BW
        FILE *          my_stdout;
        my_stdout = freopen( "tmp.log", "a", stdout );
        if( my_stdout == NULL ) {
            fprintf( stderr, "Unable to redirect stdout\n" );
            exit( -1 );
        }
    #endif
    strcpy( ProgramName, strlwr(argv[0]) );     /* store executable filename */
    if( _setmbcp( 932 ) != 0 ) {
        printf( "Cannot initialize code page.\n\n" );
        exit( EXIT_FAILURE );
    }

    /***********************************************************************/
    /*  set constraint-handler                                             */
    /***********************************************************************/

    set_constraint_handler_s( my_constraint_handler );


    /*** Test various functions ***/
    TestToWide();                               /* conversion to wide chars */
    TestFromWide();                             /* conversion from wide chars */
    TestAddendum();                             /* Normative Addendum stuff */


    /*** Print a pass/fail message and quit ***/
    if( NumErrors == 0 ) {
        printf( "%s: SUCCESS.\n", ProgramName );
        #ifdef __SW_BW
            fprintf( stderr, "%s: SUCCESS.\n", ProgramName );
        #endif
        exitcode = EXIT_SUCCESS;
    } else {
        printf( "%s: FAILURE (%d errors).\n", ProgramName, NumErrors );
        #ifdef __SW_BW
            fprintf( stderr, "%s: FAILURE (%d errors).\n",
                     ProgramName, NumErrors );
        #endif
        exitcode = EXIT_FAILURE;
    }

    #ifdef __SW_BW
        fclose( my_stdout );
        _dwShutDown();
    #endif
    exit( exitcode );
}
コード例 #2
0
ファイル: safewfmt.c プロジェクト: ABratovic/open-watcom-v2
int main( int argc, char *argv[] )
/********************************/
{
#ifdef __SW_BW
    FILE    *my_stdout;

    my_stdout = freopen( "tmp.log", "a", stdout );
    if( my_stdout == NULL ) {
        fprintf( stderr, "Unable to redirect stdout\n" );
        exit( -1 );
    }
#endif
    /*** Initialize ***/
    strcpy( ProgramName, strlwr( argv[0] ) );   /* store filename */

    /****************************/
    /*  set constraint-handler  */
    /****************************/

    set_constraint_handler_s( my_constraint_handler );

    /******************/
    /* Start of tests */
    /******************/

    Test_scan_wide();
    Test_print_wide();

    /****************/
    /* End of tests */
    /****************/

    /*** Print a pass/fail message and quit ***/
    if( NumErrors != 0 ) {
        printf( "%s: FAILURE (%d errors).\n", ProgramName, NumErrors );
        return( EXIT_FAILURE );
    }
    wprintf( L"Tests completed (%hs).\n", strlwr( argv[0] ) );
#ifdef __SW_BW
    fprintf( stderr, "Tests completed (%s).\n", strlwr( argv[0] ) );
    fclose( my_stdout );
    _dwShutDown();
#endif

    return( 0 );
}
コード例 #3
0
int main( int argc, char * const argv[] )
{
#ifdef __SW_BW
    FILE    *my_stdout;

    my_stdout = freopen( "tmp.log", "a", stdout );
    if( my_stdout == NULL ) {
        fprintf( stderr, "Unable to redirect stdout\n" );
        exit( EXIT_FAILURE );
    }
#endif
    ( void )argc;                       /* Unused */
    /*** Initialize ***/
    strcpy( ProgramName, argv[0] );     /* store filename */
    strlwr( ProgramName );              /* and lower case it */
    strcpy( myfile, __FILE__ );
    strlwr( myfile );

    /***************************************************************************/
    /*  Test Safer C Library time routines                                     */
    /***************************************************************************/

    /***********************************************************************/
    /*  set constraint-handler                                             */
    /***********************************************************************/

    set_constraint_handler_s( my_constraint_handler );

    testtime_s();

    /*** Print a pass/fail message and quit ***/
    if( NumErrors != 0 ) {
        printf( "%s: FAILURE (%d errors).\n", ProgramName, NumErrors );
        return( EXIT_FAILURE );
    }
    printf( "Tests completed (%s).\n", ProgramName );
#ifdef __SW_BW
    fprintf( stderr, "Tests completed (%s).\n", ProgramName );
    fclose( my_stdout );
    _dwShutDown();
#endif
    return( EXIT_SUCCESS );
}
コード例 #4
0
int main( int argc, char *argv[] )
/********************************/
{
#ifdef __SW_BW
    FILE    *my_stdout;

    my_stdout = freopen( "tmp.log", "a", stdout );
    if( my_stdout == NULL ) {
        fprintf( stderr, "Unable to redirect stdout\n" );
        exit( -1 );
    }
#endif

    /*** Initialize ***/
    strcpy( ProgramName, strlwr( argv[0] ) );   /* store filename */

    /****  Start of Tests  ****/

    /***********************************************************************/
    /*  set constraint-handler                                             */
    /***********************************************************************/

    set_constraint_handler_s( my_constraint_handler );

    test_bsearch_s();
    test_qsort_s();

    /****  End of Tests  ****/

    //Status_Print( );

    printf( "Tests completed (%s).\n", ProgramName );
#ifdef __SW_BW
    fprintf( stderr, "Tests completed (%s).\n", ProgramName );
    fclose( my_stdout );
    _dwShutDown();
#endif
    return( 0 );
}
コード例 #5
0
ファイル: safwstr.c プロジェクト: ABratovic/open-watcom-v2
void TestMove_s( void )
{

    wchar_t    buf[128];
    wchar_t    s2[] = L"VALUE";
    wchar_t    str[] = L"VALUE";

    wchar_t    src1[100] = L"hello";
    wchar_t    src2[7] = L"goodbye";
    wchar_t    dst1[6], dst2[5], dst3[5];

    wchar_t    sc1[100] = L"good";
    wchar_t    sc2[6] = L"hello";
    wchar_t    sc3[6] = L"hello";
    wchar_t    sc4[7] = L"abc";
    wchar_t    sc5[1000] = L"bye";

    int     violations = NumViolations;

    /*   wcstok_s */
    static wchar_t     str1[] = L"?a???b,,,#c";
    static wchar_t     str2[] = L"\t \t";
    static wchar_t     str3[] = L"?a???b,,,#c";
    wchar_t            *t, *ptr1, *ptr2, *ptr3;
    rsize_t         max1 = ARRAYCOUNT( str1 );
    rsize_t         max2 = ARRAYCOUNT( str2 );
    rsize_t         max3 = ARRAYCOUNT( str3 );


    /***********************************************************************/
    /*  set constraint-handler                                             */
    /***********************************************************************/

    set_constraint_handler_s( my_constraint_handler );

    /***********************************************************************/
    /*  memcpy_s                                                           */
    /***********************************************************************/
//  printf( "Test memcpys      (%s).\n", ProgramName );

    /* Test the "good" case */
    VERIFY( wmemcpy_s( buf, ARRAYCOUNT( buf ), s2, 0 ) == 0 );
    VERIFY( wmemcpy_s( buf, ARRAYCOUNT( buf ), s2, 1 + wcslen( s2 ) ) == 0 );
    VERIFY( wmemcpy_s( buf, wcslen( s2 ) + 2, s2, 1 + wcslen( s2 ) ) == 0 );

    VERIFY( wcslen( buf ) == wcslen( L"VALUE" ) );
    VERIFY( wcscmp( buf, L"VALUE" ) == 0 );
    VERIFY( NumViolations == violations );

    /* Test various failing cases */
    /* Test runtime-constraint violations */
    VERIFY( wmemcpy_s( buf, 3, s2, wcslen( s2 ) ) != 0 );
    VERIFY( buf[0] == L'\0' );
    VERIFY( NumViolations == ++violations );

    VERIFY( wmemcpy_s( NULL, ARRAYCOUNT( buf ), s2, wcslen( s2 ) ) != 0 );
    VERIFY( NumViolations == ++violations );

    VERIFY( wmemcpy_s( buf, ARRAYCOUNT( buf ), NULL, wcslen( s2 ) ) != 0 );
    VERIFY( NumViolations == ++violations );

    VERIFY( wmemcpy_s( buf, ARRAYCOUNT( buf ), s2, sizeof( buf ) / sizeof( buf[0] )+ 1 ) != 0 );
    VERIFY( NumViolations == ++violations );

    VERIFY( wmemcpy_s( buf, ARRAYCOUNT( buf ), buf + 1, wcslen( s2 ) ) != 0 );
    VERIFY( NumViolations == ++violations );

#if RSIZE_MAX != SIZE_MAX
    VERIFY( wmemcpy_s( buf, ARRAYCOUNT( buf ), s2, ~0 ) != 0 );
    VERIFY( NumViolations == ++violations );
#endif

    /***********************************************************************/
    /*  memmove_s                                                          */
    /***********************************************************************/
//  printf( "Test memmove      (%s).\n", ProgramName );

    /* Test the "good" cases */
    VERIFY( wmemmove_s( buf, ARRAYCOUNT( buf ), s2, 0 ) == 0 );
    VERIFY( wmemmove_s( buf, ARRAYCOUNT( buf ), s2, 1 + wcslen( s2 ) ) == 0 );

    VERIFY( wmemmove_s( buf, ARRAYCOUNT( buf ), buf + 1, 1 + wcslen( s2 ) ) == 0 );

    VERIFY( wmemmove_s( buf, 1 + wcslen( s2 ), s2, 1 + wcslen( s2 ) ) == 0 );

    VERIFY( wcslen( buf ) == wcslen( L"VALUE" ) );
    VERIFY( wcscmp( buf, L"VALUE" ) == 0 );
    VERIFY( NumViolations == violations );

    /* Test various failing cases */
    /* Test runtime-constraint violations */
    VERIFY( wmemmove_s( buf, 3, s2, wcslen( s2 ) ) != 0 );
    VERIFY( buf[0] == L'\0' );
    VERIFY( NumViolations == ++violations );

    VERIFY( wmemmove_s( NULL, ARRAYCOUNT( buf ), s2, wcslen( s2 ) ) != 0 );
    VERIFY( NumViolations == ++violations );

    VERIFY( wmemmove_s( buf, ARRAYCOUNT( buf ), NULL, wcslen( s2 ) ) != 0 );
    VERIFY( NumViolations == ++violations );

    VERIFY( wmemmove_s( buf, ARRAYCOUNT( buf ), s2, ARRAYCOUNT( buf ) + 1 ) != 0 );
    VERIFY( NumViolations == ++violations );

#if RSIZE_MAX != SIZE_MAX
    VERIFY( wmemmove_s( buf, ~0, s2, wcslen( s2 ) ) != 0 );
    VERIFY( NumViolations == ++violations );

    VERIFY( wmemmove_s( buf, ARRAYCOUNT( buf ), s2, ~0 ) != 0 );
    VERIFY( NumViolations == ++violations );
#endif

    /***********************************************************************/
    /*  wcscpy_s                                                           */
    /***********************************************************************/
//  printf( "Test memcpy       (%s).\n", ProgramName );

    /* Test the "good" cases */
    VERIFY( wcscpy_s( buf, ARRAYCOUNT( buf ), s2 ) == 0 );
    VERIFY( wcscpy_s( buf, ARRAYCOUNT( buf ), s2 ) == 0 );
    VERIFY( wcscpy_s( buf, wcslen( s2 ) + 1, s2 ) == 0 );


    VERIFY( wcslen( buf ) == wcslen( L"VALUE" ) );
    VERIFY( wcscmp( buf, L"VALUE" ) == 0 );
    VERIFY( NumViolations == violations );

    /* Test various failing cases */
    /* Test runtime-constraint violations */
    VERIFY( wcscpy_s( buf, 3, s2 ) != 0 );
    VERIFY( NumViolations == ++violations );
    VERIFY( buf[0] == L'\0' );

    VERIFY( wcscpy_s( NULL, ARRAYCOUNT( buf ), s2 ) != 0 );
    VERIFY( NumViolations == ++violations );

    VERIFY( wcscpy_s( buf, ARRAYCOUNT( buf ), NULL ) != 0 );
    VERIFY( NumViolations == ++violations );

    VERIFY( wcscpy_s( buf, 5, s2 ) != 0 );
    VERIFY( NumViolations == ++violations );

    VERIFY( wcscpy_s( buf, ARRAYCOUNT( buf ), buf + 1 ) != 0 );
    VERIFY( NumViolations == ++violations );

#if RSIZE_MAX != SIZE_MAX
    VERIFY( wcscpy_s( buf, ~0, s2 ) != 0 );
    VERIFY( NumViolations == ++violations );
#endif

    /***********************************************************************/
    /*  wcscat_s                                                           */
    /***********************************************************************/
//  printf( "Test wcscat       (%s).\n", ProgramName );
    wcscpy( sc1,src1 );
    VERIFY( wcscmp( sc1,src1 ) == 0 );
    VERIFY( wcscat_s( sc1, 100, sc5 ) == 0 );
    VERIFY( wcscmp( sc1, L"hellobye") == 0 );

    VERIFY( wcscat_s( sc2, 6, L"" ) == 0 );

    VERIFY( wcscat_s( sc3, 6, L"X" ) != 0 );
    VERIFY( NumViolations == ++violations );
    VERIFY( sc3[0] == L'\0');

    VERIFY( wcscat_s(sc4, 7, L"defghijklmn") != 0);
    VERIFY( NumViolations == ++violations );

    VERIFY( wcscmp(sc4, L"" ) == 0);


    /***********************************************************************/
    /*  wcsnlen_s                                                          */
    /***********************************************************************/

//  printf( "Test wcsnlen      (%s).\n", ProgramName );
    /* Test the "good" case */
    VERIFY( wcsnlen_s( str, ARRAYCOUNT( str ) ) == wcslen( str ) );
    VERIFY( wcsnlen_s( str, 4 ) == 4 );
    VERIFY( wcsnlen_s( str, 0 ) == 0 );
    VERIFY( wcsnlen_s( NULL, 1000 ) == 0 );

    /* Test various failing cases */

    /* No runtime-constraint violations to test */

    VERIFY( NumViolations == violations );

    /***********************************************************************/
    /*  wcsncpy_s                                                          */
    /***********************************************************************/

//  printf( "Test wcsncpy      (%s).\n", ProgramName );
    /* Test the "good" case */
    VERIFY( wcsncpy_s( buf, ARRAYCOUNT( buf ), s2, 0 ) == 0 );
    VERIFY( wcsncpy_s( buf, ARRAYCOUNT( buf ), s2, wcslen( s2 ) ) == 0 );
    VERIFY( wcsncpy_s( buf, wcslen( s2 ) + 1, s2, wcslen( s2 ) ) == 0 );

    VERIFY( wcslen( buf ) == wcslen( L"VALUE" ) );
    VERIFY( wcscmp( buf, L"VALUE" ) == 0 );
    VERIFY( NumViolations == violations );

    VERIFY( wcsncpy_s( dst1, 6, src1, 100 ) == 0 );
    VERIFY( wcscmp( dst1, src1 ) == 0 );

    VERIFY( wcsncpy_s( dst3, 5, src2, 4 ) == 0 );

    /* Test various failing cases */
    /* Test runtime-constraint violations */
    VERIFY( wcsncpy_s( buf, 3, s2, wcslen( s2 ) ) != 0 );
    VERIFY( NumViolations == ++violations );
    VERIFY( buf[0] == '\0' );

    VERIFY( wcsncpy_s( NULL, ARRAYCOUNT( buf ), s2, wcslen( s2 ) ) != 0 );
    VERIFY( NumViolations == ++violations );

    VERIFY( wcsncpy_s( buf, ARRAYCOUNT( buf ), NULL, wcslen( s2 ) ) != 0 );
    VERIFY( NumViolations == ++violations );

    VERIFY( wcsncpy_s( buf, ARRAYCOUNT( buf ), buf + 1, wcslen( s2 ) ) != 0 );
    VERIFY( NumViolations == ++violations );

    VERIFY( wcsncpy_s( dst2, 5, src2, 7 ) != 0 );
    VERIFY( NumViolations == ++violations );


#if RSIZE_MAX != SIZE_MAX
    VERIFY( wcsncpy_s( buf, ~0, s2, wcslen( s2 ) ) != 0 );
    VERIFY( NumViolations == ++violations );

    VERIFY( wcsncpy_s( buf, ARRAYCOUNT( buf ), s2, ~0 ) != 0 );
    VERIFY( NumViolations == ++violations );
#endif


    /***********************************************************************/
    /*  wcsncat_s                                                          */
    /***********************************************************************/

//  printf( "Test wcsncat      (%s).\n", ProgramName );
    wcscpy( sc1, L"good" );
    wcscpy( sc2, L"hello" );
    wcscpy( sc3, L"hello" );
    wcscpy( sc4, L"abc" );
    VERIFY( wcsncat_s( sc1, 100, sc5, 1000 ) == 0);
    VERIFY( wcscmp( sc1, L"goodbye" ) == 0 );

    VERIFY( wcsncat_s( sc2, 6, L"", 1 ) == 0 );

    VERIFY( wcsncat_s( sc4, 7, L"defghijklmn", 3 ) == 0 );
    VERIFY( wcscmp( sc4, L"abcdef" ) == 0 );

    /* Test runtime-constraint violations */
    VERIFY( wcsncat_s( sc3, 6, L"XXX", 3 ) != 0 );
    VERIFY( NumViolations == ++violations );
    VERIFY( sc3[0] == L'\0');

    /***********************************************************************/
    /*  wcstok_s                                                           */
    /***********************************************************************/

//  printf( "Test wcstok       (%s).\n", ProgramName );
    VERIFY( (t = wcstok_s( str1, &max1, L"?", &ptr1 )) != NULL );    /* points to the token "a" */
    VERIFY( wcscmp( t, L"a" ) == 0 );

    VERIFY( (t = wcstok_s( NULL, &max1, L",", &ptr1 )) != NULL );    /* points to the token "??b" */
    VERIFY( wcscmp( t, L"??b" ) == 0 );

    VERIFY( NULL == wcstok_s( str2, &max2, L" \t", &ptr2 ) );        /* null pointer */
    VERIFY( NumViolations == violations );

    VERIFY( (t = wcstok_s( NULL, &max1, L"#,", &ptr1 )) != NULL  );  /* points to the token "c" */
    VERIFY( wcscmp( t, L"c" ) == 0 );
    VERIFY( ptr1 != NULL );
    VERIFY( NumViolations == violations );

    VERIFY( NULL == wcstok_s( NULL, &max1, L"#,", &ptr1 ) );  /* at the end */

    wcscpy( str1, str3 );
    max1 = ARRAYCOUNT( str1 );
    VERIFY( NULL == wcstok_s( str1, &max1, str3, &ptr3 ) );         /* only delimiter chars */

//  printf( "Test wcstok rtc   (%s).\n", ProgramName );
    /* Test runtime-constraint violations */
    ptr1 = NULL;
    VERIFY( NULL == wcstok_s( NULL, &max1, L"?", &ptr1 ) );          /* null pointer */
    VERIFY( NumViolations == ++violations );

    VERIFY( NULL == wcstok_s( str3, NULL, L"?", &ptr1 ) );
    VERIFY( NumViolations == ++violations );

    VERIFY( NULL == wcstok_s( str3, &max3, NULL, &ptr1 ) );
    VERIFY( NumViolations == ++violations );

    VERIFY( NULL == wcstok_s( str3, &max3, L"?", NULL ) );
    VERIFY( NumViolations == ++violations );

    ptr3 = NULL;
    VERIFY( NULL == wcstok_s( NULL, &max3, L"?", &ptr3 ) );
    VERIFY( NumViolations == ++violations );

#if RSIZE_MAX != SIZE_MAX
    max1 = ~0;
    VERIFY( NULL == wcstok_s( str3, &max1, L"?", &ptr1 ) );
    VERIFY( NumViolations == ++violations );
#endif
}
コード例 #6
0
void TestMove_s( void )
{

    char    buf[128];
    char    s2[] = "VALUE";
    char    str[] = "VALUE";

    char    src1[100] = "hello";
    char    src2[7] = {'g', 'o', 'o', 'd', 'b', 'y', 'e'};
    char    dst1[6], dst2[5], dst3[5];

    char    sc1[100] = "good";
    char    sc2[6] = "hello";
    char    sc3[6] = "hello";
    char    sc4[7] = "abc";
    char    sc5[1000] = "bye";

    int     violations = NumViolations;

    /*   strtok_s */
    static char     str1[] = "?a???b,,,#c";
    static char     str2[] = "\t \t";
    static char     str3[] = "?a???b,,,#c";
    char            *t, *ptr1, *ptr2, *ptr3;
    rsize_t         max1 = sizeof( str1 );
    rsize_t         max2 = sizeof( str2 );
    rsize_t         max3 = sizeof( str3 );


    /***********************************************************************/
    /*  set constraint-handler                                             */
    /***********************************************************************/

    set_constraint_handler_s( my_constraint_handler );

    /***********************************************************************/
    /*  memcpy_s                                                           */
    /***********************************************************************/

    /* Test the "good" case */
    VERIFY( memcpy_s( buf, sizeof( buf ), s2, 0 ) == 0 );
    VERIFY( memcpy_s( buf, sizeof( buf ), s2, 1 + strlen( s2 ) ) == 0 );
    VERIFY( memcpy_s( buf, strlen( s2 ) + 2, s2, 1 + strlen( s2 ) ) == 0 );

    VERIFY( strlen( buf ) == strlen( "VALUE" ) );
    VERIFY( strcmp( buf, "VALUE" ) == 0 );
    VERIFY( NumViolations == violations );

    /* Test various failing cases */
    /* Test runtime-constraint violations */
    VERIFY( memcpy_s( buf, 3, s2, strlen( s2 ) ) != 0 );
    VERIFY( buf[0] == '\0' );
    VERIFY( NumViolations == ++violations );

    VERIFY( memcpy_s( NULL, sizeof( buf ), s2, strlen( s2 ) ) != 0 );
    VERIFY( NumViolations == ++violations );

    VERIFY( memcpy_s( buf, sizeof( buf ), NULL, strlen( s2 ) ) != 0 );
    VERIFY( NumViolations == ++violations );

    VERIFY( memcpy_s( buf, sizeof( buf ), s2, sizeof( buf ) + 1 ) != 0 );
    VERIFY( NumViolations == ++violations );

    VERIFY( memcpy_s( buf, sizeof( buf ), buf + 1, strlen( s2 ) ) != 0 );
    VERIFY( NumViolations == ++violations );

#if RSIZE_MAX != SIZE_MAX
    VERIFY( memcpy_s( buf, sizeof( buf ), s2, ~0 ) != 0 );
    VERIFY( NumViolations == ++violations );
#endif

    /***********************************************************************/
    /*  memmove_s                                                          */
    /***********************************************************************/

    /* Test the "good" cases */
    VERIFY( memmove_s( buf, sizeof( buf ) , s2, 0 ) == 0 );
    VERIFY( memmove_s( buf, sizeof( buf ) , s2, 1 + strlen( s2 ) ) == 0 );

    VERIFY( memmove_s( buf, sizeof( buf ), buf + 1, 1 + strlen( s2 ) ) == 0 );

    VERIFY( memmove_s( buf, 1 + strlen( s2 ), s2, 1 + strlen( s2 ) ) == 0 );

    VERIFY( strlen( buf ) == strlen( "VALUE" ) );
    VERIFY( strcmp( buf, "VALUE" ) == 0 );
    VERIFY( NumViolations == violations );

    /* Test various failing cases */
    /* Test runtime-constraint violations */
    VERIFY( memmove_s( buf, 3, s2, strlen( s2 ) ) != 0 );
    VERIFY( buf[0] == '\0' );
    VERIFY( NumViolations == ++violations );

    VERIFY( memmove_s( NULL, sizeof( buf ), s2, strlen( s2 ) ) != 0 );
    VERIFY( NumViolations == ++violations );

    VERIFY( memmove_s( buf, sizeof( buf ), NULL, strlen( s2 ) ) != 0 );
    VERIFY( NumViolations == ++violations );

    VERIFY( memmove_s( buf, sizeof( buf ), s2, sizeof( buf ) + 1 ) != 0 );
    VERIFY( NumViolations == ++violations );

#if RSIZE_MAX != SIZE_MAX
    VERIFY( memmove_s( buf, ~0, s2, strlen( s2 ) ) != 0 );
    VERIFY( NumViolations == ++violations );

    VERIFY( memmove_s( buf, sizeof( buf ), s2, ~0 ) != 0 );
    VERIFY( NumViolations == ++violations );
#endif

    /***********************************************************************/
    /*  strcpy_s                                                           */
    /***********************************************************************/

    /* Test the "good" cases */
    VERIFY( strcpy_s( buf, sizeof( buf ), s2 ) == 0 );
    VERIFY( strcpy_s( buf, sizeof( buf ), s2 ) == 0 );
    VERIFY( strcpy_s( buf, strlen( s2 ) + 1, s2 ) == 0 );


    VERIFY( strlen( buf ) == strlen( "VALUE" ) );
    VERIFY( strcmp( buf, "VALUE" ) == 0 );
    VERIFY( NumViolations == violations );

    /* Test various failing cases */
    /* Test runtime-constraint violations */
    VERIFY( strcpy_s( buf, 3, s2 ) != 0 );
    VERIFY( NumViolations == ++violations );
    VERIFY( buf[0] == '\0' );

    VERIFY( strcpy_s( NULL, sizeof( buf ), s2 ) != 0 );
    VERIFY( NumViolations == ++violations );

    VERIFY( strcpy_s( buf, sizeof( buf ), NULL ) != 0 );
    VERIFY( NumViolations == ++violations );

    VERIFY( strcpy_s( buf, 5, s2 ) != 0 );
    VERIFY( NumViolations == ++violations );

    VERIFY( strcpy_s( buf, sizeof( buf ), buf + 1 ) != 0 );
    VERIFY( NumViolations == ++violations );

#if RSIZE_MAX != SIZE_MAX
    VERIFY( strcpy_s( buf, ~0, s2 ) != 0 );
    VERIFY( NumViolations == ++violations );
#endif

    /***********************************************************************/
    /*  strcat_s                                                           */
    /***********************************************************************/
    strcpy( sc1, src1 );
    VERIFY( strcat_s( sc1, 100, sc5 ) == 0 );
    VERIFY( strcmp( sc1, "hellobye") == 0 );

    VERIFY( strcat_s( sc2, 6, "" ) == 0 );

    VERIFY( strcat_s( sc3, 6, "X" ) != 0 );
    VERIFY( NumViolations == ++violations );
    VERIFY( sc3[0] == '\0');

    VERIFY( strcat_s(sc4, 7, "defghijklmn") != 0);
    VERIFY( NumViolations == ++violations );

    VERIFY( strcmp(sc4, "" ) == 0);


    /***********************************************************************/
    /*  strnlen_s                                                          */
    /***********************************************************************/

    /* Test the "good" case */
    VERIFY( strnlen_s( str, sizeof( str ) ) == strlen( str ) );
    VERIFY( strnlen_s( str, 4 ) == 4 );
    VERIFY( strnlen_s( str, 0 ) == 0 );
    VERIFY( strnlen_s( NULL, 1000 ) == 0 );

    /* Test various failing cases */

    /* No runtime-constraint violations to test */

    VERIFY( NumViolations == violations );

    /***********************************************************************/
    /*  strncpy_s                                                          */
    /***********************************************************************/

    /* Test the "good" case */
    VERIFY( strncpy_s( buf, sizeof( buf ), s2, 0 ) == 0 );
    VERIFY( strncpy_s( buf, sizeof( buf ), s2, strlen( s2 ) ) == 0 );
    VERIFY( strncpy_s( buf, strlen( s2 ) + 1, s2, strlen( s2 ) ) == 0 );

    VERIFY( strlen( buf ) == strlen( "VALUE" ) );
    VERIFY( strcmp( buf, "VALUE" ) == 0 );
    VERIFY( NumViolations == violations );

    VERIFY( strncpy_s( dst1, 6, src1, 100 ) == 0 );
    VERIFY( strcmp( dst1, src1 ) == 0 );

    VERIFY( strncpy_s( dst3, 5, src2, 4 ) == 0 );

    /* Test various failing cases */
    /* Test runtime-constraint violations */
    VERIFY( strncpy_s( buf, 3, s2, strlen( s2 ) ) != 0 );
    VERIFY( NumViolations == ++violations );
    VERIFY( buf[0] == '\0' );

    VERIFY( strncpy_s( NULL, sizeof( buf ), s2, strlen( s2 ) ) != 0 );
    VERIFY( NumViolations == ++violations );

    VERIFY( strncpy_s( buf, sizeof( buf ), NULL, strlen( s2 ) ) != 0 );
    VERIFY( NumViolations == ++violations );

    VERIFY( strncpy_s( buf, sizeof( buf ), buf + 1, strlen( s2 ) ) != 0 );
    VERIFY( NumViolations == ++violations );

    VERIFY( strncpy_s( dst2, 5, src2, 7 ) != 0 );
    VERIFY( NumViolations == ++violations );


#if RSIZE_MAX != SIZE_MAX
    VERIFY( strncpy_s( buf, ~0, s2, strlen( s2 ) ) != 0 );
    VERIFY( NumViolations == ++violations );

    VERIFY( strncpy_s( buf, sizeof( buf ), s2, ~0 ) != 0 );
    VERIFY( NumViolations == ++violations );
#endif


    /***********************************************************************/
    /*  strncat_s                                                          */
    /***********************************************************************/

    strcpy( sc1, "good" );
    strcpy( sc2, "hello" );
    strcpy( sc3, "hello" );
    strcpy( sc4, "abc" );
    VERIFY( strncat_s( sc1, 100, sc5, 1000 ) == 0);
    VERIFY( strcmp( sc1, "goodbye" ) == 0 );

    VERIFY( strncat_s( sc2, 6, "", 1 ) == 0 );

    VERIFY( strncat_s( sc4, 7, "defghijklmn", 3 ) == 0 );
    VERIFY( strcmp( sc4, "abcdef" ) == 0 );

    /* Test runtime-constraint violations */
    VERIFY( strncat_s( sc3, 6, "XXX", 3 ) != 0 );
    VERIFY( NumViolations == ++violations );
    VERIFY( sc3[0] == '\0');

    /***********************************************************************/
    /*  strtok_s                                                           */
    /***********************************************************************/

    VERIFY( (t = strtok_s( str1, &max1, "?", &ptr1 )) != NULL );    /* points to the token "a" */
    VERIFY( strcmp( t, "a" ) == 0 );

    VERIFY( (t = strtok_s( NULL, &max1, ",", &ptr1 )) != NULL );    /* points to the token "??b" */
    VERIFY( strcmp( t, "??b" ) == 0 );

    VERIFY( NULL == strtok_s( str2, &max2, " \t", &ptr2 ) );        /* null pointer */
    VERIFY( NumViolations == violations );

    VERIFY( (t = strtok_s( NULL, &max1, "#,", &ptr1 )) != NULL  );  /* points to the token "c" */
    VERIFY( strcmp( t, "c" ) == 0 );
    VERIFY( ptr1 != NULL );
    VERIFY( NumViolations == violations );


    VERIFY( NULL == strtok_s( NULL, &max1, "#,", &ptr1 ) );  /* at the end */

    strcpy( str1, str3 );
    max1 = sizeof( str1 );
    VERIFY( NULL == strtok_s( str1, &max1, str3, &ptr3 ) );         /* only delimiter chars */

    /* Test runtime-constraint violations */
    ptr1 = NULL;
    VERIFY( NULL == strtok_s( NULL, &max1, "?", &ptr1 ) );          /* null pointer */
    VERIFY( NumViolations == ++violations );

    VERIFY( NULL == strtok_s( str3, NULL, "?", &ptr1 ) );
    VERIFY( NumViolations == ++violations );

    VERIFY( NULL == strtok_s( str3, &max3, NULL, &ptr1 ) );
    VERIFY( NumViolations == ++violations );

    VERIFY( NULL == strtok_s( str3, &max3, "?", NULL ) );
    VERIFY( NumViolations == ++violations );

    ptr3 = NULL;
    VERIFY( NULL == strtok_s( NULL, &max3, "?", &ptr3 ) );
    VERIFY( NumViolations == ++violations );

#if RSIZE_MAX != SIZE_MAX
    max1 = ~0;
    VERIFY( NULL == strtok_s( str3, &max1, "?", &ptr1 ) );
    VERIFY( NumViolations == ++violations );
#endif
}
コード例 #7
0
void main( int argc, char *argv[] )
{
    char filename[NUM_FILES][L_tmpnam_s];
    FILE *fp;
    int ctr, ctr2;
    struct stat info;
    errno_t rc;

    int     violations = NumViolations;

#ifdef __SW_BW
    FILE *my_stdout;
    my_stdout = freopen( "tmp.log", "a", stdout );
    if( my_stdout == NULL ) {
        fprintf( stderr, "Unable to redirect stdout\n" );
        exit( -1 );
    }
#endif
    argc=argc;

    /*** Initialize ***/
    strcpy( ProgramName, strlwr( argv[0] ) );   /* store filename */

    /***********************************************************************/
    /*  set constraint-handler                                             */
    /***********************************************************************/

    set_constraint_handler_s( my_constraint_handler );


    /* test runtime constraints */
    rc = tmpnam_s( NULL, sizeof( filename[0] ) );
    VERIFYS( rc != 0 );
    VERIFYS( NumViolations == ++violations );

    filename[0][0] = 'x';
    rc = tmpnam_s( filename[0], 0 );
    VERIFYS( rc != 0 );
    VERIFYS( filename[0][0] == 'x' );
    VERIFYS( NumViolations == ++violations );

    rc = tmpnam_s( filename[0], 4 ); /* only space for .aaa */
    VERIFYS( rc != 0 );
    VERIFYS( filename[0][0] == '\0' );
    VERIFYS( NumViolations == ++violations );


    for( ctr = 0; ctr < NUM_FILES; ++ctr ) {
        rc = tmpnam_s( filename[ctr], sizeof( filename[0] ) );
        VERIFYS( rc == 0 );
        VERIFY( remove( filename[ctr] ) != 0 ); // remove should fail
        VERIFY( errno == ENOENT );
        for( ctr2 = 0; ctr2 < ctr; ++ctr2 ) {
            VERIFY( strcmp( filename[ctr], filename[ctr2] ) != 0 );
            // Make sure all tmpfiles are unique
        }
    }
    VERIFY( utime( filename[0], NULL ) == -1 );
    EXPECT( errno == ENOENT );
    for( ctr = 0; ctr < 2; ++ctr ) {
        if( ( fp = fopen( filename[ctr], "w" ) ) == NULL ) {
            fprintf( stderr, "Internal: fopen() failed\n" );
            perror( filename[ctr] );
            exit( -1 );
        }
        fclose( fp );
    }
#ifndef __UNIX__    // rename is allowed
    VERIFY( rename( filename[0], filename[1] ) != 0 );  // rename should fail
    EXPECT( (errno == EACCES)||(errno == EEXIST) );
#endif
    VERIFY( rename( filename[0], filename[2] ) == 0 );
    VERIFY( remove( filename[0] ) != 0 );   // filename[0] was renamed -> DNE
    EXPECT( errno == ENOENT );
    VERIFY( remove( filename[2] ) == 0 );
    VERIFY( access( filename[2], F_OK ) == -1 );// filename[2] was removed
    EXPECT( errno == ENOENT );
    VERIFY( access( filename[1], F_OK ) == 0 ); // filename[1] is still here
    VERIFY( access( filename[1], R_OK ) == 0 ); // read permission
    VERIFY( access( filename[1], W_OK ) == 0 ); // write permission
    VERIFY( access( argv[0], X_OK ) == 0 );     // execute permission
    VERIFY( chmod( filename[1], S_IRUSR | S_IRGRP ) == 0 );
    // filename[1] is now read-only
    VERIFY( access( filename[1], W_OK ) == -1 );
    EXPECT( errno == EACCES );
#if !defined( __UNIX__ ) && !defined( __RDOS__ )    // remove would succeed
    VERIFY( remove( filename[1] ) != 0 );
    EXPECT( errno == EACCES );
#endif
    VERIFY( chmod( filename[1], S_IRWXU | S_IRWXG ) == 0 );
    VERIFY( stat( filename[1], &info ) == 0 );
#ifndef __UNIX__
    EXPECT( 0 <= info.st_dev && info.st_dev < 26 );
#endif
    VERIFY( utime( filename[1], NULL ) == 0 );
    VERIFY( remove( filename[1] ) == 0 );
    printf( "Tests completed (%s).\n", strlwr( argv[0] ) );
    if( warnings ) {
        printf( "Total number of warning(s) = %d.\n", warnings );
        exit( -1 );
    }
#ifdef __SW_BW
    {
        fprintf( stderr, "Tests completed (%s).\n", strlwr( argv[0] ) );
        fclose( my_stdout );
        _dwShutDown();
    }
#endif
    exit( 0 );
}