@@ -2324,6 +2324,33 @@ TORRENT_TEST(set_pad_bytes)
23242324 TEST_EQUAL (blocks[3 ].state , piece_picker::block_info::state_finished);
23252325}
23262326
2327+ TORRENT_TEST (set_pad_bytes_short_last_piece)
2328+ {
2329+ // the last piece is shorter than a full piece (2 blocks instead of 4)
2330+ // and its last block is a pad block. add_download_piece() must use the
2331+ // actual number of blocks in this short piece, not the nominal
2332+ // per-piece block count, to tell payload blocks from pad blocks.
2333+ auto p = std::make_shared<piece_picker>(
2334+ std::int64_t (default_piece_size) + 2 * default_block_size, default_piece_size);
2335+ p->set_pad_bytes (1_piece, default_block_size);
2336+
2337+ bool const ret = p->mark_as_downloading ({1_piece, 0 }, tmp_peer);
2338+ TEST_EQUAL (ret, true );
2339+
2340+ auto const dl = p->get_download_queue ();
2341+
2342+ TEST_EQUAL (dl.size (), 1 );
2343+ TEST_EQUAL (dl[0 ].finished , 1 );
2344+ TEST_EQUAL (dl[0 ].writing , 0 );
2345+ TEST_EQUAL (dl[0 ].requested , 1 );
2346+ TEST_EQUAL (dl[0 ].index , 1_piece);
2347+
2348+ auto const blocks = p->blocks_for_piece (dl[0 ]);
2349+ TEST_EQUAL (blocks.size (), 2 );
2350+ TEST_EQUAL (blocks[0 ].state , piece_picker::block_info::state_requested);
2351+ TEST_EQUAL (blocks[1 ].state , piece_picker::block_info::state_finished);
2352+ }
2353+
23272354TORRENT_TEST (set_pad_bytes_overflow)
23282355{
23292356 int const ps = file_storage::max_piece_size;
0 commit comments