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