summaryrefslogtreecommitdiff
path: root/jstests/core/json_schema/encrypt.js
blob: 32d93f43da424c41726b519bba140c587cf6d792 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
 * Tests for handling of the JSON Schema 'encrypt' keyword.
 *
 * @tags: [
 *   requires_non_retryable_commands,
 * ]
 */
(function() {
"use strict";

load("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);
}());