3 Copyright (c) 2007, Un Shyam
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
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.
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.
36 #include "libtorrent/hasher.hpp"
37 #include "libtorrent/pe_crypto.hpp"
38 #include "libtorrent/session.hpp"
39 #include <boost/filesystem/convenience.hpp>
41 #include "setup_transfer.hpp"
44 #ifndef TORRENT_DISABLE_ENCRYPTION
46 void display_pe_policy(libtorrent::pe_settings::enc_policy policy
)
48 using namespace libtorrent
;
51 if (policy
== pe_settings::disabled
) cerr
<< "disabled ";
52 else if (policy
== pe_settings::enabled
) cerr
<< "enabled ";
53 else if (policy
== pe_settings::forced
) cerr
<< "forced ";
56 void display_pe_settings(libtorrent::pe_settings s
)
58 using namespace libtorrent
;
61 cerr
<< "out_enc_policy - ";
62 display_pe_policy(s
.out_enc_policy
);
63 cerr
<< "\tin_enc_policy - ";
64 display_pe_policy(s
.in_enc_policy
);
66 cerr
<< "\nenc_level - ";
67 if (s
.allowed_enc_level
== pe_settings::plaintext
) cerr
<< "plaintext ";
68 else if (s
.allowed_enc_level
== pe_settings::rc4
) cerr
<< "rc4 ";
69 else if (s
.allowed_enc_level
== pe_settings::both
) cerr
<< "both ";
71 cerr
<< "\t\tprefer_rc4 - ";
72 (s
.prefer_rc4
) ? cerr
<< "true" : cerr
<< "false";
76 void test_transfer(libtorrent::pe_settings::enc_policy policy
,
77 libtorrent::pe_settings::enc_level level
= libtorrent::pe_settings::both
,
78 bool pref_rc4
= false)
80 using namespace libtorrent
;
83 session
ses1(fingerprint("LT", 0, 1, 0, 0), std::make_pair(48800, 49000));
84 session
ses2(fingerprint("LT", 0, 1, 0, 0), std::make_pair(49800, 50000));
87 s
.out_enc_policy
= libtorrent::pe_settings::enabled
;
88 s
.in_enc_policy
= libtorrent::pe_settings::enabled
;
90 s
.allowed_enc_level
= pe_settings::both
;
91 ses2
.set_pe_settings(s
);
93 s
.out_enc_policy
= policy
;
94 s
.in_enc_policy
= policy
;
95 s
.allowed_enc_level
= level
;
96 s
.prefer_rc4
= pref_rc4
;
97 ses1
.set_pe_settings(s
);
99 // s = ses1.get_pe_settings();
100 // cerr << " Session1 \n";
101 // display_pe_settings(s);
102 // s = ses2.get_pe_settings();
103 // cerr << " Session2 \n";
104 // display_pe_settings(s);
109 using boost::tuples::ignore
;
110 boost::tie(tor1
, tor2
, ignore
) = setup_transfer(&ses1
, &ses2
, 0, true, false, true, "_pe");
112 std::cerr
<< "waiting for transfer to complete\n";
114 for (int i
= 0; i
< 50; ++i
)
117 print_alerts(ses1
, "ses1");
118 print_alerts(ses2
, "ses2");
120 if (tor2
.is_seed()) break;
124 TEST_CHECK(tor2
.is_seed());
125 if (tor2
.is_seed()) std::cerr
<< "done\n";
127 using boost::filesystem::remove_all
;
128 remove_all("./tmp1_pe");
129 remove_all("./tmp2_pe");
130 remove_all("./tmp3_pe");
136 using namespace libtorrent
;
139 for (int rep
= 0; rep
< repcount
; ++rep
)
141 dh_key_exchange DH1
, DH2
;
143 DH1
.compute_secret(DH2
.get_local_key());
144 DH2
.compute_secret(DH1
.get_local_key());
146 TEST_CHECK(std::equal(DH1
.get_secret(), DH1
.get_secret() + 96, DH2
.get_secret()));
149 dh_key_exchange DH1
, DH2
;
150 DH1
.compute_secret(DH2
.get_local_key());
151 DH2
.compute_secret(DH1
.get_local_key());
153 TEST_CHECK(std::equal(DH1
.get_secret(), DH1
.get_secret() + 96, DH2
.get_secret()));
155 sha1_hash test1_key
= hasher("test1_key",8).final();
156 sha1_hash test2_key
= hasher("test2_key",8).final();
158 RC4_handler
RC41(test2_key
, test1_key
);
159 RC4_handler
RC42(test1_key
, test2_key
);
161 for (int rep
= 0; rep
< repcount
; ++rep
)
163 std::size_t buf_len
= rand() % (512 * 1024);
164 char* buf
= new char[buf_len
];
165 char* zero_buf
= new char[buf_len
];
167 std::fill(buf
, buf
+ buf_len
, 0);
168 std::fill(zero_buf
, zero_buf
+ buf_len
, 0);
170 RC41
.encrypt(buf
, buf_len
);
171 RC42
.decrypt(buf
, buf_len
);
172 TEST_CHECK(std::equal(buf
, buf
+ buf_len
, zero_buf
));
174 RC42
.encrypt(buf
, buf_len
);
175 RC41
.decrypt(buf
, buf_len
);
176 TEST_CHECK(std::equal(buf
, buf
+ buf_len
, zero_buf
));
183 test_transfer(pe_settings::disabled
);
185 test_transfer(pe_settings::forced
, pe_settings::plaintext
);
186 test_transfer(pe_settings::forced
, pe_settings::rc4
);
187 test_transfer(pe_settings::forced
, pe_settings::both
, false);
188 test_transfer(pe_settings::forced
, pe_settings::both
, true);
190 test_transfer(pe_settings::enabled
, pe_settings::plaintext
);
191 test_transfer(pe_settings::enabled
, pe_settings::rc4
);
192 test_transfer(pe_settings::enabled
, pe_settings::both
, false);
193 test_transfer(pe_settings::enabled
, pe_settings::both
, true);
202 std::cerr
<< "PE test not run because it's disabled" << std::endl
;