1 /*
2  * Unit test suite for *scanf functions.
3  *
4  * Copyright 2002 Uwe Bonnes
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 <stdio.h>
22 
23 #include "wine/test.h"
24 
25 static void test_sscanf( void )
26 {
27     char buffer[100], buffer1[100];
28     char format[20];
29     int result, ret;
30     LONGLONG result64;
31     char c;
32     void *ptr;
33     float res1= -82.6267f, res2= 27.76f, res11, res12;
34     double double_res;
35     static const char pname[]=" St. Petersburg, Florida\n";
36     int hour=21,min=59,sec=20;
37     int  number,number_so_far;
38 
39 
40     /* check EOF */
41     strcpy(buffer,"");
42     ret = sscanf(buffer, "%d", &result);
43     ok( ret == EOF,"sscanf returns %x instead of %x\n", ret, EOF );
44 
45     /* check %p */
46     ok( sscanf("000000000046F170", "%p", &ptr) == 1, "sscanf failed\n"  );
47     ok( ptr == (void *)0x46F170,"sscanf reads %p instead of %x\n", ptr, 0x46F170 );
48 
49     ok( sscanf("0046F171", "%p", &ptr) == 1, "sscanf failed\n"  );
50     ok( ptr == (void *)0x46F171,"sscanf reads %p instead of %x\n", ptr, 0x46F171 );
51 
52     ok( sscanf("46F172", "%p", &ptr) == 1, "sscanf failed\n"  );
53     ok( ptr == (void *)0x46F172,"sscanf reads %p instead of %x\n", ptr, 0x46F172 );
54 
55     ok( sscanf("0x46F173", "%p", &ptr) == 1, "sscanf failed\n"  );
56     ok( ptr == NULL,"sscanf reads %p instead of %x\n", ptr, 0 );
57 
58     ok( sscanf("-46F174", "%p", &ptr) == 1, "sscanf failed\n"  );
59     ok( ptr == (void *)(ULONG_PTR)-0x46f174,"sscanf reads %p instead of %p\n",
60         ptr, (void *)(ULONG_PTR)-0x46f174 );
61 
62     ok( sscanf("+46F175", "%p", &ptr) == 1, "sscanf failed\n"  );
63     ok( ptr == (void *)0x46F175,"sscanf reads %p instead of %x\n", ptr, 0x46F175 );
64 
65     /* check %p with no hex digits */
66     ok( sscanf("1233", "%p", &ptr) == 1, "sscanf failed\n"  );
67     ok( ptr == (void *)0x1233,"sscanf reads %p instead of %x\n", ptr, 0x1233 );
68 
69     ok( sscanf("1234", "%P", &ptr) == 1, "sscanf failed\n"  );
70     ok( ptr == (void *)0x1234,"sscanf reads %p instead of %x\n", ptr, 0x1234 );
71 
72     /* check %x */
73     strcpy(buffer,"0x519");
74     ok( sscanf(buffer, "%x", &result) == 1, "sscanf failed\n"  );
75     ok( result == 0x519,"sscanf reads %x instead of %x\n", result, 0x519 );
76 
77     strcpy(buffer,"0x51a");
78     ok( sscanf(buffer, "%x", &result) == 1, "sscanf failed\n" );
79     ok( result == 0x51a ,"sscanf reads %x instead of %x\n", result, 0x51a );
80 
81     strcpy(buffer,"0x51g");
82     ok( sscanf(buffer, "%x", &result) == 1, "sscanf failed\n" );
83     ok( result == 0x51, "sscanf reads %x instead of %x\n", result, 0x51 );
84 
85     result = 0;
86     ret = sscanf("-1", "%x", &result);
87     ok(ret == 1, "Wrong number of arguments read: %d (expected 1)\n", ret);
88     ok(result == -1, "Read %d, expected -1\n", result);
89 
90     /* check % followed by any char */
91     strcpy(buffer,"\"%12@");
92     strcpy(format,"%\"%%%d%@");  /* work around gcc format check */
93     ok( sscanf(buffer, format, &result) == 1, "sscanf failed\n" );
94     ok( result == 12, "sscanf reads %x instead of %x\n", result, 12 );
95 
96     /* Check float */
97     ret = sprintf(buffer,"%f %f",res1, res2);
98     ok( ret == 20, "expected 20, got %u\n", ret);
99     ret = sscanf(buffer,"%f%f",&res11, &res12);
100     ok( ret == 2, "expected 2, got %u\n", ret);
101     ok( (res11 == res1) && (res12 == res2), "Error reading floats\n");
102 
103     /* Check double */
104     ret = sprintf(buffer, "%lf", 32.715);
105     ok(ret == 9, "expected 9, got %u\n", ret);
106     ret = sscanf(buffer, "%lf", &double_res);
107     ok(ret == 1, "expected 1, got %u\n", ret);
108     ok(double_res == 32.715, "Got %lf, expected %lf\n", double_res, 32.715);
109     ret = sscanf(buffer, "%Lf", &double_res);
110     ok(ret == 1, "expected 1, got %u\n", ret);
111     ok(double_res == 32.715, "Got %lf, expected %lf\n", double_res, 32.715);
112 
113     strcpy(buffer, "1.1e-30");
114     ret = sscanf(buffer, "%lf", &double_res);
115     ok(ret == 1, "expected 1, got %u\n", ret);
116     ok(double_res >= 1.1e-30-1e-45 && double_res <= 1.1e-30+1e-45,
117             "Got %.18le, expected %.18le\n", double_res, 1.1e-30);
118 
119     /* check strings */
120     ret = sprintf(buffer," %s", pname);
121     ok( ret == 26, "expected 26, got %u\n", ret);
122     ret = sscanf(buffer,"%*c%[^\n]",buffer1);
123     ok( ret == 1, "Error with format \"%s\"\n","%*c%[^\n]");
124     ok( strncmp(pname,buffer1,strlen(buffer1)) == 0, "Error with \"%s\" \"%s\"\n",pname, buffer1);
125 
126     ret = sscanf("abcefgdh","%*[a-cg-e]%c",&buffer[0]);
127     ok( ret == 1, "Error with format \"%s\"\n","%*[a-cg-e]%c");
128     ok( buffer[0] == 'd', "Error with \"abcefgdh\" \"%c\"\n", buffer[0]);
129 
130     ret = sscanf("abcefgdh","%*[a-cd-dg-e]%c",&buffer[0]);
131     ok( ret == 1, "Error with format \"%s\"\n","%*[a-cd-dg-e]%c");
132     ok( buffer[0] == 'h', "Error with \"abcefgdh\" \"%c\"\n", buffer[0]);
133 
134     buffer1[0] = 'b';
135     ret = sscanf("a","%s%s", buffer, buffer1);
136     ok( ret == 1, "expected 1, got %u\n", ret);
137     ok( buffer[0] == 'a', "buffer[0] = '%c'\n", buffer[0]);
138     ok( buffer[1] == '\0', "buffer[1] = '%c'\n", buffer[1]);
139     ok( buffer1[0] == 'b', "buffer1[0] = '%c'\n", buffer1[0]);
140 
141     /* check digits */
142     ret = sprintf(buffer,"%d:%d:%d",hour,min,sec);
143     ok( ret == 8, "expected 8, got %u\n", ret);
144     ret = sscanf(buffer,"%d%n",&number,&number_so_far);
145     ok(ret == 1 , "problem with format arg \"%%d%%n\"\n");
146     ok(number == hour,"Read wrong arg %d instead of %d\n",number, hour);
147     ok(number_so_far == 2,"Read wrong arg for \"%%n\" %d instead of 2\n",number_so_far);
148 
149     ret = sscanf(buffer+2,"%*c%n",&number_so_far);
150     ok(ret == 0 , "problem with format arg \"%%*c%%n\"\n");
151     ok(number_so_far == 1,"Read wrong arg for \"%%n\" %d instead of 2\n",number_so_far);
152 
153     result = 0xdeadbeef;
154     strcpy(buffer,"12345678");
155     ret = sscanf(buffer, "%hd", &result);
156     ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
157     ok(result == 0xdead614e, "Wrong number read (%x)\n", result);
158 
159     result = 0xdeadbeef;
160     ret = sscanf(buffer, "%hhd", &result);
161     ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
162     ok(result == 0xbc614e, "Wrong number read (%x)\n", result);
163 
164     strcpy(buffer,"12345678901234");
165     ret = sscanf(buffer, "%lld", &result64);
166     ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
167     ret = sprintf(buffer1, "%lld", result64);
168     ok(ret==14 || broken(ret==10), "sprintf returned %d\n", ret);
169     if(ret == 14)
170         ok(!strcmp(buffer, buffer1), "got %s, expected %s\n", buffer1, buffer);
171 
172     /* Check %i according to bug 1878 */
173     strcpy(buffer,"123");
174     ret = sscanf(buffer, "%i", &result);
175     ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
176     ok(result == 123, "Wrong number read\n");
177     result = 0;
178     ret = sscanf("-1", "%i", &result);
179     ok(ret == 1, "Wrong number of arguments read: %d (expected 1)\n", ret);
180     ok(result == -1, "Read %d, expected -1\n", result);
181     ret = sscanf(buffer, "%d", &result);
182     ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
183     ok(result == 123, "Wrong number read\n");
184     result = 0;
185     ret = sscanf("-1", "%d", &result);
186     ok(ret == 1, "Wrong number of arguments read: %d (expected 1)\n", ret);
187     ok(result == -1, "Read %d, expected -1\n", result);
188 
189     /* Check %i for octal and hexadecimal input */
190     result = 0;
191     strcpy(buffer,"017");
192     ret = sscanf(buffer, "%i", &result);
193     ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
194     ok(result == 15, "Wrong number read\n");
195     result = 0;
196     strcpy(buffer,"0x17");
197     ret = sscanf(buffer, "%i", &result);
198     ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
199     ok(result == 23, "Wrong number read\n");
200 
201     /* %o */
202     result = 0;
203     ret = sscanf("-1", "%o", &result);
204     ok(ret == 1, "Wrong number of arguments read: %d (expected 1)\n", ret);
205     ok(result == -1, "Read %d, expected -1\n", result);
206 
207     /* %u */
208     result = 0;
209     ret = sscanf("-1", "%u", &result);
210     ok(ret == 1, "Wrong number of arguments read: %d (expected 1)\n", ret);
211     ok(result == -1, "Read %d, expected -1\n", result);
212 
213     /* Check %c */
214     strcpy(buffer,"a");
215     c = 0x55;
216     ret = sscanf(buffer, "%c", &c);
217     ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
218     ok(c == 'a', "Field incorrect: '%c'\n", c);
219 
220     strcpy(buffer," a");
221     c = 0x55;
222     ret = sscanf(buffer, "%c", &c);
223     ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
224     ok(c == ' ', "Field incorrect: '%c'\n", c);
225 
226     strcpy(buffer,"18:59");
227     c = 0x55;
228     ret = sscanf(buffer, "%d:%d%c", &hour, &min, &c);
229     ok(ret == 2, "Wrong number of arguments read: %d\n", ret);
230     ok(hour == 18, "Field 1 incorrect: %d\n", hour);
231     ok(min == 59, "Field 2 incorrect: %d\n", min);
232     ok(c == 0x55, "Field 3 incorrect: 0x%02x\n", c);
233 
234     /* Check %n (also whitespace in format strings and %s) */
235     buffer[0]=0; buffer1[0]=0;
236     ret = sscanf("abc   def", "%s %n%s", buffer, &number_so_far, buffer1);
237     ok(strcmp(buffer, "abc")==0, "First %%s read incorrectly: %s\n", buffer);
238     ok(strcmp(buffer1,"def")==0, "Second %%s read incorrectly: %s\n", buffer1);
239     ok(number_so_far==6, "%%n yielded wrong result: %d\n", number_so_far);
240     ok(ret == 2, "%%n shouldn't count as a conversion: %d\n", ret);
241 
242     /* Check where %n matches to EOF in buffer */
243     strcpy(buffer, "3:45");
244     ret = sscanf(buffer, "%d:%d%n", &hour, &min, &number_so_far);
245     ok(ret == 2, "Wrong number of arguments read: %d\n", ret);
246     ok(number_so_far == 4, "%%n yielded wrong result: %d\n", number_so_far);
247 
248     buffer[0] = 0;
249     buffer1[0] = 0;
250     ret = sscanf("test=value\xda", "%[^=] = %[^;]", buffer, buffer1);
251     ok(ret == 2, "got %d\n", ret);
252     ok(!strcmp(buffer, "test"), "buf %s\n", buffer);
253     ok(!strcmp(buffer1, "value\xda"), "buf %s\n", buffer1);
254 
255     ret = sscanf("\x81\x82test", "\x81%\x82%s", buffer);
256     ok(ret == 1, "got %d\n", ret);
257     ok(!strcmp(buffer, "test"), "buf = %s\n", buffer);
258 }
259 
260 static void test_sscanf_s(void)
261 {
262     int (WINAPIV *psscanf_s)(const char*,const char*,...);
263     HMODULE hmod = GetModuleHandleA("msvcrt.dll");
264     int i, ret;
265     char buf[100];
266 
267     psscanf_s = (void*)GetProcAddress(hmod, "sscanf_s");
268     if(!psscanf_s) {
269         win_skip("sscanf_s not available\n");
270         return;
271     }
272 
273     ret = psscanf_s("123", "%d", &i);
274     ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
275     ok(i == 123, "i = %d\n", i);
276 
277     ret = psscanf_s("123", "%s", buf, 100);
278     ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
279     ok(!strcmp("123", buf), "buf = %s\n", buf);
280 
281     ret = psscanf_s("123", "%s", buf, 3);
282     ok(ret == 0, "Wrong number of arguments read: %d\n", ret);
283     ok(buf[0]=='\0', "buf = %s\n", buf);
284 
285     buf[0] = 'a';
286     ret = psscanf_s("123", "%3c", buf, 2);
287     ok(ret == 0, "Wrong number of arguments read: %d\n", ret);
288     ok(buf[0]=='\0', "buf = %s\n", buf);
289 
290     i = 1;
291     ret = psscanf_s("123 123", "%s %d", buf, 2, &i);
292     ok(ret == 0, "Wrong number of arguments read: %d\n", ret);
293     ok(i==1, "i = %d\n", i);
294 
295     i = 1;
296     ret = psscanf_s("123 123", "%d %s", &i, buf, 2);
297     ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
298     ok(i==123, "i = %d\n", i);
299 }
300 
301 static void test_swscanf( void )
302 {
303     wchar_t buffer[100];
304     int result, ret;
305     static const WCHAR formatd[] = {'%','d',0};
306     const WCHAR format2[] = {'a',0x1234,'%',0x1234,'%','c',0};
307     WCHAR c;
308 
309     /* check WEOF */
310     /* WEOF is an unsigned short -1 but swscanf returns int
311        so it should be sign-extended */
312     buffer[0] = 0;
313     ret = swscanf(buffer, formatd, &result);
314     /* msvcrt returns 0 but should return -1 (later versions do) */
315     ok( ret == (short)WEOF || broken(ret == 0),
316         "swscanf returns %x instead of %x\n", ret, WEOF );
317 
318     buffer[0] = 'a';
319     buffer[1] = 0x1234;
320     buffer[2] = 0x1234;
321     buffer[3] = 'b';
322     ret = swscanf(buffer, format2, &c);
323     ok(ret == 1, "swscanf returned %d\n", ret);
324     ok(c == 'b', "c = %x\n", c);
325 }
326 
327 static void test_swscanf_s(void)
328 {
329     static const wchar_t fmt1[] = {'%','c',0};
330     static const wchar_t fmt2[] = {'%','[','a','-','z',']',0};
331 
332     int (WINAPIV *pswscanf_s)(const wchar_t*,const wchar_t*,...);
333     HMODULE hmod = GetModuleHandleA("msvcrt.dll");
334     wchar_t buf[2], out[2];
335     int ret;
336 
337     pswscanf_s = (void*)GetProcAddress(hmod, "swscanf_s");
338     if(!pswscanf_s) {
339         win_skip("swscanf_s not available\n");
340         return;
341     }
342 
343     buf[0] = 'a';
344     buf[1] = '1';
345     out[1] = 'b';
346     ret = pswscanf_s(buf, fmt1, out, 1);
347     ok(ret == 1, "swscanf_s returned %d\n", ret);
348     ok(out[0] == 'a', "out[0] = %x\n", out[0]);
349     ok(out[1] == 'b', "out[1] = %x\n", out[1]);
350 
351     ret = pswscanf_s(buf, fmt2, out, 1);
352     ok(!ret, "swscanf_s returned %d\n", ret);
353 
354     ret = pswscanf_s(buf, fmt2, out, 2);
355     ok(ret == 1, "swscanf_s returned %d\n", ret);
356     ok(out[0] == 'a', "out[0] = %x\n", out[0]);
357     ok(!out[1], "out[1] = %x\n", out[1]);
358 }
359 
360 START_TEST(scanf)
361 {
362     test_sscanf();
363     test_sscanf_s();
364     test_swscanf();
365     test_swscanf_s();
366 }
367