xref: /f-stack/dpdk/app/test/test_errno.c (revision 2d9fd380)
14418919fSjohnjiang /* SPDX-License-Identifier: BSD-3-Clause
24418919fSjohnjiang  * Copyright(c) 2010-2014 Intel Corporation
34418919fSjohnjiang  */
44418919fSjohnjiang 
54418919fSjohnjiang #include <stdint.h>
64418919fSjohnjiang #include <stdio.h>
74418919fSjohnjiang #include <stdarg.h>
84418919fSjohnjiang #include <errno.h>
94418919fSjohnjiang #include <string.h>
104418919fSjohnjiang #include <rte_per_lcore.h>
114418919fSjohnjiang #include <rte_errno.h>
124418919fSjohnjiang #include <rte_string_fns.h>
134418919fSjohnjiang 
144418919fSjohnjiang #include "test.h"
154418919fSjohnjiang 
164418919fSjohnjiang static int
test_errno(void)174418919fSjohnjiang test_errno(void)
184418919fSjohnjiang {
194418919fSjohnjiang 	const char *rte_retval;
204418919fSjohnjiang 	const char *libc_retval;
214418919fSjohnjiang #ifdef RTE_EXEC_ENV_FREEBSD
224418919fSjohnjiang 	/* BSD has a colon in the string, unlike linux */
234418919fSjohnjiang 	const char unknown_code_result[] = "Unknown error: %d";
244418919fSjohnjiang #else
254418919fSjohnjiang 	const char unknown_code_result[] = "Unknown error %d";
264418919fSjohnjiang #endif
274418919fSjohnjiang 	char expected_libc_retval[sizeof(unknown_code_result)+3];
284418919fSjohnjiang 
294418919fSjohnjiang 	/* use a small selection of standard errors for testing */
304418919fSjohnjiang 	int std_errs[] = {EAGAIN, EBADF, EACCES, EINTR, EINVAL};
314418919fSjohnjiang 	/* test ALL registered RTE error codes for overlap */
324418919fSjohnjiang 	int rte_errs[] = {E_RTE_SECONDARY, E_RTE_NO_CONFIG};
334418919fSjohnjiang 	unsigned i;
344418919fSjohnjiang 
354418919fSjohnjiang 	rte_errno = 0;
364418919fSjohnjiang 	if (rte_errno != 0)
374418919fSjohnjiang 		return -1;
384418919fSjohnjiang 	/* check for standard errors we return the same as libc */
39*2d9fd380Sjfb8856606 	for (i = 0; i < RTE_DIM(std_errs); i++) {
404418919fSjohnjiang 		rte_retval = rte_strerror(std_errs[i]);
414418919fSjohnjiang 		libc_retval = strerror(std_errs[i]);
424418919fSjohnjiang 		printf("rte_strerror: '%s', strerror: '%s'\n",
434418919fSjohnjiang 				rte_retval, libc_retval);
444418919fSjohnjiang 		if (strcmp(rte_retval, libc_retval) != 0)
454418919fSjohnjiang 			return -1;
464418919fSjohnjiang 	}
474418919fSjohnjiang 	/* for rte-specific errors ensure we return a different string
484418919fSjohnjiang 	 * and that the string for libc is for an unknown error
494418919fSjohnjiang 	 */
50*2d9fd380Sjfb8856606 	for (i = 0; i < RTE_DIM(rte_errs); i++) {
514418919fSjohnjiang 		rte_retval = rte_strerror(rte_errs[i]);
524418919fSjohnjiang 		libc_retval = strerror(rte_errs[i]);
534418919fSjohnjiang 		printf("rte_strerror: '%s', strerror: '%s'\n",
544418919fSjohnjiang 				rte_retval, libc_retval);
554418919fSjohnjiang 		if (strcmp(rte_retval, libc_retval) == 0)
564418919fSjohnjiang 			return -1;
574418919fSjohnjiang 		/* generate appropriate error string for unknown error number
584418919fSjohnjiang 		 * and then check that this is what we got back. If not, we have
594418919fSjohnjiang 		 * a duplicate error number that conflicts with errno.h */
604418919fSjohnjiang 		snprintf(expected_libc_retval, sizeof(expected_libc_retval),
614418919fSjohnjiang 				unknown_code_result, rte_errs[i]);
624418919fSjohnjiang 		if ((strcmp(expected_libc_retval, libc_retval) != 0) &&
634418919fSjohnjiang 				(strcmp("", libc_retval) != 0)){
644418919fSjohnjiang 			printf("Error, duplicate error code %d\n", rte_errs[i]);
654418919fSjohnjiang 			return -1;
664418919fSjohnjiang 		}
674418919fSjohnjiang 	}
684418919fSjohnjiang 
694418919fSjohnjiang 	/* ensure that beyond RTE_MAX_ERRNO, we always get an unknown code */
704418919fSjohnjiang 	rte_retval = rte_strerror(RTE_MAX_ERRNO + 1);
714418919fSjohnjiang 	libc_retval = strerror(RTE_MAX_ERRNO + 1);
724418919fSjohnjiang 	snprintf(expected_libc_retval, sizeof(expected_libc_retval),
734418919fSjohnjiang 			unknown_code_result, RTE_MAX_ERRNO + 1);
744418919fSjohnjiang 	printf("rte_strerror: '%s', strerror: '%s'\n",
754418919fSjohnjiang 			rte_retval, libc_retval);
764418919fSjohnjiang 	if ((strcmp(rte_retval, libc_retval) != 0) ||
774418919fSjohnjiang 			(strcmp(expected_libc_retval, libc_retval) != 0)){
784418919fSjohnjiang 		if (strcmp("", libc_retval) != 0){
794418919fSjohnjiang 			printf("Failed test for RTE_MAX_ERRNO + 1 value\n");
804418919fSjohnjiang 			return -1;
814418919fSjohnjiang 		}
824418919fSjohnjiang 	}
834418919fSjohnjiang 
844418919fSjohnjiang 	return 0;
854418919fSjohnjiang }
864418919fSjohnjiang 
874418919fSjohnjiang REGISTER_TEST_COMMAND(errno_autotest, test_errno);
88