summaryrefslogtreecommitdiffstats
path: root/tests/zsubpacket.js
blob: eaf16243b641f66c22f0ff2f302f970797815fea (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
#!/usr/bin/env node

"use strict";

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

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

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

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

tape('build, encode, parse', function(t) {
    let content = [1, 2, 3, 4];

    ["end_ack", "no_end_ack", "end_no_ack", "no_end_no_ack"].forEach( end => {
        var header = Zmodem.Subpacket.build( content, end );

        t.deepEquals(
            header.get_payload(),
            content,
            `${end}: get_payload()`
        );

        t.is(
            header.frame_end(),
            !/no_end/.test(end),
            `${end}: frame_end()`
        );

        t.is(
            header.ack_expected(),
            !/no_ack/.test(end),
            `${end}: ack_expected()`
        );

        [16, 32].forEach( crclen => {
            var encoded = header["encode" + crclen](zdle);
            var parsed = Zmodem.Subpacket["parse" + crclen](encoded);

            t.deepEquals(
                parsed.get_payload(),
                content,
                `${end}, CRC${crclen} rount-trip: get_payload()`
            );

            t.is(
                parsed.frame_end(),
                header.frame_end(),
                `${end}, CRC${crclen} rount-trip: frame_end()`
            );

            t.is(
                parsed.ack_expected(),
                header.ack_expected(),
                `${end}, CRC${crclen} rount-trip: ack_expected()`
            );
        } );
    } );

    t.end();
} );