2023-06-05 03:30:48 +03:00
|
|
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
2021-06-15 03:12:22 +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 { EventEmitter } from 'events'
|
|
|
|
import { describe, it } from 'mocha'
|
2022-06-30 16:32:56 +03:00
|
|
|
|
2021-06-15 03:12:22 +03:00
|
|
|
import {
|
|
|
|
BaseTelegramClient,
|
|
|
|
defaultDcs,
|
2022-06-30 16:32:56 +03:00
|
|
|
ITelegramTransport,
|
|
|
|
NodeCryptoProvider,
|
|
|
|
sleep,
|
2023-06-05 03:30:48 +03:00
|
|
|
tl,
|
|
|
|
TransportState,
|
2021-06-15 03:12:22 +03:00
|
|
|
} from '../../src'
|
|
|
|
|
2023-06-05 03:30:48 +03:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
2021-06-15 03:12:22 +03:00
|
|
|
require('dotenv-flow').config()
|
|
|
|
|
2021-07-27 15:32:18 +03:00
|
|
|
class RandomBytesTransport extends EventEmitter implements ITelegramTransport {
|
2022-06-30 16:32:56 +03:00
|
|
|
dc!: tl.RawDcOption
|
2023-06-05 03:30:48 +03:00
|
|
|
interval?: NodeJS.Timeout
|
2021-06-15 03:12:22 +03:00
|
|
|
|
|
|
|
close(): void {
|
2023-06-05 03:30:48 +03:00
|
|
|
clearInterval(this.interval)
|
2021-06-15 03:12:22 +03:00
|
|
|
this.emit('close')
|
2023-06-05 03:30:48 +03:00
|
|
|
this.interval = undefined
|
2021-06-15 03:12:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
connect(dc: tl.RawDcOption): void {
|
|
|
|
this.dc = dc
|
|
|
|
|
|
|
|
setTimeout(() => this.emit('ready'), 0)
|
|
|
|
|
|
|
|
this.interval = setInterval(() => {
|
|
|
|
this.emit('message', randomBytes(64))
|
|
|
|
}, 100)
|
|
|
|
}
|
|
|
|
|
|
|
|
currentDc(): tl.RawDcOption | null {
|
|
|
|
return this.dc
|
|
|
|
}
|
|
|
|
|
2023-06-05 03:30:48 +03:00
|
|
|
send(_data: Buffer): Promise<void> {
|
2021-06-15 03:12:22 +03:00
|
|
|
return Promise.resolve()
|
|
|
|
}
|
|
|
|
|
|
|
|
state(): TransportState {
|
|
|
|
return this.interval ? TransportState.Ready : TransportState.Idle
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('fuzz : transport', function () {
|
|
|
|
this.timeout(30000)
|
|
|
|
|
|
|
|
it('RandomBytesTransport (no auth)', async () => {
|
|
|
|
const client = new BaseTelegramClient({
|
2021-07-27 15:32:18 +03:00
|
|
|
crypto: () => new NodeCryptoProvider(),
|
2021-06-15 03:12:22 +03:00
|
|
|
transport: () => new RandomBytesTransport(),
|
|
|
|
apiId: 0,
|
|
|
|
apiHash: '',
|
|
|
|
primaryDc: defaultDcs.defaultTestDc,
|
|
|
|
})
|
2022-06-30 16:32:56 +03:00
|
|
|
client.log.level = 0
|
2021-06-15 03:12:22 +03:00
|
|
|
|
|
|
|
const errors: Error[] = []
|
|
|
|
|
|
|
|
client.onError((err) => {
|
|
|
|
errors.push(err)
|
|
|
|
})
|
|
|
|
|
|
|
|
await client.connect()
|
|
|
|
await sleep(15000)
|
|
|
|
await client.close()
|
|
|
|
|
|
|
|
expect(errors.length).gt(0)
|
|
|
|
errors.forEach((err) => {
|
|
|
|
expect(err.message).match(/unknown object id/i)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('RandomBytesTransport (with auth)', async () => {
|
|
|
|
const client = new BaseTelegramClient({
|
2021-07-27 15:32:18 +03:00
|
|
|
crypto: () => new NodeCryptoProvider(),
|
2021-06-15 03:12:22 +03:00
|
|
|
transport: () => new RandomBytesTransport(),
|
|
|
|
apiId: 0,
|
|
|
|
apiHash: '',
|
|
|
|
primaryDc: defaultDcs.defaultTestDc,
|
|
|
|
})
|
2022-06-30 16:32:56 +03:00
|
|
|
client.log.level = 0
|
|
|
|
|
2021-06-15 03:12:22 +03:00
|
|
|
// random key just to make it think it already has one
|
|
|
|
await client.storage.setAuthKeyFor(2, randomBytes(256))
|
|
|
|
|
|
|
|
// in this case, there will be no actual errors, only
|
|
|
|
// warnings like 'received message with unknown authKey'
|
|
|
|
//
|
|
|
|
// to test for that, we hook into `decryptMessage` and make
|
|
|
|
// sure that it returns `null`
|
|
|
|
|
|
|
|
await client.connect()
|
|
|
|
|
|
|
|
let hadNonNull = false
|
|
|
|
|
|
|
|
const decryptMessage =
|
2023-06-05 03:30:48 +03:00
|
|
|
// eslint-disable-next-line dot-notation
|
2022-06-30 16:32:56 +03:00
|
|
|
client.primaryConnection['_session'].decryptMessage
|
|
|
|
|
|
|
|
// ехал any через any
|
|
|
|
// видит any - any, any
|
|
|
|
// сунул any any в any
|
|
|
|
// any any any any
|
2023-06-05 03:30:48 +03:00
|
|
|
// eslint-disable-next-line dot-notation
|
2022-06-30 16:32:56 +03:00
|
|
|
;(client.primaryConnection['_session'] as any).decryptMessage = (
|
|
|
|
buf: any,
|
2023-06-05 03:30:48 +03:00
|
|
|
cb: any,
|
2022-06-30 16:32:56 +03:00
|
|
|
) =>
|
|
|
|
decryptMessage.call(this, buf, (...args: any[]) => {
|
|
|
|
cb(...(args as any))
|
|
|
|
hadNonNull = true
|
|
|
|
})
|
2021-06-15 03:12:22 +03:00
|
|
|
|
|
|
|
await sleep(15000)
|
|
|
|
await client.close()
|
|
|
|
|
|
|
|
expect(hadNonNull).false
|
|
|
|
})
|
|
|
|
})
|