Files
mongo/jstests/core/query/json_schema/encrypt.js
Zac 591928c619 SERVER-108478 JS formatted by prettier and remove clang-format (#39656)
GitOrigin-RevId: 6c8f6aded47f260aa4f7c231b17dae3302cb1e04
2025-08-21 17:27:09 +00:00

69 lines
2.3 KiB
JavaScript

/**
* Tests for handling of the JSON Schema 'encrypt' keyword.
*
* @tags: [
* requires_non_retryable_commands,
* ]
*/
import {assertSchemaMatch} from "jstests/libs/assert_schema_match.js";
const coll = db.jstests_schema_encrypt;
const encryptedBinDataElement = BinData(6, "AAAAAAAAAAAAAAAAAAAAAAAAAAAA");
const nonEncryptedBinDataElement = BinData(0, "AAAAAAAAAAAAAAAAAAAAAAAAAAAA");
// Only elements of type BinData with subtype '6' should match.
assertSchemaMatch(coll, {properties: {bin: {encrypt: {}}}}, {bin: encryptedBinDataElement}, true);
assertSchemaMatch(coll, {properties: {bin: {encrypt: {}}}}, {bin: {}}, false);
assertSchemaMatch(coll, {properties: {bin: {encrypt: {}}}}, {bin: nonEncryptedBinDataElement}, false);
// Nested in object.
assertSchemaMatch(
coll,
{properties: {obj: {type: "object", properties: {a: {encrypt: {}}}}}},
{obj: {a: encryptedBinDataElement}},
true,
);
assertSchemaMatch(coll, {properties: {obj: {type: "object", properties: {a: {encrypt: {}}}}}}, {obj: {a: {}}}, false);
assertSchemaMatch(
coll,
{properties: {obj: {type: "object", properties: {a: {encrypt: {}}}}}},
{obj: {a: nonEncryptedBinDataElement}},
false,
);
// Nested in array.
assertSchemaMatch(
coll,
{properties: {arr: {type: "array", items: {encrypt: {}}}}},
{arr: [encryptedBinDataElement, encryptedBinDataElement]},
true,
);
assertSchemaMatch(coll, {properties: {arr: {type: "array", items: {encrypt: {}}}}}, {arr: [{}, {}]}, false);
assertSchemaMatch(
coll,
{properties: {arr: {type: "array", items: {encrypt: {}}}}},
{arr: [encryptedBinDataElement, nonEncryptedBinDataElement]},
false,
);
// If array is not specified, should not traverse array of encrypted BinData's.
assertSchemaMatch(
coll,
{properties: {bin: {encrypt: {}}}},
{bin: [encryptedBinDataElement, encryptedBinDataElement]},
false,
);
// Encrypt alongside type/bsontype should fail to parse.
assert.commandFailedWithCode(
coll.runCommand({find: "coll", filter: {$jsonSchema: {properties: {bin: {encrypt: {}, type: "object"}}}}}),
ErrorCodes.FailedToParse,
);
assert.commandFailedWithCode(
coll.runCommand({
find: "coll",
filter: {$jsonSchema: {properties: {bin: {encrypt: {}, bsonType: "object"}}}},
}),
ErrorCodes.FailedToParse,
);