-
Notifications
You must be signed in to change notification settings - Fork 127
/
cleansing_allocator_test.cc
146 lines (126 loc) · 4.88 KB
/
cleansing_allocator_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
*
* Copyright 2017 Asylo authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include "asylo/util/cleansing_allocator.h"
#include <cstdlib>
#include <iostream>
#include <list>
#include <vector>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "asylo/crypto/util/bytes.h"
#include "asylo/crypto/util/trivial_object_util.h"
namespace asylo {
namespace {
const size_t kNumIterations = 1000;
// TestAllocator is a derived type of std::allocator, and overrides its
// deallocate method. The overridden deallocate method verifies that the memory
// being deallocated has been cleansed (i.e., zeroed out) before calling the
// deallocate method of the base class.
//
// The tests in this file use the TestAllocator as the template parameter |A|
// for the CleansingAllocator. Such a setup verifies that the
// CleansingAllocator is indeed cleaning up the memory before calling
// the deallocate method of |A|.
template <typename T>
class TestAllocator : public std::allocator<T> {
public:
TestAllocator() = default;
template <typename U>
explicit TestAllocator(const TestAllocator<U> &a)
: std::allocator<T>::allocator(a) {}
~TestAllocator() = default;
template <typename U>
struct rebind {
using other = TestAllocator<U>;
};
void deallocate(typename std::allocator<T>::pointer ptr,
typename std::allocator<T>::size_type n) {
uint8_t *buffer = reinterpret_cast<uint8_t *>(ptr);
for (int i = 0; i < n * sizeof(T); i++) {
ASSERT_EQ(buffer[i], 0);
}
std::allocator_traits<std::allocator<T>>::deallocate(*this, ptr, n);
}
};
template <typename T, typename U>
bool operator==(const TestAllocator<T> &lhs, const TestAllocator<U> &rhs) {
return true;
}
template <typename T, typename U>
bool operator!=(const TestAllocator<T> &lhs, const TestAllocator<U> &rhs) {
return !(lhs == rhs);
}
// A typed test fixture is used for tests that require a single type object.
template <typename T>
class TypedCleansingAllocatorTest : public ::testing::Test {};
// Test the allocator with objects of varying types/sizes. The sizes of the
// objects are somewhat arbitrarily chosen.
typedef ::testing::Types<SafeBytes<8>, UnsafeBytes<15>, SafeBytes<235>,
UnsafeBytes<519>, uint8_t, uint16_t, uint32_t,
uint64_t>
MyTypes;
TYPED_TEST_SUITE(TypedCleansingAllocatorTest, MyTypes);
TYPED_TEST(TypedCleansingAllocatorTest, VectorTest) {
using TestVector =
std::vector<TypeParam,
CleansingAllocator<TypeParam, TestAllocator<TypeParam>>>;
std::unique_ptr<TestVector> v(new TestVector);
for (int i = 0; i < kNumIterations; i++) {
v->push_back(TrivialRandomObject<TypeParam>());
}
// Resetting v will free up all the allocated memory, and make sure that
// the cleansing was performed correctly.
v.reset(nullptr);
}
// std::list utilizes the rebind functionality of the allocator, and
// consequently, in addition to testing the allocator with std::vector,
// it is also tested with std::list.
TYPED_TEST(TypedCleansingAllocatorTest, ListTest) {
using TestList =
std::list<TypeParam,
CleansingAllocator<TypeParam, TestAllocator<TypeParam>>>;
std::unique_ptr<TestList> l(new TestList);
for (int i = 0; i < kNumIterations; i++) {
l->push_back(TrivialRandomObject<TypeParam>());
}
// Resetting l will free up all the allocated memory, and make sure that
// the cleansing was performed correctly.
l.reset(nullptr);
}
// std::basic_string is another commonly-used type for storing secrets,
// and consequently, the following test explicitly tests the functioning
// of this type with the cleansing allocator.
TYPED_TEST(TypedCleansingAllocatorTest, StringTest) {
using CleansingString =
std::basic_string<uint8_t, std::char_traits<uint8_t>,
CleansingAllocator<uint8_t, TestAllocator<uint8_t>>>;
using TestList = std::list<
CleansingString,
CleansingAllocator<CleansingString, TestAllocator<CleansingString>>>;
std::unique_ptr<TestList> l(new TestList);
for (int i = 0; i < kNumIterations; i++) {
TypeParam obj = TrivialRandomObject<TypeParam>();
l->push_back(
CleansingString(reinterpret_cast<uint8_t *>(&obj), sizeof(obj)));
}
// Resetting l will free up all the allocated memory, and make sure that
// the cleansing was performed correctly.
l.reset(nullptr);
}
} // namespace
} // namespace asylo