2021-06-16 18:21:54 +03:00
|
|
|
import { expect } from 'chai'
|
2022-06-30 16:32:56 +03:00
|
|
|
import { randomBytes } from 'crypto'
|
2023-06-05 03:30:48 +03:00
|
|
|
import { describe, it } from 'mocha'
|
2021-06-16 18:21:54 +03:00
|
|
|
|
2022-06-30 16:32:56 +03:00
|
|
|
import { sleep } from '../../src'
|
|
|
|
import { createTestTelegramClient } from './utils'
|
|
|
|
|
2023-06-05 03:30:48 +03:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
2021-06-16 18:21:54 +03:00
|
|
|
require('dotenv-flow').config()
|
|
|
|
|
|
|
|
describe('fuzz : session', async function () {
|
|
|
|
this.timeout(45000)
|
|
|
|
|
|
|
|
it('random auth_key', async () => {
|
2021-07-27 15:32:18 +03:00
|
|
|
const client = createTestTelegramClient()
|
2021-06-16 18:21:54 +03:00
|
|
|
|
|
|
|
// random key
|
|
|
|
const initKey = randomBytes(256)
|
|
|
|
await client.storage.setAuthKeyFor(2, initKey)
|
|
|
|
|
|
|
|
// client is supposed to handle this and generate a new key
|
|
|
|
|
2023-07-20 17:51:24 +03:00
|
|
|
const errors: unknown[] = []
|
2021-06-16 18:21:54 +03:00
|
|
|
|
2023-07-20 17:51:24 +03:00
|
|
|
const errorHandler = (err: unknown) => {
|
2021-06-16 18:21:54 +03:00
|
|
|
errors.push(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
client.onError(errorHandler)
|
|
|
|
|
|
|
|
await client.connect()
|
|
|
|
|
|
|
|
await sleep(10000)
|
|
|
|
|
|
|
|
await client.close()
|
|
|
|
|
|
|
|
expect(errors.length).eq(0)
|
|
|
|
|
2023-06-05 03:30:48 +03:00
|
|
|
expect((await client.storage.getAuthKeyFor(2))?.toString('hex')).not.eq(
|
|
|
|
initKey.toString('hex'),
|
2021-06-16 18:21:54 +03:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('random auth_key for other dc', async () => {
|
2021-07-27 15:32:18 +03:00
|
|
|
const client = createTestTelegramClient()
|
2021-06-16 18:21:54 +03:00
|
|
|
|
|
|
|
// random key for dc1
|
|
|
|
const initKey = randomBytes(256)
|
|
|
|
await client.storage.setAuthKeyFor(1, initKey)
|
|
|
|
|
|
|
|
// client is supposed to handle this and generate a new key
|
|
|
|
|
2023-07-20 17:51:24 +03:00
|
|
|
const errors: unknown[] = []
|
2021-06-16 18:21:54 +03:00
|
|
|
|
2023-07-20 17:51:24 +03:00
|
|
|
const errorHandler = (err: unknown) => {
|
2021-06-16 18:21:54 +03:00
|
|
|
errors.push(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
client.onError(errorHandler)
|
|
|
|
|
|
|
|
await client.connect()
|
|
|
|
await client.waitUntilUsable()
|
|
|
|
|
|
|
|
const conn = await client.createAdditionalConnection(1)
|
2022-04-01 22:17:10 +03:00
|
|
|
await conn.sendRpc({ _: 'help.getConfig' })
|
2021-06-16 18:21:54 +03:00
|
|
|
|
|
|
|
await sleep(10000)
|
|
|
|
|
|
|
|
await client.close()
|
|
|
|
|
|
|
|
expect(errors.length).eq(0)
|
|
|
|
|
2023-06-05 03:30:48 +03:00
|
|
|
expect((await client.storage.getAuthKeyFor(1))?.toString('hex')).not.eq(
|
|
|
|
initKey.toString('hex'),
|
2021-06-16 18:21:54 +03:00
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|