Skip to content

Commit

Permalink
implement test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
jihyunlab-phil committed Jul 5, 2024
1 parent 8754fa5 commit db6fdf5
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 14 deletions.
11 changes: 3 additions & 8 deletions src/ciphers/cipher.creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export const CipherCreator = {

let ivLength: number | undefined;
let tagLength: number | undefined;
let additionalData: ArrayBuffer | undefined;

if (
options &&
Expand All @@ -31,17 +30,13 @@ export const CipherCreator = {
tagLength = options.tagLength;
}

if (options && options.additionalData) {
additionalData = options.additionalData;
}

switch (cipher) {
case CIPHER.AES_256_CBC:
instance = await WebCryptoCipher.create(
'AES-CBC',
256,
password,
ivLength !== undefined ? ivLength : 16,
ivLength || 16,
undefined,
undefined,
options
Expand All @@ -52,9 +47,9 @@ export const CipherCreator = {
'AES-GCM',
256,
password,
ivLength !== undefined ? ivLength : 12,
ivLength || 12,
tagLength || 128,
additionalData,
options?.additionalData,
options
);
break;
Expand Down
2 changes: 1 addition & 1 deletion src/ciphers/services/web-crypto.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export class WebCryptoCipher implements Cipher {
params['tagLength'] = tagLength;
}

if (additionalData !== undefined && additionalData !== null) {
if (additionalData) {
params['additionalData'] = additionalData;
}

Expand Down
2 changes: 1 addition & 1 deletion src/helpers/buffer.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const Buffer = {
},

toHex(buffer: ArrayBuffer) {
if (!buffer) {
if (buffer === undefined || buffer === null) {
return buffer;
}

Expand Down
37 changes: 33 additions & 4 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { WebSecureStorage, CIPHER, STORAGE } from '../src/index';

describe('Web secure storage', () => {
test(`Positive: STORAGE.LOCAL, CIPHER.AES_256_CBC`, async () => {
const storage = await WebSecureStorage.create(
let storage = await WebSecureStorage.create(
STORAGE.LOCAL,
CIPHER.AES_256_CBC,
'key'
Expand All @@ -12,6 +12,12 @@ describe('Web secure storage', () => {

await storage.setItem('item', 'value');

storage = await WebSecureStorage.create(
STORAGE.LOCAL,
CIPHER.AES_256_CBC,
'key'
);

const value = await storage.getItem('item');
storage.removeItem('item');

Expand All @@ -36,7 +42,7 @@ describe('Web secure storage', () => {
});

test(`Positive: STORAGE.LOCAL, CIPHER.AES_256_CBC - options`, async () => {
const storage = await WebSecureStorage.create(
let storage = await WebSecureStorage.create(
STORAGE.LOCAL,
CIPHER.AES_256_CBC,
'key',
Expand All @@ -47,6 +53,13 @@ describe('Web secure storage', () => {

await storage.setItem('item', 'value');

storage = await WebSecureStorage.create(
STORAGE.LOCAL,
CIPHER.AES_256_CBC,
'key',
{ salt: 'salt', iterations: 128, ivLength: 16 }
);

const value = await storage.getItem('item');
storage.removeItem('item');

Expand Down Expand Up @@ -106,7 +119,7 @@ describe('Web secure storage', () => {
});

test(`Positive: STORAGE.LOCAL, CIPHER.AES_256_GCM`, async () => {
const storage = await WebSecureStorage.create(
let storage = await WebSecureStorage.create(
STORAGE.LOCAL,
CIPHER.AES_256_GCM,
'key'
Expand All @@ -116,6 +129,12 @@ describe('Web secure storage', () => {

await storage.setItem('item', 'value');

storage = await WebSecureStorage.create(
STORAGE.LOCAL,
CIPHER.AES_256_GCM,
'key'
);

const value = await storage.getItem('item');
storage.removeItem('item');

Expand Down Expand Up @@ -192,7 +211,7 @@ describe('Web secure storage', () => {
});

test(`Positive: STORAGE.SESSION, CIPHER.AES_256_GCM - options`, async () => {
const storage = await WebSecureStorage.create(
let storage = await WebSecureStorage.create(
STORAGE.SESSION,
CIPHER.AES_256_GCM,
'key',
Expand All @@ -209,6 +228,16 @@ describe('Web secure storage', () => {

await storage.setItem('item', 'value');

storage = await WebSecureStorage.create(
STORAGE.SESSION,
CIPHER.AES_256_GCM,
'key',
{
salt: 'salt',
additionalData: new Uint8Array([1, 2, 3, 4]),
}
);

const value = await storage.getItem('item');
storage.removeItem('item');

Expand Down

0 comments on commit db6fdf5

Please sign in to comment.