mirror of
https://github.com/fluencelabs/js-libp2p-noise
synced 2025-04-25 08:42:26 +00:00
Merge pull request #24 from NodeFactoryIo/mpetrunic/browser-tests
Browser tests
This commit is contained in:
commit
135b5445ca
@ -8,5 +8,5 @@ install:
|
||||
script:
|
||||
set -e;
|
||||
yarn run lint;
|
||||
yarn run test
|
||||
yarn run build;
|
||||
yarn run test
|
||||
|
26
karma.conf.js
Normal file
26
karma.conf.js
Normal file
@ -0,0 +1,26 @@
|
||||
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
||||
const webpackConfig = require("./webpack.config");
|
||||
|
||||
module.exports = function(config) {
|
||||
config.set({
|
||||
|
||||
basePath: "",
|
||||
frameworks: ["mocha", "chai"],
|
||||
files: ["test/**/*.test.ts"],
|
||||
exclude: [],
|
||||
preprocessors: {
|
||||
"test/**/*.ts": ["webpack"]
|
||||
},
|
||||
webpack: {
|
||||
mode: "production",
|
||||
node: webpackConfig.node,
|
||||
module: webpackConfig.module,
|
||||
resolve: webpackConfig.resolve
|
||||
},
|
||||
reporters: ["spec"],
|
||||
|
||||
browsers: ["ChromeHeadless"],
|
||||
|
||||
singleRun: true
|
||||
});
|
||||
};
|
17
package.json
17
package.json
@ -16,7 +16,9 @@
|
||||
"check-types": "tsc --incremental --noEmit",
|
||||
"lint": "eslint --ext .ts src/",
|
||||
"pretest": "yarn check-types",
|
||||
"test": "DEBUG=libp2p:noise mocha -r ./babel-register.js \"test/**/*.test.ts\""
|
||||
"test": "yarn run test:node && yarn run test:web",
|
||||
"test:node": "mocha -r ./babel-register.js \"test/**/*.test.ts\"",
|
||||
"test:web": "karma start"
|
||||
},
|
||||
"babel": {
|
||||
"presets": [
|
||||
@ -51,9 +53,18 @@
|
||||
"bn.js-typings": "^1.0.1",
|
||||
"chai": "^4.2.0",
|
||||
"eslint": "^6.6.0",
|
||||
"karma": "^4.4.1",
|
||||
"karma-chai": "^0.1.0",
|
||||
"karma-chrome-launcher": "^3.1.0",
|
||||
"karma-cli": "^2.0.0",
|
||||
"karma-mocha": "^1.3.0",
|
||||
"karma-spec-reporter": "^0.0.32",
|
||||
"karma-webpack": "^4.0.2",
|
||||
"mocha": "^6.2.2",
|
||||
"sinon": "^8.1.0",
|
||||
"typescript": "^3.6.4"
|
||||
"ts-loader": "^6.2.1",
|
||||
"typescript": "^3.6.4",
|
||||
"webpack": "^4.41.5"
|
||||
},
|
||||
"dependencies": {
|
||||
"bcrypto": "^4.2.3",
|
||||
@ -67,6 +78,6 @@
|
||||
"it-pipe": "^1.1.0",
|
||||
"libp2p-crypto": "^0.17.1",
|
||||
"peer-id": "^0.13.5",
|
||||
"protobufjs": "~6.8.8"
|
||||
"protobufjs": "6.8.8"
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +0,0 @@
|
||||
syntax = "proto3";
|
||||
package pb;
|
||||
|
||||
message NoiseHandshakePayload {
|
||||
bytes identity_key = 1;
|
||||
bytes identity_sig = 2;
|
||||
bytes data = 3;
|
||||
}
|
20
src/proto/payload.json
Normal file
20
src/proto/payload.json
Normal file
@ -0,0 +1,20 @@
|
||||
{
|
||||
"nested": {
|
||||
"NoiseHandshakePayload": {
|
||||
"fields": {
|
||||
"identityKey": {
|
||||
"type": "bytes",
|
||||
"id": 1
|
||||
},
|
||||
"identitySig": {
|
||||
"type": "bytes",
|
||||
"id": 2
|
||||
},
|
||||
"data": {
|
||||
"type": "bytes",
|
||||
"id": 3
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
16
src/utils.ts
16
src/utils.ts
@ -3,14 +3,14 @@ import protobuf from "protobufjs";
|
||||
import { Buffer } from "buffer";
|
||||
import PeerId from "peer-id";
|
||||
import * as crypto from 'libp2p-crypto';
|
||||
|
||||
import { KeyPair } from "./@types/libp2p";
|
||||
import {bytes, bytes32} from "./@types/basic";
|
||||
import {Hkdf} from "./@types/handshake";
|
||||
import payloadProto from "./proto/payload.json";
|
||||
|
||||
export async function loadPayloadProto () {
|
||||
const payloadProtoBuf = await protobuf.load("protos/payload.proto");
|
||||
return payloadProtoBuf.lookupType("pb.NoiseHandshakePayload");
|
||||
const payloadProtoBuf = await protobuf.Root.fromJSON(payloadProto);
|
||||
return payloadProtoBuf.lookupType("NoiseHandshakePayload");
|
||||
}
|
||||
|
||||
export function generateKeypair(): KeyPair {
|
||||
@ -74,9 +74,15 @@ export async function verifySignedPayload(noiseStaticKey: bytes, plaintext: byte
|
||||
let receivedPayload;
|
||||
try {
|
||||
const NoiseHandshakePayload = await loadPayloadProto();
|
||||
receivedPayload = NoiseHandshakePayload.toObject(NoiseHandshakePayload.decode(plaintext));
|
||||
receivedPayload = NoiseHandshakePayload.toObject(
|
||||
NoiseHandshakePayload.decode(plaintext)
|
||||
);
|
||||
//temporary fix until protobufsjs conversion options starts working
|
||||
//by default it ends up as Uint8Array
|
||||
receivedPayload.identityKey = Buffer.from(receivedPayload.identityKey);
|
||||
receivedPayload.identitySig = Buffer.from(receivedPayload.identitySig);
|
||||
} catch (e) {
|
||||
throw new Error("Failed to decode received payload.");
|
||||
throw new Error("Failed to decode received payload. Reason: " + e.message);
|
||||
}
|
||||
|
||||
if (!(await isValidPeerId(peerId, receivedPayload.identityKey)) ) {
|
||||
|
@ -5,7 +5,7 @@ import {createHandshakePayload, generateKeypair, getHandshakePayload} from "../.
|
||||
import {assert, expect} from "chai";
|
||||
import {generateEd25519Keys} from "../utils";
|
||||
|
||||
describe("Index", () => {
|
||||
describe("IK handshake", () => {
|
||||
const prologue = Buffer.alloc(0);
|
||||
|
||||
it("Test complete IK handshake", async () => {
|
||||
@ -60,8 +60,8 @@ describe("Index", () => {
|
||||
assert(initiatorSession.cs2.k.equals(responderSession.cs2.k));
|
||||
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
assert(false, e.message);
|
||||
console.error(e);
|
||||
return assert(false, e.message);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
@ -6,7 +6,7 @@ import { KeyPair } from "../../src/@types/libp2p";
|
||||
import { generateEd25519Keys } from "../utils";
|
||||
import {createHandshakePayload, generateKeypair, getHandshakePayload, getHkdf} from "../../src/utils";
|
||||
|
||||
describe("Index", () => {
|
||||
describe("XX Handshake", () => {
|
||||
const prologue = Buffer.alloc(0);
|
||||
|
||||
it("Test creating new XX session", async () => {
|
||||
@ -23,7 +23,6 @@ describe("Index", () => {
|
||||
});
|
||||
|
||||
it("Test get HKDF", async () => {
|
||||
const xx = new XX();
|
||||
const ckBytes = Buffer.from('4e6f6973655f58585f32353531395f58436861436861506f6c795f53484132353600000000000000000000000000000000000000000000000000000000000000', 'hex');
|
||||
const ikm = Buffer.from('a3eae50ea37a47e8a7aa0c7cd8e16528670536dcd538cebfd724fb68ce44f1910ad898860666227d4e8dd50d22a9a64d1c0a6f47ace092510161e9e442953da3', 'hex');
|
||||
const ck = Buffer.alloc(32);
|
||||
@ -67,8 +66,7 @@ describe("Index", () => {
|
||||
expect(messageBuffer.ne.length).not.equal(0);
|
||||
|
||||
// responder receives message
|
||||
const plaintext = xx.recvMessage(nsResp, messageBuffer);
|
||||
console.log("Stage 0 responder payload: ", plaintext);
|
||||
xx.recvMessage(nsResp, messageBuffer);
|
||||
|
||||
/* STAGE 1 */
|
||||
|
||||
@ -84,8 +82,7 @@ describe("Index", () => {
|
||||
expect(messageBuffer2.ns.length).not.equal(0);
|
||||
|
||||
// initiator receive payload
|
||||
const plaintext2 = xx.recvMessage(nsInit, messageBuffer2);
|
||||
console.log("Stage 1 responder payload: ", plaintext2);
|
||||
xx.recvMessage(nsInit, messageBuffer2);
|
||||
|
||||
/* STAGE 2 */
|
||||
|
||||
@ -93,8 +90,7 @@ describe("Index", () => {
|
||||
const messageBuffer3 = xx.sendMessage(nsInit, Buffer.alloc(0));
|
||||
|
||||
// responder receive message
|
||||
const plaintext3 = xx.recvMessage(nsResp, messageBuffer3);
|
||||
console.log("Stage 2 responder payload: ", plaintext3);
|
||||
xx.recvMessage(nsResp, messageBuffer3);
|
||||
|
||||
assert(nsInit.cs1.k.equals(nsResp.cs1.k));
|
||||
assert(nsInit.cs2.k.equals(nsResp.cs2.k));
|
||||
|
@ -3,6 +3,7 @@
|
||||
"target": "es6",
|
||||
"module": "commonjs",
|
||||
"strict": true,
|
||||
"resolveJsonModule": true,
|
||||
"esModuleInterop": true,
|
||||
"noImplicitAny": false,
|
||||
"typeRoots": [
|
||||
|
19
webpack.config.js
Normal file
19
webpack.config.js
Normal file
@ -0,0 +1,19 @@
|
||||
module.exports = {
|
||||
entry: "./src/index.ts",
|
||||
mode: "production",
|
||||
output: {
|
||||
filename: "dist/bundle.js"
|
||||
},
|
||||
node: {
|
||||
fs: "empty"
|
||||
},
|
||||
resolve: {
|
||||
extensions: [".ts", ".js"],
|
||||
},
|
||||
module: {
|
||||
rules: [
|
||||
{test: /\.ts$/, use: {loader: "ts-loader", options: {transpileOnly: true}}}
|
||||
],
|
||||
},
|
||||
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user