Skip to content

Commit

Permalink
added test for onsite public key
Browse files Browse the repository at this point in the history
  • Loading branch information
THeflinKeeper committed Dec 8, 2023
1 parent 89ea521 commit f5fc12a
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 4 deletions.
4 changes: 2 additions & 2 deletions keeperapi/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion keeperapi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"build": "node ./scripts/cleanDistFolder.js && rollup -c && cp src/proto.d.ts dist",
"update-proto:es6": "pbjs -t static-module -w es6 -o src/proto.js ../../keeperapp-protobuf/APIRequest.proto ../../keeperapp-protobuf/AccountSummary.proto ../../keeperapp-protobuf/automator.proto ../../keeperapp-protobuf/breachwatch.proto ../../keeperapp-protobuf/client.proto ../../keeperapp-protobuf/externalservice.proto ../../keeperapp-protobuf/folder.proto ../../keeperapp-protobuf/push.proto ../../keeperapp-protobuf/record.proto ../../keeperapp-protobuf/servicelogger.proto ../../keeperapp-protobuf/ssocloud.proto ../../keeperapp-protobuf/token.proto ../../keeperapp-protobuf/upsell.proto ../../keeperapp-protobuf/SyncDown.proto && pbts -o src/proto.d.ts src/proto.js",
"update-proto:cjs": "pbjs -t json-module -w commonjs -o src/proto.js ../../keeperapp-protobuf/APIRequest.proto ../../keeperapp-protobuf/AccountSummary.proto ../../keeperapp-protobuf/automator.proto ../../keeperapp-protobuf/breachwatch.proto ../../keeperapp-protobuf/client.proto ../../keeperapp-protobuf/externalservice.proto ../../keeperapp-protobuf/folder.proto ../../keeperapp-protobuf/push.proto ../../keeperapp-protobuf/record.proto ../../keeperapp-protobuf/servicelogger.proto ../../keeperapp-protobuf/ssocloud.proto ../../keeperapp-protobuf/token.proto ../../keeperapp-protobuf/upsell.proto ../../keeperapp-protobuf/SyncDown.proto && pbjs -t static-module -w commonjs ../../keeperapp-protobuf/APIRequest.proto ../../keeperapp-protobuf/AccountSummary.proto ../../keeperapp-protobuf/automator.proto ../../keeperapp-protobuf/breachwatch.proto ../../keeperapp-protobuf/client.proto ../../keeperapp-protobuf/externalservice.proto ../../keeperapp-protobuf/folder.proto ../../keeperapp-protobuf/push.proto ../../keeperapp-protobuf/record.proto ../../keeperapp-protobuf/servicelogger.proto ../../keeperapp-protobuf/ssocloud.proto ../../keeperapp-protobuf/token.proto ../../keeperapp-protobuf/upsell.proto ../../keeperapp-protobuf/SyncDown.proto | pbts -o src/proto.d.ts -",
"test:2": "jest",
"test": "jest",
"types": "tsc --watch",
"types:ci": "tsc",
"prepublishOnly": "rollup -c && cp src/proto.d.ts dist",
Expand Down
93 changes: 93 additions & 0 deletions keeperapi/src/__tests__/getOnsitePublicKey.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* @jest-environment jsdom
*/

// @ts-ignore
import crypto from 'crypto'
import { browserPlatform } from '../browser/platform';
import {KeeperEndpoint} from '../endpoint'
import { nodePlatform } from '../node/platform'
import { connectPlatform } from '../platform'
// import NodeRSA from 'node-rsa';

Object.defineProperty(global.self, 'crypto', {
value: {
subtle: crypto.webcrypto.subtle,
getRandomValues: (array: any) => crypto.randomBytes(array.length)
}
})

describe('getOnsitePublicKey', () => {

let endpoint = new KeeperEndpoint({
host: 'testUrl',
deviceConfig: {
deviceName: 'test',
deviceToken: new Uint8Array(),
privateKey: new Uint8Array(),
publicKey: new Uint8Array(),
transmissionKeyId: 1,
},
})

beforeEach(() => {
endpoint = new KeeperEndpoint({
host: 'testUrl',
deviceConfig: {
deviceName: 'test',
deviceToken: new Uint8Array(),
privateKey: new Uint8Array(),
publicKey: new Uint8Array(),
transmissionKeyId: 1,
},
})
})

// NODE PLATFORM
it('(node) should return the rsa public key of the onsite keeper', async () => {
connectPlatform(nodePlatform)
const key = await endpoint.getOnsitePublicKey(false)

checkRSAKey(key)
// should node platform have a different length from browser?
expect(key).toHaveLength(392);
})

// NODE PLATFORM
it('(node) should return the ecc public key of the onsite keeper', async () => {
connectPlatform(nodePlatform)

const key = await endpoint.getOnsitePublicKey(true)
checkECCKey(key)
})

// BROWSER PLATFORM
it('(browser) should return the rsa public key of the onsite keeper', async () => {
connectPlatform(browserPlatform)

const key = await endpoint.getOnsitePublicKey(false)

checkRSAKey(key)
// should browser platform have a different length from node?
expect(key).toHaveLength(360);
})

// BROWSER PLATFORM
it('(browser) should return the ecc public key of the onsite keeper', async () => {
connectPlatform(browserPlatform)

const key = await endpoint.getOnsitePublicKey(true)
checkECCKey(key)
})
})

function checkRSAKey(key:string){
const beginningPart = key.match(/^MIIB/i)
const endingPart = key.match(/IDAQAB$/i)
expect(beginningPart).toBeTruthy();
expect(endingPart).toBeTruthy();
}

function checkECCKey(key:string){
expect(key).toHaveLength(87);
}
2 changes: 1 addition & 1 deletion keeperapi/src/node/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as crypto from "crypto";
import {createECDH, hkdfSync} from "crypto";
import * as https from "https";
import * as FormData from "form-data"
import * as NodeRSA from 'node-rsa';
import NodeRSA from 'node-rsa';
import * as WebSocket from 'faye-websocket'

import {
Expand Down

0 comments on commit f5fc12a

Please sign in to comment.