summaryrefslogtreecommitdiffstats
path: root/tests/zheader.js
blob: b462da69b0ca31050c0ac2deff01c0da50b6b30e (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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#!/usr/bin/env node

"use strict";

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

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

global.Zmodem = require('./lib/zmodem');

var zdle = new Zmodem.ZDLE( { escape_ctrl_chars: true } );

tape('trim_leading_garbage', function(t) {
    var header = Zmodem.Header.build('ZACK');

    var header_octets = new Map( [
        [ "hex", header.to_hex(), ],
        [ "b16", header.to_binary16(zdle), ],
        [ "b32", header.to_binary32(zdle), ],
    ] );

    var leading_garbage = [
        "",
        " ",
        "\n\n",
        "\r\n\r\n",
        "*",
        "**",
        "*\x18",
        "*\x18D",
        "**\x18",
    ];

    leading_garbage.forEach( (garbage) => {
        let garbage_json = JSON.stringify(garbage);
        let garbage_octets = testhelp.string_to_octets( garbage );

        for ( let [label, hdr_octets] of header_octets ) {
            var input = garbage_octets.slice(0).concat( hdr_octets );
            var trimmed = Zmodem.Header.trim_leading_garbage(input);

            t.deepEquals(trimmed, garbage_octets, `${garbage_json} + ${label}: garbage trimmed`);
            t.deepEquals(input, hdr_octets, `… leaving the header`);
        }
    } );

    //----------------------------------------------------------------------

    //input, number of bytes trimmed
    var partial_trims = [
        [ "*", 0 ],
        [ "**", 0 ],
        [ "***", 1 ],
        [ "*\x18**", 2 ],
        [ "*\x18*\x18", 2 ],
        [ "*\x18*\x18**", 4 ],
        [ "*\x18*\x18*\x18", 4 ],
    ];

    partial_trims.forEach( (cur) => {
        let [ input, trimmed_count ] = cur;

        let input_json = JSON.stringify(input);

        let input_octets = testhelp.string_to_octets(input);

        let garbage = Zmodem.Header.trim_leading_garbage(input_octets.slice(0));

        t.deepEquals(
            garbage,
            input_octets.slice(0, trimmed_count),
            `${input_json}: trim first ${trimmed_count} byte(s)`
        );
    } );

    t.end();
});

//Test that we parse a trailing 0x8a, since we ourselves follow the
//documentation and put a plain LF (0x0a).
tape('parse_hex', function(t) {
    var octets = testhelp.string_to_octets( "**\x18B0901020304a57f\x0d\x8a" );

    var parsed = Zmodem.Header.parse( octets );

    t.is( parsed[1], 16, 'CRC size' );

    t.is(
        parsed[0].NAME,
        'ZRPOS',
        'parsed NAME'
    );

    t.is(
        parsed[0].TYPENUM,
        9,
        'parsed TYPENUM'
    );

    t.is(
        parsed[0].get_offset(),
        0x04030201,             //it’s little-endian
        'parsed offset'
    );

    t.end();
} );

tape('round-trip, empty headers', function(t) {
    ["ZRQINIT", "ZSKIP", "ZABORT", "ZFIN", "ZFERR"].forEach( (n) => {
        var orig = Zmodem.Header.build(n);

        var hex = orig.to_hex();
        var b16 = orig.to_binary16(zdle);
        var b32 = orig.to_binary32(zdle);

        var rounds = new Map( [
            [ "to_hex", hex ],
            [ "to_binary16", b16 ],
            [ "to_binary32", b32 ],
        ] );

        for ( const [ enc, h ] of rounds ) {
            let [ parsed, crclen ] = Zmodem.Header.parse(h);

            t.is( parsed.NAME, orig.NAME, `${n}, ${enc}: NAME` );
            t.is( parsed.TYPENUM, orig.TYPENUM, `${n}, ${enc}: TYPENUM` );

            //Here’s where we test the CRC length in the response.
            t.is(
                crclen,
                /32/.test(enc) ? 32 : 16,
                `${n}, ${enc}: CRC length`,
            );
        }
    } );

    t.end();
} );

tape('round-trip, offset headers', function(t) {
    ["ZRPOS", "ZDATA", "ZEOF"].forEach( (n) => {
        var orig = Zmodem.Header.build(n, 12345);

        var hex = orig.to_hex();
        var b16 = orig.to_binary16(zdle);
        var b32 = orig.to_binary32(zdle);

        var rounds = new Map( [
            [ "to_hex", hex ],
            [ "to_binary16", b16 ],
            [ "to_binary32", b32 ],
        ] );

        for ( const [ enc, h ] of rounds ) {
            //Here’s where we test that parse() leaves in trailing bytes.
            let extra = [99, 99, 99];
            let bytes_with_extra = h.slice().concat(extra);

            let parsed = Zmodem.Header.parse(bytes_with_extra)[0];

            t.is( parsed.NAME, orig.NAME, `${n}, ${enc}: NAME` );
            t.is( parsed.TYPENUM, orig.TYPENUM, `${n}, ${enc}: TYPENUM` );
            t.is( parsed.get_offset(), orig.get_offset(), `${n}, ${enc}: get_offset()` );

            let expected = extra.slice(0);
            if (enc === "to_hex") {
                expected.splice( 0, 0, Zmodem.ZMLIB.XON );
            }

            t.deepEquals(
                bytes_with_extra,
                expected,
                `${enc}: parse() leaves in trailing bytes`,
            );
        }
    } );

    t.end();
} );

tape('round-trip, ZSINIT', function(t) {
    var opts = [
        [],
        ["ESCCTL"],
    ];

    opts.forEach( (args) => {
        var orig = Zmodem.Header.build("ZSINIT", args);

        var hex = orig.to_hex();
        var b16 = orig.to_binary16(zdle);
        var b32 = orig.to_binary32(zdle);

        var rounds = new Map( [
            [ "to_hex", hex ],
            [ "to_binary16", b16 ],
            [ "to_binary32", b32 ],
        ] );

        var args_str = JSON.stringify(args);

        for ( const [ enc, h ] of rounds ) {
            let parsed = Zmodem.Header.parse(h)[0];

            t.is( parsed.NAME, orig.NAME, `opts ${args_str}: ${enc}: NAME` );
            t.is( parsed.TYPENUM, orig.TYPENUM, `opts ${args_str}: ${enc}: TYPENUM` );

            t.is( parsed.escape_ctrl_chars(), orig.escape_ctrl_chars(), `opts ${args_str}: ${enc}: escape_ctrl_chars()` );
            t.is( parsed.escape_8th_bit(), orig.escape_8th_bit(), `opts ${args_str}: ${enc}: escape_8th_bit()` );
        }
    } );

    t.end();
} );

tape('round-trip, ZRINIT', function(t) {
    var opts = [];

    [ [], ["CANFDX"] ].forEach( (canfdx) => {
        [ [], ["CANOVIO"] ].forEach( (canovio) => {
            [ [], ["CANBRK"] ].forEach( (canbrk) => {
                [ [], ["CANFC32"] ].forEach( (canfc32) => {
                    [ [], ["ESCCTL"] ].forEach( (escctl) => {
                        opts.push( [
                            ...canfdx,
                            ...canovio,
                            ...canbrk,
                            ...canfc32,
                            ...escctl,
                        ] );
                    } );
                } );
            } );
        } );
    } );

    opts.forEach( (args) => {
        var orig = Zmodem.Header.build("ZRINIT", args);

        var hex = orig.to_hex();
        var b16 = orig.to_binary16(zdle);
        var b32 = orig.to_binary32(zdle);

        var rounds = new Map( [
            [ "to_hex", hex ],
            [ "to_binary16", b16 ],
            [ "to_binary32", b32 ],
        ] );

        var args_str = JSON.stringify(args);

        for ( const [ enc, h ] of rounds ) {
            let parsed = Zmodem.Header.parse(h)[0];

            t.is( parsed.NAME, orig.NAME, `opts ${args_str}: ${enc}: NAME` );
            t.is( parsed.TYPENUM, orig.TYPENUM, `opts ${args_str}: ${enc}: TYPENUM` );

            t.is( parsed.can_full_duplex(), orig.can_full_duplex(), `opts ${args_str}: ${enc}: can_full_duplex()` );
            t.is( parsed.can_overlap_io(), orig.can_overlap_io(), `opts ${args_str}: ${enc}: can_overlap_io()` );
            t.is( parsed.can_break(), orig.can_break(), `opts ${args_str}: ${enc}: can_break()` );
            t.is( parsed.can_fcs_32(), orig.can_fcs_32(), `opts ${args_str}: ${enc}: can_fcs_32()` );
            t.is( parsed.escape_ctrl_chars(), orig.escape_ctrl_chars(), `opts ${args_str}: ${enc}: escape_ctrl_chars()` );
            t.is( parsed.escape_8th_bit(), orig.escape_8th_bit(), `opts ${args_str}: ${enc}: escape_8th_bit()` );
        }
    } );

    t.end();
} );

tape('hex_final_XON', function(t) {
    var hex_ZFIN = Zmodem.Header.build("ZFIN").to_hex();

    t.notEquals(
        hex_ZFIN.slice(-1)[0],
        Zmodem.ZMLIB.XON,
        'ZFIN hex does NOT end with XON',
    );

    var hex_ZACK = Zmodem.Header.build("ZACK").to_hex();

    t.notEquals(
        hex_ZACK.slice(-1)[0],
        Zmodem.ZMLIB.XON,
        'ZACK hex does NOT end with XON',
    );

    var headers = [
        "ZRQINIT",
        Zmodem.Header.build("ZRINIT", []),
        Zmodem.Header.build("ZSINIT", []),
        "ZRPOS",
        "ZABORT",
        "ZFERR",
    ];

    //These are the only headers we expect to send as hex … right?
    headers.forEach( hdr => {
        if (typeof hdr === "string") hdr = Zmodem.Header.build(hdr);

        t.is(
            hdr.to_hex().slice(-1)[0],
            Zmodem.ZMLIB.XON,
            `${hdr.NAME} hex ends with XON`
        );
    } );

    t.end();
} );