Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: zip extraction #519

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added src/utils/__fixtures__/test.zip
Binary file not shown.
102 changes: 101 additions & 1 deletion src/utils/__tests__/system.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import * as fs from 'fs';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(can't comment on the zip file) -- would it be possible to use a smaller zip -- 1MB is quite hefty to be part of the repository forever!


import { logger } from '../../logger';
import { withTempFile } from '../files';
import { scan, withTempDir, withTempFile } from '../files';

import {
calculateChecksum,
extractZipArchive,
hasExecutable,
HashAlgorithm,
HashOutputFormat,
replaceEnvVariable,
spawnProcess,
} from '../system';
import { relative, resolve } from 'path';

jest.mock('../../logger');

Expand Down Expand Up @@ -174,3 +176,101 @@ describe('isExecutableInPath', () => {
expect(hasExecutable('./bin/non-existing-binary')).toBe(false);
});
});

describe('extractZipArchive', () => {
const testZipPath = resolve(__dirname, '../__fixtures__/test.zip');

test('extracts a zip archive', async () => {
await withTempDir(async tmpDir => {
await extractZipArchive(testZipPath, tmpDir);
const files = (await scan(tmpDir)).map(file => relative(tmpDir, file));
expect(files).toStrictEqual(['Sentry.zip']);

const innerFilePath = resolve(tmpDir, files[0])
expect(fs.statSync(innerFilePath).size).toBe(1409323);
});
});

// This is a regression test for a bug in the original implementation based on
// unzipper when running on a new NodeJS v20.
// Extraction of the root archive would succeed but actually the contents
// of the nested archive would be compromised. Following attempt to
// extract the inner archive would yield: "ERROR invalid distance code"
test('extracts archive contained in another achive', async () => {
await withTempDir(async tmpDir => {
await extractZipArchive(testZipPath, tmpDir);

const innerZipPath = resolve(tmpDir, 'Sentry.zip')
const innerDir = resolve(tmpDir, 'Sentry')
fs.mkdirSync(innerDir)

await extractZipArchive(innerZipPath, innerDir);
const files = (await scan(innerDir))
.map(file => relative(innerDir, file).replace(/\\/g, '/'))
.sort();
expect(files).toStrictEqual([
"Sentry.psd1",
"Sentry.psm1",
"assemblies-loader.ps1",
"lib/.gitignore",
"lib/net462/Microsoft.Bcl.AsyncInterfaces.dll",
"lib/net462/Microsoft.Bcl.AsyncInterfaces.license",
"lib/net462/Microsoft.Bcl.AsyncInterfaces.version",
"lib/net462/Sentry.dll",
"lib/net462/Sentry.license",
"lib/net462/Sentry.version",
"lib/net462/System.Buffers.dll",
"lib/net462/System.Buffers.license",
"lib/net462/System.Buffers.version",
"lib/net462/System.Collections.Immutable.dll",
"lib/net462/System.Collections.Immutable.license",
"lib/net462/System.Collections.Immutable.version",
"lib/net462/System.Memory.dll",
"lib/net462/System.Memory.license",
"lib/net462/System.Memory.version",
"lib/net462/System.Numerics.Vectors.dll",
"lib/net462/System.Numerics.Vectors.license",
"lib/net462/System.Numerics.Vectors.version",
"lib/net462/System.Reflection.Metadata.dll",
"lib/net462/System.Reflection.Metadata.license",
"lib/net462/System.Reflection.Metadata.version",
"lib/net462/System.Runtime.CompilerServices.Unsafe.dll",
"lib/net462/System.Runtime.CompilerServices.Unsafe.license",
"lib/net462/System.Runtime.CompilerServices.Unsafe.version",
"lib/net462/System.Text.Encodings.Web.dll",
"lib/net462/System.Text.Encodings.Web.license",
"lib/net462/System.Text.Encodings.Web.version",
"lib/net462/System.Text.Json.dll",
"lib/net462/System.Text.Json.license",
"lib/net462/System.Text.Json.version",
"lib/net462/System.Threading.Tasks.Extensions.dll",
"lib/net462/System.Threading.Tasks.Extensions.license",
"lib/net462/System.Threading.Tasks.Extensions.version",
"lib/net462/System.ValueTuple.dll",
"lib/net462/System.ValueTuple.license",
"lib/net462/System.ValueTuple.version",
"lib/net6.0/Sentry.dll",
"lib/net6.0/Sentry.license",
"lib/net6.0/Sentry.version",
"lib/net8.0/Sentry.dll",
"lib/net8.0/Sentry.license",
"lib/net8.0/Sentry.version",
"private/DiagnosticLogger.ps1",
"private/EventUpdater.ps1",
"private/Get-CurrentOptions.ps1",
"private/Get-SentryAssembliesDirectory.ps1",
"private/ScopeIntegration.ps1",
"private/SentryEventProcessor.cs",
"private/SentryEventProcessor.ps1",
"private/StackTraceProcessor.ps1",
"public/Add-SentryBreadcrumb.ps1",
"public/Edit-SentryScope.ps1",
"public/Invoke-WithSentry.ps1",
"public/Out-Sentry.ps1",
"public/Start-Sentry.ps1",
"public/Start-SentryTransaction.ps1",
"public/Stop-Sentry.ps1",
]);
});
});
});
Loading