15129044dSC.J. Collier/*-
25129044dSC.J. Collier *   BSD LICENSE
35129044dSC.J. Collier *
45129044dSC.J. Collier *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
55129044dSC.J. Collier *   All rights reserved.
65129044dSC.J. Collier *
75129044dSC.J. Collier *   Redistribution and use in source and binary forms, with or without
85129044dSC.J. Collier *   modification, are permitted provided that the following conditions
95129044dSC.J. Collier *   are met:
105129044dSC.J. Collier *
115129044dSC.J. Collier *     * Redistributions of source code must retain the above copyright
125129044dSC.J. Collier *       notice, this list of conditions and the following disclaimer.
135129044dSC.J. Collier *     * Redistributions in binary form must reproduce the above copyright
145129044dSC.J. Collier *       notice, this list of conditions and the following disclaimer in
155129044dSC.J. Collier *       the documentation and/or other materials provided with the
165129044dSC.J. Collier *       distribution.
175129044dSC.J. Collier *     * Neither the name of Intel Corporation nor the names of its
185129044dSC.J. Collier *       contributors may be used to endorse or promote products derived
195129044dSC.J. Collier *       from this software without specific prior written permission.
205129044dSC.J. Collier *
215129044dSC.J. Collier *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
225129044dSC.J. Collier *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
235129044dSC.J. Collier *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
245129044dSC.J. Collier *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
255129044dSC.J. Collier *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
265129044dSC.J. Collier *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
275129044dSC.J. Collier *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
285129044dSC.J. Collier *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
295129044dSC.J. Collier *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
305129044dSC.J. Collier *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
315129044dSC.J. Collier *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
325129044dSC.J. Collier */
335129044dSC.J. Collier
345129044dSC.J. Collier#include <stdint.h>
355129044dSC.J. Collier#include <stdio.h>
365129044dSC.J. Collier#include <stdarg.h>
375129044dSC.J. Collier#include <errno.h>
385129044dSC.J. Collier#include <string.h>
395129044dSC.J. Collier#include <rte_per_lcore.h>
405129044dSC.J. Collier#include <rte_errno.h>
415129044dSC.J. Collier#include <rte_string_fns.h>
425129044dSC.J. Collier
435129044dSC.J. Collier#include "test.h"
445129044dSC.J. Collier
455129044dSC.J. Collierstatic int
465129044dSC.J. Colliertest_errno(void)
475129044dSC.J. Collier{
485129044dSC.J. Collier	const char *rte_retval;
495129044dSC.J. Collier	const char *libc_retval;
505129044dSC.J. Collier#ifdef RTE_EXEC_ENV_BSDAPP
515129044dSC.J. Collier	/* BSD has a colon in the string, unlike linux */
525129044dSC.J. Collier	const char unknown_code_result[] = "Unknown error: %d";
535129044dSC.J. Collier#else
545129044dSC.J. Collier	const char unknown_code_result[] = "Unknown error %d";
555129044dSC.J. Collier#endif
565129044dSC.J. Collier	char expected_libc_retval[sizeof(unknown_code_result)+3];
575129044dSC.J. Collier
585129044dSC.J. Collier	/* use a small selection of standard errors for testing */
595129044dSC.J. Collier	int std_errs[] = {EAGAIN, EBADF, EACCES, EINTR, EINVAL};
605129044dSC.J. Collier	/* test ALL registered RTE error codes for overlap */
615129044dSC.J. Collier	int rte_errs[] = {E_RTE_SECONDARY, E_RTE_NO_CONFIG};
625129044dSC.J. Collier	unsigned i;
635129044dSC.J. Collier
645129044dSC.J. Collier	rte_errno = 0;
655129044dSC.J. Collier	if (rte_errno != 0)
665129044dSC.J. Collier		return -1;
675129044dSC.J. Collier	/* check for standard errors we return the same as libc */
685129044dSC.J. Collier	for (i = 0; i < sizeof(std_errs)/sizeof(std_errs[0]); i++){
695129044dSC.J. Collier		rte_retval = rte_strerror(std_errs[i]);
705129044dSC.J. Collier		libc_retval = strerror(std_errs[i]);
715129044dSC.J. Collier		printf("rte_strerror: '%s', strerror: '%s'\n",
725129044dSC.J. Collier				rte_retval, libc_retval);
735129044dSC.J. Collier		if (strcmp(rte_retval, libc_retval) != 0)
745129044dSC.J. Collier			return -1;
755129044dSC.J. Collier	}
765129044dSC.J. Collier	/* for rte-specific errors ensure we return a different string
775129044dSC.J. Collier	 * and that the string for libc is for an unknown error
785129044dSC.J. Collier	 */
795129044dSC.J. Collier	for (i = 0; i < sizeof(rte_errs)/sizeof(rte_errs[0]); i++){
805129044dSC.J. Collier		rte_retval = rte_strerror(rte_errs[i]);
815129044dSC.J. Collier		libc_retval = strerror(rte_errs[i]);
825129044dSC.J. Collier		printf("rte_strerror: '%s', strerror: '%s'\n",
835129044dSC.J. Collier				rte_retval, libc_retval);
845129044dSC.J. Collier		if (strcmp(rte_retval, libc_retval) == 0)
855129044dSC.J. Collier			return -1;
865129044dSC.J. Collier		/* generate appropriate error string for unknown error number
875129044dSC.J. Collier		 * and then check that this is what we got back. If not, we have
885129044dSC.J. Collier		 * a duplicate error number that conflicts with errno.h */
895129044dSC.J. Collier		snprintf(expected_libc_retval, sizeof(expected_libc_retval),
905129044dSC.J. Collier				unknown_code_result, rte_errs[i]);
915129044dSC.J. Collier		if ((strcmp(expected_libc_retval, libc_retval) != 0) &&
925129044dSC.J. Collier				(strcmp("", libc_retval) != 0)){
935129044dSC.J. Collier			printf("Error, duplicate error code %d\n", rte_errs[i]);
945129044dSC.J. Collier			return -1;
955129044dSC.J. Collier		}
965129044dSC.J. Collier	}
975129044dSC.J. Collier
985129044dSC.J. Collier	/* ensure that beyond RTE_MAX_ERRNO, we always get an unknown code */
995129044dSC.J. Collier	rte_retval = rte_strerror(RTE_MAX_ERRNO + 1);
1005129044dSC.J. Collier	libc_retval = strerror(RTE_MAX_ERRNO + 1);
1015129044dSC.J. Collier	snprintf(expected_libc_retval, sizeof(expected_libc_retval),
1025129044dSC.J. Collier			unknown_code_result, RTE_MAX_ERRNO + 1);
1035129044dSC.J. Collier	printf("rte_strerror: '%s', strerror: '%s'\n",
1045129044dSC.J. Collier			rte_retval, libc_retval);
1055129044dSC.J. Collier	if ((strcmp(rte_retval, libc_retval) != 0) ||
1065129044dSC.J. Collier			(strcmp(expected_libc_retval, libc_retval) != 0)){
1075129044dSC.J. Collier		if (strcmp("", libc_retval) != 0){
1085129044dSC.J. Collier			printf("Failed test for RTE_MAX_ERRNO + 1 value\n");
1095129044dSC.J. Collier			return -1;
1105129044dSC.J. Collier		}
1115129044dSC.J. Collier	}
1125129044dSC.J. Collier
1135129044dSC.J. Collier	return 0;
1145129044dSC.J. Collier}
1155129044dSC.J. Collier
1169ecc306dSRicardo SalvetiREGISTER_TEST_COMMAND(errno_autotest, test_errno);
117