Skip to content

Commit

Permalink
#342 multiple connections test done
Browse files Browse the repository at this point in the history
  • Loading branch information
tegefaulkes committed Mar 1, 2022
1 parent ba2d74a commit e95a4ec
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 67 deletions.
8 changes: 1 addition & 7 deletions src/agent/service/echo.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
import type * as grpc from '@grpc/grpc-js';
import type { ConnectionInfoGet } from 'agent/types';
import * as utilsPB from '../../proto/js/polykey/v1/utils/utils_pb';
import * as nodesUtils from '../../nodes/utils';

function echo({ connectionInfoGet }: { connectionInfoGet: ConnectionInfoGet }) {
return async (
call: grpc.ServerUnaryCall<utilsPB.EchoMessage, utilsPB.EchoMessage>,
callback: grpc.sendUnaryData<utilsPB.EchoMessage>,
): Promise<void> => {
const connectionInfo = connectionInfoGet(call)!;
console.log(
connectionInfo.egressPort,
connectionInfo.ingressPort,
nodesUtils.encodeNodeId(connectionInfo.nodeId),
);
connectionInfoGet(call);
const response = new utilsPB.EchoMessage();
response.setChallenge(call.request.getChallenge());
callback(null, response);
Expand Down
131 changes: 71 additions & 60 deletions tests/agent/GRPCClientAgent.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Host, Port, TLSConfig } from '@/network/types';
import type * as grpc from '@grpc/grpc-js';
import type { NodeId } from '@/nodes/types';
import fs from 'fs';
import path from 'path';
import os from 'os';
Expand All @@ -21,25 +22,18 @@ import * as utilsPB from '@/proto/js/polykey/v1/utils/utils_pb';
import * as agentErrors from '@/agent/errors';
import * as keysUtils from '@/keys/utils';
import * as testAgentUtils from './utils';
import * as testUtils from '../utils';
import * as nodesUtils from '@/nodes/utils';

describe(GRPCClientAgent.name, () => {
const host = '127.0.0.1' as Host;
const password = 'password';
const logger = new Logger(`${GRPCClientAgent.name} test`, LogLevel.WARN, [
new StreamHandler(),
]);
let mockedGenerateKeyPair: jest.SpyInstance;
let mockedGenerateDeterministicKeyPair: jest.SpyInstance;
beforeAll(async () => {
const globalKeyPair = await testUtils.setupGlobalKeypair();
mockedGenerateKeyPair = jest
.spyOn(keysUtils, 'generateKeyPair')
.mockResolvedValue(globalKeyPair);
mockedGenerateDeterministicKeyPair = jest
.spyOn(keysUtils, 'generateDeterministicKeyPair')
.mockResolvedValue(globalKeyPair);
.mockImplementation((bits, _) => keysUtils.generateKeyPair(bits));
});
afterAll(async () => {
mockedGenerateDeterministicKeyPair.mockRestore();
Expand Down Expand Up @@ -221,87 +215,97 @@ describe(GRPCClientAgent.name, () => {
expect(client.secured).toBeFalsy();
});
describe('With connection through proxies', () => {
const logger = new Logger(`${GRPCClientAgent.name} test`, LogLevel.WARN, [
new StreamHandler(),
]);
const localHost = '127.0.0.1' as Host;

let clientWithProxies1: GRPCClientAgent;
let clientFwdProxy1: ForwardProxy;
let clientKeyManager1: KeyManager;
let nodeId1: NodeId;

let clientWithProxies2: GRPCClientAgent;
let clientFwdProxy2: ForwardProxy;
let clientKeyManager2: KeyManager;
let nodeId2: NodeId;

beforeEach(async () => {
mockedGenerateDeterministicKeyPair
.mockClear()
.mockImplementation((bits, _) => {
return keysUtils.generateKeyPair(bits);
});

dataDir = await fs.promises.mkdtemp(
path.join(os.tmpdir(), 'polykey-test-'),
);
// Setting up clients
clientFwdProxy1 = new ForwardProxy({
authToken: 'auth',
logger,
});
clientFwdProxy2 = new ForwardProxy({
authToken: 'auth',
logger,
});

clientKeyManager1 = await KeyManager.createKeyManager({
keysPath: path.join(dataDir, 'clientKeys1'),
password: 'password',
logger,
});
nodeId1 = clientKeyManager1.getNodeId();
await clientFwdProxy1.start({
tlsConfig: {
keyPrivatePem: clientKeyManager1.getRootKeyPairPem().privateKey,
certChainPem: await clientKeyManager1.getRootCertChainPem(),
},
egressHost: localHost,
proxyHost: localHost,
});
clientWithProxies1 = await GRPCClientAgent.createGRPCClientAgent({
host: localHost,
nodeId: keyManager.getNodeId(),
port: revProxy.getIngressPort(),
proxyConfig: {
host: clientFwdProxy1.getProxyHost(),
port: clientFwdProxy1.getProxyPort(),
authToken: clientFwdProxy1.authToken,
},
timeout: 5000,
logger,
});

clientFwdProxy2 = new ForwardProxy({
authToken: 'auth',
logger,
});
clientKeyManager2 = await KeyManager.createKeyManager({
keysPath: path.join(dataDir, 'clientKeys2'),
password: 'password',
logger,
});
const clientTlsConfig1: TLSConfig = {
keyPrivatePem: clientKeyManager1.getRootKeyPairPem().privateKey,
certChainPem: await clientKeyManager1.getRootCertChainPem(),
};
const clientTlsConfig2: TLSConfig = {
keyPrivatePem: clientKeyManager2.getRootKeyPairPem().privateKey,
certChainPem: await clientKeyManager2.getRootCertChainPem(),
};
await clientFwdProxy1.start({
tlsConfig: clientTlsConfig1,
egressHost: host,
proxyHost: host,
});
nodeId2 = clientKeyManager2.getNodeId();
await clientFwdProxy2.start({
tlsConfig: clientTlsConfig2,
egressHost: host,
proxyHost: host,
});
clientWithProxies1 = await testAgentUtils.openTestAgentClient(
revProxy.getIngressPort(),
clientKeyManager1.getNodeId(),
{
host: clientFwdProxy1.getProxyHost(),
port: clientFwdProxy1.getProxyPort(),
authToken: clientFwdProxy1.authToken,
tlsConfig: {
keyPrivatePem: clientKeyManager2.getRootKeyPairPem().privateKey,
certChainPem: await clientKeyManager2.getRootCertChainPem(),
},
);
clientWithProxies2 = await testAgentUtils.openTestAgentClient(
revProxy.getIngressPort(),
clientKeyManager2.getNodeId(),
{
egressHost: localHost,
proxyHost: localHost,
});
clientWithProxies2 = await GRPCClientAgent.createGRPCClientAgent({
host: '127.0.0.1' as Host,
logger,
nodeId: keyManager.getNodeId(),
port: revProxy.getIngressPort(),
proxyConfig: {
host: clientFwdProxy2.getProxyHost(),
port: clientFwdProxy2.getProxyPort(),
authToken: clientFwdProxy2.authToken,
},
);
console.log(nodesUtils.encodeNodeId(clientKeyManager1.getNodeId()), nodesUtils.encodeNodeId(clientKeyManager2.getNodeId()))
});
timeout: 5000,
});
}, 26000);
afterEach(async () => {
await testAgentUtils.closeTestAgentClient(clientWithProxies1);
await clientFwdProxy1.stop();
await clientKeyManager1.stop();
await testAgentUtils.closeTestAgentClient(clientWithProxies2);
await clientFwdProxy2.stop();
await clientKeyManager2.stop();
});
test('connectionInfoGetter is called and returns the expected information', async () => {
}, 25000);
test('connectionInfoGetter returns correct information for each connection', async () => {
// We can't directly spy on the connectionInfoGetter result
// but we can check that it called `getConnectionInfoByProxy` properly
const getConnectionInfoByProxySpy = jest.spyOn(
Expand All @@ -311,12 +315,19 @@ describe(GRPCClientAgent.name, () => {
await clientWithProxies1.echo(new utilsPB.EchoMessage());
await clientWithProxies2.echo(new utilsPB.EchoMessage());
// It should've returned the expected information
const returnedInfo = getConnectionInfoByProxySpy.mock.results[0].value;
expect(returnedInfo.ingressPort).toEqual(revProxy.getIngressPort());
expect(returnedInfo.ingressHost).toEqual(host);
expect(returnedInfo.egressPort).toEqual(clientFwdProxy1.getEgressPort());
expect(returnedInfo.egressHost).toEqual(host);
expect(returnedInfo.nodeId).toStrictEqual(clientKeyManager1.getNodeId());
});
const returnedInfo1 = getConnectionInfoByProxySpy.mock.results[0].value;
expect(returnedInfo1.ingressPort).toEqual(revProxy.getIngressPort());
expect(returnedInfo1.ingressHost).toEqual(localHost);
expect(returnedInfo1.egressPort).toEqual(clientFwdProxy1.getEgressPort());
expect(returnedInfo1.egressHost).toEqual(localHost);
expect(returnedInfo1.nodeId).toStrictEqual(nodeId1);
// Checking second call
const returnedInfo2 = getConnectionInfoByProxySpy.mock.results[1].value;
expect(returnedInfo2.ingressPort).toEqual(revProxy.getIngressPort());
expect(returnedInfo2.ingressHost).toEqual(localHost);
expect(returnedInfo2.egressPort).toEqual(clientFwdProxy2.getEgressPort());
expect(returnedInfo2.egressHost).toEqual(localHost);
expect(returnedInfo2.nodeId).toStrictEqual(nodeId2);
}, 100000);
});
});

0 comments on commit e95a4ec

Please sign in to comment.