aboutsummaryrefslogtreecommitdiff
path: root/test/common_plat/validation/api/random/random.c
blob: a0e2ef72ffa29dc2483b4e5e47aa7ae27126f849 (plain)
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
/* Copyright (c) 2015, Linaro Limited
 * All rights reserved.
 *
 * SPDX-License-Identifier:     BSD-3-Clause
 */

#include <odp_api.h>
#include <odp_cunit_common.h>
#include "random.h"

void random_test_get_size(void)
{
	int32_t ret;
	uint8_t buf[32];

	ret = odp_random_data(buf, sizeof(buf), ODP_RANDOM_BASIC);
	CU_ASSERT(ret == sizeof(buf));
}

void random_test_kind(void)
{
	int32_t rc;
	uint8_t buf[4096];
	uint32_t buf_size = sizeof(buf);
	odp_random_kind_t max_kind = odp_random_max_kind();

	rc = odp_random_data(buf, buf_size, max_kind);
	CU_ASSERT(rc > 0);

	switch (max_kind) {
	case ODP_RANDOM_BASIC:
		rc = odp_random_data(buf, 4, ODP_RANDOM_CRYPTO);
		CU_ASSERT(rc < 0);
		/* Fall through */

	case ODP_RANDOM_CRYPTO:
		rc = odp_random_data(buf, 4, ODP_RANDOM_TRUE);
		CU_ASSERT(rc < 0);
		break;

	default:
		break;
	}
}

void random_test_repeat(void)
{
	uint8_t buf1[1024];
	uint8_t buf2[1024];
	int32_t rc;
	uint64_t seed1 = 12345897;
	uint64_t seed2 = seed1;

	rc = odp_random_test_data(buf1, sizeof(buf1), &seed1);
	CU_ASSERT(rc == sizeof(buf1));

	rc = odp_random_test_data(buf2, sizeof(buf2), &seed2);
	CU_ASSERT(rc == sizeof(buf2));

	CU_ASSERT(seed1 == seed2);
	CU_ASSERT(memcmp(buf1, buf2, sizeof(buf1)) == 0);
}

odp_testinfo_t random_suite[] = {
	ODP_TEST_INFO(random_test_get_size),
	ODP_TEST_INFO(random_test_kind),
	ODP_TEST_INFO(random_test_repeat),
	ODP_TEST_INFO_NULL,
};

odp_suiteinfo_t random_suites[] = {
	{"Random", NULL, NULL, random_suite},
	ODP_SUITE_INFO_NULL,
};

int random_main(int argc, char *argv[])
{
	int ret;

	/* parse common options: */
	if (odp_cunit_parse_options(argc, argv))
		return -1;

	ret = odp_cunit_register(random_suites);

	if (ret == 0)
		ret = odp_cunit_run();

	return ret;
}