|
| 1 | +#include <stdio.h> |
| 2 | +#include <string.h> |
| 3 | +#include "unity.h" |
| 4 | +#include "esp_system.h" |
| 5 | + |
| 6 | +/* Note: these are just sanity tests, not the same as |
| 7 | + entropy tests |
| 8 | +*/ |
| 9 | + |
| 10 | +TEST_CASE("call esp_random()", "[random]") |
| 11 | +{ |
| 12 | + const size_t NUM_RANDOM = 128; /* in most cases this is massive overkill */ |
| 13 | + |
| 14 | + uint32_t zeroes = UINT32_MAX; |
| 15 | + uint32_t ones = 0; |
| 16 | + for (int i = 0; i < NUM_RANDOM - 1; i++) { |
| 17 | + uint32_t r = esp_random(); |
| 18 | + ones |= r; |
| 19 | + zeroes &= ~r; |
| 20 | + } |
| 21 | + |
| 22 | + /* assuming a 'white' random distribution, we can expect |
| 23 | + usually at least one time each bit will be zero and at |
| 24 | + least one time each will be one. Statistically this |
| 25 | + can still fail, just *very* unlikely to. */ |
| 26 | + TEST_ASSERT_EQUAL_HEX32(0, zeroes); |
| 27 | + TEST_ASSERT_EQUAL_HEX32(UINT32_MAX, ones); |
| 28 | +} |
| 29 | + |
| 30 | +TEST_CASE("call esp_fill_random()", "[random]") |
| 31 | +{ |
| 32 | + const size_t NUM_BUF = 200; |
| 33 | + const size_t BUF_SZ = 16; |
| 34 | + uint8_t buf[NUM_BUF][BUF_SZ]; |
| 35 | + uint8_t zero_buf[BUF_SZ]; |
| 36 | + uint8_t one_buf[BUF_SZ]; |
| 37 | + |
| 38 | + bzero(buf, sizeof(buf)); |
| 39 | + bzero(one_buf, sizeof(zero_buf)); |
| 40 | + memset(zero_buf, 0xFF, sizeof(one_buf)); |
| 41 | + |
| 42 | + for (int i = 0; i < NUM_BUF; i++) { |
| 43 | + esp_fill_random(buf[i], BUF_SZ); |
| 44 | + } |
| 45 | + /* No two 128-bit buffers should be the same |
| 46 | + (again, statistically this could happen but it's very unlikely) */ |
| 47 | + for (int i = 0; i < NUM_BUF; i++) { |
| 48 | + for (int j = 0; j < NUM_BUF; j++) { |
| 49 | + if (i != j) { |
| 50 | + TEST_ASSERT_NOT_EQUAL(0, memcmp(buf[i], buf[j], BUF_SZ)); |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + /* Do the same all bits are zero and one at least once test across the buffers */ |
| 56 | + for (int i = 0; i < NUM_BUF; i++) { |
| 57 | + for (int x = 0; x < BUF_SZ; x++) { |
| 58 | + zero_buf[x] &= ~buf[i][x]; |
| 59 | + one_buf[x] |= buf[i][x]; |
| 60 | + } |
| 61 | + } |
| 62 | + for (int x = 0; x < BUF_SZ; x++) { |
| 63 | + TEST_ASSERT_EQUAL_HEX8(0, zero_buf[x]); |
| 64 | + TEST_ASSERT_EQUAL_HEX8(0xFF, one_buf[x]); |
| 65 | + } |
| 66 | +} |
| 67 | + |
0 commit comments