2020-05-04 20:28:01 +03:00
|
|
|
"use strict";
|
2021-07-27 19:59:59 +03:00
|
|
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
|
|
if (k2 === undefined) k2 = k;
|
2023-01-18 23:00:33 +03:00
|
|
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
|
|
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
|
|
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
|
|
}
|
|
|
|
Object.defineProperty(o, k2, desc);
|
2021-07-27 19:59:59 +03:00
|
|
|
}) : (function(o, m, k, k2) {
|
|
|
|
if (k2 === undefined) k2 = k;
|
|
|
|
o[k2] = m[k];
|
|
|
|
}));
|
|
|
|
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
|
|
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
|
|
}) : function(o, v) {
|
|
|
|
o["default"] = v;
|
|
|
|
});
|
2020-05-04 20:28:01 +03:00
|
|
|
var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
|
if (mod && mod.__esModule) return mod;
|
|
|
|
var result = {};
|
2021-07-27 19:59:59 +03:00
|
|
|
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
|
|
__setModuleDefault(result, mod);
|
2020-05-04 20:28:01 +03:00
|
|
|
return result;
|
|
|
|
};
|
2020-09-29 16:43:37 +03:00
|
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
|
|
};
|
2020-05-04 20:28:01 +03:00
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
|
const fs = __importStar(require("fs"));
|
|
|
|
const path = __importStar(require("path"));
|
2020-09-29 16:43:37 +03:00
|
|
|
const ava_1 = __importDefault(require("ava"));
|
2020-10-01 13:03:30 +03:00
|
|
|
const fingerprints = __importStar(require("./fingerprints"));
|
2020-08-17 14:42:23 +03:00
|
|
|
const logging_1 = require("./logging");
|
2020-06-22 14:12:14 +03:00
|
|
|
const testing_utils_1 = require("./testing-utils");
|
2021-06-07 14:07:56 +03:00
|
|
|
const util = __importStar(require("./util"));
|
2021-09-10 23:53:13 +03:00
|
|
|
(0, testing_utils_1.setupTests)(ava_1.default);
|
2021-06-07 14:07:56 +03:00
|
|
|
async function testHash(t, input, expectedHashes) {
|
|
|
|
await util.withTmpDir(async (tmpDir) => {
|
|
|
|
const tmpFile = path.resolve(tmpDir, "testfile");
|
|
|
|
fs.writeFileSync(tmpFile, input);
|
|
|
|
let index = 0;
|
|
|
|
const callback = function (lineNumber, hash) {
|
|
|
|
t.is(lineNumber, index + 1);
|
|
|
|
t.is(hash, expectedHashes[index]);
|
|
|
|
index++;
|
|
|
|
};
|
|
|
|
await fingerprints.hash(callback, tmpFile);
|
|
|
|
t.is(index, input.split(/\r\n|\r|\n/).length);
|
|
|
|
});
|
2020-05-04 20:28:01 +03:00
|
|
|
}
|
2021-09-10 23:53:13 +03:00
|
|
|
(0, ava_1.default)("hash", async (t) => {
|
2020-05-04 20:28:01 +03:00
|
|
|
// Try empty file
|
2021-06-07 14:07:56 +03:00
|
|
|
await testHash(t, "", ["c129715d7a2bc9a3:1"]);
|
2020-05-04 20:28:01 +03:00
|
|
|
// Try various combinations of newline characters
|
2021-06-07 14:07:56 +03:00
|
|
|
await testHash(t, " a\nb\n \t\tc\n d", [
|
2020-05-04 20:28:01 +03:00
|
|
|
"271789c17abda88f:1",
|
|
|
|
"54703d4cd895b18:1",
|
|
|
|
"180aee12dab6264:1",
|
2020-09-14 12:44:43 +03:00
|
|
|
"a23a3dc5e078b07b:1",
|
2020-05-04 20:28:01 +03:00
|
|
|
]);
|
2021-06-07 14:07:56 +03:00
|
|
|
await testHash(t, " hello; \t\nworld!!!\n\n\n \t\tGreetings\n End", [
|
2020-05-04 20:28:01 +03:00
|
|
|
"8b7cf3e952e7aeb2:1",
|
|
|
|
"b1ae1287ec4718d9:1",
|
|
|
|
"bff680108adb0fcc:1",
|
|
|
|
"c6805c5e1288b612:1",
|
|
|
|
"b86d3392aea1be30:1",
|
|
|
|
"e6ceba753e1a442:1",
|
|
|
|
]);
|
2021-06-07 14:07:56 +03:00
|
|
|
await testHash(t, " hello; \t\nworld!!!\n\n\n \t\tGreetings\n End\n", [
|
2020-05-04 20:28:01 +03:00
|
|
|
"e9496ae3ebfced30:1",
|
|
|
|
"fb7c023a8b9ccb3f:1",
|
|
|
|
"ce8ba1a563dcdaca:1",
|
|
|
|
"e20e36e16fcb0cc8:1",
|
|
|
|
"b3edc88f2938467e:1",
|
|
|
|
"c8e28b0b4002a3a0:1",
|
|
|
|
"c129715d7a2bc9a3:1",
|
|
|
|
]);
|
2021-06-07 14:07:56 +03:00
|
|
|
await testHash(t, " hello; \t\nworld!!!\r\r\r \t\tGreetings\r End\r", [
|
2020-05-04 20:28:01 +03:00
|
|
|
"e9496ae3ebfced30:1",
|
|
|
|
"fb7c023a8b9ccb3f:1",
|
|
|
|
"ce8ba1a563dcdaca:1",
|
|
|
|
"e20e36e16fcb0cc8:1",
|
|
|
|
"b3edc88f2938467e:1",
|
|
|
|
"c8e28b0b4002a3a0:1",
|
|
|
|
"c129715d7a2bc9a3:1",
|
|
|
|
]);
|
2021-06-07 14:07:56 +03:00
|
|
|
await testHash(t, " hello; \t\r\nworld!!!\r\n\r\n\r\n \t\tGreetings\r\n End\r\n", [
|
2020-05-04 20:28:01 +03:00
|
|
|
"e9496ae3ebfced30:1",
|
|
|
|
"fb7c023a8b9ccb3f:1",
|
|
|
|
"ce8ba1a563dcdaca:1",
|
|
|
|
"e20e36e16fcb0cc8:1",
|
|
|
|
"b3edc88f2938467e:1",
|
|
|
|
"c8e28b0b4002a3a0:1",
|
|
|
|
"c129715d7a2bc9a3:1",
|
|
|
|
]);
|
2021-06-07 14:07:56 +03:00
|
|
|
await testHash(t, " hello; \t\nworld!!!\r\n\n\r \t\tGreetings\r End\r\n", [
|
2020-05-04 20:28:01 +03:00
|
|
|
"e9496ae3ebfced30:1",
|
|
|
|
"fb7c023a8b9ccb3f:1",
|
|
|
|
"ce8ba1a563dcdaca:1",
|
|
|
|
"e20e36e16fcb0cc8:1",
|
|
|
|
"b3edc88f2938467e:1",
|
|
|
|
"c8e28b0b4002a3a0:1",
|
|
|
|
"c129715d7a2bc9a3:1",
|
|
|
|
]);
|
|
|
|
// Try repeating line that will generate identical hashes
|
2021-06-07 14:07:56 +03:00
|
|
|
await testHash(t, "Lorem ipsum dolor sit amet.\n".repeat(10), [
|
2020-05-04 20:28:01 +03:00
|
|
|
"a7f2ff13bc495cf2:1",
|
|
|
|
"a7f2ff13bc495cf2:2",
|
|
|
|
"a7f2ff13bc495cf2:3",
|
|
|
|
"a7f2ff13bc495cf2:4",
|
|
|
|
"a7f2ff13bc495cf2:5",
|
|
|
|
"a7f2ff13bc495cf2:6",
|
|
|
|
"a7f2ff1481e87703:1",
|
|
|
|
"a9cf91f7bbf1862b:1",
|
|
|
|
"55ec222b86bcae53:1",
|
|
|
|
"cc97dc7b1d7d8f7b:1",
|
2020-09-14 12:44:43 +03:00
|
|
|
"c129715d7a2bc9a3:1",
|
2020-05-04 20:28:01 +03:00
|
|
|
]);
|
2021-06-07 14:07:56 +03:00
|
|
|
await testHash(t, "x = 2\nx = 1\nprint(x)\nx = 3\nprint(x)\nx = 4\nprint(x)\n", [
|
2020-09-09 19:36:24 +03:00
|
|
|
"e54938cc54b302f1:1",
|
|
|
|
"bb609acbe9138d60:1",
|
|
|
|
"1131fd5871777f34:1",
|
|
|
|
"5c482a0f8b35ea28:1",
|
|
|
|
"54517377da7028d2:1",
|
|
|
|
"2c644846cb18d53e:1",
|
|
|
|
"f1b89f20de0d133:1",
|
2020-09-14 12:44:43 +03:00
|
|
|
"c129715d7a2bc9a3:1",
|
2020-09-09 19:36:24 +03:00
|
|
|
]);
|
2020-05-04 20:28:01 +03:00
|
|
|
});
|
|
|
|
function testResolveUriToFile(uri, index, artifactsURIs) {
|
2020-09-14 12:44:43 +03:00
|
|
|
const location = { uri, index };
|
2020-11-20 01:03:45 +03:00
|
|
|
const artifacts = artifactsURIs.map((artifactURI) => ({
|
|
|
|
location: { uri: artifactURI },
|
|
|
|
}));
|
2021-09-10 23:53:13 +03:00
|
|
|
return fingerprints.resolveUriToFile(location, artifacts, process.cwd(), (0, logging_1.getRunnerLogger)(true));
|
2020-05-04 20:28:01 +03:00
|
|
|
}
|
2021-09-10 23:53:13 +03:00
|
|
|
(0, ava_1.default)("resolveUriToFile", (t) => {
|
2020-05-04 20:28:01 +03:00
|
|
|
// The resolveUriToFile method checks that the file exists and is in the right directory
|
|
|
|
// so we need to give it real files to look at. We will use this file as an example.
|
|
|
|
// For this to work we require the current working directory to be a parent, but this
|
|
|
|
// should generally always be the case so this is fine.
|
2022-06-17 23:35:06 +03:00
|
|
|
const filepath = __filename.split(path.sep).join("/");
|
|
|
|
const relativeFilepath = path
|
|
|
|
.relative(process.cwd(), __filename)
|
|
|
|
.split(path.sep)
|
|
|
|
.join("/");
|
2020-05-04 20:28:01 +03:00
|
|
|
// Absolute paths are unmodified
|
|
|
|
t.is(testResolveUriToFile(filepath, undefined, []), filepath);
|
2020-09-14 12:44:43 +03:00
|
|
|
t.is(testResolveUriToFile(`file://${filepath}`, undefined, []), filepath);
|
2020-05-04 20:28:01 +03:00
|
|
|
// Relative paths are made absolute
|
2023-01-18 23:00:33 +03:00
|
|
|
t.is(testResolveUriToFile(relativeFilepath, undefined, [])
|
|
|
|
?.split(path.sep)
|
|
|
|
.join("/"), filepath);
|
|
|
|
t.is(testResolveUriToFile(`file://${relativeFilepath}`, undefined, [])
|
|
|
|
?.split(path.sep)
|
|
|
|
.join("/"), filepath);
|
2020-05-04 20:28:01 +03:00
|
|
|
// Absolute paths outside the src root are discarded
|
2020-09-14 12:44:43 +03:00
|
|
|
t.is(testResolveUriToFile("/src/foo/bar.js", undefined, []), undefined);
|
|
|
|
t.is(testResolveUriToFile("file:///src/foo/bar.js", undefined, []), undefined);
|
2020-05-04 20:28:01 +03:00
|
|
|
// Other schemes are discarded
|
2020-09-14 12:44:43 +03:00
|
|
|
t.is(testResolveUriToFile(`https://${filepath}`, undefined, []), undefined);
|
|
|
|
t.is(testResolveUriToFile(`ftp://${filepath}`, undefined, []), undefined);
|
2020-05-04 20:28:01 +03:00
|
|
|
// Invalid URIs are discarded
|
|
|
|
t.is(testResolveUriToFile(1, undefined, []), undefined);
|
|
|
|
t.is(testResolveUriToFile(undefined, undefined, []), undefined);
|
2020-11-20 13:35:59 +03:00
|
|
|
// Non-existent files are discarded
|
2020-09-14 12:44:43 +03:00
|
|
|
t.is(testResolveUriToFile(`${filepath}2`, undefined, []), undefined);
|
2020-05-04 20:28:01 +03:00
|
|
|
// Index is resolved
|
|
|
|
t.is(testResolveUriToFile(undefined, 0, [filepath]), filepath);
|
2020-09-14 12:44:43 +03:00
|
|
|
t.is(testResolveUriToFile(undefined, 1, ["foo", filepath]), filepath);
|
2020-05-04 20:28:01 +03:00
|
|
|
// Invalid indexes are discarded
|
|
|
|
t.is(testResolveUriToFile(undefined, 1, [filepath]), undefined);
|
2020-09-14 12:44:43 +03:00
|
|
|
t.is(testResolveUriToFile(undefined, "0", [filepath]), undefined);
|
2021-05-05 22:19:59 +03:00
|
|
|
// Directories are discarded
|
|
|
|
const dirpath = __dirname;
|
|
|
|
t.is(testResolveUriToFile(dirpath, undefined, []), undefined);
|
|
|
|
t.is(testResolveUriToFile(`file://${dirpath}`, undefined, []), undefined);
|
2020-05-04 20:28:01 +03:00
|
|
|
});
|
2021-09-10 23:53:13 +03:00
|
|
|
(0, ava_1.default)("addFingerprints", async (t) => {
|
2020-05-04 20:28:01 +03:00
|
|
|
// Run an end-to-end test on a test file
|
2022-01-13 02:26:34 +03:00
|
|
|
const input = JSON.parse(fs
|
2020-09-14 12:44:43 +03:00
|
|
|
.readFileSync(`${__dirname}/../src/testdata/fingerprinting.input.sarif`)
|
2022-01-13 02:26:34 +03:00
|
|
|
.toString());
|
|
|
|
const expected = JSON.parse(fs
|
2020-09-14 12:44:43 +03:00
|
|
|
.readFileSync(`${__dirname}/../src/testdata/fingerprinting.expected.sarif`)
|
2022-01-13 02:26:34 +03:00
|
|
|
.toString());
|
2020-05-04 20:28:01 +03:00
|
|
|
// The URIs in the SARIF files resolve to files in the testdata directory
|
2021-07-01 12:38:14 +03:00
|
|
|
const sourceRoot = path.normalize(`${__dirname}/../src/testdata`);
|
2021-09-10 23:53:13 +03:00
|
|
|
t.deepEqual(await fingerprints.addFingerprints(input, sourceRoot, (0, logging_1.getRunnerLogger)(true)), expected);
|
2020-05-04 20:28:01 +03:00
|
|
|
});
|
2021-09-10 23:53:13 +03:00
|
|
|
(0, ava_1.default)("missingRegions", async (t) => {
|
2020-05-04 20:28:01 +03:00
|
|
|
// Run an end-to-end test on a test file
|
2022-01-13 02:26:34 +03:00
|
|
|
const input = JSON.parse(fs
|
2020-09-14 12:44:43 +03:00
|
|
|
.readFileSync(`${__dirname}/../src/testdata/fingerprinting2.input.sarif`)
|
2022-01-13 02:26:34 +03:00
|
|
|
.toString());
|
|
|
|
const expected = JSON.parse(fs
|
2020-09-14 12:44:43 +03:00
|
|
|
.readFileSync(`${__dirname}/../src/testdata/fingerprinting2.expected.sarif`)
|
2022-01-13 02:26:34 +03:00
|
|
|
.toString());
|
2020-05-04 20:28:01 +03:00
|
|
|
// The URIs in the SARIF files resolve to files in the testdata directory
|
2021-07-01 12:38:14 +03:00
|
|
|
const sourceRoot = path.normalize(`${__dirname}/../src/testdata`);
|
2021-09-10 23:53:13 +03:00
|
|
|
t.deepEqual(await fingerprints.addFingerprints(input, sourceRoot, (0, logging_1.getRunnerLogger)(true)), expected);
|
2020-05-04 20:28:01 +03:00
|
|
|
});
|
2020-05-13 18:31:24 +03:00
|
|
|
//# sourceMappingURL=fingerprints.test.js.map
|