1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#ifndef PTT_WINDOWS_SOURCE
#define PTT_WINDOWS_SOURCE "../../../map65/libm65/ptt.c"
#endif
/* Redirect ptt.c's call (and our mock below) so our definition doesn't collide with libkernel32's real GetLastError at link time. */
#define GetLastError GetLastErr
DWORD GetLastErr(void);
#include PTT_WINDOWS_SOURCE
enum { OPEN_PORT = 100, CLOSE_PORT, RTS = 1, DTR = 2 };
struct event {
DWORD operation;
int handle;
char path[64];
};
static struct event events[128];
static int event_count;
static int opened[64];
static int lines[64];
static int next_handle = 10;
static int fail_open;
static int fail_close;
static int failures[10];
static DWORD errors[10];
static DWORD last_error;
static int mock_failed;
static int iptt;
static int port;
#define CHECK(condition) do { \
if (!(condition)) { \
fprintf(stderr, "%s:%d: %s\n", __func__, __LINE__, #condition); \
return 0; \
} \
} while (0)
static struct event *record(DWORD operation, int handle)
{
struct event *event;
if (event_count == (int)(sizeof(events) / sizeof(events[0])))
exit(2);
event = &events[event_count++];
memset(event, 0, sizeof(*event));
event->operation = operation;
event->handle = handle;
return event;
}
static int valid_handle(HANDLE handle)
{
intptr_t value = (intptr_t)handle;
if (value < 0 || value >= 64 || !opened[value]) {
mock_failed = 1;
return 0;
}
return 1;
}
HANDLE CreateFileA(const char *path, DWORD access, DWORD sharing, void *security,
DWORD creation, DWORD attributes, HANDLE template_file)
{
int handle = next_handle++;
struct event *event = record(OPEN_PORT, handle);
snprintf(event->path, sizeof(event->path), "%s", path);
if (access != GENERIC_WRITE || sharing || security || creation != OPEN_EXISTING
|| attributes != FILE_ATTRIBUTE_NORMAL || template_file || handle >= 64)
mock_failed = 1;
if (mock_failed || fail_open) {
last_error = ERROR_ACCESS_DENIED;
return INVALID_HANDLE_VALUE;
}
opened[handle] = 1;
return (HANDLE)(intptr_t)handle;
}
BOOL EscapeCommFunction(HANDLE handle, DWORD operation)
{
int value = (int)(intptr_t)handle;
record(operation, value);
if (!valid_handle(handle))
return 0;
if (operation != SETRTS && operation != SETDTR && operation != CLRRTS
&& operation != CLRDTR && operation != CLRBREAK) {
mock_failed = 1;
return 0;
}
if (failures[operation]) {
if (failures[operation] > 0)
--failures[operation];
last_error = errors[operation];
return 0;
}
last_error = 0;
if (operation == SETRTS) lines[value] |= RTS;
if (operation == SETDTR) lines[value] |= DTR;
if (operation == CLRRTS) lines[value] &= ~RTS;
if (operation == CLRDTR) lines[value] &= ~DTR;
return 1;
}
BOOL CloseHandle(HANDLE handle)
{
record(CLOSE_PORT, (int)(intptr_t)handle);
if (!valid_handle(handle))
return 0;
if (fail_close) {
last_error = ERROR_GEN_FAILURE;
return 0;
}
opened[(intptr_t)handle] = 0;
return 1;
}
DWORD GetLastError(void) { return last_error; }
static int command(int on) { return ptt_(&port, &on, &iptt); }
static int count(DWORD operation)
{
int total = 0;
int i;
for (i = 0; i < event_count; ++i)
total += events[i].operation == operation;
return total;
}
static void fail(DWORD operation, DWORD error, int attempts)
{
failures[operation] = attempts;
errors[operation] = error;
}
static void reset(void)
{
memset(failures, 0, sizeof(failures));
fail_open = fail_close = 0;
ptt_close();
memset(opened, 0, sizeof(opened));
memset(lines, 0, sizeof(lines));
event_count = mock_failed = iptt = 0;
next_handle = 10;
port = 3;
}
static int disabled_and_unopened_release(void)
{
CHECK(command(0) == PTT_OK && !iptt);
port = 0;
CHECK(command(1) == PTT_OK && iptt);
CHECK(command(0) == PTT_OK && !iptt);
ptt_close();
CHECK(event_count == 0);
return 1;
}
static int normal_cycle_and_switch(void)
{
CHECK(command(1) == PTT_OK && iptt);
CHECK(strcmp(events[0].path, "COM3") == 0);
CHECK(lines[10] == (RTS | DTR));
event_count = 0;
port = 12;
CHECK(command(1) == PTT_OK && iptt);
CHECK(!event_count);
CHECK(command(0) == PTT_OK && !iptt);
CHECK(!lines[10] && !opened[10]);
CHECK(events[0].operation == CLRRTS && events[1].operation == CLRDTR);
CHECK(events[2].operation == CLRBREAK && events[3].operation == CLOSE_PORT);
CHECK(command(1) == PTT_OK && iptt);
CHECK(strcmp(events[4].path, "\\\\.\\COM12") == 0);
port = 0;
CHECK(command(0) == PTT_OK && !iptt && !opened[11] && !lines[11]);
event_count = 0;
CHECK(command(1) == PTT_OK && iptt);
CHECK(command(0) == PTT_OK && !iptt && !event_count);
return 1;
}
static int open_failure(void)
{
fail_open = 1;
CHECK(command(1) == PTT_ERROR && !iptt);
CHECK(event_count == 1);
CHECK(command(0) == PTT_OK && !iptt && event_count == 1);
fail_open = 0;
CHECK(command(1) == PTT_OK && iptt);
return 1;
}
static int key_failure(DWORD operation)
{
fail(operation, ERROR_GEN_FAILURE, 1);
CHECK(command(1) == PTT_ERROR && !iptt);
CHECK(opened[10] && count(CLOSE_PORT) == 0);
CHECK(lines[10] == (operation == SETDTR ? RTS : 0));
if (operation == SETRTS) CHECK(count(SETDTR) == 0);
event_count = 0;
port = 12;
CHECK(command(1) == PTT_ERROR && !iptt && !event_count);
CHECK(command(0) == PTT_OK && !iptt && !opened[10] && !lines[10]);
CHECK(command(1) == PTT_OK && iptt);
CHECK(strcmp(events[4].path, "\\\\.\\COM12") == 0);
return 1;
}
static int rts_key_failure(void) { return key_failure(SETRTS); }
static int dtr_key_failure(void) { return key_failure(SETDTR); }
static int release_failure(DWORD operation, int attempts)
{
CHECK(command(1) == PTT_OK && iptt);
event_count = 0;
port = 0;
fail(operation, ERROR_GEN_FAILURE, attempts);
CHECK(command(0) == PTT_ERROR && iptt && opened[10]);
CHECK(count(CLRRTS) == 1 && count(CLRDTR) == 1 && count(CLRBREAK) == 1);
CHECK(count(CLOSE_PORT) == 0);
CHECK(command(1) == PTT_ERROR && iptt);
CHECK(count(SETRTS) == 0 && count(SETDTR) == 0 && count(OPEN_PORT) == 0);
if (attempts < 0) {
CHECK(command(0) == PTT_ERROR && iptt && opened[10]);
CHECK(count(CLOSE_PORT) == 0);
failures[operation] = 0;
}
CHECK(command(0) == PTT_OK && !iptt && !opened[10] && !lines[10]);
CHECK(count(CLOSE_PORT) == 1);
event_count = 0;
CHECK(command(1) == PTT_OK && iptt && !event_count);
return 1;
}
static int transient_rts_failure(void) { return release_failure(CLRRTS, 1); }
static int persistent_dtr_failure(void) { return release_failure(CLRDTR, -1); }
static int unsupported_break(void)
{
CHECK(command(1) == PTT_OK && iptt);
event_count = 0;
fail(CLRBREAK, ERROR_GEN_FAILURE, -1);
CHECK(command(0) == PTT_OK && !iptt && !opened[10] && !lines[10]);
CHECK(count(CLRRTS) == 1 && count(CLRDTR) == 1 && count(CLRBREAK) == 1);
CHECK(count(CLOSE_PORT) == 1);
CHECK(command(1) == PTT_OK && iptt && opened[11]);
return 1;
}
static int device_loss(DWORD operation, DWORD error)
{
if (operation == CLRRTS) CHECK(command(1) == PTT_OK && iptt);
fail(operation, error, -1);
CHECK(command(operation == SETDTR) == PTT_DEVICE_LOST && !iptt);
CHECK(!opened[10] && count(CLOSE_PORT) == 1);
event_count = 0;
CHECK(command(0) == PTT_OK && !iptt && !event_count);
failures[operation] = 0;
CHECK(command(1) == PTT_OK && iptt && opened[11]);
return 1;
}
static int lost_during_key(void) { return device_loss(SETDTR, ERROR_NO_SUCH_DEVICE); }
static int lost_during_release(void) { return device_loss(CLRRTS, ERROR_DEVICE_NOT_CONNECTED); }
static int close_failure(void)
{
CHECK(command(1) == PTT_OK);
fail_close = 1;
CHECK(command(0) == PTT_ERROR && !iptt && opened[10] && !lines[10]);
event_count = 0;
CHECK(command(1) == PTT_ERROR && !event_count);
fail_close = 0;
CHECK(command(0) == PTT_OK && !opened[10]);
return 1;
}
static int shutdown_cleanup(void)
{
CHECK(command(1) == PTT_OK);
event_count = 0;
fail(CLRRTS, ERROR_GEN_FAILURE, -1);
ptt_close();
CHECK(count(CLRRTS) == 1 && count(CLRDTR) == 1 && count(CLRBREAK) == 1);
CHECK(count(CLOSE_PORT) == 1 && !opened[10]);
event_count = 0;
ptt_close();
CHECK(!event_count);
return 1;
}
int main(void)
{
const struct {
const char *name;
int (*run)(void);
} tests[] = {
{"disabled and unopened release", disabled_and_unopened_release},
{"normal cycle and switch", normal_cycle_and_switch},
{"open failure", open_failure},
{"RTS key failure", rts_key_failure},
{"DTR key failure", dtr_key_failure},
{"transient RTS release failure", transient_rts_failure},
{"persistent DTR release failure", persistent_dtr_failure},
{"unsupported break", unsupported_break},
{"lost during key", lost_during_key},
{"lost during release", lost_during_release},
{"close failure", close_failure},
{"shutdown cleanup", shutdown_cleanup}
};
int failed = 0;
size_t i;
for (i = 0; i < sizeof(tests) / sizeof(tests[0]); ++i) {
int passed;
reset();
passed = tests[i].run() && !mock_failed;
printf("%s: %s\n", passed ? "PASS" : "FAIL", tests[i].name);
failed += !passed;
}
reset();
return failed ? 1 : 0;
}
|