forked from arvidn/libtorrent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_storage.cpp
1503 lines (1253 loc) · 45.8 KB
/
test_storage.cpp
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
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2008, Arvid Norberg
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution.
* Neither the name of the author nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include "test.hpp"
#include "setup_transfer.hpp"
#include "test_utils.hpp"
#include "settings.hpp"
#include "libtorrent/storage.hpp"
#include "libtorrent/file_pool.hpp"
#include "libtorrent/hasher.hpp"
#include "libtorrent/session.hpp"
#include "libtorrent/alert_types.hpp"
#include "libtorrent/create_torrent.hpp"
#include "libtorrent/torrent_info.hpp"
#include "libtorrent/read_resume_data.hpp"
#include "libtorrent/write_resume_data.hpp"
#include "libtorrent/aux_/path.hpp"
#include "libtorrent/aux_/storage_utils.hpp"
#include <memory>
#include <functional> // for bind
#include <iostream>
#include <fstream>
#include <boost/variant/get.hpp>
using namespace std::placeholders;
using namespace lt;
std::size_t const piece_size = 16 * 1024 * 16;
std::size_t const half = piece_size / 2;
void signal_bool(bool* b, char const* string)
{
*b = true;
std::cout << time_now_string() << " " << string << std::endl;
}
void on_read_piece(int ret, disk_io_job const& j, char const* data, int size)
{
std::cout << time_now_string() << " on_read_piece piece: " << j.piece << std::endl;
TEST_EQUAL(ret, size);
auto& buffer = boost::get<disk_buffer_holder>(j.argument);
if (ret > 0) TEST_CHECK(std::equal(buffer.get(), buffer.get() + ret, data));
}
void on_check_resume_data(status_t const status, storage_error const& error, bool* done)
{
std::cout << time_now_string() << " on_check_resume_data ret: "
<< static_cast<int>(status);
switch (status)
{
case status_t::no_error:
std::cout << time_now_string() << " success" << std::endl;
break;
case status_t::fatal_disk_error:
std::cout << time_now_string() << " disk error: " << error.ec.message()
<< " file: " << error.file() << std::endl;
break;
case status_t::need_full_check:
std::cout << time_now_string() << " need full check" << std::endl;
break;
case status_t::file_exist:
std::cout << time_now_string() << " file exist" << std::endl;
break;
}
std::cout << std::endl;
*done = true;
}
void print_error(char const* call, int ret, storage_error const& ec)
{
std::printf("%s: %s() returned: %d error: \"%s\" in file: %d operation: %s\n"
, time_now_string(), call, ret, ec.ec.message().c_str()
, static_cast<int>(ec.file()), operation_name(ec.operation));
}
void run_until(io_service& ios, bool const& done)
{
while (!done)
{
ios.reset();
error_code ec;
ios.run_one(ec);
if (ec)
{
std::cout << "run_one: " << ec.message().c_str() << std::endl;
return;
}
std::cout << time_now_string() << " done: " << done << std::endl;
}
}
void nop() {}
std::shared_ptr<torrent_info> setup_torrent_info(file_storage& fs
, std::vector<char>& buf)
{
fs.add_file(combine_path("temp_storage", "test1.tmp"), 8);
fs.add_file(combine_path("temp_storage", combine_path("folder1", "test2.tmp")), 8);
fs.add_file(combine_path("temp_storage", combine_path("folder2", "test3.tmp")), 0);
fs.add_file(combine_path("temp_storage", combine_path("_folder3", "test4.tmp")), 0);
fs.add_file(combine_path("temp_storage", combine_path("_folder3", combine_path("subfolder", "test5.tmp"))), 8);
lt::create_torrent t(fs, 4, -1, {});
char buf_[4] = {0, 0, 0, 0};
sha1_hash h = hasher(buf_).final();
for (piece_index_t i(0); i < piece_index_t(6); ++i) t.set_hash(i, h);
bencode(std::back_inserter(buf), t.generate());
error_code ec;
auto info = std::make_shared<torrent_info>(buf, ec, from_span);
if (ec)
{
std::printf("torrent_info constructor failed: %s\n"
, ec.message().c_str());
}
return info;
}
std::shared_ptr<default_storage> setup_torrent(file_storage& fs
, file_pool& fp
, std::vector<char>& buf
, std::string const& test_path
, aux::session_settings& set)
{
std::shared_ptr<torrent_info> info = setup_torrent_info(fs, buf);
aux::vector<download_priority_t, file_index_t> priorities;
sha1_hash info_hash;
storage_params p{
fs,
nullptr,
test_path,
storage_mode_allocate,
priorities,
info_hash
};
std::shared_ptr<default_storage> s(new default_storage(p, fp));
s->m_settings = &set;
// allocate the files and create the directories
storage_error se;
s->initialize(se);
if (se)
{
TEST_ERROR(se.ec.message().c_str());
std::printf("default_storage::initialize %s: %d\n"
, se.ec.message().c_str(), static_cast<int>(se.file()));
}
return s;
}
std::vector<char> new_piece(std::size_t const size)
{
std::vector<char> ret(size);
std::generate(ret.begin(), ret.end(), random_byte);
return ret;
}
void run_storage_tests(std::shared_ptr<torrent_info> info
, file_storage& fs
, std::string const& test_path
, lt::storage_mode_t storage_mode
, bool unbuffered)
{
TORRENT_ASSERT(fs.num_files() > 0);
error_code ec;
create_directory(combine_path(test_path, "temp_storage"), ec);
if (ec) std::cout << "create_directory '" << combine_path(test_path, "temp_storage")
<< "': " << ec.message() << std::endl;
remove_all(combine_path(test_path, "temp_storage2"), ec);
if (ec && ec != boost::system::errc::no_such_file_or_directory)
std::cout << "remove_all '" << combine_path(test_path, "temp_storage2")
<< "': " << ec.message() << std::endl;
remove_all(combine_path(test_path, "part0"), ec);
if (ec && ec != boost::system::errc::no_such_file_or_directory)
std::cout << "remove_all '" << combine_path(test_path, "part0")
<< "': " << ec.message() << std::endl;
int num_pieces = fs.num_pieces();
TEST_CHECK(info->num_pieces() == num_pieces);
std::vector<char> piece0 = new_piece(piece_size);
std::vector<char> piece1 = new_piece(piece_size);
std::vector<char> piece2 = new_piece(piece_size);
aux::session_settings set;
set.set_int(settings_pack::disk_io_write_mode
, unbuffered ? settings_pack::disable_os_cache
: settings_pack::enable_os_cache);
set.set_int(settings_pack::disk_io_read_mode
, unbuffered ? settings_pack::disable_os_cache
: settings_pack::enable_os_cache);
char* piece = static_cast<char*>(malloc(piece_size));
{ // avoid having two storages use the same files
file_pool fp;
boost::asio::io_service ios;
disk_buffer_pool dp(16 * 1024, ios, std::bind(&nop));
aux::vector<download_priority_t, file_index_t> priorities;
sha1_hash info_hash;
storage_params p{
fs,
nullptr,
test_path,
storage_mode,
priorities,
info_hash
};
std::unique_ptr<storage_interface> s(new default_storage(p, fp));
s->m_settings = &set;
storage_error ec;
s->initialize(ec);
TEST_CHECK(!ec);
if (ec) print_error("initialize", 0, ec);
int ret = 0;
// write piece 1 (in slot 0)
iovec_t iov = { piece1.data(), half};
ret = s->writev(iov, piece_index_t(0), 0, open_mode::read_write, ec);
if (ret != half) print_error("writev", ret, ec);
iov = { piece1.data() + half, half };
ret = s->writev(iov, piece_index_t(0), half, open_mode::read_write, ec);
if (ret != half) print_error("writev", ret, ec);
// test unaligned read (where the bytes are aligned)
iov = { piece + 3, piece_size - 9};
ret = s->readv(iov, piece_index_t(0), 3, open_mode::read_write, ec);
if (ret != piece_size - 9) print_error("readv",ret, ec);
TEST_CHECK(std::equal(piece+3, piece + piece_size-9, piece1.data()+3));
// test unaligned read (where the bytes are not aligned)
iov = { piece, piece_size - 9};
ret = s->readv(iov, piece_index_t(0), 3, open_mode::read_write, ec);
TEST_CHECK(ret == piece_size - 9);
if (ret != piece_size - 9) print_error("readv", ret, ec);
TEST_CHECK(std::equal(piece, piece + piece_size-9, piece1.data()+3));
// verify piece 1
iov = { piece, piece_size };
ret = s->readv(iov, piece_index_t(0), 0, open_mode::read_write, ec);
TEST_CHECK(ret == piece_size);
if (ret != piece_size) print_error("readv", ret, ec);
TEST_CHECK(std::equal(piece, piece + piece_size, piece1.data()));
// do the same with piece 0 and 2 (in slot 1 and 2)
iov = { piece0.data(), piece_size };
ret = s->writev(iov, piece_index_t(1), 0, open_mode::read_write, ec);
if (ret != piece_size) print_error("writev", ret, ec);
iov = { piece2.data(), piece_size };
ret = s->writev(iov, piece_index_t(2), 0, open_mode::read_write, ec);
if (ret != piece_size) print_error("writev", ret, ec);
// verify piece 0 and 2
iov = { piece, piece_size };
ret = s->readv(iov, piece_index_t(1), 0, open_mode::read_write, ec);
if (ret != piece_size) print_error("readv", ret, ec);
TEST_CHECK(std::equal(piece, piece + piece_size, piece0.data()));
iov = { piece, piece_size };
ret = s->readv(iov, piece_index_t(2), 0, open_mode::read_write, ec);
if (ret != piece_size) print_error("readv", ret, ec);
TEST_CHECK(std::equal(piece, piece + piece_size, piece2.data()));
s->release_files(ec);
}
free(piece);
}
void test_remove(std::string const& test_path, bool unbuffered)
{
error_code ec;
remove_all(combine_path(test_path, "temp_storage"), ec);
if (ec && ec != boost::system::errc::no_such_file_or_directory)
std::cout << "remove_all '" << combine_path(test_path, "temp_storage")
<< "': " << ec.message() << std::endl;
TEST_CHECK(!exists(combine_path(test_path, "temp_storage")));
file_storage fs;
std::vector<char> buf;
file_pool fp;
io_service ios;
disk_buffer_pool dp(16 * 1024, ios, std::bind(&nop));
aux::session_settings set;
set.set_int(settings_pack::disk_io_write_mode
, unbuffered ? settings_pack::disable_os_cache
: settings_pack::enable_os_cache);
set.set_int(settings_pack::disk_io_read_mode
, unbuffered ? settings_pack::disable_os_cache
: settings_pack::enable_os_cache);
std::shared_ptr<default_storage> s = setup_torrent(fs, fp, buf, test_path, set);
// directories are not created up-front, unless they contain
// an empty file (all of which are created up-front, along with
// all required directories)
// files are created on first write
TEST_CHECK(!exists(combine_path(test_path, combine_path("temp_storage"
, combine_path("_folder3", combine_path("subfolder", "test5.tmp"))))));
// this directory and file is created up-front because it's an empty file
TEST_CHECK(exists(combine_path(test_path, combine_path("temp_storage"
, combine_path("folder2", "test3.tmp")))));
// this isn't
TEST_CHECK(!exists(combine_path(test_path, combine_path("temp_storage"
, combine_path("folder1", "test2.tmp")))));
iovec_t b = {&buf[0], 4};
storage_error se;
s->writev(b, piece_index_t(2), 0, open_mode::read_write, se);
TEST_CHECK(exists(combine_path(test_path, combine_path("temp_storage"
, combine_path("folder1", "test2.tmp")))));
TEST_CHECK(!exists(combine_path(test_path, combine_path("temp_storage"
, combine_path("_folder3", combine_path("subfolder", "test5.tmp"))))));
file_status st;
stat_file(combine_path(test_path, combine_path("temp_storage"
, combine_path("folder1", "test2.tmp"))), &st, ec);
TEST_EQUAL(st.file_size, 8);
s->writev(b, piece_index_t(4), 0, open_mode::read_write, se);
TEST_CHECK(exists(combine_path(test_path, combine_path("temp_storage"
, combine_path("_folder3", combine_path("subfolder", "test5.tmp"))))));
stat_file(combine_path(test_path, combine_path("temp_storage"
, combine_path("_folder3", "test5.tmp"))), &st, ec);
TEST_EQUAL(st.file_size, 8);
s->delete_files(session::delete_files, se);
if (se) print_error("delete_files", 0, se);
if (se)
{
TEST_ERROR(se.ec.message().c_str());
std::printf("default_storage::delete_files %s: %d\n"
, se.ec.message().c_str(), static_cast<int>(se.file()));
}
TEST_CHECK(!exists(combine_path(test_path, "temp_storage")));
}
void test_rename(std::string const& test_path)
{
error_code ec;
remove_all(combine_path(test_path, "temp_storage"), ec);
if (ec && ec != boost::system::errc::no_such_file_or_directory)
std::cout << "remove_all '" << combine_path(test_path, "temp_storage")
<< "': " << ec.message() << std::endl;
TEST_CHECK(!exists(combine_path(test_path, "temp_storage")));
file_storage fs;
std::vector<char> buf;
file_pool fp;
io_service ios;
disk_buffer_pool dp(16 * 1024, ios, std::bind(&nop));
aux::session_settings set;
std::shared_ptr<default_storage> s = setup_torrent(fs, fp, buf, test_path
, set);
// directories are not created up-front, unless they contain
// an empty file
std::string first_file = fs.file_path(file_index_t(0));
for (file_index_t i(0); i < fs.end_file(); ++i)
{
TEST_CHECK(!exists(combine_path(test_path, combine_path("temp_storage"
, fs.file_path(i)))));
}
storage_error se;
s->rename_file(file_index_t(0), "new_filename", se);
if (se.ec)
{
std::printf("default_storage::rename_file failed: %s\n"
, se.ec.message().c_str());
}
TEST_CHECK(!se.ec);
TEST_EQUAL(s->files().file_path(file_index_t(0)), "new_filename");
}
void test_check_files(std::string const& test_path
, lt::storage_mode_t storage_mode
, bool unbuffered)
{
std::shared_ptr<torrent_info> info;
error_code ec;
const int piece_size = 16 * 1024;
remove_all(combine_path(test_path, "temp_storage"), ec);
if (ec && ec != boost::system::errc::no_such_file_or_directory)
std::cout << "remove_all '" << combine_path(test_path, "temp_storage")
<< "': " << ec.message() << std::endl;
file_storage fs;
fs.add_file("temp_storage/test1.tmp", piece_size);
fs.add_file("temp_storage/test2.tmp", piece_size * 2);
fs.add_file("temp_storage/test3.tmp", piece_size);
std::vector<char> piece0 = new_piece(piece_size);
std::vector<char> piece2 = new_piece(piece_size);
lt::create_torrent t(fs, piece_size, -1, {});
t.set_hash(piece_index_t(0), hasher(piece0).final());
t.set_hash(piece_index_t(1), sha1_hash(nullptr));
t.set_hash(piece_index_t(2), sha1_hash(nullptr));
t.set_hash(piece_index_t(3), hasher(piece2).final());
create_directory(combine_path(test_path, "temp_storage"), ec);
if (ec) std::cout << "create_directory: " << ec.message() << std::endl;
std::ofstream f;
f.open(combine_path(test_path, combine_path("temp_storage", "test1.tmp")).c_str()
, std::ios::trunc | std::ios::binary);
f.write(piece0.data(), piece_size);
f.close();
f.open(combine_path(test_path, combine_path("temp_storage", "test3.tmp")).c_str()
, std::ios::trunc | std::ios::binary);
f.write(piece2.data(), piece_size);
f.close();
std::vector<char> buf;
bencode(std::back_inserter(buf), t.generate());
info = std::make_shared<torrent_info>(buf, ec, from_span);
aux::session_settings set;
file_pool fp;
boost::asio::io_service ios;
counters cnt;
disk_io_thread io(ios, cnt);
settings_pack sett;
sett.set_int(settings_pack::aio_threads, 1);
io.set_settings(&sett);
disk_buffer_pool dp(16 * 1024, ios, std::bind(&nop));
aux::vector<download_priority_t, file_index_t> priorities;
sha1_hash info_hash;
storage_params p{
fs,
nullptr,
test_path,
storage_mode,
priorities,
info_hash
};
auto st = io.new_torrent(default_storage_constructor, std::move(p)
, std::shared_ptr<void>());
std::mutex lock;
bool done = false;
add_torrent_params frd;
aux::vector<std::string, file_index_t> links;
io.async_check_files(st, &frd, links
, std::bind(&on_check_resume_data, _1, _2, &done));
io.submit_jobs();
ios.reset();
run_until(ios, done);
io.abort(true);
}
// TODO: 2 split this test up into smaller parts
void run_test(bool unbuffered)
{
std::string test_path = current_working_directory();
std::cout << "\n=== " << test_path << " ===\n" << std::endl;
std::shared_ptr<torrent_info> info;
std::vector<char> piece0 = new_piece(piece_size);
std::vector<char> piece1 = new_piece(piece_size);
std::vector<char> piece2 = new_piece(piece_size);
std::vector<char> piece3 = new_piece(piece_size);
{
error_code ec;
remove_all(combine_path(test_path, "temp_storage"), ec);
if (ec && ec != boost::system::errc::no_such_file_or_directory)
std::cout << "remove_all '" << combine_path(test_path, "temp_storage")
<< "': " << ec.message() << std::endl;
file_storage fs;
fs.add_file("temp_storage/test1.tmp", 17);
fs.add_file("temp_storage/test2.tmp", 612);
fs.add_file("temp_storage/test3.tmp", 0);
fs.add_file("temp_storage/test4.tmp", 0);
fs.add_file("temp_storage/test5.tmp", 3253);
fs.add_file("temp_storage/test6.tmp", 841);
const int last_file_size = 4 * piece_size - int(fs.total_size());
fs.add_file("temp_storage/test7.tmp", last_file_size);
// File layout
// +-+--+++-------+-------+----------------------------------------------------------------------------------------+
// |1| 2||| file5 | file6 | file7 |
// +-+--+++-------+-------+----------------------------------------------------------------------------------------+
// | | | | |
// | piece 0 | piece 1 | piece 2 | piece 3 |
lt::create_torrent t(fs, piece_size, -1, {});
TEST_CHECK(t.num_pieces() == 4);
t.set_hash(piece_index_t(0), hasher(piece0).final());
t.set_hash(piece_index_t(1), hasher(piece1).final());
t.set_hash(piece_index_t(2), hasher(piece2).final());
t.set_hash(piece_index_t(3), hasher(piece3).final());
std::vector<char> buf;
bencode(std::back_inserter(buf), t.generate());
info = std::make_shared<torrent_info>(buf, from_span);
std::cout << "=== test 1 === " << (unbuffered?"unbuffered":"buffered") << std::endl;
// run_storage_tests writes piece 0, 1 and 2. not 3
run_storage_tests(info, fs, test_path, storage_mode_sparse, unbuffered);
// make sure the files have the correct size
std::string base = combine_path(test_path, "temp_storage");
std::printf("base = \"%s\"\n", base.c_str());
TEST_EQUAL(file_size(combine_path(base, "test1.tmp")), 17);
TEST_EQUAL(file_size(combine_path(base, "test2.tmp")), 612);
// these files should have been allocated as 0 size
TEST_CHECK(exists(combine_path(base, "test3.tmp")));
TEST_CHECK(exists(combine_path(base, "test4.tmp")));
TEST_CHECK(file_size(combine_path(base, "test3.tmp")) == 0);
TEST_CHECK(file_size(combine_path(base, "test4.tmp")) == 0);
TEST_EQUAL(file_size(combine_path(base, "test5.tmp")), 3253);
TEST_EQUAL(file_size(combine_path(base, "test6.tmp")), 841);
std::printf("file: %d expected: %d last_file_size: %d, piece_size: %d\n"
, int(file_size(combine_path(base, "test7.tmp")))
, int(last_file_size - piece_size), last_file_size, int(piece_size));
TEST_EQUAL(file_size(combine_path(base, "test7.tmp")), last_file_size - int(piece_size));
remove_all(combine_path(test_path, "temp_storage"), ec);
if (ec && ec != boost::system::errc::no_such_file_or_directory)
std::cout << "remove_all '" << combine_path(test_path, "temp_storage")
<< "': " << ec.message() << std::endl;
}
// ==============================================
{
error_code ec;
file_storage fs;
fs.add_file(combine_path("temp_storage", "test1.tmp"), 3 * piece_size);
lt::create_torrent t(fs, piece_size, -1, {});
TEST_CHECK(fs.file_path(file_index_t(0)) == combine_path("temp_storage", "test1.tmp"));
t.set_hash(piece_index_t(0), hasher(piece0).final());
t.set_hash(piece_index_t(1), hasher(piece1).final());
t.set_hash(piece_index_t(2), hasher(piece2).final());
std::vector<char> buf;
bencode(std::back_inserter(buf), t.generate());
info = std::make_shared<torrent_info>(buf, ec, from_span);
std::cout << "=== test 3 ===" << std::endl;
run_storage_tests(info, fs, test_path, storage_mode_sparse, unbuffered);
TEST_EQUAL(file_size(combine_path(test_path, combine_path("temp_storage", "test1.tmp"))), piece_size * 3);
remove_all(combine_path(test_path, "temp_storage"), ec);
if (ec && ec != boost::system::errc::no_such_file_or_directory)
std::cout << "remove_all '" << combine_path(test_path, "temp_storage")
<< "': " << ec.message() << std::endl;
// ==============================================
std::cout << "=== test 4 ===" << std::endl;
run_storage_tests(info, fs, test_path, storage_mode_allocate, unbuffered);
std::cout << file_size(combine_path(test_path, combine_path("temp_storage", "test1.tmp"))) << std::endl;
TEST_EQUAL(file_size(combine_path(test_path, combine_path("temp_storage", "test1.tmp"))), 3 * piece_size);
remove_all(combine_path(test_path, "temp_storage"), ec);
if (ec && ec != boost::system::errc::no_such_file_or_directory)
std::cout << "remove_all '" << combine_path(test_path, "temp_storage")
<< "': " << ec.message() << std::endl;
}
// ==============================================
std::cout << "=== test 5 ===" << std::endl;
test_remove(test_path, unbuffered);
// ==============================================
std::cout << "=== test 6 ===" << std::endl;
test_check_files(test_path, storage_mode_sparse, unbuffered);
test_check_files(test_path, storage_mode_sparse, unbuffered);
std::cout << "=== test 7 ===" << std::endl;
test_rename(test_path);
}
void test_fastresume(bool const test_deprecated)
{
std::string test_path = current_working_directory();
error_code ec;
std::cout << "\n\n=== test fastresume ===" << std::endl;
remove_all(combine_path(test_path, "tmp1"), ec);
if (ec && ec != boost::system::errc::no_such_file_or_directory)
std::cout << "remove_all '" << combine_path(test_path, "tmp1")
<< "': " << ec.message() << std::endl;
create_directory(combine_path(test_path, "tmp1"), ec);
if (ec) std::cout << "create_directory '" << combine_path(test_path, "tmp1")
<< "': " << ec.message() << std::endl;
std::ofstream file(combine_path(test_path, "tmp1/temporary").c_str());
std::shared_ptr<torrent_info> t = ::create_torrent(&file);
file.close();
TEST_CHECK(exists(combine_path(test_path, "tmp1/temporary")));
if (!exists(combine_path(test_path, "tmp1/temporary")))
return;
entry resume;
{
settings_pack pack = settings();
lt::session ses(pack);
error_code ec;
add_torrent_params p;
p.ti = std::make_shared<torrent_info>(std::cref(*t));
p.save_path = combine_path(test_path, "tmp1");
p.storage_mode = storage_mode_sparse;
torrent_handle h = ses.add_torrent(std::move(p), ec);
TEST_CHECK(exists(combine_path(p.save_path, "temporary")));
if (!exists(combine_path(p.save_path, "temporary")))
return;
torrent_status s;
for (int i = 0; i < 50; ++i)
{
print_alerts(ses, "ses");
s = h.status();
if (s.progress == 1.0f)
{
std::cout << "progress: 1.0f" << std::endl;
break;
}
std::this_thread::sleep_for(lt::milliseconds(100));
}
// the whole point of the test is to have a resume
// data which expects the file to exist in full. If
// we failed to do that, we might as well abort
TEST_EQUAL(s.progress, 1.0f);
if (s.progress != 1.0f)
return;
h.save_resume_data();
alert const* ra = wait_for_alert(ses, save_resume_data_alert::alert_type);
TEST_CHECK(ra);
if (ra) resume = write_resume_data(alert_cast<save_resume_data_alert>(ra)->params);
ses.remove_torrent(h, lt::session::delete_files);
alert const* da = wait_for_alert(ses, torrent_deleted_alert::alert_type);
TEST_CHECK(da);
}
TEST_CHECK(!exists(combine_path(test_path, combine_path("tmp1", "temporary"))));
if (exists(combine_path(test_path, combine_path("tmp1", "temporary"))))
return;
std::cout << resume.to_string() << "\n";
// make sure the fast resume check fails! since we removed the file
{
settings_pack pack = settings();
lt::session ses(pack);
std::vector<char> resume_data;
bencode(std::back_inserter(resume_data), resume);
add_torrent_params p;
#ifndef TORRENT_NO_DEPRECATE
if (test_deprecated)
{
p.resume_data = resume_data;
}
else
#endif
{
p = read_resume_data(resume_data, ec);
TEST_CHECK(!ec);
}
p.flags &= ~torrent_flags::paused;
p.flags &= ~torrent_flags::auto_managed;
p.ti = std::make_shared<torrent_info>(std::cref(*t));
p.save_path = combine_path(test_path, "tmp1");
p.storage_mode = storage_mode_sparse;
torrent_handle h = ses.add_torrent(std::move(p), ec);
std::printf("expecting fastresume to be rejected becase the files were removed");
alert const* a = wait_for_alert(ses, fastresume_rejected_alert::alert_type
, "ses");
// we expect the fast resume to be rejected because the files were removed
TEST_CHECK(alert_cast<fastresume_rejected_alert>(a) != nullptr);
}
remove_all(combine_path(test_path, "tmp1"), ec);
if (ec && ec != boost::system::errc::no_such_file_or_directory)
std::cout << "remove_all '" << combine_path(test_path, "tmp1")
<< "': " << ec.message() << std::endl;
}
TORRENT_TEST(fastresume)
{
test_fastresume(false);
}
#ifndef TORRENT_NO_DEPRECATE
TORRENT_TEST(fastresume_deprecated)
{
test_fastresume(true);
}
#endif
bool got_file_rename_alert(alert const* a)
{
return alert_cast<lt::file_renamed_alert>(a)
|| alert_cast<lt::file_rename_failed_alert>(a);
}
TORRENT_TEST(rename_file)
{
std::vector<char> buf;
file_storage fs;
std::shared_ptr<torrent_info> info = setup_torrent_info(fs, buf);
auto const mask = alert::all_categories
& ~(alert::performance_warning
| alert::stats_notification);
settings_pack pack = settings();
pack.set_int(settings_pack::alert_mask, mask);
pack.set_bool(settings_pack::disable_hash_checks, true);
lt::session ses(pack);
add_torrent_params p;
p.ti = info;
p.save_path = ".";
error_code ec;
torrent_handle h = ses.add_torrent(std::move(p), ec);
// make it a seed
std::vector<char> tmp(info->piece_length());
for (piece_index_t i(0); i < fs.end_piece(); ++i)
h.add_piece(i, &tmp[0]);
// wait for the files to have been written
for (int i = 0; i < info->num_pieces(); ++i)
{
alert const* pf = wait_for_alert(ses, piece_finished_alert::alert_type
, "ses", pop_alerts::cache_alerts);
TEST_CHECK(pf);
}
// now rename them. This is the test
for (file_index_t i(0); i < fs.end_file(); ++i)
{
std::string name = fs.file_path(i);
h.rename_file(i, "temp_storage__" + name.substr(12));
}
// wait for the files to have been renamed
for (int i = 0; i < info->num_files(); ++i)
{
alert const* fra = wait_for_alert(ses, file_renamed_alert::alert_type
, "ses", pop_alerts::cache_alerts);
TEST_CHECK(fra);
}
TEST_CHECK(exists(info->name() + "__"));
h.save_resume_data();
alert const* ra = wait_for_alert(ses, save_resume_data_alert::alert_type);
TEST_CHECK(ra);
if (!ra) return;
add_torrent_params resume = alert_cast<save_resume_data_alert>(ra)->params;
auto const files = resume.renamed_files;
for (auto const& i : files)
{
TEST_EQUAL(i.second.substr(0, 14), "temp_storage__");
}
}
void test_rename_file_fastresume(bool test_deprecated)
{
std::string test_path = current_working_directory();
error_code ec;
std::cout << "\n\n=== test rename file in fastresume ===" << std::endl;
remove_all(combine_path(test_path, "tmp2"), ec);
if (ec && ec != boost::system::errc::no_such_file_or_directory)
std::cout << "remove_all '" << combine_path(test_path, "tmp2")
<< "': " << ec.message() << std::endl;
create_directory(combine_path(test_path, "tmp2"), ec);
if (ec) std::cout << "create_directory: " << ec.message() << std::endl;
std::ofstream file(combine_path(test_path, "tmp2/temporary").c_str());
std::shared_ptr<torrent_info> t = ::create_torrent(&file);
file.close();
TEST_CHECK(exists(combine_path(test_path, "tmp2/temporary")));
add_torrent_params resume;
{
settings_pack pack = settings();
lt::session ses(pack);
add_torrent_params p;
p.ti = std::make_shared<torrent_info>(std::cref(*t));
p.save_path = combine_path(test_path, "tmp2");
p.storage_mode = storage_mode_sparse;
torrent_handle h = ses.add_torrent(std::move(p), ec);
h.rename_file(file_index_t(0), "testing_renamed_files");
std::cout << "renaming file" << std::endl;
bool renamed = false;
for (int i = 0; i < 30; ++i)
{
if (print_alerts(ses, "ses", true, true, &got_file_rename_alert)) renamed = true;
torrent_status s = h.status();
if (s.state == torrent_status::seeding && renamed) break;
std::this_thread::sleep_for(lt::milliseconds(100));
}
std::cout << "stop loop" << std::endl;
torrent_status s = h.status();
TEST_CHECK(s.state == torrent_status::seeding);
h.save_resume_data();
alert const* ra = wait_for_alert(ses, save_resume_data_alert::alert_type);
TEST_CHECK(ra);
if (ra) resume = alert_cast<save_resume_data_alert>(ra)->params;
ses.remove_torrent(h);
}
TEST_CHECK(!exists(combine_path(test_path, "tmp2/temporary")));
TEST_CHECK(exists(combine_path(test_path, "tmp2/testing_renamed_files")));
TEST_CHECK(!resume.renamed_files.empty());
entry resume_ent = write_resume_data(resume);
std::cout << resume_ent.to_string() << "\n";
// make sure the fast resume check succeeds, even though we renamed the file
{
settings_pack pack = settings();
lt::session ses(pack);
add_torrent_params p;
std::vector<char> resume_data;
bencode(std::back_inserter(resume_data), resume_ent);
#ifndef TORRENT_NO_DEPRECATE
if (test_deprecated)
{
p.resume_data = resume_data;
}
else
#endif
{
p = read_resume_data(resume_data, ec);
TEST_CHECK(!ec);
}
p.ti = std::make_shared<torrent_info>(std::cref(*t));
p.save_path = combine_path(test_path, "tmp2");
p.storage_mode = storage_mode_sparse;
torrent_handle h = ses.add_torrent(std::move(p), ec);
torrent_status stat;
for (int i = 0; i < 50; ++i)
{
stat = h.status();
print_alerts(ses, "ses");
if (stat.state == torrent_status::seeding)
break;
std::this_thread::sleep_for(lt::milliseconds(100));
}
TEST_CHECK(stat.state == torrent_status::seeding);
h.save_resume_data();
alert const* ra = wait_for_alert(ses, save_resume_data_alert::alert_type);
TEST_CHECK(ra);
if (ra) resume = alert_cast<save_resume_data_alert>(ra)->params;
ses.remove_torrent(h);
}
TEST_CHECK(!resume.renamed_files.empty());
resume_ent = write_resume_data(resume);
std::cout << resume_ent.to_string() << "\n";
remove_all(combine_path(test_path, "tmp2"), ec);
if (ec && ec != boost::system::errc::no_such_file_or_directory)
std::cout << "remove_all '" << combine_path(test_path, "tmp2")
<< "': " << ec.message() << std::endl;
}
TORRENT_TEST(rename_file_fastresume)
{
test_rename_file_fastresume(false);
}
#ifndef TORRENT_NO_DEPRECATE
TORRENT_TEST(rename_file_fastresume_deprecated)
{
test_rename_file_fastresume(true);
}
#endif
void alloc_iov(iovec_t* iov, int num_bufs)
{
for (int i = 0; i < num_bufs; ++i)
{
iov[i] = { new char[num_bufs * (i + 1)]
, static_cast<std::size_t>(num_bufs * (i + 1)) };
}
}
// TODO: this should take a span of iovec_ts
void fill_pattern(iovec_t* iov, int num_bufs)
{
int counter = 0;
for (int i = 0; i < num_bufs; ++i)
{
for (char& v : iov[i])
{
v = counter & 0xff;
++counter;
}
}
}
bool check_pattern(std::vector<char> const& buf, int counter)
{
unsigned char* p = (unsigned char*)&buf[0];
for (int k = 0; k < int(buf.size()); ++k)
{
if (p[k] != (counter & 0xff)) return false;
++counter;
}
return true;
}
// TODO: this should take a span
void fill_pattern2(iovec_t* iov, int num_bufs)
{
for (int i = 0; i < num_bufs; ++i)
{
memset(iov[i].data(), 0xfe, iov[i].size());
}
}
// TODO: this should take a span
void free_iov(iovec_t* iov, int num_bufs)
{
for (int i = 0; i < num_bufs; ++i)
{