Skip to content

Commit

Permalink
feat: start unit test on serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
T1B0 committed Jan 18, 2024
1 parent 951ddcf commit 30521f8
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"description": "nodejs iggy.rs binary client",
"main": "index.js",
"scripts": {
"test": "node --test --experimental-test-coverage dist/",
"test": "node --test dist/",
"clean": "rm -Rf dist/",
"build": "tsc -p tsconfig.json",
"start": "node dist/index.js"
Expand Down
41 changes: 41 additions & 0 deletions src/wire/identifier.utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
import { serializeIdentifier } from './identifier.utils.js';

describe('serializeIdentifier', () => {

it('serialize numeric id into buffer', () => {
assert.deepEqual(
serializeIdentifier(123).length,
1 + 1 + 4
);
});

it('serialize numeric string into buffer', () => {
const strId = 'Groot';
assert.deepEqual(
serializeIdentifier(strId).length,
1 + 1 + strId.length
);
});

it('throw on empty string id', () => {
assert.throws(
() => serializeIdentifier(''),
);
});

it('throw on string id > 255 bytes', () => {
assert.throws(
() => serializeIdentifier('YoLo'.repeat(65)),
);
});

it('throw on login > 255 bytes - utf8 version', () => {
assert.throws(
() => serializeIdentifier('¥Ø£Ø'.repeat(33)),
);
});

});
2 changes: 1 addition & 1 deletion src/wire/identifier.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const serializeIdentifier = (id: Id): Buffer => {
const serializeStringId = (id: string): Buffer => {
const b = Buffer.alloc(1 + 1);
const bId = Buffer.from(id);
if (bId.length > 255)
if (bId.length < 1 || bId.length > 255)
throw new Error('identifier/name should not exceed 255 chars');
b.writeUInt8(STRING);
b.writeUInt8(bId.length, 1);
Expand Down
49 changes: 49 additions & 0 deletions src/wire/session/login.command.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
import { LOGIN } from './login.command.js';

describe("Login Command", () => {

// @warn use ascii char to keep char.length === byteLength
const login = 'iggyYolo';
const password = 'unitTestSeCret';

it("serialize credentials into a buffer", () => {
assert.deepEqual(
LOGIN.serialize(login, password).length,
2 + login.length + password.length
);
});

it("throw on empty login", () => {
assert.throws(
() => LOGIN.serialize("", password)
);
});

it("throw on empty password", () => {
assert.throws(
() => LOGIN.serialize(login, "")
);
});

it("throw on login > 255 bytes", () => {
assert.throws(
() => LOGIN.serialize("YoLo".repeat(65), password)
);
});

it("throw on login > 255 bytes - utf8 version", () => {
assert.throws(
() => LOGIN.serialize("¥Ø£Ø".repeat(33), password)
);
});

it("throw on password > 255 bytes - utf8 version", () => {
assert.throws(
() => LOGIN.serialize(login, "¥Ø£Ø".repeat(33))
);
});

});
5 changes: 3 additions & 2 deletions src/wire/session/login.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ export const LOGIN = {
serialize: (username: string, password: string) => {
const bUsername = Buffer.from(username);
const bPassword = Buffer.from(password);
if (bUsername.length > 255)

if (bUsername.length < 1 || bUsername.length > 255)
throw new Error('username should not exceed 255 chars');
if (bPassword.length > 255)
if (bPassword.length < 1 || bPassword.length > 255)
throw new Error('password name should not exceed 255 chars');

const l1 = Buffer.alloc(1);
Expand Down

0 comments on commit 30521f8

Please sign in to comment.