msvcrt: Implement _set_errno.
[wine] / dlls / msvcrt / tests / misc.c
1 /*
2  * Unit tests for miscellaneous msvcrt functions
3  *
4  * Copyright 2010 Andrew Nguyen
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "wine/test.h"
22 #include <errno.h>
23 #include "msvcrt.h"
24
25 static int (__cdecl *prand_s)(unsigned int *);
26 static int (__cdecl *pmemcpy_s)(void *, MSVCRT_size_t, void*, MSVCRT_size_t);
27 static int (__cdecl *pI10_OUTPUT)(long double, int, int, void*);
28 static int (__cdecl *pstrerror_s)(char *, MSVCRT_size_t, int);
29 static int (__cdecl *p_get_doserrno)(int *);
30 static int (__cdecl *p_get_errno)(int *);
31 static int (__cdecl *p_set_errno)(int);
32
33 static void init(void)
34 {
35     HMODULE hmod = GetModuleHandleA("msvcrt.dll");
36
37     prand_s = (void *)GetProcAddress(hmod, "rand_s");
38     pmemcpy_s = (void*)GetProcAddress(hmod, "memcpy_s");
39     pI10_OUTPUT = (void*)GetProcAddress(hmod, "$I10_OUTPUT");
40     pstrerror_s = (void *)GetProcAddress(hmod, "strerror_s");
41     p_get_doserrno = (void *)GetProcAddress(hmod, "_get_doserrno");
42     p_get_errno = (void *)GetProcAddress(hmod, "_get_errno");
43     p_set_errno = (void *)GetProcAddress(hmod, "_set_errno");
44 }
45
46 static void test_rand_s(void)
47 {
48     int ret;
49     unsigned int rand;
50
51     if (!prand_s)
52     {
53         win_skip("rand_s is not available\n");
54         return;
55     }
56
57     errno = EBADF;
58     ret = prand_s(NULL);
59     ok(ret == EINVAL, "Expected rand_s to return EINVAL, got %d\n", ret);
60     ok(errno == EINVAL, "Expected errno to return EINVAL, got %d\n", errno);
61
62     ret = prand_s(&rand);
63     ok(ret == 0, "Expected rand_s to return 0, got %d\n", ret);
64 }
65
66 static void test_memcpy_s(void)
67 {
68     static char data[] = "data\0to\0be\0copied";
69     static char dest[32];
70     int ret;
71
72     if(!pmemcpy_s)
73     {
74         win_skip("memcpy_s is not available\n");
75         return;
76     }
77
78     errno = 0xdeadbeef;
79     ret = pmemcpy_s(NULL, 0, NULL, 0);
80     ok(ret == 0, "ret = %x\n", ret);
81     ok(errno == 0xdeadbeef, "errno = %x\n", errno);
82
83     errno = 0xdeadbeef;
84     dest[0] = 'x';
85     ret = pmemcpy_s(dest, 10, NULL, 0);
86     ok(ret == 0, "ret = %x\n", ret);
87     ok(errno == 0xdeadbeef, "errno = %x\n", errno);
88     ok(dest[0] == 'x', "dest[0] != \'x\'\n");
89
90     errno = 0xdeadbeef;
91     ret = pmemcpy_s(NULL, 10, data, 10);
92     ok(ret == EINVAL, "ret = %x\n", ret);
93     ok(errno == EINVAL, "errno = %x\n", errno);
94
95     errno = 0xdeadbeef;
96     dest[7] = 'x';
97     ret = pmemcpy_s(dest, 10, data, 5);
98     ok(ret == 0, "ret = %x\n", ret);
99     ok(errno == 0xdeadbeef, "errno = %x\n", errno);
100     ok(memcmp(dest, data, 10), "All data copied\n");
101     ok(!memcmp(dest, data, 5), "First five bytes are different\n");
102
103     errno = 0xdeadbeef;
104     ret = pmemcpy_s(data, 10, data, 10);
105     ok(ret == 0, "ret = %x\n", ret);
106     ok(errno == 0xdeadbeef, "errno = %x\n", errno);
107     ok(!memcmp(dest, data, 5), "data was destroyed during overwriting\n");
108
109     errno = 0xdeadbeef;
110     dest[0] = 'x';
111     ret = pmemcpy_s(dest, 5, data, 10);
112     ok(ret == ERANGE, "ret = %x\n", ret);
113     ok(errno == ERANGE, "errno = %x\n", errno);
114     ok(dest[0] == '\0', "dest[0] != \'\\0\'\n");
115 }
116
117 typedef struct _I10_OUTPUT_data {
118     short pos;
119     char sign;
120     BYTE len;
121     char str[100];
122 } I10_OUTPUT_data;
123
124 typedef struct _I10_OUTPUT_test {
125     long double d;
126     int size;
127     int flags;
128
129     I10_OUTPUT_data out;
130     int ret;
131     const char *remain;
132 } I10_OUTPUT_test;
133
134 static const I10_OUTPUT_test I10_OUTPUT_tests[] = {
135     /* arg3 = 0 */
136     { 0.0, 10, 0, {0, ' ', 1, "0"}, 1, "" },
137     { 1.0, 10, 0, {1, ' ', 1, "1"}, 1, "000000009" },
138     { -1.0, 10, 0, {1, '-', 1, "1"}, 1, "000000009" },
139     { 1.23, 10, 0, {1, ' ', 3, "123"}, 1, "0000009" },
140     { 1e13, 10, 0, {14, ' ', 1, "1"}, 1, "000000009" },
141     { 1e30, 30, 0, {31, ' ', 21, "100000000000000001988"}, 1, "" },
142     { 1e-13, 10, 0, {-12, ' ', 1, "1"}, 1, "000000000" },
143     { 0.25, 10, 0, {0, ' ', 2, "25"}, 1, "00000000" },
144     { 1.0000001, 10, 0, {1, ' ', 8, "10000001"}, 1, "00" },
145     /* arg3 = 1 */
146     { 0.0, 10, 1, {0, ' ', 1, "0"}, 1, "" },
147     { 1.0, 10, 1, {1, ' ', 1, "1"}, 1, "0000000009" },
148     { -1.0, 10, 1, {1, '-', 1, "1"}, 1, "0000000009" },
149     { 1.23, 10, 1, {1, ' ', 3, "123"}, 1, "00000009" },
150     { 1e13, 10, 1, {14, ' ', 1, "1"}, 1, "00000000000000000009" },
151     { 1e30, 30, 1, {31, ' ', 21, "100000000000000001988"}, 1, "" },
152     { 1e-13, 10, 1, {0, ' ', 1, "0"}, 1, "" },
153     { 1e-7, 10, 1, {-6, ' ', 1, "1"}, 1, "09" },
154     { 0.25, 10, 1, {0, ' ', 2, "25"}, 1, "00000000" },
155     { 1.0000001, 10, 1, {1, ' ', 8, "10000001"}, 1, "000" },
156     /* too small buffer */
157     { 0.0, 0, 0, {0, ' ', 1, "0"}, 1, "" },
158     { 0.0, 0, 1, {0, ' ', 1, "0"}, 1, "" },
159     { 123.0, 2, 0, {3, ' ', 2, "12"}, 1, "" },
160     { 123.0, 0, 0, {0, ' ', 1, "0"}, 1, "" },
161     { 123.0, 2, 1, {3, ' ', 3, "123"}, 1, "09" },
162     { 0.99, 1, 0, {1, ' ', 1, "1"}, 1, "" },
163     { 1264567.0, 2, 0, {7, ' ', 2, "13"}, 1, "" },
164     { 1264567.0, 2, 1, {7, ' ', 7, "1264567"}, 1, "00" },
165     { 1234567891.0, 2, 1, {10, ' ', 10, "1234567891"}, 1, "09" }
166 };
167
168 static void test_I10_OUTPUT(void)
169 {
170     I10_OUTPUT_data out;
171     int i, j, ret;
172
173     if(!pI10_OUTPUT) {
174         win_skip("I10_OUTPUT not available\n");
175         return;
176     }
177
178     for(i=0; i<sizeof(I10_OUTPUT_tests)/sizeof(I10_OUTPUT_test); i++) {
179         memset(out.str, '#', sizeof(out.str));
180
181         ret = pI10_OUTPUT(I10_OUTPUT_tests[i].d, I10_OUTPUT_tests[i].size, I10_OUTPUT_tests[i].flags, &out);
182         ok(ret == I10_OUTPUT_tests[i].ret, "%d: ret = %d\n", i, ret);
183         ok(out.pos == I10_OUTPUT_tests[i].out.pos, "%d: out.pos = %hd\n", i, out.pos);
184         ok(out.sign == I10_OUTPUT_tests[i].out.sign, "%d: out.size = %c\n", i, out.sign);
185         ok(out.len == I10_OUTPUT_tests[i].out.len, "%d: out.len = %d\n", i, (int)out.len);
186         ok(!strcmp(out.str, I10_OUTPUT_tests[i].out.str), "%d: out.str = %s\n", i, out.str);
187
188         j = strlen(I10_OUTPUT_tests[i].remain);
189         if(j && I10_OUTPUT_tests[i].remain[j-1]=='9')
190             todo_wine ok(!strncmp(out.str+out.len+1, I10_OUTPUT_tests[i].remain, j),
191                     "%d: &out.str[%d] = %.25s...\n", i, out.len+1, out.str+out.len+1);
192         else
193             ok(!strncmp(out.str+out.len+1, I10_OUTPUT_tests[i].remain, j),
194                     "%d: &out.str[%d] = %.25s...\n", i, out.len+1, out.str+out.len+1);
195
196
197         for(j=out.len+strlen(I10_OUTPUT_tests[i].remain)+1; j<sizeof(out.str); j++)
198             if(out.str[j] != '#')
199                 ok(0, "%d: out.str[%d] = %c (expected \'#\')\n", i, j, out.str[j]);
200     }
201 }
202
203 static void test_strerror_s(void)
204 {
205     int ret;
206     char buf[256];
207
208     if (!pstrerror_s)
209     {
210         win_skip("strerror_s is not available\n");
211         return;
212     }
213
214     errno = EBADF;
215     ret = pstrerror_s(NULL, 0, 0);
216     ok(ret == EINVAL, "Expected strerror_s to return EINVAL, got %d\n", ret);
217     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
218
219     errno = EBADF;
220     ret = pstrerror_s(NULL, sizeof(buf), 0);
221     ok(ret == EINVAL, "Expected strerror_s to return EINVAL, got %d\n", ret);
222     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
223
224     memset(buf, 'X', sizeof(buf));
225     errno = EBADF;
226     ret = pstrerror_s(buf, 0, 0);
227     ok(ret == EINVAL, "Expected strerror_s to return EINVAL, got %d\n", ret);
228     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
229     ok(buf[0] == 'X', "Expected output buffer to be untouched\n");
230
231     memset(buf, 'X', sizeof(buf));
232     ret = pstrerror_s(buf, 1, 0);
233     ok(ret == 0, "Expected strerror_s to return 0, got %d\n", ret);
234     ok(strlen(buf) == 0, "Expected output buffer to be null terminated\n");
235
236     memset(buf, 'X', sizeof(buf));
237     ret = pstrerror_s(buf, 2, 0);
238     ok(ret == 0, "Expected strerror_s to return 0, got %d\n", ret);
239     ok(strlen(buf) == 1, "Expected output buffer to be truncated\n");
240
241     memset(buf, 'X', sizeof(buf));
242     ret = pstrerror_s(buf, sizeof(buf), 0);
243     ok(ret == 0, "Expected strerror_s to return 0, got %d\n", ret);
244
245     memset(buf, 'X', sizeof(buf));
246     ret = pstrerror_s(buf, sizeof(buf), -1);
247     ok(ret == 0, "Expected strerror_s to return 0, got %d\n", ret);
248 }
249
250 static void test__get_doserrno(void)
251 {
252     int ret, out;
253
254     if (!p_get_doserrno)
255     {
256         win_skip("_get_doserrno is not available\n");
257         return;
258     }
259
260     _doserrno = ERROR_INVALID_CMM;
261     errno = EBADF;
262     ret = p_get_doserrno(NULL);
263     ok(ret == EINVAL, "Expected _get_doserrno to return EINVAL, got %d\n", ret);
264     ok(_doserrno = ERROR_INVALID_CMM, "Expected _doserrno to be ERROR_INVALID_CMM, got %d\n", _doserrno);
265     ok(errno == EBADF, "Expected errno to be EBADF, got %d\n", errno);
266
267     _doserrno = ERROR_INVALID_CMM;
268     errno = EBADF;
269     out = 0xdeadbeef;
270     ret = p_get_doserrno(&out);
271     ok(ret == 0, "Expected _get_doserrno to return 0, got %d\n", ret);
272     ok(out == ERROR_INVALID_CMM, "Expected output variable to be ERROR_INVAID_CMM, got %d\n", out);
273 }
274
275 static void test__get_errno(void)
276 {
277     int ret, out;
278
279     if (!p_get_errno)
280     {
281         win_skip("_get_errno is not available\n");
282         return;
283     }
284
285     errno = EBADF;
286     ret = p_get_errno(NULL);
287     ok(ret == EINVAL, "Expected _get_errno to return EINVAL, got %d\n", ret);
288     ok(errno == EBADF, "Expected errno to be EBADF, got %d\n", errno);
289
290     errno = EBADF;
291     out = 0xdeadbeef;
292     ret = p_get_errno(&out);
293     ok(ret == 0, "Expected _get_errno to return 0, got %d\n", ret);
294     ok(out == EBADF, "Expected output variable to be EBADF, got %d\n", out);
295 }
296
297 static void test__set_errno(void)
298 {
299     int ret;
300
301     if (!p_set_errno)
302     {
303         win_skip("_set_errno is not available\n");
304         return;
305     }
306
307     errno = EBADF;
308     ret = p_set_errno(EINVAL);
309     ok(ret == 0, "Expected _set_errno to return 0, got %d\n", ret);
310     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
311
312     errno = EBADF;
313     ret = p_set_errno(-1);
314     ok(ret == 0, "Expected _set_errno to return 0, got %d\n", ret);
315     ok(errno == -1, "Expected errno to be -1, got %d\n", errno);
316
317     errno = EBADF;
318     ret = p_set_errno(0xdeadbeef);
319     ok(ret == 0, "Expected _set_errno to return 0, got %d\n", ret);
320     ok(errno == 0xdeadbeef, "Expected errno to be 0xdeadbeef, got %d\n", errno);
321 }
322
323 START_TEST(misc)
324 {
325     init();
326
327     test_rand_s();
328     test_memcpy_s();
329     test_I10_OUTPUT();
330     test_strerror_s();
331     test__get_doserrno();
332     test__get_errno();
333     test__set_errno();
334 }