1 /*
2 
3 Copyright (c) 2012, Arvid Norberg
4 All rights reserved.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
9 
10     * Redistributions of source code must retain the above copyright
11       notice, this list of conditions and the following disclaimer.
12     * Redistributions in binary form must reproduce the above copyright
13       notice, this list of conditions and the following disclaimer in
14       the documentation and/or other materials provided with the distribution.
15     * Neither the name of the author nor the names of its
16       contributors may be used to endorse or promote products derived
17       from this software without specific prior written permission.
18 
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 POSSIBILITY OF SUCH DAMAGE.
30 
31 */
32 
33 #include <cstring>
34 #include <array>
35 
36 #include "test.hpp"
37 #include "libtorrent/part_file.hpp"
38 #include "libtorrent/aux_/path.hpp"
39 #include "libtorrent/error_code.hpp"
40 
41 using namespace lt;
42 
TORRENT_TEST(part_file)43 TORRENT_TEST(part_file)
44 {
45 	error_code ec;
46 	std::string cwd = complete(".");
47 
48 	remove_all(combine_path(cwd, "partfile_test_dir"), ec);
49 	if (ec) std::printf("remove_all: %s\n", ec.message().c_str());
50 	remove_all(combine_path(cwd, "partfile_test_dir2"), ec);
51 	if (ec) std::printf("remove_all: %s\n", ec.message().c_str());
52 
53 	int piece_size = 16 * 0x4000;
54 	std::array<char, 1024> buf;
55 
56 	{
57 		create_directory(combine_path(cwd, "partfile_test_dir"), ec);
58 		if (ec) std::printf("create_directory: %s\n", ec.message().c_str());
59 		create_directory(combine_path(cwd, "partfile_test_dir2"), ec);
60 		if (ec) std::printf("create_directory: %s\n", ec.message().c_str());
61 
62 		part_file pf(combine_path(cwd, "partfile_test_dir"), "partfile.parts", 100, piece_size);
63 		pf.flush_metadata(ec);
64 		if (ec) std::printf("flush_metadata: %s\n", ec.message().c_str());
65 
66 		// since we don't have anything in the part file, it will have
67 		// not have been created yet
68 
69 		TEST_CHECK(!exists(combine_path(combine_path(cwd, "partfile_test_dir"), "partfile.parts")));
70 
71 		// write something to the metadata file
72 		for (int i = 0; i < 1024; ++i) buf[std::size_t(i)] = char(i & 0xff);
73 
74 		iovec_t v = buf;
75 		pf.writev(v, piece_index_t(10), 0, ec);
76 		if (ec) std::printf("part_file::writev: %s\n", ec.message().c_str());
77 
78 		pf.flush_metadata(ec);
79 		if (ec) std::printf("flush_metadata: %s\n", ec.message().c_str());
80 
81 		// now wwe should have created the partfile
82 		TEST_CHECK(exists(combine_path(combine_path(cwd, "partfile_test_dir"), "partfile.parts")));
83 
84 		pf.move_partfile(combine_path(cwd, "partfile_test_dir2"), ec);
85 		TEST_CHECK(!ec);
86 		if (ec) std::printf("move_partfile: %s\n", ec.message().c_str());
87 
88 		TEST_CHECK(!exists(combine_path(combine_path(cwd, "partfile_test_dir"), "partfile.parts")));
89 		TEST_CHECK(exists(combine_path(combine_path(cwd, "partfile_test_dir2"), "partfile.parts")));
90 
91 		buf.fill(0);
92 
93 		pf.readv(v, piece_index_t(10), 0, ec);
94 		if (ec) std::printf("part_file::readv: %s\n", ec.message().c_str());
95 
96 		for (int i = 0; i < 1024; ++i)
97 			TEST_CHECK(buf[std::size_t(i)] == char(i));
98 	}
99 
100 	{
101 		// load the part file back in
102 		part_file pf(combine_path(cwd, "partfile_test_dir2"), "partfile.parts", 100, piece_size);
103 
104 		buf.fill(0);
105 
106 		iovec_t v = buf;
107 		pf.readv(v, piece_index_t(10), 0, ec);
108 		if (ec) std::printf("part_file::readv: %s\n", ec.message().c_str());
109 
110 		for (int i = 0; i < 1024; ++i)
111 			TEST_CHECK(buf[std::size_t(i)] == static_cast<char>(i));
112 
113 		// test exporting the piece to a file
114 
115 		std::string output_filename = combine_path(combine_path(cwd, "partfile_test_dir")
116 			, "part_file_test_export");
117 
118 		pf.export_file([](std::int64_t file_offset, span<char> buf_data)
119 		{
120 			for (char i : buf_data)
121 			{
122 				// make sure we got the bytes we expected
123 				TEST_CHECK(i == static_cast<char>(file_offset));
124 				++file_offset;
125 			}
126 		}, 10 * piece_size, 1024, ec);
127 		if (ec) std::printf("export_file: %s\n", ec.message().c_str());
128 
129 		pf.free_piece(piece_index_t(10));
130 
131 		pf.flush_metadata(ec);
132 		if (ec) std::printf("flush_metadata: %s\n", ec.message().c_str());
133 
134 		// we just removed the last piece. The partfile does not
135 		// contain anything anymore, it should have deleted itself
136 		TEST_CHECK(!exists(combine_path(combine_path(cwd, "partfile_test_dir2"), "partfile.parts"), ec));
137 		TEST_CHECK(!ec);
138 		if (ec) std::printf("exists: %s\n", ec.message().c_str());
139 	}
140 }
141