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 { describe, it } from 'mocha'
|
|
|
|
|
2022-06-30 16:32:56 +03:00
|
|
|
import __tlReaderMap from '@mtcute/tl/binary/reader'
|
2023-06-05 03:30:48 +03:00
|
|
|
import { TlBinaryReader } from '@mtcute/tl-runtime'
|
|
|
|
|
2022-06-30 16:32:56 +03:00
|
|
|
import { createTestTelegramClient } from './utils'
|
2021-06-15 03:12:22 +03:00
|
|
|
|
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()
|
|
|
|
|
|
|
|
describe('fuzz : packet', async function () {
|
|
|
|
this.timeout(45000)
|
|
|
|
|
|
|
|
it('random packet', async () => {
|
2021-07-27 15:32:18 +03:00
|
|
|
const client = createTestTelegramClient()
|
2021-06-15 03:12:22 +03:00
|
|
|
|
|
|
|
await client.connect()
|
|
|
|
await client.waitUntilUsable()
|
|
|
|
|
2022-06-30 16:32:56 +03:00
|
|
|
let errors = 0
|
2021-06-15 03:12:22 +03:00
|
|
|
|
|
|
|
const conn = client.primaryConnection
|
2023-06-05 03:30:48 +03:00
|
|
|
// eslint-disable-next-line dot-notation
|
2022-06-30 16:32:56 +03:00
|
|
|
const mtproto = conn['_session']
|
2021-06-15 03:12:22 +03:00
|
|
|
|
|
|
|
for (let i = 0; i < 100; i++) {
|
|
|
|
const payload = randomBytes(Math.round(Math.random() * 16) * 16)
|
|
|
|
|
2022-06-30 16:32:56 +03:00
|
|
|
try {
|
2023-06-05 03:30:48 +03:00
|
|
|
// eslint-disable-next-line dot-notation
|
2022-06-30 16:32:56 +03:00
|
|
|
conn['_handleRawMessage'](
|
|
|
|
mtproto.getMessageId().sub(1),
|
|
|
|
0,
|
2023-06-05 03:30:48 +03:00
|
|
|
new TlBinaryReader(__tlReaderMap, payload),
|
2022-06-30 16:32:56 +03:00
|
|
|
)
|
|
|
|
} catch (e) {
|
|
|
|
errors += 1
|
|
|
|
}
|
2021-06-15 03:12:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// similar test, but this time only using object ids that do exist
|
2022-06-30 16:32:56 +03:00
|
|
|
const objectIds = Object.keys(__tlReaderMap)
|
2023-06-05 03:30:48 +03:00
|
|
|
|
2021-06-15 03:12:22 +03:00
|
|
|
for (let i = 0; i < 100; i++) {
|
2022-06-30 16:32:56 +03:00
|
|
|
const payload = randomBytes(
|
2023-06-05 03:30:48 +03:00
|
|
|
(Math.round(Math.random() * 16) + 1) * 16,
|
2022-06-30 16:32:56 +03:00
|
|
|
)
|
|
|
|
const objectId = parseInt(
|
2023-06-05 03:30:48 +03:00
|
|
|
objectIds[Math.round(Math.random() * objectIds.length)],
|
2022-06-30 16:32:56 +03:00
|
|
|
)
|
2021-06-15 03:12:22 +03:00
|
|
|
payload.writeUInt32LE(objectId, 0)
|
|
|
|
|
2022-06-30 16:32:56 +03:00
|
|
|
try {
|
2023-06-05 03:30:48 +03:00
|
|
|
// eslint-disable-next-line dot-notation
|
2022-06-30 16:32:56 +03:00
|
|
|
conn['_handleRawMessage'](
|
|
|
|
mtproto.getMessageId().sub(1),
|
|
|
|
0,
|
2023-06-05 03:30:48 +03:00
|
|
|
new TlBinaryReader(__tlReaderMap, payload),
|
2022-06-30 16:32:56 +03:00
|
|
|
)
|
|
|
|
} catch (e) {
|
|
|
|
errors += 1
|
|
|
|
}
|
2021-06-15 03:12:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
await client.close()
|
|
|
|
|
2022-06-30 16:32:56 +03:00
|
|
|
expect(errors).gt(0)
|
2021-06-15 03:12:22 +03:00
|
|
|
})
|
|
|
|
})
|