wininet: Keep handles invalid but reserved in InternetCloseHandle.
[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 DWORD (WINAPI *pGetConsoleProcessList)(LPDWORD, DWORD);
28 static HANDLE (WINAPI *pOpenConsoleW)(LPCWSTR,DWORD,BOOL,DWORD);
29 static BOOL (WINAPI *pSetConsoleInputExeNameA)(LPCSTR);
30 static BOOL (WINAPI *pVerifyConsoleIoHandle)(HANDLE handle);
31
32 /* DEFAULT_ATTRIB is used for all initial filling of the console.
33  * all modifications are made with TEST_ATTRIB so that we could check
34  * what has to be modified or not
35  */
36 #define TEST_ATTRIB    (BACKGROUND_BLUE | FOREGROUND_GREEN)
37 #define DEFAULT_ATTRIB (FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED)
38 /* when filling the screen with non-blank chars, this macro defines
39  * what character should be at position 'c'
40  */
41 #define CONTENT(c)    ('A' + (((c).Y * 17 + (c).X) % 23))
42
43 #define okCURSOR(hCon, c) do { \
44   CONSOLE_SCREEN_BUFFER_INFO __sbi; \
45   BOOL expect = GetConsoleScreenBufferInfo((hCon), &__sbi) && \
46                 __sbi.dwCursorPosition.X == (c).X && __sbi.dwCursorPosition.Y == (c).Y; \
47   ok(expect, "Expected cursor at (%d,%d), got (%d,%d)\n", \
48      (c).X, (c).Y, __sbi.dwCursorPosition.X, __sbi.dwCursorPosition.Y); \
49 } while (0)
50
51 #define okCHAR(hCon, c, ch, attr) do { \
52   char __ch; WORD __attr; DWORD __len; BOOL expect; \
53   expect = ReadConsoleOutputCharacter((hCon), &__ch, 1, (c), &__len) == 1 && __len == 1 && __ch == (ch); \
54   ok(expect, "At (%d,%d): expecting char '%c'/%02x got '%c'/%02x\n", (c).X, (c).Y, (ch), (ch), __ch, __ch); \
55   expect = ReadConsoleOutputAttribute((hCon), &__attr, 1, (c), &__len) == 1 && __len == 1 && __attr == (attr); \
56   ok(expect, "At (%d,%d): expecting attr %04x got %04x\n", (c).X, (c).Y, (attr), __attr); \
57 } while (0)
58
59 static void init_function_pointers(void)
60 {
61     HMODULE hKernel32;
62
63 #define KERNEL32_GET_PROC(func)                                     \
64     p##func = (void *)GetProcAddress(hKernel32, #func);             \
65     if(!p##func) trace("GetProcAddress(hKernel32, '%s') failed\n", #func);
66
67     hKernel32 = GetModuleHandleA("kernel32.dll");
68     KERNEL32_GET_PROC(GetConsoleInputExeNameA);
69     KERNEL32_GET_PROC(GetConsoleProcessList);
70     KERNEL32_GET_PROC(OpenConsoleW);
71     KERNEL32_GET_PROC(SetConsoleInputExeNameA);
72     KERNEL32_GET_PROC(VerifyConsoleIoHandle);
73
74 #undef KERNEL32_GET_PROC
75 }
76
77 /* FIXME: this could be optimized on a speed point of view */
78 static void resetContent(HANDLE hCon, COORD sbSize, BOOL content)
79 {
80     COORD       c;
81     WORD        attr = DEFAULT_ATTRIB;
82     char        ch;
83     DWORD       len;
84
85     for (c.X = 0; c.X < sbSize.X; c.X++)
86     {
87         for (c.Y = 0; c.Y < sbSize.Y; c.Y++)
88         {
89             ch = (content) ? CONTENT(c) : ' ';
90             WriteConsoleOutputAttribute(hCon, &attr, 1, c, &len);
91             WriteConsoleOutputCharacterA(hCon, &ch, 1, c, &len);
92         }
93     }
94 }
95
96 static void testCursor(HANDLE hCon, COORD sbSize)
97 {
98     COORD               c;
99
100     c.X = c.Y = 0;
101     ok(SetConsoleCursorPosition(0, c) == 0, "No handle\n");
102     ok(GetLastError() == ERROR_INVALID_HANDLE, "GetLastError: expecting %u got %u\n",
103        ERROR_INVALID_HANDLE, GetLastError());
104
105     c.X = c.Y = 0;
106     ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left\n");
107     okCURSOR(hCon, c);
108
109     c.X = sbSize.X - 1;
110     c.Y = sbSize.Y - 1;
111     ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in lower-right\n");
112     okCURSOR(hCon, c);
113
114     c.X = sbSize.X;
115     c.Y = sbSize.Y - 1;
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 = sbSize.X - 1;
121     c.Y = sbSize.Y;
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 = -1;
127     c.Y = 0;
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     c.X = 0;
133     c.Y = -1;
134     ok(SetConsoleCursorPosition(hCon, c) == 0, "Cursor is outside\n");
135     ok(GetLastError() == ERROR_INVALID_PARAMETER, "GetLastError: expecting %u got %u\n",
136        ERROR_INVALID_PARAMETER, GetLastError());
137 }
138
139 static void testCursorInfo(HANDLE hCon)
140 {
141     BOOL ret;
142     CONSOLE_CURSOR_INFO info;
143
144     SetLastError(0xdeadbeef);
145     ret = GetConsoleCursorInfo(NULL, NULL);
146     ok(!ret, "Expected failure\n");
147     ok(GetLastError() == ERROR_INVALID_HANDLE, "GetLastError: expecting %u got %u\n",
148        ERROR_INVALID_HANDLE, GetLastError());
149
150     SetLastError(0xdeadbeef);
151     info.dwSize = -1;
152     ret = GetConsoleCursorInfo(NULL, &info);
153     ok(!ret, "Expected failure\n");
154     ok(info.dwSize == -1, "Expected no change for dwSize\n");
155     ok(GetLastError() == ERROR_INVALID_HANDLE, "GetLastError: expecting %u got %u\n",
156        ERROR_INVALID_HANDLE, GetLastError());
157
158     /* Test the correct call first to distinguish between win9x and the rest */
159     SetLastError(0xdeadbeef);
160     ret = GetConsoleCursorInfo(hCon, &info);
161     ok(ret, "Expected success\n");
162     ok(info.dwSize == 25 ||
163        info.dwSize == 12 /* win9x */,
164        "Expected 12 or 25, got %d\n", info.dwSize);
165     ok(info.bVisible, "Expected the cursor to be visible\n");
166     ok(GetLastError() == 0xdeadbeef, "GetLastError: expecting %u got %u\n",
167        0xdeadbeef, GetLastError());
168
169     /* Don't test NULL CONSOLE_CURSOR_INFO, it crashes on win9x and win7 */
170 }
171
172 static void testEmptyWrite(HANDLE hCon)
173 {
174     static const char   emptybuf[16];
175     COORD               c;
176     DWORD               len;
177
178     c.X = c.Y = 0;
179     ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left\n");
180
181     len = -1;
182     ok(WriteConsole(hCon, NULL, 0, &len, NULL) != 0 && len == 0, "WriteConsole\n");
183     okCURSOR(hCon, c);
184
185     /* Passing a NULL lpBuffer with sufficiently large non-zero length succeeds
186      * on native Windows and result in memory-like contents being written to
187      * the console. Calling WriteConsoleW like this will crash on Wine. */
188     if (0)
189     {
190         len = -1;
191         ok(!WriteConsole(hCon, NULL, 16, &len, NULL) && len == -1, "WriteConsole\n");
192         okCURSOR(hCon, c);
193
194         /* Cursor advances for this call. */
195         len = -1;
196         ok(WriteConsole(hCon, NULL, 128, &len, NULL) != 0 && len == 128, "WriteConsole\n");
197     }
198
199     len = -1;
200     ok(WriteConsole(hCon, emptybuf, 0, &len, NULL) != 0 && len == 0, "WriteConsole\n");
201     okCURSOR(hCon, c);
202
203     /* WriteConsole does not halt on a null terminator and is happy to write
204      * memory contents beyond the actual size of the buffer. */
205     len = -1;
206     ok(WriteConsole(hCon, emptybuf, 16, &len, NULL) != 0 && len == 16, "WriteConsole\n");
207     c.X += 16;
208     okCURSOR(hCon, c);
209 }
210
211 static void testWriteSimple(HANDLE hCon)
212 {
213     COORD               c;
214     DWORD               len;
215     const char*         mytest = "abcdefg";
216     const int   mylen = strlen(mytest);
217
218     /* single line write */
219     c.X = c.Y = 0;
220     ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left\n");
221
222     ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
223     c.Y = 0;
224     for (c.X = 0; c.X < mylen; c.X++)
225     {
226         okCHAR(hCon, c, mytest[c.X], TEST_ATTRIB);
227     }
228
229     okCURSOR(hCon, c);
230     okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
231 }
232
233 static void testWriteNotWrappedNotProcessed(HANDLE hCon, COORD sbSize)
234 {
235     COORD               c;
236     DWORD               len, mode;
237     const char*         mytest = "123";
238     const int           mylen = strlen(mytest);
239     int                 ret;
240     int                 p;
241
242     ok(GetConsoleMode(hCon, &mode) && SetConsoleMode(hCon, mode & ~(ENABLE_PROCESSED_OUTPUT|ENABLE_WRAP_AT_EOL_OUTPUT)),
243        "clearing wrap at EOL & processed output\n");
244
245     /* write line, wrapping disabled, buffer exceeds sb width */
246     c.X = sbSize.X - 3; c.Y = 0;
247     ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-3\n");
248
249     ret = WriteConsole(hCon, mytest, mylen, &len, NULL);
250     ok(ret != 0 && len == mylen, "Couldn't write, ret = %d, len = %d\n", ret, len);
251     c.Y = 0;
252     for (p = mylen - 3; p < mylen; p++)
253     {
254         c.X = sbSize.X - 3 + p % 3;
255         okCHAR(hCon, c, mytest[p], TEST_ATTRIB);
256     }
257
258     c.X = 0; c.Y = 1;
259     okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
260
261     p = sbSize.X - 3 + mylen % 3;
262     c.X = p; c.Y = 0;
263
264     /* write line, wrapping disabled, strings end on end of line */
265     c.X = sbSize.X - mylen; c.Y = 0;
266     ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-3\n");
267
268     ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
269 }
270
271 static void testWriteNotWrappedProcessed(HANDLE hCon, COORD sbSize)
272 {
273     COORD               c;
274     DWORD               len, mode;
275     const char*         mytest = "abcd\nf\tg";
276     const int   mylen = strlen(mytest);
277     const int   mylen2 = strchr(mytest, '\n') - mytest;
278     int                 p;
279     WORD                attr;
280
281     ok(GetConsoleMode(hCon, &mode) && SetConsoleMode(hCon, (mode | ENABLE_PROCESSED_OUTPUT) & ~ENABLE_WRAP_AT_EOL_OUTPUT),
282        "clearing wrap at EOL & setting processed output\n");
283
284     /* write line, wrapping disabled, buffer exceeds sb width */
285     c.X = sbSize.X - 5; c.Y = 0;
286     ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-5\n");
287
288     ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
289     c.Y = 0;
290     for (c.X = sbSize.X - 5; c.X < sbSize.X - 1; c.X++)
291     {
292         okCHAR(hCon, c, mytest[c.X - sbSize.X + 5], TEST_ATTRIB);
293     }
294
295     ReadConsoleOutputAttribute(hCon, &attr, 1, c, &len);
296     /* Win9x and WinMe change the attribs for '\n' up to 'f' */
297     if (attr == TEST_ATTRIB)
298     {
299         win_skip("Win9x/WinMe don't respect ~ENABLE_WRAP_AT_EOL_OUTPUT\n");
300         return;
301     }
302
303     okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
304
305     c.X = 0; c.Y++;
306     okCHAR(hCon, c, mytest[5], TEST_ATTRIB);
307     for (c.X = 1; c.X < 8; c.X++)
308         okCHAR(hCon, c, ' ', TEST_ATTRIB);
309     okCHAR(hCon, c, mytest[7], TEST_ATTRIB);
310     c.X++;
311     okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
312
313     okCURSOR(hCon, c);
314
315     /* write line, wrapping disabled, strings end on end of line */
316     c.X = sbSize.X - 4; c.Y = 0;
317     ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-4\n");
318
319     ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
320     c.Y = 0;
321     for (c.X = sbSize.X - 4; c.X < sbSize.X; c.X++)
322     {
323         okCHAR(hCon, c, mytest[c.X - sbSize.X + 4], TEST_ATTRIB);
324     }
325     c.X = 0; c.Y++;
326     okCHAR(hCon, c, mytest[5], TEST_ATTRIB);
327     for (c.X = 1; c.X < 8; c.X++)
328         okCHAR(hCon, c, ' ', TEST_ATTRIB);
329     okCHAR(hCon, c, mytest[7], TEST_ATTRIB);
330     c.X++;
331     okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
332
333     okCURSOR(hCon, c);
334
335     /* write line, wrapping disabled, strings end after end of line */
336     c.X = sbSize.X - 3; c.Y = 0;
337     ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-4\n");
338
339     ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
340     c.Y = 0;
341     for (p = mylen2 - 3; p < mylen2; p++)
342     {
343         c.X = sbSize.X - 3 + p % 3;
344         okCHAR(hCon, c, mytest[p], TEST_ATTRIB);
345     }
346     c.X = 0; c.Y = 1;
347     okCHAR(hCon, c, mytest[5], TEST_ATTRIB);
348     for (c.X = 1; c.X < 8; c.X++)
349         okCHAR(hCon, c, ' ', TEST_ATTRIB);
350     okCHAR(hCon, c, mytest[7], TEST_ATTRIB);
351     c.X++;
352     okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
353
354     okCURSOR(hCon, c);
355 }
356
357 static void testWriteWrappedNotProcessed(HANDLE hCon, COORD sbSize)
358 {
359     COORD               c;
360     DWORD               len, mode;
361     const char*         mytest = "abcd\nf\tg";
362     const int   mylen = strlen(mytest);
363     int                 p;
364
365     ok(GetConsoleMode(hCon, &mode) && SetConsoleMode(hCon,(mode | ENABLE_WRAP_AT_EOL_OUTPUT) & ~(ENABLE_PROCESSED_OUTPUT)),
366        "setting wrap at EOL & clearing processed output\n");
367
368     /* write line, wrapping enabled, buffer doesn't exceed sb width */
369     c.X = sbSize.X - 9; c.Y = 0;
370     ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-9\n");
371
372     ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
373     c.Y = 0;
374     for (p = 0; p < mylen; p++)
375     {
376         c.X = sbSize.X - 9 + p;
377         okCHAR(hCon, c, mytest[p], TEST_ATTRIB);
378     }
379     c.X = sbSize.X - 9 + mylen;
380     okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
381     c.X = 0; c.Y = 1;
382     okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
383
384     /* write line, wrapping enabled, buffer does exceed sb width */
385     c.X = sbSize.X - 3; c.Y = 0;
386     ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-3\n");
387
388     c.Y = 1;
389     c.X = mylen - 3;
390     okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
391 }
392
393 static void testWriteWrappedProcessed(HANDLE hCon, COORD sbSize)
394 {
395     COORD               c;
396     DWORD               len, mode;
397     const char*         mytest = "abcd\nf\tg";
398     const int   mylen = strlen(mytest);
399     int                 p;
400     WORD                attr;
401
402     ok(GetConsoleMode(hCon, &mode) && SetConsoleMode(hCon, mode | (ENABLE_WRAP_AT_EOL_OUTPUT|ENABLE_PROCESSED_OUTPUT)),
403        "setting wrap at EOL & processed output\n");
404
405     /* write line, wrapping enabled, buffer doesn't exceed sb width */
406     c.X = sbSize.X - 9; c.Y = 0;
407     ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-9\n");
408
409     ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
410     for (p = 0; p < 4; p++)
411     {
412         c.X = sbSize.X - 9 + p;
413         okCHAR(hCon, c, mytest[p], TEST_ATTRIB);
414     }
415     c.X = sbSize.X - 9 + p;
416     ReadConsoleOutputAttribute(hCon, &attr, 1, c, &len);
417     if (attr == TEST_ATTRIB)
418         win_skip("Win9x/WinMe changes attribs for '\\n' up to 'f'\n");
419     else
420         okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
421     c.X = 0; c.Y++;
422     okCHAR(hCon, c, mytest[5], TEST_ATTRIB);
423     for (c.X = 1; c.X < 8; c.X++)
424         okCHAR(hCon, c, ' ', TEST_ATTRIB);
425     okCHAR(hCon, c, mytest[7], TEST_ATTRIB);
426     c.X++;
427     okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
428     okCURSOR(hCon, c);
429
430     /* write line, wrapping enabled, buffer does exceed sb width */
431     c.X = sbSize.X - 3; c.Y = 2;
432     ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-3\n");
433
434     ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
435     for (p = 0; p < 3; p++)
436     {
437         c.X = sbSize.X - 3 + p;
438         okCHAR(hCon, c, mytest[p], TEST_ATTRIB);
439     }
440     c.X = 0; c.Y++;
441     okCHAR(hCon, c, mytest[3], TEST_ATTRIB);
442     c.X++;
443     ReadConsoleOutputAttribute(hCon, &attr, 1, c, &len);
444     if (attr == TEST_ATTRIB)
445         win_skip("Win9x/WinMe changes attribs for '\\n' up to 'f'\n");
446     else
447         okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
448
449     c.X = 0; c.Y++;
450     okCHAR(hCon, c, mytest[5], TEST_ATTRIB);
451     for (c.X = 1; c.X < 8; c.X++)
452         okCHAR(hCon, c, ' ', TEST_ATTRIB);
453     okCHAR(hCon, c, mytest[7], TEST_ATTRIB);
454     c.X++;
455     okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
456     okCURSOR(hCon, c);
457 }
458
459 static void testWrite(HANDLE hCon, COORD sbSize)
460 {
461     /* FIXME: should in fact insure that the sb is at least 10 character wide */
462     ok(SetConsoleTextAttribute(hCon, TEST_ATTRIB), "Setting default text color\n");
463     resetContent(hCon, sbSize, FALSE);
464     testEmptyWrite(hCon);
465     resetContent(hCon, sbSize, FALSE);
466     testWriteSimple(hCon);
467     resetContent(hCon, sbSize, FALSE);
468     testWriteNotWrappedNotProcessed(hCon, sbSize);
469     resetContent(hCon, sbSize, FALSE);
470     testWriteNotWrappedProcessed(hCon, sbSize);
471     resetContent(hCon, sbSize, FALSE);
472     testWriteWrappedNotProcessed(hCon, sbSize);
473     resetContent(hCon, sbSize, FALSE);
474     testWriteWrappedProcessed(hCon, sbSize);
475 }
476
477 static void testScroll(HANDLE hCon, COORD sbSize)
478 {
479     SMALL_RECT  scroll, clip;
480     COORD       dst, c, tc;
481     CHAR_INFO   ci;
482     BOOL ret;
483
484 #define W 11
485 #define H 7
486
487 #define IN_SRECT(r,c) ((r).Left <= (c).X && (c).X <= (r).Right && (r).Top <= (c).Y && (c).Y <= (r).Bottom)
488 #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)
489
490     /* no clipping, src & dst rect don't overlap */
491     resetContent(hCon, sbSize, TRUE);
492
493     scroll.Left = 0;
494     scroll.Right = W - 1;
495     scroll.Top = 0;
496     scroll.Bottom = H - 1;
497     dst.X = W + 3;
498     dst.Y = H + 3;
499     ci.Char.UnicodeChar = '#';
500     ci.Attributes = TEST_ATTRIB;
501
502     clip.Left = 0;
503     clip.Right = sbSize.X - 1;
504     clip.Top = 0;
505     clip.Bottom = sbSize.Y - 1;
506
507     ok(ScrollConsoleScreenBuffer(hCon, &scroll, NULL, dst, &ci), "Scrolling SB\n");
508
509     for (c.Y = 0; c.Y < sbSize.Y; c.Y++)
510     {
511         for (c.X = 0; c.X < sbSize.X; c.X++)
512         {
513             if (IN_SRECT2(scroll, dst, c) && IN_SRECT(clip, c))
514             {
515                 tc.X = c.X - dst.X;
516                 tc.Y = c.Y - dst.Y;
517                 okCHAR(hCon, c, CONTENT(tc), DEFAULT_ATTRIB);
518             }
519             else if (IN_SRECT(scroll, c) && IN_SRECT(clip, c))
520                 okCHAR(hCon, c, '#', TEST_ATTRIB);
521             else okCHAR(hCon, c, CONTENT(c), DEFAULT_ATTRIB);
522         }
523     }
524
525     /* no clipping, src & dst rect do overlap */
526     resetContent(hCon, sbSize, TRUE);
527
528     scroll.Left = 0;
529     scroll.Right = W - 1;
530     scroll.Top = 0;
531     scroll.Bottom = H - 1;
532     dst.X = W /2;
533     dst.Y = H / 2;
534     ci.Char.UnicodeChar = '#';
535     ci.Attributes = TEST_ATTRIB;
536
537     clip.Left = 0;
538     clip.Right = sbSize.X - 1;
539     clip.Top = 0;
540     clip.Bottom = sbSize.Y - 1;
541
542     ok(ScrollConsoleScreenBuffer(hCon, &scroll, NULL, dst, &ci), "Scrolling SB\n");
543
544     for (c.Y = 0; c.Y < sbSize.Y; c.Y++)
545     {
546         for (c.X = 0; c.X < sbSize.X; c.X++)
547         {
548             if (dst.X <= c.X && c.X < dst.X + W && dst.Y <= c.Y && c.Y < dst.Y + H)
549             {
550                 tc.X = c.X - dst.X;
551                 tc.Y = c.Y - dst.Y;
552                 okCHAR(hCon, c, CONTENT(tc), DEFAULT_ATTRIB);
553             }
554             else if (c.X < W && c.Y < H) okCHAR(hCon, c, '#', TEST_ATTRIB);
555             else okCHAR(hCon, c, CONTENT(c), DEFAULT_ATTRIB);
556         }
557     }
558
559     /* clipping, src & dst rect don't overlap */
560     resetContent(hCon, sbSize, TRUE);
561
562     scroll.Left = 0;
563     scroll.Right = W - 1;
564     scroll.Top = 0;
565     scroll.Bottom = H - 1;
566     dst.X = W + 3;
567     dst.Y = H + 3;
568     ci.Char.UnicodeChar = '#';
569     ci.Attributes = TEST_ATTRIB;
570
571     clip.Left = W / 2;
572     clip.Right = min(W + W / 2, sbSize.X - 1);
573     clip.Top = H / 2;
574     clip.Bottom = min(H + H / 2, sbSize.Y - 1);
575
576     SetLastError(0xdeadbeef);
577     ret = ScrollConsoleScreenBuffer(hCon, &scroll, &clip, dst, &ci);
578     if (ret)
579     {
580         for (c.Y = 0; c.Y < sbSize.Y; c.Y++)
581         {
582             for (c.X = 0; c.X < sbSize.X; c.X++)
583             {
584                 if (IN_SRECT2(scroll, dst, c) && IN_SRECT(clip, c))
585                 {
586                     tc.X = c.X - dst.X;
587                     tc.Y = c.Y - dst.Y;
588                     okCHAR(hCon, c, CONTENT(tc), DEFAULT_ATTRIB);
589                 }
590                 else if (IN_SRECT(scroll, c) && IN_SRECT(clip, c))
591                     okCHAR(hCon, c, '#', TEST_ATTRIB);
592                 else okCHAR(hCon, c, CONTENT(c), DEFAULT_ATTRIB);
593             }
594         }
595     }
596     else
597     {
598         /* Win9x will fail, Only accept ERROR_NOT_ENOUGH_MEMORY */
599         ok(GetLastError() == ERROR_NOT_ENOUGH_MEMORY,
600             "Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
601     }
602
603     /* clipping, src & dst rect do overlap */
604     resetContent(hCon, sbSize, TRUE);
605
606     scroll.Left = 0;
607     scroll.Right = W - 1;
608     scroll.Top = 0;
609     scroll.Bottom = H - 1;
610     dst.X = W / 2 - 3;
611     dst.Y = H / 2 - 3;
612     ci.Char.UnicodeChar = '#';
613     ci.Attributes = TEST_ATTRIB;
614
615     clip.Left = W / 2;
616     clip.Right = min(W + W / 2, sbSize.X - 1);
617     clip.Top = H / 2;
618     clip.Bottom = min(H + H / 2, sbSize.Y - 1);
619
620     ok(ScrollConsoleScreenBuffer(hCon, &scroll, &clip, dst, &ci), "Scrolling SB\n");
621
622     for (c.Y = 0; c.Y < sbSize.Y; c.Y++)
623     {
624         for (c.X = 0; c.X < sbSize.X; c.X++)
625         {
626             if (IN_SRECT2(scroll, dst, c) && IN_SRECT(clip, c))
627             {
628                 tc.X = c.X - dst.X;
629                 tc.Y = c.Y - dst.Y;
630                 okCHAR(hCon, c, CONTENT(tc), DEFAULT_ATTRIB);
631             }
632             else if (IN_SRECT(scroll, c) && IN_SRECT(clip, c))
633                 okCHAR(hCon, c, '#', TEST_ATTRIB);
634             else okCHAR(hCon, c, CONTENT(c), DEFAULT_ATTRIB);
635         }
636     }
637 }
638
639 static int mch_count;
640 /* we need the event as Wine console event generation isn't synchronous
641  * (ie GenerateConsoleCtrlEvent returns before all ctrl-handlers in all
642  * processes have been called).
643  */
644 static HANDLE mch_event;
645 static BOOL WINAPI mch(DWORD event)
646 {
647     mch_count++;
648     SetEvent(mch_event);
649     return TRUE;
650 }
651
652 static void testCtrlHandler(void)
653 {
654     ok(!SetConsoleCtrlHandler(mch, FALSE), "Shouldn't succeed\n");
655     ok(GetLastError() == ERROR_INVALID_PARAMETER, "Bad error %u\n", GetLastError());
656     ok(SetConsoleCtrlHandler(mch, TRUE), "Couldn't set handler\n");
657     /* wine requires the event for the test, as we cannot insure, so far, that event
658      * are processed synchronously in GenerateConsoleCtrlEvent()
659      */
660     mch_event = CreateEventA(NULL, TRUE, FALSE, NULL);
661     mch_count = 0;
662     ok(GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0), "Couldn't send ctrl-c event\n");
663     /* FIXME: it isn't synchronous on wine but it can still happen before we test */
664     if (0) ok(mch_count == 1, "Event isn't synchronous\n");
665     ok(WaitForSingleObject(mch_event, 3000) == WAIT_OBJECT_0, "event sending didn't work\n");
666     CloseHandle(mch_event);
667
668     /* Turning off ctrl-c handling doesn't work on win9x such way ... */
669     ok(SetConsoleCtrlHandler(NULL, TRUE), "Couldn't turn off ctrl-c handling\n");
670     mch_event = CreateEventA(NULL, TRUE, FALSE, NULL);
671     mch_count = 0;
672     if(!(GetVersion() & 0x80000000))
673         /* ... and next line leads to an unhandled exception on 9x.  Avoid it on 9x. */
674         ok(GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0), "Couldn't send ctrl-c event\n");
675     ok(WaitForSingleObject(mch_event, 3000) == WAIT_TIMEOUT && mch_count == 0, "Event shouldn't have been sent\n");
676     CloseHandle(mch_event);
677     ok(SetConsoleCtrlHandler(mch, FALSE), "Couldn't remove handler\n");
678     ok(!SetConsoleCtrlHandler(mch, FALSE), "Shouldn't succeed\n");
679     ok(GetLastError() == ERROR_INVALID_PARAMETER, "Bad error %u\n", GetLastError());
680 }
681
682 /*
683  * Test console screen buffer:
684  * 1) Try to set invalid handle.
685  * 2) Try to set non-console handles.
686  * 3) Use CONOUT$ file as active SB.
687  * 4) Test cursor.
688  * 5) Test output codepage to show it is not a property of SB.
689  * 6) Test switching to old SB if we close all handles to current SB - works
690  * in Windows, TODO in wine.
691  *
692  * What is not tested but should be:
693  * 1) ScreenBufferInfo
694  */
695 static void testScreenBuffer(HANDLE hConOut)
696 {
697     HANDLE hConOutRW, hConOutRO, hConOutWT;
698     HANDLE hFileOutRW, hFileOutRO, hFileOutWT;
699     HANDLE hConOutNew;
700     char test_str1[] = "Test for SB1";
701     char test_str2[] = "Test for SB2";
702     char test_cp866[] = {0xe2, 0xa5, 0xe1, 0xe2, 0};
703     char test_cp1251[] = {0xf2, 0xe5, 0xf1, 0xf2, 0};
704     WCHAR test_unicode[] = {0x0442, 0x0435, 0x0441, 0x0442, 0};
705     WCHAR str_wbuf[20];
706     char str_buf[20];
707     DWORD len;
708     COORD c;
709     BOOL ret;
710     DWORD oldcp;
711
712     if (!IsValidCodePage(866))
713     {
714         skip("Codepage 866 not available\n");
715         return;
716     }
717
718     /* In the beginning set output codepage to 866 */
719     oldcp = GetConsoleOutputCP();
720     SetLastError(0xdeadbeef);
721     ret = SetConsoleOutputCP(866);
722     if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
723     {
724         win_skip("SetConsoleOutputCP is not implemented\n");
725         return;
726     }
727     ok(ret, "Cannot set output codepage to 866\n");
728
729     hConOutRW = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE,
730                          FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
731                          CONSOLE_TEXTMODE_BUFFER, NULL);
732     ok(hConOutRW != INVALID_HANDLE_VALUE,
733        "Cannot create a new screen buffer for ReadWrite\n");
734     hConOutRO = CreateConsoleScreenBuffer(GENERIC_READ,
735                          FILE_SHARE_READ, NULL,
736                          CONSOLE_TEXTMODE_BUFFER, NULL);
737     ok(hConOutRO != INVALID_HANDLE_VALUE,
738        "Cannot create a new screen buffer for ReadOnly\n");
739     hConOutWT = CreateConsoleScreenBuffer(GENERIC_WRITE,
740                          FILE_SHARE_WRITE, NULL,
741                          CONSOLE_TEXTMODE_BUFFER, NULL);
742     ok(hConOutWT != INVALID_HANDLE_VALUE,
743        "Cannot create a new screen buffer for WriteOnly\n");
744
745     hFileOutRW = CreateFileA("NUL", GENERIC_READ | GENERIC_WRITE,
746                              FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
747                              OPEN_EXISTING, 0, NULL);
748     ok(hFileOutRW != INVALID_HANDLE_VALUE, "Cannot open NUL for ReadWrite\n");
749     hFileOutRO = CreateFileA("NUL", GENERIC_READ, FILE_SHARE_READ,
750                              NULL, OPEN_EXISTING, 0, NULL);
751     ok(hFileOutRO != INVALID_HANDLE_VALUE, "Cannot open NUL for ReadOnly\n");
752     hFileOutWT = CreateFileA("NUL", GENERIC_WRITE, FILE_SHARE_WRITE,
753                              NULL, OPEN_EXISTING, 0, NULL);
754     ok(hFileOutWT != INVALID_HANDLE_VALUE, "Cannot open NUL for WriteOnly\n");
755
756     /* Trying to set invalid handle */
757     SetLastError(0);
758     ok(!SetConsoleActiveScreenBuffer(INVALID_HANDLE_VALUE),
759        "Shouldn't succeed\n");
760     ok(GetLastError() == ERROR_INVALID_HANDLE,
761        "GetLastError: expecting %u got %u\n",
762        ERROR_INVALID_HANDLE, GetLastError());
763
764     /* Trying to set non-console handles */
765     SetLastError(0);
766     ok(!SetConsoleActiveScreenBuffer(hFileOutRW), "Shouldn't succeed\n");
767     ok(GetLastError() == ERROR_INVALID_HANDLE,
768        "GetLastError: expecting %u got %u\n",
769        ERROR_INVALID_HANDLE, GetLastError());
770
771     SetLastError(0);
772     ok(!SetConsoleActiveScreenBuffer(hFileOutRO), "Shouldn't succeed\n");
773     ok(GetLastError() == ERROR_INVALID_HANDLE,
774        "GetLastError: expecting %u got %u\n",
775        ERROR_INVALID_HANDLE, GetLastError());
776
777     SetLastError(0);
778     ok(!SetConsoleActiveScreenBuffer(hFileOutWT), "Shouldn't succeed\n");
779     ok(GetLastError() == ERROR_INVALID_HANDLE,
780        "GetLastError: expecting %u got %u\n",
781        ERROR_INVALID_HANDLE, GetLastError());
782
783     CloseHandle(hFileOutRW);
784     CloseHandle(hFileOutRO);
785     CloseHandle(hFileOutWT);
786
787     /* Trying to set SB handles with various access modes */
788     SetLastError(0);
789     ok(!SetConsoleActiveScreenBuffer(hConOutRO), "Shouldn't succeed\n");
790     ok(GetLastError() == ERROR_INVALID_HANDLE,
791        "GetLastError: expecting %u got %u\n",
792        ERROR_INVALID_HANDLE, GetLastError());
793
794     ok(SetConsoleActiveScreenBuffer(hConOutWT), "Couldn't set new WriteOnly SB\n");
795
796     ok(SetConsoleActiveScreenBuffer(hConOutRW), "Couldn't set new ReadWrite SB\n");
797
798     CloseHandle(hConOutWT);
799     CloseHandle(hConOutRO);
800
801     /* Now we have two ReadWrite SB, active must be hConOutRW */
802     /* Open current SB via CONOUT$ */
803     hConOutNew = CreateFileA("CONOUT$", GENERIC_READ|GENERIC_WRITE, 0,
804                              NULL, OPEN_EXISTING, 0, 0);
805     ok(hConOutNew != INVALID_HANDLE_VALUE, "CONOUT$ is not opened\n");
806
807
808     /* test cursor */
809     c.X = c.Y = 10;
810     SetConsoleCursorPosition(hConOut, c);
811     c.X = c.Y = 5;
812     SetConsoleCursorPosition(hConOutRW, c);
813     okCURSOR(hConOutNew, c);
814     c.X = c.Y = 10;
815     okCURSOR(hConOut, c);
816
817
818     c.X = c.Y = 0;
819
820     /* Write using hConOutNew... */
821     SetConsoleCursorPosition(hConOutNew, c);
822     ret = WriteConsoleA(hConOutNew, test_str2, lstrlenA(test_str2), &len, NULL);
823     ok (ret && len == lstrlenA(test_str2), "WriteConsoleA failed\n");
824     /* ... and read it back via hConOutRW */
825     ret = ReadConsoleOutputCharacterA(hConOutRW, str_buf, lstrlenA(test_str2), c, &len);
826     ok(ret && len == lstrlenA(test_str2), "ReadConsoleOutputCharacterA failed\n");
827     str_buf[lstrlenA(test_str2)] = 0;
828     ok(!lstrcmpA(str_buf, test_str2), "got '%s' expected '%s'\n", str_buf, test_str2);
829
830
831     /* Now test output codepage handling. Current is 866 as we set earlier. */
832     SetConsoleCursorPosition(hConOutRW, c);
833     ret = WriteConsoleA(hConOutRW, test_cp866, lstrlenA(test_cp866), &len, NULL);
834     ok(ret && len == lstrlenA(test_cp866), "WriteConsoleA failed\n");
835     ret = ReadConsoleOutputCharacterW(hConOutRW, str_wbuf, lstrlenA(test_cp866), c, &len);
836     ok(ret && len == lstrlenA(test_cp866), "ReadConsoleOutputCharacterW failed\n");
837     str_wbuf[lstrlenA(test_cp866)] = 0;
838     ok(!lstrcmpW(str_wbuf, test_unicode), "string does not match the pattern\n");
839
840     /*
841      * cp866 is OK, let's switch to cp1251.
842      * We expect that this codepage will be used in every SB - active and not.
843      */
844     ok(SetConsoleOutputCP(1251), "Cannot set output cp to 1251\n");
845     SetConsoleCursorPosition(hConOutRW, c);
846     ret = WriteConsoleA(hConOutRW, test_cp1251, lstrlenA(test_cp1251), &len, NULL);
847     ok(ret && len == lstrlenA(test_cp1251), "WriteConsoleA failed\n");
848     ret = ReadConsoleOutputCharacterW(hConOutRW, str_wbuf, lstrlenA(test_cp1251), c, &len);
849     ok(ret && len == lstrlenA(test_cp1251), "ReadConsoleOutputCharacterW failed\n");
850     str_wbuf[lstrlenA(test_cp1251)] = 0;
851     ok(!lstrcmpW(str_wbuf, test_unicode), "string does not match the pattern\n");
852
853     /* Check what has happened to hConOut. */
854     SetConsoleCursorPosition(hConOut, c);
855     ret = WriteConsoleA(hConOut, test_cp1251, lstrlenA(test_cp1251), &len, NULL);
856     ok(ret && len == lstrlenA(test_cp1251), "WriteConsoleA failed\n");
857     ret = ReadConsoleOutputCharacterW(hConOut, str_wbuf, lstrlenA(test_cp1251), c, &len);
858     ok(ret && len == lstrlenA(test_cp1251), "ReadConsoleOutputCharacterW failed\n");
859     str_wbuf[lstrlenA(test_cp1251)] = 0;
860     ok(!lstrcmpW(str_wbuf, test_unicode), "string does not match the pattern\n");
861
862     /* Close all handles of current console SB */
863     CloseHandle(hConOutNew);
864     CloseHandle(hConOutRW);
865
866     /* Now active SB should be hConOut */
867     hConOutNew = CreateFileA("CONOUT$", GENERIC_READ|GENERIC_WRITE, 0,
868                              NULL, OPEN_EXISTING, 0, 0);
869     ok(hConOutNew != INVALID_HANDLE_VALUE, "CONOUT$ is not opened\n");
870
871     /* Write using hConOutNew... */
872     SetConsoleCursorPosition(hConOutNew, c);
873     ret = WriteConsoleA(hConOutNew, test_str1, lstrlenA(test_str1), &len, NULL);
874     ok (ret && len == lstrlenA(test_str1), "WriteConsoleA failed\n");
875     /* ... and read it back via hConOut */
876     ret = ReadConsoleOutputCharacterA(hConOut, str_buf, lstrlenA(test_str1), c, &len);
877     ok(ret && len == lstrlenA(test_str1), "ReadConsoleOutputCharacterA failed\n");
878     str_buf[lstrlenA(test_str1)] = 0;
879     todo_wine ok(!lstrcmpA(str_buf, test_str1), "got '%s' expected '%s'\n", str_buf, test_str1);
880     CloseHandle(hConOutNew);
881
882     /* This is not really needed under Windows */
883     SetConsoleActiveScreenBuffer(hConOut);
884
885     /* restore codepage */
886     SetConsoleOutputCP(oldcp);
887 }
888
889 static void test_GetSetConsoleInputExeName(void)
890 {
891     BOOL ret;
892     DWORD error;
893     char buffer[MAX_PATH], module[MAX_PATH], *p;
894     static char input_exe[MAX_PATH] = "winetest.exe";
895
896     SetLastError(0xdeadbeef);
897     ret = pGetConsoleInputExeNameA(0, NULL);
898     error = GetLastError();
899     ok(ret, "GetConsoleInputExeNameA failed\n");
900     ok(error == ERROR_BUFFER_OVERFLOW, "got %u expected ERROR_BUFFER_OVERFLOW\n", error);
901
902     SetLastError(0xdeadbeef);
903     ret = pGetConsoleInputExeNameA(0, buffer);
904     error = GetLastError();
905     ok(ret, "GetConsoleInputExeNameA failed\n");
906     ok(error == ERROR_BUFFER_OVERFLOW, "got %u expected ERROR_BUFFER_OVERFLOW\n", error);
907
908     GetModuleFileNameA(GetModuleHandle(NULL), module, sizeof(module));
909     p = strrchr(module, '\\') + 1;
910
911     ret = pGetConsoleInputExeNameA(sizeof(buffer)/sizeof(buffer[0]), buffer);
912     ok(ret, "GetConsoleInputExeNameA failed\n");
913     todo_wine ok(!lstrcmpA(buffer, p), "got %s expected %s\n", buffer, p);
914
915     SetLastError(0xdeadbeef);
916     ret = pSetConsoleInputExeNameA(NULL);
917     error = GetLastError();
918     ok(!ret, "SetConsoleInputExeNameA failed\n");
919     ok(error == ERROR_INVALID_PARAMETER, "got %u expected ERROR_INVALID_PARAMETER\n", error);
920
921     SetLastError(0xdeadbeef);
922     ret = pSetConsoleInputExeNameA("");
923     error = GetLastError();
924     ok(!ret, "SetConsoleInputExeNameA failed\n");
925     ok(error == ERROR_INVALID_PARAMETER, "got %u expected ERROR_INVALID_PARAMETER\n", error);
926
927     ret = pSetConsoleInputExeNameA(input_exe);
928     ok(ret, "SetConsoleInputExeNameA failed\n");
929
930     ret = pGetConsoleInputExeNameA(sizeof(buffer)/sizeof(buffer[0]), buffer);
931     ok(ret, "GetConsoleInputExeNameA failed\n");
932     ok(!lstrcmpA(buffer, input_exe), "got %s expected %s\n", buffer, input_exe);
933 }
934
935 static void test_GetConsoleProcessList(void)
936 {
937     DWORD ret, *list = NULL;
938
939     if (!pGetConsoleProcessList)
940     {
941         win_skip("GetConsoleProcessList is not available\n");
942         return;
943     }
944
945     SetLastError(0xdeadbeef);
946     ret = pGetConsoleProcessList(NULL, 0);
947     ok(ret == 0, "Expected failure\n");
948     ok(GetLastError() == ERROR_INVALID_PARAMETER,
949        "Expected ERROR_INVALID_PARAMETER, got %d\n",
950        GetLastError());
951
952     SetLastError(0xdeadbeef);
953     ret = pGetConsoleProcessList(NULL, 1);
954     ok(ret == 0, "Expected failure\n");
955     ok(GetLastError() == ERROR_INVALID_PARAMETER,
956        "Expected ERROR_INVALID_PARAMETER, got %d\n",
957        GetLastError());
958
959     /* We should only have 1 process but only for these specific unit tests as
960      * we created our own console. An AttachConsole(ATTACH_PARENT_PROCESS) would
961      * give us two processes for example.
962      */
963     list = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD));
964
965     SetLastError(0xdeadbeef);
966     ret = pGetConsoleProcessList(list, 0);
967     ok(ret == 0, "Expected failure\n");
968     ok(GetLastError() == ERROR_INVALID_PARAMETER,
969        "Expected ERROR_INVALID_PARAMETER, got %d\n",
970        GetLastError());
971
972     SetLastError(0xdeadbeef);
973     ret = pGetConsoleProcessList(list, 1);
974     todo_wine
975     ok(ret == 1, "Expected 1, got %d\n", ret);
976
977     HeapFree(GetProcessHeap(), 0, list);
978
979     list = HeapAlloc(GetProcessHeap(), 0, ret * sizeof(DWORD));
980
981     SetLastError(0xdeadbeef);
982     ret = pGetConsoleProcessList(list, ret);
983     todo_wine
984     ok(ret == 1, "Expected 1, got %d\n", ret);
985
986     if (ret == 1)
987     {
988         DWORD pid = GetCurrentProcessId();
989         ok(list[0] == pid, "Expected %d, got %d\n", pid, list[0]);
990     }
991
992     HeapFree(GetProcessHeap(), 0, list);
993 }
994
995 static void test_OpenConsoleW(void)
996 {
997     static const WCHAR coninW[] = {'C','O','N','I','N','$',0};
998     static const WCHAR conoutW[] = {'C','O','N','O','U','T','$',0};
999     static const WCHAR emptyW[] = {0};
1000     static const WCHAR invalidW[] = {'I','N','V','A','L','I','D',0};
1001
1002     static const struct
1003     {
1004         LPCWSTR name;
1005         DWORD access;
1006         BOOL inherit;
1007         DWORD creation;
1008         DWORD gle;
1009     } invalid_table[] = {
1010         {NULL,     0,                            FALSE,      0,                 ERROR_INVALID_PARAMETER},
1011         {NULL,     0xdeadbeef,                   0xdeadbeef, 0xdeadbeef,        ERROR_INVALID_PARAMETER},
1012         {NULL,     0,                            FALSE,      OPEN_ALWAYS,       ERROR_INVALID_PARAMETER},
1013         {NULL,     GENERIC_READ | GENERIC_WRITE, FALSE,      0,                 ERROR_INVALID_PARAMETER},
1014         {NULL,     GENERIC_READ | GENERIC_WRITE, FALSE,      OPEN_ALWAYS,       ERROR_INVALID_PARAMETER},
1015         {NULL,     GENERIC_READ | GENERIC_WRITE, FALSE,      OPEN_EXISTING,     ERROR_INVALID_PARAMETER},
1016         {emptyW,   0,                            FALSE,      0,                 ERROR_INVALID_PARAMETER},
1017         {emptyW,   0xdeadbeef,                   0xdeadbeef, 0xdeadbeef,        ERROR_INVALID_PARAMETER},
1018         {emptyW,   0,                            FALSE,      OPEN_ALWAYS,       ERROR_INVALID_PARAMETER},
1019         {emptyW,   GENERIC_READ | GENERIC_WRITE, FALSE,      0,                 ERROR_INVALID_PARAMETER},
1020         {emptyW,   GENERIC_READ | GENERIC_WRITE, FALSE,      OPEN_ALWAYS,       ERROR_INVALID_PARAMETER},
1021         {emptyW,   GENERIC_READ | GENERIC_WRITE, FALSE,      OPEN_EXISTING,     ERROR_INVALID_PARAMETER},
1022         {invalidW, 0,                            FALSE,      0,                 ERROR_INVALID_PARAMETER},
1023         {invalidW, 0xdeadbeef,                   0xdeadbeef, 0xdeadbeef,        ERROR_INVALID_PARAMETER},
1024         {invalidW, 0,                            FALSE,      OPEN_ALWAYS,       ERROR_INVALID_PARAMETER},
1025         {invalidW, GENERIC_READ | GENERIC_WRITE, FALSE,      0,                 ERROR_INVALID_PARAMETER},
1026         {invalidW, GENERIC_READ | GENERIC_WRITE, FALSE,      OPEN_ALWAYS,       ERROR_INVALID_PARAMETER},
1027         {invalidW, GENERIC_READ | GENERIC_WRITE, FALSE,      OPEN_EXISTING,     ERROR_INVALID_PARAMETER},
1028         {coninW,   0,                            FALSE,      0,                 ERROR_SHARING_VIOLATION},
1029         {coninW,   0xdeadbeef,                   0xdeadbeef, 0xdeadbeef,        ERROR_INVALID_PARAMETER},
1030         {coninW,   0,                            FALSE,      OPEN_ALWAYS,       ERROR_INVALID_PARAMETER},
1031         {coninW,   GENERIC_READ | GENERIC_WRITE, FALSE,      0,                 ERROR_SHARING_VIOLATION},
1032         {coninW,   GENERIC_READ | GENERIC_WRITE, FALSE,      CREATE_NEW,        ERROR_SHARING_VIOLATION},
1033         {coninW,   GENERIC_READ | GENERIC_WRITE, FALSE,      CREATE_ALWAYS,     ERROR_SHARING_VIOLATION},
1034         {coninW,   GENERIC_READ | GENERIC_WRITE, FALSE,      OPEN_ALWAYS,       ERROR_INVALID_PARAMETER},
1035         {coninW,   GENERIC_READ | GENERIC_WRITE, FALSE,      TRUNCATE_EXISTING, ERROR_INVALID_PARAMETER},
1036         {conoutW,  0,                            FALSE,      0,                 ERROR_SHARING_VIOLATION},
1037         {conoutW,  0xdeadbeef,                   0xdeadbeef, 0xdeadbeef,        ERROR_INVALID_PARAMETER},
1038         {conoutW,  0,                            FALSE,      OPEN_ALWAYS,       ERROR_INVALID_PARAMETER},
1039         {conoutW,  GENERIC_READ | GENERIC_WRITE, FALSE,      0,                 ERROR_SHARING_VIOLATION},
1040         {conoutW,  GENERIC_READ | GENERIC_WRITE, FALSE,      CREATE_NEW,        ERROR_SHARING_VIOLATION},
1041         {conoutW,  GENERIC_READ | GENERIC_WRITE, FALSE,      CREATE_ALWAYS,     ERROR_SHARING_VIOLATION},
1042         {conoutW,  GENERIC_READ | GENERIC_WRITE, FALSE,      OPEN_ALWAYS,       ERROR_INVALID_PARAMETER},
1043         {conoutW,  GENERIC_READ | GENERIC_WRITE, FALSE,      TRUNCATE_EXISTING, ERROR_INVALID_PARAMETER},
1044     };
1045
1046     int index;
1047     HANDLE ret;
1048
1049     if (!pOpenConsoleW)
1050     {
1051         win_skip("OpenConsoleW is not available\n");
1052         return;
1053     }
1054
1055     for (index = 0; index < sizeof(invalid_table)/sizeof(invalid_table[0]); index++)
1056     {
1057         SetLastError(0xdeadbeef);
1058         ret = pOpenConsoleW(invalid_table[index].name, invalid_table[index].access,
1059                             invalid_table[index].inherit, invalid_table[index].creation);
1060         ok(ret == INVALID_HANDLE_VALUE,
1061            "Expected OpenConsoleW to return INVALID_HANDLE_VALUE for index %d, got %p\n",
1062            index, ret);
1063         ok(GetLastError() == invalid_table[index].gle,
1064            "Expected GetLastError() to return %u for index %d, got %u\n",
1065            invalid_table[index].gle, index, GetLastError());
1066     }
1067
1068     /* OpenConsoleW should not touch the last error on success. */
1069     SetLastError(0xdeadbeef);
1070     ret = pOpenConsoleW(coninW, GENERIC_READ | GENERIC_WRITE, FALSE, OPEN_EXISTING);
1071     ok(ret != INVALID_HANDLE_VALUE,
1072        "Expected OpenConsoleW to return a valid handle\n");
1073     ok(GetLastError() == 0xdeadbeef,
1074        "Expected the last error to be untouched, got %u\n", GetLastError());
1075     if (ret != INVALID_HANDLE_VALUE)
1076         CloseHandle(ret);
1077
1078     SetLastError(0xdeadbeef);
1079     ret = pOpenConsoleW(conoutW, GENERIC_READ | GENERIC_WRITE, FALSE, OPEN_EXISTING);
1080     ok(ret != INVALID_HANDLE_VALUE,
1081        "Expected OpenConsoleW to return a valid handle\n");
1082     ok(GetLastError() == 0xdeadbeef,
1083        "Expected the last error to be untouched, got %u\n", GetLastError());
1084     if (ret != INVALID_HANDLE_VALUE)
1085         CloseHandle(ret);
1086 }
1087
1088 static void test_VerifyConsoleIoHandle( HANDLE handle )
1089 {
1090     BOOL ret;
1091     DWORD error;
1092
1093     if (!pVerifyConsoleIoHandle)
1094     {
1095         win_skip("VerifyConsoleIoHandle is not available\n");
1096         return;
1097     }
1098
1099     /* invalid handle */
1100     SetLastError(0xdeadbeef);
1101     ret = pVerifyConsoleIoHandle((HANDLE)0xdeadbee0);
1102     error = GetLastError();
1103     ok(!ret, "expected VerifyConsoleIoHandle to fail\n");
1104     ok(error == 0xdeadbeef, "wrong GetLastError() %d\n", error);
1105
1106     /* invalid handle + 1 */
1107     SetLastError(0xdeadbeef);
1108     ret = pVerifyConsoleIoHandle((HANDLE)0xdeadbee1);
1109     error = GetLastError();
1110     ok(!ret, "expected VerifyConsoleIoHandle to fail\n");
1111     ok(error == 0xdeadbeef, "wrong GetLastError() %d\n", error);
1112
1113     /* invalid handle + 2 */
1114     SetLastError(0xdeadbeef);
1115     ret = pVerifyConsoleIoHandle((HANDLE)0xdeadbee2);
1116     error = GetLastError();
1117     ok(!ret, "expected VerifyConsoleIoHandle to fail\n");
1118     ok(error == 0xdeadbeef, "wrong GetLastError() %d\n", error);
1119
1120     /* invalid handle + 3 */
1121     SetLastError(0xdeadbeef);
1122     ret = pVerifyConsoleIoHandle((HANDLE)0xdeadbee3);
1123     error = GetLastError();
1124     ok(!ret, "expected VerifyConsoleIoHandle to fail\n");
1125     ok(error == 0xdeadbeef, "wrong GetLastError() %d\n", error);
1126
1127     /* valid handle */
1128     SetLastError(0xdeadbeef);
1129     ret = pVerifyConsoleIoHandle(handle);
1130     error = GetLastError();
1131     ok(ret, "expected VerifyConsoleIoHandle to succeed\n");
1132     ok(error == 0xdeadbeef, "wrong GetLastError() %d\n", error);
1133 }
1134
1135 static void test_GetSetStdHandle(void)
1136 {
1137     HANDLE handle;
1138     DWORD error;
1139     BOOL ret;
1140
1141     /* get invalid std handle */
1142     SetLastError(0xdeadbeef);
1143     handle = GetStdHandle(42);
1144     error = GetLastError();
1145     ok(error == ERROR_INVALID_HANDLE || broken(error == ERROR_INVALID_FUNCTION)/* Win9x */,
1146        "wrong GetLastError() %d\n", error);
1147     ok(handle == INVALID_HANDLE_VALUE, "expected INVALID_HANDLE_VALUE\n");
1148
1149     /* get valid */
1150     SetLastError(0xdeadbeef);
1151     handle = GetStdHandle(STD_INPUT_HANDLE);
1152     error = GetLastError();
1153     ok(error == 0xdeadbeef, "wrong GetLastError() %d\n", error);
1154
1155     /* set invalid std handle */
1156     SetLastError(0xdeadbeef);
1157     ret = SetStdHandle(42, handle);
1158     error = GetLastError();
1159     ok(!ret, "expected SetStdHandle to fail\n");
1160     ok(error == ERROR_INVALID_HANDLE || broken(error == ERROR_INVALID_FUNCTION)/* Win9x */,
1161        "wrong GetLastError() %d\n", error);
1162
1163     /* set valid (restore old value) */
1164     SetLastError(0xdeadbeef);
1165     ret = SetStdHandle(STD_INPUT_HANDLE, handle);
1166     error = GetLastError();
1167     ok(ret, "expected SetStdHandle to succeed\n");
1168     ok(error == 0xdeadbeef, "wrong GetLastError() %d\n", error);
1169 }
1170
1171 static void test_GetNumberOfConsoleInputEvents(HANDLE input_handle)
1172 {
1173     DWORD count;
1174     BOOL ret;
1175     int i;
1176
1177     const struct
1178     {
1179         HANDLE handle;
1180         LPDWORD nrofevents;
1181         DWORD last_error;
1182     } invalid_table[] =
1183     {
1184         {NULL,                 NULL,   ERROR_INVALID_HANDLE},
1185         {NULL,                 &count, ERROR_INVALID_HANDLE},
1186         {INVALID_HANDLE_VALUE, NULL,   ERROR_INVALID_HANDLE},
1187         {INVALID_HANDLE_VALUE, &count, ERROR_INVALID_HANDLE},
1188     };
1189
1190     for (i = 0; i < sizeof(invalid_table)/sizeof(invalid_table[0]); i++)
1191     {
1192         SetLastError(0xdeadbeef);
1193         if (invalid_table[i].nrofevents) count = 0xdeadbeef;
1194         ret = GetNumberOfConsoleInputEvents(invalid_table[i].handle,
1195                                             invalid_table[i].nrofevents);
1196         ok(!ret, "[%d] Expected GetNumberOfConsoleInputEvents to return FALSE, got %d\n", i, ret);
1197         if (invalid_table[i].nrofevents)
1198         {
1199             ok(count == 0xdeadbeef,
1200                "[%d] Expected output count to be unmodified, got %u\n", i, count);
1201         }
1202         ok(GetLastError() == invalid_table[i].last_error,
1203            "[%d] Expected last error to be %u, got %u\n",
1204            i, invalid_table[i].last_error, GetLastError());
1205     }
1206
1207     /* Test crashes on Windows 7. */
1208     if (0)
1209     {
1210         SetLastError(0xdeadbeef);
1211         ret = GetNumberOfConsoleInputEvents(input_handle, NULL);
1212         ok(!ret, "Expected GetNumberOfConsoleInputEvents to return FALSE, got %d\n", ret);
1213         ok(GetLastError() == ERROR_INVALID_ACCESS,
1214            "Expected last error to be ERROR_INVALID_ACCESS, got %u\n",
1215            GetLastError());
1216     }
1217
1218     count = 0xdeadbeef;
1219     ret = GetNumberOfConsoleInputEvents(input_handle, &count);
1220     ok(ret == TRUE, "Expected GetNumberOfConsoleInputEvents to return TRUE, got %d\n", ret);
1221     ok(count != 0xdeadbeef, "Expected output count to initialized\n");
1222 }
1223
1224 static void test_WriteConsoleInputA(HANDLE input_handle)
1225 {
1226     INPUT_RECORD event;
1227     INPUT_RECORD event_list[5];
1228     MOUSE_EVENT_RECORD mouse_event = { {0, 0}, 0, 0, MOUSE_MOVED };
1229     KEY_EVENT_RECORD key_event;
1230     DWORD count, console_mode;
1231     BOOL ret;
1232     int i;
1233
1234     const struct
1235     {
1236         HANDLE handle;
1237         const INPUT_RECORD *buffer;
1238         DWORD count;
1239         LPDWORD written;
1240         DWORD expected_count;
1241         DWORD last_error;
1242         int win7_crash;
1243     } invalid_table[] =
1244     {
1245         {NULL, NULL, 0, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1246         {NULL, NULL, 0, &count, 0, ERROR_INVALID_HANDLE},
1247         {NULL, NULL, 1, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS},
1248         {NULL, NULL, 1, &count, 0xdeadbeef, ERROR_INVALID_ACCESS},
1249         {NULL, &event, 0, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1250         {NULL, &event, 0, &count, 0, ERROR_INVALID_HANDLE},
1251         {NULL, &event, 1, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1252         {NULL, &event, 1, &count, 0, ERROR_INVALID_HANDLE},
1253         {INVALID_HANDLE_VALUE, NULL, 0, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1254         {INVALID_HANDLE_VALUE, NULL, 0, &count, 0, ERROR_INVALID_HANDLE},
1255         {INVALID_HANDLE_VALUE, NULL, 1, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS},
1256         {INVALID_HANDLE_VALUE, NULL, 1, &count, 0xdeadbeef, ERROR_INVALID_ACCESS},
1257         {INVALID_HANDLE_VALUE, &event, 0, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1258         {INVALID_HANDLE_VALUE, &event, 0, &count, 0, ERROR_INVALID_HANDLE},
1259         {INVALID_HANDLE_VALUE, &event, 1, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1260         {INVALID_HANDLE_VALUE, &event, 1, &count, 0, ERROR_INVALID_HANDLE},
1261         {input_handle, NULL, 0, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1262         {input_handle, NULL, 1, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS},
1263         {input_handle, NULL, 1, &count, 0xdeadbeef, ERROR_INVALID_ACCESS},
1264         {input_handle, &event, 0, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1265         {input_handle, &event, 1, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1266     };
1267
1268     /* Suppress external sources of input events for the duration of the test. */
1269     ret = GetConsoleMode(input_handle, &console_mode);
1270     ok(ret == TRUE, "Expected GetConsoleMode to return TRUE, got %d\n", ret);
1271     if (!ret)
1272     {
1273         skip("GetConsoleMode failed with last error %u\n", GetLastError());
1274         return;
1275     }
1276
1277     ret = SetConsoleMode(input_handle, console_mode & ~(ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT));
1278     ok(ret == TRUE, "Expected SetConsoleMode to return TRUE, got %d\n", ret);
1279     if (!ret)
1280     {
1281         skip("SetConsoleMode failed with last error %u\n", GetLastError());
1282         return;
1283     }
1284
1285     /* Discard any events queued before the tests. */
1286     ret = FlushConsoleInputBuffer(input_handle);
1287     ok(ret == TRUE, "Expected FlushConsoleInputBuffer to return TRUE, got %d\n", ret);
1288
1289     event.EventType = MOUSE_EVENT;
1290     event.Event.MouseEvent = mouse_event;
1291
1292     for (i = 0; i < sizeof(invalid_table)/sizeof(invalid_table[0]); i++)
1293     {
1294         if (invalid_table[i].win7_crash)
1295             continue;
1296
1297         SetLastError(0xdeadbeef);
1298         if (invalid_table[i].written) count = 0xdeadbeef;
1299         ret = WriteConsoleInputA(invalid_table[i].handle,
1300                                  invalid_table[i].buffer,
1301                                  invalid_table[i].count,
1302                                  invalid_table[i].written);
1303         ok(!ret, "[%d] Expected WriteConsoleInputA to return FALSE, got %d\n", i, ret);
1304         if (invalid_table[i].written)
1305         {
1306             ok(count == invalid_table[i].expected_count,
1307                "[%d] Expected output count to be %u, got %u\n",
1308                i, invalid_table[i].expected_count, count);
1309         }
1310         ok(GetLastError() == invalid_table[i].last_error,
1311            "[%d] Expected last error to be %u, got %u\n",
1312            i, invalid_table[i].last_error, GetLastError());
1313     }
1314
1315     count = 0xdeadbeef;
1316     ret = WriteConsoleInputA(input_handle, NULL, 0, &count);
1317     ok(ret == TRUE, "Expected WriteConsoleInputA to return TRUE, got %d\n", ret);
1318     ok(count == 0, "Expected count to be 0, got %u\n", count);
1319
1320     count = 0xdeadbeef;
1321     ret = WriteConsoleInputA(input_handle, &event, 0, &count);
1322     ok(ret == TRUE, "Expected WriteConsoleInputA to return TRUE, got %d\n", ret);
1323     ok(count == 0, "Expected count to be 0, got %u\n", count);
1324
1325     count = 0xdeadbeef;
1326     ret = WriteConsoleInputA(input_handle, &event, 1, &count);
1327     ok(ret == TRUE, "Expected WriteConsoleInputA to return TRUE, got %d\n", ret);
1328     ok(count == 1, "Expected count to be 1, got %u\n", count);
1329
1330     ret = FlushConsoleInputBuffer(input_handle);
1331     ok(ret == TRUE, "Expected FlushConsoleInputBuffer to return TRUE, got %d\n", ret);
1332
1333     /* Writing a single mouse event doesn't seem to affect the count if an adjacent mouse event is already queued. */
1334     event.EventType = MOUSE_EVENT;
1335     event.Event.MouseEvent = mouse_event;
1336
1337     ret = WriteConsoleInputA(input_handle, &event, 1, &count);
1338     ok(ret == TRUE, "Expected WriteConsoleInputA to return TRUE, got %d\n", ret);
1339     ok(count == 1, "Expected count to be 1, got %u\n", count);
1340
1341     ret = GetNumberOfConsoleInputEvents(input_handle, &count);
1342     ok(ret == TRUE, "Expected GetNumberOfConsoleInputEvents to return TRUE, got %d\n", ret);
1343     ok(count == 1, "Expected count to be 1, got %u\n", count);
1344
1345     ret = WriteConsoleInputA(input_handle, &event, 1, &count);
1346     ok(ret == TRUE, "Expected WriteConsoleInputA to return TRUE, got %d\n", ret);
1347     ok(count == 1, "Expected count to be 1, got %u\n", count);
1348
1349     ret = GetNumberOfConsoleInputEvents(input_handle, &count);
1350     ok(ret == TRUE, "Expected GetNumberOfConsoleInputEvents to return TRUE, got %d\n", ret);
1351     todo_wine
1352     ok(count == 1, "Expected count to be 1, got %u\n", count);
1353
1354     ret = FlushConsoleInputBuffer(input_handle);
1355     ok(ret == TRUE, "Expected FlushConsoleInputBuffer to return TRUE, got %d\n", ret);
1356
1357     for (i = 0; i < sizeof(event_list)/sizeof(event_list[0]); i++)
1358     {
1359         event_list[i].EventType = MOUSE_EVENT;
1360         event_list[i].Event.MouseEvent = mouse_event;
1361     }
1362
1363     /* Writing consecutive chunks of mouse events appears to work. */
1364     ret = WriteConsoleInputA(input_handle, event_list, sizeof(event_list)/sizeof(event_list[0]), &count);
1365     ok(ret == TRUE, "Expected WriteConsoleInputA to return TRUE, got %d\n", ret);
1366     ok(count == sizeof(event_list)/sizeof(event_list[0]),
1367        "Expected count to be event list length, got %u\n", count);
1368
1369     ret = GetNumberOfConsoleInputEvents(input_handle, &count);
1370     ok(ret == TRUE, "Expected GetNumberOfConsoleInputEvents to return TRUE, got %d\n", ret);
1371     ok(count == sizeof(event_list)/sizeof(event_list[0]),
1372        "Expected count to be event list length, got %u\n", count);
1373
1374     ret = WriteConsoleInputA(input_handle, event_list, sizeof(event_list)/sizeof(event_list[0]), &count);
1375     ok(ret == TRUE, "Expected WriteConsoleInputA to return TRUE, got %d\n", ret);
1376     ok(count == sizeof(event_list)/sizeof(event_list[0]),
1377        "Expected count to be event list length, got %u\n", count);
1378
1379     ret = GetNumberOfConsoleInputEvents(input_handle, &count);
1380     ok(ret == TRUE, "Expected GetNumberOfConsoleInputEvents to return TRUE, got %d\n", ret);
1381     ok(count == 2*sizeof(event_list)/sizeof(event_list[0]),
1382        "Expected count to be twice event list length, got %u\n", count);
1383
1384     /* Again, writing a single mouse event with adjacent mouse events queued doesn't appear to affect the count. */
1385     ret = WriteConsoleInputA(input_handle, &event, 1, &count);
1386     ok(ret == TRUE, "Expected WriteConsoleInputA to return TRUE, got %d\n", ret);
1387     ok(count == 1, "Expected count to be 1, got %u\n", count);
1388
1389     ret = GetNumberOfConsoleInputEvents(input_handle, &count);
1390     ok(ret == TRUE, "Expected GetNumberOfConsoleInputEvents to return TRUE, got %d\n", ret);
1391     todo_wine
1392     ok(count == 2*sizeof(event_list)/sizeof(event_list[0]),
1393        "Expected count to be twice event list length, got %u\n", count);
1394
1395     ret = FlushConsoleInputBuffer(input_handle);
1396     ok(ret == TRUE, "Expected FlushConsoleInputBuffer to return TRUE, got %d\n", ret);
1397
1398     key_event.bKeyDown = FALSE;
1399     key_event.wRepeatCount = 0;
1400     key_event.wVirtualKeyCode = VK_SPACE;
1401     key_event.wVirtualScanCode = VK_SPACE;
1402     key_event.uChar.AsciiChar = ' ';
1403     key_event.dwControlKeyState = 0;
1404
1405     event.EventType = KEY_EVENT;
1406     event.Event.KeyEvent = key_event;
1407
1408     /* Key events don't exhibit the same behavior as mouse events. */
1409     ret = WriteConsoleInputA(input_handle, &event, 1, &count);
1410     ok(ret == TRUE, "Expected WriteConsoleInputA to return TRUE, got %d\n", ret);
1411     ok(count == 1, "Expected count to be 1, got %u\n", count);
1412
1413     ret = GetNumberOfConsoleInputEvents(input_handle, &count);
1414     ok(ret == TRUE, "Expected GetNumberOfConsoleInputEvents to return TRUE, got %d\n", ret);
1415     ok(count == 1, "Expected count to be 1, got %u\n", count);
1416
1417     ret = WriteConsoleInputA(input_handle, &event, 1, &count);
1418     ok(ret == TRUE, "Expected WriteConsoleInputA to return TRUE, got %d\n", ret);
1419     ok(count == 1, "Expected count to be 1, got %u\n", count);
1420
1421     ret = GetNumberOfConsoleInputEvents(input_handle, &count);
1422     ok(ret == TRUE, "Expected GetNumberOfConsoleInputEvents to return TRUE, got %d\n", ret);
1423     ok(count == 2, "Expected count to be 2, got %u\n", count);
1424
1425     ret = FlushConsoleInputBuffer(input_handle);
1426     ok(ret == TRUE, "Expected FlushConsoleInputBuffer to return TRUE, got %d\n", ret);
1427
1428     /* Try interleaving mouse and key events. */
1429     event.EventType = MOUSE_EVENT;
1430     event.Event.MouseEvent = mouse_event;
1431
1432     ret = WriteConsoleInputA(input_handle, &event, 1, &count);
1433     ok(ret == TRUE, "Expected WriteConsoleInputA to return TRUE, got %d\n", ret);
1434     ok(count == 1, "Expected count to be 1, got %u\n", count);
1435
1436     ret = GetNumberOfConsoleInputEvents(input_handle, &count);
1437     ok(ret == TRUE, "Expected GetNumberOfConsoleInputEvents to return TRUE, got %d\n", ret);
1438     ok(count == 1, "Expected count to be 1, got %u\n", count);
1439
1440     event.EventType = KEY_EVENT;
1441     event.Event.KeyEvent = key_event;
1442
1443     ret = WriteConsoleInputA(input_handle, &event, 1, &count);
1444     ok(ret == TRUE, "Expected WriteConsoleInputA to return TRUE, got %d\n", ret);
1445     ok(count == 1, "Expected count to be 1, got %u\n", count);
1446
1447     ret = GetNumberOfConsoleInputEvents(input_handle, &count);
1448     ok(ret == TRUE, "Expected GetNumberOfConsoleInputEvents to return TRUE, got %d\n", ret);
1449     ok(count == 2, "Expected count to be 2, got %u\n", count);
1450
1451     event.EventType = MOUSE_EVENT;
1452     event.Event.MouseEvent = mouse_event;
1453
1454     ret = WriteConsoleInputA(input_handle, &event, 1, &count);
1455     ok(ret == TRUE, "Expected WriteConsoleInputA to return TRUE, got %d\n", ret);
1456     ok(count == 1, "Expected count to be 1, got %u\n", count);
1457
1458     ret = GetNumberOfConsoleInputEvents(input_handle, &count);
1459     ok(ret == TRUE, "Expected GetNumberOfConsoleInputEvents to return TRUE, got %d\n", ret);
1460     ok(count == 3, "Expected count to be 3, got %u\n", count);
1461
1462     /* Restore the old console mode. */
1463     ret = SetConsoleMode(input_handle, console_mode);
1464     ok(ret == TRUE, "Expected SetConsoleMode to return TRUE, got %d\n", ret);
1465 }
1466
1467 static void test_WriteConsoleInputW(HANDLE input_handle)
1468 {
1469     INPUT_RECORD event;
1470     INPUT_RECORD event_list[5];
1471     MOUSE_EVENT_RECORD mouse_event = { {0, 0}, 0, 0, MOUSE_MOVED };
1472     KEY_EVENT_RECORD key_event;
1473     DWORD count, console_mode;
1474     BOOL ret;
1475     int i;
1476
1477     const struct
1478     {
1479         HANDLE handle;
1480         const INPUT_RECORD *buffer;
1481         DWORD count;
1482         LPDWORD written;
1483         DWORD expected_count;
1484         DWORD last_error;
1485         int win7_crash;
1486     } invalid_table[] =
1487     {
1488         {NULL, NULL, 0, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1489         {NULL, NULL, 0, &count, 0, ERROR_INVALID_HANDLE},
1490         {NULL, NULL, 1, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS},
1491         {NULL, NULL, 1, &count, 0xdeadbeef, ERROR_INVALID_ACCESS},
1492         {NULL, &event, 0, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1493         {NULL, &event, 0, &count, 0, ERROR_INVALID_HANDLE},
1494         {NULL, &event, 1, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1495         {NULL, &event, 1, &count, 0, ERROR_INVALID_HANDLE},
1496         {INVALID_HANDLE_VALUE, NULL, 0, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1497         {INVALID_HANDLE_VALUE, NULL, 0, &count, 0, ERROR_INVALID_HANDLE},
1498         {INVALID_HANDLE_VALUE, NULL, 1, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS},
1499         {INVALID_HANDLE_VALUE, NULL, 1, &count, 0xdeadbeef, ERROR_INVALID_ACCESS},
1500         {INVALID_HANDLE_VALUE, &event, 0, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1501         {INVALID_HANDLE_VALUE, &event, 0, &count, 0, ERROR_INVALID_HANDLE},
1502         {INVALID_HANDLE_VALUE, &event, 1, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1503         {INVALID_HANDLE_VALUE, &event, 1, &count, 0, ERROR_INVALID_HANDLE},
1504         {input_handle, NULL, 0, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1505         {input_handle, NULL, 1, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS},
1506         {input_handle, NULL, 1, &count, 0xdeadbeef, ERROR_INVALID_ACCESS},
1507         {input_handle, &event, 0, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1508         {input_handle, &event, 1, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1509     };
1510
1511     /* Suppress external sources of input events for the duration of the test. */
1512     ret = GetConsoleMode(input_handle, &console_mode);
1513     ok(ret == TRUE, "Expected GetConsoleMode to return TRUE, got %d\n", ret);
1514     if (!ret)
1515     {
1516         skip("GetConsoleMode failed with last error %u\n", GetLastError());
1517         return;
1518     }
1519
1520     ret = SetConsoleMode(input_handle, console_mode & ~(ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT));
1521     ok(ret == TRUE, "Expected SetConsoleMode to return TRUE, got %d\n", ret);
1522     if (!ret)
1523     {
1524         skip("SetConsoleMode failed with last error %u\n", GetLastError());
1525         return;
1526     }
1527
1528     /* Discard any events queued before the tests. */
1529     ret = FlushConsoleInputBuffer(input_handle);
1530     ok(ret == TRUE, "Expected FlushConsoleInputBuffer to return TRUE, got %d\n", ret);
1531
1532     event.EventType = MOUSE_EVENT;
1533     event.Event.MouseEvent = mouse_event;
1534
1535     for (i = 0; i < sizeof(invalid_table)/sizeof(invalid_table[0]); i++)
1536     {
1537         if (invalid_table[i].win7_crash)
1538             continue;
1539
1540         SetLastError(0xdeadbeef);
1541         if (invalid_table[i].written) count = 0xdeadbeef;
1542         ret = WriteConsoleInputW(invalid_table[i].handle,
1543                                  invalid_table[i].buffer,
1544                                  invalid_table[i].count,
1545                                  invalid_table[i].written);
1546         ok(!ret, "[%d] Expected WriteConsoleInputW to return FALSE, got %d\n", i, ret);
1547         if (invalid_table[i].written)
1548         {
1549             ok(count == invalid_table[i].expected_count,
1550                "[%d] Expected output count to be %u, got %u\n",
1551                i, invalid_table[i].expected_count, count);
1552         }
1553         ok(GetLastError() == invalid_table[i].last_error,
1554            "[%d] Expected last error to be %u, got %u\n",
1555            i, invalid_table[i].last_error, GetLastError());
1556     }
1557
1558     count = 0xdeadbeef;
1559     ret = WriteConsoleInputW(input_handle, NULL, 0, &count);
1560     ok(ret == TRUE, "Expected WriteConsoleInputW to return TRUE, got %d\n", ret);
1561     ok(count == 0, "Expected count to be 0, got %u\n", count);
1562
1563     count = 0xdeadbeef;
1564     ret = WriteConsoleInputW(input_handle, &event, 0, &count);
1565     ok(ret == TRUE, "Expected WriteConsoleInputW to return TRUE, got %d\n", ret);
1566     ok(count == 0, "Expected count to be 0, got %u\n", count);
1567
1568     count = 0xdeadbeef;
1569     ret = WriteConsoleInputW(input_handle, &event, 1, &count);
1570     ok(ret == TRUE, "Expected WriteConsoleInputW to return TRUE, got %d\n", ret);
1571     ok(count == 1, "Expected count to be 1, got %u\n", count);
1572
1573     ret = FlushConsoleInputBuffer(input_handle);
1574     ok(ret == TRUE, "Expected FlushConsoleInputBuffer to return TRUE, got %d\n", ret);
1575
1576     /* Writing a single mouse event doesn't seem to affect the count if an adjacent mouse event is already queued. */
1577     event.EventType = MOUSE_EVENT;
1578     event.Event.MouseEvent = mouse_event;
1579
1580     ret = WriteConsoleInputW(input_handle, &event, 1, &count);
1581     ok(ret == TRUE, "Expected WriteConsoleInputW to return TRUE, got %d\n", ret);
1582     ok(count == 1, "Expected count to be 1, got %u\n", count);
1583
1584     ret = GetNumberOfConsoleInputEvents(input_handle, &count);
1585     ok(ret == TRUE, "Expected GetNumberOfConsoleInputEvents to return TRUE, got %d\n", ret);
1586     ok(count == 1, "Expected count to be 1, got %u\n", count);
1587
1588     ret = WriteConsoleInputW(input_handle, &event, 1, &count);
1589     ok(ret == TRUE, "Expected WriteConsoleInputW to return TRUE, got %d\n", ret);
1590     ok(count == 1, "Expected count to be 1, got %u\n", count);
1591
1592     ret = GetNumberOfConsoleInputEvents(input_handle, &count);
1593     ok(ret == TRUE, "Expected GetNumberOfConsoleInputEvents to return TRUE, got %d\n", ret);
1594     todo_wine
1595     ok(count == 1, "Expected count to be 1, got %u\n", count);
1596
1597     ret = FlushConsoleInputBuffer(input_handle);
1598     ok(ret == TRUE, "Expected FlushConsoleInputBuffer to return TRUE, got %d\n", ret);
1599
1600     for (i = 0; i < sizeof(event_list)/sizeof(event_list[0]); i++)
1601     {
1602         event_list[i].EventType = MOUSE_EVENT;
1603         event_list[i].Event.MouseEvent = mouse_event;
1604     }
1605
1606     /* Writing consecutive chunks of mouse events appears to work. */
1607     ret = WriteConsoleInputW(input_handle, event_list, sizeof(event_list)/sizeof(event_list[0]), &count);
1608     ok(ret == TRUE, "Expected WriteConsoleInputW to return TRUE, got %d\n", ret);
1609     ok(count == sizeof(event_list)/sizeof(event_list[0]),
1610        "Expected count to be event list length, got %u\n", count);
1611
1612     ret = GetNumberOfConsoleInputEvents(input_handle, &count);
1613     ok(ret == TRUE, "Expected GetNumberOfConsoleInputEvents to return TRUE, got %d\n", ret);
1614     ok(count == sizeof(event_list)/sizeof(event_list[0]),
1615        "Expected count to be event list length, got %u\n", count);
1616
1617     ret = WriteConsoleInputW(input_handle, event_list, sizeof(event_list)/sizeof(event_list[0]), &count);
1618     ok(ret == TRUE, "Expected WriteConsoleInputW to return TRUE, got %d\n", ret);
1619     ok(count == sizeof(event_list)/sizeof(event_list[0]),
1620        "Expected count to be event list length, got %u\n", count);
1621
1622     ret = GetNumberOfConsoleInputEvents(input_handle, &count);
1623     ok(ret == TRUE, "Expected GetNumberOfConsoleInputEvents to return TRUE, got %d\n", ret);
1624     ok(count == 2*sizeof(event_list)/sizeof(event_list[0]),
1625        "Expected count to be twice event list length, got %u\n", count);
1626
1627     /* Again, writing a single mouse event with adjacent mouse events queued doesn't appear to affect the count. */
1628     ret = WriteConsoleInputW(input_handle, &event, 1, &count);
1629     ok(ret == TRUE, "Expected WriteConsoleInputW to return TRUE, got %d\n", ret);
1630     ok(count == 1, "Expected count to be 1, got %u\n", count);
1631
1632     ret = GetNumberOfConsoleInputEvents(input_handle, &count);
1633     ok(ret == TRUE, "Expected GetNumberOfConsoleInputEvents to return TRUE, got %d\n", ret);
1634     todo_wine
1635     ok(count == 2*sizeof(event_list)/sizeof(event_list[0]),
1636        "Expected count to be twice event list length, got %u\n", count);
1637
1638     ret = FlushConsoleInputBuffer(input_handle);
1639     ok(ret == TRUE, "Expected FlushConsoleInputBuffer to return TRUE, got %d\n", ret);
1640
1641     key_event.bKeyDown = FALSE;
1642     key_event.wRepeatCount = 0;
1643     key_event.wVirtualKeyCode = VK_SPACE;
1644     key_event.wVirtualScanCode = VK_SPACE;
1645     key_event.uChar.UnicodeChar = ' ';
1646     key_event.dwControlKeyState = 0;
1647
1648     event.EventType = KEY_EVENT;
1649     event.Event.KeyEvent = key_event;
1650
1651     /* Key events don't exhibit the same behavior as mouse events. */
1652     ret = WriteConsoleInputW(input_handle, &event, 1, &count);
1653     ok(ret == TRUE, "Expected WriteConsoleInputW to return TRUE, got %d\n", ret);
1654     ok(count == 1, "Expected count to be 1, got %u\n", count);
1655
1656     ret = GetNumberOfConsoleInputEvents(input_handle, &count);
1657     ok(ret == TRUE, "Expected GetNumberOfConsoleInputEvents to return TRUE, got %d\n", ret);
1658     ok(count == 1, "Expected count to be 1, got %u\n", count);
1659
1660     ret = WriteConsoleInputW(input_handle, &event, 1, &count);
1661     ok(ret == TRUE, "Expected WriteConsoleInputW to return TRUE, got %d\n", ret);
1662     ok(count == 1, "Expected count to be 1, got %u\n", count);
1663
1664     ret = GetNumberOfConsoleInputEvents(input_handle, &count);
1665     ok(ret == TRUE, "Expected GetNumberOfConsoleInputEvents to return TRUE, got %d\n", ret);
1666     ok(count == 2, "Expected count to be 2, got %u\n", count);
1667
1668     ret = FlushConsoleInputBuffer(input_handle);
1669     ok(ret == TRUE, "Expected FlushConsoleInputBuffer to return TRUE, got %d\n", ret);
1670
1671     /* Try interleaving mouse and key events. */
1672     event.EventType = MOUSE_EVENT;
1673     event.Event.MouseEvent = mouse_event;
1674
1675     ret = WriteConsoleInputW(input_handle, &event, 1, &count);
1676     ok(ret == TRUE, "Expected WriteConsoleInputW to return TRUE, got %d\n", ret);
1677     ok(count == 1, "Expected count to be 1, got %u\n", count);
1678
1679     ret = GetNumberOfConsoleInputEvents(input_handle, &count);
1680     ok(ret == TRUE, "Expected GetNumberOfConsoleInputEvents to return TRUE, got %d\n", ret);
1681     ok(count == 1, "Expected count to be 1, got %u\n", count);
1682
1683     event.EventType = KEY_EVENT;
1684     event.Event.KeyEvent = key_event;
1685
1686     ret = WriteConsoleInputW(input_handle, &event, 1, &count);
1687     ok(ret == TRUE, "Expected WriteConsoleInputW to return TRUE, got %d\n", ret);
1688     ok(count == 1, "Expected count to be 1, got %u\n", count);
1689
1690     ret = GetNumberOfConsoleInputEvents(input_handle, &count);
1691     ok(ret == TRUE, "Expected GetNumberOfConsoleInputEvents to return TRUE, got %d\n", ret);
1692     ok(count == 2, "Expected count to be 2, got %u\n", count);
1693
1694     event.EventType = MOUSE_EVENT;
1695     event.Event.MouseEvent = mouse_event;
1696
1697     ret = WriteConsoleInputW(input_handle, &event, 1, &count);
1698     ok(ret == TRUE, "Expected WriteConsoleInputW to return TRUE, got %d\n", ret);
1699     ok(count == 1, "Expected count to be 1, got %u\n", count);
1700
1701     ret = GetNumberOfConsoleInputEvents(input_handle, &count);
1702     ok(ret == TRUE, "Expected GetNumberOfConsoleInputEvents to return TRUE, got %d\n", ret);
1703     ok(count == 3, "Expected count to be 3, got %u\n", count);
1704
1705     /* Restore the old console mode. */
1706     ret = SetConsoleMode(input_handle, console_mode);
1707     ok(ret == TRUE, "Expected SetConsoleMode to return TRUE, got %d\n", ret);
1708 }
1709
1710 static void test_WriteConsoleOutputCharacterA(HANDLE output_handle)
1711 {
1712     static const char output[] = {'a', 0};
1713
1714     COORD origin = {0, 0};
1715     DWORD count;
1716     BOOL ret;
1717     int i;
1718
1719     const struct
1720     {
1721         HANDLE hConsoleOutput;
1722         LPCSTR str;
1723         DWORD length;
1724         COORD coord;
1725         LPDWORD lpNumCharsWritten;
1726         DWORD expected_count;
1727         DWORD last_error;
1728         int win7_crash;
1729     } invalid_table[] =
1730     {
1731         {NULL, NULL, 0, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1732         {NULL, NULL, 0, origin, &count, 0, ERROR_INVALID_HANDLE},
1733         {NULL, NULL, 1, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1734         {NULL, NULL, 1, origin, &count, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1735         {NULL, output, 0, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1736         {NULL, output, 0, origin, &count, 0, ERROR_INVALID_HANDLE},
1737         {NULL, output, 1, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1738         {NULL, output, 1, origin, &count, 0, ERROR_INVALID_HANDLE},
1739         {INVALID_HANDLE_VALUE, NULL, 0, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1740         {INVALID_HANDLE_VALUE, NULL, 0, origin, &count, 0, ERROR_INVALID_HANDLE},
1741         {INVALID_HANDLE_VALUE, NULL, 1, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1742         {INVALID_HANDLE_VALUE, NULL, 1, origin, &count, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1743         {INVALID_HANDLE_VALUE, output, 0, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1744         {INVALID_HANDLE_VALUE, output, 0, origin, &count, 0, ERROR_INVALID_HANDLE},
1745         {INVALID_HANDLE_VALUE, output, 1, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1746         {INVALID_HANDLE_VALUE, output, 1, origin, &count, 0, ERROR_INVALID_HANDLE},
1747         {output_handle, NULL, 0, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1748         {output_handle, NULL, 1, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1749         {output_handle, NULL, 1, origin, &count, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1750         {output_handle, output, 0, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1751         {output_handle, output, 1, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1752     };
1753
1754     for (i = 0; i < sizeof(invalid_table)/sizeof(invalid_table[0]); i++)
1755     {
1756         if (invalid_table[i].win7_crash)
1757             continue;
1758
1759         SetLastError(0xdeadbeef);
1760         if (invalid_table[i].lpNumCharsWritten) count = 0xdeadbeef;
1761         ret = WriteConsoleOutputCharacterA(invalid_table[i].hConsoleOutput,
1762                                            invalid_table[i].str,
1763                                            invalid_table[i].length,
1764                                            invalid_table[i].coord,
1765                                            invalid_table[i].lpNumCharsWritten);
1766         ok(!ret, "[%d] Expected WriteConsoleOutputCharacterA to return FALSE, got %d\n", i, ret);
1767         if (invalid_table[i].lpNumCharsWritten)
1768         {
1769             ok(count == invalid_table[i].expected_count,
1770                "[%d] Expected count to be %u, got %u\n",
1771                i, invalid_table[i].expected_count, count);
1772         }
1773         ok(GetLastError() == invalid_table[i].last_error,
1774            "[%d] Expected last error to be %u, got %u\n",
1775            i, invalid_table[i].last_error, GetLastError());
1776     }
1777
1778     count = 0xdeadbeef;
1779     ret = WriteConsoleOutputCharacterA(output_handle, NULL, 0, origin, &count);
1780     ok(ret == TRUE, "Expected WriteConsoleOutputCharacterA to return TRUE, got %d\n", ret);
1781     ok(count == 0, "Expected count to be 0, got %u\n", count);
1782
1783     count = 0xdeadbeef;
1784     ret = WriteConsoleOutputCharacterA(output_handle, output, 0, origin, &count);
1785     ok(ret == TRUE, "Expected WriteConsoleOutputCharacterA to return TRUE, got %d\n", ret);
1786     ok(count == 0, "Expected count to be 0, got %u\n", count);
1787
1788     count = 0xdeadbeef;
1789     ret = WriteConsoleOutputCharacterA(output_handle, output, 1, origin, &count);
1790     ok(ret == TRUE, "Expected WriteConsoleOutputCharacterA to return TRUE, got %d\n", ret);
1791     ok(count == 1, "Expected count to be 1, got %u\n", count);
1792 }
1793
1794 static void test_WriteConsoleOutputCharacterW(HANDLE output_handle)
1795 {
1796     static const WCHAR outputW[] = {'a',0};
1797
1798     COORD origin = {0, 0};
1799     DWORD count;
1800     BOOL ret;
1801     int i;
1802
1803     const struct
1804     {
1805         HANDLE hConsoleOutput;
1806         LPCWSTR str;
1807         DWORD length;
1808         COORD coord;
1809         LPDWORD lpNumCharsWritten;
1810         DWORD expected_count;
1811         DWORD last_error;
1812         int win7_crash;
1813     } invalid_table[] =
1814     {
1815         {NULL, NULL, 0, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1816         {NULL, NULL, 0, origin, &count, 0, ERROR_INVALID_HANDLE},
1817         {NULL, NULL, 1, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1818         {NULL, NULL, 1, origin, &count, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1819         {NULL, outputW, 0, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1820         {NULL, outputW, 0, origin, &count, 0, ERROR_INVALID_HANDLE},
1821         {NULL, outputW, 1, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1822         {NULL, outputW, 1, origin, &count, 0, ERROR_INVALID_HANDLE},
1823         {INVALID_HANDLE_VALUE, NULL, 0, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1824         {INVALID_HANDLE_VALUE, NULL, 0, origin, &count, 0, ERROR_INVALID_HANDLE},
1825         {INVALID_HANDLE_VALUE, NULL, 1, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1826         {INVALID_HANDLE_VALUE, NULL, 1, origin, &count, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1827         {INVALID_HANDLE_VALUE, outputW, 0, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1828         {INVALID_HANDLE_VALUE, outputW, 0, origin, &count, 0, ERROR_INVALID_HANDLE},
1829         {INVALID_HANDLE_VALUE, outputW, 1, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1830         {INVALID_HANDLE_VALUE, outputW, 1, origin, &count, 0, ERROR_INVALID_HANDLE},
1831         {output_handle, NULL, 0, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1832         {output_handle, NULL, 1, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1833         {output_handle, NULL, 1, origin, &count, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1834         {output_handle, outputW, 0, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1835         {output_handle, outputW, 1, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1836     };
1837
1838     for (i = 0; i < sizeof(invalid_table)/sizeof(invalid_table[0]); i++)
1839     {
1840         if (invalid_table[i].win7_crash)
1841             continue;
1842
1843         SetLastError(0xdeadbeef);
1844         if (invalid_table[i].lpNumCharsWritten) count = 0xdeadbeef;
1845         ret = WriteConsoleOutputCharacterW(invalid_table[i].hConsoleOutput,
1846                                            invalid_table[i].str,
1847                                            invalid_table[i].length,
1848                                            invalid_table[i].coord,
1849                                            invalid_table[i].lpNumCharsWritten);
1850         ok(!ret, "[%d] Expected WriteConsoleOutputCharacterW to return FALSE, got %d\n", i, ret);
1851         if (invalid_table[i].lpNumCharsWritten)
1852         {
1853             ok(count == invalid_table[i].expected_count,
1854                "[%d] Expected count to be %u, got %u\n",
1855                i, invalid_table[i].expected_count, count);
1856         }
1857         ok(GetLastError() == invalid_table[i].last_error,
1858            "[%d] Expected last error to be %u, got %u\n",
1859            i, invalid_table[i].last_error, GetLastError());
1860     }
1861
1862     count = 0xdeadbeef;
1863     ret = WriteConsoleOutputCharacterW(output_handle, NULL, 0, origin, &count);
1864     ok(ret == TRUE, "Expected WriteConsoleOutputCharacterW to return TRUE, got %d\n", ret);
1865     ok(count == 0, "Expected count to be 0, got %u\n", count);
1866
1867     count = 0xdeadbeef;
1868     ret = WriteConsoleOutputCharacterW(output_handle, outputW, 0, origin, &count);
1869     ok(ret == TRUE, "Expected WriteConsoleOutputCharacterW to return TRUE, got %d\n", ret);
1870     ok(count == 0, "Expected count to be 0, got %u\n", count);
1871
1872     count = 0xdeadbeef;
1873     ret = WriteConsoleOutputCharacterW(output_handle, outputW, 1, origin, &count);
1874     ok(ret == TRUE, "Expected WriteConsoleOutputCharacterW to return TRUE, got %d\n", ret);
1875     ok(count == 1, "Expected count to be 1, got %u\n", count);
1876 }
1877
1878 static void test_WriteConsoleOutputAttribute(HANDLE output_handle)
1879 {
1880     WORD attr = FOREGROUND_BLUE;
1881     COORD origin = {0, 0};
1882     DWORD count;
1883     BOOL ret;
1884     int i;
1885
1886     const struct
1887     {
1888         HANDLE hConsoleOutput;
1889         CONST WORD *attr;
1890         DWORD length;
1891         COORD coord;
1892         LPDWORD lpNumAttrsWritten;
1893         DWORD expected_count;
1894         DWORD last_error;
1895         int win7_crash;
1896     } invalid_table[] =
1897     {
1898         {NULL, NULL, 0, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1899         {NULL, NULL, 0, origin, &count, 0, ERROR_INVALID_HANDLE},
1900         {NULL, NULL, 1, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1901         {NULL, NULL, 1, origin, &count, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1902         {NULL, &attr, 0, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1903         {NULL, &attr, 0, origin, &count, 0, ERROR_INVALID_HANDLE},
1904         {NULL, &attr, 1, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1905         {NULL, &attr, 1, origin, &count, 0, ERROR_INVALID_HANDLE},
1906         {INVALID_HANDLE_VALUE, NULL, 0, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1907         {INVALID_HANDLE_VALUE, NULL, 0, origin, &count, 0, ERROR_INVALID_HANDLE},
1908         {INVALID_HANDLE_VALUE, NULL, 1, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1909         {INVALID_HANDLE_VALUE, NULL, 1, origin, &count, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1910         {INVALID_HANDLE_VALUE, &attr, 0, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1911         {INVALID_HANDLE_VALUE, &attr, 0, origin, &count, 0, ERROR_INVALID_HANDLE},
1912         {INVALID_HANDLE_VALUE, &attr, 1, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1913         {INVALID_HANDLE_VALUE, &attr, 1, origin, &count, 0, ERROR_INVALID_HANDLE},
1914         {output_handle, NULL, 0, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1915         {output_handle, NULL, 1, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1916         {output_handle, NULL, 1, origin, &count, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1917         {output_handle, &attr, 0, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1918         {output_handle, &attr, 1, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1919     };
1920
1921     for (i = 0; i < sizeof(invalid_table)/sizeof(invalid_table[0]); i++)
1922     {
1923         if (invalid_table[i].win7_crash)
1924             continue;
1925
1926         SetLastError(0xdeadbeef);
1927         if (invalid_table[i].lpNumAttrsWritten) count = 0xdeadbeef;
1928         ret = WriteConsoleOutputAttribute(invalid_table[i].hConsoleOutput,
1929                                           invalid_table[i].attr,
1930                                           invalid_table[i].length,
1931                                           invalid_table[i].coord,
1932                                           invalid_table[i].lpNumAttrsWritten);
1933         ok(!ret, "[%d] Expected WriteConsoleOutputAttribute to return FALSE, got %d\n", i, ret);
1934         if (invalid_table[i].lpNumAttrsWritten)
1935         {
1936             ok(count == invalid_table[i].expected_count,
1937                "[%d] Expected count to be %u, got %u\n",
1938                i, invalid_table[i].expected_count, count);
1939         }
1940         ok(GetLastError() == invalid_table[i].last_error,
1941            "[%d] Expected last error to be %u, got %u\n",
1942            i, invalid_table[i].last_error, GetLastError());
1943     }
1944
1945     count = 0xdeadbeef;
1946     ret = WriteConsoleOutputAttribute(output_handle, NULL, 0, origin, &count);
1947     ok(ret == TRUE, "Expected WriteConsoleOutputAttribute to return TRUE, got %d\n", ret);
1948     ok(count == 0, "Expected count to be 0, got %u\n", count);
1949
1950     count = 0xdeadbeef;
1951     ret = WriteConsoleOutputAttribute(output_handle, &attr, 0, origin, &count);
1952     ok(ret == TRUE, "Expected WriteConsoleOutputAttribute to return TRUE, got %d\n", ret);
1953     ok(count == 0, "Expected count to be 0, got %u\n", count);
1954
1955     count = 0xdeadbeef;
1956     ret = WriteConsoleOutputAttribute(output_handle, &attr, 1, origin, &count);
1957     ok(ret == TRUE, "Expected WriteConsoleOutputAttribute to return TRUE, got %d\n", ret);
1958     ok(count == 1, "Expected count to be 1, got %u\n", count);
1959 }
1960
1961 static void test_FillConsoleOutputCharacterA(HANDLE output_handle)
1962 {
1963     COORD origin = {0, 0};
1964     DWORD count;
1965     BOOL ret;
1966     int i;
1967
1968     const struct
1969     {
1970         HANDLE hConsoleOutput;
1971         CHAR ch;
1972         DWORD length;
1973         COORD coord;
1974         LPDWORD lpNumCharsWritten;
1975         DWORD expected_count;
1976         DWORD last_error;
1977         int win7_crash;
1978     } invalid_table[] =
1979     {
1980         {NULL, 'a', 0, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1981         {NULL, 'a', 0, origin, &count, 0, ERROR_INVALID_HANDLE},
1982         {NULL, 'a', 1, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1983         {NULL, 'a', 1, origin, &count, 0, ERROR_INVALID_HANDLE},
1984         {INVALID_HANDLE_VALUE, 'a', 0, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1985         {INVALID_HANDLE_VALUE, 'a', 0, origin, &count, 0, ERROR_INVALID_HANDLE},
1986         {INVALID_HANDLE_VALUE, 'a', 1, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1987         {INVALID_HANDLE_VALUE, 'a', 1, origin, &count, 0, ERROR_INVALID_HANDLE},
1988         {output_handle, 'a', 0, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1989         {output_handle, 'a', 1, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
1990     };
1991
1992     for (i = 0; i < sizeof(invalid_table)/sizeof(invalid_table[0]); i++)
1993     {
1994         if (invalid_table[i].win7_crash)
1995             continue;
1996
1997         SetLastError(0xdeadbeef);
1998         if (invalid_table[i].lpNumCharsWritten) count = 0xdeadbeef;
1999         ret = FillConsoleOutputCharacterA(invalid_table[i].hConsoleOutput,
2000                                           invalid_table[i].ch,
2001                                           invalid_table[i].length,
2002                                           invalid_table[i].coord,
2003                                           invalid_table[i].lpNumCharsWritten);
2004         ok(!ret, "[%d] Expected FillConsoleOutputCharacterA to return FALSE, got %d\n", i, ret);
2005         if (invalid_table[i].lpNumCharsWritten)
2006         {
2007             ok(count == invalid_table[i].expected_count,
2008                "[%d] Expected count to be %u, got %u\n",
2009                i, invalid_table[i].expected_count, count);
2010         }
2011         ok(GetLastError() == invalid_table[i].last_error,
2012            "[%d] Expected last error to be %u, got %u\n",
2013            i, invalid_table[i].last_error, GetLastError());
2014     }
2015
2016     count = 0xdeadbeef;
2017     ret = FillConsoleOutputCharacterA(output_handle, 'a', 0, origin, &count);
2018     ok(ret == TRUE, "Expected FillConsoleOutputCharacterA to return TRUE, got %d\n", ret);
2019     ok(count == 0, "Expected count to be 0, got %u\n", count);
2020
2021     count = 0xdeadbeef;
2022     ret = FillConsoleOutputCharacterA(output_handle, 'a', 1, origin, &count);
2023     ok(ret == TRUE, "Expected FillConsoleOutputCharacterA to return TRUE, got %d\n", ret);
2024     ok(count == 1, "Expected count to be 1, got %u\n", count);
2025 }
2026
2027 static void test_FillConsoleOutputCharacterW(HANDLE output_handle)
2028 {
2029     COORD origin = {0, 0};
2030     DWORD count;
2031     BOOL ret;
2032     int i;
2033
2034     const struct
2035     {
2036         HANDLE hConsoleOutput;
2037         WCHAR ch;
2038         DWORD length;
2039         COORD coord;
2040         LPDWORD lpNumCharsWritten;
2041         DWORD expected_count;
2042         DWORD last_error;
2043         int win7_crash;
2044     } invalid_table[] =
2045     {
2046         {NULL, 'a', 0, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
2047         {NULL, 'a', 0, origin, &count, 0, ERROR_INVALID_HANDLE},
2048         {NULL, 'a', 1, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
2049         {NULL, 'a', 1, origin, &count, 0, ERROR_INVALID_HANDLE},
2050         {INVALID_HANDLE_VALUE, 'a', 0, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
2051         {INVALID_HANDLE_VALUE, 'a', 0, origin, &count, 0, ERROR_INVALID_HANDLE},
2052         {INVALID_HANDLE_VALUE, 'a', 1, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
2053         {INVALID_HANDLE_VALUE, 'a', 1, origin, &count, 0, ERROR_INVALID_HANDLE},
2054         {output_handle, 'a', 0, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
2055         {output_handle, 'a', 1, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
2056     };
2057
2058     for (i = 0; i < sizeof(invalid_table)/sizeof(invalid_table[0]); i++)
2059     {
2060         if (invalid_table[i].win7_crash)
2061             continue;
2062
2063         SetLastError(0xdeadbeef);
2064         if (invalid_table[i].lpNumCharsWritten) count = 0xdeadbeef;
2065         ret = FillConsoleOutputCharacterW(invalid_table[i].hConsoleOutput,
2066                                           invalid_table[i].ch,
2067                                           invalid_table[i].length,
2068                                           invalid_table[i].coord,
2069                                           invalid_table[i].lpNumCharsWritten);
2070         ok(!ret, "[%d] Expected FillConsoleOutputCharacterW to return FALSE, got %d\n", i, ret);
2071         if (invalid_table[i].lpNumCharsWritten)
2072         {
2073             ok(count == invalid_table[i].expected_count,
2074                "[%d] Expected count to be %u, got %u\n",
2075                i, invalid_table[i].expected_count, count);
2076         }
2077         ok(GetLastError() == invalid_table[i].last_error,
2078            "[%d] Expected last error to be %u, got %u\n",
2079            i, invalid_table[i].last_error, GetLastError());
2080     }
2081
2082     count = 0xdeadbeef;
2083     ret = FillConsoleOutputCharacterW(output_handle, 'a', 0, origin, &count);
2084     ok(ret == TRUE, "Expected FillConsoleOutputCharacterW to return TRUE, got %d\n", ret);
2085     ok(count == 0, "Expected count to be 0, got %u\n", count);
2086
2087     count = 0xdeadbeef;
2088     ret = FillConsoleOutputCharacterW(output_handle, 'a', 1, origin, &count);
2089     ok(ret == TRUE, "Expected FillConsoleOutputCharacterW to return TRUE, got %d\n", ret);
2090     ok(count == 1, "Expected count to be 1, got %u\n", count);
2091 }
2092
2093 static void test_FillConsoleOutputAttribute(HANDLE output_handle)
2094 {
2095     COORD origin = {0, 0};
2096     DWORD count;
2097     BOOL ret;
2098     int i;
2099
2100     const struct
2101     {
2102         HANDLE hConsoleOutput;
2103         WORD attr;
2104         DWORD length;
2105         COORD coord;
2106         LPDWORD lpNumAttrsWritten;
2107         DWORD expected_count;
2108         DWORD last_error;
2109         int win7_crash;
2110     } invalid_table[] =
2111     {
2112         {NULL, FOREGROUND_BLUE, 0, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
2113         {NULL, FOREGROUND_BLUE, 0, origin, &count, 0, ERROR_INVALID_HANDLE},
2114         {NULL, FOREGROUND_BLUE, 1, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
2115         {NULL, FOREGROUND_BLUE, 1, origin, &count, 0, ERROR_INVALID_HANDLE},
2116         {INVALID_HANDLE_VALUE, FOREGROUND_BLUE, 0, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
2117         {INVALID_HANDLE_VALUE, FOREGROUND_BLUE, 0, origin, &count, 0, ERROR_INVALID_HANDLE},
2118         {INVALID_HANDLE_VALUE, FOREGROUND_BLUE, 1, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
2119         {INVALID_HANDLE_VALUE, FOREGROUND_BLUE, 1, origin, &count, 0, ERROR_INVALID_HANDLE},
2120         {output_handle, FOREGROUND_BLUE, 0, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
2121         {output_handle, FOREGROUND_BLUE, 1, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
2122     };
2123
2124     for (i = 0; i < sizeof(invalid_table)/sizeof(invalid_table[0]); i++)
2125     {
2126         if (invalid_table[i].win7_crash)
2127             continue;
2128
2129         SetLastError(0xdeadbeef);
2130         if (invalid_table[i].lpNumAttrsWritten) count = 0xdeadbeef;
2131         ret = FillConsoleOutputAttribute(invalid_table[i].hConsoleOutput,
2132                                          invalid_table[i].attr,
2133                                          invalid_table[i].length,
2134                                          invalid_table[i].coord,
2135                                          invalid_table[i].lpNumAttrsWritten);
2136         ok(!ret, "[%d] Expected FillConsoleOutputAttribute to return FALSE, got %d\n", i, ret);
2137         if (invalid_table[i].lpNumAttrsWritten)
2138         {
2139             ok(count == invalid_table[i].expected_count,
2140                "[%d] Expected count to be %u, got %u\n",
2141                i, invalid_table[i].expected_count, count);
2142         }
2143         ok(GetLastError() == invalid_table[i].last_error,
2144            "[%d] Expected last error to be %u, got %u\n",
2145            i, invalid_table[i].last_error, GetLastError());
2146     }
2147
2148     count = 0xdeadbeef;
2149     ret = FillConsoleOutputAttribute(output_handle, FOREGROUND_BLUE, 0, origin, &count);
2150     ok(ret == TRUE, "Expected FillConsoleOutputAttribute to return TRUE, got %d\n", ret);
2151     ok(count == 0, "Expected count to be 0, got %u\n", count);
2152
2153     count = 0xdeadbeef;
2154     ret = FillConsoleOutputAttribute(output_handle, FOREGROUND_BLUE, 1, origin, &count);
2155     ok(ret == TRUE, "Expected FillConsoleOutputAttribute to return TRUE, got %d\n", ret);
2156     ok(count == 1, "Expected count to be 1, got %u\n", count);
2157
2158     count = 0xdeadbeef;
2159     ret = FillConsoleOutputAttribute(output_handle, ~0, 1, origin, &count);
2160     ok(ret == TRUE, "Expected FillConsoleOutputAttribute to return TRUE, got %d\n", ret);
2161     ok(count == 1, "Expected count to be 1, got %u\n", count);
2162 }
2163
2164 static void test_ReadConsoleOutputCharacterA(HANDLE output_handle)
2165 {
2166     CHAR read;
2167     COORD origin = {0, 0};
2168     DWORD count;
2169     BOOL ret;
2170     int i;
2171
2172     const struct
2173     {
2174         HANDLE hConsoleOutput;
2175         LPSTR lpstr;
2176         DWORD length;
2177         COORD coord;
2178         LPDWORD read_count;
2179         DWORD expected_count;
2180         DWORD last_error;
2181         int win7_crash;
2182     } invalid_table[] =
2183     {
2184         {NULL, NULL, 0, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
2185         {NULL, NULL, 0, origin, &count, 0, ERROR_INVALID_HANDLE},
2186         {NULL, NULL, 1, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
2187         {NULL, NULL, 1, origin, &count, 0, ERROR_INVALID_HANDLE, 1},
2188         {NULL, &read, 0, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
2189         {NULL, &read, 0, origin, &count, 0, ERROR_INVALID_HANDLE},
2190         {NULL, &read, 1, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
2191         {NULL, &read, 1, origin, &count, 0, ERROR_INVALID_HANDLE},
2192         {INVALID_HANDLE_VALUE, NULL, 0, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
2193         {INVALID_HANDLE_VALUE, NULL, 0, origin, &count, 0, ERROR_INVALID_HANDLE},
2194         {INVALID_HANDLE_VALUE, NULL, 1, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
2195         {INVALID_HANDLE_VALUE, NULL, 1, origin, &count, 0, ERROR_INVALID_HANDLE, 1},
2196         {INVALID_HANDLE_VALUE, &read, 0, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
2197         {INVALID_HANDLE_VALUE, &read, 0, origin, &count, 0, ERROR_INVALID_HANDLE},
2198         {INVALID_HANDLE_VALUE, &read, 1, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
2199         {INVALID_HANDLE_VALUE, &read, 1, origin, &count, 0, ERROR_INVALID_HANDLE},
2200         {output_handle, NULL, 0, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
2201         {output_handle, NULL, 1, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
2202         {output_handle, NULL, 1, origin, &count, 1, ERROR_INVALID_ACCESS, 1},
2203         {output_handle, NULL, 10, origin, &count, 10, ERROR_INVALID_ACCESS, 1},
2204         {output_handle, &read, 0, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
2205         {output_handle, &read, 1, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
2206     };
2207
2208     for (i = 0; i < sizeof(invalid_table)/sizeof(invalid_table[0]); i++)
2209     {
2210         if (invalid_table[i].win7_crash)
2211             continue;
2212
2213         SetLastError(0xdeadbeef);
2214         if (invalid_table[i].read_count) count = 0xdeadbeef;
2215         ret = ReadConsoleOutputCharacterA(invalid_table[i].hConsoleOutput,
2216                                           invalid_table[i].lpstr,
2217                                           invalid_table[i].length,
2218                                           invalid_table[i].coord,
2219                                           invalid_table[i].read_count);
2220         ok(!ret, "[%d] Expected ReadConsoleOutputCharacterA to return FALSE, got %d\n", i, ret);
2221         if (invalid_table[i].read_count)
2222         {
2223             ok(count == invalid_table[i].expected_count,
2224                "[%d] Expected count to be %u, got %u\n",
2225                i, invalid_table[i].expected_count, count);
2226         }
2227         ok(GetLastError() == invalid_table[i].last_error,
2228            "[%d] Expected last error to be %u, got %u\n",
2229            i, invalid_table[i].last_error, GetLastError());
2230     }
2231
2232     count = 0xdeadbeef;
2233     ret = ReadConsoleOutputCharacterA(output_handle, NULL, 0, origin, &count);
2234     ok(ret == TRUE, "Expected ReadConsoleOutputCharacterA to return TRUE, got %d\n", ret);
2235     ok(count == 0, "Expected count to be 0, got %u\n", count);
2236
2237     count = 0xdeadbeef;
2238     ret = ReadConsoleOutputCharacterA(output_handle, &read, 0, origin, &count);
2239     ok(ret == TRUE, "Expected ReadConsoleOutputCharacterA to return TRUE, got %d\n", ret);
2240     ok(count == 0, "Expected count to be 0, got %u\n", count);
2241
2242     count = 0xdeadbeef;
2243     ret = ReadConsoleOutputCharacterA(output_handle, &read, 1, origin, &count);
2244     ok(ret == TRUE, "Expected ReadConsoleOutputCharacterA to return TRUE, got %d\n", ret);
2245     ok(count == 1, "Expected count to be 1, got %u\n", count);
2246 }
2247
2248 static void test_ReadConsoleOutputCharacterW(HANDLE output_handle)
2249 {
2250     WCHAR read;
2251     COORD origin = {0, 0};
2252     DWORD count;
2253     BOOL ret;
2254     int i;
2255
2256     const struct
2257     {
2258         HANDLE hConsoleOutput;
2259         LPWSTR buffer;
2260         DWORD length;
2261         COORD coord;
2262         LPDWORD read_count;
2263         DWORD expected_count;
2264         DWORD last_error;
2265         int win7_crash;
2266     } invalid_table[] =
2267     {
2268         {NULL, NULL, 0, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
2269         {NULL, NULL, 0, origin, &count, 0, ERROR_INVALID_HANDLE},
2270         {NULL, NULL, 1, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
2271         {NULL, NULL, 1, origin, &count, 0, ERROR_INVALID_HANDLE, 1},
2272         {NULL, &read, 0, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
2273         {NULL, &read, 0, origin, &count, 0, ERROR_INVALID_HANDLE},
2274         {NULL, &read, 1, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
2275         {NULL, &read, 1, origin, &count, 0, ERROR_INVALID_HANDLE},
2276         {INVALID_HANDLE_VALUE, NULL, 0, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
2277         {INVALID_HANDLE_VALUE, NULL, 0, origin, &count, 0, ERROR_INVALID_HANDLE},
2278         {INVALID_HANDLE_VALUE, NULL, 1, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
2279         {INVALID_HANDLE_VALUE, NULL, 1, origin, &count, 0, ERROR_INVALID_HANDLE, 1},
2280         {INVALID_HANDLE_VALUE, &read, 0, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
2281         {INVALID_HANDLE_VALUE, &read, 0, origin, &count, 0, ERROR_INVALID_HANDLE},
2282         {INVALID_HANDLE_VALUE, &read, 1, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
2283         {INVALID_HANDLE_VALUE, &read, 1, origin, &count, 0, ERROR_INVALID_HANDLE},
2284         {output_handle, NULL, 0, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
2285         {output_handle, NULL, 1, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
2286         {output_handle, NULL, 1, origin, &count, 1, ERROR_INVALID_ACCESS, 1},
2287         {output_handle, NULL, 10, origin, &count, 10, ERROR_INVALID_ACCESS, 1},
2288         {output_handle, &read, 0, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
2289         {output_handle, &read, 1, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
2290     };
2291
2292     for (i = 0; i < sizeof(invalid_table)/sizeof(invalid_table[0]); i++)
2293     {
2294         if (invalid_table[i].win7_crash)
2295             continue;
2296
2297         SetLastError(0xdeadbeef);
2298         if (invalid_table[i].read_count) count = 0xdeadbeef;
2299         ret = ReadConsoleOutputCharacterW(invalid_table[i].hConsoleOutput,
2300                                           invalid_table[i].buffer,
2301                                           invalid_table[i].length,
2302                                           invalid_table[i].coord,
2303                                           invalid_table[i].read_count);
2304         ok(!ret, "[%d] Expected ReadConsoleOutputCharacterW to return FALSE, got %d\n", i, ret);
2305         if (invalid_table[i].read_count)
2306         {
2307             ok(count == invalid_table[i].expected_count,
2308                "[%d] Expected count to be %u, got %u\n",
2309                i, invalid_table[i].expected_count, count);
2310         }
2311         ok(GetLastError() == invalid_table[i].last_error,
2312            "[%d] Expected last error to be %u, got %u\n",
2313            i, invalid_table[i].last_error, GetLastError());
2314     }
2315
2316     count = 0xdeadbeef;
2317     ret = ReadConsoleOutputCharacterW(output_handle, NULL, 0, origin, &count);
2318     ok(ret == TRUE, "Expected ReadConsoleOutputCharacterW to return TRUE, got %d\n", ret);
2319     ok(count == 0, "Expected count to be 0, got %u\n", count);
2320
2321     count = 0xdeadbeef;
2322     ret = ReadConsoleOutputCharacterW(output_handle, &read, 0, origin, &count);
2323     ok(ret == TRUE, "Expected ReadConsoleOutputCharacterW to return TRUE, got %d\n", ret);
2324     ok(count == 0, "Expected count to be 0, got %u\n", count);
2325
2326     count = 0xdeadbeef;
2327     ret = ReadConsoleOutputCharacterW(output_handle, &read, 1, origin, &count);
2328     ok(ret == TRUE, "Expected ReadConsoleOutputCharacterW to return TRUE, got %d\n", ret);
2329     ok(count == 1, "Expected count to be 1, got %u\n", count);
2330 }
2331
2332 static void test_ReadConsoleOutputAttribute(HANDLE output_handle)
2333 {
2334     WORD attr;
2335     COORD origin = {0, 0};
2336     DWORD count;
2337     BOOL ret;
2338     int i;
2339
2340     const struct
2341     {
2342         HANDLE hConsoleOutput;
2343         LPWORD lpAttribute;
2344         DWORD length;
2345         COORD coord;
2346         LPDWORD read_count;
2347         DWORD expected_count;
2348         DWORD last_error;
2349         int win7_crash;
2350     } invalid_table[] =
2351     {
2352         {NULL, NULL, 0, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
2353         {NULL, NULL, 0, origin, &count, 0, ERROR_INVALID_HANDLE},
2354         {NULL, NULL, 1, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
2355         {NULL, NULL, 1, origin, &count, 0, ERROR_INVALID_HANDLE, 1},
2356         {NULL, &attr, 0, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
2357         {NULL, &attr, 0, origin, &count, 0, ERROR_INVALID_HANDLE},
2358         {NULL, &attr, 1, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
2359         {NULL, &attr, 1, origin, &count, 0, ERROR_INVALID_HANDLE},
2360         {INVALID_HANDLE_VALUE, NULL, 0, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
2361         {INVALID_HANDLE_VALUE, NULL, 0, origin, &count, 0, ERROR_INVALID_HANDLE},
2362         {INVALID_HANDLE_VALUE, NULL, 1, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
2363         {INVALID_HANDLE_VALUE, NULL, 1, origin, &count, 0, ERROR_INVALID_HANDLE, 1},
2364         {INVALID_HANDLE_VALUE, &attr, 0, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
2365         {INVALID_HANDLE_VALUE, &attr, 0, origin, &count, 0, ERROR_INVALID_HANDLE},
2366         {INVALID_HANDLE_VALUE, &attr, 1, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
2367         {INVALID_HANDLE_VALUE, &attr, 1, origin, &count, 0, ERROR_INVALID_HANDLE},
2368         {output_handle, NULL, 0, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
2369         {output_handle, NULL, 1, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
2370         {output_handle, NULL, 1, origin, &count, 1, ERROR_INVALID_ACCESS, 1},
2371         {output_handle, &attr, 0, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
2372         {output_handle, &attr, 1, origin, NULL, 0xdeadbeef, ERROR_INVALID_ACCESS, 1},
2373     };
2374
2375     for (i = 0; i < sizeof(invalid_table)/sizeof(invalid_table[0]); i++)
2376     {
2377         if (invalid_table[i].win7_crash)
2378             continue;
2379
2380         SetLastError(0xdeadbeef);
2381         if (invalid_table[i].read_count) count = 0xdeadbeef;
2382         ret = ReadConsoleOutputAttribute(invalid_table[i].hConsoleOutput,
2383                                          invalid_table[i].lpAttribute,
2384                                          invalid_table[i].length,
2385                                          invalid_table[i].coord,
2386                                          invalid_table[i].read_count);
2387         ok(!ret, "[%d] Expected ReadConsoleOutputAttribute to return FALSE, got %d\n", i, ret);
2388         if (invalid_table[i].read_count)
2389         {
2390             ok(count == invalid_table[i].expected_count,
2391                "[%d] Expected count to be %u, got %u\n",
2392                i, invalid_table[i].expected_count, count);
2393         }
2394         ok(GetLastError() == invalid_table[i].last_error,
2395            "[%d] Expected last error to be %u, got %u\n",
2396            i, invalid_table[i].last_error, GetLastError());
2397     }
2398
2399     count = 0xdeadbeef;
2400     ret = ReadConsoleOutputAttribute(output_handle, NULL, 0, origin, &count);
2401     ok(ret == TRUE, "Expected ReadConsoleOutputAttribute to return TRUE, got %d\n", ret);
2402     ok(count == 0, "Expected count to be 0, got %u\n", count);
2403
2404     count = 0xdeadbeef;
2405     ret = ReadConsoleOutputAttribute(output_handle, &attr, 0, origin, &count);
2406     ok(ret == TRUE, "Expected ReadConsoleOutputAttribute to return TRUE, got %d\n", ret);
2407     ok(count == 0, "Expected count to be 0, got %u\n", count);
2408
2409     count = 0xdeadbeef;
2410     ret = ReadConsoleOutputAttribute(output_handle, &attr, 1, origin, &count);
2411     ok(ret == TRUE, "Expected ReadConsoleOutputAttribute to return TRUE, got %d\n", ret);
2412     ok(count == 1, "Expected count to be 1, got %u\n", count);
2413 }
2414
2415 START_TEST(console)
2416 {
2417     static const char font_name[] = "Lucida Console";
2418     HANDLE hConIn, hConOut;
2419     BOOL ret;
2420     CONSOLE_SCREEN_BUFFER_INFO  sbi;
2421     LONG err;
2422     HKEY console_key;
2423     char old_font[LF_FACESIZE];
2424     BOOL delete = FALSE;
2425     DWORD size;
2426
2427     init_function_pointers();
2428
2429     /* be sure we have a clean console (and that's our own)
2430      * FIXME: this will make the test fail (currently) if we don't run
2431      * under X11
2432      * Another solution would be to rerun the test under wineconsole with
2433      * the curses backend
2434      */
2435
2436     /* ReadConsoleOutputW doesn't retrieve characters from the output buffer
2437      * correctly for characters that don't have a glyph in the console font. So,
2438      * we first set the console font to Lucida Console (which has a wider
2439      * selection of glyphs available than the default raster fonts). We want
2440      * to be able to restore the original font afterwards, so don't change
2441      * if we can't read the original font.
2442      */
2443     err = RegOpenKeyExA(HKEY_CURRENT_USER, "Console", 0,
2444                         KEY_QUERY_VALUE | KEY_SET_VALUE, &console_key);
2445     if (err == ERROR_SUCCESS)
2446     {
2447         size = sizeof(old_font);
2448         err = RegQueryValueExA(console_key, "FaceName", NULL, NULL,
2449                                (LPBYTE) old_font, &size);
2450         if (err == ERROR_SUCCESS || err == ERROR_FILE_NOT_FOUND)
2451         {
2452             delete = (err == ERROR_FILE_NOT_FOUND);
2453             err = RegSetValueExA(console_key, "FaceName", 0, REG_SZ,
2454                                  (const BYTE *) font_name, sizeof(font_name));
2455             if (err != ERROR_SUCCESS)
2456                 trace("Unable to change default console font, error %d\n", err);
2457         }
2458         else
2459         {
2460             trace("Unable to query default console font, error %d\n", err);
2461             RegCloseKey(console_key);
2462             console_key = NULL;
2463         }
2464     }
2465     else
2466     {
2467         trace("Unable to open HKCU\\Console, error %d\n", err);
2468         console_key = NULL;
2469     }
2470
2471     /* Now detach and open a fresh console to play with */
2472     FreeConsole();
2473     ok(AllocConsole(), "Couldn't alloc console\n");
2474
2475     /* Restore default console font if needed */
2476     if (console_key != NULL)
2477     {
2478         if (delete)
2479             err = RegDeleteValueA(console_key, "FaceName");
2480         else
2481             err = RegSetValueExA(console_key, "FaceName", 0, REG_SZ,
2482                                  (const BYTE *) old_font, strlen(old_font) + 1);
2483         ok(err == ERROR_SUCCESS, "Unable to restore default console font, error %d\n", err);
2484     }
2485     hConIn = CreateFileA("CONIN$", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
2486     hConOut = CreateFileA("CONOUT$", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
2487
2488     /* now verify everything's ok */
2489     ok(hConIn != INVALID_HANDLE_VALUE, "Opening ConIn\n");
2490     ok(hConOut != INVALID_HANDLE_VALUE, "Opening ConOut\n");
2491
2492     ret = GetConsoleScreenBufferInfo(hConOut, &sbi);
2493     ok(ret, "Getting sb info\n");
2494     if (!ret) return;
2495
2496     /* Non interactive tests */
2497     testCursor(hConOut, sbi.dwSize);
2498     /* test parameters (FIXME: test functionality) */
2499     testCursorInfo(hConOut);
2500     /* will test wrapped (on/off) & processed (on/off) strings output */
2501     testWrite(hConOut, sbi.dwSize);
2502     /* will test line scrolling at the bottom of the screen */
2503     /* testBottomScroll(); */
2504     /* will test all the scrolling operations */
2505     testScroll(hConOut, sbi.dwSize);
2506     /* will test sb creation / modification / codepage handling */
2507     testScreenBuffer(hConOut);
2508     testCtrlHandler();
2509     /* still to be done: access rights & access on objects */
2510
2511     if (!pGetConsoleInputExeNameA || !pSetConsoleInputExeNameA)
2512         win_skip("GetConsoleInputExeNameA and/or SetConsoleInputExeNameA is not available\n");
2513     else
2514         test_GetSetConsoleInputExeName();
2515
2516     test_GetConsoleProcessList();
2517     test_OpenConsoleW();
2518     test_VerifyConsoleIoHandle(hConOut);
2519     test_GetSetStdHandle();
2520     test_GetNumberOfConsoleInputEvents(hConIn);
2521     test_WriteConsoleInputA(hConIn);
2522     test_WriteConsoleInputW(hConIn);
2523     test_WriteConsoleOutputCharacterA(hConOut);
2524     test_WriteConsoleOutputCharacterW(hConOut);
2525     test_WriteConsoleOutputAttribute(hConOut);
2526     test_FillConsoleOutputCharacterA(hConOut);
2527     test_FillConsoleOutputCharacterW(hConOut);
2528     test_FillConsoleOutputAttribute(hConOut);
2529     test_ReadConsoleOutputCharacterA(hConOut);
2530     test_ReadConsoleOutputCharacterW(hConOut);
2531     test_ReadConsoleOutputAttribute(hConOut);
2532 }