-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from AElfProject/feature/unit-test
Feature/unit test
- Loading branch information
Showing
34 changed files
with
2,051 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
name: Coverage Diff | ||
|
||
on: | ||
push: | ||
branches: | ||
- master | ||
pull_request: {} | ||
|
||
jobs: | ||
test: | ||
name: Coverage Diff | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
- uses: actions/setup-node@v2 | ||
with: | ||
node-version: 20 | ||
cache: yarn | ||
- run: yarn install | ||
- run: yarn run test:coverage | ||
- name: Coverage Diff | ||
uses: greatwizard/coverage-diff-action@v1 | ||
with: | ||
github-token: ${{ secrets.GITHUB_TOKEN }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import HKDF from '../../../src/command/dappServer/HKDF'; | ||
|
||
describe('HKDF', () => { | ||
const hash = 'sha256'; | ||
const random = '52695e07c0c545e8a279a71b1f47b9b7'; | ||
const salt = Buffer.from(random, 'hex'); | ||
console.log(salt, 'salt'); | ||
const initialKey = '667a8937f4da939b93e716211e83f787741be3fe0c938bea9e333d37c779b5'; | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
test('should initialize with correct values', () => { | ||
const hkdf = new HKDF(hash, salt, initialKey); | ||
expect(hkdf.hashMethod).toBe(hash); | ||
expect(hkdf.hashLength).toBe(32); | ||
expect(hkdf.salt).toBe(salt); | ||
expect(hkdf.initialKey).toBe(initialKey); | ||
console.log(hkdf.prk, 'hkdf.prk'); | ||
expect(hkdf.prk.toString('hex')).toEqual('6269eb093d3ed47cd698158c8f404b6380cd3222f3889323ea7814ea341456f2'); | ||
}); | ||
test('should throw an error for unsupported hash method', () => { | ||
expect(() => new HKDF('sha1', Buffer.from('salt'), 'initialKey')).toThrow('not supported hash method'); | ||
}); | ||
test('should expand correctly', () => { | ||
const hkdf = new HKDF(hash, salt, initialKey); | ||
const result = hkdf.expand(); | ||
expect(result.toString('hex')).toBe('50649277a8ec2090de2c15af4aab197a7baa3c09accb755d3fafa829c370ba62'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* eslint-disable max-len */ | ||
import elliptic from 'elliptic'; | ||
import Encrypt from '../../../src/command/dappServer/encrypt'; | ||
import { randomId } from '../../../src/utils/utils.js'; | ||
|
||
jest.mock('../../../src/utils/utils.js', () => { | ||
return { | ||
randomId: () => '1ef39a09ce4f415da335dcb408989116' | ||
}; | ||
}); | ||
|
||
describe('Encrypt', () => { | ||
const algorithm = 'curve25519'; | ||
const remotePublicKey = '75d5e81323eecc4e887ef0934a52d8de1a2785ca04f4a4e9a39359c4bfd8cc9d'; | ||
const random = 'e44ab14618724a80be03f4565e7ed668'; | ||
const ecInstance = new Encrypt(algorithm, remotePublicKey, random); | ||
const data = | ||
'JTdCJTIyY29kZSUyMiUzQTAlMkMlMjJtc2clMjIlM0ElMjJzdWNjZXNzJTIyJTJDJTIyZXJyb3IlMjIlM0ElNUIlNUQlMkMlMjJkYXRhJTIyJTNBJTdCJTIyYWNjb3VudHMlMjIlM0ElNUIlN0IlMjJuYW1lJTIyJTNBJTIyYWVsZi1jb21tYW5kJTIyJTJDJTIyYWRkcmVzcyUyMiUzQSUyMkd5UVg2dDE4a3B3YUQ5WEhYZTFUb0t4Zm92OG1TZVRMRTlxOU53VUFlVEU4dFVMWmslMjIlMkMlMjJwdWJsaWNLZXklMjIlM0ElMjIwNDcwM2JiZTk1ZTk4NmM5ZDkwMWYyOGVkZDYwOTc1YTdhNmMzYjJkY2U0MWRmZWMyZTc5ODNkMjkzYzYwMGU4MjQ5NjQyYTNkYTM3OWM0MTk0YTZkNjJiZDg5YWZlNjc1M2U4MWFjZmMyYjZiYmYzYjQwNzM2ZWUwOTQ5MTAyMDcxJTIyJTdEJTVEJTJDJTIyY2hhaW5zJTIyJTNBJTVCJTdCJTIydXJsJTIyJTNBJTIyaHR0cHMlM0ElMkYlMkZ0ZHZ3LXRlc3Qtbm9kZS5hZWxmLmlvJTJGJTIyJTJDJTIyaXNNYWluQ2hhaW4lMjIlM0F0cnVlJTJDJTIyY2hhaW5JZCUyMiUzQSUyMkFFTEYlMjIlN0QlNUQlN0QlN0Q='; | ||
const encrypted = | ||
'U5E3LMWOVAintuE4zJf+8O4XSHgAtZTeQX831sc53r9yZJGnh6fiwNR3tF8zCORnwtVUaYh+pnzPgxeQz8b1tKEUhc/VYoy2dtqMubMDq+WUeF5tFnFhv6kuhHkMSDYQnpwOpwSvzJ4kWr+cxdHGOg+qoWe1nPujQlc/gQ/7Z4MeEHWXDagtKXzKGfE/rYJhnlAN+K1xHhq//tS4g4izEuHe2J2RK5xYa91e5p1qzLTEQyqax2q1pyYFPbaRyrP/6yAlUPOkdyuIGdMXwEp7BdfPXF3xPemaj6w+WgOkmAUcKk35blQvNLuJAWux6ZhLl+P+w6sfinT3Mk1ymrz2SEdThjB/LEPbbBlx+in80y0S8bSgUpsXbW1mfMPX3svDks0cK3Thjf/o+sGed5Ej4cFMFbgeCI4JdDnKeG7RWPWN9lL77kzPq0q60zXr/70jyRElktzSWzGfcOEr+TQiEx3MfiRHgAoqz5glc/ml7VlUP9ouLvRQfmrWcYIsbkf9WnBac/G68rSGOEqR/qbugsQCclO+V6yJt3o4NL94Nbn8V/+gQo430zdefkljGum84c0e2vVYxXyRbrFIAROJeO4IRiVWAaLrnFc0h/Shtt8EupaWGYJps+aYODUpfulJjDm6pZoDE+bp5h02XV0C/akhIUclLveNJc14qYc7tGpSRBDNXC/z6PfZgPlV3OL+'; | ||
const iv = '1ef39a09ce4f415da335dcb408989116'; | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
test('should initialize correctly', () => { | ||
expect(ecInstance).toHaveProperty('keyPair'); | ||
expect(ecInstance).toHaveProperty('cipher'); | ||
expect(ecInstance).toHaveProperty('remoteKeyPair'); | ||
expect(ecInstance).toHaveProperty('sharedKey'); | ||
expect(ecInstance).toHaveProperty('derivedKey'); | ||
}); | ||
|
||
test('should ecInstance data correctly', () => { | ||
// mock | ||
ecInstance.sharedKey = Buffer.from('7f8e6db3591d76846d8b6ffefe5e0a19f85feee2cb5d41131dac2b9a668b41ca', 'hex'); | ||
ecInstance.derivedKey = Buffer.from('0369788fa050c720131722efb25281f444857db4b833ea89ede08a8fdf6117c0', 'hex'); | ||
const result = ecInstance.encrypt(data); | ||
expect(result).toEqual({ | ||
encryptedResult: encrypted, | ||
iv | ||
}); | ||
}); | ||
test('should decrypt data correctly', () => { | ||
const decrypt = ecInstance.decrypt(encrypted, iv); | ||
expect(decrypt).toBe(data); | ||
}); | ||
test('should return correct public key', () => { | ||
const result = ecInstance.getPublicKey(); | ||
expect(result.length).toBe(64); | ||
}); | ||
|
||
test(`should initialize with not default algorithm`, () => { | ||
// another algorithm | ||
const ecInstanceNotDefault = new Encrypt( | ||
'secp256k1', | ||
'04695fb2e8ce837d5b9e79df046dd1947a558b884165c8f83a9e9b01e47a37135fc4ff42256e4a79f25f740a840b58f47f79a3bf934857c7397e545163cddf663e', | ||
'beae60451b554891bb8967d0c2bdaa4e' | ||
); | ||
expect(ecInstance).toHaveProperty('keyPair'); | ||
expect(ecInstance).toHaveProperty('cipher'); | ||
expect(ecInstance).toHaveProperty('remoteKeyPair'); | ||
expect(ecInstance).toHaveProperty('sharedKey'); | ||
expect(ecInstance).toHaveProperty('derivedKey'); | ||
}); | ||
}); |
Oops, something went wrong.