示例#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
  // @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);
  }
示例#6
0
 @Subscribe
 public void peerListRefreshed(PeerListChangedEvent event) {
   System.out.println("Peer list refresh");
   assertNull(peers);
   peers = client.getPeers();
   assertNotNull(peers);
   lock.countDown();
 }
示例#7
0
 @Subscribe
 public void metaInfoLoaded(MetaInfoLoadedEvent event) {
   System.out.println("Meta Info Loaded");
   assertNull(metaInfo);
   metaInfo = client.getMetaInfo();
   assertNotNull(metaInfo);
   lock.countDown();
 }
示例#8
0
  // @Test
  public void clientShouldStart()
      throws InterruptedException, THPRequestException, HashException, CannotUnserializeException,
          CannotReadTokenException, CannotReadBencodedException, CannotReadMessageException {
    lock = new CountDownLatch(2);
    initGuice();
    initEventBus();
    initTorrentStream();
    initClient();

    client.configure(
        ClientSettings.settingsBuilder()
            .basePath(Paths.get(this.getClass().getResource("").getPath()))
            .metainfoSource(torrentStream));

    client.start();

    while (true) {
      Thread.sleep(200);
    }
  }
示例#9
0
  // @Test
  public void clientShouldLoadPeerList()
      throws CannotUnserializeException, CannotReadTokenException, CannotReadBencodedException,
          THPRequestException, HashException, InterruptedException {
    lock = new CountDownLatch(2);

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

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

    System.out.println("Async");
    lock.await(10, TimeUnit.SECONDS);
    assertNotNull(peers);
    assertFalse(peers.isEmpty());

    System.out.println("The client returned " + peers.size() + " peers : " + peers);
  }
示例#10
0
  // @Test
  public void clientShouldLoadMetaInfo()
      throws CannotUnserializeException, CannotReadTokenException, CannotReadBencodedException,
          InterruptedException {
    lock = new CountDownLatch(1);
    initGuice();
    initEventBus();
    initTorrentStream();

    client.loadMetaInfo(torrentStream);
    System.out.println("Async");
    lock.await(2, TimeUnit.SECONDS);
    assertNotNull(metaInfo);
  }
示例#11
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));
  }
示例#12
0
 private void initClient() {
   client.setListenPort(6669);
 }