first public release
[dpdk.git] / app / test / test_errno.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without 
8  *   modification, are permitted provided that the following conditions 
9  *   are met:
10  * 
11  *     * Redistributions of source code must retain the above copyright 
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright 
14  *       notice, this list of conditions and the following disclaimer in 
15  *       the documentation and/or other materials provided with the 
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its 
18  *       contributors may be used to endorse or promote products derived 
19  *       from this software without specific prior written permission.
20  * 
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  * 
33  *  version: DPDK.L.1.2.3-3
34  */
35
36 #include <stdint.h>
37 #include <stdio.h>
38 #include <stdarg.h>
39 #include <errno.h>
40 #include <string.h>
41 #include <rte_per_lcore.h>
42 #include <rte_errno.h>
43 #include <rte_string_fns.h>
44
45 #include <cmdline_parse.h>
46
47 #include "test.h"
48
49 int
50 test_errno(void)
51 {
52         const char *rte_retval;
53         const char *libc_retval;
54         const char unknown_code_result[] = "Unknown error %d";
55         char expected_libc_retval[sizeof(unknown_code_result)+3];
56
57         /* use a small selection of standard errors for testing */
58         int std_errs[] = {EAGAIN, EBADF, EACCES, EINTR, EINVAL};
59         /* test ALL registered RTE error codes for overlap */
60         int rte_errs[] = {E_RTE_SECONDARY, E_RTE_NO_CONFIG, E_RTE_NO_TAILQ};
61         unsigned i;
62
63         rte_errno = 0;
64         if (rte_errno != 0)
65                 return -1;
66         /* check for standard errors we return the same as libc */
67         for (i = 0; i < sizeof(std_errs)/sizeof(std_errs[0]); i++){
68                 rte_retval = rte_strerror(std_errs[i]);
69                 libc_retval = strerror(std_errs[i]);
70                 printf("rte_strerror: '%s', strerror: '%s'\n",
71                                 rte_retval, libc_retval);
72                 if (strcmp(rte_retval, libc_retval) != 0)
73                         return -1;
74         }
75         /* for rte-specific errors ensure we return a different string
76          * and that the string for libc is for an unknown error
77          */
78         for (i = 0; i < sizeof(rte_errs)/sizeof(rte_errs[0]); i++){
79                 rte_retval = rte_strerror(rte_errs[i]);
80                 libc_retval = strerror(rte_errs[i]);
81                 printf("rte_strerror: '%s', strerror: '%s'\n",
82                                 rte_retval, libc_retval);
83                 if (strcmp(rte_retval, libc_retval) == 0)
84                         return -1;
85                 /* generate appropriate error string for unknown error number
86                  * and then check that this is what we got back. If not, we have
87                  * a duplicate error number that conflicts with errno.h */
88                 rte_snprintf(expected_libc_retval, sizeof(expected_libc_retval),
89                                 unknown_code_result, rte_errs[i]);
90                 if (strcmp(expected_libc_retval, libc_retval) != 0){
91                         printf("Error, duplicate error code %d\n", rte_errs[i]);
92                         return -1;
93                 }
94         }
95
96         /* ensure that beyond RTE_MAX_ERRNO, we always get an unknown code */
97         rte_retval = rte_strerror(RTE_MAX_ERRNO + 1);
98         libc_retval = strerror(RTE_MAX_ERRNO + 1);
99         rte_snprintf(expected_libc_retval, sizeof(expected_libc_retval),
100                         unknown_code_result, RTE_MAX_ERRNO + 1);
101         printf("rte_strerror: '%s', strerror: '%s'\n",
102                         rte_retval, libc_retval);
103         if ((strcmp(rte_retval, libc_retval) != 0) ||
104                         (strcmp(expected_libc_retval, libc_retval) != 0)){
105                 printf("Failed test for RTE_MAX_ERRNO + 1 value\n");
106                 return -1;
107         }
108
109         return 0;
110 }