]> arthur.barton.de Git - ngircd-alex.git/blob - src/portab/portabtest.c
Support non-standard vsnprintf() return code
[ngircd-alex.git] / src / portab / portabtest.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2013 Alexander Barton (alex@barton.de) and Contributors.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * Please read the file COPYING, README and AUTHORS for more information.
10  */
11
12 #include "portab.h"
13
14 /**
15  * @file
16  * Test program for portab.h and friends ;-)
17  */
18
19 #include "imp.h"
20 #include <stdarg.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "exp.h"
26
27 static void
28 Panic(char *Reason)
29 {
30         /* Oops, something failed!? */
31         fprintf(stderr, "Oops, test for %s failed!?\n", Reason);
32         exit(1);
33 } /* Panic */
34
35 static void
36 Check_snprintf(void)
37 {
38         char str[5];
39
40         snprintf(str, sizeof(str), "%s", "1234567890");
41         if (str[4] != '\0')
42                 Panic("snprintf NULL byte");
43         if (strlen(str) != 4)
44                 Panic("snprintf string length");
45 }
46
47 static void
48 Check_strdup(void)
49 {
50         char *ptr;
51
52         ptr = strdup("1234567890");
53         if (!ptr)
54                 Panic("strdup");
55         if (ptr[10] != '\0')
56                 Panic("strdup NULL byte");
57         if (strlen(ptr) != 10)
58                 Panic("strdup string length");
59         free(ptr);
60 }
61
62 static void
63 Check_strndup(void)
64 {
65         char *ptr;
66
67         ptr = strndup("1234567890", 5);
68         if (!ptr)
69                 Panic("strndup");
70         if (ptr[5] != '\0')
71                 Panic("strndup NULL byte");
72         if (strlen(ptr) != 5)
73                 Panic("strndup string length");
74         free(ptr);
75 }
76
77 static void
78 Check_strlcpy(void)
79 {
80         char str[5];
81
82         if (strlcpy(str, "1234567890", sizeof(str)) != 10)
83                 Panic("strlcpy return code");
84         if (str[4] != '\0')
85                 Panic("strlcpy NULL byte");
86         if (strlen(str) != 4)
87                 Panic("strlcpy string length");
88 }
89
90 static void
91 Check_strlcat(void)
92 {
93         char str[5];
94
95         if (strlcpy(str, "12", sizeof(str)) != 2)
96                 Panic("strlcpy for strlcat");
97         if (strlcat(str, "1234567890", sizeof(str)) != 12)
98                 Panic("strlcat return code");
99         if (str[4] != '\0')
100                 Panic("strlcat NULL byte");
101         if (strlen(str) != 4)
102                 Panic("strlcat string length");
103 }
104
105 static void
106 Check_strtok_r(void)
107 {
108         char *ptr, *last;
109
110         ptr = strdup("12,abc");
111
112         ptr = strtok_r(ptr, ",", &last);
113         if (!ptr)
114                 Panic("strtok_r result #1");
115         if (strcmp(ptr, "12") != 0)
116                 Panic("strtok_r token #1");
117
118         ptr = strtok_r(NULL, ",", &last);
119         if (!ptr)
120                 Panic("strtok_r result #2");
121         if (strcmp(ptr, "abc") != 0)
122                 Panic("strtok_r token #2");
123
124         ptr = strtok_r(NULL, ",", &last);
125         if (ptr)
126                 Panic("strtok_r result #3");
127 }
128
129 #ifdef PROTOTYPES
130 static void
131 Check_vsnprintf(const int Len, const char *Format, ...)
132 #else
133 static void
134 Check_vsnprintf(Len, Format, va_alist)
135 const int Len;
136 const char *Format;
137 va_dcl
138 #endif
139 {
140         char str[5];
141         va_list ap;
142         int r;
143
144 #ifdef PROTOTYPES
145         va_start(ap, Format);
146 #else
147         va_start(ap);
148 #endif
149         r = vsnprintf(str, sizeof(str), Format, ap);
150         va_end(ap);
151         if (r != Len) {
152                 /* C99 states that vsnprintf() "returns the number of
153                  * characters that would have been printed if the n were
154                  * unlimited", but according to the Linux manual page "glibc
155                  * until 2.0.6 would return -1 when the output was truncated",
156                  * and other implementations (libUTIL on A/UX) even return the
157                  * number of characters processed ... so we only test our own
158                  * implementation and warn on errors otherwise :-/ */
159 #ifdef HAVE_VSNPRINTF
160                 fprintf(stderr,
161                         "\n ** WARNING: The vsnprintf() function of this system isn't standard\n");
162                 fprintf(stderr,
163                         " ** conformant and returns a WRONG result: %d (should be %d)! The test\n",
164                         r, Len);
165                 fprintf(stderr,
166                         " ** result has been ignored but may lead to errors during execution!\n\n");
167 #else
168                 Panic("vsnprintf return code");
169 #endif
170         }
171         if (str[4] != '\0')
172                 Panic("vsnprintf NULL byte");
173         if (strlen(str) != 4)
174                 Panic("vsnprintf string length");
175 }
176
177 GLOBAL int
178 main(void)
179 {
180         /* validate datatypes */
181         if (false != 0)
182                 Panic("false");
183         if (true != 1)
184                 Panic("true");
185         if (sizeof(UINT8) != 1)
186                 Panic("UINT8");
187         if (sizeof(UINT16) != 2)
188                 Panic("UINT16");
189         if (sizeof(UINT32) != 4)
190                 Panic("UINT32");
191
192         /* check functions */
193         Check_snprintf();
194         Check_strdup();
195         Check_strndup();
196         Check_strlcpy();
197         Check_strlcat();
198         Check_strtok_r();
199         Check_vsnprintf(2+10, "%s%s", "ab", "1234567890");
200         
201         return 0;
202 }
203
204 /* -eof- */