Skip to content

Commit d8aea0c

Browse files
committed
Modify the tests
1 parent d07bfe9 commit d8aea0c

File tree

7 files changed

+67
-42
lines changed

7 files changed

+67
-42
lines changed

src/packageManager/downloadAndInstallPackages.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export async function downloadAndInstallPackages(packages: AbsolutePathPackage[]
2929
count++;
3030
let buffer = await DownloadFile(pkg.description, eventStream, provider, pkg.url, pkg.fallbackUrl);
3131
eventStream.post(new DownloadValidation(pkg.description));
32-
if (await isValidDownload(buffer, pkg.integrity)) {
32+
if (isValidDownload(buffer, pkg.integrity)) {
3333
eventStream.post(new IntegrityCheckSuccess());
3434
await InstallZip(buffer, pkg.description, pkg.installPath, pkg.binaries, eventStream);
3535
installationStage = 'touchLockFile';

src/packageManager/isValidDownload.ts

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,19 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
import * as stream from "stream";
76
import * as crypto from "crypto";
87

9-
const hash = crypto.createHash('sha256');
10-
11-
export async function isValidDownload(buffer: Buffer, integrity: string): Promise<boolean> {
8+
export function isValidDownload(buffer: Buffer, integrity: string): boolean {
9+
let hash = crypto.createHash('sha256');
1210
if (integrity && integrity.length > 0) {
13-
return new Promise<boolean>((resolve) => {
14-
let value: string;
15-
var bufferStream = new stream.PassThrough();
16-
bufferStream.end(buffer);
17-
bufferStream.on("readable", () => {
18-
const data = bufferStream.read();
19-
if (data) {
20-
hash.update(data);
21-
}
22-
});
23-
bufferStream.on('end', () => {
24-
value = hash.digest('hex');
25-
if (value == integrity) {
26-
resolve(true);
27-
}
28-
else {
29-
resolve(false);
30-
}
31-
});
32-
33-
bufferStream.on("error", () => {
34-
//if the bufferstream errored
35-
resolve(false);
36-
});
37-
});
11+
hash.update(buffer);
12+
let value = hash.digest('hex');
13+
if (value.toUpperCase() == integrity.toUpperCase()) {
14+
return true;
15+
}
16+
else {
17+
return false;
18+
}
3819
}
3920

4021
// no integrity has been specified

test/unitTests/OmnisharpDownloader.test.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import MockHttpsServer from "./testAssets/MockHttpsServer";
1414
import {expect} from 'chai';
1515
import TestZip from "./testAssets/TestZip";
1616
import { createTestFile } from "./testAssets/TestFile";
17-
import { PackageInstallation, LogPlatformInfo, DownloadStart, DownloadSizeObtained, DownloadProgress, DownloadSuccess, InstallationStart, InstallationSuccess, PackageInstallStart } from "../../src/omnisharp/loggingEvents";
17+
import { PackageInstallation, LogPlatformInfo, DownloadStart, DownloadSizeObtained, DownloadProgress, DownloadSuccess, InstallationStart, InstallationSuccess, PackageInstallStart, DownloadValidation, IntegrityCheckSuccess } from "../../src/omnisharp/loggingEvents";
1818
import TestEventBus from "./testAssets/TestEventBus";
1919
import { testPackageJSON } from "./testAssets/testAssets";
2020

@@ -59,14 +59,16 @@ suite('OmnisharpDownloader', () => {
5959
});
6060

6161
test('Events are created', async () => {
62-
let expectedSequence = [
63-
new PackageInstallation('OmniSharp Version = 1.2.3'),
64-
new LogPlatformInfo(new PlatformInformation("win32", "x86")),
62+
let expectedSequence = [
63+
new PackageInstallation('OmniSharp Version = 1.2.3'),
64+
new LogPlatformInfo(new PlatformInformation("win32", "x86")),
6565
new PackageInstallStart(),
66-
new DownloadStart('OmniSharp for Windows (.NET 4.6 / x86), Version = 1.2.3'),
67-
new DownloadSizeObtained(testZip.size),
68-
new DownloadProgress(100, 'OmniSharp for Windows (.NET 4.6 / x86), Version = 1.2.3'),
69-
new DownloadSuccess(' Done!'),
66+
new DownloadStart('OmniSharp for Windows (.NET 4.6 / x86), Version = 1.2.3'),
67+
new DownloadSizeObtained(testZip.size),
68+
new DownloadProgress(100, 'OmniSharp for Windows (.NET 4.6 / x86), Version = 1.2.3'),
69+
new DownloadSuccess(' Done!'),
70+
new DownloadValidation('OmniSharp for Windows (.NET 4.6 / x86), Version = 1.2.3'),
71+
new IntegrityCheckSuccess(),
7072
new InstallationStart('OmniSharp for Windows (.NET 4.6 / x86), Version = 1.2.3'),
7173
new InstallationSuccess()
7274
];

test/unitTests/Packages/ZipInstaller.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ suite('ZipInstaller', () => {
7676
});
7777

7878
test('Error is thrown when the buffer contains an invalid zip', async () => {
79-
expect(InstallZip(new Buffer("My file", "utf8"), "Text File", installationPath, [], eventStream)).to.be.rejected;
79+
expect(InstallZip(Buffer.from("My file", "utf8"), "Text File", installationPath, [], eventStream)).to.be.rejected;
8080
});
8181

8282
test('Error event is created when the buffer contains an invalid zip', async () => {
8383
try {
84-
await InstallZip(new Buffer("some content", "utf8"), "Text File", installationPath, [], eventStream);
84+
await InstallZip(Buffer.from("some content", "utf8"), "Text File", installationPath, [], eventStream);
8585
}
8686
catch{
8787
let eventSequence: BaseEvent[] = [

test/unitTests/Packages/downloadAndInstallPackages.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import TestZip from '../testAssets/TestZip';
1212
import { downloadAndInstallPackages } from '../../../src/packageManager/downloadAndInstallPackages';
1313
import NetworkSettings from '../../../src/NetworkSettings';
1414
import { EventStream } from '../../../src/EventStream';
15-
import { DownloadStart, DownloadSizeObtained, DownloadProgress, DownloadSuccess, InstallationStart, PackageInstallStart } from '../../../src/omnisharp/loggingEvents';
15+
import { DownloadStart, DownloadSizeObtained, DownloadProgress, DownloadSuccess, InstallationStart, PackageInstallStart, DownloadValidation, IntegrityCheckSuccess } from '../../../src/omnisharp/loggingEvents';
1616
import MockHttpsServer from '../testAssets/MockHttpsServer';
1717
import { createTestFile } from '../testAssets/TestFile';
1818
import TestEventBus from '../testAssets/TestEventBus';
@@ -91,10 +91,13 @@ suite(`${downloadAndInstallPackages.name}`, () => {
9191
new DownloadSizeObtained(testZip.size),
9292
new DownloadProgress(100, packageDescription),
9393
new DownloadSuccess(' Done!'),
94+
new DownloadValidation(packageDescription),
95+
new IntegrityCheckSuccess(),
9496
new InstallationStart(packageDescription)
9597
];
9698

9799
await downloadAndInstallPackages(downloadablePackage, networkSettingsProvider, eventStream);
100+
console.log(eventBus.getEvents());
98101
expect(eventBus.getEvents()).to.be.deep.equal(eventsSequence);
99102
});
100103
});
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
import { isValidDownload } from "../../../src/packageManager/isValidDownload";
7+
import { createTestFile } from "../testAssets/TestFile";
8+
import TestZip from "../testAssets/TestZip";
9+
import * as chai from "chai";
10+
11+
chai.should();
12+
const expect = chai.expect;
13+
14+
suite(`${isValidDownload.name}`, () => {
15+
const files = [
16+
createTestFile("file1", "file1.txt"),
17+
createTestFile("file2", "file2.txt")
18+
];
19+
20+
let testZip: TestZip;
21+
22+
setup(async() => {
23+
testZip = await TestZip.createTestZipAsync(...files);
24+
});
25+
26+
test('Returns false for non-matching integrity', async () => {
27+
let result = await isValidDownload(testZip.buffer, "inValidIntegrity");
28+
expect(result).to.be.false;
29+
});
30+
31+
test('Returns true for matching integrity', async () => {
32+
let result = await isValidDownload(testZip.buffer, "212785b9cf15888785ed55a9357b4c4e29d0acca6a978ccb1df7cc8ee7423071");
33+
expect(result).to.be.true;
34+
});
35+
});

test/unitTests/logging/CsharpLoggerObserver.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,5 +175,9 @@ suite("CsharpLoggerObserver", () => {
175175
expect(logOutput).to.contain(description);
176176
});
177177

178-
test(`${Event}`)
178+
test(`${Event.IntegrityCheckSuccess.name}: Some message is logged`, () => {
179+
let event = new Event.IntegrityCheckSuccess();
180+
observer.post(event);
181+
expect(logOutput).to.not.be.empty;
182+
});
179183
});

0 commit comments

Comments
 (0)