forked from JayPNanduri/ast-cli-javascript-wrapper-jay
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCxInstallerTest.test.ts
More file actions
170 lines (147 loc) · 7.81 KB
/
CxInstallerTest.test.ts
File metadata and controls
170 lines (147 loc) · 7.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { CxInstaller } from "../main/osinstaller/CxInstaller";
import { anyString, mock, instance, when, verify } from "ts-mockito";
import { AstClient } from "../main/client/AstClient";
import * as fs from "fs";
import * as crypto from "crypto";
// Mock AstClient and set up an instance from it
const astClientMock = mock(AstClient);
const astClientInstance = instance(astClientMock);
// Create CxInstaller instances with the mocked AstClient
const cxInstallerLinux = new CxInstaller("linux", astClientInstance);
const cxInstallerMac = new CxInstaller("darwin", astClientInstance);
const cxInstallerWindows = new CxInstaller("win32", astClientInstance);
describe("CxInstaller cases", () => {
it('CxInstaller getDownloadURL Linux Successful case', async () => {
const { url } = await cxInstallerLinux.getDownloadURL();
const { version } = await cxInstallerLinux.readASTCLIVersion();
const architecture = getArchitecture(cxInstallerLinux.getPlatform());
expect(url).toBe(`https://download.checkmarx.com/CxOne/CLI/${version}/ast-cli_${version}_linux_${architecture}.tar.gz`);
});
it('CxInstaller getDownloadURL Mac Successful case', async () => {
const { url } = await cxInstallerMac.getDownloadURL();
const { version } = await cxInstallerLinux.readASTCLIVersion();
const architecture = getArchitecture(cxInstallerMac.getPlatform());
expect(url).toBe(`https://download.checkmarx.com/CxOne/CLI/${version}/ast-cli_${version}_darwin_${architecture}.tar.gz`);
});
it('CxInstaller getDownloadURL Windows Successful case', async () => {
const { url } = await cxInstallerWindows.getDownloadURL();
const { version } = await cxInstallerLinux.readASTCLIVersion();
const architecture = getArchitecture(cxInstallerWindows.getPlatform());
expect(url).toBe(`https://download.checkmarx.com/CxOne/CLI/${version}/ast-cli_${version}_windows_${architecture}.zip`);
});
});
describe("CxInstaller getExecutablePath cases", () => {
it('CxInstaller getExecutablePath Linux Successful case', () => {
const executablePath = cxInstallerLinux.getExecutablePath();
expect(executablePath).toContain(`src/main/wrapper/resources/cx`);
});
it('CxInstaller getExecutablePath Mac Successful case', () => {
const executablePath = cxInstallerMac.getExecutablePath();
expect(executablePath).toContain(`src/main/wrapper/resources/cx`);
});
it('CxInstaller getExecutablePath Windows Successful case', () => {
const executablePath = cxInstallerWindows.getExecutablePath();
expect(executablePath).toContain(`src/main/wrapper/resources/cx.exe`);
});
});
describe("CxInstaller checkExecutableExists cases", () => {
beforeAll(async () => {
when(astClientMock.downloadFile(anyString(), anyString())).thenResolve(); // Set up mock behavior here
await cxInstallerWindows.downloadIfNotInstalledCLI();
});
it('CxInstaller checkExecutableExists Windows Successful case', () => {
verify(astClientMock.downloadFile(anyString(), anyString())).called();
});
});
describe("CxInstaller checksum verification cases", () => {
let localMock: AstClient;
let localInstance: AstClient;
let localLinux: CxInstaller;
let localMac: CxInstaller;
let exitSpy: jest.SpyInstance;
beforeEach(() => {
localMock = mock(AstClient);
localInstance = instance(localMock);
localLinux = new CxInstaller('linux', localInstance);
localMac = new CxInstaller('darwin', localInstance);
exitSpy = jest.spyOn(process, 'exit').mockImplementation(() => undefined as never);
});
afterEach(() => {
exitSpy.mockRestore();
delete process.env.CX_CLI_LOCATION;
});
it('CxInstaller checksum match does not call process.exit (linux)', async () => {
const content = Buffer.from('test-binary-linux');
const hash = crypto.createHash('sha256').update(content).digest('hex');
jest.spyOn(localLinux as any, 'readASTCLIVersion').mockResolvedValue({ version: '2.3.48', checksum: hash });
when(localMock.downloadFile(anyString(), anyString())).thenCall((_url: string, dest: string) => {
fs.writeFileSync(dest, content);
return Promise.resolve();
});
await localLinux.downloadIfNotInstalledCLI();
expect(exitSpy).not.toHaveBeenCalled();
});
it('CxInstaller checksum mismatch calls process.exit(1) (linux)', async () => {
jest.spyOn(localLinux as any, 'readASTCLIVersion').mockResolvedValue({ version: '2.3.48', checksum: 'deadbeef'.repeat(8) });
when(localMock.downloadFile(anyString(), anyString())).thenCall((_url: string, dest: string) => {
fs.writeFileSync(dest, Buffer.from('tampered'));
return Promise.resolve();
});
await localLinux.downloadIfNotInstalledCLI();
expect(exitSpy).toHaveBeenCalledWith(1);
});
it('CxInstaller checksum match does not call process.exit (darwin)', async () => {
const content = Buffer.from('test-binary-darwin');
const hash = crypto.createHash('sha256').update(content).digest('hex');
jest.spyOn(localMac as any, 'readASTCLIVersion').mockResolvedValue({ version: '2.3.48', checksum: hash });
when(localMock.downloadFile(anyString(), anyString())).thenCall((_url: string, dest: string) => {
fs.writeFileSync(dest, content);
return Promise.resolve();
});
await localMac.downloadIfNotInstalledCLI();
expect(exitSpy).not.toHaveBeenCalled();
});
it('CxInstaller checksum match does not call process.exit for custom version', async () => {
const content = Buffer.from('test-binary-custom-version');
const hash = crypto.createHash('sha256').update(content).digest('hex');
jest.spyOn(localLinux as any, 'readASTCLIVersion').mockResolvedValue({ version: '9.9.99', checksum: hash });
when(localMock.downloadFile(anyString(), anyString())).thenCall((_url: string, dest: string) => {
fs.writeFileSync(dest, content);
return Promise.resolve();
});
await localLinux.downloadIfNotInstalledCLI();
expect(exitSpy).not.toHaveBeenCalled();
});
it('CxInstaller checksum mismatch calls process.exit(1) for custom version', async () => {
jest.spyOn(localLinux as any, 'readASTCLIVersion').mockResolvedValue({ version: '9.9.99', checksum: 'deadbeef'.repeat(8) });
when(localMock.downloadFile(anyString(), anyString())).thenCall((_url: string, dest: string) => {
fs.writeFileSync(dest, Buffer.from('tampered'));
return Promise.resolve();
});
await localLinux.downloadIfNotInstalledCLI();
expect(exitSpy).toHaveBeenCalledWith(1);
});
it('CxInstaller null checksum skips verification', async () => {
jest.spyOn(localLinux as any, 'readASTCLIVersion').mockResolvedValue({ version: '9.9.99', checksum: null });
when(localMock.downloadFile(anyString(), anyString())).thenResolve();
await localLinux.downloadIfNotInstalledCLI();
expect(exitSpy).not.toHaveBeenCalled();
});
it('CxInstaller CX_CLI_LOCATION skips checksum verification', async () => {
process.env.CX_CLI_LOCATION = 'https://internal.example.com/cli';
jest.spyOn(localLinux as any, 'readASTCLIVersion').mockResolvedValue({ version: '2.3.48', checksum: 'irrelevant' });
when(localMock.downloadFile(anyString(), anyString())).thenResolve();
await localLinux.downloadIfNotInstalledCLI();
expect(exitSpy).not.toHaveBeenCalled();
});
});
function getArchitecture(platform: string): string {
if (platform !== 'linux') {
return 'x64';
}
const archMap: Record<string, string> = {
'arm64': 'arm64',
'arm': 'armv6'
};
return archMap[process.arch] || 'x64';
}