Skip to content

Commit de56272

Browse files
authored
fix: correct length-coded number size for the 3-byte range (#4500)
* fix: correct length-coded number size for the 3-byte range * test: align new test names with existing tag naming
1 parent ca10232 commit de56272

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

lib/packets/packet.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1135,7 +1135,7 @@ class Packet {
11351135
return 3;
11361136
}
11371137
if (n < 0xffffff) {
1138-
return 5;
1138+
return 4;
11391139
}
11401140
return 9;
11411141
}

test/unit/packets/test-length-coded-number.test.mts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,42 @@ describe('writeLengthCodedNumber / readLengthCodedNumber roundtrip', () => {
3939
strict.strictEqual(roundtrip(0x1ffffffffff), 2199023255551);
4040
});
4141
});
42+
43+
describe('lengthCodedNumberLength matches the bytes writeLengthCodedNumber emits', () => {
44+
const writtenBytes = (n: number): number => {
45+
const buffer = Buffer.alloc(16);
46+
const packet = new Packet(0, buffer, 0, buffer.length);
47+
packet.offset = 0;
48+
packet.writeLengthCodedNumber(n);
49+
return packet.offset;
50+
};
51+
52+
it('1-byte range', () => {
53+
strict.strictEqual(Packet.lengthCodedNumberLength(250), writtenBytes(250));
54+
});
55+
56+
it('0xFC tag', () => {
57+
strict.strictEqual(
58+
Packet.lengthCodedNumberLength(0xfffe),
59+
writtenBytes(0xfffe)
60+
);
61+
});
62+
63+
it('0xFD tag', () => {
64+
strict.strictEqual(
65+
Packet.lengthCodedNumberLength(0xffff),
66+
writtenBytes(0xffff)
67+
);
68+
strict.strictEqual(
69+
Packet.lengthCodedNumberLength(0xfffffe),
70+
writtenBytes(0xfffffe)
71+
);
72+
});
73+
74+
it('0xFE tag', () => {
75+
strict.strictEqual(
76+
Packet.lengthCodedNumberLength(0xffffff),
77+
writtenBytes(0xffffff)
78+
);
79+
});
80+
});

0 commit comments

Comments
 (0)