Ejemplo n.º 1
0
 inline void MockExecLoop(SignalCounter &sc, int interval = 0)
 {
   while(true) {
     QCoreApplication::processEvents();
     QCoreApplication::sendPostedEvents();
     if(sc.GetCount() == sc.Max()) {
       return;
     }
     Sleeper::MSleep(interval);
   }
 }
Ejemplo n.º 2
0
inline void RunUntil(const SignalCounter &sc, int count)
{
  Time &time = Time::GetInstance();
  Timer &timer = Timer::GetInstance();
  while(sc.GetCount() != count) {
    time.IncrementVirtualClock(timer.VirtualRun());
  }
}
Ejemplo n.º 3
0
  void TestRoundBad(CreateRound good_cr, CreateRound bad_cr,
      const BadGuyCB &callback, bool client, bool will_finish)
  {
    int servers = 3, clients = 10;
    ConnectionManager::UseTimer = false;
    Timer::GetInstance().UseVirtualTime();
    OverlayNetwork net = ConstructOverlay(servers, clients);
    VerifyStoppedNetwork(net);
    StartNetwork(net);
    VerifyNetwork(net);

    Sessions sessions = BuildSessions(net, good_cr);
    // Find a bad guy and replace him...
    int badguy = Random::GetInstance().GetInt(0, clients);
    Id badid = net.second[badguy]->GetId();

    if(!client) {
      badguy = Random::GetInstance().GetInt(0, servers);
      badid = net.first[badguy]->GetId();
    }

    qDebug() << "Bad guy at" << badguy << badid;
    QSharedPointer<AsymmetricKey> key =
      sessions.private_keys[badid.ToString()];

    if(client) {
      ClientPointer cs = MakeSession<ClientSession>(
          net.second[badguy], key, sessions.keys, bad_cr);
      cs->SetSink(sessions.sink_multiplexers[servers + badguy].data());
      sessions.clients[badguy] = cs;
    } else {
      ServerPointer ss = MakeSession<ServerSession>(
          net.first[badguy], key, sessions.keys, bad_cr);
      ss->SetSink(sessions.sink_multiplexers[badguy].data());
      sessions.servers[badguy] = ss;
    }

    // Find a sender != badguy
    int sender = Random::GetInstance().GetInt(0, clients);
    if(client) {
      while(sender == badguy) {
        sender = Random::GetInstance().GetInt(0, clients);
      }
    }
    QByteArray msg(64, 0);
    CryptoRandom().GenerateBlock(msg);
    sessions.clients[sender]->Send(msg);

    qDebug() << "Starting sessions...";
    StartSessions(sessions);
    StartRound(sessions);

    QSharedPointer<Round> bad_round;
    if(client) {
      bad_round = sessions.clients[badguy]->GetRound();
    } else {
      bad_round = sessions.servers[badguy]->GetRound();
    }

    SignalCounter sc;
    for(int idx = 0; idx < servers; idx++) {
      QObject::connect(sessions.servers[idx].data(),
          SIGNAL(RoundFinished(const QSharedPointer<Anonymity::Round> &)),
          &sc, SLOT(Counter()));
    }

    for(int idx = 0; idx < clients; idx++) {
      QObject::connect(sessions.clients[idx].data(),
          SIGNAL(RoundFinished(const QSharedPointer<Anonymity::Round> &)),
          &sc, SLOT(Counter()));
    }

    RunUntil(sc, clients + servers);
    if(will_finish) {
      ASSERT_EQ(sc.GetCount(), clients + servers);
      ASSERT_EQ(bad_round->GetBadMembers().size(), 1);
      ASSERT_EQ(badid, bad_round->GetBadMembers()[0]);
    }

    if(!callback(bad_round.data())) {
      std::cout << "RoundTest_BadGuy was never triggered, "
        "consider rerunning." << std::endl;
    }

    StopNetwork(sessions.network);
    VerifyStoppedNetwork(sessions.network);
    ConnectionManager::UseTimer = true;
  }
Ejemplo n.º 4
0
  void RoundTest_MultiRound(CreateSessionCallback callback, CreateGroupGenerator cgg)
  {
    Timer::GetInstance().UseVirtualTime();

    int count = Random::GetInstance().GetInt(TEST_RANGE_MIN, TEST_RANGE_MAX);
    int leader = Random::GetInstance().GetInt(0, count);
    int sender0 = Random::GetInstance().GetInt(0, count);
    int sender1 = Random::GetInstance().GetInt(0, count);
    while(sender0 != sender1) {
      sender1 = Random::GetInstance().GetInt(0, count);
    }

    QVector<TestNode *> nodes;
    Group *group;
    ConstructOverlay(count, nodes, group);

    Id leader_id = nodes[leader]->cm.GetId();
    Id session_id;

    CreateSessions(nodes, *group, leader_id, session_id, callback, cgg);

    Library *lib = CryptoFactory::GetInstance().GetLibrary();
    QScopedPointer<Dissent::Utils::Random> rand(lib->GetRandomNumberGenerator());

    QByteArray msg(512, 0);
    rand->GenerateBlock(msg);
    nodes[sender0]->session->Send(msg);

    SignalCounter sc;
    for(int idx = 0; idx < count; idx++) {
      QObject::connect(&nodes[idx]->sink, SIGNAL(DataReceived()), &sc, SLOT(Counter()));
      nodes[idx]->session->Start();
    }

    TestNode::calledback = 0;
    qint64 next = Timer::GetInstance().VirtualRun();
    while(next != -1 && sc.GetCount() < count && TestNode::calledback < count) {
      Time::GetInstance().IncrementVirtualClock(next);
      next = Timer::GetInstance().VirtualRun();
    }

    sc.Reset();

    for(int idx = 0; idx < count; idx++) {
      EXPECT_EQ(msg, nodes[idx]->sink.Last().first);
    }

    rand->GenerateBlock(msg);
    nodes[sender1]->session->Send(msg);

    TestNode::calledback = 0;
    next = Timer::GetInstance().VirtualRun();
    while(next != -1 && sc.GetCount() < count && TestNode::calledback < count * 2) {
      Time::GetInstance().IncrementVirtualClock(next);
      next = Timer::GetInstance().VirtualRun();
    }

    for(int idx = 0; idx < count; idx++) {
      EXPECT_EQ(msg, nodes[idx]->sink.Last().first);
    }

    CleanUp(nodes);
    delete group;
  }