Implemented Packet structure decoding
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { describe, expect, test } from 'vitest';
|
||||
import { Packet } from '../src/packet';
|
||||
import { PayloadType, RouteType, NodeType, TracePayload, AdvertPayload, RequestPayload, TextPayload, ResponsePayload, RawCustomPayload, AnonReqPayload } from '../src/packet.types';
|
||||
import { PayloadType, RouteType, NodeType, TracePayload, AdvertPayload, RequestPayload, TextPayload, ResponsePayload, RawCustomPayload, AnonReqPayload, Payload, AckPayload, PathPayload, GroupDataPayload, GroupTextPayload } from '../src/packet.types';
|
||||
import { hexToBytes, bytesToHex } from '../src/parser';
|
||||
|
||||
describe('Packet.fromBytes', () => {
|
||||
@@ -51,7 +51,7 @@ describe('Packet.fromBytes', () => {
|
||||
const pkt = Packet.fromBytes(bytes);
|
||||
expect(pkt.routeType).toBe(RouteType.TRANSPORT_DIRECT);
|
||||
expect(pkt.payloadType).toBe(PayloadType.TRACE);
|
||||
const payload = pkt.decode();
|
||||
const payload = pkt.decode() as TracePayload;
|
||||
expect(payload.type).toBe(PayloadType.TRACE);
|
||||
// the TRACE payload format has been updated; ensure we decode a TRACE payload
|
||||
expect(payload.type).toBe(PayloadType.TRACE);
|
||||
@@ -148,7 +148,7 @@ describe('Packet decode branches and transport/path parsing', () => {
|
||||
|
||||
test('ACK decode and RAW_CUSTOM', () => {
|
||||
const ackPayload = new Uint8Array([0x01,0x02,0x03,0x04]);
|
||||
const ack = Packet.fromBytes(makePacket(PayloadType.ACK, RouteType.DIRECT, new Uint8Array([]), ackPayload)).decode();
|
||||
const ack = Packet.fromBytes(makePacket(PayloadType.ACK, RouteType.DIRECT, new Uint8Array([]), ackPayload)).decode() as AckPayload;
|
||||
expect(ack.type).toBe(PayloadType.ACK);
|
||||
|
||||
const custom = new Uint8Array([0x99,0x88,0x77]);
|
||||
@@ -173,9 +173,9 @@ describe('Packet decode branches and transport/path parsing', () => {
|
||||
|
||||
test('GROUP_TEXT and GROUP_DATA decode', () => {
|
||||
const payload = new Uint8Array([0x55, 0x01, 0x02, 0x03]); // channelHash + mac(2) + cipher
|
||||
const gt = Packet.fromBytes(makePacket(PayloadType.GROUP_TEXT, RouteType.DIRECT, new Uint8Array([]), payload)).decode();
|
||||
const gt = Packet.fromBytes(makePacket(PayloadType.GROUP_TEXT, RouteType.DIRECT, new Uint8Array([]), payload)).decode() as GroupTextPayload;
|
||||
expect(gt.type).toBe(PayloadType.GROUP_TEXT);
|
||||
const gd = Packet.fromBytes(makePacket(PayloadType.GROUP_DATA, RouteType.DIRECT, new Uint8Array([]), payload)).decode();
|
||||
const gd = Packet.fromBytes(makePacket(PayloadType.GROUP_DATA, RouteType.DIRECT, new Uint8Array([]), payload)).decode() as GroupDataPayload;
|
||||
expect(gd.type).toBe(PayloadType.GROUP_DATA);
|
||||
});
|
||||
|
||||
@@ -191,7 +191,7 @@ describe('Packet decode branches and transport/path parsing', () => {
|
||||
|
||||
test('PATH and TRACE decode nodes', () => {
|
||||
const pathPayload = new Uint8Array([0x0a, 0x0b]);
|
||||
const path = Packet.fromBytes(makePacket(PayloadType.PATH, RouteType.DIRECT, new Uint8Array([]), pathPayload)).decode();
|
||||
const path = Packet.fromBytes(makePacket(PayloadType.PATH, RouteType.DIRECT, new Uint8Array([]), pathPayload)).decode() as PathPayload;
|
||||
expect(path.type).toBe(PayloadType.PATH);
|
||||
|
||||
const nodes = new Uint8Array([0x01,0x02,0x03]);
|
||||
@@ -217,10 +217,10 @@ describe('Packet decode branches and transport/path parsing', () => {
|
||||
arr.set(pathBytes, parts.length);
|
||||
arr.set(payload, parts.length + pathBytes.length);
|
||||
const pkt = Packet.fromBytes(arr);
|
||||
expect(pkt.pathHashCount).toBe(2);
|
||||
expect(pkt.pathHashSize).toBe(3);
|
||||
expect(pkt.pathHashes.length).toBe(2);
|
||||
expect(pkt.pathHashes[0]).toBe(bytesToHex(pathBytes.subarray(0,3)));
|
||||
expect(pkt.pathHashCount).toBe(3);
|
||||
expect(pkt.pathHashSize).toBe(2);
|
||||
expect(pkt.pathHashes.length).toBe(3);
|
||||
expect(pkt.pathHashes[0]).toBe(bytesToHex(pathBytes.subarray(0,2)));
|
||||
});
|
||||
|
||||
test('unsupported payload type throws', () => {
|
||||
@@ -231,3 +231,24 @@ describe('Packet decode branches and transport/path parsing', () => {
|
||||
expect(() => pkt.decode()).toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe("Packet.decode overloads", () => {
|
||||
const ackBytes = new Uint8Array([ /* header */ 13, /* pathLength */ 0, /* payload (4 bytes checksum) */ 1, 2, 3, 4 ]);
|
||||
|
||||
test("decode() returns payload only", () => {
|
||||
const pkt = Packet.fromBytes(ackBytes);
|
||||
const payload = pkt.decode() as Payload;
|
||||
expect(payload.type).toBe(PayloadType.ACK);
|
||||
expect((payload as any).checksum).toEqual(new Uint8Array([1, 2, 3, 4]));
|
||||
});
|
||||
|
||||
test("decode(true) returns { payload, structure }", () => {
|
||||
const pkt = Packet.fromBytes(ackBytes);
|
||||
const res = pkt.decode(true) as any;
|
||||
expect(res).toHaveProperty("payload");
|
||||
expect(res).toHaveProperty("structure");
|
||||
expect(res.payload.type).toBe(PayloadType.ACK);
|
||||
expect(Array.isArray(res.structure)).toBe(true);
|
||||
expect(res.structure[res.structure.length - 1].name).toBe("ack payload");
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user