Add a few unit tests for MSP protocol (#3421)

* Add a few unit tests for msp protocol

* fixup! Add a few unit tests for msp protocol
master
Richard Benkovsky 2023-04-16 14:57:34 -07:00 committed by GitHub
parent db97687d17
commit 9d9edf9266
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 71 additions and 0 deletions

71
test/js/msp.test.js Normal file
View File

@ -0,0 +1,71 @@
import {describe, expect, it} from "vitest";
import MSP from "../../src/js/msp";
describe("MSP", () => {
describe("encode_message_v1", () => {
it("handles correctly any code and no data", () => {
for (let code = 0; code < 256; code++) {
let encodedMessage = MSP.encode_message_v1(code, false);
expect(new Uint8Array(encodedMessage)).toEqual(new Uint8Array([36, 77, 60, 0, code, code]));
}
});
it("handles non-empty messages correctly", () => {
let [operationCode, inputDataLengthPadding] = crypto.getRandomValues(new Uint8Array(2));
let inputData = crypto.getRandomValues(new Uint8Array(100 + inputDataLengthPadding % 100));
let encodedMessage = new Uint8Array(MSP.encode_message_v1(operationCode, inputData));
// check that header is in place
expect(encodedMessage.slice(0, 3)).toEqual(new Uint8Array([36, 77, 60]));
// check that length got encoded as expected
expect(encodedMessage[3]).toEqual(inputData.length);
// check that operation code is there
expect(encodedMessage[4]).toEqual(operationCode);
// check that data got encoded as expected
expect(encodedMessage.slice(5, -1)).toEqual(inputData);
// and that the checksum is valid
let checksum = encodedMessage.slice(3, -1).reduce((acc, curr) => acc ^ curr);
expect(encodedMessage[encodedMessage.length - 1]).toEqual(checksum);
});
});
describe("encode_message_v2", () => {
it("handles correctly any code and no data", () => {
for (let codeLowByte = 0; codeLowByte < 256; codeLowByte++) {
for (let codeHighByte = 0; codeHighByte < 256; codeHighByte++) {
let encodedMessage = MSP.encode_message_v2(codeLowByte + codeHighByte * 256, false).slice(0, -1);
expect(new Uint8Array(encodedMessage)).toEqual(new Uint8Array([36, 88, 60, 0, codeLowByte, codeHighByte, 0, 0]));
}
}
});
it("handles non-empty messages correctly", () => {
let [lengthLowByte, lengthHighByte] = crypto.getRandomValues(new Uint8Array(2));
let inputData = crypto.getRandomValues(new Uint8Array(lengthLowByte + lengthHighByte * 256));
let [operationCodeLowByte, operationCodeHighByte] = crypto.getRandomValues(new Uint8Array(2));
let encodedMessage = new Uint8Array(MSP.encode_message_v2(operationCodeLowByte + 256 * operationCodeHighByte, inputData));
// check that header is in place
expect(encodedMessage.slice(0, 3)).toEqual(new Uint8Array([36, 88, 60]));
expect(encodedMessage[3]).toEqual(0);
// check that operation code is there
expect(encodedMessage[4]).toEqual(operationCodeLowByte);
expect(encodedMessage[5]).toEqual(operationCodeHighByte);
// check that length got encoded as expected
expect(encodedMessage[6]).toEqual(lengthLowByte);
expect(encodedMessage[7]).toEqual(lengthHighByte);
// check that data got encoded as expected
expect(encodedMessage.slice(8, -1)).toEqual(inputData);
});
});
});