forked from arvidn/libtorrent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_pe_crypto.cpp
197 lines (168 loc) · 6.39 KB
/
test_pe_crypto.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
/*
Copyright (c) 2007, Un Shyam
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 <algorithm>
#include <iostream>
#include "libtorrent/pe_crypto.hpp"
#include "libtorrent/session.hpp"
#include "setup_transfer.hpp"
#include "test.hpp"
#include "settings.hpp"
#include "setup_swarm.hpp"
#include "utils.hpp"
#if !defined TORRENT_DISABLE_ENCRYPTION
using namespace lt;
char const* pe_policy(int const policy)
{
if (policy == settings_pack::pe_disabled) return "disabled";
else if (policy == settings_pack::pe_enabled) return "enabled";
else if (policy == settings_pack::pe_forced) return "forced";
return "unknown";
}
void display_pe_settings(lt::settings_pack const& s)
{
std::printf("out_enc_policy - %s\tin_enc_policy - %s\n"
, pe_policy(s.get_int(settings_pack::out_enc_policy))
, pe_policy(s.get_int(settings_pack::in_enc_policy)));
std::printf("enc_level - %s\t\tprefer_rc4 - %s\n"
, s.get_int(settings_pack::allowed_enc_level) == settings_pack::pe_plaintext ? "plaintext"
: s.get_int(settings_pack::allowed_enc_level) == settings_pack::pe_rc4 ? "rc4"
: s.get_int(settings_pack::allowed_enc_level) == settings_pack::pe_both ? "both" : "unknown"
, s.get_bool(settings_pack::prefer_rc4) ? "true": "false");
}
void test_transfer(int enc_policy, int level, bool prefer_rc4)
{
lt::settings_pack default_settings = settings();
default_settings.set_bool(settings_pack::prefer_rc4, prefer_rc4);
default_settings.set_int(settings_pack::in_enc_policy, enc_policy);
default_settings.set_int(settings_pack::out_enc_policy, enc_policy);
default_settings.set_int(settings_pack::allowed_enc_level, level);
display_pe_settings(default_settings);
sim::default_config cfg;
sim::simulation sim{cfg};
lt::add_torrent_params default_add_torrent;
default_add_torrent.flags &= ~lt::torrent_flags::paused;
default_add_torrent.flags &= ~lt::torrent_flags::auto_managed;
setup_swarm(2, swarm_test::download, sim, default_settings, default_add_torrent
// add session
, [](lt::settings_pack& pack) {
pack.set_int(settings_pack::out_enc_policy, settings_pack::pe_enabled);
pack.set_int(settings_pack::in_enc_policy, settings_pack::pe_enabled);
pack.set_int(settings_pack::allowed_enc_level, settings_pack::pe_both);
pack.set_bool(settings_pack::prefer_rc4, false);
}
// add torrent
, [](lt::add_torrent_params&) {}
// on alert
, [](lt::alert const*, lt::session&) {}
// terminate
, [](int ticks, lt::session& ses) -> bool
{
if (ticks > 20)
{
TEST_ERROR("timeout");
return true;
}
return is_seed(ses);
});
}
TORRENT_TEST(pe_disabled)
{
test_transfer(settings_pack::pe_disabled, settings_pack::pe_plaintext, false);
}
TORRENT_TEST(forced_plaintext)
{
test_transfer(settings_pack::pe_forced, settings_pack::pe_plaintext, false);
}
TORRENT_TEST(forced_rc4)
{
test_transfer(settings_pack::pe_forced, settings_pack::pe_rc4, true);
}
TORRENT_TEST(forced_both)
{
test_transfer(settings_pack::pe_forced, settings_pack::pe_both, false);
}
TORRENT_TEST(forced_both_prefer_rc4)
{
test_transfer(settings_pack::pe_forced, settings_pack::pe_both, true);
}
TORRENT_TEST(enabled_plaintext)
{
test_transfer(settings_pack::pe_forced, settings_pack::pe_plaintext, false);
}
TORRENT_TEST(enabled_rc4)
{
test_transfer(settings_pack::pe_enabled, settings_pack::pe_rc4, false);
}
TORRENT_TEST(enabled_both)
{
test_transfer(settings_pack::pe_enabled, settings_pack::pe_both, false);
}
TORRENT_TEST(enabled_both_prefer_rc4)
{
test_transfer(settings_pack::pe_enabled, settings_pack::pe_both, true);
}
// make sure that a peer with encryption disabled cannot talk to a peer with
// encryption forced
TORRENT_TEST(disabled_failing)
{
lt::settings_pack default_settings = settings();
default_settings.set_bool(settings_pack::prefer_rc4, false);
default_settings.set_int(settings_pack::in_enc_policy, settings_pack::pe_disabled);
default_settings.set_int(settings_pack::out_enc_policy, settings_pack::pe_disabled);
default_settings.set_int(settings_pack::allowed_enc_level, settings_pack::pe_both);
display_pe_settings(default_settings);
sim::default_config cfg;
sim::simulation sim{cfg};
lt::add_torrent_params default_add_torrent;
default_add_torrent.flags &= ~lt::torrent_flags::paused;
default_add_torrent.flags &= ~lt::torrent_flags::auto_managed;
setup_swarm(2, swarm_test::download, sim, default_settings, default_add_torrent
// add session
, [](lt::settings_pack& pack) {
pack.set_int(settings_pack::out_enc_policy, settings_pack::pe_forced);
pack.set_int(settings_pack::in_enc_policy, settings_pack::pe_forced);
pack.set_int(settings_pack::allowed_enc_level, settings_pack::pe_both);
pack.set_bool(settings_pack::prefer_rc4, true);
}
// add torrent
, [](lt::add_torrent_params&) {}
// on alert
, [](lt::alert const*, lt::session&) {}
// terminate
, [](int ticks, lt::session& ses) -> bool
{
// this download should never succeed
TEST_CHECK(!is_seed(ses));
return ticks > 120;
});
}
#else
TORRENT_TEST(disabled)
{
std::printf("PE test not run because it's disabled\n");
}
#endif