File: securearray.cc

package info (click to toggle)
yapet 2.6-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,920 kB
  • sloc: cpp: 32,397; sh: 5,032; makefile: 880; ansic: 36; sed: 16
file content (335 lines) | stat: -rw-r--r-- 10,735 bytes parent folder | download | duplicates (4)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#include <cppunit/CompilerOutputter.h>
#include <cppunit/TestCaller.h>
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>

#include "securearray.hh"

class SecureArrayTest : public CppUnit::TestFixture {
   public:
    static CppUnit::TestSuite *suite() {
        CppUnit::TestSuite *suiteOfTests =
            new CppUnit::TestSuite("SecureArrayTest");

        suiteOfTests->addTest(new CppUnit::TestCaller<SecureArrayTest>(
            "should not allow initialization with negative size",
            &SecureArrayTest::testNegativeSize));

        suiteOfTests->addTest(new CppUnit::TestCaller<SecureArrayTest>(
            "test empty array", &SecureArrayTest::testEmptyArray));
        suiteOfTests->addTest(new CppUnit::TestCaller<SecureArrayTest>(
            "should set empty array upon destruction",
            &SecureArrayTest::testZeroOutBuffer));
        suiteOfTests->addTest(new CppUnit::TestCaller<SecureArrayTest>(
            "copy constructor", &SecureArrayTest::testCopy));
        suiteOfTests->addTest(new CppUnit::TestCaller<SecureArrayTest>(
            "assignment", &SecureArrayTest::testAssignment));
        suiteOfTests->addTest(new CppUnit::TestCaller<SecureArrayTest>(
            "move", &SecureArrayTest::testMove));
        suiteOfTests->addTest(new CppUnit::TestCaller<SecureArrayTest>(
            "move assignment", &SecureArrayTest::testMove));
        suiteOfTests->addTest(new CppUnit::TestCaller<SecureArrayTest>(
            "equality", &SecureArrayTest::testEquality));
        suiteOfTests->addTest(new CppUnit::TestCaller<SecureArrayTest>(
            "should properly implement index operator",
            &SecureArrayTest::testIndexOperator));
        suiteOfTests->addTest(new CppUnit::TestCaller<SecureArrayTest>(
            "should properly handle assignment of empty array",
            &SecureArrayTest::testEmptyAssignment));
        suiteOfTests->addTest(new CppUnit::TestCaller<SecureArrayTest>(
            "should properly handle copy of empty array",
            &SecureArrayTest::testEmptyCopy));
        suiteOfTests->addTest(new CppUnit::TestCaller<SecureArrayTest>(
            "should properly handle move of empty array",
            &SecureArrayTest::testEmptyMove));
        suiteOfTests->addTest(new CppUnit::TestCaller<SecureArrayTest>(
            "should properly add two secure arrays",
            &SecureArrayTest::testAdd));
        suiteOfTests->addTest(new CppUnit::TestCaller<SecureArrayTest>(
            "test toSecureArray", &SecureArrayTest::testToSecureArray));
        suiteOfTests->addTest(new CppUnit::TestCaller<SecureArrayTest>(
            "test toSecureArray with empty string",
            &SecureArrayTest::testToSecureArrayEmpty));
        suiteOfTests->addTest(new CppUnit::TestCaller<SecureArrayTest>(
            "test SecureArray copy operator",
            &SecureArrayTest::testCopyOperator));

        return suiteOfTests;
    }

    void testNegativeSize() {
        CPPUNIT_ASSERT_THROW(yapet::SecureArray{-1}, std::invalid_argument);
    }

    void testEmptyArray() {
        yapet::SecureArray *ptr{new yapet::SecureArray()};

        CPPUNIT_ASSERT(ptr->size() == 0);
        CPPUNIT_ASSERT(ptr->operator*() == nullptr);

        delete ptr;

        ptr = new yapet::SecureArray(0);

        CPPUNIT_ASSERT(ptr->size() == 0);
        CPPUNIT_ASSERT(ptr->operator*() == nullptr);

        delete ptr;
    }

#if defined(__SANITIZE_ADDRESS__)
#define USES_ADDRESS_SANITIZER 1
#elif defined(__has_feature)
#if __has_feature(address_sanitizer)
#define USES_ADDRESS_SANITIZER 1
#endif
#endif

#ifdef USES_ADDRESS_SANITIZER
    // We cannot run the test when the address sanitizer is used, since we're
    // accessing memory after free.
    void testZeroOutBuffer() {}
#else
    void testZeroOutBuffer() {
        auto subject = new yapet::SecureArray{3};

        for (auto i = 0; i < 3; i++) {
            subject->operator*()[i] = 42;
        }

        std::uint8_t *array = **subject;
        delete subject;
        for (auto i = 0; i < 3; i++) {
            // it is not guaranteed that upon free the memory retains it
            // content, i.e. zeros.
            CPPUNIT_ASSERT(array[i] != 42);
        }
    }
#endif
    void testCopy() {
        yapet::SecureArray a = yapet::SecureArray(1);
        **a = 42;

        yapet::SecureArray b = yapet::SecureArray{a};

        CPPUNIT_ASSERT(**a == 42);
        CPPUNIT_ASSERT(**a == **b);

        CPPUNIT_ASSERT(*a != *b);
    }

    void testAssignment() {
        yapet::SecureArray a = yapet::SecureArray{1};
        **a = 43;

        yapet::SecureArray b = yapet::SecureArray{1};
        **b = 42;

        a = b;

        CPPUNIT_ASSERT(**a == 42);
        CPPUNIT_ASSERT(*a != *b);
    }

    void testMove() {
        yapet::SecureArray a = yapet::SecureArray{1};
        **a = 42;

        yapet::SecureArray b{std::move(a)};

        CPPUNIT_ASSERT(nullptr == *a);
        CPPUNIT_ASSERT(**b == 42);

        CPPUNIT_ASSERT(a.size() == 0);
        CPPUNIT_ASSERT(b.size() == 1);
    }

    void testMoveAssignment() {
        yapet::SecureArray a = yapet::SecureArray{1};
        **a = 42;
        std::uint8_t *ptrAArray = *a;

        yapet::SecureArray b = yapet::SecureArray{1};
        **b = 43;
        std::uint8_t *ptrBArray = *b;

        b = std::move(a);

        CPPUNIT_ASSERT(nullptr == *a);
        CPPUNIT_ASSERT(a.size() == 0);
        CPPUNIT_ASSERT(b.size() == 1);

        CPPUNIT_ASSERT(**b == 42);
        CPPUNIT_ASSERT(ptrAArray == ptrBArray);
        CPPUNIT_ASSERT(*ptrAArray == 0);
    }

    void testEquality() {
        yapet::SecureArray a = yapet::SecureArray{1};
        **a = 42;
        CPPUNIT_ASSERT(a == a);

        yapet::SecureArray b = yapet::SecureArray{1};
        **b = 42;

        CPPUNIT_ASSERT(a == b);
        CPPUNIT_ASSERT(!(a != b));

        b = yapet::SecureArray{1};
        **b = 43;

        CPPUNIT_ASSERT(!(a == b));
        CPPUNIT_ASSERT(a != b);

        yapet::SecureArray empty;
        CPPUNIT_ASSERT(empty == empty);
        CPPUNIT_ASSERT(!(a == empty));
        CPPUNIT_ASSERT(a != empty);

        yapet::SecureArray empty2;
        CPPUNIT_ASSERT(empty == empty2);
    }

    void testIndexOperator() {
        yapet::SecureArray secureArray(3);

        CPPUNIT_ASSERT_THROW(secureArray[-1], std::out_of_range);
        CPPUNIT_ASSERT_THROW(secureArray[4], std::out_of_range);

        CPPUNIT_ASSERT_THROW(secureArray[-1] = 2, std::out_of_range);
        CPPUNIT_ASSERT_THROW(secureArray[4] = 3, std::out_of_range);

        for (auto i = 0; i < 3; i++) {
            secureArray[i] = i;
            CPPUNIT_ASSERT(secureArray[i] == i);
        }
    }

    void testEmptyAssignment() {
        yapet::SecureArray empty;
        yapet::SecureArray nonEmpty{1};
        nonEmpty = empty;
        CPPUNIT_ASSERT(nonEmpty.size() == 0);
        CPPUNIT_ASSERT(*nonEmpty == nullptr);

        yapet::SecureArray nonEmpty2{2};
        (*nonEmpty2)[0] = 'A';
        (*nonEmpty2)[1] = 'B';
        empty = nonEmpty2;
        CPPUNIT_ASSERT(empty.size() == 2);
        CPPUNIT_ASSERT((*empty)[0] == (std::uint8_t)'A');
        CPPUNIT_ASSERT((*empty)[1] == (std::uint8_t)'B');
    }

    void testEmptyCopy() {
        yapet::SecureArray empty;
        yapet::SecureArray empty2{empty};
        CPPUNIT_ASSERT(empty2.size() == 0);
        CPPUNIT_ASSERT(*empty2 == nullptr);

        yapet::SecureArray nonEmpty{2};
        (*nonEmpty)[0] = 'A';
        (*nonEmpty)[1] = 'B';
        yapet::SecureArray empty3{nonEmpty};
        CPPUNIT_ASSERT(empty3.size() == 2);
        CPPUNIT_ASSERT((*empty3)[0] == (std::uint8_t)'A');
        CPPUNIT_ASSERT((*empty3)[1] == (std::uint8_t)'B');
    }

    void testEmptyMove() {
        yapet::SecureArray empty;
        yapet::SecureArray empty2{std::move(empty)};
        CPPUNIT_ASSERT(empty2.size() == 0);
        CPPUNIT_ASSERT(*empty2 == nullptr);

        yapet::SecureArray nonEmpty{2};
        (*nonEmpty)[0] = 'A';
        (*nonEmpty)[1] = 'B';
        yapet::SecureArray empty3{std::move(nonEmpty)};
        CPPUNIT_ASSERT(empty3.size() == 2);
        CPPUNIT_ASSERT((*empty3)[0] == (std::uint8_t)'A');
        CPPUNIT_ASSERT((*empty3)[1] == (std::uint8_t)'B');
    }

    void testAdd() {
        yapet::SecureArray a{1};
        **a = 'A';

        yapet::SecureArray b{1};
        **b = 'B';

        yapet::SecureArray result = a + b;
        CPPUNIT_ASSERT(result.size() == 2);
        CPPUNIT_ASSERT(**result == 'A');
        CPPUNIT_ASSERT((*result)[1] == 'B');
    }

    void testToSecureArray() {
        yapet::SecureArray a = yapet::toSecureArray("ABCDE");
        yapet::SecureArray b = yapet::toSecureArray(std::string{"ABCDE"});
        yapet::SecureArray c = yapet::toSecureArray(
            reinterpret_cast<const std::uint8_t *>("ABCDE"), 6);

        CPPUNIT_ASSERT(a == b);
        CPPUNIT_ASSERT(b == c);
        CPPUNIT_ASSERT(a.size() == 6);
        CPPUNIT_ASSERT(b.size() == 6);
        CPPUNIT_ASSERT(c.size() == 6);
    }

    void testToSecureArrayEmpty() {
        yapet::SecureArray a = yapet::toSecureArray("");
        yapet::SecureArray expectedEmptySecureArrayFromCstring{1};
        **expectedEmptySecureArrayFromCstring = '\0';
        CPPUNIT_ASSERT(a == expectedEmptySecureArrayFromCstring);

        yapet::SecureArray b = yapet::toSecureArray(std::string{""});
        CPPUNIT_ASSERT(b == expectedEmptySecureArrayFromCstring);
    }

    void testCopyOperator() {
        yapet::SecureArray empty{};
        yapet::SecureArray a{1};
        **a = 'A';

        empty << a;
        CPPUNIT_ASSERT(empty.size() == 0);
        CPPUNIT_ASSERT(*empty == nullptr);
        CPPUNIT_ASSERT(a.size() == 1);
        CPPUNIT_ASSERT(**a == 'A');

        a << empty;
        CPPUNIT_ASSERT(a.size() == 1);
        CPPUNIT_ASSERT(**a == 'A');
        CPPUNIT_ASSERT(empty.size() == 0);
        CPPUNIT_ASSERT(*empty == nullptr);

        yapet::SecureArray bc{2};
        (*bc)[0] = 'B';
        (*bc)[1] = 'C';

        a << bc;
        CPPUNIT_ASSERT(a.size() == 1);
        CPPUNIT_ASSERT(**a == 'B');
        CPPUNIT_ASSERT(bc.size() == 2);
        CPPUNIT_ASSERT((*bc)[0] == 'B' && (*bc)[1] == 'C');
        CPPUNIT_ASSERT(*a != *bc);

        yapet::SecureArray d{1};
        **d = 'D';

        bc << d;
        CPPUNIT_ASSERT(bc.size() == 1);
        CPPUNIT_ASSERT(**bc == 'D');
        CPPUNIT_ASSERT(d.size() == 1);
        CPPUNIT_ASSERT(**d == 'D');
        CPPUNIT_ASSERT(*bc != *d);
    }
};

int main() {
    CppUnit::TextUi::TestRunner runner;
    runner.addTest(SecureArrayTest::suite());
    return runner.run() ? 0 : 1;
}