aboutsummaryrefslogtreecommitdiff
path: root/test/validation/api/init/init_main.c
blob: 64cefa30bcbfc475af4ddbb2f55aff9555ab34c8 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/* Copyright (c) 2015-2018, Linaro Limited
 * Copyright (c) 2019-2023, Nokia
 * All rights reserved.
 *
 * SPDX-License-Identifier:     BSD-3-Clause
 */

#include <odp_api.h>
#include <odp/helper/odph_api.h>
#include <odp_cunit_common.h>

#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

/* Replacement abort function */
static void ODP_NORETURN my_abort_func(void)
{
	abort();
}

/* Replacement log function */
ODP_PRINTF_FORMAT(2, 3)
static int my_log_func(odp_log_level_t level __attribute__((unused)),
		       const char *fmt, ...)
{
	va_list args;
	int r;

	va_start(args, fmt);
	r = vfprintf(stderr, fmt, args);
	va_end(args);

	return r;
}

static uint32_t my_log_thread_func_count;

/* Thread specific log function */
ODP_PRINTF_FORMAT(2, 3)
static int my_log_thread_func(odp_log_level_t level, const char *fmt, ...)
{
	(void)level;
	(void)fmt;

	my_log_thread_func_count++;

	return 0;
}

static void test_param_init(uint8_t fill)
{
	odp_init_t param;

	memset(&param, fill, sizeof(param));
	odp_init_param_init(&param);
	CU_ASSERT(param.mem_model == ODP_MEM_MODEL_THREAD);
	CU_ASSERT(param.shm.max_memory == 0);
}

static void init_test_param_init(void)
{
	test_param_init(0);
	test_param_init(0xff);
}

static void init_test_defaults(void)
{
	int ret;
	odp_instance_t instance;
	odp_instance_t current_instance;
	odp_init_t param;

	odp_init_param_init(&param);

	ret = odp_init_global(&instance, &param, NULL);
	CU_ASSERT_FATAL(ret == 0);

	ret = odp_init_local(instance, ODP_THREAD_WORKER);
	CU_ASSERT_FATAL(ret == 0);

	CU_ASSERT_FATAL(odp_instance(&current_instance) == 0);
	CU_ASSERT(memcmp(&current_instance, &instance, sizeof(odp_instance_t)) == 0);

	ret = odp_term_local();
	CU_ASSERT_FATAL(ret == 0);

	ret = odp_term_global(instance);
	CU_ASSERT(ret == 0);
}

static void init_test_abort(void)
{
	int ret;
	odp_instance_t instance;
	odp_init_t param;

	odp_init_param_init(&param);
	param.abort_fn = &my_abort_func;

	ret = odp_init_global(&instance, &param, NULL);
	CU_ASSERT_FATAL(ret == 0);

	ret = odp_init_local(instance, ODP_THREAD_WORKER);
	CU_ASSERT_FATAL(ret == 0);

	ret = odp_term_local();
	CU_ASSERT_FATAL(ret == 0);

	ret = odp_term_global(instance);
	CU_ASSERT(ret == 0);
}

static void init_test_log(void)
{
	int ret;
	odp_instance_t instance;
	odp_init_t param;

	odp_init_param_init(&param);
	param.log_fn = &my_log_func;

	ret = odp_init_global(&instance, &param, NULL);
	CU_ASSERT_FATAL(ret == 0);

	ret = odp_init_local(instance, ODP_THREAD_WORKER);
	CU_ASSERT_FATAL(ret == 0);

	ret = odp_term_local();
	CU_ASSERT_FATAL(ret == 0);

	ret = odp_term_global(instance);
	CU_ASSERT(ret == 0);
}

static void init_test_log_thread(void)
{
	int ret;
	odp_instance_t instance;
	odp_init_t param;

	odp_init_param_init(&param);

	ret = odp_init_global(&instance, &param, NULL);
	CU_ASSERT_FATAL(ret == 0);

	ret = odp_init_local(instance, ODP_THREAD_WORKER);
	CU_ASSERT_FATAL(ret == 0);

	/* Test that our print function is called when set. */
	odp_log_thread_fn_set(my_log_thread_func);
	my_log_thread_func_count = 0;
	odp_sys_info_print();
	CU_ASSERT(my_log_thread_func_count != 0);

	/* Test that our print function is not called when not set. */
	odp_log_thread_fn_set(NULL);
	my_log_thread_func_count = 0;
	odp_sys_info_print();
	CU_ASSERT(my_log_thread_func_count == 0);

	ret = odp_term_local();
	CU_ASSERT_FATAL(ret == 0);

	ret = odp_term_global(instance);
	CU_ASSERT(ret == 0);
}

static void init_test_num_thr(void)
{
	int ret;
	odp_instance_t instance;
	odp_init_t param;

	odp_init_param_init(&param);
	param.mem_model    = ODP_MEM_MODEL_THREAD;
	param.num_worker   = 1;
	param.num_control  = 1;
	param.worker_cpus  = NULL;
	param.control_cpus = NULL;

	ret = odp_init_global(&instance, &param, NULL);
	CU_ASSERT_FATAL(ret == 0);

	ret = odp_init_local(instance, ODP_THREAD_WORKER);
	CU_ASSERT_FATAL(ret == 0);

	ret = odp_term_local();
	CU_ASSERT_FATAL(ret == 0);

	ret = odp_term_global(instance);
	CU_ASSERT(ret == 0);
}

static void init_test_feature(int disable)
{
	int ret;
	odp_instance_t instance;
	odp_init_t param;

	odp_init_param_init(&param);
	param.not_used.all_feat = 0;

	if (disable) {
		param.not_used.feat.cls      = 1;
		param.not_used.feat.compress = 1;
		param.not_used.feat.crypto   = 1;
		param.not_used.feat.ipsec    = 1;
		param.not_used.feat.schedule = 1;
		param.not_used.feat.stash    = 1;
		param.not_used.feat.time     = 1;
		param.not_used.feat.timer    = 1;
		param.not_used.feat.tm       = 1;
	}

	ret = odp_init_global(&instance, &param, NULL);
	CU_ASSERT_FATAL(ret == 0);

	ret = odp_init_local(instance, ODP_THREAD_CONTROL);
	CU_ASSERT_FATAL(ret == 0);

	/* Print system and SHM information into test log. It may show
	 * e.g. memory usage difference when features are disabled. */
	odp_sys_info_print();
	odp_shm_print_all();

	ret = odp_term_local();
	CU_ASSERT_FATAL(ret == 0);

	ret = odp_term_global(instance);
	CU_ASSERT(ret == 0);
}

static void init_test_feature_enabled(void)
{
	init_test_feature(0);
}

static void init_test_feature_disabled(void)
{
	init_test_feature(1);
}

static void init_test_term_abnormal(void)
{
	int ret;
	odp_instance_t instance;

	ret = odp_init_global(&instance, NULL, NULL);
	CU_ASSERT_FATAL(ret == 0);

	ret = odp_init_local(instance, ODP_THREAD_WORKER);
	CU_ASSERT_FATAL(ret == 0);

	/* odp_term_abnormal() is allowed to fail */
	ret = odp_term_abnormal(instance, 0, NULL);

	if (ret < 0)
		ODPH_ERR("Failed to perform all abnormal termination actions: %d\n", ret);
}

odp_testinfo_t testinfo[] = {
	ODP_TEST_INFO(init_test_defaults),
	ODP_TEST_INFO(init_test_abort),
	ODP_TEST_INFO(init_test_log),
	ODP_TEST_INFO(init_test_num_thr),
	ODP_TEST_INFO(init_test_feature_enabled),
	ODP_TEST_INFO(init_test_feature_disabled),
	ODP_TEST_INFO(init_test_log_thread),
	ODP_TEST_INFO(init_test_param_init),
	ODP_TEST_INFO(init_test_term_abnormal)
};

odp_testinfo_t init_suite[] = {
	ODP_TEST_INFO_NULL,
	ODP_TEST_INFO_NULL
};

odp_suiteinfo_t init_suites[] = {
	{"Init", NULL, NULL, init_suite},
	ODP_SUITE_INFO_NULL,
};

static int fill_testinfo(odp_testinfo_t *info, unsigned int test_case)
{
	if (test_case >= ODPH_ARRAY_SIZE(testinfo)) {
		ODPH_ERR("Bad test case number %u\n", test_case);
		return -1;
	}

	*info = testinfo[test_case];

	return 0;
}

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

	if (fill_testinfo(&init_suite[0], INIT_TEST))
		return -1;

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

	/* Prevent default ODP init */
	odp_cunit_register_global_init(NULL);
	odp_cunit_register_global_term(NULL);

	/* Register the tests */
	ret = odp_cunit_register(init_suites);

	/* Run the tests */
	if (ret == 0)
		ret = odp_cunit_run();

	return ret;
}