user32/tests: Fix the listbox delete test on NT4.
[wine] / dlls / kernel32 / tests / comm.c
1 /* Unit test suite for comm functions
2  *
3  * Copyright 2003 Kevin Groeneveld
4  * Copyright 2005 Uwe Bonnes
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <stdio.h>
22
23 #include "wine/test.h"
24 #include "winbase.h"
25 #include "winnls.h"
26
27 #define TIMEOUT 1000   /* one second for Timeouts*/
28 #define SLOWBAUD 150
29 #define FASTBAUD 115200
30 #define TIMEDELTA 150  /* 150 ms uncertainty allowed */
31
32 /* Define the appropriate LOOPBACK(s) TRUE if you have a Loopback cable with
33  * the mentioned shorts connected to your Serial port
34  */
35 #define LOOPBACK_TXD_RXD  FALSE /* Sub-D 9: Short 2-3 */
36 #define LOOPBACK_CTS_RTS  FALSE /* Sub-D 9: Short 7-8 */
37 #define LOOPBACK_DTR_DSR  FALSE /* Sub-D 9: Short 4-6 */
38 #define LOOPBACK_DTR_RING FALSE /* Sub-D 9: Short 4-9 */
39 #define LOOPBACK_DTR_DCD  FALSE /* Sub-D 9: Short 4-1 */
40 /* Many Linux serial drivers have the TIOCM_LOOP flag in the TIOCM_SET ioctl
41  * available. For the 8250 this is equivalent to TXD->RXD, OUT2->DCD,
42  * OUT1->RI, RTS->CTS and DTR->DSR
43  */
44
45 typedef struct
46 {
47         char string[100];
48         BOOL result;
49         BOOL old_style;
50         DCB dcb1, dcb2;
51         COMMTIMEOUTS timeouts1, timeouts2;
52 } TEST;
53
54 static TEST test[] =
55 {
56         {
57                 "baud=9600 parity=e data=5 stop=1 xon=on odsr=off octs=off dtr=on rts=on idsr=on",
58                 TRUE, FALSE,
59                 { 0x00000000, 0x00002580, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x05, 0x02, 0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, 0x0000 },
60                 { 0xffffffff, 0x00002580, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0x05, 0x02, 0x00, (char)0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, 0xffff },
61                 { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
62                 { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
63         },
64         {
65                 "baud=0 parity=M data=6 stop=1.5 xon=off odsr=on octs=ON dtr=off rts=off idsr=OFF",
66                 TRUE, FALSE,
67                 { 0x00000000, 0x00000000, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x06, 0x03, 0x01, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, 0x0000 },
68                 { 0xffffffff, 0x00000000, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0x06, 0x03, 0x01, (char)0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, 0xffff },
69                 { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
70                 { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
71         },
72         {
73                 "BAUD=4000000000 parity=n data=7 stop=2 to=off",
74                 TRUE, FALSE,
75                 { 0x00000000, 0xee6b2800, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x07, 0x00, 0x02, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, 0x0000 },
76                 { 0xffffffff, 0xee6b2800, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0x07, 0x00, 0x02, (char)0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, 0xffff },
77                 { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
78                 { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 }
79         },
80         {
81                 "Baud=115200 Parity=O Data=8 To=On",
82                 TRUE, FALSE,
83                 { 0x00000000, 0x0001c200, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x08, 0x01, 0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, 0x0000 },
84                 { 0xffffffff, 0x0001c200, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0x08, 0x01, 0x00, (char)0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, 0xffff },
85                 { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x0000EA60 },
86                 { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x0000EA60 }
87         },
88         {
89                 "PaRiTy=s           Data=7          DTR=on",
90                 TRUE, FALSE,
91                 { 0x00000000, 0x00000000, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x07, 0x04, 0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, 0x0000 },
92                 { 0xffffffff, 0xffffffff, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0x07, 0x04, 0x00, (char)0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, 0xffff },
93                 { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
94                 { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
95         },
96         {
97                 "data=4",
98                 FALSE, FALSE,
99                 { 0x00000000, 0x00000000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x00, 0x00, 0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, 0x0000 },
100                 { 0xffffffff, 0xffffffff, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0xff, 0xff, 0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, 0xffff },
101                 { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
102                 { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
103         },
104         {
105                 "data=9",
106                 FALSE, FALSE,
107                 { 0x00000000, 0x00000000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x00, 0x00, 0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, 0x0000 },
108                 { 0xffffffff, 0xffffffff, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0xff, 0xff, 0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, 0xffff },
109                 { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
110                 { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
111         },
112         {
113                 "parity=no",
114                 FALSE, FALSE,
115                 { 0x00000000, 0x00000000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x00, 0x00, 0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, 0x0000 },
116                 { 0xffffffff, 0xffffffff, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0xff, 0xff, 0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, 0xffff },
117                 { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
118                 { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
119         },
120         {
121                 "stop=0",
122                 FALSE, FALSE,
123                 { 0x00000000, 0x00000000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x00, 0x00, 0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, 0x0000 },
124                 { 0xffffffff, 0xffffffff, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0xff, 0xff, 0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, 0xffff },
125                 { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
126                 { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
127         },
128         {
129                 "stop=1.501",
130                 FALSE, FALSE,
131                 { 0x00000000, 0x00000000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x00, 0x00, 0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, 0x0000 },
132                 { 0xffffffff, 0xffffffff, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0xff, 0xff, 0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, 0xffff },
133                 { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
134                 { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
135         },
136         {
137                 "stop=3",
138                 FALSE, FALSE,
139                 { 0x00000000, 0x00000000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x00, 0x00, 0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, 0x0000 },
140                 { 0xffffffff, 0xffffffff, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0xff, 0xff, 0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, 0xffff },
141                 { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
142                 { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
143         },
144         {
145                 "to=foobar",
146                 FALSE, FALSE,
147                 { 0x00000000, 0x00000000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x00, 0x00, 0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, 0x0000 },
148                 { 0xffffffff, 0xffffffff, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0xff, 0xff, 0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, 0xffff },
149                 { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
150                 { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
151         },
152         {
153                 " baud=9600",
154                 FALSE, FALSE,
155                 { 0x00000000, 0x00000000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x00, 0x00, 0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, 0x0000 },
156                 { 0xffffffff, 0xffffffff, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0xff, 0xff, 0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, 0xffff },
157                 { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
158                 { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
159         },
160         {
161                 "baud= 9600",
162                 FALSE, FALSE,
163                 { 0x00000000, 0x00000000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x00, 0x00, 0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, 0x0000 },
164                 { 0xffffffff, 0xffffffff, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0xff, 0xff, 0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, 0xffff },
165                 { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
166                 { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
167         },
168         {
169                 "baud=9600,data=8",
170                 FALSE, FALSE,
171                 { 0x00000000, 0x00000000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x00, 0x00, 0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, 0x0000 },
172                 { 0xffffffff, 0xffffffff, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0xff, 0xff, 0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, 0xffff },
173                 { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
174                 { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
175         },
176         {
177                 "11,n,8,1",
178                 TRUE, TRUE,
179                 { 0x00000000, 0x0000006e, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x08, 0x00, 0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, 0x0000 },
180                 { 0xffffffff, 0x0000006e, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0x08, 0x00, 0x00, (char)0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, 0xffff },
181                 { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
182                 { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
183         },
184         {
185                 "30 ,E, 5,1.5",
186                 TRUE, TRUE,
187                 { 0x00000000, 0x0000012c, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x05, 0x02, 0x01, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, 0x0000 },
188                 { 0xffffffff, 0x0000012c, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0x05, 0x02, 0x01, (char)0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, 0xffff },
189                 { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
190                 { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
191         },
192         {
193                 "60, m, 6, 2 ",
194                 TRUE, TRUE,
195                 { 0x00000000, 0x00000258, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x06, 0x03, 0x02, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, 0x0000 },
196                 { 0xffffffff, 0x00000258, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0x06, 0x03, 0x02, (char)0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, 0xffff },
197                 { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
198                 { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
199         },
200         {
201                 "12 , o , 7 , 1",
202                 TRUE, TRUE,
203                 { 0x00000000, 0x000004b0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x07, 0x01, 0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, 0x0000 },
204                 { 0xffffffff, 0x000004b0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0x07, 0x01, 0x00, (char)0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, 0xffff },
205                 { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
206                 { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
207         },
208         {
209                 "24,s,8,1.5",
210                 TRUE, TRUE,
211                 { 0x00000000, 0x00000960, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x08, 0x04, 0x01, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, 0x0000 },
212                 { 0xffffffff, 0x00000960, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0x08, 0x04, 0x01, (char)0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, 0xffff },
213                 { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
214                 { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
215         },
216         {
217                 "48,n,8,1,p",
218                 TRUE, TRUE,
219                 { 0x00000000, 0x000012c0, 0, 0, 1, 1, 2, 0, 0, 0, 0, 0, 0, 2, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x08, 0x00, 0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, 0x0000 },
220                 { 0xffffffff, 0x000012c0, 1, 1, 1, 1, 2, 1, 1, 0, 0, 1, 1, 2, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0x08, 0x00, 0x00, (char)0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, 0xffff },
221                 { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
222                 { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
223         },
224         {
225                 "96,N,8,1 , x ",
226                 TRUE, TRUE,
227                 { 0x00000000, 0x00002580, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x08, 0x00, 0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, 0x0000 },
228                 { 0xffffffff, 0x00002580, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0x08, 0x00, 0x00, (char)0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, 0xffff },
229                 { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
230                 { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
231         },
232         {
233                 "19, e, 7, 1, x",
234                 TRUE, TRUE,
235                 { 0x00000000, 0x00004b00, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x07, 0x02, 0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, 0x0000 },
236                 { 0xffffffff, 0x00004b00, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0x07, 0x02, 0x00, (char)0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, 0xffff },
237                 { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
238                 { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
239         },
240         {
241                 "0,M,7,1,P",
242                 TRUE, TRUE,
243                 { 0x00000000, 0x00000000, 0, 0, 1, 1, 2, 0, 0, 0, 0, 0, 0, 2, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x07, 0x03, 0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, 0x0000 },
244                 { 0xffffffff, 0x00000000, 1, 1, 1, 1, 2, 1, 1, 0, 0, 1, 1, 2, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0x07, 0x03, 0x00, (char)0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, 0xffff },
245                 { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
246                 { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
247         },
248         {
249                 "4000000000,O,7,1.5,X",
250                 TRUE, TRUE,
251                 { 0x00000000, 0xee6b2800, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x07, 0x01, 0x01, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, 0x0000 },
252                 { 0xffffffff, 0xee6b2800, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0x07, 0x01, 0x01, (char)0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, 0xffff },
253                 { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
254                 { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
255         },
256         {
257                 "96,N,8,1 to=on",
258                 FALSE, TRUE,
259                 { 0x00000000, 0x00000000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x00, 0x00, 0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, 0x0000 },
260                 { 0xffffffff, 0xffffffff, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0xff, 0xff, 0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, 0xffff },
261                 { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
262                 { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
263         },
264         {
265                 "96,NO,8,1",
266                 FALSE, TRUE,
267                 { 0x00000000, 0x00000000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x00, 0x00, 0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, 0x0000 },
268                 { 0xffffffff, 0xffffffff, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0xff, 0xff, 0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, 0xffff },
269                 { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
270                 { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
271         },
272         {
273                 "96,N,4,1",
274                 FALSE, TRUE,
275                 { 0x00000000, 0x00000000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x00, 0x00, 0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, 0x0000 },
276                 { 0xffffffff, 0xffffffff, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0xff, 0xff, 0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, 0xffff },
277                 { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
278                 { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
279         },
280         {
281                 "96,N,9,1",
282                 FALSE, TRUE,
283                 { 0x00000000, 0x00000000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x00, 0x00, 0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, 0x0000 },
284                 { 0xffffffff, 0xffffffff, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0xff, 0xff, 0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, 0xffff },
285                 { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
286                 { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
287         },
288         {
289                 "96,N,8,0",
290                 FALSE, TRUE,
291                 { 0x00000000, 0x00000000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x00, 0x00, 0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, 0x0000 },
292                 { 0xffffffff, 0xffffffff, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0xff, 0xff, 0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, 0xffff },
293                 { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
294                 { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
295         },
296         {
297                 "96,N,8,3",
298                 FALSE, TRUE,
299                 { 0x00000000, 0x00000000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x00, 0x00, 0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, 0x0000 },
300                 { 0xffffffff, 0xffffffff, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0xff, 0xff, 0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, 0xffff },
301                 { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
302                 { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
303         },
304         {
305                 "96,N,8,1,K",
306                 FALSE, TRUE,
307                 { 0x00000000, 0x00000000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x00, 0x00, 0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, 0x0000 },
308                 { 0xffffffff, 0xffffffff, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0xff, 0xff, 0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, 0xffff },
309                 { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
310                 { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
311         },
312         {
313                 "COM0:baud=115200",
314                 FALSE, FALSE,
315                 { 0x00000000, 0x00000000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x00, 0x00, 0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, 0x0000 },
316                 { 0xffffffff, 0xffffffff, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0xff, 0xff, 0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, 0xffff },
317                 { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
318                 { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
319         },
320         {
321                 "COMx:baud=38400 data=8",
322                 TRUE, FALSE,
323                 { 0x00000000, 0x00009600, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x08, 0x00, 0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, 0x0000 },
324                 { 0xffffffff, 0x00009600, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0x08, 0xff, 0x00, (char)0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, 0xffff },
325                 { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
326                 { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
327         },
328         {
329                 "COMx  :to=on stop=1.5",
330                 TRUE, FALSE,
331                 { 0x00000000, 0x00000000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x00, 0x00, 0x01, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, 0x0000 },
332                 { 0xffffffff, 0xffffffff, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0xff, 0xff, 0x01, (char)0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, 0xffff },
333                 { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x0000EA60 },
334                 { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x0000EA60 }
335         },
336         {
337                 "COMx:               baud=12345     data=7",
338                 TRUE, FALSE,
339                 { 0x00000000, 0x00003039, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x07, 0x00, 0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, 0x0000 },
340                 { 0xffffffff, 0x00003039, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0x07, 0xff, 0x00, (char)0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, 0xffff },
341                 { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
342                 { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
343         },
344         {
345                 "COMx : xon=on odsr=off",
346                 TRUE, FALSE,
347                 { 0x00000000, 0x00000000, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x00, 0x00, 0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, 0x0000 },
348                 { 0xffffffff, 0xffffffff, 1, 1, 1, 0, 3, 1, 1, 1, 1, 1, 1, 3, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0xff, 0xff, 0x00, (char)0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, 0xffff },
349                 { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
350                 { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
351         },
352         {
353                 "COM0:9600,N,8,1",
354                 FALSE, TRUE,
355                 { 0x00000000, 0x00000000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x00, 0x00, 0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, 0x0000 },
356                 { 0xffffffff, 0xffffffff, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0xff, 0xff, 0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, 0xffff },
357                 { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
358                 { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
359         },
360         {
361                 "COMx:9600,N,8,1",
362                 TRUE, TRUE,
363                 { 0x00000000, 0x00002580, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x08, 0x00, 0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, 0x0000 },
364                 { 0xffffffff, 0x00002580, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0x08, 0x00, 0x00, (char)0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, 0xffff },
365                 { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
366                 { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
367         },
368         {
369                 "COMx:  11,E,7,2",
370                 TRUE, TRUE,
371                 { 0x00000000, 0x0000006e, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x07, 0x02, 0x02, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, 0x0000 },
372                 { 0xffffffff, 0x0000006e, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0x07, 0x02, 0x02, (char)0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, 0xffff },
373                 { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
374                 { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
375         },
376         {
377                 "COMx  :19,M,5,1",
378                 TRUE, TRUE,
379                 { 0x00000000, 0x00004b00, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x05, 0x03, 0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, 0x0000 },
380                 { 0xffffffff, 0x00004b00, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0x05, 0x03, 0x00, (char)0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, 0xffff },
381                 { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
382                 { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
383         },
384         {
385                 "COMx  :    57600,S,6,2,x",
386                 TRUE, TRUE,
387                 { 0x00000000, 0x0000e100, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x06, 0x04, 0x02, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, 0x0000 },
388                 { 0xffffffff, 0x0000e100, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0x06, 0x04, 0x02, (char)0xff, (char)0xff, (char)0xff, (char)0xff, (char)0xff, 0xffff },
389                 { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
390                 { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
391         },
392 };
393
394 #define TEST_COUNT (sizeof(test) / sizeof(TEST))
395
396 /* This function can be useful if you are modifying the test cases and want to
397    output the contents of a DCB structure. */
398 /*static print_dcb(DCB *pdcb)
399 {
400         printf("0x%08x, 0x%08x, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, 0x%05x, 0x%04x, 0x%04x, 0x%04x, 0x%02x, 0x%02x, 0x%02x, (char)0x%02x, (char)0x%02x, (char)0x%02x, (char)0x%02x, (char)0x%02x, 0x%04x\n",
401                 pdcb->DCBlength,
402                 pdcb->BaudRate,
403                 pdcb->fBinary,
404                 pdcb->fParity,
405                 pdcb->fOutxCtsFlow,
406                 pdcb->fOutxDsrFlow,
407                 pdcb->fDtrControl,
408                 pdcb->fDsrSensitivity,
409                 pdcb->fTXContinueOnXoff,
410                 pdcb->fOutX,
411                 pdcb->fInX,
412                 pdcb->fErrorChar,
413                 pdcb->fNull,
414                 pdcb->fRtsControl,
415                 pdcb->fAbortOnError,
416                 pdcb->fDummy2,
417                 pdcb->wReserved,
418                 pdcb->XonLim,
419                 pdcb->XoffLim,
420                 pdcb->ByteSize,
421                 pdcb->Parity,
422                 pdcb->StopBits,
423                 pdcb->XonChar & 0xff,
424                 pdcb->XoffChar & 0xff,
425                 pdcb->ErrorChar & 0xff,
426                 pdcb->EofChar & 0xff,
427                 pdcb->EvtChar & 0xff,
428                 pdcb->wReserved1 & 0xffff );
429 } */
430
431 static void check_result(const char *function, TEST *ptest, int initial_value, BOOL result)
432 {
433         DWORD LastError = GetLastError();
434         DWORD CorrectError = (ptest->result ? 0xdeadbeef : ERROR_INVALID_PARAMETER);
435
436         ok(LastError == CorrectError, "%s(\"%s\"), 0x%02x: GetLastError() returned %d, should be %d\n", function, ptest->string, initial_value, LastError, CorrectError);
437         ok(result == ptest->result, "%s(\"%s\"), 0x%02x: return value should be %s\n", function, ptest->string, initial_value, ptest->result ? "TRUE" : "FALSE");
438 }
439
440 #define check_dcb_member(a,b) ok(pdcb1->a == pdcb2->a, "%s(\"%s\"), 0x%02x: "#a" is "b", should be "b"\n", function, ptest->string, initial_value, pdcb1->a, pdcb2->a)
441 #define check_dcb_member2(a,c,b) if(pdcb2->a == c) { check_dcb_member(a,b); } else { ok(pdcb1->a == pdcb2->a || pdcb1->a == c, "%s(\"%s\"), 0x%02x: "#a" is "b", should be "b" or "b"\n", function, ptest->string, initial_value, pdcb1->a, pdcb2->a, c); }
442
443 static void check_dcb(const char *function, TEST *ptest, int initial_value, DCB *pdcb1, DCB *pdcb2)
444 {
445         /* DCBlength is a special case since Win 9x sets it but NT does not.
446            We will accept either as correct. */
447         check_dcb_member2(DCBlength, (DWORD)sizeof(DCB), "%u");
448
449         /* For old style control strings Win 9x does not set the next five members, NT does. */
450         if(ptest->old_style && ptest->result)
451         {
452                 check_dcb_member2(fOutxCtsFlow, ((unsigned int)initial_value & 1), "%u");
453                 check_dcb_member2(fDtrControl, ((unsigned int)initial_value & 3), "%u");
454                 check_dcb_member2(fOutX, ((unsigned int)initial_value & 1), "%u");
455                 check_dcb_member2(fInX, ((unsigned)initial_value & 1), "%u");
456                 check_dcb_member2(fRtsControl, ((unsigned)initial_value & 3), "%u");
457         }
458         else
459         {
460                 check_dcb_member(fOutxCtsFlow, "%u");
461                 check_dcb_member(fDtrControl, "%u");
462                 check_dcb_member(fOutX, "%u");
463                 check_dcb_member(fInX, "%u");
464                 check_dcb_member(fRtsControl, "%u");
465         }
466
467         if(ptest->result)
468         {
469                 /* For the idsr=xxx parameter, NT sets fDsrSensitivity, 9x sets
470                    fOutxDsrFlow. */
471                 if(!ptest->old_style)
472                 {
473                         check_dcb_member2(fOutxDsrFlow, pdcb2->fDsrSensitivity, "%u");
474                         check_dcb_member2(fDsrSensitivity, pdcb2->fOutxDsrFlow, "%u");
475                 }
476                 else
477                 {
478                         /* For old style control strings Win 9x does not set the
479                            fOutxDsrFlow member, NT does. */
480                         check_dcb_member2(fOutxDsrFlow, ((unsigned int)initial_value & 1), "%u");
481                         check_dcb_member(fDsrSensitivity, "%u");
482                 }
483         }
484         else
485         {
486                 check_dcb_member(fOutxDsrFlow, "%u");
487                 check_dcb_member(fDsrSensitivity, "%u");
488         }
489
490         /* Check the result of the DCB members. */
491         check_dcb_member(BaudRate, "%u");
492         check_dcb_member(fBinary, "%u");
493         check_dcb_member(fParity, "%u");
494         check_dcb_member(fTXContinueOnXoff, "%u");
495         check_dcb_member(fErrorChar, "%u");
496         check_dcb_member(fNull, "%u");
497         check_dcb_member(fAbortOnError, "%u");
498         check_dcb_member(fDummy2, "%u");
499         check_dcb_member(wReserved, "%u");
500         check_dcb_member(XonLim, "%u");
501         check_dcb_member(XoffLim, "%u");
502         check_dcb_member(ByteSize, "%u");
503         check_dcb_member(Parity, "%u");
504         check_dcb_member(StopBits, "%u");
505         check_dcb_member(XonChar, "%d");
506         check_dcb_member(XoffChar, "%d");
507         check_dcb_member(ErrorChar, "%d");
508         check_dcb_member(EofChar, "%d");
509         check_dcb_member(EvtChar, "%d");
510         check_dcb_member(wReserved1, "%u");
511 }
512
513 #define check_timeouts_member(a) ok(ptimeouts1->a == ptimeouts2->a, "%s(\"%s\"), 0x%02x: "#a" is %u, should be %u\n", function, ptest->string, initial_value, ptimeouts1->a, ptimeouts2->a);
514
515 static void check_timeouts(const char *function, TEST *ptest, int initial_value, COMMTIMEOUTS *ptimeouts1, COMMTIMEOUTS *ptimeouts2)
516 {
517         check_timeouts_member(ReadIntervalTimeout);
518         check_timeouts_member(ReadTotalTimeoutMultiplier);
519         check_timeouts_member(ReadTotalTimeoutConstant);
520         check_timeouts_member(WriteTotalTimeoutMultiplier);
521         check_timeouts_member(WriteTotalTimeoutConstant);
522 }
523
524 static void test_BuildCommDCBA(TEST *ptest, int initial_value, DCB *pexpected_dcb)
525 {
526         BOOL result;
527         DCB dcb;
528
529         /* set initial conditions */
530         memset(&dcb, initial_value, sizeof(DCB));
531         SetLastError(0xdeadbeef);
532
533         result = BuildCommDCBA(ptest->string, &dcb);
534
535         /* check results */
536         check_result("BuildCommDCBA", ptest, initial_value, result);
537         check_dcb("BuildCommDCBA", ptest, initial_value, &dcb, pexpected_dcb);
538 }
539
540 static void test_BuildCommDCBAndTimeoutsA(TEST *ptest, int initial_value, DCB *pexpected_dcb, COMMTIMEOUTS *pexpected_timeouts)
541 {
542         BOOL result;
543         DCB dcb;
544         COMMTIMEOUTS timeouts;
545
546         /* set initial conditions */
547         memset(&dcb, initial_value, sizeof(DCB));
548         memset(&timeouts, initial_value, sizeof(COMMTIMEOUTS));
549         SetLastError(0xdeadbeef);
550
551         result = BuildCommDCBAndTimeoutsA(ptest->string, &dcb, &timeouts);
552
553         /* check results */
554         check_result("BuildCommDCBAndTimeoutsA", ptest, initial_value, result);
555         check_dcb("BuildCommDCBAndTimeoutsA", ptest, initial_value, &dcb, pexpected_dcb);
556         check_timeouts("BuildCommDCBAndTimeoutsA", ptest, initial_value, &timeouts, pexpected_timeouts);
557 }
558
559 static void test_BuildCommDCBW(TEST *ptest, int initial_value, DCB *pexpected_dcb)
560 {
561         BOOL result;
562         DCB dcb;
563         WCHAR wide_string[sizeof(ptest->string)];
564
565         MultiByteToWideChar(CP_ACP, 0, ptest->string, -1, wide_string, sizeof(wide_string) / sizeof(WCHAR));
566
567         /* set initial conditions */
568         memset(&dcb, initial_value, sizeof(DCB));
569         SetLastError(0xdeadbeef);
570
571         result = BuildCommDCBW(wide_string, &dcb);
572
573         if(GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
574         {
575                 win_skip("BuildCommDCBW is not available\n");
576                 return;
577         }
578
579         /* check results */
580         check_result("BuildCommDCBW", ptest, initial_value, result);
581         check_dcb("BuildCommDCBW", ptest, initial_value, &dcb, pexpected_dcb);
582 }
583
584 static void test_BuildCommDCBAndTimeoutsW(TEST *ptest, int initial_value, DCB *pexpected_dcb, COMMTIMEOUTS *pexpected_timeouts)
585 {
586         BOOL result;
587         DCB dcb;
588         COMMTIMEOUTS timeouts;
589         WCHAR wide_string[sizeof(ptest->string)];
590
591         MultiByteToWideChar(CP_ACP, 0, ptest->string, -1, wide_string, sizeof(wide_string) / sizeof(WCHAR));
592
593         /* set initial conditions */
594         memset(&dcb, initial_value, sizeof(DCB));
595         memset(&timeouts, initial_value, sizeof(COMMTIMEOUTS));
596         SetLastError(0xdeadbeef);
597
598         result = BuildCommDCBAndTimeoutsW(wide_string, &dcb, &timeouts);
599
600         if(GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
601         {
602                 win_skip("BuildCommDCBAndTimeoutsW is not available\n");
603                 return;
604         }
605
606         /* check results */
607         check_result("BuildCommDCBAndTimeoutsA", ptest, initial_value, result);
608         check_dcb("BuildCommDCBAndTimeoutsA", ptest, initial_value, &dcb, pexpected_dcb);
609         check_timeouts("BuildCommDCBAndTimeoutsA", ptest, initial_value, &timeouts, pexpected_timeouts);
610 }
611
612 static void test_BuildCommDCB(void)
613 {
614         char port_name[] = "COMx";
615         char port = 0;
616         unsigned int i;
617         char *ptr;
618
619         /* Some of these tests require a valid COM port.  This loop will try to find
620            a valid port. */
621         for(port_name[3] = '1'; port_name[3] <= '9'; port_name[3]++)
622         {
623                 COMMCONFIG commconfig;
624                 DWORD size = sizeof(COMMCONFIG);
625
626                 if(GetDefaultCommConfig(port_name, &commconfig, &size))
627                 {
628                         port = port_name[3];
629                         break;
630                 }
631         }
632
633         if(!port)
634                 trace("Could not find a valid COM port.  Some tests will be skipped.\n");
635
636         for(i = 0; i < TEST_COUNT; i++)
637         {
638                 /* Check if this test case needs a valid COM port. */
639                 ptr = strstr(test[i].string, "COMx");
640
641                 /* If required, substitute valid port number into device control string. */
642                 if(ptr)
643                 {
644                         if(port)
645                                 ptr[3] = port;
646                         else
647                                 continue;
648                 }
649
650                 test_BuildCommDCBA(&test[i], 0x00, &test[i].dcb1);
651                 test_BuildCommDCBA(&test[i], 0xff, &test[i].dcb2);
652                 test_BuildCommDCBAndTimeoutsA(&test[i], 0x00, &test[i].dcb1, &test[i].timeouts1);
653                 test_BuildCommDCBAndTimeoutsA(&test[i], 0xff, &test[i].dcb2, &test[i].timeouts2);
654
655                 test_BuildCommDCBW(&test[i], 0x00, &test[i].dcb1);
656                 test_BuildCommDCBW(&test[i], 0xff, &test[i].dcb2);
657                 test_BuildCommDCBAndTimeoutsW(&test[i], 0x00, &test[i].dcb1, &test[i].timeouts1);
658                 test_BuildCommDCBAndTimeoutsW(&test[i], 0xff, &test[i].dcb2, &test[i].timeouts2);
659         }
660 }
661
662 static HANDLE test_OpenComm(BOOL doOverlap)
663 {
664     HANDLE hcom = INVALID_HANDLE_VALUE;
665     char port_name[] = "COMx";
666     static BOOL shown = FALSE;
667     DWORD errors;
668     COMSTAT comstat;
669
670     /* Try to find a port */
671     for(port_name[3] = '1'; port_name[3] <= '9'; port_name[3]++)
672     {
673         hcom = CreateFile( port_name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
674                            (doOverlap)?FILE_FLAG_OVERLAPPED:0, NULL );
675         if (hcom != INVALID_HANDLE_VALUE)
676             break;
677     }
678     if(!shown)
679     {
680         if (hcom == INVALID_HANDLE_VALUE)
681             trace("Could not find a valid COM port.  Skipping test_ReadTimeOut\n");
682         else
683             trace("Found Com port %s. Connected devices may disturbe results\n", port_name);
684         /*shown = TRUE; */
685     }
686     if (hcom != INVALID_HANDLE_VALUE)
687     {
688         BOOL ret;
689
690         ret = ClearCommError(hcom, &errors, &comstat);
691         if (!ret && GetLastError() == ERROR_NOT_READY)
692         {
693             trace("%s doesn't respond, skipping the test\n", port_name);
694             CloseHandle(hcom);
695             return INVALID_HANDLE_VALUE;
696         }
697
698         ok(ret, "Unexpected error %u on open\n", GetLastError());
699         ok(comstat.cbInQue == 0, "Unexpected %d chars in InQueue\n",comstat.cbInQue);
700         ok(comstat.cbOutQue == 0, "Still pending %d charcters in OutQueue\n", comstat.cbOutQue);
701         ok(errors == 0, "Unexpected errors 0x%08x\n", errors);
702     }
703     return hcom;
704 }
705
706 static void test_GetModemStatus(HANDLE hcom)
707 {
708     DWORD ModemStat = 0;
709
710     ok(GetCommModemStatus(hcom, &ModemStat), "GetCommModemStatus failed\n");
711     trace("GetCommModemStatus returned 0x%08x->%s%s%s%s\n", ModemStat,
712           (ModemStat &MS_RLSD_ON)?"MS_RLSD_ON ":"",
713           (ModemStat &MS_RING_ON)?"MS_RING_ON ":"",
714           (ModemStat &MS_DSR_ON)?"MS_DSR_ON ":"",
715           (ModemStat &MS_CTS_ON)?"MS_CTS_ON ":"");
716 }
717
718 /* When we don't write anything, Read should time out even on a loopbacked port */
719 static void test_ReadTimeOut(HANDLE hcom)
720 {
721     DCB dcb;
722     COMMTIMEOUTS timeouts;
723     char rbuf[32];
724     DWORD before, after, read, timediff, LastError;
725     BOOL res;
726
727     ok(GetCommState(hcom, &dcb), "GetCommState failed\n");
728     dcb.BaudRate = FASTBAUD;
729     dcb.ByteSize = 8;
730     dcb.Parity = NOPARITY;
731     dcb.fRtsControl=RTS_CONTROL_ENABLE;
732     dcb.fDtrControl=DTR_CONTROL_ENABLE;
733     dcb.StopBits = ONESTOPBIT;
734     ok(SetCommState(hcom, &dcb), "SetCommState failed\n");
735
736     ZeroMemory( &timeouts, sizeof(timeouts));
737     timeouts.ReadTotalTimeoutConstant = TIMEOUT;
738     ok(SetCommTimeouts(hcom, &timeouts),"SetCommTimeouts failed\n");
739
740     before = GetTickCount();
741     SetLastError(0xdeadbeef);
742     res = ReadFile(hcom, rbuf, sizeof(rbuf), &read, NULL);
743     LastError = GetLastError();
744     after = GetTickCount();
745     ok( res == TRUE, "A timed-out read should return TRUE\n");
746     ok( LastError == 0xdeadbeef, "err=%d\n", LastError);
747     timediff = after - before;
748     ok( timediff > TIMEOUT>>2 && timediff < TIMEOUT *2,
749         "Unexpected TimeOut %d, expected %d\n", timediff, TIMEOUT);
750 }
751
752 static void test_waittxempty(HANDLE hcom)
753 {
754     DCB dcb;
755     COMMTIMEOUTS timeouts;
756     char tbuf[]="test_waittxempty";
757     DWORD before, after, written, timediff, evtmask = 0;
758     BOOL res_write, res;
759     DWORD baud = SLOWBAUD;
760
761     trace("test_waittxempty\n");
762     /* set a low baud rate to have ample time*/
763     ok(GetCommState(hcom, &dcb), "GetCommState failed\n");
764     dcb.BaudRate = baud;
765     dcb.ByteSize = 8;
766     dcb.Parity = NOPARITY;
767     dcb.fRtsControl=RTS_CONTROL_ENABLE;
768     dcb.fDtrControl=DTR_CONTROL_ENABLE;
769     dcb.StopBits = ONESTOPBIT;
770     ok(SetCommState(hcom, &dcb), "SetCommState failed\n");
771
772     ZeroMemory( &timeouts, sizeof(timeouts));
773     timeouts.ReadTotalTimeoutConstant = TIMEOUT;
774     ok(SetCommTimeouts(hcom, &timeouts),"SetCommTimeouts failed\n");
775
776     ok(SetupComm(hcom,1024,1024),"SetUpComm failed\n");
777     ok(SetCommMask(hcom, EV_TXEMPTY), "SetCommMask failed\n");
778
779     before = GetTickCount();
780     res_write=WriteFile(hcom, tbuf, sizeof(tbuf), &written, NULL);
781     after = GetTickCount();
782     ok(res_write == TRUE, "WriteFile failed\n");
783     ok(written == sizeof(tbuf),
784        "WriteFile: Unexpected write_size %d\n", written);
785
786     trace("WriteFile succeeded, took %d ms to write %d Bytes at %d Baud\n",
787           after - before, written, baud);
788
789     before = GetTickCount();
790     res = WaitCommEvent(hcom, &evtmask, NULL);
791     after = GetTickCount();
792
793     ok(res == TRUE, "WaitCommEvent failed\n");
794     ok((evtmask & EV_TXEMPTY),
795                  "WaitCommEvent: Unexpected EvtMask 0x%08x, expected 0x%08x\n",
796                  evtmask, EV_TXEMPTY);
797
798     timediff = after - before;
799
800     trace("WaitCommEvent for EV_TXEMPTY took %d ms\n", timediff);
801     /* 050604: This shows a difference between XP (tested with mingw compiled crosstest):
802        XP returns Writefile only after everything went out of the Serial port,
803        while wine returns immedate.
804        Thus on XP, WaintCommEvent after setting the CommMask for EV_TXEMPTY
805        nearly return immediate,
806        while on wine the most time is spent here
807     */
808 }
809
810 /* A new open handle should not return error or have bytes in the Queues */
811 static void test_ClearCommErrors(HANDLE hcom)
812 {
813     DWORD   errors;
814     COMSTAT lpStat;
815
816     ok(ClearCommError(hcom, &errors, &lpStat), "ClearCommError failed\n");
817     ok(lpStat.cbInQue == 0, "Unexpected %d chars in InQueue\n", lpStat.cbInQue);
818     ok(lpStat.cbOutQue == 0, "Unexpected %d chars in OutQueue\n", lpStat.cbOutQue);
819     ok(errors == 0, "ClearCommErrors: Unexpected error 0x%08x\n", errors);
820     trace("test_ClearCommErrors done\n");
821 }
822
823 static void test_non_pending_errors(HANDLE hcom)
824 {
825     DCB dcb;
826     DWORD err;
827
828     ok(GetCommState(hcom, &dcb), "GetCommState failed\n");
829     dcb.ByteSize = 255; /* likely bogus */
830     ok(!SetCommState(hcom, &dcb), "SetCommState should have failed\n");
831     ok(ClearCommError(hcom, &err, NULL), "ClearCommError should succeed\n");
832     ok(!(err & CE_MODE), "ClearCommError shouldn't set CE_MODE byte in this case (%x)\n", err);
833 }
834
835 /**/
836 static void test_LoopbackRead(HANDLE hcom)
837 {
838     DCB dcb;
839     COMMTIMEOUTS timeouts;
840     char rbuf[32];
841     DWORD before, after, diff, read, read1, written, evtmask=0, i;
842     BOOL res;
843     char tbuf[]="test_LoopbackRead";
844
845     trace("Starting test_LoopbackRead\n");
846     ok(GetCommState(hcom, &dcb), "GetCommState failed\n");
847     dcb.BaudRate = FASTBAUD;
848     dcb.ByteSize = 8;
849     dcb.Parity = NOPARITY;
850     dcb.fRtsControl=RTS_CONTROL_ENABLE;
851     dcb.fDtrControl=DTR_CONTROL_ENABLE;
852     dcb.StopBits = ONESTOPBIT;
853     ok(SetCommState(hcom, &dcb), "SetCommState failed\n");
854
855     ZeroMemory( &timeouts, sizeof(timeouts));
856     timeouts.ReadTotalTimeoutConstant = TIMEOUT;
857     ok(SetCommTimeouts(hcom, &timeouts),"SetCommTimeouts failed\n");
858
859     ok(SetCommMask(hcom, EV_TXEMPTY), "SetCommMask failed\n");
860
861     before = GetTickCount();
862     ok(WriteFile(hcom,tbuf,sizeof(tbuf),&written, NULL), "WriteFile failed\n");
863     after = GetTickCount();
864     ok(written == sizeof(tbuf),"WriteFile %d bytes written\n", written);
865     diff = after -before;
866
867     /* make sure all bytes are written, so Readfile will succeed in one call*/
868     ok(WaitCommEvent(hcom, &evtmask, NULL), "WaitCommEvent failed\n");
869     before = GetTickCount();
870     ok(evtmask == EV_TXEMPTY,
871                  "WaitCommEvent: Unexpected EvtMask 0x%08x, expected 0x%08x\n",
872                  evtmask, EV_TXEMPTY);
873     trace("Write %d ms WaitCommEvent EV_TXEMPTY %d ms\n", diff, before- after);
874
875     read=0;
876     ok(ReadFile(hcom, rbuf, sizeof(rbuf), &read, NULL), "Readfile failed\n");
877     ok(read == sizeof(tbuf),"ReadFile read %d bytes, expected \"%s\"\n", read,rbuf);
878
879     /* Now do the same with a slower Baud rate.
880        As we request more characters then written, we will hit the timeout
881     */
882
883     ok(GetCommState(hcom, &dcb), "GetCommState failed\n");
884     dcb.BaudRate = 9600;
885     dcb.ByteSize = 8;
886     dcb.Parity = NOPARITY;
887     dcb.fRtsControl=RTS_CONTROL_ENABLE;
888     dcb.fDtrControl=DTR_CONTROL_ENABLE;
889     dcb.StopBits = ONESTOPBIT;
890     ok(SetCommState(hcom, &dcb), "SetCommState failed\n");
891
892     ok(SetCommMask(hcom, EV_RXCHAR), "SetCommMask failed\n");
893     ok(WriteFile(hcom,tbuf,sizeof(tbuf),&written, NULL), "WriteFile failed\n");
894     ok(written == sizeof(tbuf),"WriteFile %d bytes written\n", written);
895
896     trace("WaitCommEventEV_RXCHAR\n");
897     ok(WaitCommEvent(hcom, &evtmask, NULL), "WaitCommEvent failed\n");
898     ok(evtmask == EV_RXCHAR, "WaitCommEvent: Unexpected EvtMask 0x%08x, expected 0x%08x\n",
899        evtmask, EV_RXCHAR);
900
901     before = GetTickCount();
902     res = ReadFile(hcom, rbuf, sizeof(rbuf), &read, NULL);
903     after = GetTickCount();
904     ok(res, "Readfile failed\n");
905     ok(read == sizeof(tbuf),"ReadFile read %d bytes\n", read);
906     diff = after - before;
907     trace("Readfile for %d chars took %d ms\n", read, diff);
908     ok( (diff > TIMEOUT - TIMEDELTA) && (diff < TIMEOUT + TIMEDELTA),
909         "Timedout Wait took %d ms, expected around %d\n", diff, TIMEOUT);
910
911     /* now do a plain read with slow speed
912      * This will result in several low level reads and a timeout to happen
913      */
914     dcb.BaudRate = SLOWBAUD;
915     ok(SetCommState(hcom, &dcb), "SetCommState failed\n");
916     ok(WriteFile(hcom,tbuf,sizeof(tbuf),&written, NULL), "WriteFile failed\n");
917     before = GetTickCount();
918     read = 0;
919     read1 =0;
920     i=0;
921     do 
922     {
923         res = ReadFile(hcom, rbuf+read, sizeof(rbuf-read), &read1, NULL);
924         ok(res, "Readfile failed\n");
925         read += read1;
926         i++;
927     }
928     while ((read < sizeof(tbuf)) && (i <10));
929     after =  GetTickCount();
930     ok( read == sizeof(tbuf),"ReadFile read %d bytes\n", read);
931     trace("Plain Read for %d char at %d baud took %d ms\n", read, SLOWBAUD, after-before);
932 }
933
934 static void test_LoopbackCtsRts(HANDLE hcom)
935 {
936     DWORD ModemStat = 0, defaultStat = 0;
937     DCB dcb;
938
939     memset (&dcb, 0, sizeof (dcb));
940     ok(GetCommState(hcom, &dcb), "GetCommState failed\n");
941     if (dcb.fRtsControl == RTS_CONTROL_HANDSHAKE)
942     {
943         trace("RTS_CONTROL_HANDSHAKE is set, so don't manipulate RTS\n");
944         return;
945     }
946     ok(GetCommModemStatus(hcom, &defaultStat), "GetCommModemStatus failed\n");
947     /* XP returns some values in the low nibble, so mask them out*/
948     defaultStat &= MS_CTS_ON|MS_DSR_ON|MS_RING_ON|MS_RLSD_ON;
949     if(defaultStat & MS_CTS_ON)
950     {
951         ok(EscapeCommFunction(hcom, CLRRTS), "EscapeCommFunction failed to clear RTS\n");
952         ok(GetCommModemStatus(hcom, &ModemStat), "GetCommModemStatus failed\n");
953         ok ((ModemStat & MS_CTS_ON) == 0, "CTS didn't react: 0x%04x,  expected 0x%04x\n",
954             ModemStat, (defaultStat & ~MS_CTS_ON));
955         ok(EscapeCommFunction(hcom, SETRTS), "EscapeCommFunction failed to clear RTS\n");
956         ok(GetCommModemStatus(hcom, &ModemStat), "GetCommModemStatus failed\n");
957         ok (ModemStat ==  defaultStat, "Failed to restore CTS: 0x%04x, expected 0x%04x\n",
958             ModemStat, defaultStat);
959     }
960     else
961     {
962         ok(EscapeCommFunction(hcom, SETRTS), "EscapeCommFunction failed to set RTS\n");
963         ok(GetCommModemStatus(hcom, &ModemStat), "GetCommModemStatus failed\n");
964         ok ((ModemStat & MS_CTS_ON) == MS_CTS_ON,
965             "CTS didn't react: 0x%04x,  expected 0x%04x\n",
966             ModemStat, (defaultStat | MS_CTS_ON));
967         ok(EscapeCommFunction(hcom, CLRRTS), "EscapeCommFunction failed to clear RTS\n");
968         ok(GetCommModemStatus(hcom, &ModemStat), "GetCommModemStatus failed\n");
969         ok (ModemStat ==  defaultStat, "Failed to restore CTS: 0x%04x, expected 0x%04x\n",
970             ModemStat, defaultStat);
971     }
972 }
973
974 static void test_LoopbackDtrDcd(HANDLE hcom)
975 {
976     DWORD ModemStat = 0, defaultStat = 0;
977     DCB dcb;
978
979     ok(GetCommState(hcom, &dcb), "GetCommState failed\n");
980     if (dcb.fDtrControl == DTR_CONTROL_HANDSHAKE)
981     {
982         trace("DTR_CONTROL_HANDSHAKE is set, so don't manipulate DTR\n");
983         return;
984     }
985     ok(GetCommModemStatus(hcom, &defaultStat), "GetCommModemStatus failed\n");
986     /* XP returns some values in the low nibble, so mask them out*/
987     defaultStat &= MS_CTS_ON|MS_DSR_ON|MS_RING_ON|MS_RLSD_ON;
988     if(defaultStat & MS_RLSD_ON)
989     {
990         ok(EscapeCommFunction(hcom, CLRDTR), "EscapeCommFunction failed to clear DTR\n");
991         ok(GetCommModemStatus(hcom, &ModemStat), "GetCommModemStatus failed\n");
992         ok ((ModemStat & MS_RLSD_ON) == 0, "RLSD didn't react: 0x%04x,  expected 0x%04x\n",
993             ModemStat, (defaultStat & ~MS_RLSD_ON));
994         ok(EscapeCommFunction(hcom, SETDTR), "EscapeCommFunction failed to set DTR\n");
995         ok(GetCommModemStatus(hcom, &ModemStat), "GetCommModemStatus failed\n");
996         ok (ModemStat ==  defaultStat, "Failed to restore RLSD: 0x%04x, expected 0x%04x\n",
997             ModemStat, defaultStat);
998     }
999     else
1000     {
1001         ok(EscapeCommFunction(hcom, SETDTR), "EscapeCommFunction failed to set DTR\n");
1002         ok(GetCommModemStatus(hcom, &ModemStat), "GetCommModemStatus failed\n");
1003         ok ((ModemStat & MS_RLSD_ON) == MS_RLSD_ON,
1004             "RLSD didn't react: 0x%04x,  expected 0x%04x\n",
1005             ModemStat, (defaultStat | MS_RLSD_ON));
1006         ok(EscapeCommFunction(hcom, CLRDTR), "EscapeCommFunction failed to clear DTR\n");
1007         ok(GetCommModemStatus(hcom, &ModemStat), "GetCommModemStatus failed\n");
1008         ok (ModemStat ==  defaultStat, "Failed to restore RLSD: 0x%04x, expected 0x%04x\n",
1009             ModemStat, defaultStat);
1010     }
1011 }
1012
1013 static void test_LoopbackDtrDsr(HANDLE hcom)
1014 {
1015     DWORD ModemStat = 0, defaultStat = 0;
1016     DCB dcb;
1017
1018     ok(GetCommState(hcom, &dcb), "GetCommState failed\n");
1019     if (dcb.fDtrControl == DTR_CONTROL_DISABLE)
1020     {
1021         trace("DTR_CONTROL_HANDSHAKE is set, so don't manipulate DTR\n");
1022         return;
1023     }
1024     ok(GetCommModemStatus(hcom, &defaultStat), "GetCommModemStatus failed\n");
1025     /* XP returns some values in the low nibble, so mask them out*/
1026     defaultStat &= MS_CTS_ON|MS_DSR_ON|MS_RING_ON|MS_RLSD_ON;
1027     if(defaultStat & MS_DSR_ON)
1028     {
1029         ok(EscapeCommFunction(hcom, CLRDTR), "EscapeCommFunction failed to clear DTR\n");
1030         ok(GetCommModemStatus(hcom, &ModemStat), "GetCommModemStatus failed\n");
1031         ok ((ModemStat & MS_DSR_ON) == 0, "CTS didn't react: 0x%04x,  expected 0x%04x\n",
1032             ModemStat, (defaultStat & ~MS_DSR_ON));
1033         ok(EscapeCommFunction(hcom, SETDTR), "EscapeCommFunction failed to clear DTR\n");
1034         ok(GetCommModemStatus(hcom, &ModemStat), "GetCommModemStatus failed\n");
1035         ok (ModemStat ==  defaultStat, "Failed to restore DSR: 0x%04x, expected 0x%04x\n",
1036             ModemStat, defaultStat);
1037     }
1038     else
1039     {
1040         ok(EscapeCommFunction(hcom, SETDTR), "EscapeCommFunction failed to set DTR\n");
1041         ok(GetCommModemStatus(hcom, &ModemStat), "GetCommModemStatus failed\n");
1042         ok ((ModemStat & MS_DSR_ON) == MS_DSR_ON,
1043             "CTS didn't react: 0x%04x,expected 0x%04x\n",
1044             ModemStat, (defaultStat | MS_DSR_ON));
1045         ok(EscapeCommFunction(hcom, CLRDTR), "EscapeCommFunction failed to clear DTR\n");
1046         ok(GetCommModemStatus(hcom, &ModemStat), "GetCommModemStatus failed\n");
1047         ok (ModemStat ==  defaultStat, "Failed to restore DSR: 0x%04x, expected 0x%04x\n",
1048             ModemStat, defaultStat);
1049     }
1050 }
1051
1052 static void test_LoopbackDtrRing(HANDLE hcom)
1053 {
1054     DWORD ModemStat = 0, defaultStat = 0;
1055     DCB dcb;
1056
1057     ok(GetCommState(hcom, &dcb), "GetCommState failed\n");
1058     if (dcb.fDtrControl == DTR_CONTROL_HANDSHAKE)
1059     {
1060         trace("DTR_CONTROL_HANDSHAKE is set, so don't manipulate DTR\n");
1061         return;
1062     }
1063     ok(GetCommModemStatus(hcom, &defaultStat), "GetCommModemStatus failed\n");
1064     /* XP returns some values in the low nibble, so mask them out*/
1065     defaultStat &= MS_CTS_ON|MS_DSR_ON|MS_RING_ON|MS_RLSD_ON;
1066     if(defaultStat & MS_RING_ON)
1067     {
1068         ok(EscapeCommFunction(hcom, CLRDTR), "EscapeCommFunction failed to clear DTR\n");
1069         ok(GetCommModemStatus(hcom, &ModemStat), "GetCommModemStatus failed\n");
1070         ok ((ModemStat & MS_RING_ON) == 0, "RING didn't react: 0x%04x,  expected 0x%04x\n",
1071             ModemStat, (defaultStat & ~MS_RING_ON));
1072         ok(EscapeCommFunction(hcom, SETDTR), "EscapeCommFunction failed to set DTR\n");
1073         ok(GetCommModemStatus(hcom, &ModemStat), "GetCommModemStatus failed\n");
1074         ok (ModemStat ==  defaultStat, "Failed to restore RING: 0x%04x, expected 0x%04x\n",
1075             ModemStat, defaultStat);
1076     }
1077     else
1078     {
1079         ok(EscapeCommFunction(hcom, SETDTR), "EscapeCommFunction failed to set DTR\n");
1080         ok(GetCommModemStatus(hcom, &ModemStat), "GetCommModemStatus failed\n");
1081         ok ((ModemStat & MS_RING_ON) == MS_RING_ON,
1082             "RING didn't react: 0x%04x,expected 0x%04x\n",
1083             ModemStat, (defaultStat | MS_RING_ON));
1084         ok(EscapeCommFunction(hcom, CLRDTR), "EscapeCommFunction failed to clear DTR\n");
1085         ok(GetCommModemStatus(hcom, &ModemStat), "GetCommModemStatus failed\n");
1086         ok (ModemStat ==  defaultStat, "Failed to restore RING: 0x%04x, expected 0x%04x\n",
1087             ModemStat, defaultStat);
1088     }
1089 }
1090
1091 /*
1092  * Set up a WaitCommEvent for anything in the receive buffer,
1093  * then write to TX to put a character
1094  * into the RX buffer
1095  * Need Loopback TX->RX
1096 */
1097
1098 static void  test_WaitRx(HANDLE hcom)
1099 {
1100     OVERLAPPED overlapped, overlapped_w;
1101     HANDLE hComPortEvent, hComWriteEvent;
1102     DWORD before, after, after1, diff, success_wait = FALSE, success_write;
1103     DWORD err_wait, err_write, written, evtmask=0;
1104
1105     ok(SetCommMask(hcom, EV_RXCHAR), "SetCommMask failed\n");
1106     hComPortEvent =  CreateEvent( NULL, TRUE, FALSE, NULL );
1107     ok(hComPortEvent != 0, "CreateEvent failed\n");
1108     ZeroMemory( &overlapped, sizeof(overlapped));
1109     overlapped.hEvent = hComPortEvent;
1110
1111     ok((hComWriteEvent =  CreateEvent( NULL, TRUE, FALSE, NULL )) !=0,
1112        "CreateEvent res %d\n",
1113        GetLastError());
1114     ZeroMemory( &overlapped_w, sizeof(overlapped_w));
1115     overlapped_w.hEvent = hComWriteEvent;
1116
1117     before = GetTickCount();
1118     {success_wait = WaitCommEvent(hcom, &evtmask, &overlapped);}
1119     err_wait = GetLastError();
1120     after = GetTickCount();
1121     trace("Success 0x%08x err %d evtmask 0x%08x\n", success_wait, err_wait, evtmask);
1122     ok(success_wait || err_wait == ERROR_IO_PENDING, "overlapped WaitCommEvent failed\n");
1123     trace("overlapped WriteCommEvent returned.\n");
1124
1125     success_write= WriteFile(hcom, "X", 1, &written, &overlapped_w);
1126     err_write = GetLastError();
1127     ok(success_write || err_write == ERROR_IO_PENDING,
1128        "overlapped WriteFile failed, err %d\n",
1129        err_write);
1130
1131     if (!success_write && (err_write == ERROR_IO_PENDING)) {
1132       success_write = WaitForSingleObjectEx(hComWriteEvent, TIMEOUT, TRUE);
1133       err_write = GetLastError();
1134       ok(success_write == WAIT_OBJECT_0, "WaitForSingleObjectEx, res %d, err %d\n",
1135          success_write, err_write);
1136     }
1137     Sleep(TIMEOUT >>1);
1138     success_write = GetOverlappedResult(hcom, &overlapped_w, &written, FALSE);
1139     err_write = GetLastError();
1140
1141     trace("Write after Wait res 0x%08x err %d\n",success_write, err_write);
1142     ok(success_write && written ==1, "Write after Wait res 0x%08x err %d\n",
1143        success_write, err_write);
1144
1145     if (!success_wait && (err_wait == ERROR_IO_PENDING)) {
1146       success_wait = WaitForSingleObjectEx(hComPortEvent, TIMEOUT, TRUE);
1147       err_wait = GetLastError();
1148       ok(success_wait == WAIT_OBJECT_0, "wait hComPortEvent, res 0x%08x, err %d\n",
1149          success_wait, err_wait);
1150     }
1151     success_wait = GetOverlappedResult(hcom, &overlapped, &written, FALSE);
1152     err_wait = GetLastError();
1153     after1 = GetTickCount();
1154     trace("Success 0x%08x err %d evtmask 0x%08x diff1 %d, diff2 %d\n",
1155           success_wait, err_wait, evtmask, after-before, after1-before);
1156
1157     ok(evtmask & EV_RXCHAR, "Detect  EV_RXCHAR: 0x%08x, expected 0x%08x\n",
1158        evtmask, EV_RXCHAR);
1159     diff = after1 - before;
1160     ok ((diff > (TIMEOUT>>1) -TIMEDELTA) && (diff < (TIMEOUT>>1) + TIMEDELTA),
1161         "Unexpected time %d, expected around %d\n", diff, TIMEOUT>>1);
1162
1163 }
1164
1165 /* Change the controling line after the given timeout to the given state
1166    By the loopback, this should trigger the WaitCommEvent
1167 */
1168 static DWORD CALLBACK toggle_ctlLine(LPVOID arg)
1169 {
1170     DWORD_PTR *args = arg;
1171     DWORD timeout = args[0];
1172     DWORD ctl     = args[1];
1173     HANDLE hcom   = (HANDLE) args[2];
1174     HANDLE hComPortEvent = (HANDLE) args[3];
1175     DWORD success, err;
1176
1177     trace("toggle_ctlLine timeout %d ctl 0x%08x handle %p\n", timeout, ctl, hcom );
1178     Sleep(timeout);
1179     ok(EscapeCommFunction(hcom, ctl),"EscapeCommFunction 0x%08x failed\n", ctl);
1180     trace("toggle_ctline done\n");
1181     success = WaitForSingleObjectEx(hComPortEvent, TIMEOUT, TRUE);
1182     err = GetLastError();
1183     trace("toggle_ctline WaitForSingleObjectEx res 0x%08x err %d\n",
1184           success, err);
1185     return 0;
1186 }
1187
1188 /*
1189  * Wait for a change in CTS
1190  * Needs Loopback from DTR to CTS
1191  */
1192 static void  test_WaitCts(HANDLE hcom)
1193 {
1194     DCB dcb;
1195     OVERLAPPED overlapped;
1196     HANDLE hComPortEvent;
1197     HANDLE alarmThread;
1198     DWORD_PTR args[4];
1199     DWORD alarmThreadId, before, after, after1, diff, success, err, written, evtmask=0, defaultStat = 0;
1200
1201     ok(GetCommState(hcom, &dcb), "GetCommState failed\n");
1202     dcb.fRtsControl=RTS_CONTROL_ENABLE;
1203     dcb.fDtrControl=DTR_CONTROL_ENABLE;
1204     ok(SetCommState(hcom, &dcb), "SetCommState failed\n");
1205     if (dcb.fDtrControl == RTS_CONTROL_DISABLE)
1206     {
1207         trace("RTS_CONTROL_HANDSHAKE is set, so don't manipulate DTR\n");
1208         return;
1209     }
1210     args[0]= TIMEOUT >>1;
1211     ok(GetCommModemStatus(hcom, &defaultStat), "GetCommModemStatus failed\n");
1212     if(defaultStat & MS_CTS_ON)
1213         args[1] = CLRRTS;
1214     else
1215         args[1] = SETRTS;
1216     args[2]=(DWORD_PTR)hcom;
1217
1218     trace("test_WaitCts timeout %ld clt 0x%08lx handle %p\n",args[0], args[1], hcom);
1219
1220     ok(SetCommMask(hcom, EV_CTS), "SetCommMask failed\n");
1221     hComPortEvent =  CreateEvent( NULL, TRUE, FALSE, NULL );
1222     ok(hComPortEvent != 0, "CreateEvent failed\n");
1223     args[3] = (DWORD_PTR)hComPortEvent;
1224     alarmThread = CreateThread(NULL, 0, toggle_ctlLine, args, 0, &alarmThreadId);
1225     /* Wait a minimum to let the thread start up */
1226     Sleep(10);
1227     trace("Thread created\n");
1228     ok(alarmThread !=0 , "CreateThread Failed\n");
1229
1230     ZeroMemory( &overlapped, sizeof(overlapped));
1231     overlapped.hEvent = hComPortEvent;
1232     before = GetTickCount();
1233     success = WaitCommEvent(hcom, &evtmask, &overlapped);
1234     err = GetLastError();
1235     after = GetTickCount();
1236
1237     trace("Success 0x%08x err %d evtmask 0x%08x\n", success, err, evtmask);
1238     ok(success || err == ERROR_IO_PENDING, "overlapped WaitCommEvent failed\n");
1239     trace("overlapped WriteCommEvent returned.\n");
1240     if (!success && (err == ERROR_IO_PENDING))
1241         ok(WaitForSingleObjectEx(hComPortEvent, TIMEOUT, TRUE) == 0,
1242                      "WaitCts hComPortEvent failed\n");
1243     success = GetOverlappedResult(hcom, &overlapped, &written, FALSE);
1244     err = GetLastError();
1245     after1 = GetTickCount();
1246     trace("Success 0x%08x err %d evtmask 0x%08x diff1 %d, diff2 %d\n",
1247           success, err, evtmask, after-before, after1-before);
1248
1249     ok(evtmask & EV_CTS, "Failed to detect  EV_CTS: 0x%08x, expected 0x%08x\n",
1250                  evtmask, EV_CTS);
1251     ok(GetCommModemStatus(hcom, &evtmask), "GetCommModemStatus failed\n");
1252     if(defaultStat & MS_CTS_ON)
1253         ok((evtmask & MS_CTS_ON) == 0,"CTS didn't change state!\n");
1254     else
1255         ok((evtmask & MS_CTS_ON), "CTS didn't change state!\n");
1256
1257     diff = after1 - before;
1258     ok ((diff > (TIMEOUT>>1) -TIMEDELTA) && (diff < (TIMEOUT>>1) + TIMEDELTA),
1259                   "Unexpected time %d, expected around %d\n", diff, TIMEOUT>>1);
1260
1261     /*restore RTS Settings*/
1262     if(defaultStat & MS_CTS_ON)
1263         args[1] = SETRTS;
1264     else
1265         args[1] = CLRRTS;
1266 }
1267
1268 /* Change the  Comm Mask while a Wait is going on
1269    WaitCommevent should return with a EVTMASK set to zero
1270 */
1271 static DWORD CALLBACK reset_CommMask(LPVOID arg)
1272 {
1273     DWORD_PTR *args = arg;
1274     DWORD timeout = args[0];
1275     HANDLE hcom   = (HANDLE) args[1];
1276
1277     trace(" Changing CommMask on the fly for handle %p after timeout %d\n",
1278           hcom, timeout);
1279     Sleep(timeout);
1280     ok(SetCommMask(hcom, 0),"SetCommMask %p failed\n", hcom);
1281     trace("SetCommMask changed\n");
1282     return 0;
1283 }
1284
1285 /* Set up a Wait for a change on CTS. We don't toggle any line, but we
1286    reset the CommMask and expect the wait to return with a mask of 0
1287    No special port connections needed
1288 */
1289 static void  test_AbortWaitCts(HANDLE hcom)
1290 {
1291     DCB dcb;
1292     OVERLAPPED overlapped;
1293     HANDLE hComPortEvent;
1294     HANDLE alarmThread;
1295     DWORD_PTR args[2];
1296     DWORD alarmThreadId, before, after, after1, diff, success, err, written, evtmask=0;
1297
1298     ok(GetCommState(hcom, &dcb), "GetCommState failed\n");
1299     if (dcb.fDtrControl == RTS_CONTROL_DISABLE)
1300     {
1301         trace("RTS_CONTROL_HANDSHAKE is set, so don't manipulate DTR\n");
1302         return;
1303     }
1304     args[0]= TIMEOUT >>1;
1305     args[1]= (DWORD_PTR)hcom;
1306
1307     trace("test_AbortWaitCts timeout %ld handle %p\n",args[0], hcom);
1308
1309     ok(SetCommMask(hcom, EV_CTS), "SetCommMask failed\n");
1310     hComPortEvent =  CreateEvent( NULL, TRUE, FALSE, NULL );
1311     ok(hComPortEvent != 0, "CreateEvent failed\n");
1312     alarmThread = CreateThread(NULL, 0, reset_CommMask, args, 0, &alarmThreadId);
1313     /* Wait a minimum to let the thread start up */
1314     Sleep(10);
1315     trace("Thread created\n");
1316     ok(alarmThread !=0 , "CreateThread Failed\n");
1317
1318     ZeroMemory( &overlapped, sizeof(overlapped));
1319     overlapped.hEvent = hComPortEvent;
1320     before = GetTickCount();
1321     success = WaitCommEvent(hcom, &evtmask, &overlapped);
1322     err = GetLastError();
1323     after = GetTickCount();
1324
1325     trace("Success 0x%08x err %d evtmask 0x%08x\n", success, err, evtmask);
1326     ok(success || err == ERROR_IO_PENDING, "overlapped WaitCommEvent failed\n");
1327     trace("overlapped WriteCommEvent returned.\n");
1328     if (!success && (err == ERROR_IO_PENDING))
1329         ok(WaitForSingleObjectEx(hComPortEvent, TIMEOUT, TRUE) == 0,
1330                      "AbortWaitCts hComPortEvent failed\n");
1331     success = GetOverlappedResult(hcom, &overlapped, &written, FALSE);
1332     err = GetLastError();
1333     after1 = GetTickCount();
1334     trace("Success 0x%08x err %d evtmask 0x%08x diff1 %d, diff2 %d\n",
1335           success, err, evtmask, after-before, after1-before);
1336
1337     ok(evtmask == 0, "Incorect EventMask 0x%08x returned on Wait aborted bu SetCommMask, expected 0x%08x\n",
1338                  evtmask, 0);
1339     ok(GetCommModemStatus(hcom, &evtmask), "GetCommModemStatus failed\n");
1340     diff = after1 - before;
1341     ok ((diff > (TIMEOUT>>1) -TIMEDELTA) && (diff < (TIMEOUT>>1) + TIMEDELTA),
1342                   "Unexpected time %d, expected around %d\n", diff, TIMEOUT>>1);
1343
1344 }
1345
1346 /*
1347  * Wait for a change in DSR
1348  * Needs Loopback from DTR to DSR
1349  */
1350 static void  test_WaitDsr(HANDLE hcom)
1351 {
1352     DCB dcb;
1353     OVERLAPPED overlapped;
1354     HANDLE hComPortEvent;
1355     HANDLE alarmThread;
1356     DWORD_PTR args[3];
1357     DWORD alarmThreadId, before, after, after1, diff, success, err, written, evtmask=0, defaultStat = 0;
1358
1359     ok(GetCommState(hcom, &dcb), "GetCommState failed\n");
1360     if (dcb.fDtrControl == DTR_CONTROL_DISABLE)
1361     {
1362         trace("DTR_CONTROL_HANDSHAKE is set, so don't manipulate DTR\n");
1363         return;
1364     }
1365     args[0]= TIMEOUT >>1;
1366     ok(GetCommModemStatus(hcom, &defaultStat), "GetCommModemStatus failed\n");
1367     if(defaultStat & MS_DSR_ON)
1368         args[1] = CLRDTR;
1369     else
1370         args[1] = SETDTR;
1371     args[2]= (DWORD_PTR)hcom;
1372
1373     trace("test_WaitDsr timeout %ld clt 0x%08lx handle %p\n",args[0], args[1], hcom);
1374
1375     ok(SetCommMask(hcom, EV_DSR), "SetCommMask failed\n");
1376     hComPortEvent =  CreateEvent( NULL, TRUE, FALSE, NULL );
1377     ok(hComPortEvent != 0, "CreateEvent failed\n");
1378     alarmThread = CreateThread(NULL, 0, toggle_ctlLine, args, 0, &alarmThreadId);
1379     ok(alarmThread !=0 , "CreateThread Failed\n");
1380
1381     ZeroMemory( &overlapped, sizeof(overlapped));
1382     overlapped.hEvent = hComPortEvent;
1383     before = GetTickCount();
1384     success = WaitCommEvent(hcom, &evtmask, &overlapped);
1385     err = GetLastError();
1386     after = GetTickCount();
1387
1388     trace("Success 0x%08x err %d evtmask 0x%08x\n", success, err, evtmask);
1389     ok(success || err == ERROR_IO_PENDING, "overlapped WaitCommEvent failed\n");
1390     trace("overlapped WriteCommEvent returned.\n");
1391     if (!success && (err == ERROR_IO_PENDING))
1392         ok(WaitForSingleObjectEx(hComPortEvent, TIMEOUT, TRUE) == 0,
1393                      "wait hComPortEvent failed\n");
1394     success = GetOverlappedResult(hcom, &overlapped, &written, FALSE);
1395     err = GetLastError();
1396     after1 = GetTickCount();
1397     trace("Success 0x%08x err %d evtmask 0x%08x diff1 %d, diff2 %d\n",
1398           success, err, evtmask, after-before, after1-before);
1399
1400     ok(evtmask & EV_DSR, "Failed to detect  EV_DSR: 0x%08x, expected 0x%08x\n",
1401                  evtmask, EV_DSR);
1402     ok(GetCommModemStatus(hcom, &evtmask), "GetCommModemStatus failed\n");
1403     if(defaultStat & MS_DSR_ON)
1404         ok((evtmask & MS_DSR_ON) == 0,"DTR didn't change state!\n");
1405     else
1406         ok((evtmask & MS_DSR_ON), "DTR didn't change state!\n");
1407
1408     diff = after1 - before;
1409     ok ((diff > (TIMEOUT>>1) -TIMEDELTA) && (diff < (TIMEOUT>>1) + TIMEDELTA),
1410                   "Unexpected time %d, expected around %d\n", diff, TIMEOUT>>1);
1411
1412     /*restore RTS Settings*/
1413     if(defaultStat & MS_DSR_ON)
1414         args[1] = SETDTR;
1415     else
1416         args[1] = CLRDTR;
1417 }
1418
1419 /*
1420  * Wait for a Ring
1421  * Needs Loopback from DTR to RING
1422  */
1423 static void  test_WaitRing(HANDLE hcom)
1424 {
1425     DCB dcb;
1426     OVERLAPPED overlapped;
1427     HANDLE hComPortEvent;
1428     HANDLE alarmThread;
1429     DWORD_PTR args[3];
1430     DWORD alarmThreadId, before, after, after1, diff, success, err, written, evtmask=0, defaultStat;
1431     BOOL ret;
1432
1433     ok(GetCommState(hcom, &dcb), "GetCommState failed\n");
1434     if (dcb.fDtrControl == DTR_CONTROL_DISABLE)
1435     {
1436         trace("DTR_CONTROL_HANDSHAKE is set, so don't manipulate DTR\n");
1437         return;
1438     }
1439     args[0]= TIMEOUT >>1;
1440     ok((ret = GetCommModemStatus(hcom, &defaultStat)), "GetCommModemStatus failed\n");
1441     if (!ret) {
1442         skip("modem status failed -> skip.\n");
1443         return;
1444     }
1445     if(defaultStat & MS_RING_ON)
1446         args[1] = CLRDTR;
1447     else
1448         args[1] = SETDTR;
1449     args[2]=(DWORD_PTR) hcom;
1450
1451     trace("test_WaitRing timeout %ld clt 0x%08lx handle %p\n",args[0], args[1], hcom);
1452
1453     ok(SetCommMask(hcom, EV_RING), "SetCommMask failed\n");
1454     hComPortEvent =  CreateEvent( NULL, TRUE, FALSE, NULL );
1455     ok(hComPortEvent != 0, "CreateEvent failed\n");
1456     alarmThread = CreateThread(NULL, 0, toggle_ctlLine, args, 0, &alarmThreadId);
1457     ok(alarmThread !=0 , "CreateThread Failed\n");
1458
1459     ZeroMemory( &overlapped, sizeof(overlapped));
1460     overlapped.hEvent = hComPortEvent;
1461     before = GetTickCount();
1462     success = WaitCommEvent(hcom, &evtmask, &overlapped);
1463     err = GetLastError();
1464     after = GetTickCount();
1465
1466     trace("Success 0x%08x err %d evtmask 0x%08x\n", success, err, evtmask);
1467     ok(success || err == ERROR_IO_PENDING, "overlapped WaitCommEvent failed\n");
1468     trace("overlapped WriteCommEvent returned.\n");
1469     if (!success && (err == ERROR_IO_PENDING))
1470         ok(WaitForSingleObjectEx(hComPortEvent, TIMEOUT, TRUE) == 0,
1471                      "wait hComPortEvent failed\n");
1472     success = GetOverlappedResult(hcom, &overlapped, &written, FALSE);
1473     err = GetLastError();
1474     after1 = GetTickCount();
1475     trace("Success 0x%08x err %d evtmask 0x%08x diff1 %d, diff2 %d\n",
1476           success, err, evtmask, after-before, after1-before);
1477
1478     ok(evtmask & EV_RING, "Failed to detect  EV_RING: 0x%08x, expected 0x%08x\n",
1479        evtmask, EV_CTS);
1480     ok(GetCommModemStatus(hcom, &evtmask), "GetCommModemStatus failed\n");
1481     if(defaultStat & MS_RING_ON)
1482         ok((evtmask & MS_RING_ON) == 0,"DTR didn't change state!\n");
1483     else
1484         ok((evtmask & MS_RING_ON), "DTR didn't change state!\n");
1485
1486     diff = after1 - before;
1487     ok ((diff > (TIMEOUT>>1) -TIMEDELTA) && (diff < (TIMEOUT>>1) + TIMEDELTA),
1488                   "Unexpected time %d, expected around %d\n", diff, TIMEOUT>>1);
1489
1490     /*restore RTS Settings*/
1491     if(defaultStat & MS_RING_ON)
1492         args[1] = SETDTR;
1493     else
1494         args[1] = CLRDTR;
1495 }
1496 /*
1497  * Wait for a change in DCD
1498  * Needs Loopback from DTR to DCD
1499  */
1500 static void  test_WaitDcd(HANDLE hcom)
1501 {
1502     DCB dcb;
1503     OVERLAPPED overlapped;
1504     HANDLE hComPortEvent;
1505     HANDLE alarmThread;
1506     DWORD_PTR args[3];
1507     DWORD alarmThreadId, before, after, after1, diff, success, err, written, evtmask=0, defaultStat = 0;
1508
1509     ok(GetCommState(hcom, &dcb), "GetCommState failed\n");
1510     if (dcb.fDtrControl == DTR_CONTROL_DISABLE)
1511     {
1512         trace("DTR_CONTROL_HANDSHAKE is set, so don't manipulate DTR\n");
1513         return;
1514     }
1515     args[0]= TIMEOUT >>1;
1516     ok(GetCommModemStatus(hcom, &defaultStat), "GetCommModemStatus failed\n");
1517     if(defaultStat & MS_RLSD_ON)
1518         args[1] = CLRDTR;
1519     else
1520         args[1] = SETDTR;
1521     args[2]= (DWORD_PTR)hcom;
1522
1523     trace("test_WaitDcd timeout %ld clt 0x%08lx handle %p\n",args[0], args[1], hcom);
1524
1525     ok(SetCommMask(hcom, EV_RLSD), "SetCommMask failed\n");
1526     hComPortEvent =  CreateEvent( NULL, TRUE, FALSE, NULL );
1527     ok(hComPortEvent != 0, "CreateEvent failed\n");
1528     alarmThread = CreateThread(NULL, 0, toggle_ctlLine, args, 0, &alarmThreadId);
1529     ok(alarmThread !=0 , "CreateThread Failed\n");
1530
1531     ZeroMemory( &overlapped, sizeof(overlapped));
1532     overlapped.hEvent = hComPortEvent;
1533     before = GetTickCount();
1534     success = WaitCommEvent(hcom, &evtmask, &overlapped);
1535     err = GetLastError();
1536     after = GetTickCount();
1537
1538     trace("Success 0x%08x err %d evtmask 0x%08x\n", success, err, evtmask);
1539     ok(success || err == ERROR_IO_PENDING, "overlapped WaitCommEvent failed\n");
1540     trace("overlapped WriteCommEvent returned.\n");
1541     if (!success && (err == ERROR_IO_PENDING))
1542         ok(WaitForSingleObjectEx(hComPortEvent, TIMEOUT, TRUE) == 0,
1543                      "wait hComPortEvent failed\n");
1544     success = GetOverlappedResult(hcom, &overlapped, &written, FALSE);
1545     err = GetLastError();
1546     after1 = GetTickCount();
1547     trace("Success 0x%08x err %d evtmask 0x%08x diff1 %d, diff2 %d\n",
1548           success, err, evtmask, after-before, after1-before);
1549
1550     ok(evtmask & EV_RLSD, "Failed to detect  EV_RLSD: 0x%08x, expected 0x%08x\n",
1551                  evtmask, EV_CTS);
1552     ok(GetCommModemStatus(hcom, &evtmask), "GetCommModemStatus failed\n");
1553     if(defaultStat & MS_RLSD_ON)
1554         ok((evtmask & MS_RLSD_ON) == 0,"DTR didn't change state!\n");
1555     else
1556         ok((evtmask & MS_RLSD_ON), "DTR didn't change state!\n");
1557
1558     diff = after1 - before;
1559     ok ((diff > (TIMEOUT>>1) -TIMEDELTA) && (diff < (TIMEOUT>>1) + TIMEDELTA),
1560                   "Unexpected time %d, expected around %d\n", diff, TIMEOUT>>1);
1561
1562     /*restore RTS Settings*/
1563     if(defaultStat & MS_RLSD_ON)
1564         args[1] = SETDTR;
1565     else
1566         args[1] = CLRDTR;
1567 }
1568
1569 /* 
1570    Set Break after timeout
1571 */
1572 static DWORD CALLBACK set_CommBreak(LPVOID arg)
1573 {
1574     DWORD_PTR *args = arg;
1575     DWORD timeout = args[0];
1576     HANDLE hcom   = (HANDLE) args[1];
1577
1578     trace("SetCommBreak for handle %p after timeout %d\n",
1579           hcom, timeout);
1580     Sleep(timeout);
1581     ok(SetCommBreak(hcom),"SetCommBreak %p failed\n", hcom);
1582     trace("SetCommBreak done\n");
1583     return 0;
1584 }
1585
1586 /*
1587    Wait for the Break condition (TX resp. RX active)
1588    Needs Loopback TX-RX
1589 */
1590 static void  test_WaitBreak(HANDLE hcom)
1591 {
1592     OVERLAPPED overlapped;
1593     HANDLE hComPortEvent;
1594     HANDLE alarmThread;
1595     DWORD_PTR args[2];
1596     DWORD alarmThreadId, before, after, after1, diff, success, err, written, evtmask=0;
1597
1598     ok(SetCommMask(hcom, EV_BREAK), "SetCommMask failed\n");
1599     hComPortEvent =  CreateEvent( NULL, TRUE, FALSE, NULL );
1600     ok(hComPortEvent != 0, "CreateEvent failed\n");
1601
1602     trace("test_WaitBreak\n");
1603     args[0]= TIMEOUT >>1;
1604     args[1]= (DWORD_PTR)hcom;
1605     alarmThread = CreateThread(NULL, 0, set_CommBreak, args, 0, &alarmThreadId);
1606     /* Wait a minimum to let the thread start up */
1607     Sleep(10);
1608     trace("Thread created\n");
1609     ok(alarmThread !=0 , "CreateThread Failed\n");
1610
1611     ZeroMemory( &overlapped, sizeof(overlapped));
1612     overlapped.hEvent = hComPortEvent;
1613     before = GetTickCount();
1614     success = WaitCommEvent(hcom, &evtmask, &overlapped);
1615     err = GetLastError();
1616     after = GetTickCount();
1617
1618     trace("Success 0x%08x err %d evtmask 0x%08x\n", success, err, evtmask);
1619     ok(success || err == ERROR_IO_PENDING, "overlapped WaitCommEvent failed\n");
1620     trace("overlapped WriteCommEvent returned.\n");
1621
1622     if (!success && (err == ERROR_IO_PENDING))
1623         ok(WaitForSingleObjectEx(hComPortEvent, TIMEOUT, TRUE) == 0,
1624            "wait hComPortEvent res %d\n", GetLastError());
1625     success = GetOverlappedResult(hcom, &overlapped, &written, FALSE);
1626     err = GetLastError();
1627     after1 = GetTickCount();
1628     trace("Success 0x%08x err %d evtmask 0x%08x diff1 %d, diff2 %d\n",
1629           success, err, evtmask, after-before, after1-before);
1630
1631     ok(evtmask & EV_BREAK, "Failed to detect  EV_BREAK: 0x%08x, expected 0x%08x\n",
1632        evtmask, EV_BREAK);
1633     ok(GetCommModemStatus(hcom, &evtmask), "GetCommModemStatus failed\n");
1634
1635     diff = after1 - before;
1636     ok ((diff > (TIMEOUT>>1) -TIMEDELTA) && (diff < (TIMEOUT>>1) + TIMEDELTA),
1637         "Unexpected time %d, expected around %d\n", diff, TIMEOUT>>1);
1638
1639     ok(ClearCommBreak(hcom), "ClearCommBreak failed\n");
1640 }
1641
1642 START_TEST(comm)
1643 {
1644     HANDLE hcom;
1645     /* use variables and not #define to compile the code */
1646     BOOL loopback_txd_rxd  = LOOPBACK_TXD_RXD;
1647     BOOL loopback_rts_cts  = LOOPBACK_CTS_RTS;
1648     BOOL loopback_dtr_dsr  = LOOPBACK_DTR_DSR;
1649     BOOL loopback_dtr_ring = LOOPBACK_DTR_RING;
1650     BOOL loopback_dtr_dcd  = LOOPBACK_DTR_DCD;
1651
1652     test_BuildCommDCB();
1653     hcom = test_OpenComm(FALSE);
1654     if (hcom != INVALID_HANDLE_VALUE)
1655     {
1656         test_GetModemStatus(hcom);
1657         test_ReadTimeOut(hcom);
1658         test_waittxempty(hcom);
1659         CloseHandle(hcom);
1660     }
1661     hcom = test_OpenComm(FALSE);
1662     if (hcom != INVALID_HANDLE_VALUE)
1663     {
1664         Sleep(200); /* Give the laster character of test_waittxempty to drop into the receiver */
1665         test_ClearCommErrors(hcom);
1666         CloseHandle(hcom);
1667     }
1668     hcom = test_OpenComm(FALSE);
1669     if (hcom != INVALID_HANDLE_VALUE)
1670     {
1671         test_non_pending_errors(hcom);
1672         CloseHandle(hcom);
1673     }
1674     if((loopback_txd_rxd) && ((hcom = test_OpenComm(FALSE))!=INVALID_HANDLE_VALUE))
1675     {
1676         test_LoopbackRead(hcom);
1677         CloseHandle(hcom);
1678     }
1679     if((loopback_rts_cts) && ((hcom = test_OpenComm(FALSE))!=INVALID_HANDLE_VALUE))
1680     {
1681         test_LoopbackCtsRts(hcom);
1682         CloseHandle(hcom);
1683     }
1684     if((loopback_dtr_dsr) && ((hcom = test_OpenComm(FALSE))!=INVALID_HANDLE_VALUE))
1685     {
1686         test_LoopbackDtrDsr(hcom);
1687         CloseHandle(hcom);
1688     }
1689     if((loopback_dtr_ring) && ((hcom = test_OpenComm(FALSE))!=INVALID_HANDLE_VALUE))
1690     {
1691         test_LoopbackDtrRing(hcom);
1692         CloseHandle(hcom);
1693     }
1694     if((loopback_dtr_dcd) && ((hcom = test_OpenComm(FALSE))!=INVALID_HANDLE_VALUE))
1695     {
1696         test_LoopbackDtrDcd(hcom);
1697         CloseHandle(hcom);
1698     }
1699     if((loopback_txd_rxd) && ((hcom = test_OpenComm(TRUE))!=INVALID_HANDLE_VALUE))
1700     {
1701         test_WaitRx(hcom);
1702         CloseHandle(hcom);
1703     }
1704     if((loopback_rts_cts) && ((hcom = test_OpenComm(TRUE))!=INVALID_HANDLE_VALUE))
1705     {
1706         test_WaitCts(hcom);
1707         CloseHandle(hcom);
1708     }
1709     if((hcom = test_OpenComm(TRUE))!=INVALID_HANDLE_VALUE)
1710     {
1711         test_AbortWaitCts(hcom);
1712         CloseHandle(hcom);
1713     }
1714     if((loopback_dtr_dsr) && ((hcom = test_OpenComm(TRUE))!=INVALID_HANDLE_VALUE))
1715     {
1716         test_WaitDsr(hcom);
1717         CloseHandle(hcom);
1718     }
1719     if((loopback_dtr_ring) && ((hcom = test_OpenComm(TRUE))!=INVALID_HANDLE_VALUE))
1720     {
1721         test_WaitRing(hcom);
1722         CloseHandle(hcom);
1723     }
1724     if((loopback_dtr_dcd) && ((hcom = test_OpenComm(TRUE))!=INVALID_HANDLE_VALUE))
1725     {
1726         test_WaitDcd(hcom);
1727         CloseHandle(hcom);
1728     }
1729     if(loopback_txd_rxd && (hcom = test_OpenComm(TRUE))!=INVALID_HANDLE_VALUE)
1730     {
1731         test_WaitBreak(hcom);
1732         CloseHandle(hcom);
1733     }
1734 }