1 /*
2 
3 Copyright (c) 2017, 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 "test.hpp"
34 #include "libtorrent/add_torrent_params.hpp"
35 #include "libtorrent/session.hpp"
36 #include "libtorrent/torrent_handle.hpp"
37 #include "libtorrent/torrent_info.hpp"
38 #include "libtorrent/aux_/path.hpp"
39 #include "settings.hpp"
40 
41 using namespace libtorrent;
42 namespace lt = libtorrent;
43 
44 namespace {
45 
file(std::string name)46 std::string file(std::string name)
47 {
48 	return combine_path(parent_path(current_working_directory())
49 		, combine_path("test_torrents", name));
50 }
51 
test_add_and_get_flags(torrent_flags_t const flags)52 void test_add_and_get_flags(torrent_flags_t const flags)
53 {
54 	session ses(settings());
55 	add_torrent_params p;
56 	p.save_path = ".";
57 	error_code ec;
58 	p.ti = std::make_shared<torrent_info>(file("base.torrent"),
59 		std::ref(ec));
60 	TEST_CHECK(!ec);
61 	p.flags = flags;
62 	const torrent_handle h = ses.add_torrent(p);
63 	TEST_CHECK(h.is_valid());
64 	TEST_EQUAL(h.flags() & flags, flags);
65 }
66 
test_set_after_add(torrent_flags_t const flags)67 void test_set_after_add(torrent_flags_t const flags)
68 {
69 	session ses(settings());
70 	add_torrent_params p;
71 	p.save_path = ".";
72 	error_code ec;
73 	p.ti = std::make_shared<torrent_info>(file("base.torrent"),
74 		std::ref(ec));
75 	TEST_CHECK(!ec);
76 	p.flags = torrent_flags::all & ~flags;
77 	const torrent_handle h = ses.add_torrent(p);
78 	TEST_CHECK(h.is_valid());
79 	TEST_EQUAL(h.flags() & flags, torrent_flags_t{});
80 	h.set_flags(flags);
81 	TEST_EQUAL(h.flags() & flags, flags);
82 }
83 
test_unset_after_add(torrent_flags_t const flags)84 void test_unset_after_add(torrent_flags_t const flags)
85 {
86 	session ses(settings());
87 	add_torrent_params p;
88 	p.save_path = ".";
89 	error_code ec;
90 	p.ti = std::make_shared<torrent_info>(file("base.torrent"),
91 		std::ref(ec));
92 	TEST_CHECK(!ec);
93 	p.flags = flags;
94 	const torrent_handle h = ses.add_torrent(p);
95 	TEST_CHECK(h.is_valid());
96 	TEST_EQUAL(h.flags() & flags, flags);
97 	h.unset_flags(flags);
98 	TEST_EQUAL(h.flags() & flags, torrent_flags_t{});
99 }
100 
101 } // anonymous namespace
102 
TORRENT_TEST(flag_seed_mode)103 TORRENT_TEST(flag_seed_mode)
104 {
105 	// seed-mode (can't be set after adding)
106 	test_add_and_get_flags(torrent_flags::seed_mode);
107 	test_unset_after_add(torrent_flags::seed_mode);
108 }
109 
TORRENT_TEST(flag_upload_mode)110 TORRENT_TEST(flag_upload_mode)
111 {
112 	// upload-mode
113 	test_add_and_get_flags(torrent_flags::upload_mode);
114 	test_set_after_add(torrent_flags::upload_mode);
115 	test_unset_after_add(torrent_flags::upload_mode);
116 }
117 
118 #ifndef TORRENT_DISABLE_SHARE_MODE
TORRENT_TEST(flag_share_mode)119 TORRENT_TEST(flag_share_mode)
120 {
121 	// share-mode
122 	test_add_and_get_flags(torrent_flags::share_mode);
123 	test_set_after_add(torrent_flags::share_mode);
124 	test_unset_after_add(torrent_flags::share_mode);
125 }
126 #endif
127 
TORRENT_TEST(flag_apply_ip_filter)128 TORRENT_TEST(flag_apply_ip_filter)
129 {
130 	// apply-ip-filter
131 	test_add_and_get_flags(torrent_flags::apply_ip_filter);
132 	test_set_after_add(torrent_flags::apply_ip_filter);
133 	test_unset_after_add(torrent_flags::apply_ip_filter);
134 }
135 
TORRENT_TEST(flag_paused)136 TORRENT_TEST(flag_paused)
137 {
138 	// paused
139 	test_add_and_get_flags(torrent_flags::paused);
140 	// TODO: change to a different test setup. currently always paused.
141 	//test_set_after_add(torrent_flags::paused);
142 	//test_unset_after_add(torrent_flags::paused);
143 }
144 
TORRENT_TEST(flag_auto_managed)145 TORRENT_TEST(flag_auto_managed)
146 {
147 	// auto-managed
148 	test_add_and_get_flags(torrent_flags::auto_managed);
149 	test_set_after_add(torrent_flags::auto_managed);
150 	test_unset_after_add(torrent_flags::auto_managed);
151 }
152 
153 #ifndef TORRENT_DISABLE_SUPERSEEDING
TORRENT_TEST(flag_super_seeding)154 TORRENT_TEST(flag_super_seeding)
155 {
156 	// super-seeding
157 	test_add_and_get_flags(torrent_flags::super_seeding);
158 	test_set_after_add(torrent_flags::super_seeding);
159 	test_unset_after_add(torrent_flags::super_seeding);
160 }
161 #endif
162 
TORRENT_TEST(flag_sequential_download)163 TORRENT_TEST(flag_sequential_download)
164 {
165 	// sequential-download
166 	test_add_and_get_flags(torrent_flags::sequential_download);
167 	test_set_after_add(torrent_flags::sequential_download);
168 	test_unset_after_add(torrent_flags::sequential_download);
169 }
170 
TORRENT_TEST(flag_stop_when_ready)171 TORRENT_TEST(flag_stop_when_ready)
172 {
173 	// stop-when-ready
174 	test_add_and_get_flags(torrent_flags::stop_when_ready);
175 	// setting stop-when-ready when already stopped has no effect.
176 	// TODO: change to a different test setup. currently always paused.
177 	//test_set_after_add(torrent_flags::stop_when_ready);
178 	test_unset_after_add(torrent_flags::stop_when_ready);
179 }
180 
TORRENT_TEST(flag_disable_dht)181 TORRENT_TEST(flag_disable_dht)
182 {
183 	test_add_and_get_flags(torrent_flags::disable_dht);
184 	test_set_after_add(torrent_flags::disable_dht);
185 	test_unset_after_add(torrent_flags::disable_dht);
186 }
187 
188 
TORRENT_TEST(flag_disable_lsd)189 TORRENT_TEST(flag_disable_lsd)
190 {
191 	test_add_and_get_flags(torrent_flags::disable_lsd);
192 	test_set_after_add(torrent_flags::disable_lsd);
193 	test_unset_after_add(torrent_flags::disable_lsd);
194 }
195 
TORRENT_TEST(flag_disable_pex)196 TORRENT_TEST(flag_disable_pex)
197 {
198 	test_add_and_get_flags(torrent_flags::disable_pex);
199 	test_set_after_add(torrent_flags::disable_pex);
200 	test_unset_after_add(torrent_flags::disable_pex);
201 }
202