diff --git a/cxAstScan/test/_suite.ts b/cxAstScan/test/_suite.ts index d1844b4c..134cda23 100644 --- a/cxAstScan/test/_suite.ts +++ b/cxAstScan/test/_suite.ts @@ -1,6 +1,7 @@ import * as path from 'path'; import * as ttm from 'azure-pipelines-task-lib/mock-test'; import * as assert from 'assert'; +import './cleanup/_suite'; const nodeVersion = 20; describe('Task runner test', function () { @@ -111,4 +112,23 @@ describe('Task runner test', function () { true, "should display cleanup message: Log file not created. Task ended successfully."); }); + + it('should handle cleanup error cases', async function () { + this.timeout(3000000); + const tp = path.join(__dirname, 'failure_cleanup_cases.js'); + const tr: ttm.MockTestRunner = new ttm.MockTestRunner(tp); + await tr.runAsync(nodeVersion); + + console.log(tr.stdout); + console.log(tr.stderr); + + // Verify error handling for file permissions + assert.strictEqual(tr.stdout.indexOf('No permissions to read log file') >= 0, true); + + // Verify error handling for file deletion + assert.strictEqual(tr.stdout.indexOf('Unable to delete log file.') >= 0, true); + + // Verify error handling for authentication + assert.strictEqual(tr.stdout.indexOf('Error creating scan:') >= 0, true); + }); }); diff --git a/cxAstScan/test/cleanup/_suite.ts b/cxAstScan/test/cleanup/_suite.ts new file mode 100644 index 00000000..84d3da26 --- /dev/null +++ b/cxAstScan/test/cleanup/_suite.ts @@ -0,0 +1,37 @@ +import * as path from 'path'; +import * as ttm from 'azure-pipelines-task-lib/mock-test'; +import * as assert from 'assert'; + +const nodeVersion = 20; + +describe('Cleanup Tests', function () { + it('should handle file permission errors', async function () { + this.timeout(3000000); + const tp = path.join(__dirname, 'failure_permissions.js'); + const tr: ttm.MockTestRunner = new ttm.MockTestRunner(tp); + await tr.runAsync(nodeVersion); + + console.log(tr.stdout); + assert.strictEqual(tr.stdout.indexOf('No permissions to read log file') >= 0, true); + }); + + it('should handle file deletion errors', async function () { + this.timeout(3000000); + const tp = path.join(__dirname, 'failure_deletion.js'); + const tr: ttm.MockTestRunner = new ttm.MockTestRunner(tp); + await tr.runAsync(nodeVersion); + + console.log(tr.stdout); + assert.strictEqual(tr.stdout.indexOf('Unable to delete log file.') >= 0, true); + }); + + it('should handle authentication errors', async function () { + this.timeout(3000000); + const tp = path.join(__dirname, 'failure_auth.js'); + const tr: ttm.MockTestRunner = new ttm.MockTestRunner(tp); + await tr.runAsync(nodeVersion); + + console.log(tr.stdout); + assert.strictEqual(tr.stdout.indexOf('Error creating scan:') >= 0, true); + }); +}); \ No newline at end of file diff --git a/cxAstScan/test/cleanup/failure_auth.ts b/cxAstScan/test/cleanup/failure_auth.ts new file mode 100644 index 00000000..59de82ac --- /dev/null +++ b/cxAstScan/test/cleanup/failure_auth.ts @@ -0,0 +1,18 @@ +import tmrm = require('azure-pipelines-task-lib/mock-run'); +import path = require('path'); +import process from "process"; + +process.env['Build_BuildId'] = 'test_build_id'; +process.env["AGENT_JOBSTATUS"] = 'Canceled'; + +const taskPath = path.join(__dirname, '..', '..', 'cleanup.js'); +const tmr: tmrm.TaskMockRunner = new tmrm.TaskMockRunner(taskPath); + +tmr.setInput("tenantName", "test-tenant"); +tmr.setInput("CheckmarxService", "cxauth"); + +// Mock authentication failure +tmr.registerMockExport('getEndpointUrl', () => { return ""; }); +tmr.registerMockExport('getEndpointAuthorizationParameter', () => { return ""; }); + +tmr.run(); \ No newline at end of file diff --git a/cxAstScan/test/cleanup/failure_deletion.ts b/cxAstScan/test/cleanup/failure_deletion.ts new file mode 100644 index 00000000..b2c9610f --- /dev/null +++ b/cxAstScan/test/cleanup/failure_deletion.ts @@ -0,0 +1,26 @@ +import tmrm = require('azure-pipelines-task-lib/mock-run'); +import path = require('path'); +import process from "process"; +import fs from 'fs'; + +process.env['Build_BuildId'] = 'test_build_id'; +process.env["AGENT_JOBSTATUS"] = 'Canceled'; + +const taskPath = path.join(__dirname, '..', '..', 'cleanup.js'); +const tmr: tmrm.TaskMockRunner = new tmrm.TaskMockRunner(taskPath); + +tmr.setInput("tenantName", "test-tenant"); +tmr.setInput("CheckmarxService", "cxauth"); + +// Mock fs module for deletion error +tmr.registerMock('fs', { + ...fs, + promises: { + readFile: async () => JSON.stringify({ "ID": "test-scan-id" }), + unlink: async () => { + throw new Error('Mock file deletion error'); + } + } +}); + +tmr.run(); \ No newline at end of file diff --git a/cxAstScan/test/cleanup/failure_permissions.ts b/cxAstScan/test/cleanup/failure_permissions.ts new file mode 100644 index 00000000..765a2e94 --- /dev/null +++ b/cxAstScan/test/cleanup/failure_permissions.ts @@ -0,0 +1,33 @@ +import tmrm = require('azure-pipelines-task-lib/mock-run'); +import path = require('path'); +import process from "process"; +import fs from 'fs'; + +process.env['Build_BuildId'] = 'test_build_id'; +process.env["AGENT_JOBSTATUS"] = 'Canceled'; + +const taskPath = path.join(__dirname, '..', '..', 'cleanup.js'); +const tmr: tmrm.TaskMockRunner = new tmrm.TaskMockRunner(taskPath); + +tmr.setInput("tenantName", "test-tenant"); +tmr.setInput("CheckmarxService", "cxauth"); + +// Mock temp directory with invalid path +tmr.registerMockExport('getVariable', (name: string) => { + if (name === 'Agent.TempDirectory') return '/invalid/path'; + if (name === 'AGENT_JOBSTATUS') return 'Canceled'; + if (name === 'Build.BuildId') return 'test_build_id'; + return ''; +}); + +// Mock fs module for permission error +tmr.registerMock('fs', { + ...fs, + promises: { + readFile: async () => { + throw { code: 'EACCES' }; // Simulate permission error + } + } +}); + +tmr.run(); \ No newline at end of file diff --git a/cxAstScan/test/failure_cleanup_cases.ts b/cxAstScan/test/failure_cleanup_cases.ts new file mode 100644 index 00000000..b64d7b84 --- /dev/null +++ b/cxAstScan/test/failure_cleanup_cases.ts @@ -0,0 +1,49 @@ +import tmrm = require('azure-pipelines-task-lib/mock-run'); +import path = require('path'); +import process from "process"; +import fs from 'fs'; + +// Mock environment variables +process.env['Build_BuildId'] = 'test_build_id'; +process.env["AGENT_JOBSTATUS"] = 'Canceled'; + +const taskPath = path.join(__dirname, '..', 'cleanup.js'); +const tmr: tmrm.TaskMockRunner = new tmrm.TaskMockRunner(taskPath); + +// Setup basic mock environment +tmr.setInput("tenantName", "test-tenant"); +tmr.setInput("CheckmarxService", "cxauth"); + +// Mock authentication failure case +tmr.registerMockExport('getEndpointUrl', () => { return ""; }); +tmr.registerMockExport('getEndpointAuthorizationParameter', () => { return ""; }); + +// Mock temp directory with invalid path to trigger file access errors +tmr.registerMockExport('getVariable', (name: string) => { + if (name === 'Agent.TempDirectory') return '/invalid/path'; + if (name === 'AGENT_JOBSTATUS') return 'Canceled'; + if (name === 'Build.BuildId') return 'test_build_id'; + return ''; +}); + +// Create a mock log file with invalid scan ID +const mockLogContent = JSON.stringify({ "ID": "invalid-scan-id" }); +const mockLogPath = path.join('/invalid/path', 'CxLog_test_build_id.txt'); + +// Mock fs module +tmr.registerMock('fs', { + ...fs, + promises: { + readFile: async (path: string) => { + if (path === mockLogPath) { + throw { code: 'EACCES' }; // Simulate permission error + } + return mockLogContent; + }, + unlink: async (path: string) => { + throw new Error('Mock file deletion error'); + } + } +}); + +tmr.run(); \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 67a9957a..5a6e358f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -20,6 +20,7 @@ "@typescript-eslint/parser": "^5.62.0", "eslint": "^8.57.0", "mocha": "10.7.0", + "ts-node": "^10.9.2", "typescript": "^5.6.3" }, "engines": { @@ -425,6 +426,28 @@ "node": ">=6.9.0" } }, + "node_modules/@cspotcode/source-map-support": { + "version": "0.8.1", + "resolved": "https://registry.npmjs.org/@cspotcode/source-map-support/-/source-map-support-0.8.1.tgz", + "integrity": "sha512-IchNf6dN4tHoMFIn/7OE8LWZ19Y6q/67Bmf6vnGREv8RSbBVb9LPJxEcnwrcwX6ixSvaiGoomAUvu4YSxXrVgw==", + "dev": true, + "dependencies": { + "@jridgewell/trace-mapping": "0.3.9" + }, + "engines": { + "node": ">=12" + } + }, + "node_modules/@cspotcode/source-map-support/node_modules/@jridgewell/trace-mapping": { + "version": "0.3.9", + "resolved": "https://registry.npmjs.org/@jridgewell/trace-mapping/-/trace-mapping-0.3.9.tgz", + "integrity": "sha512-3Belt6tdc8bPgAtbcmdtNJlirVoTmEb5e2gC94PnkwEW9jI6CAHUeoG85tjWP5WquqfavoMtMwiG4P926ZKKuQ==", + "dev": true, + "dependencies": { + "@jridgewell/resolve-uri": "^3.0.3", + "@jridgewell/sourcemap-codec": "^1.4.10" + } + }, "node_modules/@eslint-community/eslint-utils": { "version": "4.4.0", "resolved": "https://registry.npmjs.org/@eslint-community/eslint-utils/-/eslint-utils-4.4.0.tgz", @@ -699,6 +722,30 @@ "node": ">= 8" } }, + "node_modules/@tsconfig/node10": { + "version": "1.0.11", + "resolved": "https://registry.npmjs.org/@tsconfig/node10/-/node10-1.0.11.tgz", + "integrity": "sha512-DcRjDCujK/kCk/cUe8Xz8ZSpm8mS3mNNpta+jGCA6USEDfktlNvm1+IuZ9eTcDbNk41BHwpHHeW+N1lKCz4zOw==", + "dev": true + }, + "node_modules/@tsconfig/node12": { + "version": "1.0.11", + "resolved": "https://registry.npmjs.org/@tsconfig/node12/-/node12-1.0.11.tgz", + "integrity": "sha512-cqefuRsh12pWyGsIoBKJA9luFu3mRxCA+ORZvA4ktLSzIuCUtWVxGIuXigEwO5/ywWFMZ2QEGKWvkZG1zDMTag==", + "dev": true + }, + "node_modules/@tsconfig/node14": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/@tsconfig/node14/-/node14-1.0.3.tgz", + "integrity": "sha512-ysT8mhdixWK6Hw3i1V2AeRqZ5WfXg1G43mqoYlM2nc6388Fq5jcXyr5mRsqViLx/GJYdoL0bfXD8nmF+Zn/Iow==", + "dev": true + }, + "node_modules/@tsconfig/node16": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/@tsconfig/node16/-/node16-1.0.4.tgz", + "integrity": "sha512-vxhUy4J8lyeyinH7Azl1pdd43GJhZH/tP2weN8TntQblOY+A0XbT8DJk1/oCPuOOyg/Ja757rG0CgHcWC8OfMA==", + "dev": true + }, "node_modules/@types/jquery": { "version": "3.5.29", "resolved": "https://registry.npmjs.org/@types/jquery/-/jquery-3.5.29.tgz", @@ -1010,6 +1057,18 @@ "acorn": "^6.0.0 || ^7.0.0 || ^8.0.0" } }, + "node_modules/acorn-walk": { + "version": "8.3.4", + "resolved": "https://registry.npmjs.org/acorn-walk/-/acorn-walk-8.3.4.tgz", + "integrity": "sha512-ueEepnujpqee2o5aIYnvHU6C0A42MNdsIDeqy5BydrkuC5R1ZuUFnm27EeFJGoEHJQgn3uleRvmTXaJgfXbt4g==", + "dev": true, + "dependencies": { + "acorn": "^8.11.0" + }, + "engines": { + "node": ">=0.4.0" + } + }, "node_modules/aggregate-error": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/aggregate-error/-/aggregate-error-3.1.0.tgz", @@ -1098,6 +1157,12 @@ "resolved": "https://registry.npmjs.org/archy/-/archy-1.0.0.tgz", "integrity": "sha512-Xg+9RwCg/0p32teKdGMPTPnVXKD0w3DfHnFTficozsAgsvq2XenPJq/MYpzzQ/v8zrOyJn6Ds39VA4JIDwFfqw==" }, + "node_modules/arg": { + "version": "4.1.3", + "resolved": "https://registry.npmjs.org/arg/-/arg-4.1.3.tgz", + "integrity": "sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==", + "dev": true + }, "node_modules/argparse": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz", @@ -1344,6 +1409,12 @@ "resolved": "https://registry.npmjs.org/convert-source-map/-/convert-source-map-1.9.0.tgz", "integrity": "sha512-ASFBup0Mz1uyiIjANan1jzLQami9z1PoYSZCiiYW2FczPbenXc45FZdBZLzOT+r6+iciuEModtmCti+hjaAk0A==" }, + "node_modules/create-require": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/create-require/-/create-require-1.1.1.tgz", + "integrity": "sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==", + "dev": true + }, "node_modules/cross-spawn": { "version": "7.0.5", "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.5.tgz", @@ -2365,6 +2436,12 @@ "semver": "bin/semver.js" } }, + "node_modules/make-error": { + "version": "1.3.6", + "resolved": "https://registry.npmjs.org/make-error/-/make-error-1.3.6.tgz", + "integrity": "sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==", + "dev": true + }, "node_modules/merge2": { "version": "1.4.1", "resolved": "https://registry.npmjs.org/merge2/-/merge2-1.4.1.tgz", @@ -3330,6 +3407,58 @@ "node": ">=8.0" } }, + "node_modules/ts-node": { + "version": "10.9.2", + "resolved": "https://registry.npmjs.org/ts-node/-/ts-node-10.9.2.tgz", + "integrity": "sha512-f0FFpIdcHgn8zcPSbf1dRevwt047YMnaiJM3u2w2RewrB+fob/zePZcrOyQoLMMO7aBIddLcQIEK5dYjkLnGrQ==", + "dev": true, + "dependencies": { + "@cspotcode/source-map-support": "^0.8.0", + "@tsconfig/node10": "^1.0.7", + "@tsconfig/node12": "^1.0.7", + "@tsconfig/node14": "^1.0.0", + "@tsconfig/node16": "^1.0.2", + "acorn": "^8.4.1", + "acorn-walk": "^8.1.1", + "arg": "^4.1.0", + "create-require": "^1.1.0", + "diff": "^4.0.1", + "make-error": "^1.1.1", + "v8-compile-cache-lib": "^3.0.1", + "yn": "3.1.1" + }, + "bin": { + "ts-node": "dist/bin.js", + "ts-node-cwd": "dist/bin-cwd.js", + "ts-node-esm": "dist/bin-esm.js", + "ts-node-script": "dist/bin-script.js", + "ts-node-transpile-only": "dist/bin-transpile.js", + "ts-script": "dist/bin-script-deprecated.js" + }, + "peerDependencies": { + "@swc/core": ">=1.2.50", + "@swc/wasm": ">=1.2.50", + "@types/node": "*", + "typescript": ">=2.7" + }, + "peerDependenciesMeta": { + "@swc/core": { + "optional": true + }, + "@swc/wasm": { + "optional": true + } + } + }, + "node_modules/ts-node/node_modules/diff": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/diff/-/diff-4.0.2.tgz", + "integrity": "sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==", + "dev": true, + "engines": { + "node": ">=0.3.1" + } + }, "node_modules/tslib": { "version": "1.14.1", "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.14.1.tgz", @@ -3455,6 +3584,12 @@ "uuid": "dist/bin/uuid" } }, + "node_modules/v8-compile-cache-lib": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz", + "integrity": "sha512-wa7YjyUGfNZngI/vtK0UHAN+lgDCxBPCylVXGp0zu59Fz5aiGtNXaq3DhIov063MorB+VfufLh3JlF2KdTK3xg==", + "dev": true + }, "node_modules/vss-web-extension-sdk": { "version": "5.141.0", "resolved": "https://registry.npmjs.org/vss-web-extension-sdk/-/vss-web-extension-sdk-5.141.0.tgz", @@ -3584,6 +3719,15 @@ "node": ">=10" } }, + "node_modules/yn": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/yn/-/yn-3.1.1.tgz", + "integrity": "sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==", + "dev": true, + "engines": { + "node": ">=6" + } + }, "node_modules/yocto-queue": { "version": "0.1.0", "resolved": "https://registry.npmjs.org/yocto-queue/-/yocto-queue-0.1.0.tgz", diff --git a/package.json b/package.json index d690980c..e114dccc 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ "@typescript-eslint/parser": "^5.62.0", "eslint": "^8.57.0", "mocha": "10.7.0", + "ts-node": "^10.9.2", "typescript": "^5.6.3" }, "publishConfig": {