-#include <boost/test/unit_test.hpp>
+#include <vector>
+#include <boost/test/unit_test.hpp>
#include <test_common.h>
#include <ckm/ckm-raw-buffer.h>
using namespace CKM;
namespace {
- const RawBuffer::size_type LEN = 10;
- struct Item
- {
- Item(size_t a) : mA(a) {}
- ~Item() {}
+const size_t LEN = 100;
- size_t mA;
- };
-} // namespace anonymous
+struct Item
+{
+ Item(size_t a) : mA(a) {}
+ ~Item() {}
-BOOST_AUTO_TEST_SUITE(SAFE_BUFFER_TEST)
+ bool operator==(const size_t& other) const {
+ return mA == other;
+ }
-/*
- * Test for SafeBuffer. Checks if memory occupied by the buffer is wiped after
- * it's deleted
- */
-BOOST_AUTO_TEST_CASE(SafeBufferTest_uc) {
- const unsigned char* data = NULL;
- RawBuffer::size_type i = 0;
+ size_t mA;
+};
+
+template <typename T>
+size_t buffer_erase_test()
+{
+ typename T::value_type* data = NULL;
+ typename T::size_type i = 0;
{
- RawBuffer buffer;
+ T buffer;
for (i=0;i<LEN;++i)
- buffer.push_back(i);
+ buffer.push_back(typename T::value_type(i));
data = buffer.data();
for (i=0;i<LEN;++i)
BOOST_CHECK(data[i] == i);
}
+
+ /*
+ * operator delete of RawBuffer which is called after buffer memory is erased
+ * (see erase_on_dealloc::deallocate) sometimes leaves garbage in the beginning of that memory.
+ * Therefore the test will be marked as failing only if more than 1/10 of the data matches
+ * original
+ */
+ size_t cnt = 0;
for (i=0;i<LEN;++i)
- BOOST_CHECK(data[i] == 0);
+ cnt += (data[i] == i?1:0);
+
+ return cnt;
}
-BOOST_AUTO_TEST_CASE(SafeBufferTest_item) {
- const unsigned char* data = NULL;
- RawBuffer::size_type i = 0;
- {
- SafeBuffer<Item>::Type buffer;
- for (i=0;i<LEN;++i)
- buffer.push_back(i);
+} // namespace anonymous
- for (i=0;i<LEN;++i) {
- BOOST_CHECK(buffer[i].mA == i);
- }
+BOOST_AUTO_TEST_SUITE(SAFE_BUFFER_TEST)
- data = reinterpret_cast<unsigned char*>(buffer.data());
- }
- for (i=0;i<LEN*sizeof(Item);++i)
- BOOST_CHECK(data[i] == 0);
+// Tests for SafeBuffer. Checks if memory occupied by the buffer is wiped after it's deleted.
+
+BOOST_AUTO_TEST_CASE(SafeBufferTest_uc_control_group) {
+ size_t cnt = buffer_erase_test<std::vector<unsigned char> >();
+
+ BOOST_REQUIRE_MESSAGE(cnt > LEN/2, "Less than 1/2 of data matches the original.");
+}
+
+BOOST_AUTO_TEST_CASE(SafeBufferTest_item_control_group) {
+ size_t cnt = buffer_erase_test<std::vector<Item> >();
+
+ BOOST_REQUIRE_MESSAGE(cnt > LEN/2, "Less than 1/2 of data matches the original.");
+}
+
+BOOST_AUTO_TEST_CASE(SafeBufferTest_uc) {
+ size_t cnt = buffer_erase_test<RawBuffer>();
+
+ BOOST_REQUIRE_MESSAGE(cnt <= LEN/10, "More than 1/10 of data matches the original.");
+}
+
+BOOST_AUTO_TEST_CASE(SafeBufferTest_item) {
+ size_t cnt = buffer_erase_test<SafeBuffer<Item>::Type>();
+
+ BOOST_REQUIRE_MESSAGE(cnt <= LEN/10, "More than 1/10 of data matches the original.");
}
BOOST_AUTO_TEST_SUITE_END()