|
2 | 2 | * Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License. |
3 | 3 | * This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2022 Datadog, Inc. |
4 | 4 | **/ |
5 | | -const { TaintedUtils } = require('./util') |
| 5 | +const { TaintedUtils, taintFormattedString, formatTaintedValue } = require('./util') |
6 | 6 | const assert = require('assert') |
7 | 7 |
|
8 | 8 | describe('Plus operator', function () { |
@@ -137,3 +137,90 @@ describe('Plus operator', function () { |
137 | 137 | assert.deepEqual(expected, TaintedUtils.getRanges(id, ret)) |
138 | 138 | }) |
139 | 139 | }) |
| 140 | + |
| 141 | +describe('concat method', () => { |
| 142 | + const id = '1' |
| 143 | + |
| 144 | + afterEach(function () { |
| 145 | + TaintedUtils.removeTransaction(id) |
| 146 | + }) |
| 147 | + |
| 148 | + const rangesTestCases = [ |
| 149 | + { |
| 150 | + testStrings: [':+-A-+:', 'B', 'C'], |
| 151 | + result: ':+-A-+:BC' |
| 152 | + }, |
| 153 | + { |
| 154 | + testStrings: ['A', ':+-B-+:', 'C'], |
| 155 | + result: 'A:+-B-+:C' |
| 156 | + }, |
| 157 | + { |
| 158 | + testStrings: ['A', 'B', ':+-C-+:'], |
| 159 | + result: 'AB:+-C-+:' |
| 160 | + }, |
| 161 | + { |
| 162 | + testStrings: ['A', '�', ':+-C-+:'], |
| 163 | + result: 'A�:+-C-+:' |
| 164 | + }, |
| 165 | + { |
| 166 | + testStrings: ['A', null, ':+-C-+:'], |
| 167 | + result: 'Anull:+-C-+:' |
| 168 | + }, |
| 169 | + { |
| 170 | + testStrings: ['A', undefined, ':+-C-+:'], |
| 171 | + result: 'Aundefined:+-C-+:' |
| 172 | + }, |
| 173 | + { |
| 174 | + testStrings: ['A', 'B', 'C'], |
| 175 | + result: 'ABC', |
| 176 | + tainted: false |
| 177 | + }, |
| 178 | + { |
| 179 | + testStrings: [':+-A-+:', 'B', ':+-C-+:'], |
| 180 | + result: ':+-A-+:B:+-C-+:' |
| 181 | + }, |
| 182 | + { |
| 183 | + testStrings: [':+-A-+:', ':+-B-+:', 'C'], |
| 184 | + result: ':+-A-+::+-B-+:C' |
| 185 | + }, |
| 186 | + { |
| 187 | + testStrings: [':+-A-+: BC :+-D-+:', ':+-E-+:', 'F'], |
| 188 | + result: ':+-A-+: BC :+-D-+::+-E-+:F' |
| 189 | + }, |
| 190 | + { |
| 191 | + testStrings: [':+-A-+: ���', ':+-B-+:', 'C'], |
| 192 | + result: ':+-A-+: ���:+-B-+:C' |
| 193 | + }, |
| 194 | + { |
| 195 | + testStrings: [':+-A-+:', 1, 'C'], |
| 196 | + result: ':+-A-+:1C' |
| 197 | + }, |
| 198 | + { |
| 199 | + testStrings: ['A', {}, ':+-C-+:'], |
| 200 | + result: 'A[object Object]:+-C-+:' |
| 201 | + } |
| 202 | + ] |
| 203 | + |
| 204 | + function testConcatCheckRanges (formattedTestStrings, expectedResult, resultTainted) { |
| 205 | + const [testString, ...rest] = formattedTestStrings |
| 206 | + .map(formattedTestString => taintFormattedString(id, formattedTestString)) |
| 207 | + const res = String.prototype.concat.call(testString, ...rest) |
| 208 | + |
| 209 | + const ret = TaintedUtils.concat(id, res, testString, ...rest) |
| 210 | + assert.equal(res, ret, 'Unexpected vale') |
| 211 | + assert.equal(TaintedUtils.isTainted(id, ret), resultTainted, |
| 212 | + `Concat returned value ${resultTainted ? 'not tainted' : 'tainted'}`) |
| 213 | + |
| 214 | + const formattedResult = resultTainted ? formatTaintedValue(id, ret) : ret |
| 215 | + assert.equal(formattedResult, expectedResult, 'Unexpected ranges') |
| 216 | + } |
| 217 | + |
| 218 | + describe('Check ranges', function () { |
| 219 | + rangesTestCases.forEach(({ testStrings, result, tainted }) => { |
| 220 | + it(`Test ${testStrings}`, () => { |
| 221 | + const resultTainted = tainted === undefined ? true : tainted |
| 222 | + testConcatCheckRanges(testStrings, result, resultTainted) |
| 223 | + }) |
| 224 | + }) |
| 225 | + }) |
| 226 | +}) |
0 commit comments