xref: /dpdk/app/test/test_errno.c (revision e0a8442ccd15bafbb7eb150c35331c8e3b828c53)
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
test_errno(void)17 test_errno(void)
18 {
19 	const char *rte_retval;
20 	const char *libc_retval;
21 
22 #ifndef RTE_EXEC_ENV_WINDOWS
23 #ifdef RTE_EXEC_ENV_FREEBSD
24 	/* BSD has a colon in the string, unlike linux */
25 	const char unknown_code_result[] = "Unknown error: %d";
26 #else
27 	const char unknown_code_result[] = "Unknown error %d";
28 #endif
29 	char expected_libc_retval[sizeof(unknown_code_result) + 3];
30 #else
31 	/* Windows doesn't return error number for error greater than MAX_errno*/
32 	static const char expected_libc_retval[] = "Unknown error";
33 #endif
34 
35 	/* use a small selection of standard errors for testing */
36 	int std_errs[] = {EAGAIN, EBADF, EACCES, EINTR, EINVAL};
37 	/* test ALL registered RTE error codes for overlap */
38 	int rte_errs[] = {E_RTE_SECONDARY, E_RTE_NO_CONFIG};
39 	unsigned i;
40 
41 	rte_errno = 0;
42 	if (rte_errno != 0)
43 		return -1;
44 	/* check for standard errors we return the same as libc */
45 	for (i = 0; i < RTE_DIM(std_errs); i++) {
46 		rte_retval = rte_strerror(std_errs[i]);
47 		libc_retval = strerror(std_errs[i]);
48 		printf("rte_strerror: '%s', strerror: '%s'\n",
49 				rte_retval, libc_retval);
50 		if (strcmp(rte_retval, libc_retval) != 0)
51 			return -1;
52 	}
53 	/* for rte-specific errors ensure we return a different string
54 	 * and that the string for libc is for an unknown error
55 	 */
56 	for (i = 0; i < RTE_DIM(rte_errs); i++) {
57 		rte_retval = rte_strerror(rte_errs[i]);
58 		libc_retval = strerror(rte_errs[i]);
59 		printf("rte_strerror: '%s', strerror: '%s'\n",
60 				rte_retval, libc_retval);
61 		if (strcmp(rte_retval, libc_retval) == 0)
62 			return -1;
63 #ifndef RTE_EXEC_ENV_WINDOWS
64 		/* generate appropriate error string for unknown error number
65 		 * and then check that this is what we got back. If not, we have
66 		 * a duplicate error number that conflicts with errno.h */
67 		snprintf(expected_libc_retval, sizeof(expected_libc_retval),
68 				unknown_code_result, rte_errs[i]);
69 #endif
70 		if ((strcmp(expected_libc_retval, libc_retval) != 0) &&
71 				(strcmp("", libc_retval) != 0)){
72 			printf("Error, duplicate error code %d\n", rte_errs[i]);
73 			return -1;
74 		}
75 	}
76 
77 	/* ensure that beyond RTE_MAX_ERRNO, we always get an unknown code */
78 	rte_retval = rte_strerror(RTE_MAX_ERRNO + 1);
79 	libc_retval = strerror(RTE_MAX_ERRNO + 1);
80 #ifndef RTE_EXEC_ENV_WINDOWS
81 	snprintf(expected_libc_retval, sizeof(expected_libc_retval),
82 			unknown_code_result, RTE_MAX_ERRNO + 1);
83 #endif
84 	printf("rte_strerror: '%s', strerror: '%s'\n",
85 			rte_retval, libc_retval);
86 	if ((strcmp(rte_retval, libc_retval) != 0) ||
87 			(strcmp(expected_libc_retval, libc_retval) != 0)){
88 		if (strcmp("", libc_retval) != 0){
89 			printf("Failed test for RTE_MAX_ERRNO + 1 value\n");
90 			return -1;
91 		}
92 	}
93 
94 	return 0;
95 }
96 
97 REGISTER_FAST_TEST(errno_autotest, true, true, test_errno);
98