Skip to content

Commit

Permalink
Changed: test clean-up
Browse files Browse the repository at this point in the history
Signed-off-by: diva.exchange <[email protected]>
  • Loading branch information
diva.exchange committed Aug 6, 2023
1 parent a20c773 commit bb1684a
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 118 deletions.
100 changes: 56 additions & 44 deletions test/lib/i2p-sam-datagram.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import { suite, test, timeout } from '@testdeck/mocha';
import { expect } from 'chai';
import { createDatagram, createRaw, I2pSamDatagram } from '../../lib/index.js';
import { createDatagram, I2pSamDatagram } from '../../lib/index.js';
import crypto from 'crypto';

const SAM_HOST = process.env.SAM_HOST || '172.19.74.11';
Expand All @@ -44,83 +44,93 @@ class TestI2pSamDatagram {
'\n' + crypto.randomFillSync(Buffer.alloc(1000)).toString('base64').substring(0, 1023)
);

console.log('Creating Sender...');
const i2pSender: I2pSamDatagram = (
await createDatagram({
let i2pSender: I2pSamDatagram = {} as I2pSamDatagram;
let i2pRecipient: I2pSamDatagram = {} as I2pSamDatagram;
try {
console.log('Creating Sender...');
i2pSender = await createDatagram({
sam: { host: SAM_HOST, portTCP: SAM_PORT_TCP, portUDP: SAM_PORT_UDP },
listen: {
address: SAM_LISTEN_ADDRESS,
port: SAM_LISTEN_PORT,
hostForward: SAM_LISTEN_FORWARD,
},
})
).on('data', (data: Buffer) => {
expect(data.toString()).to.be.equal(dataToSend.toString());
messageCounterA++;
});
destinationSender = i2pSender.getPublicKey();

console.log('Creating Recipient...');
const i2pRecipient: I2pSamDatagram = (
await createDatagram({
});
i2pSender.on('data', (data: Buffer) => {
expect(data.toString()).to.be.equal(dataToSend.toString());
messageCounterA++;
});

destinationSender = i2pSender.getPublicKey();

console.log('Creating Recipient...');
i2pRecipient = await createDatagram({
sam: { host: SAM_HOST, portTCP: SAM_PORT_TCP, portUDP: SAM_PORT_UDP },
listen: {
address: SAM_LISTEN_ADDRESS,
port: SAM_LISTEN_PORT + 1,
hostForward: SAM_LISTEN_FORWARD,
},
})
).on('data', (data: Buffer) => {
expect(data.toString()).to.be.equal(dataToSend.toString());
messageCounterB++;
});
destinationRecipient = i2pRecipient.getPublicKey();

console.log(Date.now() + ' - start sending data...');
let sentMsgs = 0;
const intervalSender = setInterval(async () => {
i2pSender.send(destinationRecipient, dataToSend);
sentMsgs++;
}, 50);

const intervalRecipient = setInterval(async () => {
i2pRecipient.send(destinationSender, dataToSend);
sentMsgs++;
}, 50);

while (!(messageCounterA >= 10 && messageCounterB >= 10)) {
await TestI2pSamDatagram.wait(100);
}
console.log(Date.now() + ' - total Sent: ' + sentMsgs);
console.log('Arrived: ' + Math.round(((messageCounterA + messageCounterB) / sentMsgs) * 1000) / 10 + '%');
});
i2pRecipient.on('data', (data: Buffer) => {
expect(data.toString()).to.be.equal(dataToSend.toString());
messageCounterB++;
});
destinationRecipient = i2pRecipient.getPublicKey();

console.log(Date.now() + ' - start sending data...');
let sentMsg: number = 0;
const intervalSender: NodeJS.Timer = setInterval(async (): Promise<void> => {
i2pSender.send(destinationRecipient, dataToSend);
sentMsg++;
}, 50);

const intervalRecipient: NodeJS.Timer = setInterval(async (): Promise<void> => {
i2pRecipient.send(destinationSender, dataToSend);
sentMsg++;
}, 50);

clearInterval(intervalSender);
clearInterval(intervalRecipient);
i2pSender.close();
i2pRecipient.close();
while (!(messageCounterA >= 10 && messageCounterB >= 10)) {
await TestI2pSamDatagram.wait(100);
}
console.log(Date.now() + ' - total Sent: ' + sentMsg);
console.log('Arrived: ' + Math.round(((messageCounterA + messageCounterB) / sentMsg) * 1000) / 10 + '%');

clearInterval(intervalSender);
clearInterval(intervalRecipient);
} catch (error: any) {
console.debug(error.toString());
} finally {
Object.keys(i2pSender).length && i2pSender.close();
Object.keys(i2pRecipient).length && i2pRecipient.close();
}

expect(messageCounterA).not.to.be.equal(0);
expect(messageCounterB).not.to.be.equal(0);
}

@test
async failTimeout(): Promise<void> {
let datagram: I2pSamDatagram = {} as I2pSamDatagram;
// timeout error
try {
await createDatagram({
datagram = await createDatagram({
sam: { host: SAM_HOST, portTCP: SAM_PORT_TCP, portUDP: SAM_PORT_UDP, timeout: 1 },
});
expect(false).to.be.true;
} catch (error: any) {
expect(error.toString()).contains('timeout');
} finally {
Object.keys(datagram).length && datagram.close();
}
}

@test
async failKeys(): Promise<void> {
let datagram: I2pSamDatagram = {} as I2pSamDatagram;
// public key / private key issues
try {
await createDatagram({
datagram = await createDatagram({
sam: {
host: SAM_HOST,
portTCP: SAM_PORT_TCP,
Expand All @@ -131,6 +141,8 @@ class TestI2pSamDatagram {
expect(false).to.be.true;
} catch (error: any) {
expect(error.toString()).contains('SESSION failed').contains('RESULT=INVALID_KEY');
} finally {
Object.keys(datagram).length && datagram.close();
}
}

Expand Down
157 changes: 83 additions & 74 deletions test/lib/i2p-sam-raw.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,122 +43,131 @@ class TestI2pSamRaw {
// 16K data
const dataToSend: Buffer = crypto.randomFillSync(Buffer.alloc(16 * 1024));

console.log('Creating Sender...');
const i2pSender: I2pSamRaw = (
await createRaw({
let i2pSender: I2pSamRaw = {} as I2pSamRaw;
let i2pRecipient: I2pSamRaw = {} as I2pSamRaw;

try {
console.log('Creating Sender...');
i2pSender = await createRaw({
session: { options: 'inbound.lengthVariance=2 outbound.lengthVariance=2 shouldBundleReplyInfo=false' },
sam: { host: SAM_HOST, portTCP: SAM_PORT_TCP, portUDP: SAM_PORT_UDP },
listen: {
address: SAM_LISTEN_ADDRESS,
port: SAM_LISTEN_PORT,
hostForward: SAM_LISTEN_FORWARD,
},
})
).on('data', (data: Buffer) => {
expect(data.toString('base64')).to.be.equal(dataToSend.toString('base64'));
messageCounterA++;
});
destinationSender = i2pSender.getPublicKey();

console.log('Creating Recipient...');
const i2pRecipient: I2pSamRaw = (
await createRaw({
});
i2pSender.on('data', (data: Buffer) => {
expect(data.toString('base64')).to.be.equal(dataToSend.toString('base64'));
messageCounterA++;
});

destinationSender = i2pSender.getPublicKey();

console.log('Creating Recipient...');
i2pRecipient = await createRaw({
session: { options: 'inbound.lengthVariance=2 outbound.lengthVariance=2 shouldBundleReplyInfo=false' },
sam: { host: SAM_HOST, portTCP: SAM_PORT_TCP, portUDP: SAM_PORT_UDP },
listen: {
address: SAM_LISTEN_ADDRESS,
port: SAM_LISTEN_PORT + 1,
hostForward: SAM_LISTEN_FORWARD,
},
})
).on('data', (data: Buffer): void => {
expect(data.toString('base64')).to.be.equal(dataToSend.toString('base64'));
messageCounterB++;
});
destinationRecipient = i2pRecipient.getPublicKey();

console.log(Date.now() + ' - send udp to diva.i2p (provoking a lookup)');
i2pSender.send('diva.i2p', dataToSend);

console.log(Date.now() + ' - start sending data...');
let sentMsgs: number = 0;
const intervalSender: NodeJS.Timer = setInterval(async (): Promise<void> => {
i2pSender.send(destinationRecipient, dataToSend);
sentMsgs++;
}, 50);

const intervalRecipient: NodeJS.Timer = setInterval(async (): Promise<void> => {
i2pRecipient.send(destinationSender, dataToSend);
sentMsgs++;
}, 50);

while (!(messageCounterA >= 10 && messageCounterB >= 10)) {
await TestI2pSamRaw.wait(100);
}
console.log(Date.now() + ' - total Sent: ' + sentMsgs);
console.log('Arrived: ' + Math.round(((messageCounterA + messageCounterB) / sentMsgs) * 1000) / 10 + '%');
});
i2pRecipient.on('data', (data: Buffer): void => {
expect(data.toString('base64')).to.be.equal(dataToSend.toString('base64'));
messageCounterB++;
});

destinationRecipient = i2pRecipient.getPublicKey();

console.log(Date.now() + ' - send udp to diva.i2p (provoking a lookup)');
i2pSender.send('diva.i2p', dataToSend);

console.log(Date.now() + ' - start sending data...');
let sentMsg: number = 0;
const intervalSender: NodeJS.Timer = setInterval(async (): Promise<void> => {
i2pSender.send(destinationRecipient, dataToSend);
sentMsg++;
}, 50);

clearInterval(intervalSender);
clearInterval(intervalRecipient);
i2pSender.close();
i2pRecipient.close();
const intervalRecipient: NodeJS.Timer = setInterval(async (): Promise<void> => {
i2pRecipient.send(destinationSender, dataToSend);
sentMsg++;
}, 50);

while (!(messageCounterA >= 10 && messageCounterB >= 10)) {
await TestI2pSamRaw.wait(100);
}
console.log(Date.now() + ' - total Sent: ' + sentMsg);
console.log('Arrived: ' + Math.round(((messageCounterA + messageCounterB) / sentMsg) * 1000) / 10 + '%');

clearInterval(intervalSender);
clearInterval(intervalRecipient);
} catch (error: any) {
console.debug(error.toString());
} finally {
Object.keys(i2pSender).length && i2pSender.close();
Object.keys(i2pRecipient).length && i2pRecipient.close();
}

expect(messageCounterA).not.to.be.equal(0);
expect(messageCounterB).not.to.be.equal(0);
}

@test
@timeout(90000)
async fail(): Promise<void> {
async failEmptyMessage(): Promise<void> {
const config: Configuration = { sam: { host: SAM_HOST, portTCP: SAM_PORT_TCP } };
const dest: string = await lookup(config, 'diva.i2p');
const sam: I2pSamRaw = await createRaw(config);

let e: string = '';
await new Promise((resolve) => {
sam.removeAllListeners();
sam.once('error', (error: any) => {
e = error.toString();
resolve(true);
});
sam.send(dest, Buffer.from(''));
});
expect(e).contains('invalid message length');

await new Promise((resolve) => {
sam.removeAllListeners();
sam.once('error', (error: any) => {
e = error.toString();
resolve(true);
});
sam.send(dest, Buffer.from(crypto.randomFillSync(Buffer.alloc(65 * 1024))));
});
expect(e).contains('invalid message length');
let raw: I2pSamRaw = {} as I2pSamRaw;
try {
raw = await createRaw(config);
raw.send(dest, Buffer.from(''));
} catch (error: any) {
expect(error.toString()).contains('invalid message length');
} finally {
Object.keys(raw).length && raw.close();
}
}

sam.close();
@test
@timeout(90000)
async failTooLargeMessage(): Promise<void> {
const config: Configuration = { sam: { host: SAM_HOST, portTCP: SAM_PORT_TCP } };
const dest: string = await lookup(config, 'diva.i2p');
let raw: I2pSamRaw = {} as I2pSamRaw;
try {
raw = await createRaw(config);
raw.send(dest, Buffer.from(crypto.randomFillSync(Buffer.alloc(65 * 1024))));
} catch (error: any) {
expect(error.toString()).contains('invalid message length');
} finally {
Object.keys(raw).length && raw.close();
}
}

@test
async failTimeout(): Promise<void> {
let f: I2pSamRaw = {} as I2pSamRaw;
let raw: I2pSamRaw = {} as I2pSamRaw;
// timeout error
try {
f = await createRaw({
raw = await createRaw({
sam: { host: SAM_HOST, portTCP: SAM_PORT_TCP, timeout: 1 },
});
expect(false).to.be.true;
} catch (error: any) {
expect(error.toString()).contains('timeout');
} finally {
f && f.close();
Object.keys(raw).length && raw.close();
}
}

@test
async failListen(): Promise<void> {
let f: I2pSamRaw = {} as I2pSamRaw;
let raw: I2pSamRaw = {} as I2pSamRaw;
try {
f = await createRaw({
raw = await createRaw({
sam: { host: SAM_HOST, portTCP: SAM_PORT_TCP },
listen: {
address: SAM_HOST,
Expand All @@ -169,7 +178,7 @@ class TestI2pSamRaw {
} catch (error: any) {
expect(error.toString()).contains('EADDRNOTAVAIL');
} finally {
f && f.close();
Object.keys(raw).length && raw.close();
}
}

Expand Down

0 comments on commit bb1684a

Please sign in to comment.