test_ncbi_socket.c

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

C
1,021
字号
/* * =========================================================================== * PRODUCTION $Log: test_ncbi_socket.c,v $ * PRODUCTION Revision 1000.0  2003/10/29 17:06:50  gouriano * PRODUCTION PRODUCTION: IMPORTED [ORIGINAL] Dev-tree R6.21 * PRODUCTION * =========================================================================== *//*  $Id: test_ncbi_socket.c,v 1000.0 2003/10/29 17:06:50 gouriano Exp $ * =========================================================================== * *                            PUBLIC DOMAIN NOTICE *               National Center for Biotechnology Information * *  This software/database is a "United States Government Work" under the *  terms of the United States Copyright Act.  It was written as part of *  the author's official duties as a United States Government employee and *  thus cannot be copyrighted.  This software/database is freely available *  to the public for use. The National Library of Medicine and the U.S. *  Government have not placed any restriction on its use or reproduction. * *  Although all reasonable efforts have been taken to ensure the accuracy *  and reliability of the software and data, the NLM and the U.S. *  Government do not and cannot warrant the performance or results that *  may be obtained by using this software or data. The NLM and the U.S. *  Government disclaim all warranties, express or implied, including *  warranties of performance, merchantability or fitness for any particular *  purpose. * *  Please cite the author in any work or product based on this material. * * =========================================================================== * * Author:  Denis Vakatov * * File Description: *   Test suite for "ncbi_socket.[ch]", the portable TCP/IP socket API * */#include "../ncbi_config.h"/* OS must be specified in the command-line ("-D....") or in the conf. header */#if !defined(NCBI_OS_UNIX) && !defined(NCBI_OS_MSWIN) && !defined(NCBI_OS_MAC)#  error "Unknown OS, must be one of NCBI_OS_UNIX, NCBI_OS_MSWIN, NCBI_OS_MAC!"#endif#include <connect/ncbi_socket.h>#include <connect/ncbi_util.h>#include <stdlib.h>#include <string.h>#if defined(NCBI_OS_UNIX)#  include <unistd.h>#  define X_SLEEP(x) ((void) sleep(x))#elif defined(NCBI_OS_MSWIN)#  include <windows.h>#  define X_SLEEP(x) ((void) Sleep(1000 * x))#else#  define X_SLEEP(x) ((void) 0)#endif/* This header must go last */#include "test_assert.h"/* #define DO_CLIENT *//* #define DO_SERVER */#define TEST_BUFSIZE 8192/* exit server before sending the data expected by client(Test 1) *//*#define TEST_SRV1_SHUTDOWN*/#ifndef TEST_SRV1_SHUTDOWN/* exit server immediately after its first client is served(Test 1) *//*#define TEST_SRV1_ONCE*/#endif/* test SOCK_Reconnect() */#define DO_RECONNECT/* Log stream */static FILE* log_fp;/* The simplest randezvous (a plain request-reply) test functions *      "TEST__client_1(SOCK sock)" *      "TEST__server_1(SOCK sock)" */static const char s_C1[] = "C1";static const char s_M1[] = "M1";static const char s_S1[] = "S1";#define N_SUB_BLOB    10#define SUB_BLOB_SIZE 70000#define BIG_BLOB_SIZE (N_SUB_BLOB * SUB_BLOB_SIZE)static void TEST__client_1(SOCK sock){    EIO_Status status;    size_t     n_io, n_io_done;    char       buf[TEST_BUFSIZE];    fprintf(log_fp, "[INFO] TEST__client_1(TC1)\n");    /* Send a short string */    SOCK_SetDataLoggingAPI(eOn);    {{#if defined(NCBI_OS_MAC)        /* Special treatment for MAC clients -- server not to         * shutdown the socket on writing. MAC library         * mistakingly assumes that if server is shutdown on writing then         * it is shutdown on reading, too (?!).          */        const char* x_C1 = s_M1;#else        const char* x_C1 = s_C1;#endif        n_io = strlen(x_C1) + 1;        status = SOCK_Write(sock, x_C1, n_io, &n_io_done, eIO_WritePersist);    }}    assert(status == eIO_Success  &&  n_io == n_io_done);    /* Read the string back (it must be bounced by the server) */    SOCK_SetDataLoggingAPI(eOff);    SOCK_SetDataLogging(sock, eOn);    n_io = strlen(s_S1) + 1;    status = SOCK_Read(sock, buf, n_io, &n_io_done, eIO_ReadPeek);    status = SOCK_Read(sock, buf, n_io, &n_io_done, eIO_ReadPlain);    if (status == eIO_Closed) {        fprintf(log_fp, "[WARNING] TC1:: connection closed\n");        assert(0);    }    assert(status == eIO_Success  &&  n_io == n_io_done);    assert(strcmp(buf, s_S1) == 0);    assert(SOCK_PushBack(sock, buf, n_io_done) == eIO_Success);    memset(buf, '\xFF', n_io_done);    assert(SOCK_Read(sock, buf, n_io_done, &n_io_done, eIO_ReadPlain)           == eIO_Success);    assert(SOCK_Status(sock, eIO_Read) == eIO_Success);    assert(strcmp(buf, s_S1) == 0);    SOCK_SetDataLoggingAPI(eDefault);    SOCK_SetDataLogging(sock, eDefault);    /* Send a very big binary blob */    {{        size_t i;        unsigned char* blob = (unsigned char*) malloc(BIG_BLOB_SIZE);        for (i = 0;  i < BIG_BLOB_SIZE;  blob[i] = (unsigned char) i, i++)            continue;        for (i = 0;  i < 10;  i++) {            status = SOCK_Write(sock, blob + i * SUB_BLOB_SIZE, SUB_BLOB_SIZE,                                &n_io_done, eIO_WritePersist);            assert(status == eIO_Success  &&  n_io_done==SUB_BLOB_SIZE);        }        free(blob);    }}    /* Send a very big binary blob with read on write */    /* (it must be bounced by the server) */    {{        size_t i;        unsigned char* blob = (unsigned char*) malloc(BIG_BLOB_SIZE);        SOCK_SetReadOnWrite(sock, eOn);        for (i = 0;  i < BIG_BLOB_SIZE;  blob[i] = (unsigned char) i, i++)            continue;        for (i = 0;  i < 10;  i++) {            status = SOCK_Write(sock, blob + i * SUB_BLOB_SIZE, SUB_BLOB_SIZE,                                &n_io_done, eIO_WritePersist);            assert(status == eIO_Success  &&  n_io_done == SUB_BLOB_SIZE);        }        /* Receive back a very big binary blob, and check its content */        memset(blob,0,BIG_BLOB_SIZE);        for (i = 0;  i < 10;  i++) {            status = SOCK_Read(sock, blob + i * SUB_BLOB_SIZE, SUB_BLOB_SIZE,                               &n_io_done, eIO_ReadPersist);            assert(status == eIO_Success  &&  n_io_done == SUB_BLOB_SIZE);        }        for (n_io = 0;  n_io < BIG_BLOB_SIZE;  n_io++)            assert(blob[n_io] == (unsigned char) n_io);        free(blob);    }}    /* Try to read more data (must hit EOF as the peer is shutdown) */#if !defined(NCBI_OS_MAC)    assert(SOCK_Read(sock, buf, 1, &n_io_done, eIO_ReadPeek)           == eIO_Closed);    assert(SOCK_Status(sock, eIO_Read) == eIO_Closed);    assert(SOCK_Read(sock, buf, 1, &n_io_done, eIO_ReadPlain)           == eIO_Closed);    assert(SOCK_Status(sock, eIO_Read) == eIO_Closed);#endif    /* Shutdown on read */    assert(SOCK_Shutdown(sock, eIO_Read)  == eIO_Success);    assert(SOCK_Status  (sock, eIO_Write) == eIO_Success);    assert(SOCK_Status  (sock, eIO_Read)  == eIO_Closed);    assert(SOCK_Read    (sock, 0, 0, &n_io_done, eIO_ReadPlain) == eIO_Closed);    assert(SOCK_Read    (sock, 0, 0, &n_io_done, eIO_ReadPeek)  == eIO_Closed);    assert(SOCK_Status  (sock, eIO_Read)  == eIO_Closed);    assert(SOCK_Status  (sock, eIO_Write) == eIO_Success);    assert(SOCK_Read    (sock, buf, 1,&n_io_done,eIO_ReadPlain) == eIO_Closed);    assert(SOCK_Read    (sock, buf, 1,&n_io_done,eIO_ReadPeek)  == eIO_Closed);    assert(SOCK_Status  (sock, eIO_Read)  == eIO_Closed);    assert(SOCK_Status  (sock, eIO_Write) == eIO_Success);    /* Shutdown on write */    assert(SOCK_Shutdown(sock, eIO_Write)          == eIO_Success);    assert(SOCK_Status  (sock, eIO_Write)          == eIO_Closed);    assert(SOCK_Write   (sock, 0, 0, &n_io_done, eIO_WritePersist)                                                   == eIO_Closed);    assert(SOCK_Status  (sock, eIO_Write)          == eIO_Closed);    assert(SOCK_Write   (sock, buf, 1, &n_io_done, eIO_WritePersist)                                                   == eIO_Closed);    assert(SOCK_Status  (sock, eIO_Write)          == eIO_Closed);    /* Double shutdown should be okay */    assert(SOCK_Shutdown(sock, eIO_Read)      == eIO_Success);    assert(SOCK_Shutdown(sock, eIO_ReadWrite) == eIO_Success);    assert(SOCK_Shutdown(sock, eIO_Write)     == eIO_Success);    assert(SOCK_Status  (sock, eIO_Read)      == eIO_Closed);    assert(SOCK_Status  (sock, eIO_Write)     == eIO_Closed);    assert(SOCK_Status  (sock, eIO_ReadWrite) == eIO_InvalidArg);}static void TEST__server_1(SOCK sock){    EIO_Status status;    size_t     n_io, n_io_done;    char       buf[TEST_BUFSIZE];    fprintf(log_fp, "[INFO] TEST__server_1(TS1)\n");    /* Receive and send back a short string */    SOCK_SetDataLogging(sock, eOn);    n_io = strlen(s_C1) + 1;    status = SOCK_Read(sock, buf, n_io, &n_io_done, eIO_ReadPlain);    assert(status == eIO_Success  &&  n_io == n_io_done);    assert(strcmp(buf, s_C1) == 0  ||  strcmp(buf, s_M1) == 0);#ifdef TEST_SRV1_SHUTDOWN    return 212;#endif    SOCK_SetDataLogging(sock, eDefault);    SOCK_SetDataLoggingAPI(eOn);    n_io = strlen(s_S1) + 1;    status = SOCK_Write(sock, s_S1, n_io, &n_io_done, eIO_WritePersist);    assert(status == eIO_Success  &&  n_io == n_io_done);    SOCK_SetDataLoggingAPI(eOff);    /* Receive a very big binary blob, and check its content */    {{#define DO_LOG_SIZE    300#define DONT_LOG_SIZE  BIG_BLOB_SIZE - DO_LOG_SIZE        unsigned char* blob = (unsigned char*) malloc(BIG_BLOB_SIZE);        status = SOCK_Read(sock,blob,DONT_LOG_SIZE,&n_io_done,eIO_ReadPersist);        assert(status == eIO_Success  &&  n_io_done == DONT_LOG_SIZE);        SOCK_SetDataLogging(sock, eOn);        status = SOCK_Read(sock, blob + DONT_LOG_SIZE, DO_LOG_SIZE,                           &n_io_done, eIO_ReadPersist);        assert(status == eIO_Success  &&  n_io_done == DO_LOG_SIZE);        SOCK_SetDataLogging(sock, eDefault);        SOCK_SetDataLoggingAPI(eDefault);        for (n_io = 0;  n_io < BIG_BLOB_SIZE;  n_io++)            assert(blob[n_io] == (unsigned char) n_io);        free(blob);    }}    /* Receive a very big binary blob, and write data back */    {{#define DO_LOG_SIZE    300#define DONT_LOG_SIZE  BIG_BLOB_SIZE - DO_LOG_SIZE        unsigned char* blob = (unsigned char*) malloc(BIG_BLOB_SIZE);        int i;        for (i = 0;  i < 10;  i++) {            /*            X_SLEEP(1);*/            status = SOCK_Read(sock, blob + i * SUB_BLOB_SIZE, SUB_BLOB_SIZE,                               &n_io_done, eIO_ReadPersist);            assert(status == eIO_Success  &&  n_io_done == SUB_BLOB_SIZE);            status = SOCK_Write(sock, blob + i * SUB_BLOB_SIZE, SUB_BLOB_SIZE,                                &n_io_done, eIO_WritePersist);            assert(status == eIO_Success  &&  n_io_done == SUB_BLOB_SIZE);        }        for (n_io = 0;  n_io < BIG_BLOB_SIZE;  n_io++)            assert(blob[n_io] == (unsigned char) n_io);        free(blob);    }}    /* Shutdown on write */#ifdef NCBI_OS_MSWIN    assert(SOCK_Shutdown(sock, eIO_ReadWrite)    == eIO_Success);#else    assert(SOCK_Shutdown(sock, eIO_Write)        == eIO_Success);#endif    assert(SOCK_Status  (sock, eIO_Write)        == eIO_Closed);    assert(SOCK_Write   (sock, 0, 0, &n_io_done, eIO_WritePersist)                                                 == eIO_Closed);    assert(SOCK_Status  (sock, eIO_Write)        == eIO_Closed);#ifdef NCBI_OS_MSWIN    assert(SOCK_Status  (sock, eIO_Read)         == eIO_Closed);#else    assert(SOCK_Status  (sock, eIO_Read)         == eIO_Success);#endif}/* More complicated randezvous test functions *      "TEST__client_2(SOCK sock)" *      "TEST__server_2(SOCK sock)" */static void s_DoubleTimeout(STimeout *to) {    if (!to->sec  &&  !to->usec) {        to->usec = 1;    } else {        to->sec   = 2 * to->sec + (2 * to->usec) / 1000000;        to->usec = (2 * to->usec) % 1000000;    }}static void TEST__client_2(SOCK sock){#define W_FIELD  10#define N_FIELD  1000#define N_REPEAT 10#define N_RECONNECT 3

⌨️ 快捷键说明

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