init commit

This commit is contained in:
rainbownapkin 2021-12-06 19:56:40 -05:00
parent ae639426d0
commit 7a491681cc
257 changed files with 95524 additions and 80 deletions

53
test/io/globalban.js Normal file
View file

@ -0,0 +1,53 @@
const assert = require('assert');
const sinon = require('sinon');
const GlobalBanDB = require('../../lib/db/globalban').GlobalBanDB;
const CachingGlobalBanlist = require('../../lib/io/globalban').CachingGlobalBanlist;
describe('CachingGlobalBanlist', () => {
let banlist = null;
let banDB = null;
beforeEach(() => {
banDB = new GlobalBanDB();
banlist = new CachingGlobalBanlist(banDB);
});
describe('refreshCache', () => {
it('caches bans', () => {
const bans = [{ ip: '1.1.1.1', reason: 'test' }];
sinon.stub(banDB, 'listGlobalBans').resolves(bans);
return banlist.refreshCache().then(() => {
assert(banlist.cache.has(bans[0].ip), 'Cache was not populated');
});
});
it('clears removed bans', () => {
banlist.cache.add('1.1.1.1');
sinon.stub(banDB, 'listGlobalBans').resolves([]);
return banlist.refreshCache().then(() => {
assert(!banlist.cache.has('1.1.1.1'), 'Cache was not updated');
});
});
it('fails open', () => {
sinon.stub(banDB, 'listGlobalBans').rejects(new Error('Broken'));
return banlist.refreshCache();
});
});
describe('isIPGlobalBanned', () => {
it('checks the full IP', () => {
banlist.cache.add('1.2.3.4');
assert(banlist.isIPGlobalBanned('1.2.3.4'), 'Expected IP to be banned');
});
it('checks the range IP', () => {
banlist.cache.add('1.2.3');
assert(banlist.isIPGlobalBanned('1.2.3.4'), 'Expected IP to be banned');
});
it('checks the wrange IP', () => {
banlist.cache.add('1.2');
assert(banlist.isIPGlobalBanned('1.2.3.4'), 'Expected IP to be banned');
});
});
});

98
test/io/ioserver.js Normal file
View file

@ -0,0 +1,98 @@
const assert = require('assert');
const IOServer = require('../../lib/io/ioserver').IOServer;
describe('IOServer', () => {
let server;
let socket;
beforeEach(() => {
server = new IOServer();
socket = {
context: {
ipAddress: '9.9.9.9'
},
handshake: {
address: '127.0.0.1',
headers: {
'x-forwarded-for': '1.2.3.4'
}
}
};
});
describe('#ipProxyMiddleware', () => {
it('proxies from a trusted address', done => {
server.ipProxyMiddleware(socket, error => {
assert(!error);
assert.strictEqual(socket.context.ipAddress, '1.2.3.4');
done();
});
});
it('does not proxy from a non-trusted address', done => {
socket.handshake.address = '5.6.7.8';
server.ipProxyMiddleware(socket, error => {
assert(!error);
assert.strictEqual(socket.context.ipAddress, '5.6.7.8');
done();
});
});
it('sets context.torConnection = true for Tor exits', () => {
// TODO
});
});
describe('#ipBanMiddleware', () => {
// TODO
});
describe('#ipThrottleMiddleware', () => {
it('throttles connections', done => {
let i = 0;
function callback(error) {
if (i < 5) {
assert(!error);
} else {
assert.strictEqual(error.message, 'Rate limit exceeded');
done();
}
}
function next() {
server.ipThrottleMiddleware(socket, error => {
callback(error);
if (++i < 6) next();
});
}
next();
});
});
describe('#cookieParsingMiddleware', () => {
it('parses cookies', done => {
socket.handshake.headers.cookie = 'flavor=chocolate%20chip';
server.cookieParsingMiddleware(socket, () => {
assert.strictEqual(socket.handshake.cookies.flavor, 'chocolate chip');
done();
});
});
it('defaults to empty objects if no cookies', done => {
server.cookieParsingMiddleware(socket, () => {
assert.deepStrictEqual(socket.handshake.cookies, {});
assert.deepStrictEqual(socket.handshake.signedCookies, {});
done();
});
});
});
describe('#ipSessionCookieMiddleware', () => {
// TODO
});
describe('#authUserMiddleware', () => {
// TODO
});
});