fix bug in settings_pack causing intermittent unit test failures. added unit test to cover the bug

This commit is contained in:
Arvid Norberg 2015-01-03 16:52:22 +00:00
parent 290260054d
commit b897a6ce29
4 changed files with 39 additions and 7 deletions

View File

@ -41,11 +41,19 @@ POSSIBILITY OF SUCH DAMAGE.
namespace {
template <class T>
bool compare_first(std::pair<boost::uint16_t, T> const& lhs
, std::pair<boost::uint16_t, T> const& rhs)
{
return lhs.first < rhs.first;
}
template <class T>
void insort_replace(std::vector<std::pair<boost::uint16_t, T> >& c, std::pair<boost::uint16_t, T> const& v)
{
typedef std::vector<std::pair<boost::uint16_t, T> > container_t;
typename container_t::iterator i = std::lower_bound(c.begin(), c.end(), v);
typename container_t::iterator i = std::lower_bound(c.begin(), c.end(), v
, &compare_first<T>);
if (i != c.end() && i->first == v.first) i->second = v.second;
else c.insert(i, v);
}
@ -669,21 +677,24 @@ namespace libtorrent
{
std::pair<boost::uint16_t, std::string> v(name, std::string());
std::vector<std::pair<boost::uint16_t, std::string> >::const_iterator i =
std::lower_bound(m_strings.begin(), m_strings.end(), v);
std::lower_bound(m_strings.begin(), m_strings.end(), v
, &compare_first<std::string>);
return i != m_strings.end() && i->first == name;
}
case int_type_base:
{
std::pair<boost::uint16_t, int> v(name, 0);
std::vector<std::pair<boost::uint16_t, int> >::const_iterator i =
std::lower_bound(m_ints.begin(), m_ints.end(), v);
std::lower_bound(m_ints.begin(), m_ints.end(), v
, &compare_first<int>);
return i != m_ints.end() && i->first == name;
}
case bool_type_base:
{
std::pair<boost::uint16_t, bool> v(name, false);
std::vector<std::pair<boost::uint16_t, bool> >::const_iterator i =
std::lower_bound(m_bools.begin(), m_bools.end(), v);
std::lower_bound(m_bools.begin(), m_bools.end(), v
, &compare_first<bool>);
return i != m_bools.end() && i->first == name;
}
}
@ -698,7 +709,8 @@ namespace libtorrent
std::pair<boost::uint16_t, std::string> v(name, std::string());
std::vector<std::pair<boost::uint16_t, std::string> >::const_iterator i
= std::lower_bound(m_strings.begin(), m_strings.end(), v);
= std::lower_bound(m_strings.begin(), m_strings.end(), v
, &compare_first<std::string>);
if (i != m_strings.end() && i->first == name) return i->second;
return std::string();
}
@ -710,7 +722,8 @@ namespace libtorrent
std::pair<boost::uint16_t, int> v(name, 0);
std::vector<std::pair<boost::uint16_t, int> >::const_iterator i
= std::lower_bound(m_ints.begin(), m_ints.end(), v);
= std::lower_bound(m_ints.begin(), m_ints.end(), v
, &compare_first<int>);
if (i != m_ints.end() && i->first == name) return i->second;
return 0;
}
@ -722,7 +735,8 @@ namespace libtorrent
std::pair<boost::uint16_t, bool> v(name, false);
std::vector<std::pair<boost::uint16_t, bool> >::const_iterator i
= std::lower_bound(m_bools.begin(), m_bools.end(), v);
= std::lower_bound(m_bools.begin(), m_bools.end(), v
, &compare_first<bool>);
if (i != m_bools.end() && i->first == name) return i->second;
return false;
}

View File

@ -704,15 +704,21 @@ setup_transfer(lt::session* ses1, lt::session* ses2, lt::session* ses3
std::generate(&pid[0], &pid[0] + 20, random_byte);
pack.set_str(settings_pack::peer_fingerprint, pid.to_string());
ses1->apply_settings(pack);
TORRENT_ASSERT(ses1->id() == pid);
std::generate(&pid[0], &pid[0] + 20, random_byte);
TORRENT_ASSERT(ses1->id() != pid);
pack.set_str(settings_pack::peer_fingerprint, pid.to_string());
ses2->apply_settings(pack);
TORRENT_ASSERT(ses2->id() == pid);
if (ses3)
{
std::generate(&pid[0], &pid[0] + 20, random_byte);
TORRENT_ASSERT(ses1->id() != pid);
TORRENT_ASSERT(ses2->id() != pid);
pack.set_str(settings_pack::peer_fingerprint, pid.to_string());
ses3->apply_settings(pack);
TORRENT_ASSERT(ses3->id() == pid);
}
TORRENT_ASSERT(ses1->id() != ses2->id());

View File

@ -79,6 +79,14 @@ int test_main()
TEST_NAME(peer_turnover_interval);
TEST_NAME(mmap_cache);
settings_pack p;
p.set_str(settings_pack::peer_fingerprint, "abc");
p.set_str(settings_pack::peer_fingerprint, "cde");
p.set_str(settings_pack::peer_fingerprint, "efg");
p.set_str(settings_pack::peer_fingerprint, "hij");
TEST_EQUAL(p.get_str(settings_pack::peer_fingerprint), "hij");
return 0;
}

View File

@ -395,6 +395,10 @@ int EXPORT run_http_suite(int proxy, char const* protocol, bool test_url_seed
pack.set_str(settings_pack::listen_interfaces, "0.0.0.0:51000");
pack.set_int(settings_pack::max_retry_port_bind, 1000);
pack.set_int(settings_pack::alert_mask, ~(alert::progress_notification | alert::stats_notification));
pack.set_bool(settings_pack::enable_lsd, false);
pack.set_bool(settings_pack::enable_natpmp, false);
pack.set_bool(settings_pack::enable_upnp, false);
pack.set_bool(settings_pack::enable_dht, false);
libtorrent::session ses(pack, 0);
test_transfer(ses, torrent_file, proxy, port, protocol, test_url_seed