Exemple #1
0
int testListening() {
    Protocol p;
    p.outgoingDataPacket = std::tr1::shared_ptr<ProtocolPacket>(new ProtocolPacket(TYPE_DATA,5,NULL,0));
    p.listen();
    ASSERT(p.state == STATE_LISTENING);
    ASSERT(! p.outgoingDataPacket);
    std::vector<ProtocolPacket> out;
    ProtocolPacket pp = ProtocolPacket(TYPE_CON);
    out = p._packetEvent(pp,50).first;
    ASSERT( p.state == STATE_CONNECTED);
    ASSERT( out.size() == 1);
    ASSERT( out[0].type == TYPE_CONACK);
    return 0;
}
Exemple #2
0
int testAck() {
    char hello[] = "hello";
    Protocol p;
    
    std::vector<ProtocolPacket> out;
    p = Protocol();
    p.state = STATE_CONNECTED;
    p.seqnum = 5;
    p.outgoingDataPacket = std::tr1::shared_ptr<ProtocolPacket>(new ProtocolPacket(TYPE_DATA,5,hello,5));
    
    ProtocolPacket pp = ProtocolPacket(TYPE_ACK,4);
    out = p._packetEvent(pp,8).first;
    ASSERT(p.lastKeepAlive == 0);
    ASSERT(p.seqnum == 5);
    ASSERT(out.size() == 0);
    ASSERT(p.outgoingDataPacket);
    pp = ProtocolPacket(TYPE_ACK,5);
    out = p._packetEvent(pp,8).first;
    ASSERT(p.lastKeepAlive == 8);
    ASSERT(p.seqnum == 6);
    ASSERT(out.size() == 0);
    ASSERT(!(p.outgoingDataPacket));
   
    p = Protocol();
    p.state = STATE_CONNECTED;
    p.seqnum = 5;
    p.outgoingDataPacket = std::tr1::shared_ptr<ProtocolPacket>(new ProtocolPacket(TYPE_DATA,5,hello,5));   
    
    pp = ProtocolPacket(TYPE_DATA,5);
    out = p._packetEvent(pp,8).first;
    ASSERT(p.lastKeepAlive != 8);
    ASSERT(p.seqnum == 5);
    ASSERT(out.size() == 0);
    ASSERT(p.outgoingDataPacket);
    return 0;
}
Exemple #3
0
int testRecoverLost() {
    
    Protocol a;
    Protocol b;
    
    uint64_t t = 0;
    a.listen();
    std::vector<ProtocolPacket> fora = b._connect(t);
    std::vector<ProtocolPacket> forb;
    
    std::vector<ProtocolPacket> out;
    
    std::string arecieved;
    std::string brecieved;
    int asentCount = 0;
    int bsentCount = 0;
    int loopCounter = 0;
    while (loopCounter < 500000) {
        t += 10;
        if (asentCount < 500 && a.readyForData()) {
            out = a._sendData("foo",t);
            forb.insert(forb.end(),out.begin(),out.end());
            asentCount += 1;
        }
           
        if (bsentCount < 1000 && b.readyForData()) {
            out = b._sendData("bar",t);
            fora.insert(fora.end(),out.begin(),out.end());
            bsentCount += 1;
        }
        
        if (loopCounter > 10) { //start dropping packets after _connection is up
            if  (((loopCounter % 8) == 0) && fora.size() ) {
                fora.pop_back();
            }
            
            if (((loopCounter % 7) == 0) && forb.size()){
                forb.pop_back();
            }
        }
        
        // drop some more packets by not accepting data arbitrarily
        for (std::vector<ProtocolPacket>::iterator it = fora.begin() ; it != fora.end() ; it++ ) {
            std::pair<std::vector<ProtocolPacket>,std::vector<uint8_t> > eventResult;
            eventResult = a._packetEvent(*it,t,true);
            out = eventResult.first;
            std::string gotData(eventResult.second.begin(),eventResult.second.end());
            arecieved += gotData;
            forb.insert(forb.end(),out.begin(),out.end());
           
        }
        fora.clear();
        for (std::vector<ProtocolPacket>::iterator it = forb.begin() ; it != forb.end() ; it++ ) {
            std::pair<std::vector<ProtocolPacket>,std::vector<uint8_t> > eventResult;
            eventResult = b._packetEvent(*it,t,true);
            out = eventResult.first;
            std::string gotData(eventResult.second.begin(),eventResult.second.end());
            brecieved += gotData;
            fora.insert(fora.end(),out.begin(),out.end());
        }
        forb.clear();
        
        out = a._timerEvent(t);
        forb.insert(forb.end(),out.begin(),out.end());
        out = b._timerEvent(t);
        fora.insert(fora.end(),out.begin(),out.end());
        
        if (asentCount == 500 && bsentCount == 1000 && !a.outgoingDataPacket && !b.outgoingDataPacket) {
            break;
        }
        
        loopCounter += 1;
    }
    ASSERT(a.state == STATE_CONNECTED);
    ASSERT(b.state == STATE_CONNECTED);
    ASSERT(loopCounter != 500000);
    ASSERT(arecieved.size() == 1000*3);
    for(int i = 0 ; i < 1000; i++){
        ASSERT(arecieved.substr(i*3,3) == std::string("bar"));
    }
    for(int i = 0 ; i < 500; i++){
        ASSERT(brecieved.substr(i*3,3) == std::string("foo"));
    }    
    return 0;
}
Exemple #4
0
int testConnect() {
    
    Protocol a;
    Protocol b;
    
    uint64_t t = 0;
    a.listen();
    std::vector<ProtocolPacket> fora = b._connect(t);
    std::vector<ProtocolPacket> forb;
    
    std::vector<ProtocolPacket> out;
    
    std::set<PacketType> packetTypes;
    
    for( int i = 0;  i < 1000 ; i++) {
        for (std::vector<ProtocolPacket>::iterator it = fora.begin() ; it != fora.end() ; it++ ) {
            packetTypes.insert(it->type);
            out = a._packetEvent(*it,t).first;
            forb.insert(forb.end(),out.begin(),out.end()); 
        }
        fora.clear();
        for (std::vector<ProtocolPacket>::iterator it = forb.begin() ; it != forb.end() ; it++ ) {
            packetTypes.insert(it->type);
            out = b._packetEvent(*it,t).first;
            fora.insert(fora.end(),out.begin(),out.end());
        }
        forb.clear();
        
        out = a._timerEvent(t);
        forb.insert(forb.end(),out.begin(),out.end());
        out = b._timerEvent(t);
        fora.insert(fora.end(),out.begin(),out.end());
        
        t += 10;
    }
    
    ASSERT(a.state == STATE_CONNECTED);
    ASSERT(b.state == STATE_CONNECTED);
    
    ASSERT(packetTypes.find(TYPE_CON) != packetTypes.end());
    ASSERT(packetTypes.find(TYPE_CONACK) != packetTypes.end());
    ASSERT(packetTypes.find(TYPE_PING) != packetTypes.end());
    
    for ( int i = 0; i < 10000 ; i++) {
        a._timerEvent(t);
        b._timerEvent(t);
        t += 10;
    }
    ASSERT(a.state == STATE_UNINIT);
    ASSERT(b.state == STATE_UNINIT);
    
    b._connect(t);
    ASSERT(b.state == STATE_CONNECTING);
    b._timerEvent(t);
    ASSERT(b.state == STATE_CONNECTING);
    for(int i = 0; i < 10000 ; i++) {
        b._timerEvent(t);
        t += 10;
    }
    ASSERT(b.state == STATE_UNINIT);
    
    return 0;
    
}