xref: /dpdk/app/test/test_errno.c (revision 68a03efeed657e6e05f281479b33b51102797e15)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4 
5 #include <stdint.h>
6 #include <stdio.h>
7 #include <stdarg.h>
8 #include <errno.h>
9 #include <string.h>
10 #include <rte_per_lcore.h>
11 #include <rte_errno.h>
12 #include <rte_string_fns.h>
13 
14 #include "test.h"
15 
16 static int
17 test_errno(void)
18 {
19 	const char *rte_retval;
20 	const char *libc_retval;
21 #ifdef RTE_EXEC_ENV_FREEBSD
22 	/* BSD has a colon in the string, unlike linux */
23 	const char unknown_code_result[] = "Unknown error: %d";
24 #else
25 	const char unknown_code_result[] = "Unknown error %d";
26 #endif
27 	char expected_libc_retval[sizeof(unknown_code_result)+3];
28 
29 	/* use a small selection of standard errors for testing */
30 	int std_errs[] = {EAGAIN, EBADF, EACCES, EINTR, EINVAL};
31 	/* test ALL registered RTE error codes for overlap */
32 	int rte_errs[] = {E_RTE_SECONDARY, E_RTE_NO_CONFIG};
33 	unsigned i;
34 
35 	rte_errno = 0;
36 	if (rte_errno != 0)
37 		return -1;
38 	/* check for standard errors we return the same as libc */
39 	for (i = 0; i < RTE_DIM(std_errs); i++) {
40 		rte_retval = rte_strerror(std_errs[i]);
41 		libc_retval = strerror(std_errs[i]);
42 		printf("rte_strerror: '%s', strerror: '%s'\n",
43 				rte_retval, libc_retval);
44 		if (strcmp(rte_retval, libc_retval) != 0)
45 			return -1;
46 	}
47 	/* for rte-specific errors ensure we return a different string
48 	 * and that the string for libc is for an unknown error
49 	 */
50 	for (i = 0; i < RTE_DIM(rte_errs); i++) {
51 		rte_retval = rte_strerror(rte_errs[i]);
52 		libc_retval = strerror(rte_errs[i]);
53 		printf("rte_strerror: '%s', strerror: '%s'\n",
54 				rte_retval, libc_retval);
55 		if (strcmp(rte_retval, libc_retval) == 0)
56 			return -1;
57 		/* generate appropriate error string for unknown error number
58 		 * and then check that this is what we got back. If not, we have
59 		 * a duplicate error number that conflicts with errno.h */
60 		snprintf(expected_libc_retval, sizeof(expected_libc_retval),
61 				unknown_code_result, rte_errs[i]);
62 		if ((strcmp(expected_libc_retval, libc_retval) != 0) &&
63 				(strcmp("", libc_retval) != 0)){
64 			printf("Error, duplicate error code %d\n", rte_errs[i]);
65 			return -1;
66 		}
67 	}
68 
69 	/* ensure that beyond RTE_MAX_ERRNO, we always get an unknown code */
70 	rte_retval = rte_strerror(RTE_MAX_ERRNO + 1);
71 	libc_retval = strerror(RTE_MAX_ERRNO + 1);
72 	snprintf(expected_libc_retval, sizeof(expected_libc_retval),
73 			unknown_code_result, RTE_MAX_ERRNO + 1);
74 	printf("rte_strerror: '%s', strerror: '%s'\n",
75 			rte_retval, libc_retval);
76 	if ((strcmp(rte_retval, libc_retval) != 0) ||
77 			(strcmp(expected_libc_retval, libc_retval) != 0)){
78 		if (strcmp("", libc_retval) != 0){
79 			printf("Failed test for RTE_MAX_ERRNO + 1 value\n");
80 			return -1;
81 		}
82 	}
83 
84 	return 0;
85 }
86 
87 REGISTER_TEST_COMMAND(errno_autotest, test_errno);
88