test_ncbi_socket.c

来自「ncbi源码」· C语言 代码 · 共 1,021 行 · 第 1/3 页

C
1,021
字号
#endif/* Fake (printout only) MT critical section callback and data */static char TEST_LockUserData[] = "TEST_LockUserData";#if defined(__cplusplus)extern "C" {    static int/*bool*/ TEST_LockHandler(void* user_data, EMT_Lock how);    static void        TEST_LockCleanup(void* user_data);}#endif /* __cplusplus */static int/*bool*/ TEST_LockHandler(void* user_data, EMT_Lock how){    const char* what_str = 0;    switch ( how ) {    case eMT_Lock:        what_str = "eMT_Lock";        break;    case eMT_LockRead:        what_str = "eMT_LockRead";        break;    case eMT_Unlock:        what_str = "eMT_Unlock";        break;    }    fprintf(log_fp, "TEST_LockHandler(\"%s\", %s)\n",            user_data ? (char*)user_data : "<NULL>", what_str);    return 1/*true*/;}static void TEST_LockCleanup(void* user_data){    fprintf(log_fp, "TEST_LockCleanup(\"%s\")\n",            user_data ? (char*)user_data : "<NULL>");}static int/*bool*/ TEST_gethostbyaddr(unsigned int host);static const char* s_ntoa(unsigned int host){    static char buf[256];    if (SOCK_ntoa(host, buf, sizeof(buf)) != 0) {        buf[0] = '?';        buf[1] = '\0';    }    return buf;}static int/*bool*/ TEST_gethostbyname(const char* name){    char         buf[256];    unsigned int host;    fprintf(log_fp, "------------\n");    host = SOCK_gethostbyname(name);    fprintf(log_fp, "SOCK_gethostbyname(\"%s\"):  0x%08X [%s]\n",            name, (unsigned int) host, s_ntoa(host));    if ( !host ) {        return 0/*false*/;    }          name = SOCK_gethostbyaddr(host, buf, sizeof(buf));    if ( name ) {        assert(name == buf);        assert(0 < strlen(buf)  &&  strlen(buf) < sizeof(buf));        fprintf(log_fp, "SOCK_gethostbyaddr(0x%08X [%s]):  \"%s\"\n",                (unsigned int) host, s_ntoa(host), name);    } else {        fprintf(log_fp, "SOCK_gethostbyaddr(0x%08X [%s]):  <not found>\n",                (unsigned int) host, s_ntoa(host));    }    return 1/*true*/;}static int/*bool*/ TEST_gethostbyaddr(unsigned int host){    const char*  name;    char         buf[1024];    fprintf(log_fp, "- - - - - - -\n");    name = SOCK_gethostbyaddr(host, buf, sizeof(buf));    if ( name ) {        assert(name == buf);        assert(0 < strlen(buf)  &&  strlen(buf) < sizeof(buf));        fprintf(log_fp, "SOCK_gethostbyaddr(0x%08X [%s]):  \"%s\"\n",                (unsigned int) host, s_ntoa(host), name);    } else {        fprintf(log_fp, "SOCK_gethostbyaddr(0x%08X [%s]):  <not found>\n",                (unsigned int) host, s_ntoa(host));        return 0/*false*/;    }    host = SOCK_gethostbyname(name);    fprintf(log_fp, "SOCK_gethostbyname(\"%s\"):  0x%08X [%s]\n",            name, (unsigned int) host, s_ntoa(host));          return 1/*true*/;}/* Try SOCK_htonl(), SOCK_gethostbyname() and SOCK_gethostbyaddr() */static void TEST_gethostby(void){    fprintf(log_fp, "\n===============================\n");    assert( SOCK_htonl(0) == 0 );    assert( SOCK_htonl(0xFFFFFFFF) == 0xFFFFFFFF );    assert( !TEST_gethostbyname("  ") );    assert( !TEST_gethostbyname("a1....b1") );    assert( !TEST_gethostbyname("boo.foo.bar.doo") );    fprintf(log_fp, "\n++++++++++++++++++++++\n");    (void) TEST_gethostbyname("localhost");    (void) TEST_gethostbyname("ncbi.nlm.nih.gov");    (void) TEST_gethostbyname("127.0.0.1");    (void) TEST_gethostbyname("130.14.25.1");    (void) TEST_gethostbyaddr(0);    (void) TEST_gethostbyaddr(SOCK_gethostbyname("127.0.0.1"));    (void) TEST_gethostbyaddr(SOCK_gethostbyname("130.14.25.1"));    (void) TEST_gethostbyaddr(SOCK_gethostbyname("234.234.234.234"));    (void) TEST_gethostbyaddr(0xFFFFFFFF);    fprintf(log_fp, "\n===============================\n");}/* Main function * Parse command-line options, initialize and cleanup API internals; * run client or server test */extern int main(int argc, char** argv){#define MIN_PORT 5001#define DEF_PORT 5555#define DEF_HOST "localhost"    /* Error log stream */    log_fp = stderr;    /* Test client or server using hard-coded parameters */#if   defined(DO_SERVER)    argc = 2;#elif defined(DO_CLIENT)    argc = 3;#endif    /* Try to set various fake MT safety locks     */    CORE_SetLOCK( MT_LOCK_Create(0, TEST_LockHandler, TEST_LockCleanup) );    CORE_SetLOCK(0);    CORE_SetLOCK(0);    CORE_SetLOCK( MT_LOCK_Create(&TEST_LockUserData,                                 TEST_LockHandler, TEST_LockCleanup) );    /* Setup log stream     */    CORE_SetLOGFormatFlags(fLOG_None          | fLOG_Level   |                           fLOG_OmitNoteLevel | fLOG_DateTime);    CORE_SetLOGFILE(stderr, 0/*false*/);    /* Printout local hostname     */    {{        char local_host[64];        assert(SOCK_gethostname(local_host, sizeof(local_host)) == 0);        fprintf(log_fp, "[INFO] Running NCBISOCK test on host \"%s\"\n",                local_host);    }}    /* Parse cmd.-line args and decide whether it's a client or a server     */    switch ( argc ) {    case 2: {        /*** SERVER ***/        int port;#if defined(DO_SERVER)        port = DEF_PORT;#else        if (sscanf(argv[1], "%d", &port) != 1  ||  port < MIN_PORT)            break;#endif /* DO_SERVER */        TEST__server((unsigned short) port);        assert(SOCK_ShutdownAPI() == eIO_Success);        CORE_SetLOG(0);        CORE_SetLOCK(0);        return 0;    }    case 3: case 4: {        /*** CLIENT ***/        const char* server_host;        int         server_port;        STimeout*   timeout = 0;#if defined(DO_CLIENT)        server_host = DEF_HOST;        server_port = DEF_PORT;#else        STimeout    x_timeout;        /* host */        server_host = argv[1];        /* port */        if (sscanf(argv[2], "%d", &server_port) != 1  ||            server_port < MIN_PORT)            break;        /* timeout */        if (argc == 4) {            double tm_out = atof(argv[3]);            if (tm_out < 0)                break;            x_timeout.sec  = (unsigned int)tm_out;            x_timeout.usec = (unsigned int)((tm_out - x_timeout.sec) *1000000);            timeout = &x_timeout;        };#endif /* DO_CLIENT */        TEST_gethostby();        TEST__client(server_host, (unsigned short)server_port, timeout);        assert(SOCK_ShutdownAPI() == eIO_Success);        CORE_SetLOG(0);        CORE_SetLOCK(0);        return 0;    }    } /* switch */    /* USAGE     */    fprintf(log_fp,            "\nUSAGE:\n"            "  Client: %s <srv_host> <port> [conn_timeout]\n"            "  Server: %s <port>\n"            " where <port> is greater than %d, and [conn_timeout] is double\n",            argv[0], argv[0], (int)MIN_PORT);    CORE_SetLOG(0);    CORE_SetLOCK(0);    return 1;}/* * --------------------------------------------------------------------------- * $Log: test_ncbi_socket.c,v $ * Revision 1000.0  2003/10/29 17:06:50  gouriano * PRODUCTION: IMPORTED [ORIGINAL] Dev-tree R6.21 * * Revision 6.21  2003/05/21 17:46:51  lavr * Fix MSVC-related problems with SOCK_Shutdown() * * Revision 6.20  2003/05/14 03:58:43  lavr * Match changes in respective APIs of the tests * * Revision 6.19  2002/12/04 16:58:13  lavr * No changes * * Revision 6.18  2002/11/01 20:17:39  lavr * Change hostname buffers to hold up to 256 chars * * Revision 6.17  2002/10/11 19:58:23  lavr * Remove asserts() (replace with tests) for SOCK_gethostbyaddr({0|0xFFFFFFFF}) * * Revision 6.16  2002/08/12 15:10:43  lavr * Use persistent SOCK_Write() * * Revision 6.15  2002/08/07 16:38:08  lavr * EIO_ReadMethod enums changed accordingly; log moved to end * * Revision 6.14  2002/03/22 19:47:48  lavr * Test_assert.h made last among the include files * * Revision 6.13  2002/02/11 20:36:45  lavr * Use "ncbi_config.h" * * Revision 6.12  2002/01/16 21:23:15  vakatov * Utilize header "test_assert.h" to switch on ASSERTs in the Release mode too * * Revision 6.11  2001/07/11 00:44:33  vakatov * Added TEST_gethostby***() -- tests for SOCK_gethostby{addr,name}() * * Revision 6.10  2001/05/21 15:11:13  ivanov * Added test for automatic read on write data from the socket * (stall protection). * * Revision 6.9  2001/01/26 23:55:10  vakatov * [NCBI_OS_MAC]  Do not do server write shutdown for MAC client * * Revision 6.8  2000/11/15 18:51:44  vakatov * Add tests for SOCK_Shutdown() and SOCK_Status(). * Use SOCK_Status() instead of SOCK_Eof(). * * Revision 6.7  2000/06/23 19:39:22  vakatov * Test the logging of socket I/O (incl. binary data) * * Revision 6.6  2000/03/24 23:12:13  vakatov * Starting the development quasi-branch to implement CONN API. * All development is performed in the NCBI C++ tree only, while * the NCBI C tree still contains "frozen" (see the last revision) code. * * Revision 6.5  2000/02/24 23:09:42  vakatov * Use C++ Toolkit specific wrapper "test_ncbi_socket_.c" for * "test_ncbi_socket.c" * * Revision 6.4  2000/02/23 22:34:37  vakatov * Can work both "standalone" and as a part of NCBI C++ or C toolkits * * Revision 6.3  1999/11/26 19:05:21  vakatov * Initialize "log_fp" in "main()"... * * Revision 6.2  1999/10/19 16:16:03  vakatov * Try the NCBI C and C++ headers only if NCBI_OS_{UNIX, MSWIN, MAC} is * not #define'd * * Revision 6.1  1999/10/18 15:40:21  vakatov * Initial revision * * =========================================================================== */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?