summaryrefslogtreecommitdiffstats
path: root/tests/zsession_receive.js
blob: 4b126a9a5311f53308e3c02f3e5183361d929c8d (plain)
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
#!/usr/bin/env node

"use strict";

const tape = require('blue-tape');

const SZ_PATH = require('which').sync('sz', {nothrow: true});

if (!SZ_PATH) {
    tape.only('SKIP: no “sz” in PATH!', (t) => {
        t.end();
    });
}

const spawn = require('child_process').spawn;

var helper = require('./lib/testhelp');

Object.assign(
    global,
    {
        Zmodem: require('./lib/zmodem'),
    }
);

var FILE1 = helper.make_temp_file(10 * 1024 * 1024);    //10 MiB

function _test_steps(t, sz_args, steps) {
    return helper.exec_lrzsz_steps( t, SZ_PATH, sz_args, steps );
}

tape('abort() after ZRQINIT', (t) => {
    return _test_steps( t, [FILE1], [
        (zsession, child) => {
            zsession.abort();
            return true;
        },
    ] ).then( (inputs) => {
        //console.log("inputs", inputs);

        var str = String.fromCharCode.apply( String, inputs[ inputs.length - 1 ]);
        t.ok(
            str.match(/\x18\x18\x18\x18\x18/),
            'abort() right after receipt of ZRQINIT',
        );
    } );
});

tape('abort() after ZFILE', (t) => {
    return _test_steps( t, [FILE1], [
        (zsession) => {
            zsession.start();
            return true;
        },
        (zsession) => {
            zsession.abort();
            return true;
        },
    ] ).then( (inputs) => {
        //console.log("inputs", inputs);

        var str = String.fromCharCode.apply( String, inputs[ inputs.length - 1 ]);
        t.ok(
            str.match(/\x18\x18\x18\x18\x18/),
            'abort() right after receipt of ZFILE',
        );
    } );
});

//NB: This test is not unlikely to flap since it depends
//on sz reading the abort sequence prior to finishing its read
//of the file.
tape('abort() during download', { timeout: 30000 }, (t) => {
    var child_pms = _test_steps( t, [FILE1], [
        (zsession) => {
            zsession.on("offer", (offer) => offer.accept() );
            zsession.start();
            return true;
        },
        (zsession) => {
            zsession.abort();
            return true;
        },
    ] );

    return child_pms.then( (inputs) => {
        t.notEquals( inputs, undefined, 'abort() during download ends the transmission' );

        t.ok(
            inputs.every( function(bytes) {
                var str = String.fromCharCode.apply( String, bytes );
                return !/THE_END/.test(str);
            } ),
            "the end of the file was not sent",
        );
    } );
});

//This only works because we use CRC32 to receive. CRC16 in lsz has a
//buffer overflow bug, fixed here:
//
//  https://github.com/gooselinux/lrzsz/blob/master/lrzsz-0.12.20.patch
//
tape('skip() during download', { timeout: 30000 }, (t) => {
    var filenames = [FILE1, helper.make_temp_file(12345678)];
    //filenames = ["-vvvvvvvvvvvvv", FILE1, _make_temp_file()];

    var started, second_offer;

    return _test_steps( t, filenames, [
        (zsession) => {
            if (!started) {
                function offer_taker(offer) {
                    offer.accept();
                    offer.skip();
                    zsession.off("offer", offer_taker);
                    zsession.on("offer", (offer2) => {
                        second_offer = offer2;
                        offer2.skip();
                    });
                }
                zsession.on("offer", offer_taker);
                zsession.start();
                started = true;
            }
            //return true;
        },
    ] ).then( (inputs) => {
        var never_end = inputs.every( function(bytes) {
            var str = String.fromCharCode.apply( String, bytes );
            return !/THE_END/.test(str);
        } );

        // This is race-prone.
        //t.ok( never_end, "the end of a file is never sent" );

        t.ok( !!second_offer, "we got a 2nd offer after the first" );
    } );
});

tape('skip() - immediately - at end of download', { timeout: 30000 }, (t) => {
    var filenames = [helper.make_temp_file(123)];

    var started;

    return _test_steps( t, filenames, [
        (zsession) => {
            if (!started) {
                function offer_taker(offer) {
                    offer.accept();
                    offer.skip();
                }
                zsession.on("offer", offer_taker);
                zsession.start();

                started = true;
            }
        },
    ] );
});

// Verify a skip() that happens after a transfer is complete.
// There are no assertions here.
tape('skip() - after a parse - at end of download', { timeout: 30000 }, (t) => {
    var filenames = [helper.make_temp_file(123)];

    var the_offer, started, skipped, completed;

    return _test_steps( t, filenames, [
        (zsession) => {
            if (!started) {
                function offer_taker(offer) {
                    the_offer = offer;
                    var promise = the_offer.accept();
                    promise.then( () => {
                        completed = 1;
                    } );
                }
                zsession.on("offer", offer_taker);
                zsession.start();
                started = true;
            }

            return the_offer;
        },
        () => {
            if (!skipped && !completed) {
                the_offer.skip();
                skipped = true;
            }
        },
    ] );
});

var happy_filenames = [
    helper.make_temp_file(5),
    helper.make_temp_file(3),
    helper.make_temp_file(1),
    helper.make_empty_temp_file(),
];

tape('happy-path: single batch', { timeout: 30000 }, (t) => {
    var started, the_offer;

    var args = happy_filenames;

    var buffers = [];

    var child_pms = _test_steps( t, args, [
        (zsession) => {
            if (!started) {
                function offer_taker(offer) {
                    the_offer = offer;
                    the_offer.accept( { on_input: "spool_array" } ).then( (byte_lists) => {
                        var flat = [].concat.apply([], byte_lists);
                        var str = String.fromCharCode.apply( String, flat );
                        buffers.push(str);
                    } );
                }
                zsession.on("offer", offer_taker);
                zsession.start();
                started = true;
            }

            return false;
        },
    ] );

    return child_pms.then( (inputs) => {
        t.equals( buffers[0], "xxxxx=THE_END", '5-byte transfer plus end' );
        t.equals( buffers[1], "xxx=THE_END", '3-byte transfer plus end' );
        t.equals( buffers[2], "x=THE_END", '1-byte transfer plus end' );
        t.equals( buffers[3], "", 'empty transfer plus end' );
    } );
});

tape('happy-path: individual transfers', { timeout: 30000 }, (t) => {
    var promises = happy_filenames.map( (fn) => {
        var str;

        var started;

        var child_pms = _test_steps( t, [fn], [
            (zsession) => {
                if (!started) {
                    function offer_taker(offer) {
                        offer.accept( { on_input: "spool_array" } ).then( (byte_lists) => {
                            var flat = [].concat.apply([], byte_lists);
                            str = String.fromCharCode.apply( String, flat );
                        } );
                    }
                    zsession.on("offer", offer_taker);
                    zsession.start();
                    started = true;
                }

                return false;
            },
        ] );

        return child_pms.then( () => str );
    } );

    return Promise.all(promises).then( (strs) => {
        t.equals( strs[0], "xxxxx=THE_END", '5-byte transfer plus end' );
        t.equals( strs[1], "xxx=THE_END", '3-byte transfer plus end' );
        t.equals( strs[2], "x=THE_END", '1-byte transfer plus end' );
        t.equals( strs[3], "", 'empty transfer plus end' );
    } );
});

//This doesn’t work because we automatically send ZFIN once we receive it,
//which prompts the child to finish up.
tape.skip("abort() after ZEOF", (t) => {
    var received;

    return _test_steps( t, [FILE1], [
        (zsession) => {
            zsession.on("offer", (offer) => {
                offer.accept().then( () => { received = true } );
            } );
            zsession.start();
            return true;
        },
        (zsession) => {
            if (received) {
                zsession.abort();
                return true;
            }
        },
    ] ).then( (inputs) => {
        var str = String.fromCharCode.apply( String, inputs[ inputs.length - 1 ]);
        t.is( str, "OO", "successful close despite abort" );
    } );
});