Ejemplo n.º 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;
    }
Ejemplo n.º 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;
 }
Ejemplo n.º 3
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
}