tor push
This commit is contained in:
+117
@@ -0,0 +1,117 @@
|
||||
import BackgroundMain, { doNotProxy } from '../../src/background/BackgroundMain'
|
||||
import { Store } from '../../src/store/Store'
|
||||
import webExtensionsApiFake from 'webextensions-api-fake'
|
||||
|
||||
import { expect } from 'chai'
|
||||
import { ProxySettings } from '../../src/domain/ProxySettings'
|
||||
const tryFromDao = ProxySettings.tryFromDao
|
||||
|
||||
/* eslint-disable @typescript-eslint/no-unused-expressions */
|
||||
|
||||
const store = new Store()
|
||||
|
||||
describe('BackgroundMain', function () {
|
||||
beforeEach(() => {
|
||||
// @ts-expect-error
|
||||
global.browser = webExtensionsApiFake()
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
// @ts-expect-error
|
||||
delete global.browser
|
||||
})
|
||||
|
||||
const backgroundMain = new BackgroundMain({ store: store })
|
||||
|
||||
// TODO: Add test for proxyDNS property
|
||||
|
||||
describe('onRequest', function () {
|
||||
it('should return empty array if no proxy is set up', async () => {
|
||||
const result = await backgroundMain.onRequest({ cookieStoreId: 'firefox-default', url: 'https://google.com', tabId: 0 })
|
||||
|
||||
expect(result).to.be.deep.equal(doNotProxy)
|
||||
})
|
||||
|
||||
it('should return proxy if proxy is set up', async () => {
|
||||
await givenSomeProxyIsSetUpForContainer({ containerId: 'firefox-default', host: undefined, doNotProxyLocal: undefined })
|
||||
|
||||
const result = await backgroundMain.onRequest({ cookieStoreId: 'firefox-default', url: 'https://google.com', tabId: 0 })
|
||||
|
||||
expect(result).to.be.an('array')
|
||||
expect(result).to.be.not.empty
|
||||
})
|
||||
|
||||
it('should remove doNotProxyLocal flag from proxy settings if proxy is set up', async () => {
|
||||
await givenSomeProxyIsSetUpForContainer({ containerId: 'firefox-default', host: undefined, doNotProxyLocal: undefined })
|
||||
|
||||
const result = await backgroundMain.onRequest({ cookieStoreId: 'firefox-default', url: 'https://google.com', tabId: 0 })
|
||||
|
||||
expect((result[0] as any).doNotProxyLocal).to.be.undefined
|
||||
})
|
||||
|
||||
it('should return proxy for the container if url is invalid', async () => {
|
||||
// To be more on a safe side
|
||||
await givenSomeProxyIsSetUpForContainer({ containerId: 'firefox-default', host: undefined, doNotProxyLocal: undefined })
|
||||
|
||||
const result = await backgroundMain.onRequest({ cookieStoreId: 'firefox-default', url: 'np-protocol-url.com', tabId: 0 })
|
||||
|
||||
expect(result).to.be.an('array')
|
||||
expect(result).to.be.not.empty
|
||||
})
|
||||
|
||||
// Connections to localhost, 127.0.0.1, and ::1 are never proxied. (From FF settings)
|
||||
const localAddresses = [
|
||||
'http://localhost/index.html',
|
||||
'https://localhost/index.html',
|
||||
'http://127.0.0.1/',
|
||||
'https://127.0.0.1/',
|
||||
'http://[::1]/test',
|
||||
'https://[::1]/test',
|
||||
'http://[0:0:0:0:0:0:0:1]/test',
|
||||
'https://[0:0:0:0:0:0:0:1]/test',
|
||||
'https://user:password@127.0.0.1:123/',
|
||||
'http://[::1]:123/test'
|
||||
]
|
||||
|
||||
describe('proxying of local addresses is disabled', () => {
|
||||
localAddresses.forEach(url => {
|
||||
it(`should return empty array if the address is local: ${url}`, async () => {
|
||||
await givenSomeProxyIsSetUpForContainer({ containerId: 'container1', host: undefined, doNotProxyLocal: true })
|
||||
|
||||
const result = await backgroundMain.onRequest({ cookieStoreId: 'container1', url, tabId: 0 })
|
||||
|
||||
expect(result).to.be.deep.equal(doNotProxy)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('proxying of local addresses is enabled', () => {
|
||||
localAddresses.forEach(url => {
|
||||
it(`should return array with proxy: ${url}`, async () => {
|
||||
const host = 'proxyX.example.com'
|
||||
await givenSomeProxyIsSetUpForContainer({ host, containerId: 'container1', doNotProxyLocal: false })
|
||||
|
||||
const result = await backgroundMain.onRequest({ cookieStoreId: 'container1', url, tabId: 0 })
|
||||
|
||||
expect(result[0].host).to.be.equal(host)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
async function givenSomeProxyIsSetUpForContainer({ host, containerId, doNotProxyLocal }: any): Promise<void> {
|
||||
const proxyId = 'proxy1'
|
||||
const proxy: any = {
|
||||
id: proxyId,
|
||||
type: 'socks',
|
||||
host: (host as string) ?? 'example.com',
|
||||
port: 1080
|
||||
}
|
||||
if (typeof doNotProxyLocal !== 'undefined') {
|
||||
proxy.doNotProxyLocal = doNotProxyLocal
|
||||
}
|
||||
await store.putProxy(tryFromDao(proxy) as ProxySettings)
|
||||
|
||||
await store.setContainerProxyRelation(containerId, proxyId)
|
||||
}
|
||||
+167
@@ -0,0 +1,167 @@
|
||||
import { ProxyDao, Store } from '../../src/store/Store'
|
||||
import webExtensionsApiFake from 'webextensions-api-fake'
|
||||
|
||||
import { expect } from 'chai'
|
||||
import { ProxySettings } from '../../src/domain/ProxySettings'
|
||||
const tryFromDao = ProxySettings.tryFromDao
|
||||
|
||||
/* eslint-disable @typescript-eslint/no-unused-expressions */
|
||||
|
||||
describe('Store', () => {
|
||||
const store = new Store()
|
||||
|
||||
beforeEach(() => {
|
||||
(global as any).browser = webExtensionsApiFake()
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
delete (global as any).browser
|
||||
})
|
||||
|
||||
function someProxyWith(id: string, props: Partial<ProxyDao> = {}): ProxySettings {
|
||||
const dao = {
|
||||
id: id,
|
||||
title: 'some title',
|
||||
type: 'socks',
|
||||
host: 'localhost',
|
||||
port: 1080,
|
||||
username: 'user',
|
||||
password: 'password',
|
||||
proxyDNS: true,
|
||||
failoverTimeout: 5,
|
||||
doNotProxyLocal: true
|
||||
}
|
||||
return tryFromDao({ ...dao, ...props }) as ProxySettings
|
||||
}
|
||||
|
||||
describe('getAllProxies', function () {
|
||||
it('should return empty array if nothing is saved', async () => {
|
||||
const proxies = await store.getAllProxies()
|
||||
|
||||
expect(proxies).to.be.an('array')
|
||||
expect(proxies).to.be.empty
|
||||
})
|
||||
})
|
||||
|
||||
it('should put and get the proxy back', async () => {
|
||||
const id = 'someId'
|
||||
const proxy = someProxyWith(id)
|
||||
|
||||
await store.putProxy(proxy)
|
||||
|
||||
const gotProxy = await store.getProxyById(id)
|
||||
|
||||
expect(gotProxy).to.be.deep.equal(proxy)
|
||||
})
|
||||
|
||||
it('when get, returns doNotProxyLocal `true` if was not set when put', async () => {
|
||||
const id = 'someId'
|
||||
const proxy = someProxyWith(id)
|
||||
delete (proxy as any).doNotProxyLocal
|
||||
|
||||
await store.putProxy(proxy)
|
||||
|
||||
const gotProxy = await store.getProxyById(id)
|
||||
|
||||
expect(gotProxy?.doNotProxyLocal).to.be.equal(true)
|
||||
})
|
||||
|
||||
it('stores doNotProxyLocal value', async () => {
|
||||
const id = 'someId'
|
||||
const proxy = someProxyWith(id, { doNotProxyLocal: false })
|
||||
|
||||
await store.putProxy(proxy)
|
||||
|
||||
const gotProxy = await store.getProxyById(id)
|
||||
|
||||
expect(gotProxy?.doNotProxyLocal).to.be.equal(false)
|
||||
})
|
||||
|
||||
it('should be able to delete proxy', async () => {
|
||||
const id = 'someId'
|
||||
await store.putProxy(someProxyWith(id))
|
||||
await store.deleteProxyById(id)
|
||||
|
||||
const result = await store.getProxyById(id)
|
||||
|
||||
expect(result).to.be.null
|
||||
})
|
||||
|
||||
// describe('getProxiesForContainer', () => {
|
||||
// it('should find proxy by container id if one present', async () => {
|
||||
// const cookieStoreId = 'container-1'
|
||||
// const proxyId = 'proxy-1'
|
||||
// // TODO Store should probably take care of this
|
||||
// const relations = {
|
||||
// [cookieStoreId]: [proxyId]
|
||||
// }
|
||||
// const givenProxy = someProxyWith(proxyId)
|
||||
// await store.putProxy(givenProxy)
|
||||
|
||||
// await browser.storage.local.set({ relations: relations })
|
||||
|
||||
// const [gotProxy] = await store.getProxiesForContainer(cookieStoreId)
|
||||
|
||||
// expect(gotProxy).to.be.deep.equal(givenProxy)
|
||||
// })
|
||||
|
||||
// it('doNotProxyLocal should be `true` if not set', async () => {
|
||||
// const cookieStoreId = 'container-1'
|
||||
// const proxyId = 'proxy-1'
|
||||
// // TODO Store should probably take care of this
|
||||
// const relations = {
|
||||
// [cookieStoreId]: [proxyId]
|
||||
// }
|
||||
// const givenProxy = someProxyWith(proxyId)
|
||||
// delete (givenProxy as any).doNotProxyLocal
|
||||
// await store.putProxy(givenProxy)
|
||||
|
||||
// await browser.storage.local.set({ relations: relations })
|
||||
|
||||
// const [gotProxy] = await store.getProxiesForContainer(cookieStoreId)
|
||||
|
||||
// expect(gotProxy.doNotProxyLocal).to.be.equal(true)
|
||||
// })
|
||||
|
||||
// it('should return empty array if proxy not set for the container', async () => {
|
||||
// const cookieStoreId = 'container-1'
|
||||
|
||||
// const result = await store.getProxiesForContainer(cookieStoreId)
|
||||
|
||||
// expect(result).to.be.empty
|
||||
// })
|
||||
|
||||
// it('should return empty array if proxy is set for the container but does not exist', async () => {
|
||||
// const cookieStoreId = 'container-1'
|
||||
// const proxyId = 'something-absent'
|
||||
// const relations = {
|
||||
// [cookieStoreId]: [proxyId]
|
||||
// }
|
||||
// await browser.storage.local.set({ relations: relations })
|
||||
|
||||
// const result = await store.getProxiesForContainer(cookieStoreId)
|
||||
|
||||
// expect(result).to.be.empty
|
||||
// })
|
||||
// })
|
||||
|
||||
describe('setContainerProxyRelation', function () {
|
||||
it('should set the relation', async () => {
|
||||
await store.setContainerProxyRelation('container1', 'proxy1')
|
||||
|
||||
const relations = await store.getRelations()
|
||||
|
||||
expect(relations.container1).to.be.deep.equal(['proxy1'])
|
||||
})
|
||||
|
||||
it('should not remove existing relations', async () => {
|
||||
await store.setContainerProxyRelation('container1', 'proxy1')
|
||||
await store.setContainerProxyRelation('container2', 'proxy2')
|
||||
|
||||
const relations = await store.getRelations()
|
||||
|
||||
expect(relations.container1).to.be.deep.equal(['proxy1'])
|
||||
expect(relations.container2).to.be.deep.equal(['proxy2'])
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user