1// Copyright 2022 The Pigweed Authors 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); you may not 4// use this file except in compliance with the License. You may obtain a copy of 5// the License at 6// 7// https://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12// License for the specific language governing permissions and limitations under 13// the License. 14 15/* eslint-env browser */ 16 17import { Encoder } from './encoder'; 18import * as protocol from './protocol'; 19import * as util from './util'; 20 21const FLAG = Uint8Array.from([protocol.FLAG]); 22 23function withFCS(data: Uint8Array): Uint8Array { 24 return util.concatenate(data, protocol.frameCheckSequence(data)); 25} 26 27function withFlags(data: Uint8Array): Uint8Array { 28 return util.concatenate(FLAG, data, FLAG); 29} 30 31describe('Encoder', () => { 32 let encoder: Encoder; 33 let textEncoder: TextEncoder; 34 35 beforeEach(() => { 36 encoder = new Encoder(); 37 textEncoder = new TextEncoder(); 38 }); 39 40 it('creates frame for empty data', () => { 41 const data = textEncoder.encode(''); 42 expect(encoder.uiFrame(0, data)).toEqual( 43 withFlags(withFCS(new Uint8Array([0x01, 0x03]))), 44 ); 45 expect(encoder.uiFrame(0x1a, data)).toEqual( 46 withFlags(withFCS(new Uint8Array([0x35, 0x03]))), 47 ); 48 expect(encoder.uiFrame(0x1a, data)).toEqual( 49 withFlags(withFCS(textEncoder.encode('\x35\x03'))), 50 ); 51 }); 52 53 it('creates frame for one byte', () => { 54 const data = textEncoder.encode('A'); 55 expect(encoder.uiFrame(0, data)).toEqual( 56 withFlags(withFCS(textEncoder.encode('\x01\x03A'))), 57 ); 58 }); 59 60 it('creates frame for multibyte data', () => { 61 const data = textEncoder.encode('123456789'); 62 expect(encoder.uiFrame(0, data)).toEqual( 63 withFlags(withFCS(textEncoder.encode('\x01\x03123456789'))), 64 ); 65 }); 66 67 it('creates frame for multibyte data with address', () => { 68 const data = textEncoder.encode('123456789'); 69 expect(encoder.uiFrame(128, data)).toEqual( 70 withFlags(withFCS(textEncoder.encode('\x00\x03\x03123456789'))), 71 ); 72 }); 73 74 it('creates frame for data with escape sequence', () => { 75 const data = textEncoder.encode('\x7d'); 76 const expectedContent = util.concatenate( 77 textEncoder.encode('\x7d\x5d\x03\x7d\x5d'), 78 protocol.frameCheckSequence(textEncoder.encode('\x7d\x03\x7d')), 79 ); 80 expect(encoder.uiFrame(0x3e, data)).toEqual(withFlags(expectedContent)); 81 82 const data2 = textEncoder.encode('A\x7e\x7dBC'); 83 const expectedContent2 = util.concatenate( 84 textEncoder.encode('\x7d\x5d\x03A\x7d\x5e\x7d\x5dBC'), 85 protocol.frameCheckSequence(textEncoder.encode('\x7d\x03A\x7e\x7dBC')), 86 ); 87 expect(encoder.uiFrame(0x3e, data2)).toEqual(withFlags(expectedContent2)); 88 }); 89 90 it('Computes frameCheckSequence correctly', () => { 91 expect( 92 protocol.frameCheckSequence(textEncoder.encode('\x7d\x03A\x7e\x7dBC')), 93 ).toEqual(new Uint8Array([195, 124, 135, 9])); 94 expect( 95 protocol.frameCheckSequence(textEncoder.encode('\x7d\x5d\x03\x7d\x5d')), 96 ).toEqual(new Uint8Array([183, 144, 10, 115])); 97 expect( 98 protocol.frameCheckSequence(textEncoder.encode('\x7d\x03\x7d')), 99 ).toEqual(new Uint8Array([83, 124, 241, 166])); 100 expect( 101 protocol.frameCheckSequence(textEncoder.encode('hello pigweed')), 102 ).toEqual(new Uint8Array([34, 22, 236, 2])); 103 }); 104}); 105