msvcrt: Implement strerror_s.
[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
30 static void init(void)
31 {
32     HMODULE hmod = GetModuleHandleA("msvcrt.dll");
33
34     prand_s = (void *)GetProcAddress(hmod, "rand_s");
35     pmemcpy_s = (void*)GetProcAddress(hmod, "memcpy_s");
36     pI10_OUTPUT = (void*)GetProcAddress(hmod, "$I10_OUTPUT");
37     pstrerror_s = (void *)GetProcAddress(hmod, "strerror_s");
38 }
39
40 static void test_rand_s(void)
41 {
42     int ret;
43     unsigned int rand;
44
45     if (!prand_s)
46     {
47         win_skip("rand_s is not available\n");
48         return;
49     }
50
51     errno = EBADF;
52     ret = prand_s(NULL);
53     ok(ret == EINVAL, "Expected rand_s to return EINVAL, got %d\n", ret);
54     ok(errno == EINVAL, "Expected errno to return EINVAL, got %d\n", errno);
55
56     ret = prand_s(&rand);
57     ok(ret == 0, "Expected rand_s to return 0, got %d\n", ret);
58 }
59
60 static void test_memcpy_s(void)
61 {
62     static char data[] = "data\0to\0be\0copied";
63     static char dest[32];
64     int ret;
65
66     if(!pmemcpy_s)
67     {
68         win_skip("memcpy_s is not available\n");
69         return;
70     }
71
72     errno = 0xdeadbeef;
73     ret = pmemcpy_s(NULL, 0, NULL, 0);
74     ok(ret == 0, "ret = %x\n", ret);
75     ok(errno == 0xdeadbeef, "errno = %x\n", errno);
76
77     errno = 0xdeadbeef;
78     dest[0] = 'x';
79     ret = pmemcpy_s(dest, 10, NULL, 0);
80     ok(ret == 0, "ret = %x\n", ret);
81     ok(errno == 0xdeadbeef, "errno = %x\n", errno);
82     ok(dest[0] == 'x', "dest[0] != \'x\'\n");
83
84     errno = 0xdeadbeef;
85     ret = pmemcpy_s(NULL, 10, data, 10);
86     ok(ret == EINVAL, "ret = %x\n", ret);
87     ok(errno == EINVAL, "errno = %x\n", errno);
88
89     errno = 0xdeadbeef;
90     dest[7] = 'x';
91     ret = pmemcpy_s(dest, 10, data, 5);
92     ok(ret == 0, "ret = %x\n", ret);
93     ok(errno == 0xdeadbeef, "errno = %x\n", errno);
94     ok(memcmp(dest, data, 10), "All data copied\n");
95     ok(!memcmp(dest, data, 5), "First five bytes are different\n");
96
97     errno = 0xdeadbeef;
98     ret = pmemcpy_s(data, 10, data, 10);
99     ok(ret == 0, "ret = %x\n", ret);
100     ok(errno == 0xdeadbeef, "errno = %x\n", errno);
101     ok(!memcmp(dest, data, 5), "data was destroyed during overwriting\n");
102
103     errno = 0xdeadbeef;
104     dest[0] = 'x';
105     ret = pmemcpy_s(dest, 5, data, 10);
106     ok(ret == ERANGE, "ret = %x\n", ret);
107     ok(errno == ERANGE, "errno = %x\n", errno);
108     ok(dest[0] == '\0', "dest[0] != \'\\0\'\n");
109 }
110
111 typedef struct _I10_OUTPUT_data {
112     short pos;
113     char sign;
114     BYTE len;
115     char str[100];
116 } I10_OUTPUT_data;
117
118 typedef struct _I10_OUTPUT_test {
119     long double d;
120     int size;
121     int flags;
122
123     I10_OUTPUT_data out;
124     int ret;
125     const char *remain;
126 } I10_OUTPUT_test;
127
128 static const I10_OUTPUT_test I10_OUTPUT_tests[] = {
129     /* arg3 = 0 */
130     { 0.0, 10, 0, {0, ' ', 1, "0"}, 1, "" },
131     { 1.0, 10, 0, {1, ' ', 1, "1"}, 1, "000000009" },
132     { -1.0, 10, 0, {1, '-', 1, "1"}, 1, "000000009" },
133     { 1.23, 10, 0, {1, ' ', 3, "123"}, 1, "0000009" },
134     { 1e13, 10, 0, {14, ' ', 1, "1"}, 1, "000000009" },
135     { 1e30, 30, 0, {31, ' ', 21, "100000000000000001988"}, 1, "" },
136     { 1e-13, 10, 0, {-12, ' ', 1, "1"}, 1, "000000000" },
137     { 0.25, 10, 0, {0, ' ', 2, "25"}, 1, "00000000" },
138     { 1.0000001, 10, 0, {1, ' ', 8, "10000001"}, 1, "00" },
139     /* arg3 = 1 */
140     { 0.0, 10, 1, {0, ' ', 1, "0"}, 1, "" },
141     { 1.0, 10, 1, {1, ' ', 1, "1"}, 1, "0000000009" },
142     { -1.0, 10, 1, {1, '-', 1, "1"}, 1, "0000000009" },
143     { 1.23, 10, 1, {1, ' ', 3, "123"}, 1, "00000009" },
144     { 1e13, 10, 1, {14, ' ', 1, "1"}, 1, "00000000000000000009" },
145     { 1e30, 30, 1, {31, ' ', 21, "100000000000000001988"}, 1, "" },
146     { 1e-13, 10, 1, {0, ' ', 1, "0"}, 1, "" },
147     { 1e-7, 10, 1, {-6, ' ', 1, "1"}, 1, "09" },
148     { 0.25, 10, 1, {0, ' ', 2, "25"}, 1, "00000000" },
149     { 1.0000001, 10, 1, {1, ' ', 8, "10000001"}, 1, "000" },
150     /* too small buffer */
151     { 0.0, 0, 0, {0, ' ', 1, "0"}, 1, "" },
152     { 0.0, 0, 1, {0, ' ', 1, "0"}, 1, "" },
153     { 123.0, 2, 0, {3, ' ', 2, "12"}, 1, "" },
154     { 123.0, 0, 0, {0, ' ', 1, "0"}, 1, "" },
155     { 123.0, 2, 1, {3, ' ', 3, "123"}, 1, "09" },
156     { 0.99, 1, 0, {1, ' ', 1, "1"}, 1, "" },
157     { 1264567.0, 2, 0, {7, ' ', 2, "13"}, 1, "" },
158     { 1264567.0, 2, 1, {7, ' ', 7, "1264567"}, 1, "00" },
159     { 1234567891.0, 2, 1, {10, ' ', 10, "1234567891"}, 1, "09" }
160 };
161
162 static void test_I10_OUTPUT(void)
163 {
164     I10_OUTPUT_data out;
165     int i, j, ret;
166
167     if(!pI10_OUTPUT) {
168         win_skip("I10_OUTPUT not available\n");
169         return;
170     }
171
172     for(i=0; i<sizeof(I10_OUTPUT_tests)/sizeof(I10_OUTPUT_test); i++) {
173         memset(out.str, '#', sizeof(out.str));
174
175         ret = pI10_OUTPUT(I10_OUTPUT_tests[i].d, I10_OUTPUT_tests[i].size, I10_OUTPUT_tests[i].flags, &out);
176         ok(ret == I10_OUTPUT_tests[i].ret, "%d: ret = %d\n", i, ret);
177         ok(out.pos == I10_OUTPUT_tests[i].out.pos, "%d: out.pos = %hd\n", i, out.pos);
178         ok(out.sign == I10_OUTPUT_tests[i].out.sign, "%d: out.size = %c\n", i, out.sign);
179         ok(out.len == I10_OUTPUT_tests[i].out.len, "%d: out.len = %d\n", i, (int)out.len);
180         ok(!strcmp(out.str, I10_OUTPUT_tests[i].out.str), "%d: out.str = %s\n", i, out.str);
181
182         j = strlen(I10_OUTPUT_tests[i].remain);
183         if(j && I10_OUTPUT_tests[i].remain[j-1]=='9')
184             todo_wine ok(!strncmp(out.str+out.len+1, I10_OUTPUT_tests[i].remain, j),
185                     "%d: &out.str[%d] = %.25s...\n", i, out.len+1, out.str+out.len+1);
186         else
187             ok(!strncmp(out.str+out.len+1, I10_OUTPUT_tests[i].remain, j),
188                     "%d: &out.str[%d] = %.25s...\n", i, out.len+1, out.str+out.len+1);
189
190
191         for(j=out.len+strlen(I10_OUTPUT_tests[i].remain)+1; j<sizeof(out.str); j++)
192             if(out.str[j] != '#')
193                 ok(0, "%d: out.str[%d] = %c (expected \'#\')\n", i, j, out.str[j]);
194     }
195 }
196
197 static void test_strerror_s(void)
198 {
199     int ret;
200     char buf[256];
201
202     if (!pstrerror_s)
203     {
204         win_skip("strerror_s is not available\n");
205         return;
206     }
207
208     errno = EBADF;
209     ret = pstrerror_s(NULL, 0, 0);
210     ok(ret == EINVAL, "Expected strerror_s to return EINVAL, got %d\n", ret);
211     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
212
213     errno = EBADF;
214     ret = pstrerror_s(NULL, sizeof(buf), 0);
215     ok(ret == EINVAL, "Expected strerror_s to return EINVAL, got %d\n", ret);
216     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
217
218     memset(buf, 'X', sizeof(buf));
219     errno = EBADF;
220     ret = pstrerror_s(buf, 0, 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     ok(buf[0] == 'X', "Expected output buffer to be untouched\n");
224
225     memset(buf, 'X', sizeof(buf));
226     ret = pstrerror_s(buf, 1, 0);
227     ok(ret == 0, "Expected strerror_s to return 0, got %d\n", ret);
228     ok(strlen(buf) == 0, "Expected output buffer to be null terminated\n");
229
230     memset(buf, 'X', sizeof(buf));
231     ret = pstrerror_s(buf, 2, 0);
232     ok(ret == 0, "Expected strerror_s to return 0, got %d\n", ret);
233     ok(strlen(buf) == 1, "Expected output buffer to be truncated\n");
234
235     memset(buf, 'X', sizeof(buf));
236     ret = pstrerror_s(buf, sizeof(buf), 0);
237     ok(ret == 0, "Expected strerror_s to return 0, got %d\n", ret);
238
239     memset(buf, 'X', sizeof(buf));
240     ret = pstrerror_s(buf, sizeof(buf), -1);
241     ok(ret == 0, "Expected strerror_s to return 0, got %d\n", ret);
242 }
243
244 START_TEST(misc)
245 {
246     init();
247
248     test_rand_s();
249     test_memcpy_s();
250     test_I10_OUTPUT();
251     test_strerror_s();
252 }