|
| 1 | +// Tests rewrites for $match expression fields in change streams in combination with special |
| 2 | +// characters inside the field path. |
| 3 | + |
| 4 | +(function() { |
| 5 | +"use strict"; |
| 6 | + |
| 7 | +load('jstests/libs/change_stream_util.js'); |
| 8 | +load("jstests/libs/collection_drop_recreate.js"); |
| 9 | + |
| 10 | +// Collection name with special characters inside it. |
| 11 | +const kCollName = "booooo '\"bar baz]]]}}}}{{{{{{{{{{{{{{{{{{{{{{{{{"; |
| 12 | + |
| 13 | +function runTest(inserts, pipelineStages, expectedChanges) { |
| 14 | + assertDropAndRecreateCollection(db, kCollName); |
| 15 | + |
| 16 | + const cst = new ChangeStreamTest(db); |
| 17 | + const pipeline = [{$changeStream: {}}].concat(pipelineStages); |
| 18 | + |
| 19 | + jsTestLog("Running test with pipeline: " + tojson(pipeline)); |
| 20 | + let cursor = cst.startWatchingChanges({pipeline, collection: kCollName}); |
| 21 | + assert.eq(0, cursor.firstBatch.length, "Cursor had changes: " + tojson(cursor)); |
| 22 | + |
| 23 | + assert.commandWorked(db[kCollName].insert(inserts)); |
| 24 | + cst.assertNextChangesEqual({cursor, expectedChanges}); |
| 25 | + cst.cleanUp(); |
| 26 | +} |
| 27 | + |
| 28 | +// Pipeline without any match expressions. |
| 29 | +runTest( |
| 30 | + [ |
| 31 | + {_id: "one", value: 1}, |
| 32 | + {_id: {sub: "two"}, value: 2}, |
| 33 | + ], |
| 34 | + [], |
| 35 | + [ |
| 36 | + { |
| 37 | + operationType: "insert", |
| 38 | + fullDocument: {_id: "one", value: 1}, |
| 39 | + ns: {db: "test", coll: kCollName}, |
| 40 | + documentKey: {_id: "one"}, |
| 41 | + }, |
| 42 | + { |
| 43 | + operationType: "insert", |
| 44 | + fullDocument: {_id: {sub: "two"}, value: 2}, |
| 45 | + ns: {db: "test", coll: kCollName}, |
| 46 | + documentKey: {_id: {sub: "two"}}, |
| 47 | + }, |
| 48 | + ]); |
| 49 | + |
| 50 | +// Value with characters in it that can a have special meaning inside strings. |
| 51 | +const fieldValue = "fx {{{\"'x\\"; |
| 52 | + |
| 53 | +// Pipeline with literal match expression on _id with special value. |
| 54 | +runTest( |
| 55 | + [ |
| 56 | + {_id: fieldValue}, |
| 57 | + ], |
| 58 | + [ |
| 59 | + {$match: {"fullDocument._id": fieldValue}}, |
| 60 | + ], |
| 61 | + [ |
| 62 | + { |
| 63 | + operationType: "insert", |
| 64 | + fullDocument: {_id: fieldValue}, |
| 65 | + ns: {db: "test", coll: kCollName}, |
| 66 | + documentKey: {_id: fieldValue}, |
| 67 | + }, |
| 68 | + ]); |
| 69 | + |
| 70 | +// Field name with characters in it that can a have special meaning inside strings. |
| 71 | +const subField = "fx {{{\"'x"; |
| 72 | + |
| 73 | +// Pipeline with $expr match expression on $fullDocument _id with subfield that has a special name. |
| 74 | +runTest( |
| 75 | + [ |
| 76 | + {_id: {[subField]: "x"}}, |
| 77 | + ], |
| 78 | + [ |
| 79 | + {$match: {["fullDocument._id." + subField]: "x"}}, |
| 80 | + ], |
| 81 | + [ |
| 82 | + { |
| 83 | + operationType: "insert", |
| 84 | + fullDocument: {_id: {[subField]: "x"}}, |
| 85 | + ns: {db: "test", coll: kCollName}, |
| 86 | + documentKey: {_id: {[subField]: "x"}}, |
| 87 | + }, |
| 88 | + ]); |
| 89 | + |
| 90 | +// Pipeline with $expr match expression on $fullDocument _id with subfield that has a special name. |
| 91 | +runTest( |
| 92 | + [ |
| 93 | + {_id: {[subField]: "x"}}, |
| 94 | + ], |
| 95 | + [ |
| 96 | + {$match: {$expr: {$eq: ["$fullDocument._id", {[subField]: "x"}]}}}, |
| 97 | + ], |
| 98 | + [ |
| 99 | + { |
| 100 | + operationType: "insert", |
| 101 | + fullDocument: {_id: {[subField]: "x"}}, |
| 102 | + ns: {db: "test", coll: kCollName}, |
| 103 | + documentKey: {_id: {[subField]: "x"}}, |
| 104 | + }, |
| 105 | + ]); |
| 106 | + |
| 107 | +// Pipeline with $expr match expression on $fullDocument _id with subfield that has a special name. |
| 108 | +runTest( |
| 109 | + [ |
| 110 | + {_id: {[subField]: "x"}}, |
| 111 | + ], |
| 112 | + [ |
| 113 | + {$match: {$expr: {$eq: ["$fullDocument._id." + subField, "x"]}}}, |
| 114 | + ], |
| 115 | + [ |
| 116 | + { |
| 117 | + operationType: "insert", |
| 118 | + fullDocument: {_id: {[subField]: "x"}}, |
| 119 | + ns: {db: "test", coll: kCollName}, |
| 120 | + documentKey: {_id: {[subField]: "x"}}, |
| 121 | + }, |
| 122 | + ]); |
| 123 | + |
| 124 | +// Pipeline with $expr match expression on subfield that has a special name. |
| 125 | +runTest( |
| 126 | + [ |
| 127 | + {_id: "z", [subField]: "y"}, |
| 128 | + ], |
| 129 | + [ |
| 130 | + {$match: {$expr: {$eq: ["$fullDocument." + subField, "y"]}}}, |
| 131 | + ], |
| 132 | + [ |
| 133 | + { |
| 134 | + operationType: "insert", |
| 135 | + fullDocument: {_id: "z", [subField]: "y"}, |
| 136 | + ns: {db: "test", coll: kCollName}, |
| 137 | + documentKey: {_id: "z"}, |
| 138 | + }, |
| 139 | + ]); |
| 140 | + |
| 141 | +// Pipeline with $expr match expression on $documentKey subfield that has a special name. |
| 142 | +runTest( |
| 143 | + [ |
| 144 | + {_id: {[subField]: "x"}}, |
| 145 | + ], |
| 146 | + [ |
| 147 | + {$match: {$expr: {$not: {$eq: ["$documentKey." + subField, "x"]}}}}, |
| 148 | + ], |
| 149 | + [ |
| 150 | + { |
| 151 | + operationType: "insert", |
| 152 | + fullDocument: {_id: {[subField]: "x"}}, |
| 153 | + ns: {db: "test", coll: kCollName}, |
| 154 | + documentKey: {_id: {[subField]: "x"}}, |
| 155 | + }, |
| 156 | + ]); |
| 157 | + |
| 158 | +// Pipeline with $expr match expression on $documentKey subfield that has a special name. |
| 159 | +runTest( |
| 160 | + [ |
| 161 | + {_id: {[subField]: "x"}}, |
| 162 | + ], |
| 163 | + [ |
| 164 | + {$match: {$expr: {$lte: ["$documentKey." + subField, "a"]}}}, |
| 165 | + ], |
| 166 | + [ |
| 167 | + { |
| 168 | + operationType: "insert", |
| 169 | + fullDocument: {_id: {[subField]: "x"}}, |
| 170 | + ns: {db: "test", coll: kCollName}, |
| 171 | + documentKey: {_id: {[subField]: "x"}}, |
| 172 | + }, |
| 173 | + ]); |
| 174 | + |
| 175 | +// Pipeline with $expr match expression on $documentKey subfield opening 1K braces. |
| 176 | +runTest( |
| 177 | + [ |
| 178 | + {_id: {[subField]: "x"}}, |
| 179 | + ], |
| 180 | + [ |
| 181 | + { |
| 182 | + $match: { |
| 183 | + $expr: { |
| 184 | + $not: { |
| 185 | + $eq: [ |
| 186 | + "$documentKey.abc', 'test': " + |
| 187 | + "{a: ".repeat(2048), |
| 188 | + "x" |
| 189 | + ] |
| 190 | + } |
| 191 | + } |
| 192 | + } |
| 193 | + }, |
| 194 | + ], |
| 195 | + []); |
| 196 | +})(); |
0 commit comments