1 /*
2 * Copyright (c) 2024 Google LLC
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/posix/unistd.h>
8 #include <zephyr/ztest.h>
9
ZTEST(posix_c_lib_ext,test_getentropy_too_large)10 ZTEST(posix_c_lib_ext, test_getentropy_too_large)
11 {
12 uint8_t buf[256 + 1] = {0};
13 int ret;
14
15 ret = getentropy(buf, sizeof(buf));
16 zassert_equal(ret, -1);
17 zassert_equal(errno, EIO);
18 }
19
ZTEST(posix_c_lib_ext,test_getentropy_null_buffer)20 ZTEST(posix_c_lib_ext, test_getentropy_null_buffer)
21 {
22 int ret;
23
24 ret = getentropy(NULL, 0);
25 zassert_equal(ret, -1);
26 zassert_equal(errno, EFAULT);
27 }
28
ZTEST(posix_c_lib_ext,test_getentropy_max_size)29 ZTEST(posix_c_lib_ext, test_getentropy_max_size)
30 {
31 uint8_t buf[256] = {0};
32 int ret;
33
34 ret = getentropy(buf, sizeof(buf));
35 zassert_equal(ret, 0);
36 }
37
ZTEST(posix_c_lib_ext,test_getentropy)38 ZTEST(posix_c_lib_ext, test_getentropy)
39 {
40 uint8_t zero[16] = {0};
41 uint8_t buf1[16];
42 uint8_t buf2[16];
43 int ret;
44
45 ret = getentropy(buf1, sizeof(buf1));
46 zassert_equal(ret, 0);
47
48 ret = getentropy(buf2, sizeof(buf2));
49 zassert_equal(ret, 0);
50
51 zassert_true(memcmp(buf1, zero, sizeof(zero)) != 0);
52 zassert_true(memcmp(buf2, zero, sizeof(zero)) != 0);
53 zassert_true(memcmp(buf1, buf2, sizeof(buf1)) != 0);
54 }
55