Exemple #1
0
  @Test
  public void testWriteBytes() {
    /** input file bigger than the array */
    byte[] rawBits = new byte[100];

    byte[] raw2 = new byte[100];

    try {
      InputStream in = f.getFile(0);
      int read = in.read(rawBits);

      /** write the same file back */
      f.writeBytes(0, rawBits, read);

      in = f.getFile(0);
      int read2 = in.read(raw2);

      /** make sure everything is the same */
      assertTrue(read == read2);
      Arrays.equals(rawBits, raw2);
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      fail();
    } catch (IncompleteFileException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      fail();
    }
  }
Exemple #2
0
  @Test
  public void testGetRawBitfield() {
    byte[] rawBits = f.getRawBitfield();

    int expectedSize = (int) Math.ceil(tf.getNumOfPieces() / 8.0);

    assertTrue(rawBits.length == expectedSize);
    assertTrue(rawBits[0] == completeBitfield);
  }
Exemple #3
0
  @Test
  public void testConstructor() {
    try {
      TorrentFile torrentFile = new TorrentFile(torrent);
      FileAccess fileAccess = new FileAccess(baseDir, torrentFile, null, true);

      fileAccess.getRawBitfield();
    } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (ParseException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
Exemple #4
0
  @Test
  public void testGetFile() {
    byte[] msg = new byte[64];
    try {
      InputStream in = f.getFile(0);
      /*
       * TODO (FindBugs)
       *
       * [RR] Method ignores results of InputStream.read()
       * [RR_NOT_CHECKED]
       *
       * This method ignores the return value of one of the variants of
       * java.io.InputStream.read() which can return multiple bytes. If
       * the return value is not checked, the caller will not be able to
       * correctly handle the case where fewer bytes were read than the
       * caller requested. This is a particularly insidious kind of bug,
       * because in many programs, reads from input streams usually do
       * read the full amount of data requested, causing the program to
       * fail only sporadically.
       */
      in.read(msg);
      System.out.println(new String(msg, "UTF-8"));

      in = f.getFile(1);
      in.read(msg);
      System.out.println(new String(msg, "UTF-8"));

      in = f.getFile(2);
      in.read(msg);
      System.out.println(new String(msg, "UTF-8"));
    } catch (IncompleteFileException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      fail();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      fail();
    }
  }
Exemple #5
0
 @Test
 public void testHavePiece() {
   assertTrue(f.havePiece(0));
 }
Exemple #6
0
 @Test
 public void testComplete() {
   assertTrue(f.complete());
 }
Exemple #7
0
 @Test
 public void testNumOfCompletePieces() {
   // only 1 piece
   assertTrue(f.numOfCompletePieces() == 1);
 }