Unit test fixes

This commit is contained in:
2021-07-30 18:43:52 -05:00
parent d428445f00
commit b58bfcf1e1
2 changed files with 16 additions and 4 deletions

View File

@@ -15,6 +15,8 @@ test(`connect fails when error occurs during createConnection`, async () => {
test(`socket receive data fails when decryption fails`, async () => { test(`socket receive data fails when decryption fails`, async () => {
let cbl = {}; let cbl = {};
const socket = { const socket = {
setNoDelay: () => {},
setKeepAlive: () => {},
on: (name, cb) => { on: (name, cb) => {
cbl[name] = cb; cbl[name] = cb;
}, },
@@ -40,6 +42,8 @@ test(`socket receive data fails when decryption fails`, async () => {
test(`disconnect succeeds if an error is thrown`, async () => { test(`disconnect succeeds if an error is thrown`, async () => {
const socket = { const socket = {
setNoDelay: () => {},
setKeepAlive: () => {},
destroy: () => { destroy: () => {
throw new Error('mock destroy error'); throw new Error('mock destroy error');
}, },
@@ -53,6 +57,8 @@ test(`disconnect succeeds if an error is thrown`, async () => {
test(`send fails on socket error`, async () => { test(`send fails on socket error`, async () => {
let cbl = {}; let cbl = {};
const socket = { const socket = {
setNoDelay: () => {},
setKeepAlive: () => {},
on: (name, cb) => { on: (name, cb) => {
cbl[name] = cb; cbl[name] = cb;
}, },
@@ -70,6 +76,8 @@ test(`send fails on socket error`, async () => {
test(`error is thrown when socket is closed`, async () => { test(`error is thrown when socket is closed`, async () => {
let cbl = {}; let cbl = {};
const socket = { const socket = {
setNoDelay: () => {},
setKeepAlive: () => {},
on: (name, cb) => { on: (name, cb) => {
cbl[name] = cb; cbl[name] = cb;
}, },
@@ -87,6 +95,8 @@ test(`error is thrown when socket is closed`, async () => {
test(`send fails when write error occurs`, async () => { test(`send fails when write error occurs`, async () => {
let cbl = {}; let cbl = {};
const socket = { const socket = {
setNoDelay: () => {},
setKeepAlive: () => {},
on: (name, cb) => { on: (name, cb) => {
cbl[name] = cb; cbl[name] = cb;
}, },

View File

@@ -24,7 +24,7 @@ export default class packet {
this.token = token; this.token = token;
} }
buffer = null; buffer = new Uint8Array(0);
decode_offset = 0; decode_offset = 0;
token; token;
@@ -180,9 +180,11 @@ export default class packet {
...this.buffer.slice(nonce.length + mac.length), ...this.buffer.slice(nonce.length + mac.length),
...this.buffer.slice(nonce.length, nonce.length + mac.length), ...this.buffer.slice(nonce.length, nonce.length + mac.length),
]); ]);
this.buffer = Buffer.from( const result = new XChaCha20Poly1305(key).open(nonce, this.buffer, aad);
new XChaCha20Poly1305(key).open(nonce, this.buffer, aad) if (!result) {
); throw new Error('decryption failed');
}
this.buffer = Buffer.from(result);
} }
this.buffer = new Uint8Array(this.buffer); this.buffer = new Uint8Array(this.buffer);