Esempio n. 1
0
int main(int argc, const char* argv[])
{
    const char custom_body[] =
        "Subject: Custom sized body\n"
        "\n"
        "Custom sized body\n"
        "0123456789\n"; /* these 11 chars to ignore */
    const char* body[] = {
        "This is a simple test",
        "This is a test with\n.",
        0,
        ".",
        "\n.\n",
        ".\n",
        "",
        "a\nb\nc\nd\n.",
        "a\r\n\rb\r\nc\r\nd\r\n.",
        ".\na"
    };
    const char* subject[] = {
        0,
        "CORE_SendMail Test",
        "",
    };
    const char* to[] = {
        "lavr",
        "lavr@pavo",
        " \"Anton Lavrentiev\"   <lavr@pavo>  , lavr, <lavr>   ",
    };
    size_t i, j, k, n, m;
    const char* mx_host;
    SSendMailInfo info;
    const char* retval;
    STimeout mx_tmo;
    char* huge_body;
    short mx_port;
    char val[32];
    FILE* fp;

    g_NCBI_ConnectRandomSeed = (int) time(0) ^ NCBI_CONNECT_SRAND_ADDEND;
    srand(g_NCBI_ConnectRandomSeed);

    CORE_SetLOGFormatFlags(fLOG_None          | fLOG_Level   |
                           fLOG_OmitNoteLevel | fLOG_DateTime);
    CORE_SetLOGFILE(stderr, 0/*false*/);

    ConnNetInfo_GetValue(0, REG_CONN_DEBUG_PRINTOUT, val, sizeof(val),
                         DEF_CONN_DEBUG_PRINTOUT);
    if (ConnNetInfo_Boolean(val)
        ||  (*val  &&  (strcasecmp(val, "all")  == 0  ||
                        strcasecmp(val, "some") == 0  ||
                        strcasecmp(val, "data") == 0))) {
        SOCK_SetDataLoggingAPI(eOn);
    }

    strcpy(val, "@");
    SendMailInfo_InitEx(&info, val, eCORE_UsernameCurrent);
    CORE_LOGF(eLOG_Note, ("@ - <%s>", info.from));

    strcpy(info.from, "user0");
    SendMailInfo_InitEx(&info, info.from, eCORE_UsernameCurrent);
    CORE_LOGF(eLOG_Note, ("user0 - <%s>", info.from));

    strcpy(info.from, "user1@");
    SendMailInfo_InitEx(&info, info.from, eCORE_UsernameCurrent);
    CORE_LOGF(eLOG_Note, ("user1@ - <%s>", info.from));

    strcpy(val, "@host2.net");
    SendMailInfo_InitEx(&info, val, eCORE_UsernameLogin);
    CORE_LOGF(eLOG_Note, ("@host2.net - <%s>", info.from));

    strcpy(val, "*****@*****.**");
    SendMailInfo_InitEx(&info, val, eCORE_UsernameReal);
    CORE_LOGF(eLOG_Note, ("[email protected] - <%s>", info.from));

    strcpy(info.from, "*****@*****.**");
    SendMailInfo_InitEx(&info, info.from, eCORE_UsernameReal);
    CORE_LOGF(eLOG_Note, ("[email protected] - <%s>", info.from));

    SendMailInfo_InitEx(&info, 0, eCORE_UsernameReal);
    CORE_LOGF(eLOG_Note, ("NULL - <%s>", info.from));

    if ((huge_body = malloc(TEST_HUGE_BODY_SIZE)) != 0) {

        strcpy(huge_body, "user5@");
        for (i = 0;  i < TEST_HUGE_BODY_SIZE - 6;  i++)
            huge_body[i + 6] = "abcdefghijklmnopqrstuvwxyz."[rand() % 27];
        huge_body[TEST_HUGE_BODY_SIZE - 1] = '\0';
        SendMailInfo_InitEx(&info, huge_body, eCORE_UsernameCurrent);
        CORE_LOGF(eLOG_Note, ("HUGE user5@host - <%s>", info.from));

        SendMailInfo_InitEx(&info, huge_body + 5, eCORE_UsernameLogin);
        CORE_LOGF(eLOG_Note, ("HUGE @host - <%s>", info.from));

        huge_body[4] = '6';
        huge_body[5] = '_';
        for (i = 6;  i < sizeof(info.from) + 1;  i++) {
            if (huge_body[i] == '.')
                huge_body[i]  = '_';
        }
        huge_body[sizeof(info.from) + 1] = '@';
        SendMailInfo_InitEx(&info, huge_body, eCORE_UsernameReal);
        CORE_LOGF(eLOG_Note, ("HUGE user6 - <%s>", info.from));

        huge_body[4] = '7';
        huge_body[sizeof(info.from) - 10]  = '@';
        SendMailInfo_InitEx(&info, huge_body, eCORE_UsernameReal);
        CORE_LOGF(eLOG_Note, ("LONG user7 - <%s>", info.from));

        memcpy(huge_body + sizeof(info.from) - 10, "user8", 5);
        huge_body[sizeof(info.from) << 1] = '\0';
        SendMailInfo_InitEx(&info, huge_body + sizeof(info.from) - 10,
                            eCORE_UsernameReal);
        CORE_LOGF(eLOG_Note, ("LONG user8 - <%s>", info.from));

        SendMailInfo_InitEx(&info, huge_body + sizeof(info.from) + 1,
                            eCORE_UsernameReal);
        CORE_LOGF(eLOG_Note, ("LONG @host - <%s>", info.from));

        free(huge_body);
    }

    if (argc > 1) {
        CORE_LOG(eLOG_Note, "Special test requested");
        if ((fp = fopen(argv[1], "rb")) != 0          &&
            fseek(fp, 0, SEEK_END) == 0               &&
            (m = ftell(fp)) != (size_t)(-1)           &&
            fseek(fp, 0, SEEK_SET) == 0               &&
            (huge_body = (char*) malloc(m + 1)) != 0  &&
            fread(huge_body, m, 1, fp) == 1) {
            huge_body[m] = '\0';
            CORE_LOGF(eLOG_Note, ("Sending file (%lu bytes)",
                                  (unsigned long) m));
            retval = CORE_SendMail("lavr", "File", huge_body);
            if (retval) {
                CORE_LOGF(eLOG_Fatal, ("Test failed: %s", retval));
            } else {
                CORE_LOG(eLOG_Note, "Test passed");
            }
        } else
            CORE_LOG_ERRNO(eLOG_Error, errno, "Test failed");
        return 0;
    }

#if 1
    CORE_LOG(eLOG_Note, "Phase 1 of 2: Testing CORE_SendMail");

    n = (sizeof(to)/sizeof(to[0]))*
        (sizeof(subject)/sizeof(subject[0]))*
        (sizeof(body)/sizeof(body[0]));
    m = 0;
    for (i = 0; i < sizeof(to)/sizeof(to[0]); i++) {
        for (j = 0; j < sizeof(subject)/sizeof(subject[0]); j++)
            for (k = 0; k < sizeof(body)/sizeof(body[0]); k++) {
                CORE_LOGF(eLOG_Note, ("Test %u of %u",
                                      (unsigned)(++m), (unsigned)n));
                retval = CORE_SendMail(to[i], subject[j], body[k]);
                if (retval != 0)
                    CORE_LOGF(eLOG_Fatal, ("Test failed: %s", retval));
            }
    }
#else
    CORE_LOG(eLOG_Note, "Phase 1 of 2: Skipping CORE_SendMail tests");
#endif

    CORE_LOG(eLOG_Note, "Phase 2 of 2: Testing CORE_SendMailEx");

    SendMailInfo_Init(&info);
    mx_host = info.mx_host;
    mx_port = info.mx_port;
    mx_tmo  = info.mx_timeout;

    info.mx_host = "localhost";

    CORE_LOG(eLOG_Note, "Testing bad port");
    info.mx_port = 10/*BAD*/;
    retval = CORE_SendMailEx("lavr", "CORE_SendMailEx", "Bad port", &info);
    if (!retval)
        CORE_LOG(eLOG_Fatal, "Test failed");
    CORE_LOGF(eLOG_Note, ("Test passed: %s", retval));

    CORE_LOG(eLOG_Note, "Testing bad protocol");
    info.mx_port = CONN_PORT_FTP;
    retval = CORE_SendMailEx("lavr", "CORE_SendMailEx", "Protocol", &info);
    if (!retval)
        CORE_LOG(eLOG_Fatal, "Test failed");
    CORE_LOGF(eLOG_Note, ("Test passed: %s", retval));

    CORE_LOG(eLOG_Note, "Testing timeout");
    info.mx_host = "www.ncbi.nlm.nih.gov";
    info.mx_timeout.sec = 5;
    info.mx_port = CONN_PORT_HTTP;
    retval = CORE_SendMailEx("lavr", "CORE_SendMailEx", "Timeout", &info);
    if (!retval)
        CORE_LOG(eLOG_Error, "Test failed");
    else
        CORE_LOGF(eLOG_Note, ("Test passed: %s", retval));

    info.mx_port = mx_port;
    info.mx_timeout = mx_tmo;

    CORE_LOG(eLOG_Note, "Testing bad host");
    info.mx_host = "abrakadabra";
    retval = CORE_SendMailEx("lavr", "CORE_SendMailEx", "Bad host", &info);
    if (!retval)
        CORE_LOG(eLOG_Fatal, "Test failed");
    CORE_LOGF(eLOG_Note, ("Test passed: %s", retval));

    info.mx_host = "localhost";

    CORE_LOG(eLOG_Note, "Testing cc");
    info.cc = "vakatov";
    retval = CORE_SendMailEx("", "CORE_SendMailEx", "CC", &info);
    if (retval)
        CORE_LOGF(eLOG_Fatal, ("Test failed: %s", retval));
    CORE_LOG(eLOG_Note, "Test passed");

    CORE_LOG(eLOG_Note, "Testing bcc");
    info.cc = 0;
    info.bcc = "vakatov";
    retval = CORE_SendMailEx(0, "CORE_SendMailEx", "Bcc", &info);
    if (retval)
        CORE_LOGF(eLOG_Fatal, ("Test failed: %s", retval));
    CORE_LOG(eLOG_Note, "Test passed");

    CORE_LOG(eLOG_Note, "Testing huge body");
    info.cc = 0;
    info.bcc = 0;
    if (!(huge_body = (char*) malloc(TEST_HUGE_BODY_SIZE)))
        CORE_LOG(eLOG_Fatal, "Test failed: Cannot allocate memory");
    for (i = 0; i < TEST_HUGE_BODY_SIZE - 1; i++)
        huge_body[i] = "0123456789\nABCDEFGHIJKLMNOPQRSTUVWXYZ ."[rand() % 39];
    huge_body[i] = 0;
    retval = CORE_SendMailEx("lavr", "CORE_SendMailEx", huge_body, &info);
    if (retval)
        CORE_LOGF(eLOG_Fatal, ("Test failed: %s", retval));
    if (!(fp = fopen("test_ncbi_sendmail.out", "w")) ||
        fwrite(huge_body, TEST_HUGE_BODY_SIZE - 1, 1, fp) != 1) {
        CORE_LOG(eLOG_Error, "Test failed: Cannot store huge body to file");
    } else {
        fclose(fp);
        CORE_LOG(eLOG_Note, "Success: Check test_ncbi_sendmail.out");
    }
    free(huge_body);

    CORE_LOG(eLOG_Note, "Testing custom headers");
    info.header = "Organization: NCBI/NLM/NIH\nReference: abcdefghijk";
    retval = CORE_SendMailEx("lavr", "CORE_SendMailEx", "Custom header",&info);
    if (retval)
        CORE_LOGF(eLOG_Fatal, ("Test failed: %s", retval));
    CORE_LOG(eLOG_Note, "Test passed");

    CORE_LOG(eLOG_Note, "Testing no recipients");
    retval = CORE_SendMailEx(0, "CORE_SendMailEx", "No recipients", &info);
    if (!retval)
        CORE_LOG(eLOG_Fatal, "Test failed");
    CORE_LOGF(eLOG_Note, ("Test passed: %s", retval));

    CORE_LOG(eLOG_Note, "Testing AS-IS message");
    info.mx_options = fSendMail_NoMxHeader;
    retval = CORE_SendMailEx("lavr",
                             "BAD SUBJECT SHOULD NOT APPEAR BUT IGNORED",
                             "From: yourself\n"
                             "To: yourself\n"
                             "Subject: AS-IS message\n"
                             "\n"
                             "AS-IS",
                             &info);
    if (retval)
        CORE_LOGF(eLOG_Fatal, ("Test failed: %s", retval));
    CORE_LOG(eLOG_Note, "Test passed");

    CORE_LOG(eLOG_Note, "Testing AS-IS custom sized message");
    info.body_size = strlen(custom_body) - 11/*to ignore*/;
    retval = CORE_SendMailEx("<lavr@pavo>",
                             "BAD SUBJECT SHOULD NOT APPEAR BUT IGNORED",
                             custom_body,
                             &info);
    if (retval)
        CORE_LOGF(eLOG_Fatal, ("Test failed: %s", retval));
    CORE_LOG(eLOG_Note, "Test passed");

    info.body_size = 0;
    info.mx_options = 0;
    info.mx_host = mx_host;

    CORE_LOG(eLOG_Note, "Testing bad from");
    strcpy(info.from, "blahblah@blahblah");
    retval = CORE_SendMailEx("lavr", "CORE_SendMailEx", "Bad from",&info);
    if (!retval)
        CORE_LOG(eLOG_Error, "Test failed");
    else
        CORE_LOGF(eLOG_Note, ("Test passed: %s", retval));

    SendMailInfo_Init(&info);
    CORE_LOG(eLOG_Note, "Testing drop no FQDN option");
    info.mx_options |= fSendMail_StripNonFQDNHost;
    retval = CORE_SendMailEx("lavr", "CORE_SendMailEx", "No FQDN", &info);
    if (retval)
        CORE_LOGF(eLOG_Error, ("Test failed: %s", retval));
    else
        CORE_LOG(eLOG_Note, "Test passed");

    CORE_LOG(eLOG_Note, "Testing bad magic");
    info.magic_cookie = 0;
    retval = CORE_SendMailEx("lavr", "CORE_SendMailEx", "Bad Magic", &info);
    if (!retval)
        CORE_LOG(eLOG_Fatal, "Test failed");
    CORE_LOGF(eLOG_Note, ("Test passed: %s", retval));

    CORE_LOG(eLOG_Note, "TEST completed successfully");
    CORE_SetLOG(0);
    return 0;
}
Esempio n. 2
0
/* Main function
 * Parse command-line options, initialize and cleanup API internals;
 * run client or server test
 */
extern int main(int argc, const char* argv[])
{
    /* Setup log stream */
    CORE_SetLOGFormatFlags(fLOG_None          | fLOG_Level   |
                           fLOG_OmitNoteLevel | fLOG_DateTime);
    CORE_SetLOGFILE(stderr, 0/*false*/);

    /* Parse cmd.-line args and decide whether it's a client or a server
     */
    switch ( argc ) {
    case 1:
        /*** 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) );

        SOCK_SetDataLoggingAPI(eOn);
        assert(SOCK_InitializeAPI() == eIO_Success);
        SOCK_SetDataLoggingAPI(eOff);

        {{
            char local_host[64];
            assert(SOCK_gethostname(local_host, sizeof(local_host)) == 0);
            CORE_LOGF(eLOG_Note,
                      ("Running NCBISOCK test on host \"%s\"", local_host));
        }}

        TEST_gethostby();

        TEST_SOCK_isip();

        assert(SOCK_ShutdownAPI() == eIO_Success);

        CORE_SetLOCK(0);
        break;

    case 2: {
        /*** SERVER ***/
        TEST__server(argv[1]);
        assert(SOCK_ShutdownAPI() == eIO_Success);
        CORE_SetLOG(0);
        return 0;
    }

    case 3: case 4: {
        /*** CLIENT ***/
        const char*    host;
        unsigned short port;
        STimeout*      tmo;
        STimeout     x_tmo;

        /* host */
        host = argv[1];

        /* port */
        if (sscanf(argv[2], "%hu", &port) != 1)
            break;

        /* timeout */
        if (argc == 4) {
            double v;
            char*  e = (char*) argv[3];
            if (!*e  ||  (v = NCBI_simple_atof(e, &e)) < 0.0  ||  errno  || *e)
                break;
            x_tmo.sec  = (unsigned int)  v;
            x_tmo.usec = (unsigned int)((v - x_tmo.sec) * 1000000.0);
            tmo        = &x_tmo;
        } else
            tmo = 0/*infinite*/;

        TEST__client(host, port, tmo);
        assert(SOCK_ShutdownAPI() == eIO_Success);
        CORE_SetLOG(0);
        return 0;
    }
    } /* switch */

    /* USAGE */
    fprintf(stderr,
            "\nClient/Server USAGE:\n"
            "Client: %s <host> <port> [timeout]\n"
            "Server: %s <port>\n\n",
            argv[0], argv[0]);
    CORE_SetLOG(0);
    return argc == 1 ? 0 : 1;
}
Esempio n. 3
0
static void TEST__server_1(SOCK sock)
{
    EIO_Status status;
    size_t     n_io, n_io_done;
    char       buf[TEST_BUFSIZE];

    CORE_LOG(eLOG_Note, "TEST__server_1(TS1)");

    /* 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);

    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);

        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 */
    {{
        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
    assert(SOCK_Close   (sock)                == eIO_Success);
}
Esempio n. 4
0
static void TEST__client_1(SOCK sock)
{
    EIO_Status status;
    size_t     n_io, n_io_done;
    char       buf[TEST_BUFSIZE];

    CORE_LOG(eLOG_Note, "TEST__client_1(TC1)");

    /* Send a short string */
    SOCK_SetDataLoggingAPI(eOn);
    {{
        const char* x_C1 = s_C1;
        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)
        CORE_LOG(eLOG_Fatal, "TC1:: connection closed");

    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_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 shut down) */
    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);

    /* 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);
}
Esempio n. 5
0
/* Main function
 * Parse command-line options, initialize and cleanup API internals;
 * run client or server test
 */
extern int main(int argc, char** argv)
{
    /* Setup log stream */
    CORE_SetLOGFormatFlags(fLOG_None          | fLOG_Level   |
                           fLOG_OmitNoteLevel | fLOG_DateTime);
    CORE_SetLOGFILE(stderr, 0/*false*/);

    /* Test client or server using hard-coded parameters */
#if   defined(DO_SERVER)
    argc = 2;
#elif defined(DO_CLIENT)
    argc = 3;
#endif

    /* Parse cmd.-line args and decide whether it's a client or a server
     */
    switch ( argc ) {
    case 1:
        /*** 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) );

        SOCK_SetDataLoggingAPI(eOn);
        assert(SOCK_InitializeAPI() == eIO_Success);
        SOCK_SetDataLoggingAPI(eOff);

        {{
            char local_host[64];
            assert(SOCK_gethostname(local_host, sizeof(local_host)) == 0);
            CORE_LOGF(eLOG_Note,
                      ("Running NCBISOCK test on host \"%s\"", local_host));
        }}

        TEST_gethostby();

        TEST_SOCK_isip();

        assert(SOCK_ShutdownAPI() == eIO_Success);

        CORE_SetLOCK(0);
        break;

    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);
        return 0;
    }

    case 3: case 4: {
        /*** CLIENT ***/
        const char* host;
        int         port;
        STimeout*   tmo;

#if defined(DO_CLIENT)
        host = DEF_HOST;
        port = DEF_PORT;
        tmo  = 0/*infinite*/;
#else
        STimeout    x_tmo;
        /* host */
        host = argv[1];

        /* port */
        if (sscanf(argv[2], "%d", &port) != 1  ||  port < MIN_PORT)
            break;

        /* timeout */
        if (argc == 4) {
            double val = atof(argv[3]);
            if (val < 0)
                break;
            x_tmo.sec  = (unsigned int)  val;
            x_tmo.usec = (unsigned int)((val - x_tmo.sec) * 1000000);
            tmo = &x_tmo;
        } else
            tmo = 0/*infinite*/;
#endif /* DO_CLIENT */

        TEST__client(host, (unsigned short) port, tmo);
        assert(SOCK_ShutdownAPI() == eIO_Success);
        CORE_SetLOG(0);
        return 0;
    }
    } /* switch */

    /* USAGE */
    fprintf(stderr,
            "\nClient/Server USAGE:\n"
            "Client: %s <host> <port> [timeout]\n"
            "Server: %s <port>\n"
            "where <port> is greater than %d, and [timeout] is a double\n\n",
            argv[0], argv[0], MIN_PORT);
    CORE_SetLOG(0);
    return argc == 1 ? 0 : 1;
}