From 6c22d426d487671903ed48487296ad6678806d2a Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Wed, 5 Feb 2014 09:38:32 +0000 Subject: [PATCH] make all unit tests pass msvc's /RTCc instrumentation (no implicit integer truncation in casts) --- ed25519/src/fe.c | 64 +++++------ ed25519/src/sc.c | 128 ++++++++++----------- include/libtorrent/bitfield.hpp | 2 +- include/libtorrent/kademlia/node_entry.hpp | 2 +- include/libtorrent/sha1_hash.hpp | 2 +- src/bt_peer_connection.cpp | 9 +- src/kademlia/node_id.cpp | 8 +- src/kademlia/rpc_manager.cpp | 2 +- src/mpi.c | 2 +- src/pe_crypto.cpp | 4 +- src/session_impl.cpp | 5 +- src/utp_socket_manager.cpp | 2 +- src/utp_stream.cpp | 18 +-- test/main.cpp | 2 +- test/setup_transfer.cpp | 17 ++- test/setup_transfer.hpp | 1 + test/socks.py | 8 +- test/test_dht.cpp | 8 +- test/test_primitives.cpp | 2 +- test/test_storage.cpp | 12 +- test/web_seed_suite.cpp | 4 +- test/web_server.py | 2 +- 22 files changed, 160 insertions(+), 144 deletions(-) diff --git a/ed25519/src/fe.c b/ed25519/src/fe.c index 448e3e920..37abefb5f 100644 --- a/ed25519/src/fe.c +++ b/ed25519/src/fe.c @@ -1456,36 +1456,36 @@ void fe_tobytes(unsigned char *s, const fe h) { evidently 2^255 h10-2^255 q = 0. Goal: Output h0+...+2^230 h9. */ - s[0] = (unsigned char) (h0 >> 0); - s[1] = (unsigned char) (h0 >> 8); - s[2] = (unsigned char) (h0 >> 16); - s[3] = (unsigned char) ((h0 >> 24) | (h1 << 2)); - s[4] = (unsigned char) (h1 >> 6); - s[5] = (unsigned char) (h1 >> 14); - s[6] = (unsigned char) ((h1 >> 22) | (h2 << 3)); - s[7] = (unsigned char) (h2 >> 5); - s[8] = (unsigned char) (h2 >> 13); - s[9] = (unsigned char) ((h2 >> 21) | (h3 << 5)); - s[10] = (unsigned char) (h3 >> 3); - s[11] = (unsigned char) (h3 >> 11); - s[12] = (unsigned char) ((h3 >> 19) | (h4 << 6)); - s[13] = (unsigned char) (h4 >> 2); - s[14] = (unsigned char) (h4 >> 10); - s[15] = (unsigned char) (h4 >> 18); - s[16] = (unsigned char) (h5 >> 0); - s[17] = (unsigned char) (h5 >> 8); - s[18] = (unsigned char) (h5 >> 16); - s[19] = (unsigned char) ((h5 >> 24) | (h6 << 1)); - s[20] = (unsigned char) (h6 >> 7); - s[21] = (unsigned char) (h6 >> 15); - s[22] = (unsigned char) ((h6 >> 23) | (h7 << 3)); - s[23] = (unsigned char) (h7 >> 5); - s[24] = (unsigned char) (h7 >> 13); - s[25] = (unsigned char) ((h7 >> 21) | (h8 << 4)); - s[26] = (unsigned char) (h8 >> 4); - s[27] = (unsigned char) (h8 >> 12); - s[28] = (unsigned char) ((h8 >> 20) | (h9 << 6)); - s[29] = (unsigned char) (h9 >> 2); - s[30] = (unsigned char) (h9 >> 10); - s[31] = (unsigned char) (h9 >> 18); + s[0] = (unsigned char) ((h0 >> 0) & 0xff); + s[1] = (unsigned char) ((h0 >> 8) & 0xff); + s[2] = (unsigned char) ((h0 >> 16) & 0xff); + s[3] = (unsigned char) (((h0 >> 24) | (h1 << 2)) & 0xff); + s[4] = (unsigned char) ((h1 >> 6) & 0xff); + s[5] = (unsigned char) ((h1 >> 14) & 0xff); + s[6] = (unsigned char) (((h1 >> 22) | (h2 << 3)) & 0xff); + s[7] = (unsigned char) ((h2 >> 5) & 0xff); + s[8] = (unsigned char) ((h2 >> 13) & 0xff); + s[9] = (unsigned char) (((h2 >> 21) | (h3 << 5)) & 0xff); + s[10] = (unsigned char) ((h3 >> 3) & 0xff); + s[11] = (unsigned char) ((h3 >> 11) & 0xff); + s[12] = (unsigned char) (((h3 >> 19) | (h4 << 6)) & 0xff); + s[13] = (unsigned char) ((h4 >> 2) & 0xff); + s[14] = (unsigned char) ((h4 >> 10) & 0xff); + s[15] = (unsigned char) ((h4 >> 18) & 0xff); + s[16] = (unsigned char) ((h5 >> 0) & 0xff); + s[17] = (unsigned char) ((h5 >> 8) & 0xff); + s[18] = (unsigned char) ((h5 >> 16) & 0xff); + s[19] = (unsigned char) (((h5 >> 24) | (h6 << 1)) & 0xff); + s[20] = (unsigned char) ((h6 >> 7) & 0xff); + s[21] = (unsigned char) ((h6 >> 15) & 0xff); + s[22] = (unsigned char) (((h6 >> 23) | (h7 << 3)) & 0xff); + s[23] = (unsigned char) ((h7 >> 5) & 0xff); + s[24] = (unsigned char) ((h7 >> 13) & 0xff); + s[25] = (unsigned char) (((h7 >> 21) | (h8 << 4)) & 0xff); + s[26] = (unsigned char) ((h8 >> 4) & 0xff); + s[27] = (unsigned char) ((h8 >> 12) & 0xff); + s[28] = (unsigned char) (((h8 >> 20) | (h9 << 6)) & 0xff); + s[29] = (unsigned char) ((h9 >> 2) & 0xff); + s[30] = (unsigned char) ((h9 >> 10) & 0xff); + s[31] = (unsigned char) ((h9 >> 18) & 0xff); } diff --git a/ed25519/src/sc.c b/ed25519/src/sc.c index ca5bad2ca..24e3c86c7 100644 --- a/ed25519/src/sc.c +++ b/ed25519/src/sc.c @@ -312,38 +312,38 @@ void sc_reduce(unsigned char *s) { s11 += carry10; s10 -= carry10 << 21; - s[0] = (unsigned char) (s0 >> 0); - s[1] = (unsigned char) (s0 >> 8); - s[2] = (unsigned char) ((s0 >> 16) | (s1 << 5)); - s[3] = (unsigned char) (s1 >> 3); - s[4] = (unsigned char) (s1 >> 11); - s[5] = (unsigned char) ((s1 >> 19) | (s2 << 2)); - s[6] = (unsigned char) (s2 >> 6); - s[7] = (unsigned char) ((s2 >> 14) | (s3 << 7)); - s[8] = (unsigned char) (s3 >> 1); - s[9] = (unsigned char) (s3 >> 9); - s[10] = (unsigned char) ((s3 >> 17) | (s4 << 4)); - s[11] = (unsigned char) (s4 >> 4); - s[12] = (unsigned char) (s4 >> 12); - s[13] = (unsigned char) ((s4 >> 20) | (s5 << 1)); - s[14] = (unsigned char) (s5 >> 7); - s[15] = (unsigned char) ((s5 >> 15) | (s6 << 6)); - s[16] = (unsigned char) (s6 >> 2); - s[17] = (unsigned char) (s6 >> 10); - s[18] = (unsigned char) ((s6 >> 18) | (s7 << 3)); - s[19] = (unsigned char) (s7 >> 5); - s[20] = (unsigned char) (s7 >> 13); - s[21] = (unsigned char) (s8 >> 0); - s[22] = (unsigned char) (s8 >> 8); - s[23] = (unsigned char) ((s8 >> 16) | (s9 << 5)); - s[24] = (unsigned char) (s9 >> 3); - s[25] = (unsigned char) (s9 >> 11); - s[26] = (unsigned char) ((s9 >> 19) | (s10 << 2)); - s[27] = (unsigned char) (s10 >> 6); - s[28] = (unsigned char) ((s10 >> 14) | (s11 << 7)); - s[29] = (unsigned char) (s11 >> 1); - s[30] = (unsigned char) (s11 >> 9); - s[31] = (unsigned char) (s11 >> 17); + s[0] = (unsigned char) ((s0 >> 0) & 0xff); + s[1] = (unsigned char) ((s0 >> 8 & 0xff)); + s[2] = (unsigned char) (((s0 >> 16) | (s1 << 5)) & 0xff); + s[3] = (unsigned char) ((s1 >> 3) & 0xff); + s[4] = (unsigned char) ((s1 >> 11) & 0xff); + s[5] = (unsigned char) (((s1 >> 19) | (s2 << 2)) & 0xff); + s[6] = (unsigned char) ((s2 >> 6) & 0xff); + s[7] = (unsigned char) (((s2 >> 14) | (s3 << 7)) & 0xff); + s[8] = (unsigned char) ((s3 >> 1) & 0xff); + s[9] = (unsigned char) ((s3 >> 9) & 0xff); + s[10] = (unsigned char) (((s3 >> 17) | (s4 << 4)) & 0xff); + s[11] = (unsigned char) ((s4 >> 4) & 0xff); + s[12] = (unsigned char) ((s4 >> 12) & 0xff); + s[13] = (unsigned char) (((s4 >> 20) | (s5 << 1)) & 0xff); + s[14] = (unsigned char) ((s5 >> 7) & 0xff); + s[15] = (unsigned char) (((s5 >> 15) | (s6 << 6)) & 0xff); + s[16] = (unsigned char) ((s6 >> 2) & 0xff); + s[17] = (unsigned char) ((s6 >> 10) & 0xff); + s[18] = (unsigned char) (((s6 >> 18) | (s7 << 3)) & 0xff); + s[19] = (unsigned char) ((s7 >> 5) & 0xff); + s[20] = (unsigned char) ((s7 >> 13) & 0xff); + s[21] = (unsigned char) ((s8 >> 0) & 0xff); + s[22] = (unsigned char) ((s8 >> 8) & 0xff); + s[23] = (unsigned char) (((s8 >> 16) | (s9 << 5)) & 0xff); + s[24] = (unsigned char) ((s9 >> 3) & 0xff); + s[25] = (unsigned char) ((s9 >> 11) & 0xff); + s[26] = (unsigned char) (((s9 >> 19) | (s10 << 2)) & 0xff); + s[27] = (unsigned char) ((s10 >> 6) & 0xff); + s[28] = (unsigned char) (((s10 >> 14) | (s11 << 7)) & 0xff); + s[29] = (unsigned char) ((s11 >> 1) & 0xff); + s[30] = (unsigned char) ((s11 >> 9) & 0xff); + s[31] = (unsigned char) ((s11 >> 17) & 0xff); } @@ -774,36 +774,36 @@ void sc_muladd(unsigned char *s, const unsigned char *a, const unsigned char *b, s11 += carry10; s10 -= carry10 << 21; - s[0] = (unsigned char) (s0 >> 0); - s[1] = (unsigned char) (s0 >> 8); - s[2] = (unsigned char) ((s0 >> 16) | (s1 << 5)); - s[3] = (unsigned char) (s1 >> 3); - s[4] = (unsigned char) (s1 >> 11); - s[5] = (unsigned char) ((s1 >> 19) | (s2 << 2)); - s[6] = (unsigned char) (s2 >> 6); - s[7] = (unsigned char) ((s2 >> 14) | (s3 << 7)); - s[8] = (unsigned char) (s3 >> 1); - s[9] = (unsigned char) (s3 >> 9); - s[10] = (unsigned char) ((s3 >> 17) | (s4 << 4)); - s[11] = (unsigned char) (s4 >> 4); - s[12] = (unsigned char) (s4 >> 12); - s[13] = (unsigned char) ((s4 >> 20) | (s5 << 1)); - s[14] = (unsigned char) (s5 >> 7); - s[15] = (unsigned char) ((s5 >> 15) | (s6 << 6)); - s[16] = (unsigned char) (s6 >> 2); - s[17] = (unsigned char) (s6 >> 10); - s[18] = (unsigned char) ((s6 >> 18) | (s7 << 3)); - s[19] = (unsigned char) (s7 >> 5); - s[20] = (unsigned char) (s7 >> 13); - s[21] = (unsigned char) (s8 >> 0); - s[22] = (unsigned char) (s8 >> 8); - s[23] = (unsigned char) ((s8 >> 16) | (s9 << 5)); - s[24] = (unsigned char) (s9 >> 3); - s[25] = (unsigned char) (s9 >> 11); - s[26] = (unsigned char) ((s9 >> 19) | (s10 << 2)); - s[27] = (unsigned char) (s10 >> 6); - s[28] = (unsigned char) ((s10 >> 14) | (s11 << 7)); - s[29] = (unsigned char) (s11 >> 1); - s[30] = (unsigned char) (s11 >> 9); - s[31] = (unsigned char) (s11 >> 17); + s[0] = (unsigned char) ((s0 >> 0) & 0xff); + s[1] = (unsigned char) ((s0 >> 8) & 0xff); + s[2] = (unsigned char) (((s0 >> 16) | (s1 << 5)) & 0xff); + s[3] = (unsigned char) ((s1 >> 3) & 0xff); + s[4] = (unsigned char) ((s1 >> 11) & 0xff); + s[5] = (unsigned char) (((s1 >> 19) | (s2 << 2)) & 0xff); + s[6] = (unsigned char) ((s2 >> 6) & 0xff); + s[7] = (unsigned char) (((s2 >> 14) | (s3 << 7)) & 0xff); + s[8] = (unsigned char) ((s3 >> 1) & 0xff); + s[9] = (unsigned char) ((s3 >> 9) & 0xff); + s[10] = (unsigned char) (((s3 >> 17) | (s4 << 4)) & 0xff); + s[11] = (unsigned char) ((s4 >> 4) & 0xff); + s[12] = (unsigned char) ((s4 >> 12) & 0xff); + s[13] = (unsigned char) (((s4 >> 20) | (s5 << 1)) & 0xff); + s[14] = (unsigned char) ((s5 >> 7) & 0xff); + s[15] = (unsigned char) (((s5 >> 15) | (s6 << 6)) & 0xff); + s[16] = (unsigned char) ((s6 >> 2) & 0xff); + s[17] = (unsigned char) ((s6 >> 10) & 0xff); + s[18] = (unsigned char) (((s6 >> 18) | (s7 << 3)) & 0xff); + s[19] = (unsigned char) ((s7 >> 5) & 0xff); + s[20] = (unsigned char) ((s7 >> 13) & 0xff); + s[21] = (unsigned char) ((s8 >> 0) & 0xff); + s[22] = (unsigned char) ((s8 >> 8) & 0xff); + s[23] = (unsigned char) (((s8 >> 16) | (s9 << 5)) & 0xff); + s[24] = (unsigned char) ((s9 >> 3) & 0xff); + s[25] = (unsigned char) ((s9 >> 11) & 0xff); + s[26] = (unsigned char) (((s9 >> 19) | (s10 << 2)) & 0xff); + s[27] = (unsigned char) ((s10 >> 6) & 0xff); + s[28] = (unsigned char) (((s10 >> 14) | (s11 << 7)) & 0xff); + s[29] = (unsigned char) ((s11 >> 1) & 0xff); + s[30] = (unsigned char) ((s11 >> 9) & 0xff); + s[31] = (unsigned char) ((s11 >> 17) & 0xff); } diff --git a/include/libtorrent/bitfield.hpp b/include/libtorrent/bitfield.hpp index bad782bc7..8464f0953 100644 --- a/include/libtorrent/bitfield.hpp +++ b/include/libtorrent/bitfield.hpp @@ -128,7 +128,7 @@ namespace libtorrent if (m_bytes[i] != 0xff) return false; } int rest = m_size - num_bytes * 8; - boost::uint8_t mask = 0xff << (8-rest); + boost::uint8_t mask = (0xff << (8-rest)) & 0xff; if (rest > 0 && (m_bytes[num_bytes] & mask) != mask) return false; return true; diff --git a/include/libtorrent/kademlia/node_entry.hpp b/include/libtorrent/kademlia/node_entry.hpp index 3eb5cf85f..fd2750fd7 100644 --- a/include/libtorrent/kademlia/node_entry.hpp +++ b/include/libtorrent/kademlia/node_entry.hpp @@ -50,7 +50,7 @@ struct node_entry node_entry(node_id const& id_, udp::endpoint ep, int roundtriptime = 0xffff, bool pinged = false) : id(id_) , endpoint(ep) - , rtt(roundtriptime) + , rtt(roundtriptime & 0xffff) , timeout_count(pinged ? 0 : 0xff) { #ifdef TORRENT_DHT_VERBOSE_LOGGING diff --git a/include/libtorrent/sha1_hash.hpp b/include/libtorrent/sha1_hash.hpp index a4c4da51e..68823c332 100644 --- a/include/libtorrent/sha1_hash.hpp +++ b/include/libtorrent/sha1_hash.hpp @@ -177,7 +177,7 @@ namespace libtorrent for (int i = number_size - 1; i > 0; --i) { m_number[i] >>= n; - m_number[i] |= m_number[i-1] << (8 - n); + m_number[i] |= (m_number[i-1] << (8 - n)) & 0xff; } m_number[0] >>= n; } diff --git a/src/bt_peer_connection.cpp b/src/bt_peer_connection.cpp index 48922ddd0..cb2d4896e 100644 --- a/src/bt_peer_connection.cpp +++ b/src/bt_peer_connection.cpp @@ -366,6 +366,9 @@ namespace libtorrent send_buffer(msg, sizeof(msg)); } + char random_byte() + { return random() & 0xff; } + void bt_peer_connection::get_specific_peer_info(peer_info& p) const { TORRENT_ASSERT(!associated_torrent().expired()); @@ -441,7 +444,7 @@ namespace libtorrent memcpy(ptr, m_dh_key_exchange->get_local_key(), dh_key_len); ptr += dh_key_len; - std::generate(ptr, ptr + pad_size, random); + std::generate(ptr, ptr + pad_size, random_byte); send_buffer(msg, buf_size); #ifdef TORRENT_VERBOSE_LOGGING @@ -571,7 +574,7 @@ namespace libtorrent detail::write_uint16(pad_size, write_buf); // len (pad) // fill pad with zeroes - std::generate(write_buf, write_buf + pad_size, &random); + std::generate(write_buf, write_buf + pad_size, random_byte); write_buf += pad_size; // append len(ia) if we are initiating @@ -788,7 +791,7 @@ namespace libtorrent // in anonymous mode, every peer connection // has a unique peer-id for (int i = 0; i < 20; ++i) - ptr[i] = random(); + ptr[i] = random() & 0xff; } else { diff --git a/src/kademlia/node_id.cpp b/src/kademlia/node_id.cpp index f2ee6746a..e470471f9 100644 --- a/src/kademlia/node_id.cpp +++ b/src/kademlia/node_id.cpp @@ -146,8 +146,8 @@ node_id generate_id_impl(address const& ip_, boost::uint32_t r) id[1] = (c >> 16) & 0xff; id[2] = ((c >> 8) & 0xf8) | (random() & 0x7); - for (int i = 3; i < 19; ++i) id[i] = random(); - id[19] = r; + for (int i = 3; i < 19; ++i) id[i] = random() & 0xff; + id[19] = r & 0xff; return id; } @@ -155,7 +155,7 @@ node_id generate_id_impl(address const& ip_, boost::uint32_t r) node_id generate_random_id() { char r[20]; - for (int i = 0; i < 20; ++i) r[i] = random(); + for (int i = 0; i < 20; ++i) r[i] = random() & 0xff; return hasher(r, 20).final(); } @@ -188,7 +188,7 @@ node_id generate_prefix_mask(int bits) node_id mask(0); int b = 0; for (; b < bits - 7; b += 8) mask[b/8] |= 0xff; - mask[b/8] |= 0xff << (8 - (bits&7)); + mask[b/8] |= (0xff << (8 - (bits&7))) & 0xff; return mask; } diff --git a/src/kademlia/rpc_manager.cpp b/src/kademlia/rpc_manager.cpp index 2f79beb25..1e9a7b8dd 100644 --- a/src/kademlia/rpc_manager.cpp +++ b/src/kademlia/rpc_manager.cpp @@ -475,7 +475,7 @@ bool rpc_manager::invoke(entry& e, udp::endpoint target_addr std::string transaction_id; transaction_id.resize(2); char* out = &transaction_id[0]; - int tid = random() ^ (random() << 5); + int tid = (random() ^ (random() << 5)) & 0xffff; io::write_uint16(tid, out); e["t"] = transaction_id; diff --git a/src/mpi.c b/src/mpi.c index e6d8b386c..e1c59ee71 100644 --- a/src/mpi.c +++ b/src/mpi.c @@ -454,7 +454,7 @@ int fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs) } /* store term */ - W[ix] = ((mp_digit)_W) & MP_MASK; + W[ix] = (mp_digit)(_W & MP_MASK); /* make next carry */ _W = _W >> ((mp_word)DIGIT_BIT); diff --git a/src/pe_crypto.cpp b/src/pe_crypto.cpp index 37922d6a6..83bb39858 100644 --- a/src/pe_crypto.cpp +++ b/src/pe_crypto.cpp @@ -111,7 +111,7 @@ get_out: #elif defined TORRENT_USE_OPENSSL // create local key for (int i = 0; i < sizeof(m_dh_local_secret); ++i) - m_dh_local_secret[i] = random(); + m_dh_local_secret[i] = random() & 0xff; BIGNUM* prime = 0; BIGNUM* secret = 0; @@ -146,7 +146,7 @@ get_out: #elif defined TORRENT_USE_TOMMATH // create local key for (int i = 0; i < int(sizeof(m_dh_local_secret)); ++i) - m_dh_local_secret[i] = random(); + m_dh_local_secret[i] = random() & 0xff; mp_int prime; mp_int secret; diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 813e61ef1..ad7c07d66 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -270,13 +270,14 @@ namespace aux { tu->system_time = min_time() + microsec(stime / 10); #endif } -#endif //TORRENT_STATS +#endif //TORRENT_STATS struct seed_random_generator { seed_random_generator() { - random_seed((unsigned int)total_microseconds(time_now_hires() - min_time())); + random_seed((unsigned int)((total_microseconds( + time_now_hires() - min_time())) & 0xffffffff)); } }; diff --git a/src/utp_socket_manager.cpp b/src/utp_socket_manager.cpp index a760d1b60..29fde3637 100644 --- a/src/utp_socket_manager.cpp +++ b/src/utp_socket_manager.cpp @@ -468,7 +468,7 @@ namespace libtorrent } else { - send_id = random(); + send_id = random() & 0xffff; recv_id = send_id - 1; } utp_socket_impl* impl = construct_utp_impl(recv_id, send_id, str, this); diff --git a/src/utp_stream.cpp b/src/utp_stream.cpp index 446c58b48..3527ec9a4 100644 --- a/src/utp_stream.cpp +++ b/src/utp_stream.cpp @@ -1227,7 +1227,7 @@ void utp_socket_impl::send_syn() { INVARIANT_CHECK; - m_seq_nr = random(); + m_seq_nr = random() & 0xffff; m_acked_seq_nr = (m_seq_nr - 1) & ACK_MASK; m_loss_seq_nr = m_acked_seq_nr; m_ack_nr = 0; @@ -1256,7 +1256,7 @@ void utp_socket_impl::send_syn() ptime now = time_now_hires(); p->send_time = now; - h->timestamp_microseconds = boost::uint32_t(total_microseconds(now - min_time())); + h->timestamp_microseconds = boost::uint32_t(total_microseconds(now - min_time()) & 0xffffffff); #if TORRENT_UTP_LOG UTP_LOGV("%8p: send_syn seq_nr:%d id:%d target:%s\n" @@ -1345,7 +1345,7 @@ void utp_socket_impl::send_reset(utp_header* ph) h.connection_id = m_send_id; h.timestamp_difference_microseconds = m_reply_micro; h.wnd_size = 0; - h.seq_nr = random(); + h.seq_nr = random() & 0xffff; h.ack_nr = ph->seq_nr; ptime now = time_now_hires(); h.timestamp_microseconds = boost::uint32_t(total_microseconds(now - min_time())); @@ -1862,7 +1862,8 @@ bool utp_socket_impl::send_pkt(int flags) // fill in the timestamp as late as possible ptime now = time_now_hires(); p->send_time = now; - h->timestamp_microseconds = boost::uint32_t(total_microseconds(now - min_time())); + h->timestamp_microseconds = boost::uint32_t( + total_microseconds(now - min_time()) & 0xffffffff); #if TORRENT_UTP_LOG UTP_LOG("%8p: sending packet seq_nr:%d ack_nr:%d type:%s " @@ -2053,7 +2054,8 @@ bool utp_socket_impl::resend_packet(packet* p, bool fast_resend) // update packet header h->timestamp_difference_microseconds = m_reply_micro; p->send_time = time_now_hires(); - h->timestamp_microseconds = boost::uint32_t(total_microseconds(p->send_time - min_time())); + h->timestamp_microseconds = boost::uint32_t( + total_microseconds(p->send_time - min_time()) & 0xffffffff); // if the packet has a selective ack header, we'll need // to update it @@ -2545,8 +2547,8 @@ bool utp_socket_impl::incoming_packet(boost::uint8_t const* buf, int size boost::uint32_t their_delay = 0; if (ph->timestamp_microseconds != 0) { - m_reply_micro = boost::uint32_t(total_microseconds(receive_time - min_time())) - - ph->timestamp_microseconds; + boost::uint32_t timestamp = boost::uint32_t(total_microseconds(receive_time - min_time()) & 0xffffffff); + m_reply_micro = timestamp - ph->timestamp_microseconds; boost::uint32_t prev_base = m_their_delay_hist.initialized() ? m_their_delay_hist.base() : 0; their_delay = m_their_delay_hist.add_sample(m_reply_micro, step); int base_change = m_their_delay_hist.base() - prev_base; @@ -2867,7 +2869,7 @@ bool utp_socket_impl::incoming_packet(boost::uint8_t const* buf, int size m_local_address = m_sm->local_endpoint(m_remote_address, ec).address(); m_ack_nr = ph->seq_nr; - m_seq_nr = random(); + m_seq_nr = random() & 0xffff; m_acked_seq_nr = (m_seq_nr - 1) & ACK_MASK; m_loss_seq_nr = m_acked_seq_nr; m_fast_resend_seq_nr = m_seq_nr; diff --git a/test/main.cpp b/test/main.cpp index 7ec1d7a7d..48d96ded3 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -91,7 +91,7 @@ int main() | SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX); #endif - srand(total_microseconds(time_now_hires() - min_time())); + srand((total_microseconds(time_now_hires() - min_time())) & 0x7fffffff); #ifdef O_NONBLOCK // on darwin, stdout is set to non-blocking mode by default // which sometimes causes tests to fail with EAGAIN just diff --git a/test/setup_transfer.cpp b/test/setup_transfer.cpp index f9fe6f4cc..6fe3e0fe6 100644 --- a/test/setup_transfer.cpp +++ b/test/setup_transfer.cpp @@ -328,6 +328,7 @@ static std::map running_proxies; void stop_proxy(int port) { + fprintf(stderr, "stopping proxy on port %d\n", port); // don't shut down proxies until the test is // completely done. This saves a lot of time. // they're closed at the end of main() by @@ -419,7 +420,7 @@ int start_proxy(int proxy_type) if (i->second.type == proxy_type) return i->first; } - unsigned int seed = total_microseconds(time_now_hires() - min_time()); + unsigned int seed = total_microseconds(time_now_hires() - min_time()) & 0xffffffff; printf("random seed: %u\n", seed); std::srand(seed); int port = 5000 + (rand() % 55000); @@ -477,13 +478,16 @@ boost::intrusive_ptr clone_ptr(boost::intrusive_ptr const& ptr) return boost::intrusive_ptr(new T(*ptr)); } +unsigned char random_byte() +{ return std::rand() & 0xff; } + void create_random_files(std::string const& path, const int file_sizes[], int num_files) { error_code ec; char* random_data = (char*)malloc(300000); for (int i = 0; i != num_files; ++i) { - std::generate(random_data, random_data + 300000, &std::rand); + std::generate(random_data, random_data + 300000, random_byte); char filename[200]; snprintf(filename, sizeof(filename), "test%d", i); std::string full_path = combine_path(path, filename); @@ -617,14 +621,14 @@ setup_transfer(session* ses1, session* ses2, session* ses3 if (ses3) ses3->set_alert_mask(~(alert::progress_notification | alert::stats_notification)); peer_id pid; - std::generate(&pid[0], &pid[0] + 20, std::rand); + std::generate(&pid[0], &pid[0] + 20, random_byte); ses1->set_peer_id(pid); - std::generate(&pid[0], &pid[0] + 20, std::rand); + std::generate(&pid[0], &pid[0] + 20, random_byte); ses2->set_peer_id(pid); assert(ses1->id() != ses2->id()); if (ses3) { - std::generate(&pid[0], &pid[0] + 20, std::rand); + std::generate(&pid[0], &pid[0] + 20, random_byte); ses3->set_peer_id(pid); assert(ses3->id() != ses2->id()); } @@ -931,7 +935,7 @@ pid_type web_server_pid = 0; int start_web_server(bool ssl, bool chunked_encoding) { - unsigned int seed = total_microseconds(time_now_hires() - min_time()); + unsigned int seed = total_microseconds(time_now_hires() - min_time()) & 0xffffffff; fprintf(stderr, "random seed: %u\n", seed); std::srand(seed); int port = 5000 + (rand() % 55000); @@ -954,6 +958,7 @@ int start_web_server(bool ssl, bool chunked_encoding) void stop_web_server() { if (web_server_pid == 0) return; + fprintf(stderr, "stopping web server\n"); stop_process(web_server_pid); web_server_pid = 0; } diff --git a/test/setup_transfer.hpp b/test/setup_transfer.hpp index 720cc5784..b81e71128 100644 --- a/test/setup_transfer.hpp +++ b/test/setup_transfer.hpp @@ -45,6 +45,7 @@ namespace libtorrent } int EXPORT print_failures(); +unsigned char EXPORT random_byte(); int EXPORT load_file(std::string const& filename, std::vector& v, libtorrent::error_code& ec, int limit = 8000000); void EXPORT save_file(char const* filename, char const* data, int size); diff --git a/test/socks.py b/test/socks.py index 88b546163..4f54a7e11 100644 --- a/test/socks.py +++ b/test/socks.py @@ -207,7 +207,10 @@ class SocksHandler(StreamRequestHandler): self.send_reply(outbound_sock.getsockname()) spawn_forwarder(outbound_sock, self.request, 'destination') - forward(self.request, outbound_sock, 'client') + try: + forward(self.request, outbound_sock, 'client') + except Exception,e: + print e def send_reply_v4(self, (bind_addr, bind_port)): self.wfile.write('\0\x5a\0\0\0\0\0\0') @@ -267,6 +270,7 @@ if __name__ == '__main__': debug('Listening on port %d...' % listen_port) server = MyTCPServer(('localhost', listen_port), SocksHandler) - server.timeout = 120 + server.timeout = 190 while True: server.handle_request() + diff --git a/test/test_dht.cpp b/test/test_dht.cpp index 70d8af136..7b59b7ca0 100644 --- a/test/test_dht.cpp +++ b/test/test_dht.cpp @@ -112,14 +112,14 @@ udp::endpoint rand_ep() sha1_hash generate_next() { sha1_hash ret; - for (int i = 0; i < 20; ++i) ret[i] = rand(); + for (int i = 0; i < 20; ++i) ret[i] = rand() & 0xff; return ret; } boost::array generate_key() { boost::array ret; - for (int i = 0; i < 64; ++i) ret[i] = rand(); + for (int i = 0; i < 64; ++i) ret[i] = rand() & 0xff; return ret; } @@ -1287,7 +1287,7 @@ int test_main() std::vector temp; - std::generate(tmp.begin(), tmp.end(), &std::rand); + std::generate(tmp.begin(), tmp.end(), random_byte); table.find_node(tmp, temp, 0, nodes.size() * 2); printf("returned-all: %d\n", int(temp.size())); TEST_EQUAL(temp.size(), nodes.size()); @@ -1304,7 +1304,7 @@ int test_main() for (int r = 0; r < reps; ++r) { - std::generate(tmp.begin(), tmp.end(), &std::rand); + std::generate(tmp.begin(), tmp.end(), random_byte); table.find_node(tmp, temp, 0, bucket_size * 2); printf("returned: %d\n", int(temp.size())); TEST_EQUAL(int(temp.size()), (std::min)(bucket_size * 2, int(nodes.size()))); diff --git a/test/test_primitives.cpp b/test/test_primitives.cpp index 6f0f94b1c..fa1d8738e 100644 --- a/test/test_primitives.cpp +++ b/test/test_primitives.cpp @@ -73,7 +73,7 @@ address rand_v4() address rand_v6() { address_v6::bytes_type bytes; - for (int i = 0; i < bytes.size(); ++i) bytes[i] = rand(); + for (int i = 0; i < bytes.size(); ++i) bytes[i] = rand() & 0xff; return address_v6(bytes); } #endif diff --git a/test/test_storage.cpp b/test/test_storage.cpp index 86b400666..80aab5dd6 100644 --- a/test/test_storage.cpp +++ b/test/test_storage.cpp @@ -771,8 +771,8 @@ void test_check_files(std::string const& test_path char piece0[piece_size]; char piece2[piece_size]; - std::generate(piece0, piece0 + piece_size, std::rand); - std::generate(piece2, piece2 + piece_size, std::rand); + std::generate(piece0, piece0 + piece_size, random_byte); + std::generate(piece2, piece2 + piece_size, random_byte); libtorrent::create_torrent t(fs, piece_size, -1, 0); t.set_hash(0, hasher(piece0, piece_size).final()); @@ -1144,13 +1144,13 @@ int test_main() // initialize test pieces for (char* p = piece0, *end(piece0 + piece_size); p < end; ++p) - *p = rand(); + *p = random_byte(); for (char* p = piece1, *end(piece1 + piece_size); p < end; ++p) - *p = rand(); + *p = random_byte(); for (char* p = piece2, *end(piece2 + piece_size); p < end; ++p) - *p = rand(); + *p = random_byte(); for (char* p = piece3, *end(piece3 + piece_size); p < end; ++p) - *p = rand(); + *p = random_byte(); std::vector test_paths; char* env = std::getenv("TORRENT_TEST_PATHS"); diff --git a/test/web_seed_suite.cpp b/test/web_seed_suite.cpp index 65965f0e7..559a94189 100644 --- a/test/web_seed_suite.cpp +++ b/test/web_seed_suite.cpp @@ -252,7 +252,7 @@ int EXPORT run_http_suite(int proxy, char const* protocol, bool test_url_seed, b { piece_size = 64 * 1024; char* random_data = (char*)malloc(64 * 1024 * num_pieces); - std::generate(random_data, random_data + 64 * 1024 * num_pieces, &std::rand); + std::generate(random_data, random_data + 64 * 1024 * num_pieces, random_byte); std::string seed_filename = combine_path(save_path, "seed"); save_file(seed_filename.c_str(), random_data, 64 * 1024 * num_pieces); fs.add_file("seed", 64 * 1024 * num_pieces); @@ -307,7 +307,7 @@ int EXPORT run_http_suite(int proxy, char const* protocol, bool test_url_seed, b { piece_size = 64 * 1024; char* random_data = (char*)malloc(64 * 1024 * num_pieces); - std::generate(random_data, random_data + 64 * 1024 * num_pieces, &std::rand); + std::generate(random_data, random_data + 64 * 1024 * num_pieces, random_byte); save_file(combine_path(save_path, "seed").c_str(), random_data, 64 * 1024 * num_pieces); free(random_data); } diff --git a/test/web_server.py b/test/web_server.py index 26316e88d..17123f097 100644 --- a/test/web_server.py +++ b/test/web_server.py @@ -18,7 +18,7 @@ except: class http_server_with_timeout(BaseHTTPServer.HTTPServer): allow_reuse_address = True - timeout = 120 + timeout = 190 def handle_timeout(self): raise Exception('timeout')