fltlib: Add a stub dll.
[wine] / dlls / kernel32 / tests / console.c
1 /*
2  * Unit tests for console API
3  *
4  * Copyright (c) 2003,2004 Eric Pouech
5  * Copyright (c) 2007 Kirill K. Smirnov
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "wine/test.h"
23 #include <windows.h>
24 #include <stdio.h>
25
26 static BOOL (WINAPI *pGetConsoleInputExeNameA)(DWORD, LPSTR);
27 static BOOL (WINAPI *pSetConsoleInputExeNameA)(LPCSTR);
28
29 /* DEFAULT_ATTRIB is used for all initial filling of the console.
30  * all modifications are made with TEST_ATTRIB so that we could check
31  * what has to be modified or not
32  */
33 #define TEST_ATTRIB    (BACKGROUND_BLUE | FOREGROUND_GREEN)
34 #define DEFAULT_ATTRIB (FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED)
35 /* when filling the screen with non-blank chars, this macro defines
36  * what character should be at position 'c'
37  */
38 #define CONTENT(c)    ('A' + (((c).Y * 17 + (c).X) % 23))
39
40 #define okCURSOR(hCon, c) do { \
41   CONSOLE_SCREEN_BUFFER_INFO __sbi; \
42   BOOL expect = GetConsoleScreenBufferInfo((hCon), &__sbi) && \
43                 __sbi.dwCursorPosition.X == (c).X && __sbi.dwCursorPosition.Y == (c).Y; \
44   ok(expect, "Expected cursor at (%d,%d), got (%d,%d)\n", \
45      (c).X, (c).Y, __sbi.dwCursorPosition.X, __sbi.dwCursorPosition.Y); \
46 } while (0)
47
48 #define okCHAR(hCon, c, ch, attr) do { \
49   char __ch; WORD __attr; DWORD __len; BOOL expect; \
50   expect = ReadConsoleOutputCharacter((hCon), &__ch, 1, (c), &__len) == 1 && __len == 1 && __ch == (ch); \
51   ok(expect, "At (%d,%d): expecting char '%c'/%02x got '%c'/%02x\n", (c).X, (c).Y, (ch), (ch), __ch, __ch); \
52   expect = ReadConsoleOutputAttribute((hCon), &__attr, 1, (c), &__len) == 1 && __len == 1 && __attr == (attr); \
53   ok(expect, "At (%d,%d): expecting attr %04x got %04x\n", (c).X, (c).Y, (attr), __attr); \
54 } while (0)
55
56 static void init_function_pointers(void)
57 {
58     HMODULE hKernel32;
59
60 #define KERNEL32_GET_PROC(func)                                     \
61     p##func = (void *)GetProcAddress(hKernel32, #func);             \
62     if(!p##func) trace("GetProcAddress(hKernel32, '%s') failed\n", #func);
63
64     hKernel32 = GetModuleHandleA("kernel32.dll");
65     KERNEL32_GET_PROC(GetConsoleInputExeNameA);
66     KERNEL32_GET_PROC(SetConsoleInputExeNameA);
67
68 #undef KERNEL32_GET_PROC
69 }
70
71 /* FIXME: this could be optimized on a speed point of view */
72 static void resetContent(HANDLE hCon, COORD sbSize, BOOL content)
73 {
74     COORD       c;
75     WORD        attr = DEFAULT_ATTRIB;
76     char        ch;
77     DWORD       len;
78
79     for (c.X = 0; c.X < sbSize.X; c.X++)
80     {
81         for (c.Y = 0; c.Y < sbSize.Y; c.Y++)
82         {
83             ch = (content) ? CONTENT(c) : ' ';
84             WriteConsoleOutputAttribute(hCon, &attr, 1, c, &len);
85             WriteConsoleOutputCharacterA(hCon, &ch, 1, c, &len);
86         }
87     }
88 }
89
90 static void testCursor(HANDLE hCon, COORD sbSize)
91 {
92     COORD               c;
93
94     c.X = c.Y = 0;
95     ok(SetConsoleCursorPosition(0, c) == 0, "No handle\n");
96     ok(GetLastError() == ERROR_INVALID_HANDLE, "GetLastError: expecting %u got %u\n",
97        ERROR_INVALID_HANDLE, GetLastError());
98
99     c.X = c.Y = 0;
100     ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left\n");
101     okCURSOR(hCon, c);
102
103     c.X = sbSize.X - 1;
104     c.Y = sbSize.Y - 1;
105     ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in lower-right\n");
106     okCURSOR(hCon, c);
107
108     c.X = sbSize.X;
109     c.Y = sbSize.Y - 1;
110     ok(SetConsoleCursorPosition(hCon, c) == 0, "Cursor is outside\n");
111     ok(GetLastError() == ERROR_INVALID_PARAMETER, "GetLastError: expecting %u got %u\n",
112        ERROR_INVALID_PARAMETER, GetLastError());
113
114     c.X = sbSize.X - 1;
115     c.Y = sbSize.Y;
116     ok(SetConsoleCursorPosition(hCon, c) == 0, "Cursor is outside\n");
117     ok(GetLastError() == ERROR_INVALID_PARAMETER, "GetLastError: expecting %u got %u\n",
118        ERROR_INVALID_PARAMETER, GetLastError());
119
120     c.X = -1;
121     c.Y = 0;
122     ok(SetConsoleCursorPosition(hCon, c) == 0, "Cursor is outside\n");
123     ok(GetLastError() == ERROR_INVALID_PARAMETER, "GetLastError: expecting %u got %u\n",
124        ERROR_INVALID_PARAMETER, GetLastError());
125
126     c.X = 0;
127     c.Y = -1;
128     ok(SetConsoleCursorPosition(hCon, c) == 0, "Cursor is outside\n");
129     ok(GetLastError() == ERROR_INVALID_PARAMETER, "GetLastError: expecting %u got %u\n",
130        ERROR_INVALID_PARAMETER, GetLastError());
131 }
132
133 static void testCursorInfo(HANDLE hCon)
134 {
135     BOOL ret;
136     CONSOLE_CURSOR_INFO info;
137
138     SetLastError(0xdeadbeef);
139     ret = GetConsoleCursorInfo(NULL, NULL);
140     ok(!ret, "Expected failure\n");
141     ok(GetLastError() == ERROR_INVALID_HANDLE, "GetLastError: expecting %u got %u\n",
142        ERROR_INVALID_HANDLE, GetLastError());
143
144     SetLastError(0xdeadbeef);
145     info.dwSize = -1;
146     ret = GetConsoleCursorInfo(NULL, &info);
147     ok(!ret, "Expected failure\n");
148     ok(info.dwSize == -1, "Expected no change for dwSize\n");
149     ok(GetLastError() == ERROR_INVALID_HANDLE, "GetLastError: expecting %u got %u\n",
150        ERROR_INVALID_HANDLE, GetLastError());
151
152     /* Test the correct call first to distinguish between win9x and the rest */
153     SetLastError(0xdeadbeef);
154     ret = GetConsoleCursorInfo(hCon, &info);
155     ok(ret, "Expected success\n");
156     ok(info.dwSize == 25 ||
157        info.dwSize == 12 /* win9x */,
158        "Expected 12 or 25, got %d\n", info.dwSize);
159     ok(info.bVisible, "Expected the cursor to be visible\n");
160     ok(GetLastError() == 0xdeadbeef, "GetLastError: expecting %u got %u\n",
161        0xdeadbeef, GetLastError());
162
163     /* Don't test NULL CONSOLE_CURSOR_INFO, it crashes on win9x and win7 */
164 }
165
166 static void testEmptyWrite(HANDLE hCon)
167 {
168     COORD               c;
169     DWORD               len;
170     const char*         mytest = "";
171
172     c.X = c.Y = 0;
173     ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left\n");
174
175     len = -1;
176     ok(WriteConsole(hCon, NULL, 0, &len, NULL) != 0 && len == 0, "WriteConsole\n");
177     okCURSOR(hCon, c);
178
179     /* Passing a NULL lpBuffer with sufficiently large non-zero length succeeds
180      * on native Windows and result in memory-like contents being written to
181      * the console. Calling WriteConsoleW like this will crash on Wine. */
182     if (0)
183     {
184         len = -1;
185         ok(!WriteConsole(hCon, NULL, 16, &len, NULL) && len == -1, "WriteConsole\n");
186         okCURSOR(hCon, c);
187
188         /* Cursor advances for this call. */
189         len = -1;
190         ok(WriteConsole(hCon, NULL, 128, &len, NULL) != 0 && len == 128, "WriteConsole\n");
191     }
192
193     len = -1;
194     ok(WriteConsole(hCon, mytest, 0, &len, NULL) != 0 && len == 0, "WriteConsole\n");
195     okCURSOR(hCon, c);
196
197     /* WriteConsole does not halt on a null terminator and is happy to write
198      * memory contents beyond the actual size of the buffer. */
199     len = -1;
200     ok(WriteConsole(hCon, mytest, 16, &len, NULL) != 0 && len == 16, "WriteConsole\n");
201     c.X += 16;
202     okCURSOR(hCon, c);
203 }
204
205 static void testWriteSimple(HANDLE hCon)
206 {
207     COORD               c;
208     DWORD               len;
209     const char*         mytest = "abcdefg";
210     const int   mylen = strlen(mytest);
211
212     /* single line write */
213     c.X = c.Y = 0;
214     ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left\n");
215
216     ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
217     c.Y = 0;
218     for (c.X = 0; c.X < mylen; c.X++)
219     {
220         okCHAR(hCon, c, mytest[c.X], TEST_ATTRIB);
221     }
222
223     okCURSOR(hCon, c);
224     okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
225 }
226
227 static void testWriteNotWrappedNotProcessed(HANDLE hCon, COORD sbSize)
228 {
229     COORD               c;
230     DWORD               len, mode;
231     const char*         mytest = "123";
232     const int           mylen = strlen(mytest);
233     int                 ret;
234     int                 p;
235
236     ok(GetConsoleMode(hCon, &mode) && SetConsoleMode(hCon, mode & ~(ENABLE_PROCESSED_OUTPUT|ENABLE_WRAP_AT_EOL_OUTPUT)),
237        "clearing wrap at EOL & processed output\n");
238
239     /* write line, wrapping disabled, buffer exceeds sb width */
240     c.X = sbSize.X - 3; c.Y = 0;
241     ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-3\n");
242
243     ret = WriteConsole(hCon, mytest, mylen, &len, NULL);
244     ok(ret != 0 && len == mylen, "Couldn't write, ret = %d, len = %d\n", ret, len);
245     c.Y = 0;
246     for (p = mylen - 3; p < mylen; p++)
247     {
248         c.X = sbSize.X - 3 + p % 3;
249         okCHAR(hCon, c, mytest[p], TEST_ATTRIB);
250     }
251
252     c.X = 0; c.Y = 1;
253     okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
254
255     p = sbSize.X - 3 + mylen % 3;
256     c.X = p; c.Y = 0;
257
258     /* write line, wrapping disabled, strings end on end of line */
259     c.X = sbSize.X - mylen; c.Y = 0;
260     ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-3\n");
261
262     ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
263 }
264
265 static void testWriteNotWrappedProcessed(HANDLE hCon, COORD sbSize)
266 {
267     COORD               c;
268     DWORD               len, mode;
269     const char*         mytest = "abcd\nf\tg";
270     const int   mylen = strlen(mytest);
271     const int   mylen2 = strchr(mytest, '\n') - mytest;
272     int                 p;
273
274     ok(GetConsoleMode(hCon, &mode) && SetConsoleMode(hCon, (mode | ENABLE_PROCESSED_OUTPUT) & ~ENABLE_WRAP_AT_EOL_OUTPUT),
275        "clearing wrap at EOL & setting processed output\n");
276
277     /* write line, wrapping disabled, buffer exceeds sb width */
278     c.X = sbSize.X - 5; c.Y = 0;
279     ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-5\n");
280
281     ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
282     c.Y = 0;
283     for (c.X = sbSize.X - 5; c.X < sbSize.X - 1; c.X++)
284     {
285         okCHAR(hCon, c, mytest[c.X - sbSize.X + 5], TEST_ATTRIB);
286     }
287     okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
288
289     c.X = 0; c.Y++;
290     okCHAR(hCon, c, mytest[5], TEST_ATTRIB);
291     for (c.X = 1; c.X < 8; c.X++)
292         okCHAR(hCon, c, ' ', TEST_ATTRIB);
293     okCHAR(hCon, c, mytest[7], TEST_ATTRIB);
294     c.X++;
295     okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
296
297     okCURSOR(hCon, c);
298
299     /* write line, wrapping disabled, strings end on end of line */
300     c.X = sbSize.X - 4; c.Y = 0;
301     ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-4\n");
302
303     ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
304     c.Y = 0;
305     for (c.X = sbSize.X - 4; c.X < sbSize.X; c.X++)
306     {
307         okCHAR(hCon, c, mytest[c.X - sbSize.X + 4], TEST_ATTRIB);
308     }
309     c.X = 0; c.Y++;
310     okCHAR(hCon, c, mytest[5], TEST_ATTRIB);
311     for (c.X = 1; c.X < 8; c.X++)
312         okCHAR(hCon, c, ' ', TEST_ATTRIB);
313     okCHAR(hCon, c, mytest[7], TEST_ATTRIB);
314     c.X++;
315     okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
316
317     okCURSOR(hCon, c);
318
319     /* write line, wrapping disabled, strings end after end of line */
320     c.X = sbSize.X - 3; c.Y = 0;
321     ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-4\n");
322
323     ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
324     c.Y = 0;
325     for (p = mylen2 - 3; p < mylen2; p++)
326     {
327         c.X = sbSize.X - 3 + p % 3;
328         okCHAR(hCon, c, mytest[p], TEST_ATTRIB);
329     }
330     c.X = 0; c.Y = 1;
331     okCHAR(hCon, c, mytest[5], TEST_ATTRIB);
332     for (c.X = 1; c.X < 8; c.X++)
333         okCHAR(hCon, c, ' ', TEST_ATTRIB);
334     okCHAR(hCon, c, mytest[7], TEST_ATTRIB);
335     c.X++;
336     okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
337
338     okCURSOR(hCon, c);
339 }
340
341 static void testWriteWrappedNotProcessed(HANDLE hCon, COORD sbSize)
342 {
343     COORD               c;
344     DWORD               len, mode;
345     const char*         mytest = "abcd\nf\tg";
346     const int   mylen = strlen(mytest);
347     int                 p;
348
349     ok(GetConsoleMode(hCon, &mode) && SetConsoleMode(hCon,(mode | ENABLE_WRAP_AT_EOL_OUTPUT) & ~(ENABLE_PROCESSED_OUTPUT)),
350        "setting wrap at EOL & clearing processed output\n");
351
352     /* write line, wrapping enabled, buffer doesn't exceed sb width */
353     c.X = sbSize.X - 9; c.Y = 0;
354     ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-9\n");
355
356     ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
357     c.Y = 0;
358     for (p = 0; p < mylen; p++)
359     {
360         c.X = sbSize.X - 9 + p;
361         okCHAR(hCon, c, mytest[p], TEST_ATTRIB);
362     }
363     c.X = sbSize.X - 9 + mylen;
364     okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
365     c.X = 0; c.Y = 1;
366     okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
367
368     /* write line, wrapping enabled, buffer does exceed sb width */
369     c.X = sbSize.X - 3; c.Y = 0;
370     ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-3\n");
371
372     c.Y = 1;
373     c.X = mylen - 3;
374     okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
375 }
376
377 static void testWriteWrappedProcessed(HANDLE hCon, COORD sbSize)
378 {
379     COORD               c;
380     DWORD               len, mode;
381     const char*         mytest = "abcd\nf\tg";
382     const int   mylen = strlen(mytest);
383     int                 p;
384
385     ok(GetConsoleMode(hCon, &mode) && SetConsoleMode(hCon, mode | (ENABLE_WRAP_AT_EOL_OUTPUT|ENABLE_PROCESSED_OUTPUT)),
386        "setting wrap at EOL & processed output\n");
387
388     /* write line, wrapping enabled, buffer doesn't exceed sb width */
389     c.X = sbSize.X - 9; c.Y = 0;
390     ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-9\n");
391
392     ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
393     for (p = 0; p < 4; p++)
394     {
395         c.X = sbSize.X - 9 + p;
396         okCHAR(hCon, c, mytest[p], TEST_ATTRIB);
397     }
398     c.X = sbSize.X - 9 + p;
399     okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
400     c.X = 0; c.Y++;
401     okCHAR(hCon, c, mytest[5], TEST_ATTRIB);
402     for (c.X = 1; c.X < 8; c.X++)
403         okCHAR(hCon, c, ' ', TEST_ATTRIB);
404     okCHAR(hCon, c, mytest[7], TEST_ATTRIB);
405     c.X++;
406     okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
407     okCURSOR(hCon, c);
408
409     /* write line, wrapping enabled, buffer does exceed sb width */
410     c.X = sbSize.X - 3; c.Y = 2;
411     ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-3\n");
412
413     ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
414     for (p = 0; p < 3; p++)
415     {
416         c.X = sbSize.X - 3 + p;
417         okCHAR(hCon, c, mytest[p], TEST_ATTRIB);
418     }
419     c.X = 0; c.Y++;
420     okCHAR(hCon, c, mytest[3], TEST_ATTRIB);
421     c.X++;
422     okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
423
424     c.X = 0; c.Y++;
425     okCHAR(hCon, c, mytest[5], TEST_ATTRIB);
426     for (c.X = 1; c.X < 8; c.X++)
427         okCHAR(hCon, c, ' ', TEST_ATTRIB);
428     okCHAR(hCon, c, mytest[7], TEST_ATTRIB);
429     c.X++;
430     okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
431     okCURSOR(hCon, c);
432 }
433
434 static void testWrite(HANDLE hCon, COORD sbSize)
435 {
436     /* FIXME: should in fact insure that the sb is at least 10 character wide */
437     ok(SetConsoleTextAttribute(hCon, TEST_ATTRIB), "Setting default text color\n");
438     resetContent(hCon, sbSize, FALSE);
439     testEmptyWrite(hCon);
440     resetContent(hCon, sbSize, FALSE);
441     testWriteSimple(hCon);
442     resetContent(hCon, sbSize, FALSE);
443     testWriteNotWrappedNotProcessed(hCon, sbSize);
444     resetContent(hCon, sbSize, FALSE);
445     testWriteNotWrappedProcessed(hCon, sbSize);
446     resetContent(hCon, sbSize, FALSE);
447     testWriteWrappedNotProcessed(hCon, sbSize);
448     resetContent(hCon, sbSize, FALSE);
449     testWriteWrappedProcessed(hCon, sbSize);
450 }
451
452 static void testScroll(HANDLE hCon, COORD sbSize)
453 {
454     SMALL_RECT  scroll, clip;
455     COORD       dst, c, tc;
456     CHAR_INFO   ci;
457     BOOL ret;
458
459 #define W 11
460 #define H 7
461
462 #define IN_SRECT(r,c) ((r).Left <= (c).X && (c).X <= (r).Right && (r).Top <= (c).Y && (c).Y <= (r).Bottom)
463 #define IN_SRECT2(r,d,c) ((d).X <= (c).X && (c).X <= (d).X + (r).Right - (r).Left && (d).Y <= (c).Y && (c).Y <= (d).Y + (r).Bottom - (r).Top)
464
465     /* no clipping, src & dst rect don't overlap */
466     resetContent(hCon, sbSize, TRUE);
467
468     scroll.Left = 0;
469     scroll.Right = W - 1;
470     scroll.Top = 0;
471     scroll.Bottom = H - 1;
472     dst.X = W + 3;
473     dst.Y = H + 3;
474     ci.Char.UnicodeChar = '#';
475     ci.Attributes = TEST_ATTRIB;
476
477     clip.Left = 0;
478     clip.Right = sbSize.X - 1;
479     clip.Top = 0;
480     clip.Bottom = sbSize.Y - 1;
481
482     ok(ScrollConsoleScreenBuffer(hCon, &scroll, NULL, dst, &ci), "Scrolling SB\n");
483
484     for (c.Y = 0; c.Y < sbSize.Y; c.Y++)
485     {
486         for (c.X = 0; c.X < sbSize.X; c.X++)
487         {
488             if (IN_SRECT2(scroll, dst, c) && IN_SRECT(clip, c))
489             {
490                 tc.X = c.X - dst.X;
491                 tc.Y = c.Y - dst.Y;
492                 okCHAR(hCon, c, CONTENT(tc), DEFAULT_ATTRIB);
493             }
494             else if (IN_SRECT(scroll, c) && IN_SRECT(clip, c))
495                 okCHAR(hCon, c, '#', TEST_ATTRIB);
496             else okCHAR(hCon, c, CONTENT(c), DEFAULT_ATTRIB);
497         }
498     }
499
500     /* no clipping, src & dst rect do overlap */
501     resetContent(hCon, sbSize, TRUE);
502
503     scroll.Left = 0;
504     scroll.Right = W - 1;
505     scroll.Top = 0;
506     scroll.Bottom = H - 1;
507     dst.X = W /2;
508     dst.Y = H / 2;
509     ci.Char.UnicodeChar = '#';
510     ci.Attributes = TEST_ATTRIB;
511
512     clip.Left = 0;
513     clip.Right = sbSize.X - 1;
514     clip.Top = 0;
515     clip.Bottom = sbSize.Y - 1;
516
517     ok(ScrollConsoleScreenBuffer(hCon, &scroll, NULL, dst, &ci), "Scrolling SB\n");
518
519     for (c.Y = 0; c.Y < sbSize.Y; c.Y++)
520     {
521         for (c.X = 0; c.X < sbSize.X; c.X++)
522         {
523             if (dst.X <= c.X && c.X < dst.X + W && dst.Y <= c.Y && c.Y < dst.Y + H)
524             {
525                 tc.X = c.X - dst.X;
526                 tc.Y = c.Y - dst.Y;
527                 okCHAR(hCon, c, CONTENT(tc), DEFAULT_ATTRIB);
528             }
529             else if (c.X < W && c.Y < H) okCHAR(hCon, c, '#', TEST_ATTRIB);
530             else okCHAR(hCon, c, CONTENT(c), DEFAULT_ATTRIB);
531         }
532     }
533
534     /* clipping, src & dst rect don't overlap */
535     resetContent(hCon, sbSize, TRUE);
536
537     scroll.Left = 0;
538     scroll.Right = W - 1;
539     scroll.Top = 0;
540     scroll.Bottom = H - 1;
541     dst.X = W + 3;
542     dst.Y = H + 3;
543     ci.Char.UnicodeChar = '#';
544     ci.Attributes = TEST_ATTRIB;
545
546     clip.Left = W / 2;
547     clip.Right = min(W + W / 2, sbSize.X - 1);
548     clip.Top = H / 2;
549     clip.Bottom = min(H + H / 2, sbSize.Y - 1);
550
551     SetLastError(0xdeadbeef);
552     ret = ScrollConsoleScreenBuffer(hCon, &scroll, &clip, dst, &ci);
553     if (ret)
554     {
555         for (c.Y = 0; c.Y < sbSize.Y; c.Y++)
556         {
557             for (c.X = 0; c.X < sbSize.X; c.X++)
558             {
559                 if (IN_SRECT2(scroll, dst, c) && IN_SRECT(clip, c))
560                 {
561                     tc.X = c.X - dst.X;
562                     tc.Y = c.Y - dst.Y;
563                     okCHAR(hCon, c, CONTENT(tc), DEFAULT_ATTRIB);
564                 }
565                 else if (IN_SRECT(scroll, c) && IN_SRECT(clip, c))
566                     okCHAR(hCon, c, '#', TEST_ATTRIB);
567                 else okCHAR(hCon, c, CONTENT(c), DEFAULT_ATTRIB);
568             }
569         }
570     }
571     else
572     {
573         /* Win9x will fail, Only accept ERROR_NOT_ENOUGH_MEMORY */
574         ok(GetLastError() == ERROR_NOT_ENOUGH_MEMORY,
575             "Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
576     }
577
578     /* clipping, src & dst rect do overlap */
579     resetContent(hCon, sbSize, TRUE);
580
581     scroll.Left = 0;
582     scroll.Right = W - 1;
583     scroll.Top = 0;
584     scroll.Bottom = H - 1;
585     dst.X = W / 2 - 3;
586     dst.Y = H / 2 - 3;
587     ci.Char.UnicodeChar = '#';
588     ci.Attributes = TEST_ATTRIB;
589
590     clip.Left = W / 2;
591     clip.Right = min(W + W / 2, sbSize.X - 1);
592     clip.Top = H / 2;
593     clip.Bottom = min(H + H / 2, sbSize.Y - 1);
594
595     ok(ScrollConsoleScreenBuffer(hCon, &scroll, &clip, dst, &ci), "Scrolling SB\n");
596
597     for (c.Y = 0; c.Y < sbSize.Y; c.Y++)
598     {
599         for (c.X = 0; c.X < sbSize.X; c.X++)
600         {
601             if (IN_SRECT2(scroll, dst, c) && IN_SRECT(clip, c))
602             {
603                 tc.X = c.X - dst.X;
604                 tc.Y = c.Y - dst.Y;
605                 okCHAR(hCon, c, CONTENT(tc), DEFAULT_ATTRIB);
606             }
607             else if (IN_SRECT(scroll, c) && IN_SRECT(clip, c))
608                 okCHAR(hCon, c, '#', TEST_ATTRIB);
609             else okCHAR(hCon, c, CONTENT(c), DEFAULT_ATTRIB);
610         }
611     }
612 }
613
614 static int mch_count;
615 /* we need the event as Wine console event generation isn't synchronous
616  * (ie GenerateConsoleCtrlEvent returns before all ctrl-handlers in all
617  * processes have been called).
618  */
619 static HANDLE mch_event;
620 static BOOL WINAPI mch(DWORD event)
621 {
622     mch_count++;
623     SetEvent(mch_event);
624     return TRUE;
625 }
626
627 static void testCtrlHandler(void)
628 {
629     ok(!SetConsoleCtrlHandler(mch, FALSE), "Shouldn't succeed\n");
630     ok(GetLastError() == ERROR_INVALID_PARAMETER, "Bad error %u\n", GetLastError());
631     ok(SetConsoleCtrlHandler(mch, TRUE), "Couldn't set handler\n");
632     /* wine requires the event for the test, as we cannot insure, so far, that event
633      * are processed synchronously in GenerateConsoleCtrlEvent()
634      */
635     mch_event = CreateEventA(NULL, TRUE, FALSE, NULL);
636     mch_count = 0;
637     ok(GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0), "Couldn't send ctrl-c event\n");
638     /* FIXME: it isn't synchronous on wine but it can still happen before we test */
639     if (0) ok(mch_count == 1, "Event isn't synchronous\n");
640     ok(WaitForSingleObject(mch_event, 3000) == WAIT_OBJECT_0, "event sending didn't work\n");
641     CloseHandle(mch_event);
642
643     /* Turning off ctrl-c handling doesn't work on win9x such way ... */
644     ok(SetConsoleCtrlHandler(NULL, TRUE), "Couldn't turn off ctrl-c handling\n");
645     mch_event = CreateEventA(NULL, TRUE, FALSE, NULL);
646     mch_count = 0;
647     if(!(GetVersion() & 0x80000000))
648         /* ... and next line leads to an unhandled exception on 9x.  Avoid it on 9x. */
649         ok(GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0), "Couldn't send ctrl-c event\n");
650     ok(WaitForSingleObject(mch_event, 3000) == WAIT_TIMEOUT && mch_count == 0, "Event shouldn't have been sent\n");
651     CloseHandle(mch_event);
652     ok(SetConsoleCtrlHandler(mch, FALSE), "Couldn't remove handler\n");
653     ok(!SetConsoleCtrlHandler(mch, FALSE), "Shouldn't succeed\n");
654     ok(GetLastError() == ERROR_INVALID_PARAMETER, "Bad error %u\n", GetLastError());
655 }
656
657 /*
658  * Test console screen buffer:
659  * 1) Try to set invalid handle.
660  * 2) Try to set non-console handles.
661  * 3) Use CONOUT$ file as active SB.
662  * 4) Test cursor.
663  * 5) Test output codepage to show it is not a property of SB.
664  * 6) Test switching to old SB if we close all handles to current SB - works
665  * in Windows, TODO in wine.
666  *
667  * What is not tested but should be:
668  * 1) ScreenBufferInfo
669  */
670 static void testScreenBuffer(HANDLE hConOut)
671 {
672     HANDLE hConOutRW, hConOutRO, hConOutWT;
673     HANDLE hFileOutRW, hFileOutRO, hFileOutWT;
674     HANDLE hConOutNew;
675     char test_str1[] = "Test for SB1";
676     char test_str2[] = "Test for SB2";
677     char test_cp866[] = {0xe2, 0xa5, 0xe1, 0xe2, 0};
678     char test_cp1251[] = {0xf2, 0xe5, 0xf1, 0xf2, 0};
679     WCHAR test_unicode[] = {0x0442, 0x0435, 0x0441, 0x0442, 0};
680     WCHAR str_wbuf[20];
681     char str_buf[20];
682     DWORD len;
683     COORD c;
684     BOOL ret;
685     DWORD oldcp;
686
687     if (!IsValidCodePage(866))
688     {
689         skip("Codepage 866 not available\n");
690         return;
691     }
692
693     /* In the beginning set output codepage to 866 */
694     oldcp = GetConsoleOutputCP();
695     SetLastError(0xdeadbeef);
696     ret = SetConsoleOutputCP(866);
697     if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
698     {
699         win_skip("SetConsoleOutputCP is not implemented\n");
700         return;
701     }
702     ok(ret, "Cannot set output codepage to 866\n");
703
704     hConOutRW = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE,
705                          FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
706                          CONSOLE_TEXTMODE_BUFFER, NULL);
707     ok(hConOutRW != INVALID_HANDLE_VALUE,
708        "Cannot create a new screen buffer for ReadWrite\n");
709     hConOutRO = CreateConsoleScreenBuffer(GENERIC_READ,
710                          FILE_SHARE_READ, NULL,
711                          CONSOLE_TEXTMODE_BUFFER, NULL);
712     ok(hConOutRO != INVALID_HANDLE_VALUE,
713        "Cannot create a new screen buffer for ReadOnly\n");
714     hConOutWT = CreateConsoleScreenBuffer(GENERIC_WRITE,
715                          FILE_SHARE_WRITE, NULL,
716                          CONSOLE_TEXTMODE_BUFFER, NULL);
717     ok(hConOutWT != INVALID_HANDLE_VALUE,
718        "Cannot create a new screen buffer for WriteOnly\n");
719
720     hFileOutRW = CreateFileA("NUL", GENERIC_READ | GENERIC_WRITE,
721                              FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
722                              OPEN_EXISTING, 0, NULL);
723     ok(hFileOutRW != INVALID_HANDLE_VALUE, "Cannot open NUL for ReadWrite\n");
724     hFileOutRO = CreateFileA("NUL", GENERIC_READ, FILE_SHARE_READ,
725                              NULL, OPEN_EXISTING, 0, NULL);
726     ok(hFileOutRO != INVALID_HANDLE_VALUE, "Cannot open NUL for ReadOnly\n");
727     hFileOutWT = CreateFileA("NUL", GENERIC_WRITE, FILE_SHARE_WRITE,
728                              NULL, OPEN_EXISTING, 0, NULL);
729     ok(hFileOutWT != INVALID_HANDLE_VALUE, "Cannot open NUL for WriteOnly\n");
730
731     /* Trying to set invalid handle */
732     SetLastError(0);
733     ok(!SetConsoleActiveScreenBuffer(INVALID_HANDLE_VALUE),
734        "Shouldn't succeed\n");
735     ok(GetLastError() == ERROR_INVALID_HANDLE,
736        "GetLastError: expecting %u got %u\n",
737        ERROR_INVALID_HANDLE, GetLastError());
738
739     /* Trying to set non-console handles */
740     SetLastError(0);
741     ok(!SetConsoleActiveScreenBuffer(hFileOutRW), "Shouldn't succeed\n");
742     ok(GetLastError() == ERROR_INVALID_HANDLE,
743        "GetLastError: expecting %u got %u\n",
744        ERROR_INVALID_HANDLE, GetLastError());
745
746     SetLastError(0);
747     ok(!SetConsoleActiveScreenBuffer(hFileOutRO), "Shouldn't succeed\n");
748     ok(GetLastError() == ERROR_INVALID_HANDLE,
749        "GetLastError: expecting %u got %u\n",
750        ERROR_INVALID_HANDLE, GetLastError());
751
752     SetLastError(0);
753     ok(!SetConsoleActiveScreenBuffer(hFileOutWT), "Shouldn't succeed\n");
754     ok(GetLastError() == ERROR_INVALID_HANDLE,
755        "GetLastError: expecting %u got %u\n",
756        ERROR_INVALID_HANDLE, GetLastError());
757
758     CloseHandle(hFileOutRW);
759     CloseHandle(hFileOutRO);
760     CloseHandle(hFileOutWT);
761
762     /* Trying to set SB handles with various access modes */
763     SetLastError(0);
764     ok(!SetConsoleActiveScreenBuffer(hConOutRO), "Shouldn't succeed\n");
765     ok(GetLastError() == ERROR_INVALID_HANDLE,
766        "GetLastError: expecting %u got %u\n",
767        ERROR_INVALID_HANDLE, GetLastError());
768
769     ok(SetConsoleActiveScreenBuffer(hConOutWT), "Couldn't set new WriteOnly SB\n");
770
771     ok(SetConsoleActiveScreenBuffer(hConOutRW), "Couldn't set new ReadWrite SB\n");
772
773     CloseHandle(hConOutWT);
774     CloseHandle(hConOutRO);
775
776     /* Now we have two ReadWrite SB, active must be hConOutRW */
777     /* Open current SB via CONOUT$ */
778     hConOutNew = CreateFileA("CONOUT$", GENERIC_READ|GENERIC_WRITE, 0,
779                              NULL, OPEN_EXISTING, 0, 0);
780     ok(hConOutNew != INVALID_HANDLE_VALUE, "CONOUT$ is not opened\n");
781
782
783     /* test cursor */
784     c.X = c.Y = 10;
785     SetConsoleCursorPosition(hConOut, c);
786     c.X = c.Y = 5;
787     SetConsoleCursorPosition(hConOutRW, c);
788     okCURSOR(hConOutNew, c);
789     c.X = c.Y = 10;
790     okCURSOR(hConOut, c);
791
792
793     c.X = c.Y = 0;
794
795     /* Write using hConOutNew... */
796     SetConsoleCursorPosition(hConOutNew, c);
797     ret = WriteConsoleA(hConOutNew, test_str2, lstrlenA(test_str2), &len, NULL);
798     ok (ret && len == lstrlenA(test_str2), "WriteConsoleA failed\n");
799     /* ... and read it back via hConOutRW */
800     ret = ReadConsoleOutputCharacterA(hConOutRW, str_buf, lstrlenA(test_str2), c, &len);
801     ok(ret && len == lstrlenA(test_str2), "ReadConsoleOutputCharacterA failed\n");
802     str_buf[lstrlenA(test_str2)] = 0;
803     ok(!lstrcmpA(str_buf, test_str2), "got '%s' expected '%s'\n", str_buf, test_str2);
804
805
806     /* Now test output codepage handling. Current is 866 as we set earlier. */
807     SetConsoleCursorPosition(hConOutRW, c);
808     ret = WriteConsoleA(hConOutRW, test_cp866, lstrlenA(test_cp866), &len, NULL);
809     ok(ret && len == lstrlenA(test_cp866), "WriteConsoleA failed\n");
810     ret = ReadConsoleOutputCharacterW(hConOutRW, str_wbuf, lstrlenA(test_cp866), c, &len);
811     ok(ret && len == lstrlenA(test_cp866), "ReadConsoleOutputCharacterW failed\n");
812     str_wbuf[lstrlenA(test_cp866)] = 0;
813     ok(!lstrcmpW(str_wbuf, test_unicode), "string does not match the pattern\n");
814
815     /*
816      * cp866 is OK, let's switch to cp1251.
817      * We expect that this codepage will be used in every SB - active and not.
818      */
819     ok(SetConsoleOutputCP(1251), "Cannot set output cp to 1251\n");
820     SetConsoleCursorPosition(hConOutRW, c);
821     ret = WriteConsoleA(hConOutRW, test_cp1251, lstrlenA(test_cp1251), &len, NULL);
822     ok(ret && len == lstrlenA(test_cp1251), "WriteConsoleA failed\n");
823     ret = ReadConsoleOutputCharacterW(hConOutRW, str_wbuf, lstrlenA(test_cp1251), c, &len);
824     ok(ret && len == lstrlenA(test_cp1251), "ReadConsoleOutputCharacterW failed\n");
825     str_wbuf[lstrlenA(test_cp1251)] = 0;
826     ok(!lstrcmpW(str_wbuf, test_unicode), "string does not match the pattern\n");
827
828     /* Check what has happened to hConOut. */
829     SetConsoleCursorPosition(hConOut, c);
830     ret = WriteConsoleA(hConOut, test_cp1251, lstrlenA(test_cp1251), &len, NULL);
831     ok(ret && len == lstrlenA(test_cp1251), "WriteConsoleA failed\n");
832     ret = ReadConsoleOutputCharacterW(hConOut, str_wbuf, lstrlenA(test_cp1251), c, &len);
833     ok(ret && len == lstrlenA(test_cp1251), "ReadConsoleOutputCharacterW failed\n");
834     str_wbuf[lstrlenA(test_cp1251)] = 0;
835     ok(!lstrcmpW(str_wbuf, test_unicode), "string does not match the pattern\n");
836
837     /* Close all handles of current console SB */
838     CloseHandle(hConOutNew);
839     CloseHandle(hConOutRW);
840
841     /* Now active SB should be hConOut */
842     hConOutNew = CreateFileA("CONOUT$", GENERIC_READ|GENERIC_WRITE, 0,
843                              NULL, OPEN_EXISTING, 0, 0);
844     ok(hConOutNew != INVALID_HANDLE_VALUE, "CONOUT$ is not opened\n");
845
846     /* Write using hConOutNew... */
847     SetConsoleCursorPosition(hConOutNew, c);
848     ret = WriteConsoleA(hConOutNew, test_str1, lstrlenA(test_str1), &len, NULL);
849     ok (ret && len == lstrlenA(test_str1), "WriteConsoleA failed\n");
850     /* ... and read it back via hConOut */
851     ret = ReadConsoleOutputCharacterA(hConOut, str_buf, lstrlenA(test_str1), c, &len);
852     ok(ret && len == lstrlenA(test_str1), "ReadConsoleOutputCharacterA failed\n");
853     str_buf[lstrlenA(test_str1)] = 0;
854     todo_wine ok(!lstrcmpA(str_buf, test_str1), "got '%s' expected '%s'\n", str_buf, test_str1);
855     CloseHandle(hConOutNew);
856
857     /* This is not really needed under Windows */
858     SetConsoleActiveScreenBuffer(hConOut);
859
860     /* restore codepage */
861     SetConsoleOutputCP(oldcp);
862 }
863
864 static void test_GetSetConsoleInputExeName(void)
865 {
866     BOOL ret;
867     DWORD error;
868     char buffer[MAX_PATH], module[MAX_PATH], *p;
869     static char input_exe[MAX_PATH] = "winetest.exe";
870
871     SetLastError(0xdeadbeef);
872     ret = pGetConsoleInputExeNameA(0, NULL);
873     error = GetLastError();
874     ok(ret, "GetConsoleInputExeNameA failed\n");
875     ok(error == ERROR_BUFFER_OVERFLOW, "got %u expected ERROR_BUFFER_OVERFLOW\n", error);
876
877     SetLastError(0xdeadbeef);
878     ret = pGetConsoleInputExeNameA(0, buffer);
879     error = GetLastError();
880     ok(ret, "GetConsoleInputExeNameA failed\n");
881     ok(error == ERROR_BUFFER_OVERFLOW, "got %u expected ERROR_BUFFER_OVERFLOW\n", error);
882
883     GetModuleFileNameA(GetModuleHandle(NULL), module, sizeof(module));
884     p = strrchr(module, '\\') + 1;
885
886     ret = pGetConsoleInputExeNameA(sizeof(buffer)/sizeof(buffer[0]), buffer);
887     ok(ret, "GetConsoleInputExeNameA failed\n");
888     todo_wine ok(!lstrcmpA(buffer, p), "got %s expected %s\n", buffer, p);
889
890     SetLastError(0xdeadbeef);
891     ret = pSetConsoleInputExeNameA(NULL);
892     error = GetLastError();
893     ok(!ret, "SetConsoleInputExeNameA failed\n");
894     ok(error == ERROR_INVALID_PARAMETER, "got %u expected ERROR_INVALID_PARAMETER\n", error);
895
896     SetLastError(0xdeadbeef);
897     ret = pSetConsoleInputExeNameA("");
898     error = GetLastError();
899     ok(!ret, "SetConsoleInputExeNameA failed\n");
900     ok(error == ERROR_INVALID_PARAMETER, "got %u expected ERROR_INVALID_PARAMETER\n", error);
901
902     ret = pSetConsoleInputExeNameA(input_exe);
903     ok(ret, "SetConsoleInputExeNameA failed\n");
904
905     ret = pGetConsoleInputExeNameA(sizeof(buffer)/sizeof(buffer[0]), buffer);
906     ok(ret, "GetConsoleInputExeNameA failed\n");
907     ok(!lstrcmpA(buffer, input_exe), "got %s expected %s\n", buffer, input_exe);
908 }
909
910 START_TEST(console)
911 {
912     HANDLE hConIn, hConOut;
913     BOOL ret;
914     CONSOLE_SCREEN_BUFFER_INFO  sbi;
915
916     init_function_pointers();
917
918     /* be sure we have a clean console (and that's our own)
919      * FIXME: this will make the test fail (currently) if we don't run
920      * under X11
921      * Another solution would be to rerun the test under wineconsole with
922      * the curses backend
923      */
924
925     /* first, we detach and open a fresh console to play with */
926     FreeConsole();
927     ok(AllocConsole(), "Couldn't alloc console\n");
928     hConIn = CreateFileA("CONIN$", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
929     hConOut = CreateFileA("CONOUT$", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
930
931     /* now verify everything's ok */
932     ok(hConIn != INVALID_HANDLE_VALUE, "Opening ConIn\n");
933     ok(hConOut != INVALID_HANDLE_VALUE, "Opening ConOut\n");
934
935     ret = GetConsoleScreenBufferInfo(hConOut, &sbi);
936     ok(ret, "Getting sb info\n");
937     if (!ret) return;
938
939     /* Non interactive tests */
940     testCursor(hConOut, sbi.dwSize);
941     /* test parameters (FIXME: test functionality) */
942     testCursorInfo(hConOut);
943     /* will test wrapped (on/off) & processed (on/off) strings output */
944     testWrite(hConOut, sbi.dwSize);
945     /* will test line scrolling at the bottom of the screen */
946     /* testBottomScroll(); */
947     /* will test all the scrolling operations */
948     testScroll(hConOut, sbi.dwSize);
949     /* will test sb creation / modification / codepage handling */
950     testScreenBuffer(hConOut);
951     testCtrlHandler();
952     /* still to be done: access rights & access on objects */
953
954     if (!pGetConsoleInputExeNameA || !pSetConsoleInputExeNameA)
955     {
956         win_skip("GetConsoleInputExeNameA and/or SetConsoleInputExeNameA is not available\n");
957         return;
958     }
959     else
960         test_GetSetConsoleInputExeName();
961 }