2015-05-23 03:38:47 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2015, Arvid Norberg
|
|
|
|
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 "test.hpp"
|
|
|
|
#include "libtorrent/bloom_filter.hpp"
|
|
|
|
#include "libtorrent/hasher.hpp"
|
|
|
|
#include "libtorrent/sha1_hash.hpp"
|
2016-06-18 20:01:38 +02:00
|
|
|
#include <cstdint>
|
2015-05-23 03:38:47 +02:00
|
|
|
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt;
|
2015-05-23 03:38:47 +02:00
|
|
|
|
2018-01-25 15:34:39 +01:00
|
|
|
namespace {
|
|
|
|
|
2015-05-23 03:38:47 +02:00
|
|
|
void test_set_and_get()
|
|
|
|
{
|
|
|
|
bloom_filter<32> filter;
|
|
|
|
sha1_hash k1 = hasher("test1", 5).final();
|
|
|
|
sha1_hash k2 = hasher("test2", 5).final();
|
|
|
|
sha1_hash k3 = hasher("test3", 5).final();
|
|
|
|
sha1_hash k4 = hasher("test4", 5).final();
|
|
|
|
TEST_CHECK(!filter.find(k1));
|
|
|
|
TEST_CHECK(!filter.find(k2));
|
|
|
|
TEST_CHECK(!filter.find(k3));
|
|
|
|
TEST_CHECK(!filter.find(k4));
|
|
|
|
|
|
|
|
filter.set(k1);
|
|
|
|
TEST_CHECK(filter.find(k1));
|
|
|
|
TEST_CHECK(!filter.find(k2));
|
|
|
|
TEST_CHECK(!filter.find(k3));
|
|
|
|
TEST_CHECK(!filter.find(k4));
|
|
|
|
|
|
|
|
filter.set(k4);
|
|
|
|
TEST_CHECK(filter.find(k1));
|
|
|
|
TEST_CHECK(!filter.find(k2));
|
|
|
|
TEST_CHECK(!filter.find(k3));
|
|
|
|
TEST_CHECK(filter.find(k4));
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_set_bits()
|
|
|
|
{
|
2016-06-18 20:01:38 +02:00
|
|
|
std::uint8_t bits[4] = {0x00, 0x00, 0x00, 0x00};
|
2015-05-23 03:38:47 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < 4 * 8; ++i)
|
|
|
|
{
|
2016-06-18 20:01:38 +02:00
|
|
|
std::uint8_t t[4] = { std::uint8_t(i & 0xff), 0, std::uint8_t(i & 0xff), 0 };
|
2015-05-23 03:38:47 +02:00
|
|
|
TEST_CHECK(!has_bits(t, bits, 6));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < 4 * 8; i += 2)
|
|
|
|
{
|
2016-06-18 20:01:38 +02:00
|
|
|
std::uint8_t t[4] = { std::uint8_t(i & 0xff), 0, std::uint8_t(i & 0xff), 0 };
|
2015-05-23 03:38:47 +02:00
|
|
|
TEST_CHECK(!has_bits(t, bits, 4));
|
|
|
|
set_bits(t, bits, 4);
|
|
|
|
TEST_CHECK(has_bits(t, bits, 4));
|
|
|
|
}
|
|
|
|
|
2016-06-18 20:01:38 +02:00
|
|
|
std::uint8_t compare[4] = { 0x55, 0x55, 0x55, 0x55};
|
2015-05-23 03:38:47 +02:00
|
|
|
TEST_EQUAL(memcmp(compare, bits, 4), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_count_zeroes()
|
|
|
|
{
|
2016-06-18 20:01:38 +02:00
|
|
|
std::uint8_t bits[4] = {0x00, 0xff, 0x55, 0xaa};
|
2015-05-23 03:38:47 +02:00
|
|
|
|
|
|
|
TEST_EQUAL(count_zero_bits(bits, 4), 16);
|
|
|
|
|
2016-06-18 20:01:38 +02:00
|
|
|
std::uint8_t t[4] = { 4, 0, 4, 0 };
|
2015-05-23 03:38:47 +02:00
|
|
|
set_bits(t, bits, 4);
|
|
|
|
TEST_EQUAL(count_zero_bits(bits, 4), 15);
|
|
|
|
|
2016-06-18 20:01:38 +02:00
|
|
|
std::uint8_t compare[4] = { 0x10, 0xff, 0x55, 0xaa};
|
2015-05-23 03:38:47 +02:00
|
|
|
TEST_EQUAL(memcmp(compare, bits, 4), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_to_from_string()
|
|
|
|
{
|
2016-06-18 20:01:38 +02:00
|
|
|
std::uint8_t bits[4] = { 0x10, 0xff, 0x55, 0xaa};
|
2015-05-23 03:38:47 +02:00
|
|
|
|
|
|
|
bloom_filter<4> filter;
|
|
|
|
filter.from_string(reinterpret_cast<char*>(bits));
|
|
|
|
|
|
|
|
std::string bits_out = filter.to_string();
|
|
|
|
TEST_EQUAL(memcmp(bits_out.c_str(), bits, 4), 0);
|
|
|
|
|
2016-11-12 19:50:49 +01:00
|
|
|
sha1_hash k("\x01\x00\x02\x00 ");
|
2015-05-23 03:38:47 +02:00
|
|
|
TEST_CHECK(!filter.find(k));
|
|
|
|
filter.set(k);
|
|
|
|
TEST_CHECK(filter.find(k));
|
|
|
|
|
2016-06-18 20:01:38 +02:00
|
|
|
std::uint8_t compare[4] = { 0x16, 0xff, 0x55, 0xaa};
|
2015-05-23 03:38:47 +02:00
|
|
|
|
|
|
|
bits_out = filter.to_string();
|
|
|
|
TEST_EQUAL(memcmp(compare, bits_out.c_str(), 4), 0);
|
|
|
|
}
|
|
|
|
|
2018-01-25 15:34:39 +01:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2015-05-30 06:31:23 +02:00
|
|
|
TORRENT_TEST(bloom_filter)
|
2015-05-23 03:38:47 +02:00
|
|
|
{
|
|
|
|
test_set_and_get();
|
|
|
|
test_set_bits();
|
|
|
|
test_count_zeroes();
|
|
|
|
test_to_from_string();
|
|
|
|
|
|
|
|
// TODO: test size()
|
|
|
|
// TODO: test clear()
|
|
|
|
}
|