示例#1
0
    /**
     * Start a client and server and send 2 messages over TCP thru them
     * 
     * NOTE: This can/will fail if /etc/hosts defines localhost as ::1 (as 
     *       opposed to 127.0.0.1).
     */
    void testWriteAndAcceptMsg()
    {
    	// Create/Verify Sockets
        OsServerSocket* server = new OsServerSocket(50, 8021);
        KNOWN_BUG("This can fail if there is a port conflict on the test system", "XECS-1924");
        CPPUNIT_ASSERT_MESSAGE("server socket failure", 
                               server->isOk());
        
        OsSocket* client = new OsConnectionSocket(8021, "localhost");        
        CPPUNIT_ASSERT_MESSAGE("client socket failure", 
                               client->isOk());
                
        OsSocket* serverClient = server->accept(1000);
        CPPUNIT_ASSERT_MESSAGE("socket server failed to accept connection", 
                               serverClient != NULL);

        // Begin read/write test
        const char* msg = "hello\n";
        int len = strlen(msg) + 1; // +1 for NULL
        int bytesWritten = client->write(msg, len);
        CPPUNIT_ASSERT_EQUAL_MESSAGE("write correct number of bytes", 
                bytesWritten, len);

        char recvBuf[1024];
        int bytesRead = serverClient->read(recvBuf, sizeof(recvBuf) - 1);
        CPPUNIT_ASSERT_EQUAL_MESSAGE("read correct number of bytes", 
                len, bytesRead);
        ASSERT_STR_EQUAL_MESSAGE("message same as was sent", msg, recvBuf);

        const char *resp = "bye";
        len = strlen(resp) + 1; // +1 for NULL
        bytesWritten = serverClient->write(resp, len);

        CPPUNIT_ASSERT_EQUAL_MESSAGE("write correct number of bytes on 2nd msg", 
            len, bytesWritten);

        bytesRead = client->read(recvBuf, sizeof(recvBuf) - 1);
        CPPUNIT_ASSERT_EQUAL_MESSAGE("read correct number of bytes on 2nd msg", 
            len, bytesRead);

        CPPUNIT_ASSERT_EQUAL_MESSAGE("write correct number of bytes on 2nd msg", 
            len, bytesWritten);

        ASSERT_STR_EQUAL_MESSAGE("2nd message same as was sent", 
                resp, recvBuf);

        serverClient->close();
        client->close();
        server->close();

        delete client;
        delete server;
    }
示例#2
0
 /**
  * Open datagram socket and send a few bytes.
  * 
  * NOTE: This can/will fail if /etc/hosts defines localhost as ::1 (as 
  *       opposed to 127.0.0.1).
  */
 void testWriteMsg()
 {
     OsSocket* s = new OsDatagramSocket(8020, "localhost");
     const char* msg = "hello\n";
     int len = strlen(msg);
     int bytesWritten = s->write(msg, len);
     CPPUNIT_ASSERT_EQUAL_MESSAGE("write correct number of bytes", 
         bytesWritten, len);
     s->close();
     delete s;
 }
示例#3
0
int SipLSendCommand::execute(int argc, char* argv[])
{
        int commandStatus = CommandProcessor::COMMAND_FAILED;
        UtlString messageBuffer;
        char buffer[1025];
        int bufferSize = 1024;
        int charsRead;

        printf("send command with %d arguments\n", argc);
        if(argc != 5)
        {
                UtlString usage;
                getUsage(argv[0], &usage);
                printf("%s", usage.data());
        }

        else
        {
        UtlString protocol(argv[2]);
        protocol.toUpper();

        UtlString hostAddress(argv[3]);
        int hostPort = atoi(argv[4]);

        FILE* sipMessageFile = fopen(argv[1], "r");
                if(sipMessageFile && hostPort > 0 && !hostAddress.isNull() &&
            (protocol.compareTo("TCP") == 0 || protocol.compareTo("UDP") == 0))
                {
                        //printf("opened file: \"%s\"\n", argv[1]);
                        do
                        {
                                charsRead = fread(buffer, 1, bufferSize, sipMessageFile);
                                if(charsRead > 0)
                                {
                                        messageBuffer.append(buffer, charsRead);
                                }
                        }
                        while(charsRead);
            fclose(sipMessageFile);

            OsSocket* writeSocket = NULL;
            if(protocol.compareTo("TCP") == 0)
                writeSocket = new OsConnectionSocket(hostPort ,hostAddress);
            else if(protocol.compareTo("UDP") == 0)
                writeSocket = new OsDatagramSocket(hostPort ,hostAddress);

                        //printf("Read file contents:\n%s\n====END====\n", messageBuffer.data());
                        //SipMessage message(messageBuffer.data());

            int bytesSent;
                        if((bytesSent = writeSocket->write(messageBuffer.data(),
                                                                                        messageBuffer.length())) > 0)
                        {
                                commandStatus = CommandProcessor::COMMAND_SUCCESS;
                        }
                        else
                        {
                                printf("Failed to send SIP message");
                        }
            printf("Send message with %d bytes\n", bytesSent);
                }
                else if(!sipMessageFile)
                {
                        printf("send file: \"%s\" does not exist\n", argv[1]);
                        commandStatus = CommandProcessor::COMMAND_FAILED;
                }
        else if(hostPort <= 0)
        {
                        printf("Invalid destination port: %s\n", argv[4]);
                        commandStatus = CommandProcessor::COMMAND_FAILED;
                }
        else if(hostAddress.isNull())
        {
                        printf("Invalid destination address: %s\n", argv[3]);
                        commandStatus = CommandProcessor::COMMAND_FAILED;
                }
        else if(protocol.compareTo("TCP")  && protocol.compareTo("UDP"))
        {
                        printf("Invalid protocol: %s\n", argv[2]);
                        commandStatus = CommandProcessor::COMMAND_FAILED;
                }

        }

        return(commandStatus);
}
示例#4
0
main(int argc, char* argv[])
#endif
{
        PT_TEST_BEGIN;

        OsSocket* s = new OsDatagramSocket(8020, "localhost");
        char* msg = "hello\n";
        int len = strlen(msg);
        int bytesWritten = s->write(msg, len);

        char recvBuf[RECV_BUF_LEN];

        char eMsg[100];
        char* resp;
        OsServerSocket* server = NULL;
        OsSocket* client = NULL;
        OsSocket* serverClient = NULL;
        int bytesRead;

        printf("wrote UDP %d of %d bytes\n", bytesWritten, len);
        PT_TEST_ASSERT(len == bytesWritten,
                "OsDatagramSOcket::write failed to write correct number of bytes",
                PT_TEST_CONTINUE);


        s->close();
        delete s;

        server = new OsServerSocket(50, 8080);
        client = new OsConnectionSocket(8080, "localhost");
    serverClient = server->accept();
        PT_TEST_ASSERT(serverClient != NULL,
                "socket server failed to accept connection",
                PT_TEST_ABORT);

    bytesWritten = client->write(msg, len);
        sprintf(eMsg, "wrote TCP %d of %d bytes", bytesWritten, len);
        PT_TEST_ASSERT(len == bytesWritten, eMsg, PT_TEST_CONTINUE);

        bytesRead = serverClient->read(recvBuf, RECV_BUF_LEN - 1);
        if(bytesRead > 0)
        {
                recvBuf[bytesRead] = '\0';
                printf("received %d of %d butes: \"%s\"\n", bytesRead, len, recvBuf);
        }
        PT_TEST_ASSERT(len == bytesRead, eMsg, PT_TEST_CONTINUE);
        PT_TEST_ASSERT(strcmp(msg, recvBuf) == 0, "message is not the same as was sent",
                                PT_TEST_CONTINUE);

        resp = "bye";
        len = strlen(resp);
        bytesWritten = serverClient->write(resp, len);
                sprintf(eMsg, "wrote TCP %d of %d bytes", bytesWritten, len);
        PT_TEST_ASSERT(len == bytesWritten, eMsg, PT_TEST_CONTINUE);
        bytesRead = client->read(recvBuf, RECV_BUF_LEN - 1);
        if(bytesRead > 0)
        {
                recvBuf[bytesRead] = '\0';
                printf("received %d of %d butes: \"%s\"\n", bytesRead, len, recvBuf);
        }
        PT_TEST_ASSERT(len == bytesRead, eMsg, PT_TEST_CONTINUE);
        PT_TEST_ASSERT(strcmp(resp, recvBuf) == 0, "response is not the same as was sent",
                                PT_TEST_CONTINUE);

        serverClient->close();
        client->close();
        server->close();
        delete serverClient;
        delete client;
        delete server;


        // Note: no semicolon
        PT_TEST_END
}