Skip to content

Commit

Permalink
Generate big test file at runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
islathehut committed Dec 19, 2024
1 parent 7d8fb43 commit 5502a5d
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 48 deletions.
15 changes: 0 additions & 15 deletions packages/backend/src/nest/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,21 +255,6 @@ export const tmpQuietDirPath = (name: string): string => {
return path.join(name, TestConfig.QUIET_DIR)
}

export function createFile(filePath: string, size: number) {
const stream = fs.createWriteStream(filePath)
const maxChunkSize = 1048576 // 1MB

let remainingSize = size

while (remainingSize > 0) {
const chunkSize = Math.min(maxChunkSize, remainingSize)
stream.write(crypto.randomBytes(chunkSize))
remainingSize -= chunkSize
}

stream.end()
}

export async function createPeerId(): Promise<PeerId> {
const peerId = await createEd25519PeerId()
return peerIdFromKeys(peerId.publicKey, peerId.privateKey)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { DownloadState, FileMetadata } from '@quiet/types'
import { DirResult } from 'tmp'
import waitForExpect from 'wait-for-expect'
import { TestModule } from '../common/test.module'
import { createFile, createTmpDir, libp2pInstanceParams } from '../common/utils'
import { createTmpDir, libp2pInstanceParams } from '../common/utils'
import { IpfsModule } from '../ipfs/ipfs.module'
import { IpfsService } from '../ipfs/ipfs.service'
import { Libp2pModule } from '../libp2p/libp2p.module'
Expand All @@ -16,6 +16,7 @@ import { IpfsFileManagerModule } from './ipfs-file-manager.module'
import { IpfsFileManagerService } from './ipfs-file-manager.service'
import fs from 'fs'
import { createLogger } from '../common/logger'
import { createArbitraryFile } from '@quiet/common'

const logger = createLogger('bigFiles:test')
const BIG_FILE_SIZE = 2147483000
Expand All @@ -33,7 +34,7 @@ describe('IpfsFileManagerService', () => {
tmpDir = createTmpDir()
filePath = new URL('./testUtils/large-file.txt', import.meta.url).pathname
// Generate 2.1GB file
createFile(filePath, BIG_FILE_SIZE)
createArbitraryFile(filePath, BIG_FILE_SIZE)
module = await Test.createTestingModule({
imports: [TestModule, IpfsFileManagerModule, IpfsModule, SocketModule, Libp2pModule],
}).compile()
Expand Down
6 changes: 3 additions & 3 deletions packages/backend/src/nest/storage/storage.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import path from 'path'
import { type PeerId } from '@libp2p/interface'
import waitForExpect from 'wait-for-expect'
import { TestModule } from '../common/test.module'
import { createFile, libp2pInstanceParams } from '../common/utils'
import { libp2pInstanceParams } from '../common/utils'
import { IpfsModule } from '../ipfs/ipfs.module'
import { IpfsService } from '../ipfs/ipfs.service'
import { Libp2pModule } from '../libp2p/libp2p.module'
Expand All @@ -38,8 +38,8 @@ import { LocalDbModule } from '../local-db/local-db.module'
import { LocalDbService } from '../local-db/local-db.service'
import { ORBIT_DB_DIR } from '../const'
import { createLogger } from '../common/logger'
import { sleep } from '../common/sleep'
import { createUserCertificateTestHelper, createTestRootCA } from '@quiet/identity'
import { createArbitraryFile } from '@quiet/common'

const logger = createLogger('storageService:test')

Expand Down Expand Up @@ -343,7 +343,7 @@ describe('StorageService', () => {

beforeEach(async () => {
realFilePath = path.join(dirname, '/real-file.txt')
createFile(realFilePath, 2147483)
createArbitraryFile(realFilePath, 2147483)
await storageService.init(peerId)

const metadata: FileMetadata = {
Expand Down
18 changes: 18 additions & 0 deletions packages/common/src/tests.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import fs from 'fs'
import crypto from 'crypto'

import { InvitationData, InvitationDataV1, InvitationDataV2, InvitationDataVersion } from '@quiet/types'
import { composeInvitationDeepUrl, composeInvitationShareUrl } from './invitationLink/invitationLink'
import { QUIET_JOIN_PAGE } from './const'
Expand Down Expand Up @@ -84,3 +87,18 @@ export function getValidInvitationUrlTestData<T extends InvitationData>(data: T)
// data: data,
// }
// }

export const createArbitraryFile = (filePath: string, sizeBytes: number) => {
const stream = fs.createWriteStream(filePath)
const maxChunkSize = 1048576 // 1MB

let remainingSize = sizeBytes

while (remainingSize > 0) {
const chunkSize = Math.min(maxChunkSize, remainingSize)
stream.write(crypto.randomBytes(chunkSize))
remainingSize -= chunkSize
}

stream.end()
}
3 changes: 2 additions & 1 deletion packages/e2e-tests/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ screenshots/
.DS_Store

Quiet/Quiet**
.env
.env
src/tests/resources/bigTestFile.bin
17 changes: 11 additions & 6 deletions packages/e2e-tests/src/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ export class Channel {
}

get uploadFileInput() {
return this.driver.wait(this.driver.findElement(By.xpath('//*[@data-testid="uploadFileInput"]')))
return this.driver.wait(this.driver.findElement(By.xpath('//*[@data-testid="uploadFileInput"]')), 15_000)
}

async sendMessage(message: string, username: string): Promise<MessageIds> {
Expand Down Expand Up @@ -646,7 +646,8 @@ export class Channel {
): Promise<WebElement> {
logger.info(`Waiting for file content for message with filename ${filename} and type ${fileType}`)
const messageContentElements = await this.driver.wait(
messageElement.findElements(By.xpath(`//*[contains(@data-testid, "messagesGroupContent-")]`))
messageElement.findElements(By.xpath(`//*[contains(@data-testid, "messagesGroupContent-")]`)),
45_000
)
for (const element of messageContentElements) {
logger.info(await element.getId())
Expand All @@ -655,12 +656,14 @@ export class Channel {
switch (fileType) {
case UploadedFileType.IMAGE:
containerElements = await this.driver.wait(
element.findElements(By.xpath(`//*[@class='UploadedImagecontainer']`))
element.findElements(By.xpath(`//*[@class='UploadedImagecontainer']`)),
45_000
)
break
case UploadedFileType.FILE:
containerElements = await this.driver.wait(
element.findElements(By.xpath(`//*[contains(@data-testid, "-fileComponent")]`))
element.findElements(By.xpath(`//*[contains(@data-testid, "-fileComponent")]`)),
45_000
)
break
}
Expand All @@ -676,12 +679,14 @@ export class Channel {
switch (fileType) {
case UploadedFileType.IMAGE:
contentElement = await this.driver.wait(
container.findElement(By.xpath(`//img[@class='UploadedImageimage']`))
container.findElement(By.xpath(`//img[@class='UploadedImageimage']`)),
45_000
)
break
case UploadedFileType.FILE:
contentElement = await this.driver.wait(
container.findElement(By.xpath(`//img[@class='FileComponentactionIcon']`))
container.findElement(By.xpath(`//img[@class='FileComponentactionIcon']`)),
45_000
)
break
}
Expand Down
52 changes: 37 additions & 15 deletions packages/e2e-tests/src/tests/multipleClients.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ import { MessageIds, UserTestData } from '../types'
import { createLogger } from '../logger'
import * as path from 'path'
import { UploadedFileType } from '../enums'
import {
BIG_FILE_SIZE,
TEST_BIG_FILE_NAME,
TEST_FILE_NAME,
TEST_IMAGE_FILE_NAME,
UPLOAD_FILE_DIR,
} from '../uploadFile.const'
import { createArbitraryFile } from '@quiet/common'

const logger = createLogger('multipleClients')

Expand Down Expand Up @@ -473,38 +481,52 @@ describe('Multiple Clients', () => {
let largeFileMessageIds: MessageIds | undefined = undefined

it('Owner uploads an image', async () => {
const filename = 'testImage.gif'
const uploadFilePath = path.resolve('./src/tests/resources/', filename)
await generalChannelOwner.uploadFile(filename, uploadFilePath, UploadedFileType.IMAGE, users.owner.username)
const uploadFilePath = path.resolve(UPLOAD_FILE_DIR, TEST_IMAGE_FILE_NAME)
await generalChannelOwner.uploadFile(
TEST_IMAGE_FILE_NAME,
uploadFilePath,
UploadedFileType.IMAGE,
users.owner.username
)
})

it('Guest sees uploaded image', async () => {
await sleep(10_000)
const filename = 'testImage.gif'
await generalChannelUser1.getMessageIdsByFile(filename, UploadedFileType.IMAGE, users.owner.username)
await generalChannelUser1.getMessageIdsByFile(
TEST_IMAGE_FILE_NAME,
UploadedFileType.IMAGE,
users.owner.username
)
})

it('Owner uploads a file', async () => {
const filename = 'testFile.pdf'
const uploadFilePath = path.resolve('./src/tests/resources/', filename)
await generalChannelOwner.uploadFile(filename, uploadFilePath, UploadedFileType.FILE, users.owner.username)
const uploadFilePath = path.resolve(UPLOAD_FILE_DIR, TEST_FILE_NAME)
await generalChannelOwner.uploadFile(
TEST_FILE_NAME,
uploadFilePath,
UploadedFileType.FILE,
users.owner.username
)
})

it('Guest sees uploaded file and it downloads', async () => {
const filename = 'testFile.pdf'
await generalChannelUser1.getMessageIdsByFile(filename, UploadedFileType.FILE, users.owner.username)
await generalChannelUser1.getMessageIdsByFile(TEST_FILE_NAME, UploadedFileType.FILE, users.owner.username)
})

it('Owner uploads a large file', async () => {
const filename = 'largeTestFile.bin'
const uploadFilePath = path.resolve('./src/tests/resources/', filename)
await generalChannelOwner.uploadFile(filename, uploadFilePath, UploadedFileType.FILE, users.owner.username)
const uploadFilePath = path.resolve(UPLOAD_FILE_DIR, TEST_BIG_FILE_NAME)
createArbitraryFile(uploadFilePath, BIG_FILE_SIZE)
await generalChannelOwner.uploadFile(
TEST_BIG_FILE_NAME,
uploadFilePath,
UploadedFileType.FILE,
users.owner.username
)
})

it(`Guest sees uploaded large file`, async () => {
const filename = 'largeTestFile.bin'
largeFileMessageIds = await generalChannelUser1.getMessageIdsByFile(
filename,
TEST_BIG_FILE_NAME,
UploadedFileType.FILE,
users.owner.username
)
Expand Down
11 changes: 5 additions & 6 deletions packages/e2e-tests/src/tests/oneClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import path from 'path'
import { createLogger } from '../logger'
import { sleep } from '../utils'
import { UploadedFileType } from '../enums'
import { TEST_IMAGE_FILE_NAME, UPLOAD_FILE_DIR } from '../uploadFile.const'

const logger = createLogger('oneClient')

Expand Down Expand Up @@ -208,15 +209,13 @@ describe('One Client', () => {

describe('Uploading files', () => {
it('Owner uploads an image', async () => {
const filename = 'testImage.gif'
const uploadFilePath = path.resolve('./src/tests/resources/', filename)
await generalChannel.uploadFile(filename, uploadFilePath, UploadedFileType.IMAGE, ownerUserName)
const uploadFilePath = path.resolve(UPLOAD_FILE_DIR, TEST_IMAGE_FILE_NAME)
await generalChannel.uploadFile(TEST_IMAGE_FILE_NAME, uploadFilePath, UploadedFileType.IMAGE, ownerUserName)
})

it('Owner uploads a non-image file', async () => {
const filename = 'testFile.pdf'
const uploadFilePath = path.resolve('./src/tests/resources/', filename)
await generalChannel.uploadFile(filename, uploadFilePath, UploadedFileType.FILE, ownerUserName)
const uploadFilePath = path.resolve(UPLOAD_FILE_DIR, TEST_IMAGE_FILE_NAME)
await generalChannel.uploadFile(TEST_IMAGE_FILE_NAME, uploadFilePath, UploadedFileType.FILE, ownerUserName)
})
})
})
Binary file not shown.
6 changes: 6 additions & 0 deletions packages/e2e-tests/src/uploadFile.const.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const TEST_IMAGE_FILE_NAME = 'testImage.gif'
export const TEST_FILE_NAME = 'testFile.pdf'
export const TEST_BIG_FILE_NAME = 'bigTestFile.bin'
export const BIG_FILE_SIZE = 15728640 // 15MB

export const UPLOAD_FILE_DIR = './src/tests/resources/'

0 comments on commit 5502a5d

Please sign in to comment.