Workaround for memory garbage in SafeBuffer test 62/28862/1
authorKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Tue, 7 Oct 2014 11:05:20 +0000 (13:05 +0200)
committerBartlomiej Grzelewski <b.grzelewski@samsung.com>
Thu, 16 Oct 2014 13:44:31 +0000 (15:44 +0200)
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. Also tests for std::vector control group
were added.

Change-Id: Ic1a0db5aa336eaca0825c945af5ccbc6fe88c13a

tests/test_safe-buffer.cpp

index 8ccbe42..691f02c 100644 (file)
@@ -1,5 +1,6 @@
-#include <boost/test/unit_test.hpp>
+#include <vector>
 
+#include <boost/test/unit_test.hpp>
 #include <test_common.h>
 
 #include <ckm/ckm-raw-buffer.h>
@@ -9,56 +10,78 @@ BOOST_GLOBAL_FIXTURE(TestConfig)
 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()