FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
hmac-cpp/test_secure_buffer.cpp at main · LimiNode/hmac-cpp · GitHub
LimiNode
/
hmac-cpp
Public
Notifications
You must be signed in to change notification settings
Fork
6
Star
11
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
hmac-cpp
/
test_secure_buffer.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
728 lines (597 loc) · 27.9 KB
Breadcrumbs
hmac-cpp
/
test_secure_buffer.cpp
Copy path
File metadata and controls
728 lines (597 loc) · 27.9 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
#
include
<
gtest/gtest.h
>
#
include
<
algorithm
>
#
include
<
array
>
#
include
<
cstddef
>
#
include
<
cstdint
>
#
include
<
limits
>
#
include
<
memory
>
#
include
<
stdexcept
>
#
include
<
system_error
>
#
include
"
hmac_cpp/page_lock_registry.hpp
"
#
include
"
hmac_cpp/secure_buffer.hpp
"
namespace
{
struct
DeallocationRecord
{
const
void
* ptr{};
std::
size_t
elements{};
bool
was_zeroed{};
};
struct
RecordingAllocator
{
using
value_type = std::
uint8_t
;
template
<
class
U
>
struct
rebind
{
using
other = RecordingAllocator;
};
RecordingAllocator
()
noexcept
=
default
;
template
<
class
U
>
RecordingAllocator
(
const
U&)
noexcept
{}
std::
uint8_t
*
allocate
(std::
size_t
count) {
return
std::allocator<std::
uint8_t
>{}.
allocate
(count);
}
void
deallocate
(std::
uint8_t
* ptr, std::
size_t
count)
noexcept
{
deallocations[deallocation_count++] = {
ptr,
count,
std::all_of
(ptr, ptr + count, [](std::
uint8_t
value) {
return
value ==
0
; })
};
std::allocator<std::
uint8_t
>{}.
deallocate
(ptr, count);
}
static
void
reset
()
noexcept
{ deallocation_count =
0
; }
static
std::array<DeallocationRecord,
128
> deallocations;
static
std::
size_t
deallocation_count;
};
bool
operator
==(
const
RecordingAllocator&,
const
RecordingAllocator&)
noexcept
{
return
true
; }
bool
operator
!=(
const
RecordingAllocator&,
const
RecordingAllocator&)
noexcept
{
return
false
; }
std::array<DeallocationRecord,
128
> RecordingAllocator::deallocations{};
std::
size_t
RecordingAllocator::deallocation_count{};
template
<
class
T
>
struct
StatefulAllocator
{
using
value_type = T;
template
<
class
U
>
struct
rebind
{
using
other = StatefulAllocator<U>;
};
explicit
StatefulAllocator
(
int
value =
0
)
noexcept
: id(value) {}
StatefulAllocator
(
const
StatefulAllocator&)
noexcept
=
default
;
StatefulAllocator
(StatefulAllocator&&)
noexcept
=
default
;
StatefulAllocator&
operator
=(
const
StatefulAllocator&)
noexcept
=
default
;
StatefulAllocator&
operator
=(StatefulAllocator&&)
noexcept
=
default
;
template
<
class
U
>
StatefulAllocator
(
const
StatefulAllocator<U>& other)
noexcept
: id(other.id) {}
T*
allocate
(std::
size_t
count) {
return
std::allocator<T>{}.
allocate
(count);
}
void
deallocate
(T* ptr, std::
size_t
count)
noexcept
{
std::allocator<T>{}.
deallocate
(ptr, count);
}
int
id{};
};
template
<
class
T
,
class
U
>
bool
operator
==(
const
StatefulAllocator<T>& lhs,
const
StatefulAllocator<U>& rhs)
noexcept
{
return
lhs.
id
== rhs.
id
;
}
template
<
class
T
,
class
U
>
bool
operator
!=(
const
StatefulAllocator<T>& lhs,
const
StatefulAllocator<U>& rhs)
noexcept
{
return
!(lhs == rhs);
}
enum
class
PageLockEventType
{ Lock, Unlock };
struct
PageLockEvent
{
PageLockEventType type{};
void
* ptr{};
std::
size_t
bytes{};
bool
was_zeroed_at_lock{};
bool
was_zeroed_at_unlock{};
};
struct
RecordingPageLocker
{
static
bool
lock
(
void
* ptr, std::
size_t
bytes)
noexcept
{
events[event_count++] = {
PageLockEventType::Lock,
ptr,
bytes,
std::all_of
(
static_cast
<
const
std::
uint8_t
*>(ptr),
static_cast
<
const
std::
uint8_t
*>(ptr) + bytes,
[](std::
uint8_t
value) {
return
value ==
0
; }),
false
};
return
lock_succeeds;
}
static
bool
unlock
(
void
* ptr, std::
size_t
bytes)
noexcept
{
events[event_count++] = {
PageLockEventType::Unlock,
ptr,
bytes,
false
,
std::all_of
(
static_cast
<
const
std::
uint8_t
*>(ptr),
static_cast
<
const
std::
uint8_t
*>(ptr) + bytes,
[](std::
uint8_t
value) {
return
value ==
0
; })
};
return
true
;
}
static
void
reset
()
noexcept
{ event_count =
0
; }
static
std::array<PageLockEvent,
16
> events;
static
std::
size_t
event_count;
static
bool
lock_succeeds;
};
std::array<PageLockEvent,
16
> RecordingPageLocker::events{};
std::
size_t
RecordingPageLocker::event_count{};
bool
RecordingPageLocker::lock_succeeds{
true
};
struct
ThrowingAllocator
{
using
value_type = std::
uint8_t
;
template
<
class
U
>
struct
rebind
{
using
other = ThrowingAllocator;
};
ThrowingAllocator
()
noexcept
=
default
;
template
<
class
U
>
ThrowingAllocator
(
const
U&)
noexcept
{}
std::
uint8_t
*
allocate
(std::
size_t
count) {
if
(fail_allocations) {
throw
std::bad_alloc
();
}
return
std::allocator<std::
uint8_t
>{}.
allocate
(count);
}
void
deallocate
(std::
uint8_t
* ptr, std::
size_t
count)
noexcept
{
std::allocator<std::
uint8_t
>{}.
deallocate
(ptr, count);
}
static
bool
fail_allocations;
};
bool
operator
==(
const
ThrowingAllocator&,
const
ThrowingAllocator&)
noexcept
{
return
true
; }
bool
operator
!=(
const
ThrowingAllocator&,
const
ThrowingAllocator&)
noexcept
{
return
false
; }
bool
ThrowingAllocator::fail_allocations{};
enum
class
RegistryEventType
{ Lock, Unlock };
struct
RegistryEvent
{
RegistryEventType type{};
void
* page{};
};
struct
RegistryPageLocker
{
bool
lock_page
(
void
* page, std::
size_t
)
const
noexcept
{
events[event_count++] = {RegistryEventType::Lock, page};
return
reinterpret_cast
<std::
uintptr_t
>(page) != failing_page;
}
bool
unlock_page
(
void
* page, std::
size_t
)
const
noexcept
{
events[event_count++] = {RegistryEventType::Unlock, page};
return
reinterpret_cast
<std::
uintptr_t
>(page) != failing_unlock_page;
}
static
void
reset
()
noexcept
{
event_count =
0
;
failing_page =
0
;
failing_unlock_page =
0
;
}
static
std::array<RegistryEvent,
16
> events;
static
std::
size_t
event_count;
static
std::
uintptr_t
failing_page;
static
std::
uintptr_t
failing_unlock_page;
};
std::array<RegistryEvent,
16
> RegistryPageLocker::events{};
std::
size_t
RegistryPageLocker::event_count{};
std::
uintptr_t
RegistryPageLocker::failing_page{};
std::
uintptr_t
RegistryPageLocker::failing_unlock_page{};
struct
ThrowingMutex
{
void
lock
() {
throw
std::system_error
(
std::make_error_code
(std::errc::resource_unavailable_try_again));
}
void
unlock
()
noexcept
{}
};
struct
PageStateAllocationControl
{
static
int
successful_allocations_before_failure;
};
int
PageStateAllocationControl::successful_allocations_before_failure{-
1
};
class
ScopedPageStateAllocationLimit
{
public:
ScopedPageStateAllocationLimit
()
noexcept
: saved_limit_(PageStateAllocationControl::successful_allocations_before_failure) {}
ScopedPageStateAllocationLimit
(
const
ScopedPageStateAllocationLimit&) =
delete
;
ScopedPageStateAllocationLimit&
operator
=(
const
ScopedPageStateAllocationLimit&) =
delete
;
~ScopedPageStateAllocationLimit
() {
PageStateAllocationControl::successful_allocations_before_failure = saved_limit_;
}
void
set
(
int
successful_allocations_before_failure)
noexcept
{
PageStateAllocationControl::successful_allocations_before_failure =
successful_allocations_before_failure;
}
private:
int
saved_limit_{};
};
template
<
class
T
>
struct
ThrowingPageStateAllocator
{
using
value_type = T;
ThrowingPageStateAllocator
()
noexcept
=
default
;
template
<
class
U
>
ThrowingPageStateAllocator
(
const
ThrowingPageStateAllocator<U>&)
noexcept
{}
T*
allocate
(std::
size_t
count) {
if
(PageStateAllocationControl::successful_allocations_before_failure ==
0
) {
throw
std::bad_alloc
();
}
if
(PageStateAllocationControl::successful_allocations_before_failure >
0
) {
--PageStateAllocationControl::successful_allocations_before_failure;
}
return
std::allocator<T>{}.
allocate
(count);
}
void
deallocate
(T* ptr, std::
size_t
count)
noexcept
{
std::allocator<T>{}.
deallocate
(ptr, count);
}
};
template
<
class
T
,
class
U
>
bool
operator
==(
const
ThrowingPageStateAllocator<T>&,
const
ThrowingPageStateAllocator<U>&)
noexcept
{
return
true
;
}
template
<
class
T
,
class
U
>
bool
operator
!=(
const
ThrowingPageStateAllocator<T>& lhs,
const
ThrowingPageStateAllocator<U>& rhs)
noexcept
{
return
!(lhs == rhs);
}
using
UnlockedBuffer = hmac_cpp::secure_buffer<std::
uint8_t
,
false
, RecordingAllocator>;
using
LockedBuffer = hmac_cpp::secure_buffer<std::
uint8_t
,
true
,
RecordingAllocator,
RecordingPageLocker>;
using
WideBuffer = hmac_cpp::secure_buffer<std::
uint32_t
,
true
,
std::allocator<std::
uint32_t
>,
RecordingPageLocker>;
using
StatefulBuffer = hmac_cpp::secure_buffer<std::
uint8_t
,
true
,
StatefulAllocator<std::
uint8_t
>,
RecordingPageLocker>;
using
ThrowingBuffer = hmac_cpp::secure_buffer<std::
uint8_t
,
false
, ThrowingAllocator>;
TEST
(SecureBufferTest, ResizeGrowthWipesOldAllocationBeforeDeallocate) {
UnlockedBuffer
buffer
(
8
);
for
(std::
size_t
index =
0
; index < buffer.
size
(); ++index) {
buffer[index] =
static_cast
<std::
uint8_t
>(index +
1
);
}
RecordingAllocator::reset
();
buffer.
resize
(
4096
);
ASSERT_EQ
(RecordingAllocator::deallocation_count,
1u
);
EXPECT_EQ
(RecordingAllocator::deallocations[
0
].
elements
,
8u
);
EXPECT_TRUE
(RecordingAllocator::deallocations[
0
].
was_zeroed
);
for
(std::
size_t
index =
0
; index <
8
; ++index) {
EXPECT_EQ
(buffer[index],
static_cast
<std::
uint8_t
>(index +
1
));
}
EXPECT_TRUE
(
std::all_of
(buffer.
begin
() +
8
, buffer.
end
(),
[](std::
uint8_t
value) {
return
value ==
0
; }));
}
TEST
(SecureBufferTest, ShrinkThenClearUnlocksTheOriginalRange) {
RecordingPageLocker::lock_succeeds =
true
;
LockedBuffer
buffer
(
8192
);
void
*
const
original_ptr = buffer.
data
();
RecordingPageLocker::reset
();
buffer.
resize
(
1
);
buffer.
clear
();
ASSERT_EQ
(RecordingPageLocker::event_count,
1u
);
EXPECT_EQ
(RecordingPageLocker::events[
0
].
type
, PageLockEventType::Unlock);
EXPECT_EQ
(RecordingPageLocker::events[
0
].
ptr
, original_ptr);
EXPECT_EQ
(RecordingPageLocker::events[
0
].
bytes
,
8192u
);
}
TEST
(SecureBufferTest, ShrinkThenGrowLocksNewRangeBeforeUnlockingOriginalRange) {
RecordingPageLocker::lock_succeeds =
true
;
LockedBuffer
buffer
(
8192
);
void
*
const
original_ptr = buffer.
data
();
RecordingPageLocker::reset
();
buffer.
resize
(
1
);
buffer.
resize
(
16384
);
ASSERT_EQ
(RecordingPageLocker::event_count,
2u
);
EXPECT_EQ
(RecordingPageLocker::events[
0
].
type
, PageLockEventType::Lock);
EXPECT_EQ
(RecordingPageLocker::events[
0
].
ptr
, buffer.
data
());
EXPECT_EQ
(RecordingPageLocker::events[
0
].
bytes
,
16384u
);
EXPECT_EQ
(RecordingPageLocker::events[
1
].
type
, PageLockEventType::Unlock);
EXPECT_EQ
(RecordingPageLocker::events[
1
].
ptr
, original_ptr);
EXPECT_EQ
(RecordingPageLocker::events[
1
].
bytes
,
8192u
);
EXPECT_TRUE
(RecordingPageLocker::events[
1
].
was_zeroed_at_unlock
);
void
*
const
replacement_ptr = buffer.
data
();
buffer.
clear
();
ASSERT_EQ
(RecordingPageLocker::event_count,
3u
);
EXPECT_EQ
(RecordingPageLocker::events[
2
].
type
, PageLockEventType::Unlock);
EXPECT_EQ
(RecordingPageLocker::events[
2
].
ptr
, replacement_ptr);
EXPECT_EQ
(RecordingPageLocker::events[
2
].
bytes
,
16384u
);
EXPECT_TRUE
(RecordingPageLocker::events[
2
].
was_zeroed_at_unlock
);
}
TEST
(SecureBufferTest, MultipleShrinksKeepTheOriginalLockRange) {
RecordingPageLocker::lock_succeeds =
true
;
LockedBuffer
buffer
(
8192
);
void
*
const
original_ptr = buffer.
data
();
RecordingPageLocker::reset
();
buffer.
resize
(
4097
);
buffer.
resize
(
1
);
buffer.
clear
();
ASSERT_EQ
(RecordingPageLocker::event_count,
1u
);
EXPECT_EQ
(RecordingPageLocker::events[
0
].
type
, PageLockEventType::Unlock);
EXPECT_EQ
(RecordingPageLocker::events[
0
].
ptr
, original_ptr);
EXPECT_EQ
(RecordingPageLocker::events[
0
].
bytes
,
8192u
);
EXPECT_TRUE
(RecordingPageLocker::events[
0
].
was_zeroed_at_unlock
);
}
TEST
(SecureBufferTest, CopyConstructionLocksAZeroedReplacementBeforeCopying) {
RecordingPageLocker::lock_succeeds =
true
;
LockedBuffer
source
(
8
);
std::fill
(source.
begin
(), source.
end
(),
static_cast
<std::
uint8_t
>(
0xA5
));
RecordingPageLocker::reset
();
LockedBuffer
copy
(source);
ASSERT_EQ
(RecordingPageLocker::event_count,
1u
);
EXPECT_EQ
(RecordingPageLocker::events[
0
].
type
, PageLockEventType::Lock);
EXPECT_EQ
(RecordingPageLocker::events[
0
].
ptr
, copy.
data
());
EXPECT_EQ
(RecordingPageLocker::events[
0
].
bytes
,
8u
);
EXPECT_TRUE
(RecordingPageLocker::events[
0
].
was_zeroed_at_lock
);
EXPECT_TRUE
(
std::equal
(source.
begin
(), source.
end
(), copy.
begin
()));
}
TEST
(SecureBufferTest, StringConstructionLocksAZeroedReplacementBeforeCopying) {
RecordingPageLocker::lock_succeeds =
true
;
std::string
source
(
"
secret
"
);
RecordingPageLocker::reset
();
LockedBuffer
buffer
(
std::move
(source));
ASSERT_EQ
(RecordingPageLocker::event_count,
1u
);
EXPECT_EQ
(RecordingPageLocker::events[
0
].
type
, PageLockEventType::Lock);
EXPECT_EQ
(RecordingPageLocker::events[
0
].
ptr
, buffer.
data
());
EXPECT_EQ
(RecordingPageLocker::events[
0
].
bytes
,
6u
);
EXPECT_TRUE
(RecordingPageLocker::events[
0
].
was_zeroed_at_lock
);
EXPECT_EQ
(buffer.
size
(),
6u
);
EXPECT_TRUE
(source.
empty
());
}
TEST
(SecureBufferTest, CopyAssignmentLocksReplacementBeforeReleasingDestination) {
RecordingPageLocker::lock_succeeds =
true
;
LockedBuffer
source
(
8
);
std::fill
(source.
begin
(), source.
end
(),
static_cast
<std::
uint8_t
>(
0xA5
));
LockedBuffer
destination
(
4
);
void
*
const
destination_ptr = destination.
data
();
RecordingPageLocker::reset
();
destination = source;
ASSERT_EQ
(RecordingPageLocker::event_count,
2u
);
EXPECT_EQ
(RecordingPageLocker::events[
0
].
type
, PageLockEventType::Lock);
EXPECT_EQ
(RecordingPageLocker::events[
0
].
ptr
, destination.
data
());
EXPECT_EQ
(RecordingPageLocker::events[
0
].
bytes
,
8u
);
EXPECT_TRUE
(RecordingPageLocker::events[
0
].
was_zeroed_at_lock
);
EXPECT_EQ
(RecordingPageLocker::events[
1
].
type
, PageLockEventType::Unlock);
EXPECT_EQ
(RecordingPageLocker::events[
1
].
ptr
, destination_ptr);
EXPECT_EQ
(RecordingPageLocker::events[
1
].
bytes
,
4u
);
EXPECT_TRUE
(RecordingPageLocker::events[
1
].
was_zeroed_at_unlock
);
EXPECT_TRUE
(
std::equal
(source.
begin
(), source.
end
(), destination.
begin
()));
}
TEST
(SecureBufferTest, AssignLocksReplacementBeforeReleasingDestination) {
RecordingPageLocker::lock_succeeds =
true
;
const
std::array<std::
uint8_t
,
8
> source{{
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
}};
LockedBuffer
destination
(
4
);
void
*
const
destination_ptr = destination.
data
();
RecordingPageLocker::reset
();
destination.
assign
(source.
data
(), source.
size
());
ASSERT_EQ
(RecordingPageLocker::event_count,
2u
);
EXPECT_EQ
(RecordingPageLocker::events[
0
].
type
, PageLockEventType::Lock);
EXPECT_EQ
(RecordingPageLocker::events[
0
].
ptr
, destination.
data
());
EXPECT_EQ
(RecordingPageLocker::events[
0
].
bytes
,
8u
);
EXPECT_TRUE
(RecordingPageLocker::events[
0
].
was_zeroed_at_lock
);
EXPECT_EQ
(RecordingPageLocker::events[
1
].
type
, PageLockEventType::Unlock);
EXPECT_EQ
(RecordingPageLocker::events[
1
].
ptr
, destination_ptr);
EXPECT_EQ
(RecordingPageLocker::events[
1
].
bytes
,
4u
);
EXPECT_TRUE
(RecordingPageLocker::events[
1
].
was_zeroed_at_unlock
);
EXPECT_TRUE
(
std::equal
(source.
begin
(), source.
end
(), destination.
begin
()));
}
TEST
(SecureBufferTest, MoveConstructionTransfersTheLockedRange) {
RecordingPageLocker::lock_succeeds =
true
;
LockedBuffer
source
(
8
);
void
*
const
source_ptr = source.
data
();
RecordingPageLocker::reset
();
LockedBuffer
destination
(
std::move
(source));
EXPECT_EQ
(destination.
data
(), source_ptr);
EXPECT_TRUE
(destination.
is_locked
());
EXPECT_FALSE
(source.
is_locked
());
EXPECT_EQ
(RecordingPageLocker::event_count,
0u
);
destination.
clear
();
ASSERT_EQ
(RecordingPageLocker::event_count,
1u
);
EXPECT_EQ
(RecordingPageLocker::events[
0
].
type
, PageLockEventType::Unlock);
EXPECT_EQ
(RecordingPageLocker::events[
0
].
ptr
, source_ptr);
EXPECT_EQ
(RecordingPageLocker::events[
0
].
bytes
,
8u
);
}
TEST
(SecureBufferTest, MoveAssignmentTransfersTheLockedRangeForEqualAllocators) {
RecordingPageLocker::lock_succeeds =
true
;
LockedBuffer
source
(
8
);
void
*
const
source_ptr = source.
data
();
LockedBuffer
destination
(
4
);
void
*
const
destination_ptr = destination.
data
();
RecordingPageLocker::reset
();
destination =
std::move
(source);
EXPECT_EQ
(destination.
data
(), source_ptr);
EXPECT_TRUE
(destination.
is_locked
());
EXPECT_FALSE
(source.
is_locked
());
ASSERT_EQ
(RecordingPageLocker::event_count,
1u
);
EXPECT_EQ
(RecordingPageLocker::events[
0
].
type
, PageLockEventType::Unlock);
EXPECT_EQ
(RecordingPageLocker::events[
0
].
ptr
, destination_ptr);
EXPECT_EQ
(RecordingPageLocker::events[
0
].
bytes
,
4u
);
destination.
clear
();
ASSERT_EQ
(RecordingPageLocker::event_count,
2u
);
EXPECT_EQ
(RecordingPageLocker::events[
1
].
type
, PageLockEventType::Unlock);
EXPECT_EQ
(RecordingPageLocker::events[
1
].
ptr
, source_ptr);
EXPECT_EQ
(RecordingPageLocker::events[
1
].
bytes
,
8u
);
}
TEST
(SecureBufferTest, MoveAssignmentCopiesSafelyForUnequalAllocators) {
RecordingPageLocker::lock_succeeds =
true
;
StatefulBuffer::vector_type
source_storage
(
8
,
static_cast
<std::
uint8_t
>(
0xA5
),
StatefulAllocator<std::
uint8_t
>(
1
));
StatefulBuffer
source
(
std::move
(source_storage));
void
*
const
source_ptr = source.
data
();
StatefulBuffer
destination
(
4
);
void
*
const
destination_ptr = destination.
data
();
RecordingPageLocker::reset
();
destination =
std::move
(source);
ASSERT_EQ
(RecordingPageLocker::event_count,
3u
);
EXPECT_EQ
(RecordingPageLocker::events[
0
].
type
, PageLockEventType::Lock);
EXPECT_EQ
(RecordingPageLocker::events[
0
].
ptr
, destination.
data
());
EXPECT_EQ
(RecordingPageLocker::events[
0
].
bytes
,
8u
);
EXPECT_TRUE
(RecordingPageLocker::events[
0
].
was_zeroed_at_lock
);
EXPECT_EQ
(RecordingPageLocker::events[
1
].
type
, PageLockEventType::Unlock);
EXPECT_EQ
(RecordingPageLocker::events[
1
].
ptr
, destination_ptr);
EXPECT_EQ
(RecordingPageLocker::events[
1
].
bytes
,
4u
);
EXPECT_EQ
(RecordingPageLocker::events[
2
].
type
, PageLockEventType::Unlock);
EXPECT_EQ
(RecordingPageLocker::events[
2
].
ptr
, source_ptr);
EXPECT_EQ
(RecordingPageLocker::events[
2
].
bytes
,
8u
);
EXPECT_TRUE
(RecordingPageLocker::events[
1
].
was_zeroed_at_unlock
);
EXPECT_TRUE
(RecordingPageLocker::events[
2
].
was_zeroed_at_unlock
);
EXPECT_FALSE
(source.
is_locked
());
EXPECT_TRUE
(
std::all_of
(destination.
begin
(), destination.
end
(),
[](std::
uint8_t
value) {
return
value ==
0xA5
; }));
}
TEST
(SecureBufferTest, FailedPageLockLeavesTheBufferUsableAndUnlocked) {
RecordingPageLocker::lock_succeeds =
false
;
RecordingPageLocker::reset
();
LockedBuffer
buffer
(
32
);
EXPECT_FALSE
(buffer.
is_locked
());
ASSERT_EQ
(RecordingPageLocker::event_count,
1u
);
EXPECT_EQ
(RecordingPageLocker::events[
0
].
type
, PageLockEventType::Lock);
buffer.
clear
();
EXPECT_EQ
(RecordingPageLocker::event_count,
1u
);
RecordingPageLocker::lock_succeeds =
true
;
}
TEST
(SecureBufferTest, LockRangeUsesByteSizeForWiderElementTypes) {
RecordingPageLocker::lock_succeeds =
true
;
RecordingPageLocker::reset
();
WideBuffer
buffer
(
3
);
void
*
const
buffer_ptr = buffer.
data
();
ASSERT_EQ
(RecordingPageLocker::event_count,
1u
);
EXPECT_EQ
(RecordingPageLocker::events[
0
].
type
, PageLockEventType::Lock);
EXPECT_EQ
(RecordingPageLocker::events[
0
].
ptr
, buffer_ptr);
EXPECT_EQ
(RecordingPageLocker::events[
0
].
bytes
,
3u
*
sizeof
(std::
uint32_t
));
EXPECT_TRUE
(RecordingPageLocker::events[
0
].
was_zeroed_at_lock
);
buffer.
clear
();
ASSERT_EQ
(RecordingPageLocker::event_count,
2u
);
EXPECT_EQ
(RecordingPageLocker::events[
1
].
type
, PageLockEventType::Unlock);
EXPECT_EQ
(RecordingPageLocker::events[
1
].
ptr
, buffer_ptr);
EXPECT_EQ
(RecordingPageLocker::events[
1
].
bytes
,
3u
*
sizeof
(std::
uint32_t
));
}
TEST
(SecureBufferTest, RejectsElementCountsWhoseByteSizeOverflows) {
hmac_cpp::secure_buffer<std::
uint32_t
> buffer;
EXPECT_THROW
(buffer.
resize
(std::numeric_limits<std::
size_t
>::
max
()), std::length_error);
}
TEST
(SecureBufferTest, StringAssignWipesTheSourceWhenReplacementAllocationFails) {
ThrowingAllocator::fail_allocations =
false
;
ThrowingBuffer
destination
(
4
);
destination[
0
] =
0x5A
;
const
void
*
const
destination_ptr = destination.
data
();
std::string
source
(
128
,
'
s
'
);
const
char
*
const
source_data = source.
data
();
const
std::
size_t
source_size = source.
size
();
ThrowingAllocator::fail_allocations =
true
;
EXPECT_THROW
(destination.
assign
(
std::move
(source)), std::bad_alloc);
ThrowingAllocator::fail_allocations =
false
;
EXPECT_TRUE
(source.
empty
());
EXPECT_TRUE
(
std::all_of
(source_data, source_data + source_size,
[](
char
value) {
return
value ==
0
; }));
EXPECT_EQ
(destination.
data
(), destination_ptr);
EXPECT_EQ
(destination.
size
(),
4u
);
EXPECT_EQ
(destination[
0
],
0x5A
);
}
TEST
(PageLockRegistryTest, SharedPageRemainsLockedUntilEveryOwnerReleasesIt) {
RegistryPageLocker::reset
();
hmac_cpp::detail::page_lock_registry<RegistryPageLocker>
registry
(
4096
);
void
*
const
first =
reinterpret_cast
<
void
*>(
static_cast
<std::
uintptr_t
>(
0x1001
));
void
*
const
second =
reinterpret_cast
<
void
*>(
static_cast
<std::
uintptr_t
>(
0x1008
));
ASSERT_TRUE
(registry.
lock
(first,
8
));
ASSERT_TRUE
(registry.
lock
(second,
8
));
ASSERT_TRUE
(registry.
unlock
(first,
8
));
ASSERT_EQ
(RegistryPageLocker::event_count,
1u
);
EXPECT_EQ
(RegistryPageLocker::events[
0
].
type
, RegistryEventType::Lock);
EXPECT_EQ
(
reinterpret_cast
<std::
uintptr_t
>(RegistryPageLocker::events[
0
].
page
),
0x1000u
);
ASSERT_TRUE
(registry.
unlock
(second,
8
));
ASSERT_EQ
(RegistryPageLocker::event_count,
2u
);
EXPECT_EQ
(RegistryPageLocker::events[
1
].
type
, RegistryEventType::Unlock);
EXPECT_EQ
(
reinterpret_cast
<std::
uintptr_t
>(RegistryPageLocker::events[
1
].
page
),
0x1000u
);
}
TEST
(PageLockRegistryTest, RollsBackPagesLockedBeforeAPartialFailure) {
RegistryPageLocker::reset
();
RegistryPageLocker::failing_page =
0x2000
;
hmac_cpp::detail::page_lock_registry<RegistryPageLocker>
registry
(
4096
);
void
*
const
range =
reinterpret_cast
<
void
*>(
static_cast
<std::
uintptr_t
>(
0x1ff8
));
EXPECT_FALSE
(registry.
lock
(range,
16
));
ASSERT_EQ
(RegistryPageLocker::event_count,
3u
);
EXPECT_EQ
(RegistryPageLocker::events[
0
].
type
, RegistryEventType::Lock);
EXPECT_EQ
(
reinterpret_cast
<std::
uintptr_t
>(RegistryPageLocker::events[
0
].
page
),
0x1000u
);
EXPECT_EQ
(RegistryPageLocker::events[
1
].
type
, RegistryEventType::Lock);
EXPECT_EQ
(
reinterpret_cast
<std::
uintptr_t
>(RegistryPageLocker::events[
1
].
page
),
0x2000u
);
EXPECT_EQ
(RegistryPageLocker::events[
2
].
type
, RegistryEventType::Unlock);
EXPECT_EQ
(
reinterpret_cast
<std::
uintptr_t
>(RegistryPageLocker::events[
2
].
page
),
0x1000u
);
EXPECT_FALSE
(registry.
unlock
(range,
16
));
}
TEST
(PageLockRegistryTest, PageStateInsertionFailureReturnsFalseAndClearsPreparedEntries) {
using
PageState = hmac_cpp::detail::page_lock_page_state;
using
Allocator = ThrowingPageStateAllocator<std::pair<
const
std::
uintptr_t
, PageState>>;
using
Registry = hmac_cpp::detail::page_lock_registry<RegistryPageLocker, std::mutex, Allocator>;
RegistryPageLocker::reset
();
ScopedPageStateAllocationLimit allocation_limit;
allocation_limit.
set
(
1
);
Registry
registry
(
4096
);
void
*
const
range =
reinterpret_cast
<
void
*>(
static_cast
<std::
uintptr_t
>(
0x1000
));
EXPECT_FALSE
(registry.
lock
(range,
8193
));
EXPECT_EQ
(RegistryPageLocker::event_count,
0u
);
//
If prepared entries remained, this range would reuse the first one and
//
reach the OS locker despite allocations being disabled.
allocation_limit.
set
(
0
);
EXPECT_FALSE
(registry.
lock
(range,
8
));
EXPECT_EQ
(RegistryPageLocker::event_count,
0u
);
allocation_limit.
set
(-
1
);
EXPECT_TRUE
(registry.
lock
(range,
8
));
EXPECT_TRUE
(registry.
unlock
(range,
8
));
EXPECT_EQ
(RegistryPageLocker::event_count,
2u
);
}
TEST
(PageLockRegistryTest, UnlockFailureDoesNotCreateAPhantomOwner) {
RegistryPageLocker::reset
();
hmac_cpp::detail::page_lock_registry<RegistryPageLocker>
registry
(
4096
);
void
*
const
page =
reinterpret_cast
<
void
*>(
static_cast
<std::
uintptr_t
>(
0x1001
));
ASSERT_TRUE
(registry.
lock
(page,
8
));
RegistryPageLocker::failing_unlock_page =
0x1000
;
EXPECT_FALSE
(registry.
unlock
(page,
8
));
EXPECT_FALSE
(registry.
unlock
(page,
8
));
ASSERT_EQ
(RegistryPageLocker::event_count,
2u
);
RegistryPageLocker::failing_unlock_page =
0
;
ASSERT_TRUE
(registry.
lock
(page,
8
));
ASSERT_EQ
(RegistryPageLocker::event_count,
2u
);
EXPECT_TRUE
(registry.
unlock
(page,
8
));
ASSERT_EQ
(RegistryPageLocker::event_count,
3u
);
EXPECT_EQ
(RegistryPageLocker::events[
2
].
type
, RegistryEventType::Unlock);
}
TEST
(PageLockRegistryTest, RetryAfterFailedMixedRangeUnlockPreservesOverlappingOwner) {
RegistryPageLocker::reset
();
hmac_cpp::detail::page_lock_registry<RegistryPageLocker>
registry
(
4096
);
void
*
const
mixed_range =
reinterpret_cast
<
void
*>(
static_cast
<std::
uintptr_t
>(
0x1000
));
void
*
const
overlapping_page =
reinterpret_cast
<
void
*>(
static_cast
<std::
uintptr_t
>(
0x1004
));
void
*
const
second_page =
reinterpret_cast
<
void
*>(
static_cast
<std::
uintptr_t
>(
0x2000
));
ASSERT_TRUE
(registry.
lock
(mixed_range,
4097
));
ASSERT_TRUE
(registry.
lock
(overlapping_page,
8
));
RegistryPageLocker::failing_unlock_page =
0x2000
;
EXPECT_FALSE
(registry.
unlock
(mixed_range,
4097
));
ASSERT_EQ
(RegistryPageLocker::event_count,
3u
);
RegistryPageLocker::failing_unlock_page =
0
;
EXPECT_FALSE
(registry.
unlock
(mixed_range,
4097
));
ASSERT_EQ
(RegistryPageLocker::event_count,
3u
);
EXPECT_TRUE
(registry.
unlock
(overlapping_page,
8
));
ASSERT_EQ
(RegistryPageLocker::event_count,
4u
);
EXPECT_EQ
(RegistryPageLocker::events[
3
].
type
, RegistryEventType::Unlock);
EXPECT_EQ
(RegistryPageLocker::events[
3
].
page
, mixed_range);
ASSERT_TRUE
(registry.
lock
(second_page,
8
));
ASSERT_EQ
(RegistryPageLocker::event_count,
4u
);
EXPECT_TRUE
(registry.
unlock
(second_page,
8
));
ASSERT_EQ
(RegistryPageLocker::event_count,
5u
);
}
TEST
(PageLockRegistryTest, RollbackUnlockFailureCanBeReleasedByAFutureOwner) {
RegistryPageLocker::reset
();
hmac_cpp::detail::page_lock_registry<RegistryPageLocker>
registry
(
4096
);
void
*
const
mixed_range =
reinterpret_cast
<
void
*>(
static_cast
<std::
uintptr_t
>(
0x1000
));
void
*
const
first_page =
reinterpret_cast
<
void
*>(
static_cast
<std::
uintptr_t
>(
0x1008
));
RegistryPageLocker::failing_page =
0x2000
;
RegistryPageLocker::failing_unlock_page =
0x1000
;
EXPECT_FALSE
(registry.
lock
(mixed_range,
4097
));
ASSERT_EQ
(RegistryPageLocker::event_count,
3u
);
RegistryPageLocker::failing_page =
0
;
RegistryPageLocker::failing_unlock_page =
0
;
EXPECT_FALSE
(registry.
unlock
(mixed_range,
4097
));
ASSERT_EQ
(RegistryPageLocker::event_count,
3u
);
ASSERT_TRUE
(registry.
lock
(first_page,
8
));
ASSERT_EQ
(RegistryPageLocker::event_count,
3u
);
EXPECT_TRUE
(registry.
unlock
(first_page,
8
));
ASSERT_EQ
(RegistryPageLocker::event_count,
4u
);
EXPECT_EQ
(RegistryPageLocker::events[
3
].
type
, RegistryEventType::Unlock);
EXPECT_EQ
(RegistryPageLocker::events[
3
].
page
,
reinterpret_cast
<
void
*>(
static_cast
<std::
uintptr_t
>(
0x1000
)));
}
TEST
(PageLockRegistryTest, MutexFailureReturnsFalseInsteadOfTerminating) {
RegistryPageLocker::reset
();
hmac_cpp::detail::page_lock_registry<RegistryPageLocker, ThrowingMutex>
registry
(
4096
);
void
*
const
page =
reinterpret_cast
<
void
*>(
static_cast
<std::
uintptr_t
>(
0x1001
));
EXPECT_FALSE
(registry.
lock
(page,
8
));
EXPECT_FALSE
(registry.
unlock
(page,
8
));
EXPECT_EQ
(RegistryPageLocker::event_count,
0u
);
}
}
//
namespace
Back
|
FazBrowse Home
|
New Git URL