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 4, 2024
1 parent 8b5e42a commit 95a0eba
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 7 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ jobs:
- name: Install
run: npm ci

# - name: Test
# run: npm run test
- name: Test
run: npm run test

- name: Build
run: npm run build --if-present

# - name: Upload coverage reports to Codecov
# uses: codecov/codecov-action@v3
# env:
# CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
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 @@ -56,7 +56,7 @@ export class WebCryptoCipher implements Cipher {
}

const textEncoder = new TextEncoder();
const iv = crypto.getRandomValues(new Uint8Array(this.ivLength || 16));
const iv = crypto.getRandomValues(new Uint8Array(this.ivLength));

const ciphertext = await crypto.subtle.encrypt(
{
Expand Down
61 changes: 61 additions & 0 deletions test/ciphers/services/web-crypto.service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { WebCryptoCipher } from '../../../src/ciphers/services/web-crypto.service';
import { Key } from '../../../src/helpers/key.helper';
import { Buffer } from '../../../src/helpers/buffer.helper';

describe('Web crypto service', () => {
test(`Negative: encrypt() - key does not exist.`, async () => {
const spy = jest.spyOn(Key as any, 'pbkdf2');
spy.mockImplementation(() => {
return undefined;
});

const cipher = await WebCryptoCipher.create('AES-GCM', 256, 'password', 12);

expect(async () => {
await cipher.encrypt('text');
}).rejects.toThrow(Error('key does not exist.'));

spy.mockReset();
spy.mockRestore();
});

test(`Negative: decrypt() - key does not exist.`, async () => {
const spy = jest.spyOn(Key as any, 'pbkdf2');
spy.mockImplementation(() => {
return undefined;
});

const cipher = await WebCryptoCipher.create('AES-GCM', 256, 'password', 12);

expect(async () => {
await cipher.decrypt('text');
}).rejects.toThrow(Error('key does not exist.'));

spy.mockReset();
spy.mockRestore();
});

test(`Negative: decrypt() - key does not exist.`, async () => {
const cipher = await WebCryptoCipher.create('AES-GCM', 256, 'password', 12);

expect(async () => {
await cipher.decrypt('0');
}).rejects.toThrow(Error('invalid text.'));
});

test(`Negative: decrypt() - data conversion failed.`, async () => {
const spy = jest.spyOn(Buffer as any, 'toUint8Array');
spy.mockImplementation(() => {
return undefined;
});

const cipher = await WebCryptoCipher.create('AES-GCM', 256, 'password', 12);

expect(async () => {
await cipher.decrypt('00000000000000000000000000000000');
}).rejects.toThrow(Error('data conversion failed.'));

spy.mockReset();
spy.mockRestore();
});
});
36 changes: 36 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,24 @@ describe('Web secure storage', () => {
expect(value).toBe('');
});

test(`Positive: STORAGE.LOCAL, CIPHER.AES_256_GCM - options`, async () => {
const storage = await WebSecureStorage.create(
STORAGE.LOCAL,
CIPHER.AES_256_GCM,
'key',
{ salt: 'salt', iterations: 128, ivLength: 12 }
);

storage.clear();

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

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

expect(value).toBe('value');
});

test(`Positive: STORAGE.SESSION, CIPHER.AES_256_GCM`, async () => {
const storage = await WebSecureStorage.create(
STORAGE.SESSION,
Expand Down Expand Up @@ -71,6 +89,24 @@ describe('Web secure storage', () => {
expect(value).toBe('');
});

test(`Positive: STORAGE.SESSION, CIPHER.AES_256_GCM - options`, async () => {
const storage = await WebSecureStorage.create(
STORAGE.SESSION,
CIPHER.AES_256_GCM,
'key',
{ salt: 'salt', iterations: 128, ivLength: 12 }
);

storage.clear();

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

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

expect(value).toBe('value');
});

test(`Negative: clear() - storage does not exist.`, async () => {
const spy = jest.spyOn(StorageCreator as any, 'create');
spy.mockImplementation(() => {});
Expand Down

0 comments on commit 95a0eba

Please sign in to comment.