1// Copyright 2017 The Fuchsia Authors
2//
3// Use of this source code is governed by a MIT-style
4// license that can be found in the LICENSE file or at
5// https://opensource.org/licenses/MIT
6
7#include <lib/crypto/entropy/collector.h>
8#include <lib/unittest/unittest.h>
9
10namespace crypto {
11
12namespace entropy {
13
14namespace {
15
16class MockCollector : public Collector {
17public:
18    MockCollector(size_t entropy_per_1000_bytes)
19         : Collector("mock", entropy_per_1000_bytes) {}
20
21    ~MockCollector() {}
22
23    size_t DrawEntropy(uint8_t* buf, size_t len) override { return 0; }
24};
25
26bool entropy_estimate_test() {
27    BEGIN_TEST;
28
29    MockCollector ec_1(1);
30    EXPECT_EQ(0u, ec_1.BytesNeeded(0),
31              "bad entropy estimate (entropy per 1000 bytes = 1, bits = 0");
32    EXPECT_EQ(1000u, ec_1.BytesNeeded(1),
33              "bad entropy estimate (entropy per 1000 bytes = 1, bits = 1");
34    EXPECT_EQ(2000u, ec_1.BytesNeeded(2),
35              "bad entropy estimate (entropy per 1000 bytes = 1, bits = 2");
36    EXPECT_EQ(1048575000u, ec_1.BytesNeeded((1024*1024-1)),
37              "bad entropy estimate (entropy per 1000 bytes = 1, bits = (1024*1024-1)");
38    EXPECT_EQ(1048576000u, ec_1.BytesNeeded((1024*1024)),
39              "bad entropy estimate (entropy per 1000 bytes = 1, bits = (1024*1024)");
40
41    MockCollector ec_2(2);
42    EXPECT_EQ(0u, ec_2.BytesNeeded(0),
43              "bad entropy estimate (entropy per 1000 bytes = 2, bits = 0");
44    EXPECT_EQ(500u, ec_2.BytesNeeded(1),
45              "bad entropy estimate (entropy per 1000 bytes = 2, bits = 1");
46    EXPECT_EQ(1000u, ec_2.BytesNeeded(2),
47              "bad entropy estimate (entropy per 1000 bytes = 2, bits = 2");
48    EXPECT_EQ(1500u, ec_2.BytesNeeded(3),
49              "bad entropy estimate (entropy per 1000 bytes = 2, bits = 3");
50    EXPECT_EQ(524287500u, ec_2.BytesNeeded((1024*1024-1)),
51              "bad entropy estimate (entropy per 1000 bytes = 2, bits = (1024*1024-1)");
52    EXPECT_EQ(524288000u, ec_2.BytesNeeded((1024*1024)),
53              "bad entropy estimate (entropy per 1000 bytes = 2, bits = (1024*1024)");
54
55    MockCollector ec_3999(3999);
56    EXPECT_EQ(0u, ec_3999.BytesNeeded(0),
57              "bad entropy estimate (entropy per 1000 bytes = 3999, bits = 0");
58    EXPECT_EQ(1u, ec_3999.BytesNeeded(1),
59              "bad entropy estimate (entropy per 1000 bytes = 3999, bits = 1");
60    EXPECT_EQ(1000u, ec_3999.BytesNeeded(3998),
61              "bad entropy estimate (entropy per 1000 bytes = 3999, bits = 3998");
62    EXPECT_EQ(1000u, ec_3999.BytesNeeded(3999),
63              "bad entropy estimate (entropy per 1000 bytes = 3999, bits = 3999");
64    EXPECT_EQ(1001u, ec_3999.BytesNeeded(4000),
65              "bad entropy estimate (entropy per 1000 bytes = 3999, bits = 4000");
66    EXPECT_EQ(262210u, ec_3999.BytesNeeded((1024*1024-1)),
67              "bad entropy estimate (entropy per 1000 bytes = 3999, bits = (1024*1024-1)");
68    EXPECT_EQ(262210u, ec_3999.BytesNeeded((1024*1024)),
69              "bad entropy estimate (entropy per 1000 bytes = 3999, bits = (1024*1024)");
70
71    MockCollector ec_4000(4000);
72    EXPECT_EQ(0u, ec_4000.BytesNeeded(0),
73              "bad entropy estimate (entropy per 1000 bytes = 4000, bits = 0");
74    EXPECT_EQ(1u, ec_4000.BytesNeeded(1),
75              "bad entropy estimate (entropy per 1000 bytes = 4000, bits = 1");
76    EXPECT_EQ(1000u, ec_4000.BytesNeeded(3999),
77              "bad entropy estimate (entropy per 1000 bytes = 4000, bits = 3999");
78    EXPECT_EQ(1000u, ec_4000.BytesNeeded(4000),
79              "bad entropy estimate (entropy per 1000 bytes = 4000, bits = 4000");
80    EXPECT_EQ(1001u, ec_4000.BytesNeeded(4001),
81              "bad entropy estimate (entropy per 1000 bytes = 4000, bits = 4001");
82    EXPECT_EQ(262144u, ec_4000.BytesNeeded((1024*1024-1)),
83              "bad entropy estimate (entropy per 1000 bytes = 4000, bits = (1024*1024-1)");
84    EXPECT_EQ(262144u, ec_4000.BytesNeeded((1024*1024)),
85              "bad entropy estimate (entropy per 1000 bytes = 4000, bits = (1024*1024)");
86
87    MockCollector ec_4001(4001);
88    EXPECT_EQ(0u, ec_4001.BytesNeeded(0),
89              "bad entropy estimate (entropy per 1000 bytes = 4001, bits = 0");
90    EXPECT_EQ(1u, ec_4001.BytesNeeded(1),
91              "bad entropy estimate (entropy per 1000 bytes = 4001, bits = 1");
92    EXPECT_EQ(1000u, ec_4001.BytesNeeded(4000),
93              "bad entropy estimate (entropy per 1000 bytes = 4001, bits = 4000");
94    EXPECT_EQ(1000u, ec_4001.BytesNeeded(4001),
95              "bad entropy estimate (entropy per 1000 bytes = 4001, bits = 4001");
96    EXPECT_EQ(1001u, ec_4001.BytesNeeded(4002),
97              "bad entropy estimate (entropy per 1000 bytes = 4001, bits = 4002");
98    EXPECT_EQ(262079u, ec_4001.BytesNeeded((1024*1024-1)),
99              "bad entropy estimate (entropy per 1000 bytes = 4001, bits = (1024*1024-1)");
100    EXPECT_EQ(262079u, ec_4001.BytesNeeded((1024*1024)),
101              "bad entropy estimate (entropy per 1000 bytes = 4001, bits = (1024*1024)");
102
103    MockCollector ec_7999(7999);
104    EXPECT_EQ(0u, ec_7999.BytesNeeded(0),
105              "bad entropy estimate (entropy per 1000 bytes = 7999, bits = 0");
106    EXPECT_EQ(1u, ec_7999.BytesNeeded(1),
107              "bad entropy estimate (entropy per 1000 bytes = 7999, bits = 1");
108    EXPECT_EQ(1000u, ec_7999.BytesNeeded(7998),
109              "bad entropy estimate (entropy per 1000 bytes = 7999, bits = 7998");
110    EXPECT_EQ(1000u, ec_7999.BytesNeeded(7999),
111              "bad entropy estimate (entropy per 1000 bytes = 7999, bits = 7999");
112    EXPECT_EQ(1001u, ec_7999.BytesNeeded(8000),
113              "bad entropy estimate (entropy per 1000 bytes = 7999, bits = 8000");
114    EXPECT_EQ(131089u, ec_7999.BytesNeeded((1024*1024-1)),
115              "bad entropy estimate (entropy per 1000 bytes = 7999, bits = (1024*1024-1)");
116    EXPECT_EQ(131089u, ec_7999.BytesNeeded((1024*1024)),
117              "bad entropy estimate (entropy per 1000 bytes = 7999, bits = (1024*1024)");
118
119    MockCollector ec_8000(8000);
120    EXPECT_EQ(0u, ec_8000.BytesNeeded(0),
121              "bad entropy estimate (entropy per 1000 bytes = 8000, bits = 0");
122    EXPECT_EQ(1u, ec_8000.BytesNeeded(1),
123              "bad entropy estimate (entropy per 1000 bytes = 8000, bits = 1");
124    EXPECT_EQ(1000u, ec_8000.BytesNeeded(7999),
125              "bad entropy estimate (entropy per 1000 bytes = 8000, bits = 7999");
126    EXPECT_EQ(1000u, ec_8000.BytesNeeded(8000),
127              "bad entropy estimate (entropy per 1000 bytes = 8000, bits = 8000");
128    EXPECT_EQ(1001u, ec_8000.BytesNeeded(8001),
129              "bad entropy estimate (entropy per 1000 bytes = 8000, bits = 8001");
130    EXPECT_EQ(131072u, ec_8000.BytesNeeded((1024*1024-1)),
131              "bad entropy estimate (entropy per 1000 bytes = 8000, bits = (1024*1024-1)");
132    EXPECT_EQ(131072u, ec_8000.BytesNeeded((1024*1024)),
133              "bad entropy estimate (entropy per 1000 bytes = 8000, bits = (1024*1024)");
134
135    END_TEST;
136}
137
138} // namespace
139
140UNITTEST_START_TESTCASE(entropy_collector_tests)
141UNITTEST("test entropy estimates", entropy_estimate_test)
142UNITTEST_END_TESTCASE(entropy_collector_tests, "entropy_collector",
143                      "Test entropy collector implementation.");
144
145} // namespace entropy
146
147} // namespace crypto
148