diff options
author | Davis Haupt <davis.haupt@mongodb.com> | 2023-02-27 21:29:37 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2023-02-27 23:34:15 +0000 |
commit | d3eacf8547d811f075ef554dc8363f8e06eea9c4 (patch) | |
tree | 4ff82d880b91669488e92ace2eab280b131b9122 /src/mongo/db/query/projection_ast_test.cpp | |
parent | 294fb3396b285d140a7a0522ce3a00a75d694448 (diff) | |
download | mongo-d3eacf8547d811f075ef554dc8363f8e06eea9c4.tar.gz |
SERVER-73488 Add redaction to projection AST serialization
Diffstat (limited to 'src/mongo/db/query/projection_ast_test.cpp')
-rw-r--r-- | src/mongo/db/query/projection_ast_test.cpp | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/src/mongo/db/query/projection_ast_test.cpp b/src/mongo/db/query/projection_ast_test.cpp index 9e0001d5a8b..90b75fe56c5 100644 --- a/src/mongo/db/query/projection_ast_test.cpp +++ b/src/mongo/db/query/projection_ast_test.cpp @@ -40,6 +40,8 @@ #include "mongo/db/query/projection_ast_util.h" #include "mongo/db/query/projection_parser.h" #include "mongo/db/query/query_planner_test_fixture.h" +#include "mongo/db/query/serialization_options.h" +#include "mongo/unittest/inline_auto_update.h" namespace { @@ -771,4 +773,69 @@ TEST_F(ProjectionASTTest, ShouldThrowWithPositionalOnExclusion) { DBException, 31395); } +std::string redactFieldNameForTest(StringData s) { + return str::stream() << "HASH(" << s << ")"; +} + +TEST_F(ProjectionASTTest, TestASTRedaction) { + SerializationOptions options; + options.replacementForLiteralArgs = "?"; + options.redactFieldNames = true; + options.redactFieldNamesStrategy = redactFieldNameForTest; + + + auto proj = fromjson("{'a.b': 1}"); + BSONObj output = projection_ast::serialize(parseWithFindFeaturesEnabled(proj), options); + ASSERT_STR_EQ_AUTO( // + "{ HASH(a): { HASH(b): true }, HASH(_id): true }", // NOLINT (test auto-update) + output.toString()); + + proj = fromjson("{'a.b': 0}"); + output = projection_ast::serialize(parseWithFindFeaturesEnabled(proj), options); + ASSERT_STR_EQ_AUTO( // + "{ HASH(a): { HASH(b): false } }", // NOLINT (test auto-update) + output.toString()); + + proj = fromjson("{a: 1, b: 1}"); + output = projection_ast::serialize(parseWithFindFeaturesEnabled(proj), options); + ASSERT_STR_EQ_AUTO( // + "{ HASH(a): true, HASH(b): true, HASH(_id): true }", // NOLINT (test auto-update) + output.toString()); + + // ElemMatch projection + proj = fromjson("{f: {$elemMatch: {foo: 'bar'}}}"); + output = projection_ast::serialize(parseWithFindFeaturesEnabled(proj), options); + ASSERT_STR_EQ_AUTO( // + "{ HASH(f): { $elemMatch: { HASH(foo): { $eq: \"?\" } } }, HASH(_id): true }", // NOLINT + output.toString()); + + // Positional projection + proj = fromjson("{'x.$': 1}"); + output = + projection_ast::serialize(parseWithFindFeaturesEnabled(proj, fromjson("{'x.a': 2}")), {}); + ASSERT_STR_EQ_AUTO( // + "{ x.$: true, _id: true }", // NOLINT (test auto-update) + output.toString()); + + // Slice (first form) + proj = fromjson("{a: {$slice: 1}}"); + output = projection_ast::serialize(parseWithFindFeaturesEnabled(proj), options); + ASSERT_STR_EQ_AUTO( // + "{ HASH(a): { $slice: \"?\" } }", // NOLINT (test auto-update) + output.toString()); + + // Slice (second form) + proj = fromjson("{a: {$slice: [1, 3]}}"); + output = projection_ast::serialize(parseWithFindFeaturesEnabled(proj), options); + ASSERT_STR_EQ_AUTO( // + "{ HASH(a): { $slice: [ \"?\", \"?\" ] } }", // NOLINT (test auto-update) + output.toString()); + + /// $meta projection + proj = fromjson("{foo: {$meta: 'indexKey'}}"); + output = projection_ast::serialize(parseWithFindFeaturesEnabled(proj), options); + ASSERT_STR_EQ_AUTO( // + "{ HASH(foo): { $meta: \"indexKey\" } }", // NOLINT (test auto-update) + output.toString()); +} } // namespace |