示例#1
0
  // @Test(expected = IllegalStateException.class)
  public void peerShouldReadBitField()
      throws InterruptedException, THPRequestException, HashException, CannotUnserializeException,
          CannotReadTokenException, CannotReadBencodedException, CannotReadMessageException {
    lock = new CountDownLatch(2);
    initGuice();
    initEventBus();
    initTorrentStream();
    initClient();

    client.loadMetaInfo(torrentStream);
    client.refreshPeerList();

    System.out.println("Refreshing peer list");
    lock.await(30, TimeUnit.SECONDS);
    assertNotNull(peers);
    assertFalse(peers.isEmpty());

    Peer testedPeer = peers.iterator().next();

    byte[] payload = new byte[metaInfo.getInformation().getPieceCount()];
    Arrays.fill(payload, (byte) 0xFF);
    testMessageType(testedPeer, MessageType.BITFIELD, new BitFieldMessage(payload));
    assertTrue(testedPeer.isSeeder());

    Arrays.fill(payload, (byte) 0);
    testMessageType(testedPeer, MessageType.BITFIELD, new BitFieldMessage(payload));
  }
示例#2
0
  // @Test
  public void peerShouldHaveConsistentPieceCount()
      throws InterruptedException, THPRequestException, HashException, CannotUnserializeException,
          CannotReadTokenException, CannotReadBencodedException {
    lock = new CountDownLatch(2);
    initGuice();
    initEventBus();
    initTorrentStream();
    initClient();

    client.loadMetaInfo(torrentStream);
    client.refreshPeerList();

    System.out.println("Refreshing peer list");
    lock.await(30, TimeUnit.SECONDS);
    assertNotNull(peers);
    assertFalse(peers.isEmpty());

    Peer testedPeer = peers.iterator().next();

    expectedMessageReceived = false;
    expectedMessageType = MessageType.HANDSHAKE;
    lock = new CountDownLatch(2);
    testedPeer.connect();
    lock.await(30, TimeUnit.SECONDS);
    assertTrue(testedPeer.getPeerId() != null);
    assertEquals(metaInfo.getInformation().getPieceCount(), testedPeer.getExpectedPieceCount());
  }
示例#3
0
  // @Test
  public void peerShouldTransmitEvent()
      throws InterruptedException, CannotReadMessageException, THPRequestException, HashException,
          CannotUnserializeException, CannotReadTokenException, CannotReadBencodedException {
    lock = new CountDownLatch(2);
    initGuice();
    initEventBus();
    initTorrentStream();
    initClient();

    client.loadMetaInfo(torrentStream);
    client.refreshPeerList();

    System.out.println("Refreshing peer list");
    lock.await(30, TimeUnit.SECONDS);
    assertNotNull(peers);
    assertFalse(peers.isEmpty());

    Peer testedPeer = peers.iterator().next();

    testMessageType(testedPeer, MessageType.CHOKE, new ChokeMessage(new byte[1]));
    assertTrue(testedPeer.isChokingUs());

    testMessageType(testedPeer, MessageType.UNCHOKE, new UnChokeMessage(new byte[1]));
    assertFalse(testedPeer.isChokingUs());

    testMessageType(testedPeer, MessageType.INTERESTED, new InterestedMessage(new byte[1]));
    assertTrue(testedPeer.isInterestedInUs());

    testMessageType(testedPeer, MessageType.NOT_INTERESTED, new NotInterestedMessage(new byte[1]));
    assertFalse(testedPeer.isInterestedInUs());
  }
示例#4
0
  // @Test
  public void clientShouldConnectToPeer()
      throws CannotUnserializeException, CannotReadTokenException, CannotReadBencodedException,
          THPRequestException, HashException, InterruptedException {
    lock = new CountDownLatch(2);

    initGuice();
    initEventBus();
    initTorrentStream();
    initClient();

    client.loadMetaInfo(torrentStream);
    client.refreshPeerList();

    System.out.println("Refreshing peer list");
    lock.await(30, TimeUnit.SECONDS);
    assertNotNull(peers);
    assertFalse(peers.isEmpty());

    lock = new CountDownLatch(1);
    // We take the first peer of the list
    Peer testedPeer = peers.iterator().next();
    client.connectToPeer(testedPeer);
    lock.await(30, TimeUnit.SECONDS);

    assertNotNull(testedPeer.getPeerWire());
    System.out.println("A new peerwire has been created for " + peers);
  }
示例#5
0
 private void testMessageType(
     final Peer testedPeer, final MessageType messageType, Message message)
     throws InterruptedException {
   expectedMessageType = messageType;
   expectedMessageReceived = false;
   lock = new CountDownLatch(1);
   testedPeer.messageReceived(message);
   lock.await(30, TimeUnit.SECONDS);
   assertTrue(expectedMessageReceived);
 }
示例#6
0
  // @Test
  public void peerShouldReadHave()
      throws InterruptedException, THPRequestException, HashException, CannotUnserializeException,
          CannotReadTokenException, CannotReadBencodedException {
    lock = new CountDownLatch(2);
    initGuice();
    initEventBus();
    initTorrentStream();
    initClient();

    client.loadMetaInfo(torrentStream);
    client.refreshPeerList();

    System.out.println("Refreshing peer list");
    lock.await(30, TimeUnit.SECONDS);
    assertNotNull(peers);
    assertFalse(peers.isEmpty());

    Peer testedPeer = peers.iterator().next();

    testMessageType(testedPeer, MessageType.HAVE, new HaveMessage(42));
    assertFalse(testedPeer.isSeeder());
    assertTrue(testedPeer.hasPiece(42));
  }
示例#7
0
  // @Test
  public void peerShouldDownloadBlock()
      throws InterruptedException, THPRequestException, HashException, CannotUnserializeException,
          CannotReadTokenException, CannotReadBencodedException, CannotReadMessageException {
    lock = new CountDownLatch(2);
    initGuice();
    initEventBus();
    initTorrentStream();
    initClient();

    client.loadMetaInfo(torrentStream);
    client.refreshPeerList();

    System.out.println("Refreshing peer list");
    lock.await(30, TimeUnit.SECONDS);
    assertNotNull(peers);
    assertFalse(peers.isEmpty());

    expectedMessageType = MessageType.PIECE;
    expectedMessageReceived = false;
    atLastOneExpected = true;
    lock = new CountDownLatch(1);

    for (Peer testedPeer : peers) {
      testedPeer.connect();

      // We have to wait for a while until the peer has been connected to and initialized
      Thread.sleep(15000);
      // We test if the peer has some pieces
      BitSet havePieces = testedPeer.getAvailablePieces();

      if (havePieces.cardinality() > 0) {
        assertNotEquals(0, havePieces.cardinality());

        // We request the first available piece
        int pieceIndex = havePieces.nextSetBit(0);
        assertTrue(pieceIndex >= 0);

        // We need to unchoke and signal our interest to the peer first
        testedPeer.unchoke();
        testedPeer.interested();

        if (!testedPeer.isChokingUs()) {
          // We can now request
          testedPeer.requestPieceBlock(pieceIndex, 0);
        }
      }
    }

    // Now that the piece has been requested we need to wait for a block message
    lock.await();
    assertTrue(expectedMessageReceived);
  }