diff --git a/ts-binary-wrapper/test/unit/common.spec.ts b/ts-binary-wrapper/test/unit/common.spec.ts index 05bcf0a2d1..42d5eccfae 100644 --- a/ts-binary-wrapper/test/unit/common.spec.ts +++ b/ts-binary-wrapper/test/unit/common.spec.ts @@ -250,6 +250,47 @@ describe('Testing binary bootstrapper', () => { fs.unlinkSync(shasumFile); fs.unlinkSync(config.getLocalLocation()); }); + it('downloadWithBackup() succesfull', async () => { + const binaryName = 'snyk-macos'; + const shafileExtension = '.sha256'; + const config = new common.WrapperConfiguration('1.1080.0', binaryName, ''); + const shasumFile = + config.getLocalLocation() + Math.random() + shafileExtension; + + // download the shasum first, here we don't expect a shasum comparison + const shasumDownload = await common.downloadWithBackup( + 'https://notdownloads.snyk.io/cli/v1.1080.0/snyk-macos.sha256', + config.getDownloadLocation() + shafileExtension, + shasumFile, + '', + ); + expect(shasumDownload).toBeUndefined(); + expect(fs.existsSync(shasumFile)).toBeTruthy(); + const expectedShasum = common.getCurrentSha256sum(binaryName, shasumFile); + + // download binary next and use previously downloaded shasum to check validity + const binaryDownload = await common.downloadExecutable( + config.getDownloadLocation(), + config.getLocalLocation(), + expectedShasum, + ); + expect(binaryDownload).toBeUndefined(); + expect(fs.existsSync(config.getLocalLocation())).toBeTruthy(); + + const stats = fs.statSync(config.getLocalLocation()); + expect(stats.mode).toEqual(0o100755); + + try { + // check if the binary is executable + fs.accessSync(config.getLocalLocation(), fs.constants.X_OK); + } catch { + // execution of binary not possible + expect(false).toBeTruthy(); + } + + fs.unlinkSync(shasumFile); + fs.unlinkSync(config.getLocalLocation()); + }); it('downloadExecutable() fails due to incorrect shasum', async () => { const binaryName = 'snyk-macos';