Repository navigation
Expand file tree
/
Copy pathentropy_sys_test.cpp
More file actions
131 lines (112 loc) · 4.75 KB
/
Copy pathentropy_sys_test.cpp
File metadata and controls
131 lines (112 loc) · 4.75 KB
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
/*
This file is part of libhttpserver
Copyright (C) 2011-2026 Sebastiano Merlino
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see the file LICENSE in
the distribution; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
// TASK-114 step 4: the OS randomness seam (entropy_sys.hpp) that
// Digest nonce generation will draw from (TASK-115). The suite pins
// the observable contract: zero-length fills succeed, fills honor
// the requested length and report ok, successive fills differ, a
// batch of fills is not degenerate (not all identical, not all zero,
// and not all repeated copies of one pattern), and a 4096-byte fill
// exercises whatever chunking the platform branch performs.
#include <cstddef>
#include <cstdint>
#include <string>
#include <vector>
#include <httpserver/detail/entropy_sys.hpp>
#include <httpserver/http/outcome.hpp>
#include "./littletest.hpp"
namespace {
namespace http = httpserver::http;
namespace detail = httpserver::detail;
bool all_zero(const std::vector<std::byte>& blob) {
for (const std::byte b : blob) {
if (b != std::byte{0}) return false;
}
return true;
}
std::vector<std::byte> drawn(std::size_t length) {
std::vector<std::byte> blob(length, std::byte{0});
const http::outcome filled = detail::entropy::fill(blob);
if (!filled.ok()) return {};
return blob;
}
} // namespace
LT_BEGIN_SUITE(entropy_suite)
void set_up() { }
void tear_down() { }
LT_END_SUITE(entropy_suite)
LT_BEGIN_AUTO_TEST(entropy_suite, zero_length_fill_succeeds)
std::vector<std::byte> none;
LT_CHECK(detail::entropy::fill(none).ok());
LT_END_AUTO_TEST(zero_length_fill_succeeds)
LT_BEGIN_AUTO_TEST(entropy_suite, single_byte_draw_preserves_bounds_and_reports_zero_rejection)
std::vector<std::byte> blob(3, std::byte{0x5a});
blob[1] = std::byte{0};
const auto filled = detail::entropy::fill(std::span(blob).subspan(1, 1));
LT_CHECK(blob.front() == std::byte{0x5a});
LT_CHECK(blob.back() == std::byte{0x5a});
// A valid single-byte OS draw is zero with probability 1/256; fill's
// documented all-zero rejection applies even to that small request.
if (blob[1] == std::byte{0}) {
LT_CHECK(filled.code() == http::outcome_code::protocol_error);
LT_CHECK(filled.message() == "entropy: OS randomness source returned zeros");
} else {
LT_CHECK(filled.ok());
}
LT_END_AUTO_TEST(single_byte_draw_preserves_bounds_and_reports_zero_rejection)
LT_BEGIN_AUTO_TEST(entropy_suite, fill_honors_length_and_reports_ok)
for (const std::size_t length : {std::size_t{16},
std::size_t{32}, std::size_t{64},
std::size_t{257}, std::size_t{4096}}) {
const std::vector<std::byte> blob = drawn(length);
LT_CHECK(blob.size() == length);
if (blob.size() == length) {
LT_CHECK(!all_zero(blob));
}
}
LT_END_AUTO_TEST(fill_honors_length_and_reports_ok)
LT_BEGIN_AUTO_TEST(entropy_suite, successive_fills_differ)
const std::vector<std::byte> first = drawn(32);
const std::vector<std::byte> second = drawn(32);
LT_CHECK(first.size() == 32);
LT_CHECK(second.size() == 32);
LT_CHECK(first != second);
LT_END_AUTO_TEST(successive_fills_differ)
LT_BEGIN_AUTO_TEST(entropy_suite, batch_of_fills_not_degenerate)
std::vector<std::vector<std::byte>> batch;
for (int i = 0; i < 256; ++i) {
batch.push_back(drawn(16));
}
std::size_t distinct = 0;
std::size_t nonzero = 0;
for (std::size_t i = 0; i < batch.size(); ++i) {
if (!all_zero(batch[i])) ++nonzero;
for (std::size_t j = 0; j < i; ++j) {
if (batch[i] == batch[j]) {
++distinct; // a duplicate pair
break;
}
}
}
// All 256 sixteen-byte fills identical would be a catastrophic
// generator; all zero is the degenerate case the seam rejects.
LT_CHECK(batch.size() == 256);
LT_CHECK(nonzero == 256);
LT_CHECK(distinct == 0);
LT_END_AUTO_TEST(batch_of_fills_not_degenerate)
LT_BEGIN_AUTO_TEST_ENV()
AUTORUN_TESTS()
LT_END_AUTO_TEST_ENV()