def test_piece_one_block(self): piece = Piece(fake_client, 1, 2**14, 'abc', test_dir) self.assertEqual(piece.num_blocks, 1) self.assertTrue(piece.not_all_blocks_requested()) block_info = piece.get_next_block() self.assertEqual(block_info[0], 1) self.assertEqual(block_info[1], 0) self.assertEqual(block_info[2], 2**14) self.assertFalse(piece.not_all_blocks_requested())
def test_piece_two_blocks(self): piece = Piece(fake_client, 1, 2**14 + 2, 'abcd', test_dir) self.assertEqual(piece.num_blocks, 2) self.assertTrue(piece.not_all_blocks_requested()) block = piece.get_next_block() self.assertTrue(piece.not_all_blocks_requested()) block = piece.get_next_block() self.assertEqual(block[1], 2**14, '2nd block should begin at byte 2**14') self.assertEqual(block[2], 2, '2nd block should have length 2') self.assertFalse(piece.not_all_blocks_requested())
def test_piece_one_short_block(self): piece = Piece(fake_client, 1, 2, 'abcd', test_dir) self.assertEqual(piece.num_blocks, 1) self.assertTrue(piece.not_all_blocks_requested()) block = piece.get_next_block() self.assertEqual(block[2], 2) (block_info, peer) = piece.get_next_block_and_peer_to_request() self.assertEqual(peer, None) self.assertEqual(block_info[0], 1) self.assertEqual(block_info[1], 0) self.assertEqual(block_info[2], 2**14) self.assertFalse(piece.not_all_blocks_requested())
def test_piece_info_hash(self): test_bytes = '\x00\x01\x02' test_hash = hashlib.sha1(test_bytes).digest() piece = Piece(fake_client, 1, 3, test_hash, test_dir) piece.write_file = StringIO() self.assertEqual(piece.num_blocks, 1) self.assertFalse(piece.check_if_finished()) self.assertTrue(piece.not_all_blocks_requested()) piece.add_block(0, test_bytes) self.assertTrue(piece.check_if_finished()) self.assertTrue(piece.check_info_hash())