File: test_reuse.py

package info (click to toggle)
torf 4.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 988 kB
  • sloc: python: 10,054; makefile: 15; sh: 8
file content (735 lines) | stat: -rw-r--r-- 29,898 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
import collections
import copy
import errno
import os
import re
from types import SimpleNamespace
from unittest.mock import MagicMock, Mock, call

import pytest

import torf

from . import ComparableException


@pytest.fixture(autouse=True)
def ordered_listdir(mocker):
    def ordered_listdir(*args, _real_listdir=os.listdir, **kwargs):
        return sorted(_real_listdir(*args, **kwargs))
    mocker.patch('os.listdir', ordered_listdir)


@pytest.fixture
def existing_torrents(create_dir, create_file, tmp_path):
    class ExistingTorrents:
        def __init__(self, **torrent_directories):
            self._torrents = {}
            for dirname, info in torrent_directories.items():
                self._torrents[dirname] = self._create_torrents(dirname, *info)

            # Sprinkle in some non-torrent files
            for dirname in self._torrents:
                (tmp_path / dirname / 'foo.jpg').write_bytes(b"Ceci n'est pas une JPEG")
                (tmp_path / dirname / 'foo.txt').write_text('But this looks like text')

        @staticmethod
        def _create_torrents(directory, *items):
            torrents_directory = tmp_path / directory
            torrents_directory.mkdir(exist_ok=True)
            torrents = []
            for item in items:
                torrent_name = item[0]
                create_args = item[1]
                torrent_kwargs = item[2]
                if isinstance(create_args, collections.abc.Sequence) and not isinstance(create_args, str):
                    content_path = create_dir(torrent_name, *create_args)
                else:
                    content_path = create_file(torrent_name, create_args)
                torrent = torf.Torrent(path=content_path, **torrent_kwargs)
                torrent_filepath = torrents_directory / f'{torrent_name}.torrent'
                # Add some non-standard fields into each file list
                if 'files' in torrent.metainfo['info']:
                    for i in range(len(torrent.metainfo['info']['files'])):
                        torrent.metainfo['info']['files'][i]['foohash'] = 'This could be your MD5 sum'
                torrent.generate()
                torrent.write(torrent_filepath)
                torrents.append(SimpleNamespace(
                    torrent=torrent,
                    torrent_path=torrent_filepath,
                    content_path=content_path,
                ))
                print('created torrent:\n', torrents[-1].torrent_path, '\n', torrents[-1].torrent.metainfo)
            return torrents

        def __del__(self, *args, **kwargs):
            # Make sure pytest can delete files and directories
            for dirname in self._torrents:
                (tmp_path / dirname).chmod(0o700)
                for rootdir, dirnames, filenames in os.walk(tmp_path / dirname):
                    for dirname in dirnames:
                        os.chmod(os.path.join(rootdir, dirname), 0o700)
                    for filename in filenames:
                        os.chmod(os.path.join(rootdir, filename), 0o600)

        def __getattr__(self, name):
            return self._torrents[name]

        @property
        def locations(self):
            return {dirname: (tmp_path / dirname) for dirname in self._torrents}

        @property
        def location_paths(self):
            return tuple(tmp_path / dirname for dirname in self._torrents)

        @property
        def torrent_filepaths(self):
            return tuple(
                tmp_path / dirname / info.torrent_path
                for dirname, infos in self._torrents.items()
                for info in infos
            )

    return ExistingTorrents


@pytest.mark.parametrize(
    argnames='path, exp_find_torrent_files_args, exp_exception',
    argvalues=(
        ('a/path', ('a/path',), None),
        (('a/path', 'another/path'), ('a/path', 'another/path'), None),
        (iter(('a/path', 'another/path')), ('a/path', 'another/path'), None),
        (123, (), ValueError('Invalid path argument: 123')),
    ),
)
def test_path_argument(path, exp_find_torrent_files_args, exp_exception, create_file, mocker):
    find_torrent_files_mock = mocker.patch('torf._reuse.find_torrent_files', MagicMock(
        __iter__=MagicMock(return_value=()),
        total=0,
    ))

    torrent = torf.Torrent(path=create_file('just_a_file', 'foo'))

    if exp_exception:
        with pytest.raises(type(exp_exception), match=rf'^{re.escape(str(exp_exception))}$'):
            torrent.reuse(path)
    else:
        return_value = torrent.reuse(path)
        assert return_value is False
        assert find_torrent_files_mock.call_args_list == [call(
            *exp_find_torrent_files_args,
            max_file_size=torf.Torrent.MAX_TORRENT_FILE_SIZE,
        )]


def test_max_torrent_file_size(create_file, existing_torrents, mocker):
    # Create and prepare existing torrents
    existing_torrents = existing_torrents(
        subpath1=(
            ('a', 'foo', {'creation_date': 123}),
            ('b', 'bar', {'creation_date': 456}),
            ('c', 'baz', {'creation_date': 789}),
        ),
        subpath2=(
            ('d', 'hey', {'private': True}),
            ('e', 'ho', {'comment': 'yo'}),
            ('f', 'oh', {'comment': 'oy'}),
            ('g', 'ohh', {'comment': 'oyy'}),
        ),
    )
    # Make some torrents really big
    with open(existing_torrents.torrent_filepaths[1], 'wb') as f:
        f.truncate(20 * 1048576)
    with open(existing_torrents.torrent_filepaths[3], 'wb') as f:
        f.truncate(30 * 1048576)

    callback = Mock(return_value=None)
    new_torrent = torf.Torrent(path=create_file('just_a_file', 'foo'))
    return_value = new_torrent.reuse(existing_torrents.location_paths, callback=callback)

    assert return_value is False
    assert callback.call_args_list == [
        call(new_torrent, str(existing_torrents.subpath1[0].torrent_path), 1, 5, False, None),
        call(new_torrent, str(existing_torrents.subpath1[2].torrent_path), 2, 5, False, None),
        call(new_torrent, str(existing_torrents.subpath2[1].torrent_path), 3, 5, False, None),
        call(new_torrent, str(existing_torrents.subpath2[2].torrent_path), 4, 5, False, None),
        call(new_torrent, str(existing_torrents.subpath2[3].torrent_path), 5, 5, False, None),
    ]


@pytest.mark.parametrize('with_callback', (True, False), ids=('with_callback', 'without_callback'))
def test__singlefile__no_exceptions(with_callback, existing_torrents):
    # Create and prepare existing torrents
    existing_torrents = existing_torrents(
        my_torrents=(
            ('a', 'foo', {'creation_date': 123}),
            ('b', 'bar', {'creation_date': 456}),
            ('c', 'baz', {'creation_date': 789}),
            ('d', 'arf', {'created_by': 'me!'}),
            ('e', 'barf', {'source': 'you!'}),
        ),
    )

    # Create and prepare the torrent we want to generate
    reused = existing_torrents.my_torrents[2]
    new_torrent = torf.Torrent(
        path=reused.content_path,
        trackers=('http://foo:1000', 'http://foo:2000'),
        webseeds=('http://bar:1000',),
        httpseeds=('http://baz:1000',),
        private=True,
        comment='This is a custom torrent',
        creation_date=123000,
        created_by='CREATOR',
        source='SRC',
        piece_size=8 * 1048576,
        randomize_infohash=True,
    )

    # Expect the same metainfo, but with important parts copied
    exp_joined_metainfo = copy.deepcopy(new_torrent.metainfo)
    exp_joined_metainfo['info']['piece length'] = reused.torrent.metainfo['info']['piece length']
    exp_joined_metainfo['info']['pieces'] = reused.torrent.metainfo['info']['pieces']

    # Reuse existing torrent
    if with_callback:
        callback = Mock(return_value=None)
        return_value = new_torrent.reuse(existing_torrents.location_paths, callback=callback)

        # Confirm everything happened as expected
        assert return_value is True
        assert new_torrent.metainfo == exp_joined_metainfo
        assert callback.call_args_list == [
            call(new_torrent, str(existing_torrents.my_torrents[0].torrent_path), 1, 5, False, None),
            call(new_torrent, str(existing_torrents.my_torrents[1].torrent_path), 2, 5, False, None),
            call(new_torrent, str(existing_torrents.my_torrents[2].torrent_path), 3, 5, None, None),
            call(new_torrent, str(existing_torrents.my_torrents[2].torrent_path), 3, 5, True, None),
        ]
    else:
        return_value = new_torrent.reuse(existing_torrents.location_paths)

        # Confirm everything happened as expected
        assert return_value is True
        assert new_torrent.metainfo == exp_joined_metainfo


@pytest.mark.parametrize('with_callback', (True, False), ids=('with_callback', 'without_callback'))
def test__multifile__no_exceptions(with_callback, existing_torrents):
    # Create and prepare existing torrents with some of them sharing the same
    # (torrent name, file name, file size) but different file contents
    existing_torrents = existing_torrents(
        torrents1=(
            ('a', (
                ('this.jpg', 16380 * 30),
                ('that.txt', 'text data'),
            ), {'creation_date': 123}),
            ('b', (
                ('this.jpg', 16380 * 30),
                ('that.txt', 'text doto'),
            ), {'creation_date': 456}),
            ('c', (
                ('this.jpg', 16380 * 30),
                ('that.txt', 'text diti'),
            ), {'creation_date': 789}),
        ),
        torrents2=(
            ('a', (
                ('this.jpg', 16380 * 30),
                ('that.txt', 'more text'),
            ), {'creation_date': 234}),
            ('b', (
                ('this.jpg', 16380 * 30),
                ('that.txt', 'mare text'),
            ), {'creation_date': 345}),
            ('c', (
                ('this.jpg', 16380 * 30),
                ('that.txt', 'mire text'),
            ), {'creation_date': 456}),
        ),
    )

    # Create and prepare the torrent we want to generate
    reused = existing_torrents.torrents2[1]
    new_torrent = torf.Torrent(
        path=reused.content_path,
        trackers=('http://foo:1000', 'http://foo:2000'),
        webseeds=('http://bar:1000',),
        httpseeds=('http://baz:1000',),
        private=True,
        comment='This is a custom torrent',
        creation_date=123000,
        created_by='CREATOR',
        source='SRC',
        piece_size=1048576,
        randomize_infohash=True,
    )

    # Expect the same metainfo, but with important parts copied
    exp_joined_metainfo = copy.deepcopy(new_torrent.metainfo)
    exp_joined_metainfo['info']['piece length'] = reused.torrent.metainfo['info']['piece length']
    exp_joined_metainfo['info']['pieces'] = reused.torrent.metainfo['info']['pieces']
    exp_joined_metainfo['info']['files'] = [
        {'length': f['length'], 'path': f['path']}
        for f in reused.torrent.metainfo['info']['files']
    ]

    # Reuse existing torrent
    if with_callback:
        callback = Mock(return_value=None)
        return_value = new_torrent.reuse(existing_torrents.location_paths, callback=callback)

        # Confirm everything happened as expected
        assert return_value is True
        assert new_torrent.metainfo == exp_joined_metainfo
        assert callback.call_args_list == [
            call(new_torrent, str(existing_torrents.torrents1[0].torrent_path), 1, 6, False, None),
            call(new_torrent, str(existing_torrents.torrents1[1].torrent_path), 2, 6, None, None),
            call(new_torrent, str(existing_torrents.torrents1[1].torrent_path), 2, 6, False, None),
            call(new_torrent, str(existing_torrents.torrents1[2].torrent_path), 3, 6, False, None),
            call(new_torrent, str(existing_torrents.torrents2[0].torrent_path), 4, 6, False, None),
            call(new_torrent, str(reused.torrent_path), 5, 6, None, None),
            call(new_torrent, str(reused.torrent_path), 5, 6, True, None),
        ]

    else:
        return_value = new_torrent.reuse(existing_torrents.location_paths)

        # Confirm everything happened as expected
        assert return_value is True
        assert new_torrent.metainfo == exp_joined_metainfo


@pytest.mark.parametrize('with_callback', (True, False), ids=('with_callback', 'without_callback'))
def test_exceptions(with_callback, existing_torrents):
    # Create and prepare existing torrents
    existing_torrents = existing_torrents(
        readable1=(
            ('a', 'foo', {'creation_date': 123}),
            ('b', 'bar', {'creation_date': 456}),
            ('c', 'baz', {'creation_date': 789}),
        ),
        unreadable=(),
        readable2=(
            ('d', 'hey', {'private': True}),
            ('e', 'ho', {'comment': 'yo'}),
            ('f', 'oh', {'comment': 'oy'}),
            ('g', 'ohh', {'comment': 'oyy'}),
        ),
    )
    # Unreadable directory
    existing_torrents.locations['unreadable'].chmod(0o300)
    # Unreadable torrent file
    existing_torrents.readable2[1].torrent_path.chmod(0o300)
    # Nonexisting torrent file
    nonexisting_torrent_file = 'no/such/path.torrent'

    # Create and prepare the torrent we want to generate
    reused = existing_torrents.readable2[2]
    new_torrent = torf.Torrent(
        path=reused.content_path,
        trackers=('http://foo:1000', 'http://foo:2000'),
        webseeds=('http://bar:1000',),
        httpseeds=('http://baz:1000',),
        private=True,
        comment='This is a custom torrent',
        creation_date=123000,
        created_by='CREATOR',
        source='SRC',
        piece_size=8 * 1048576,
        randomize_infohash=True,
    )

    # Reuse existing torrent
    if with_callback:
        # Expect the same metainfo, but with important parts copied
        exp_joined_metainfo = copy.deepcopy(new_torrent.metainfo)
        exp_joined_metainfo['info']['piece length'] = reused.torrent.metainfo['info']['piece length']
        exp_joined_metainfo['info']['pieces'] = reused.torrent.metainfo['info']['pieces']

        callback = Mock(return_value=None)
        location_paths = (nonexisting_torrent_file,) + existing_torrents.location_paths
        return_value = new_torrent.reuse(location_paths, callback=callback)

        # Confirm everything happened as expected
        assert return_value is True
        assert new_torrent.metainfo == exp_joined_metainfo
        for c in callback.call_args_list:
            print(c)

        assert callback.call_args_list == [
            call(
                new_torrent, nonexisting_torrent_file,
                1, 8, False,
                ComparableException(
                    torf.ReadError(errno.ENOENT, nonexisting_torrent_file),
                ),
            ),
            call(new_torrent, str(existing_torrents.readable1[0].torrent_path), 2, 8, False, None),
            call(new_torrent, str(existing_torrents.readable1[1].torrent_path), 3, 8, False, None),
            call(new_torrent, str(existing_torrents.readable1[2].torrent_path), 4, 8, False, None),
            call(
                new_torrent,
                None,
                4, 8, False,
                ComparableException(
                    torf.ReadError(errno.EACCES, str(existing_torrents.locations['unreadable'])),
                ),
            ),
            call(new_torrent, str(existing_torrents.readable2[0].torrent_path), 5, 8, False, None),
            call(
                new_torrent,
                str(existing_torrents.readable2[1].torrent_path),
                6, 8, False,
                ComparableException(
                    torf.ReadError(errno.EACCES, str(existing_torrents.readable2[1].torrent_path)),
                ),
            ),
            call(new_torrent, str(existing_torrents.readable2[2].torrent_path), 7, 8, None, None),
            call(new_torrent, str(existing_torrents.readable2[2].torrent_path), 7, 8, True, None),
        ]
    else:
        # Expect identical metainfo
        exp_joined_metainfo = copy.deepcopy(new_torrent.metainfo)

        exp_exception = torf.ReadError(errno.EACCES, str(existing_torrents.locations['unreadable']))
        with pytest.raises(type(exp_exception), match=rf'^{re.escape(str(exp_exception))}$'):
            new_torrent.reuse(existing_torrents.location_paths)

        # Confirm everything happened as expected
        assert new_torrent.metainfo == exp_joined_metainfo


@pytest.mark.parametrize(
    argnames='cancel_condition, exp_callback_calls_count',
    argvalues=(
        # cancel_condition gets torrent_filepath and is_match and returns True
        # for cancelling, False otherwise.
        pytest.param(
            lambda tfp, is_match: is_match is False,
            1,
            id='mismatch',
        ),
        pytest.param(
            lambda tfp, is_match: tfp is None,
            4,
            id='unreadable directory',
        ),
        pytest.param(
            lambda tfp, is_match: os.path.basename(tfp or '') == 'e.torrent',
            6,
            id='unreadable torrent file',
        ),
        pytest.param(
            lambda tfp, is_match: os.path.basename(tfp or '') == 'f.torrent',
            7,
            id='invalid bencoded data',
        ),
        pytest.param(
            lambda tfp, is_match: os.path.basename(tfp or '') == 'g.torrent',
            8,
            id='invalid metainfo',
        ),
        pytest.param(
            lambda tfp, is_match: is_match is None,
            9,
            id='verification',
        ),
    ),
)
def test_callback_cancels_when_handling(cancel_condition, exp_callback_calls_count, existing_torrents, create_file):
    # Create and prepare existing torrents
    existing_torrents = existing_torrents(
        readable1=(
            ('a', 'foo', {'creation_date': 123}),
            ('b', 'bar', {'creation_date': 456}),
            ('c', 'baz', {'creation_date': 789}),
        ),
        # Unreadable directory
        unreadable=(),
        readable2=(
            ('d', 'hey', {'private': True}),
            ('e', 'ho', {'comment': 'yo'}),
            ('f', 'oh', {'comment': 'oy'}),
            ('g', 'ohh', {'comment': 'oyy'}),
            ('h', 'ohy', {'comment': 'hoyo'}),
        ),
    )
    # ReadError (directory)
    existing_torrents.locations['unreadable'].chmod(0o300)
    # ReadError (torrent file)
    existing_torrents.readable2[1].torrent_path.chmod(0o300)
    # BdecodeError
    data = bytearray(existing_torrents.readable2[2].torrent_path.read_bytes())
    data[0] = ord('x')
    existing_torrents.readable2[2].torrent_path.write_bytes(data)
    # MetainfoError
    del existing_torrents.readable2[3].torrent.metainfo['info']['piece length']
    existing_torrents.readable2[3].torrent.write(
        existing_torrents.readable2[3].torrent_path,
        validate=False, overwrite=True,
    )

    # Create and prepare the torrent we want to generate
    reused = existing_torrents.readable2[4]
    new_torrent = torf.Torrent(path=reused.content_path)
    exp_joined_metainfo = copy.deepcopy(new_torrent.metainfo)

    def callback(torrent, torrent_path, done, total, is_match, exception):
        if cancel_condition(torrent_path, is_match):
            return 'cancel'

    callback_wrapper = Mock(side_effect=callback)

    # Reuse existing torrent
    return_value = new_torrent.reuse(existing_torrents.location_paths, callback=callback_wrapper)

    # Confirm everything happened as expected
    assert return_value is False
    assert new_torrent.metainfo == exp_joined_metainfo

    all_callback_calls = [
        call(new_torrent, str(existing_torrents.readable1[0].torrent_path), 1, 8, False, None),
        call(new_torrent, str(existing_torrents.readable1[1].torrent_path), 2, 8, False, None),
        call(new_torrent, str(existing_torrents.readable1[2].torrent_path), 3, 8, False, None),
        call(
            new_torrent,
            None,
            3, 8, False,
            ComparableException(
                torf.ReadError(errno.EACCES, str(existing_torrents.locations['unreadable'])),
            ),
        ),
        call(new_torrent, str(existing_torrents.readable2[0].torrent_path), 4, 8, False, None),
        call(
            new_torrent,
            str(existing_torrents.readable2[1].torrent_path),
            5, 8, False,
            ComparableException(
                torf.ReadError(errno.EACCES, str(existing_torrents.readable2[1].torrent_path)),
            ),
        ),
        call(
            new_torrent,
            str(existing_torrents.readable2[2].torrent_path),
            6, 8, False,
            ComparableException(
                torf.BdecodeError(str(existing_torrents.readable2[2].torrent_path)),
            ),
        ),
        call(
            new_torrent,
            str(existing_torrents.readable2[3].torrent_path),
            7, 8, False,
            ComparableException(
                torf.MetainfoError("Missing 'piece length' in ['info']"),
            ),
        ),
        call(new_torrent, str(existing_torrents.readable2[4].torrent_path), 8, 8, None, None),
        call(new_torrent, str(existing_torrents.readable2[4].torrent_path), 8, 8, False, None),
    ]
    assert callback_wrapper.call_args_list == all_callback_calls[:exp_callback_calls_count]


@pytest.mark.parametrize('with_callback', (True, False), ids=('with_callback', 'without_callback'))
def test_handling_of_nonexisting_path(with_callback, existing_torrents):
    # Create and prepare existing torrents
    existing_torrents = existing_torrents(
        my_torrents=(
            ('a', 'foo', {'creation_date': 123}),
            ('b', 'bar', {'creation_date': 456}),
            ('c', 'baz', {'creation_date': 789}),
        ),
    )

    # Create and prepare the torrent we want to generate
    reused = existing_torrents.my_torrents[0]
    new_torrent = torf.Torrent(path=reused.content_path)

    # Expect identical metainfo
    exp_joined_metainfo = copy.deepcopy(new_torrent.metainfo)

    # Reuse existing torrent
    reuse_torrent_path = 'path/to/nonexisting/directory'
    if with_callback:
        callback = Mock(return_value=None)
        return_value = new_torrent.reuse(reuse_torrent_path, callback=callback)

        # Confirm everything happened as expected
        assert return_value is False
        assert new_torrent.metainfo == exp_joined_metainfo
        assert callback.call_args_list == [
            call(
                new_torrent, None,
                0, 0, False,
                ComparableException(
                    torf.ReadError(errno.ENOENT, reuse_torrent_path),
                ),
            ),
        ]

    else:
        exp_exception = torf.ReadError(errno.ENOENT, reuse_torrent_path)
        with pytest.raises(type(exp_exception), match=rf'^{re.escape(str(exp_exception))}$'):
            new_torrent.reuse(reuse_torrent_path)
        assert new_torrent.metainfo == exp_joined_metainfo


@pytest.mark.parametrize('with_callback', (True, False), ids=('with_callback', 'without_callback'))
def test_reuse_with_empty_file_list(with_callback, existing_torrents, create_file):
    # Create and prepare existing torrents
    existing_torrents = existing_torrents(
        my_torrents=(
            ('a.jpg', 'foo', {'creation_date': 123}),
            ('b.txt', 'bar', {'creation_date': 456}),
            ('c.mp4', 'baz', {'creation_date': 789}),
        ),
    )

    # Create and prepare the torrent we want to generate
    new_torrent = torf.Torrent(
        path=create_file('just_a_file.jpg', 'foo'),
        exclude_globs=['*.jpg'],
    )

    # Expect identical metainfo
    exp_joined_metainfo = copy.deepcopy(new_torrent.metainfo)

    exp_exception = RuntimeError('reuse() called while file list is empty')
    with pytest.raises(type(exp_exception), match=rf'^{re.escape(str(exp_exception))}$'):
        if with_callback:
            new_torrent.reuse(existing_torrents.location_paths, callback=Mock())
        else:
            new_torrent.reuse(existing_torrents.location_paths)

    assert new_torrent.metainfo == exp_joined_metainfo


def test_reuse_considers_piece_size_min_max(existing_torrents):
    # Create and prepare existing torrents
    existing_torrents = existing_torrents(
        small=(
            ('a.jpg', 'foo', {'piece_size': 1048576 / 2}),
            ('b.txt', 'bar', {'piece_size': 1048576 * 1}),
            ('c.mp4', 'baz', {'piece_size': 1048576 / 2}),
        ),
        big=(
            ('a.jpg', 'foo', {'piece_size': 1048576 / 2}),
            ('b.txt', 'bar', {'piece_size': 1048576 * 4}),
            ('c.mp4', 'baz', {'piece_size': 1048576 / 2}),
        ),
        medium=(
            ('a.jpg', 'foo', {'piece_size': 1048576 / 2}),
            ('b.txt', 'bar', {'piece_size': 1048576 * 2}),
            ('c.mp4', 'baz', {'piece_size': 1048576 / 2}),
        ),
        large=(
            ('a.jpg', 'foo', {'piece_size': 1048576 / 2}),
            ('b.txt', 'bar', {'piece_size': 1048576 * 8}),
            ('c.mp4', 'baz', {'piece_size': 1048576 / 2}),
        ),
        giant=(
            ('a.jpg', 'foo', {'piece_size': 1048576 / 2}),
            ('b.txt', 'bar', {'piece_size': 1048576 * 16}),
            ('c.mp4', 'baz', {'piece_size': 1048576 / 2}),
        ),
    )

    # Create and prepare the torrent we want to generate
    reused = existing_torrents.medium[1]
    new_torrent = torf.Torrent(path=reused.content_path)
    exp_joined_metainfo = copy.deepcopy(new_torrent.metainfo)

    # Limit piece size to 1 - 2 MiB
    new_torrent.piece_size_min = 1 * 1048576
    new_torrent.piece_size_max = 2 * 1048576
    exp_joined_metainfo['info']['piece length'] = 1048576 * 1
    exp_joined_metainfo['info']['pieces'] = existing_torrents.medium[1].torrent.metainfo['info']['pieces']
    new_torrent.reuse(existing_torrents.location_paths)
    assert new_torrent.metainfo == exp_joined_metainfo

    # Limit piece size to 2 - 4 MiB
    new_torrent.piece_size_min = 2 * 1048576
    new_torrent.piece_size_max = 4 * 1048576
    exp_joined_metainfo['info']['piece length'] = 1048576 * 4
    exp_joined_metainfo['info']['pieces'] = existing_torrents.small[1].torrent.metainfo['info']['pieces']
    new_torrent.reuse(existing_torrents.location_paths)
    assert new_torrent.metainfo == exp_joined_metainfo


    # Limit piece size to 4 - 8 MiB
    new_torrent.piece_size_min = 4 * 1048576
    new_torrent.piece_size_max = 8 * 1048576
    exp_joined_metainfo['info']['piece length'] = 1048576 * 4
    exp_joined_metainfo['info']['pieces'] = existing_torrents.big[1].torrent.metainfo['info']['pieces']
    new_torrent.reuse(existing_torrents.location_paths)
    assert new_torrent.metainfo == exp_joined_metainfo

    # Limit piece size to 8 - 16 MiB
    new_torrent.piece_size_min = 8 * 1048576
    new_torrent.piece_size_max = 16 * 1048576
    exp_joined_metainfo['info']['piece length'] = 1048576 * 8
    exp_joined_metainfo['info']['pieces'] = existing_torrents.small[1].torrent.metainfo['info']['pieces']
    new_torrent.reuse(existing_torrents.location_paths)
    assert new_torrent.metainfo == exp_joined_metainfo


@pytest.mark.parametrize('with_callback', (True, False), ids=('with_callback', 'without_callback'))
def test_reuse_copies_file_order(with_callback, existing_torrents):
    # Create and prepare existing torrents with some of them sharing the same
    # (torrent name, file name, file size) but different file contents
    existing_torrents = existing_torrents(
        my_torrents=(
            ('a', (
                ('this.jpg', 16380 * 30),
                ('that.txt', 'text data'),
            ), {'creation_date': 123}),
            ('b', (
                ('this.jpg', 16380 * 30),
                ('that.txt', 'text doto'),
            ), {'creation_date': 456}),
            ('c', (
                ('this.jpg', 16380 * 30),
                ('that.txt', 'text diti'),
            ), {'creation_date': 789}),
        ),
    )

    # Create and prepare the torrent we want to generate
    reused = existing_torrents.my_torrents[1]
    new_torrent = torf.Torrent(reused.content_path)

    # Differing file order shouldn't matter, the new torrent should have the
    # same order as the reused torrent
    new_torrent.metainfo['info']['files'][0], new_torrent.metainfo['info']['files'][1] = \
        new_torrent.metainfo['info']['files'][1], new_torrent.metainfo['info']['files'][0]

    # Expect the same metainfo, but with important parts copied
    exp_joined_metainfo = copy.deepcopy(new_torrent.metainfo)
    exp_joined_metainfo['info']['piece length'] = reused.torrent.metainfo['info']['piece length']
    exp_joined_metainfo['info']['pieces'] = reused.torrent.metainfo['info']['pieces']
    exp_joined_metainfo['info']['files'] = [
        {'length': f['length'], 'path': f['path']}
        for f in reused.torrent.metainfo['info']['files']
    ]

    # Reuse existing torrent
    if with_callback:
        callback = Mock(return_value=None)
        return_value = new_torrent.reuse(existing_torrents.location_paths, callback=callback)

        # Confirm everything happened as expected
        assert return_value is True
        assert new_torrent.metainfo == exp_joined_metainfo
        assert callback.call_args_list == [
            call(new_torrent, str(existing_torrents.my_torrents[0].torrent_path), 1, 3, False, None),
            call(new_torrent, str(existing_torrents.my_torrents[1].torrent_path), 2, 3, None, None),
            call(new_torrent, str(existing_torrents.my_torrents[1].torrent_path), 2, 3, True, None),
        ]

    else:
        return_value = new_torrent.reuse(existing_torrents.location_paths)

        # Confirm everything happened as expected
        assert return_value is True
        assert new_torrent.metainfo == exp_joined_metainfo