Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fadccd4a16d69ccad5c212c6cc2ea28dbf4ced44 (plain) (blame)
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/*******************************************************************************
 * Copyright (c) 2008, 2011 Wind River Systems, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.internal.debug.tests;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Random;

import org.eclipse.tcf.protocol.IChannel;
import org.eclipse.tcf.protocol.IToken;
import org.eclipse.tcf.protocol.Protocol;
import org.eclipse.tcf.services.IFileSystem;
import org.eclipse.tcf.services.IFileSystem.DirEntry;
import org.eclipse.tcf.services.IFileSystem.FileAttrs;
import org.eclipse.tcf.services.IFileSystem.FileSystemException;
import org.eclipse.tcf.services.IFileSystem.IFileHandle;
import org.eclipse.tcf.util.TCFFileInputStream;
import org.eclipse.tcf.util.TCFFileOutputStream;

class TestFileSystem implements ITCFTest, IFileSystem.DoneStat,
        IFileSystem.DoneOpen, IFileSystem.DoneClose,
        IFileSystem.DoneWrite, IFileSystem.DoneRead,
        IFileSystem.DoneRename, IFileSystem.DoneRealPath,
        IFileSystem.DoneRemove, IFileSystem.DoneRoots,
        IFileSystem.DoneReadDir {

    private final TCFTestSuite test_suite;
    private final int channel_id;

    private static final int
        STATE_PRE = 0,
        STATE_RD_DIR = 1,
        STATE_WRITE = 2,
        STATE_READ = 3,
        STATE_OUT = 4,
        STATE_INP = 5,
        STATE_EXIT = 6;

    private final IFileSystem files;
    private final Random rnd = new Random();
    private final LinkedList<String> tmp_files = new LinkedList<String>();
    private final HashMap<IToken,Object> cmds = new HashMap<IToken,Object>();
    private byte[] data;
    private String root;
    private String tmp_path;
    private String file_name;
    private IFileHandle handle;
    private int state = STATE_PRE;
    private boolean async_close;

    private static class ReadCmd {
        int offs;
        int size;
    }

    TestFileSystem(TCFTestSuite test_suite, IChannel channel, int channel_id) {
        this.test_suite = test_suite;
        this.channel_id = channel_id;
        files = channel.getRemoteService(IFileSystem.class);
    }

    public void start() {
        if (files == null) {
            test_suite.done(this, null);
        }
        else {
            files.roots(this);
        }
    }

    public void doneRoots(IToken token, FileSystemException error, DirEntry[] entries) {
        assert state == STATE_PRE;
        if (error != null) {
            exit(error);
        }
        else if (entries == null || entries.length == 0) {
            exit(new Exception("Invalid FileSysrem.roots responce: empty roots array"));
        }
        else {
            for (DirEntry d : entries) {
                if (d.filename.startsWith("A:")) continue;
                if (d.filename.startsWith("B:")) continue;
                root = d.filename;
                break;
            }
            if (root == null) exit(new Exception("Invalid FileSystem.roots responce: no suitable root"));
            else files.opendir(root, this);
        }
    }

    public void doneReadDir(IToken token, FileSystemException error,
            DirEntry[] entries, boolean eof) {
        if (error != null) {
            exit(error);
        }
        else if (state == STATE_PRE) {
            if (entries != null && tmp_path == null) {
                for (DirEntry e : entries) {
                    if (e.filename.equals("tmp") || e.filename.equalsIgnoreCase("temp")) {
                        tmp_path = root;
                        if (!tmp_path.endsWith("/")) tmp_path += "/";
                        tmp_path += e.filename;
                        break;
                    }
                }
            }
            if (eof) {
                if (tmp_path == null) {
                    exit(new Exception("File system test filed: cannot find temporary directory"));
                    return;
                }
                files.close(handle, this);
            }
            else {
                files.readdir(handle, this);
            }
        }
        else if (state == STATE_RD_DIR) {
            if (entries != null) {
                for (DirEntry e : entries) {
                    if (e.filename.startsWith("tcf-test-" + channel_id + "-")) {
                        tmp_files.add(e.filename);
                    }
                }
            }
            if (eof) {
                files.close(handle, this);
            }
            else {
                files.readdir(handle, this);
            }
        }
        else {
            assert false;
        }
    }

    public void doneStat(IToken token, FileSystemException error, FileAttrs attrs) {
        if (error != null) {
            exit(error);
        }
        else if (state == STATE_READ) {
            if (attrs.size != data.length) {
                exit(new Exception("Invalid FileSysrem.fstat responce: wrong file size"));
            }
            else {
                files.close(handle, this);
            }
        }
        else if (state == STATE_WRITE) {
            char[] bf = new char[64];
            for (int i = 0; i < bf.length; i++) {
                char ch = (char)(rnd.nextInt(0x4000) + 0x20);
                switch (ch) {
                case '<':
                case '>':
                case ':':
                case '"':
                case '/':
                case '\\':
                case '|':
                case '?':
                case '*':
                case '~':
                    ch = '-';
                    break;
                }
                bf[i] = ch;
            }
            file_name = tmp_path + "/tcf-test-" + channel_id + "-" + new String(bf) + ".tmp";
            files.open(file_name, IFileSystem.TCF_O_CREAT | IFileSystem.TCF_O_TRUNC | IFileSystem.TCF_O_WRITE, null, this);
        }
        else {
            assert false;
        }
    }

    public void doneOpen(IToken token, FileSystemException error, final IFileHandle handle) {
        if (error != null) {
            exit(error);
        }
        else {
            this.handle = handle;
            if (state == STATE_READ) {
                for (int i = 0; i < rnd.nextInt(8); i++) {
                    ReadCmd cmd = new ReadCmd();
                    cmd.offs = rnd.nextInt(data.length);
                    cmd.size = rnd.nextInt(data.length - cmd.offs) + 2;
                    cmds.put(files.read(handle, cmd.offs, cmd.size, this), cmd);
                }
                if (rnd.nextBoolean()) {
                    ReadCmd cmd = new ReadCmd();
                    cmd.offs = 0;
                    cmd.size = data.length + 1;
                    cmds.put(files.read(handle, cmd.offs, cmd.size, this), cmd);
                }
                else {
                    int pos = 0;
                    while (pos < data.length) {
                        int size = rnd.nextInt(data.length - pos) + 1;
                        ReadCmd cmd = new ReadCmd();
                        cmd.offs = pos;
                        cmd.size = size + 1;
                        cmds.put(files.read(handle, cmd.offs, cmd.size, this), cmd);
                        pos += size;
                    }
                }
                async_close = rnd.nextBoolean();
                if (async_close) files.close(handle, this);
            }
            else if (state == STATE_WRITE) {
                data = new byte[rnd.nextInt(0x1000) + 1];
                rnd.nextBytes(data);
                if (rnd.nextBoolean()) {
                    cmds.put(files.write(handle, 0, data, 0, data.length, this), null);
                }
                else {
                    int pos = 0;
                    while (pos < data.length) {
                        int size = rnd.nextInt(data.length - pos) + 1;
                        cmds.put(files.write(handle, pos, data, pos, size, this), null);
                        pos += size;
                    }
                }
                async_close = rnd.nextBoolean();
                if (async_close) files.close(handle, this);
            }
            else if (state == STATE_INP) {
                Thread thread = new Thread() {
                    public void run() {
                        try {
                            int pos = 0;
                            int len = data.length * 16;
                            byte[] buf = new byte[333];
                            int buf_pos = 0;
                            int buf_len = 0;
                            boolean mark = true;
                            boolean reset = true;
                            int mark_pos = rnd.nextInt(len - 1);
                            int reset_pos = mark_pos + rnd.nextInt(len - mark_pos);
                            assert reset_pos >= mark_pos;
                            InputStream inp = new TCFFileInputStream(handle, 133);
                            for (;;) {
                                if (mark && pos == mark_pos) {
                                    inp.mark(len);
                                    mark = false;
                                }
                                if (reset && pos == reset_pos) {
                                    inp.reset();
                                    reset = false;
                                    pos = mark_pos;
                                    buf_pos = buf_len = 0;
                                }
                                int ch = 0;
                                if (buf_pos >= buf_len && (pos >= mark_pos || pos + buf.length <= mark_pos)) {
                                    buf_len = inp.read(buf, buf_pos = 0, buf.length);
                                    if (buf_len < 0) break;
                                }
                                if (buf_pos < buf_len) {
                                    ch = buf[buf_pos++] & 0xff;
                                }
                                else {
                                    ch = inp.read();
                                    if (ch < 0) break;
                                }
                                int dt = data[pos % data.length] & 0xff;
                                if (ch != dt) {
                                    error(new Exception("Invalid TCFFileInputStream.read responce: wrong data at offset " + pos +
                                            ", expected " + dt + ", actual " + ch));
                                }
                                pos++;
                            }
                            if (pos != data.length * 16) {
                                error(new Exception("Invalid TCFFileInputStream.read responce: wrong file length: " +
                                        "expected " + data.length + ", actual " + pos));
                            }
                            inp.close();
                            Protocol.invokeLater(new Runnable() {
                                public void run() {
                                    state = STATE_EXIT;
                                    files.rename(file_name, file_name + ".rnm", TestFileSystem.this);
                                }
                            });
                        }
                        catch (Throwable x) {
                            error(x);
                        }
                    }
                    private void error(final Throwable x) {
                        Protocol.invokeLater(new Runnable() {
                            public void run() {
                                exit(x);
                            }
                        });
                    }
                };
                thread.setName("TCF FileSystem Test");
                thread.start();
            }
            else if (state == STATE_OUT) {
                rnd.nextBytes(data);
                Thread thread = new Thread() {
                    public void run() {
                        try {
                            int pos = 0;
                            int len = data.length * 16;
                            OutputStream out = new TCFFileOutputStream(handle, 121);
                            while (pos < len) {
                                int m = pos % data.length;
                                int n = rnd.nextInt(1021);
                                if (n > data.length - m) n = data.length - m;
                                out.write(data, m, n);
                                pos += n;
                                if (pos == len) break;
                                out.write(data[pos % data.length] & 0xff);
                                pos++;
                            }
                            out.close();
                            Protocol.invokeLater(new Runnable() {
                                public void run() {
                                    state = STATE_INP;
                                    files.open(file_name, IFileSystem.TCF_O_READ, null, TestFileSystem.this);
                                }
                            });
                        }
                        catch (Throwable x) {
                            error(x);
                        }
                    }
                    private void error(final Throwable x) {
                        Protocol.invokeLater(new Runnable() {
                            public void run() {
                                exit(x);
                            }
                        });
                    }
                };
                thread.setName("TCF FileSystem Test");
                thread.start();
            }
            else {
                assert state == STATE_PRE || state == STATE_RD_DIR;
                files.readdir(handle, this);
            }
        }
    }

    public void doneWrite(IToken token, FileSystemException error) {
        assert cmds.containsKey(token);
        cmds.remove(token);
        if (error != null) {
            exit(error);
        }
        else if (cmds.size() == 0 && !async_close) {
            files.close(handle, this);
        }
    }

    public void doneRead(IToken token, FileSystemException error, byte[] data, boolean eof) {
        assert cmds.containsKey(token);
        ReadCmd cmd = (ReadCmd)cmds.remove(token);
        if (error != null) {
            exit(error);
        }
        else {
            if (cmd.offs + cmd.size > this.data.length && !eof) {
                exit(new Exception("Invalid FileSysrem.read responce: EOF expected"));
            }
            else if (data.length != (eof ? cmd.size - 1 : cmd.size)) {
                exit(new Exception("Invalid FileSysrem.read responce: wrong data array size"));
            }
            else {
                for (int i = 0; i < data.length; i++) {
                    if (data[i] != this.data[i + cmd.offs]) {
                        exit(new Exception("Invalid FileSysrem.read responce: wrong data at offset " + i +
                                ", expected " + this.data[i + cmd.offs] + ", actual " + data[i]));
                        return;
                    }
                }
                if (cmds.size() == 0 && !async_close) files.fstat(handle, this);
            }
        }
    }

    public void doneClose(IToken token, FileSystemException error) {
        if (error != null) {
            exit(error);
        }
        else {
            handle = null;
            if (state == STATE_PRE) {
                state = STATE_RD_DIR;
                files.opendir(tmp_path, this);
            }
            else if (state == STATE_RD_DIR) {
                state = STATE_WRITE;
                files.realpath(tmp_path, this);
            }
            else if (state == STATE_WRITE) {
                state = STATE_READ;
                files.open(file_name, IFileSystem.TCF_O_READ, null, this);
            }
            else if (state == STATE_READ) {
                state = STATE_OUT;
                files.open(file_name, IFileSystem.TCF_O_WRITE, null, this);
            }
            else {
                assert false;
            }
        }
    }

    public void doneRename(IToken token, FileSystemException error) {
        assert state == STATE_EXIT;
        if (error != null) {
            exit(error);
        }
        else {
            files.realpath(file_name + ".rnm", this);
        }
    }

    public void doneRealPath(IToken token, FileSystemException error, String path) {
        if (error != null) {
            exit(error);
        }
        else if (state == STATE_WRITE) {
            tmp_path = path;
            if (tmp_files.size() > 0) {
                files.remove(tmp_path + "/" + tmp_files.removeFirst(), this);
            }
            else {
                files.stat(tmp_path, this);
            }
        }
        else if (!path.equals(file_name + ".rnm")) {
            exit(new Exception("Invalid FileSysrem.realpath responce: " + path));
        }
        else {
            files.remove(file_name + ".rnm", this);
        }
    }

    public void doneRemove(IToken token, FileSystemException error) {
        if (error != null) {
            exit(error);
        }
        else if (state == STATE_WRITE) {
            if (tmp_files.size() > 0) {
                files.remove(tmp_path + "/" + tmp_files.removeFirst(), this);
            }
            else {
                files.stat(tmp_path, this);
            }
        }
        else if (state == STATE_EXIT) {
            exit(null);
        }
        else {
            assert false;
        }
    }

    private void exit(Throwable x) {
        if (!test_suite.isActive(this)) return;
        test_suite.done(this, x);
    }

    public boolean canResume(String id) {
        return true;
    }
}

Back to the top