Debian Codegraph @

crt/aws-c-common/tests/device_random_test.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5 #include <aws/common/device_random.h>
6
7 #include <aws/common/byte_buf.h>
8
9 #include <aws/testing/aws_test_harness.h>
10
11 #include <math.h>
12
13 /* Number of random numbers to generate and put in buckets. Higher numbers mean more tolerance */
14 #define DISTRIBUTION_PUT_COUNT 1000000
15
16 /* Must be a power of 2. Lower numbers mean more tolerance. */
17 #define DISTRIBUTION_BUCKET_COUNT 16
18
19 /* Fail if a bucket's contents vary from expected by more than this ratio. Higher ratio means more tolerance.
20 * For example, if putting 1000 numbers into 10 buckets, we expect 100 in each bucket.
21 * If ratio is 0.25 than accept 75 -> 125 numbers per bucket. */
22 #define DISTRIBUTION_ACCEPTED_DEVIATION_RATIO 0.05
23
24 /* For testing that random number generator has a uniform distribution.
25 * They're RANDOM numbers, so to avoid RANDOM failures use lots of inputs and be tolerate some deviance */
26 struct distribution_tester {
27 uint64_t max_value;
28 uint64_t buckets[DISTRIBUTION_BUCKET_COUNT];
29 uint64_t num_puts;
30 };
31
32 static int s_distribution_tester_put(struct distribution_tester *tester, uint64_t rand_num) {
33 ASSERT_TRUE(rand_num <= tester->max_value);
34 uint64_t bucket_size = (tester->max_value / DISTRIBUTION_BUCKET_COUNT) + 1;
35 uint64_t bucket_idx = rand_num / bucket_size;
36 ASSERT_TRUE(bucket_idx < DISTRIBUTION_BUCKET_COUNT);
37 tester->buckets[bucket_idx]++;
38 tester->num_puts++;
39 return AWS_OP_SUCCESS;
40 }
41
42 static int s_distribution_tester_check_results(const struct distribution_tester *tester) {
43 ASSERT_TRUE(tester->num_puts == DISTRIBUTION_PUT_COUNT);
44
45 double expected_numbers_per_bucket = (double)DISTRIBUTION_PUT_COUNT / DISTRIBUTION_BUCKET_COUNT;
46
47 uint64_t max_acceptable_numbers_per_bucket =
48 (uint64_t)ceil(expected_numbers_per_bucket * (1.0 + DISTRIBUTION_ACCEPTED_DEVIATION_RATIO));
49
50 uint64_t min_acceptable_numbers_per_bucket =
51 (uint64_t)floor(expected_numbers_per_bucket * (1.0 - DISTRIBUTION_ACCEPTED_DEVIATION_RATIO));
52
53 for (uint64_t i = 0; i < DISTRIBUTION_BUCKET_COUNT; ++i) {
54 uint64_t numbers_in_bucket = tester->buckets[i];
55 ASSERT_TRUE(numbers_in_bucket <= max_acceptable_numbers_per_bucket);
56 ASSERT_TRUE(numbers_in_bucket >= min_acceptable_numbers_per_bucket);
57 }
58
59 return AWS_OP_SUCCESS;
60 }
61
62 static int s_device_rand_u64_distribution_fn(struct aws_allocator *allocator, void *ctx) {
63 (void)allocator;
64 (void)ctx;
65 struct distribution_tester tester = {.max_value = UINT64_MAX};
66
67 for (size_t i = 0; i < DISTRIBUTION_PUT_COUNT; ++i) {
68 uint64_t next_value = 0;
69 ASSERT_SUCCESS(aws_device_random_u64(&next_value));
70 ASSERT_SUCCESS(s_distribution_tester_put(&tester, next_value));
71 }
72
73 ASSERT_SUCCESS(s_distribution_tester_check_results(&tester));
74 return AWS_OP_SUCCESS;
75 }
76
77 AWS_TEST_CASE(device_rand_u64_distribution, s_device_rand_u64_distribution_fn)
78
79 static int s_device_rand_u32_distribution_fn(struct aws_allocator *allocator, void *ctx) {
80 (void)allocator;
81 (void)ctx;
82 struct distribution_tester tester = {.max_value = UINT32_MAX};
83
84 for (size_t i = 0; i < DISTRIBUTION_PUT_COUNT; ++i) {
85 uint32_t next_value = 0;
86 ASSERT_SUCCESS(aws_device_random_u32(&next_value));
87 ASSERT_SUCCESS(s_distribution_tester_put(&tester, next_value));
88 }
89
90 ASSERT_SUCCESS(s_distribution_tester_check_results(&tester));
91 return AWS_OP_SUCCESS;
92 }
93
94 AWS_TEST_CASE(device_rand_u32_distribution, s_device_rand_u32_distribution_fn)
95
96 static int s_device_rand_u16_distribution_fn(struct aws_allocator *allocator, void *ctx) {
97 (void)allocator;
98 (void)ctx;
99 struct distribution_tester tester = {.max_value = UINT16_MAX};
100
101 for (size_t i = 0; i < DISTRIBUTION_PUT_COUNT; ++i) {
102 uint16_t next_value = 0;
103 ASSERT_SUCCESS(aws_device_random_u16(&next_value));
104 ASSERT_SUCCESS(s_distribution_tester_put(&tester, next_value));
105 }
106
107 ASSERT_SUCCESS(s_distribution_tester_check_results(&tester));
108 return AWS_OP_SUCCESS;
109 }
110
111 AWS_TEST_CASE(device_rand_u16_distribution, s_device_rand_u16_distribution_fn)
112
113 static int s_device_rand_buffer_distribution_fn(struct aws_allocator *allocator, void *ctx) {
114 (void)allocator;
115 (void)ctx;
116
117 uint8_t array[DISTRIBUTION_PUT_COUNT] = {0};
118 struct aws_byte_buf buf = aws_byte_buf_from_empty_array(array, sizeof(array));
119 ASSERT_SUCCESS(aws_device_random_buffer(&buf));
120
121 /* Test each byte in the buffer */
122 struct distribution_tester tester = {.max_value = UINT8_MAX};
123
124 for (size_t i = 0; i < DISTRIBUTION_PUT_COUNT; ++i) {
125 ASSERT_SUCCESS(s_distribution_tester_put(&tester, array[i]));
126 }
127
128 ASSERT_SUCCESS(s_distribution_tester_check_results(&tester));
129 return AWS_OP_SUCCESS;
130 }
131
132 AWS_TEST_CASE(device_rand_buffer_distribution, s_device_rand_buffer_distribution_fn)
133
134 static int s_device_rand_buffer_append_distribution_fn(struct aws_allocator *allocator, void *ctx) {
135 (void)allocator;
136 (void)ctx;
137
138 /* Create array full of zeroes, but only partially fill it with randomness */
139 uint8_t array[DISTRIBUTION_PUT_COUNT + 100] = {0};
140 struct aws_byte_buf buf = aws_byte_buf_from_empty_array(array, sizeof(array));
141 ASSERT_SUCCESS(aws_device_random_buffer_append(&buf, DISTRIBUTION_PUT_COUNT));
142
143 /* Test that first half of buffer has randomness */
144 struct distribution_tester tester = {.max_value = UINT8_MAX};
145
146 for (size_t i = 0; i < DISTRIBUTION_PUT_COUNT; ++i) {
147 ASSERT_SUCCESS(s_distribution_tester_put(&tester, array[i]));
148 }
149
150 ASSERT_SUCCESS(s_distribution_tester_check_results(&tester));
151
152 /* Test that second half of buffer is untouched (still full of zeroes) */
153 ASSERT_UINT_EQUALS(DISTRIBUTION_PUT_COUNT, buf.len);
154 ASSERT_UINT_EQUALS(sizeof(array), buf.capacity);
155 for (size_t i = buf.len; i < buf.capacity; ++i) {
156 ASSERT_UINT_EQUALS(0, buf.buffer[i]);
157 }
158
159 return AWS_OP_SUCCESS;
160 }
161
162 AWS_TEST_CASE(device_rand_buffer_append_distribution, s_device_rand_buffer_append_distribution_fn)
163
164 static int s_device_rand_buffer_append_short_buffer_fn(struct aws_allocator *allocator, void *ctx) {
165 (void)allocator;
166 (void)ctx;
167
168 uint8_t array[200] = {0};
169 struct aws_byte_buf buf = aws_byte_buf_from_empty_array(array, sizeof(array));
170 ASSERT_ERROR(AWS_ERROR_SHORT_BUFFER, aws_device_random_buffer_append(&buf, sizeof(array) + 1));
171 ASSERT_UINT_EQUALS(0, buf.len);
172
173 return AWS_OP_SUCCESS;
174 }
175
176 AWS_TEST_CASE(device_rand_buffer_append_short_buffer, s_device_rand_buffer_append_short_buffer_fn)