Skip to content

Commit

Permalink
fix: Buffer.toString() failure when acceptance testing very large str…
Browse files Browse the repository at this point in the history
…ings
  • Loading branch information
j-luong committed Mar 20, 2024
1 parent 463f5f1 commit 1cae449
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 34 deletions.
1 change: 0 additions & 1 deletion src/cli/commands/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,6 @@ export default async function test(
}
err.code = 'VULNS';
const dataToSendNoVulns = omit(dataToSend, 'vulnerabilities');
delete dataToSendNoVulns.vulnerabilities;
err.jsonNoVulns = dataToSendNoVulns;
}

Expand Down
61 changes: 31 additions & 30 deletions test/jest/acceptance/cli-json-output.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,30 +78,6 @@ describe('test --json', () => {
});

describe('handling responses larger than 512Mb string size limit in v8', () => {
it('test --json', async () => {
const issueID = 'SNYK-JS-CXCT-535487';
const project = await createProjectFromWorkspace('fail-on/no-fixable');
const response = await project.readJSON('vulns-result.json');
const reference = response.result.issuesData[issueID].references[0];
response.result.issuesData[issueID].references = new Array(420000).fill(
reference,
);

server.setCustomResponse(response);

const { code, stdout, stderr } = await runSnykCLI(`test --json`, {
cwd: project.path(),
env,
});

if (stderr) console.log(stderr);

expect(code).toEqual(1);
expect(
JSON.parse(stdout)?.response?.result?.issuesData[issueID],
).not.toBeNull();
}, 120000);

it('container test --json', async () => {
const issueID = 'SNYK-ALPINE319-OPENSSL-6148881';
const project = await createProjectFromWorkspace(
Expand All @@ -116,21 +92,46 @@ describe('test --json', () => {
server.setCustomResponse(response);

const imageName = 'hello-world:latest';
const { code, stdout, stderr } = await runSnykCLI(
const { code, stdoutBuffer, stderrBuffer } = await runSnykCLI(
`container test --platform=linux/amd64 ${imageName} --json`,
{
cwd: project.path(),
env,
bufferOutput: true,
},
);

if (stderr) console.log(stderr);
// check for these properties in the buffer chunks
const pathString = `"path": "${imageName}"`;
let hasExpectedPathString = false;
const vulnerabilitiesString = `"vulnerabilities": [`;
let hasExpectedVulnerabilitiesString = false;

const chunkSize = 1024 * 1024 * 450; // 450 MB
if (stdoutBuffer) {
for (let i = 0; i < stdoutBuffer.length; i += chunkSize) {
const chunk = stdoutBuffer.slice(
i,
Math.min(i + chunkSize, stdoutBuffer.length),
);

if (!hasExpectedVulnerabilitiesString) {
hasExpectedVulnerabilitiesString = chunk
.toString('utf8')
.includes(vulnerabilitiesString);
}

if (!hasExpectedPathString) {
hasExpectedPathString = chunk.toString('utf8').includes(pathString);
}
}
}

if (stderrBuffer) console.log(stderrBuffer.toString('utf8'));

expect(code).toEqual(1);
expect(JSON.parse(stdout)?.path).toEqual(imageName);
expect(
JSON.parse(stdout)?.response?.result?.issuesData[issueID],
).not.toBeNull();
expect(hasExpectedVulnerabilitiesString).toBeTruthy();
expect(hasExpectedPathString).toBeTruthy();
}, 120000);
});
});
11 changes: 8 additions & 3 deletions test/jest/util/runCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type RunCommandResult = {

// bufferOutput sets the RunCommandResult stdoutBuffer and stderrBuffer
// useful if the stdout or stderr string output is too large for the v8 engine
type RunCommandOptions = SpawnOptionsWithoutStdio
type RunCommandOptions = SpawnOptionsWithoutStdio & { bufferOutput?: boolean };

const runCommand = (
command: string,
Expand Down Expand Up @@ -42,8 +42,13 @@ const runCommand = (
stderr: '',
};

result.stdout = Buffer.concat(stdout).toString('utf-8');
result.stderr = Buffer.concat(stderr).toString('utf-8');
if (options?.bufferOutput) {
result.stdoutBuffer = Buffer.concat(stdout);
result.stderrBuffer = Buffer.concat(stderr);
} else {
result.stdout = Buffer.concat(stdout).toString('utf-8');
result.stderr = Buffer.concat(stderr).toString('utf-8');
}

resolve(result);
});
Expand Down

0 comments on commit 1cae449

Please sign in to comment.