|
| 1 | +import 'mocha'; |
| 2 | +import { expect } from 'chai'; |
| 3 | +import * as Sarif from 'sarif'; |
| 4 | + |
| 5 | +import { getPathRelativeToSourceLocationPrefix, parseSarifLocation, parseSarifPlainTextMessage, unescapeSarifText } from '../../src/sarif-utils'; |
| 6 | + |
| 7 | + |
| 8 | +describe('parsing sarif', () => { |
| 9 | + it('should be able to parse a simple message from the spec', async function() { |
| 10 | + const message = 'Tainted data was used. The data came from [here](3).'; |
| 11 | + const results = parseSarifPlainTextMessage(message); |
| 12 | + expect(results).to.deep.equal([ |
| 13 | + 'Tainted data was used. The data came from ', |
| 14 | + { dest: 3, text: 'here' }, '.' |
| 15 | + ]); |
| 16 | + }); |
| 17 | + |
| 18 | + it('should be able to parse a complex message from the spec', async function() { |
| 19 | + const message = 'Prohibited term used in [para\\[0\\]\\\\spans\\[2\\]](1).'; |
| 20 | + const results = parseSarifPlainTextMessage(message); |
| 21 | + expect(results).to.deep.equal([ |
| 22 | + 'Prohibited term used in ', |
| 23 | + { dest: 1, text: 'para[0]\\spans[2]' }, '.' |
| 24 | + ]); |
| 25 | + }); |
| 26 | + it('should be able to parse a broken complex message from the spec', async function() { |
| 27 | + const message = 'Prohibited term used in [para\\[0\\]\\\\spans\\[2\\](1).'; |
| 28 | + const results = parseSarifPlainTextMessage(message); |
| 29 | + expect(results).to.deep.equal([ |
| 30 | + 'Prohibited term used in [para[0]\\spans[2](1).' |
| 31 | + ]); |
| 32 | + }); |
| 33 | + it('should be able to parse a message with extra escaping the spec', async function() { |
| 34 | + const message = 'Tainted data was used. The data came from \\[here](3).'; |
| 35 | + const results = parseSarifPlainTextMessage(message); |
| 36 | + expect(results).to.deep.equal([ |
| 37 | + 'Tainted data was used. The data came from [here](3).' |
| 38 | + ]); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should unescape sarif text', () => { |
| 42 | + expect(unescapeSarifText('\\\\ \\\\ \\[ \\[ \\] \\]')).to.eq('\\ \\ [ [ ] ]'); |
| 43 | + // Also show that unescaped special chars are unchanged...is this correct? |
| 44 | + expect(unescapeSarifText('\\ \\ [ [ ] ]')).to.eq('\\ \\ [ [ ] ]'); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should normalize source locations', () => { |
| 48 | + expect(getPathRelativeToSourceLocationPrefix('C:\\a\\b', '?x=test')) |
| 49 | + .to.eq('file:C:/a/b/?x=test'); |
| 50 | + expect(getPathRelativeToSourceLocationPrefix('C:\\a\\b', '%3Fx%3Dtest')) |
| 51 | + .to.eq('file:C:/a/b/%3Fx%3Dtest'); |
| 52 | + }); |
| 53 | + |
| 54 | + describe('parseSarifLocation', () => { |
| 55 | + it('should parse a sarif location with "no location"', () => { |
| 56 | + expect(parseSarifLocation({ }, '')).to.deep.equal({ |
| 57 | + hint: 'no physical location' |
| 58 | + }); |
| 59 | + expect(parseSarifLocation({ physicalLocation: {} }, '')).to.deep.equal({ |
| 60 | + hint: 'no artifact location' |
| 61 | + }); |
| 62 | + expect(parseSarifLocation({ physicalLocation: { artifactLocation: { } } }, '')).to.deep.equal({ |
| 63 | + hint: 'artifact location has no uri' |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should parse a sarif location with no region and no file protocol', () => { |
| 68 | + const location: Sarif.Location = { |
| 69 | + physicalLocation: { |
| 70 | + artifactLocation: { |
| 71 | + uri: 'abc?x=test' |
| 72 | + } |
| 73 | + } |
| 74 | + }; |
| 75 | + expect(parseSarifLocation(location, 'prefix')).to.deep.equal({ |
| 76 | + uri: 'file:prefix/abc?x=test', |
| 77 | + userVisibleFile: 'abc?x=test' |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should parse a sarif location with no region and file protocol', () => { |
| 82 | + const location: Sarif.Location = { |
| 83 | + physicalLocation: { |
| 84 | + artifactLocation: { |
| 85 | + uri: 'file:abc%3Fx%3Dtest' |
| 86 | + } |
| 87 | + } |
| 88 | + }; |
| 89 | + expect(parseSarifLocation(location, 'prefix')).to.deep.equal({ |
| 90 | + uri: 'file:abc%3Fx%3Dtest', |
| 91 | + userVisibleFile: 'abc?x=test' |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should parse a sarif location with a region and file protocol', () => { |
| 96 | + const location: Sarif.Location = { |
| 97 | + physicalLocation: { |
| 98 | + artifactLocation: { |
| 99 | + uri: 'file:abc%3Fx%3Dtest' |
| 100 | + }, |
| 101 | + region: { |
| 102 | + startLine: 1, |
| 103 | + startColumn: 2, |
| 104 | + endLine: 3, |
| 105 | + endColumn: 4 |
| 106 | + } |
| 107 | + } |
| 108 | + }; |
| 109 | + expect(parseSarifLocation(location, 'prefix')).to.deep.equal({ |
| 110 | + uri: 'file:abc%3Fx%3Dtest', |
| 111 | + userVisibleFile: 'abc?x=test', |
| 112 | + startLine: 1, |
| 113 | + startColumn: 2, |
| 114 | + endLine: 3, |
| 115 | + endColumn: 3 |
| 116 | + }); |
| 117 | + }); |
| 118 | + }); |
| 119 | +}); |
0 commit comments