File: bson_size_limit_spec.rb

package info (click to toggle)
ruby-mongo 2.21.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 14,764 kB
  • sloc: ruby: 108,806; makefile: 5; sh: 2
file content (190 lines) | stat: -rw-r--r-- 5,722 bytes parent folder | download
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# frozen_string_literal: true
# rubocop:todo all

require 'spec_helper'

describe 'Client-Side Encryption' do
  describe 'Prose tests: BSON size limits and batch splitting' do
    require_libmongocrypt
    require_enterprise
    min_server_fcv '4.2'

    include_context 'define shared FLE helpers'

    let(:subscriber) { Mrss::EventSubscriber.new }

    let(:client) do
      authorized_client.use('db')
    end

    let(:json_schema) do
      BSON::ExtJSON.parse(File.read('spec/support/crypt/limits/limits-schema.json'))
    end

    let(:limits_doc) do
      BSON::ExtJSON.parse(File.read('spec/support/crypt/limits/limits-doc.json'))
    end

    let(:client_encrypted) do
      new_local_client(
        SpecConfig.instance.addresses,
        SpecConfig.instance.test_options.merge(
          auto_encryption_options: {
            kms_providers: {
              local: { key: local_master_key },
            },
            key_vault_namespace: 'keyvault.datakeys',
            # Spawn mongocryptd on non-default port for sharded cluster tests
            extra_options: extra_options,
          },
          database: 'db',
        )
      ).tap do |client|
        client.subscribe(Mongo::Monitoring::COMMAND, subscriber)
      end
    end

    before do
      client['coll'].drop
      client['coll',
        {
          'validator' => { '$jsonSchema' => json_schema }
        }
      ].create

      key_vault_collection = client.use('keyvault')['datakeys', write_concern: { w: :majority }]

      key_vault_collection.drop
      key_vault_collection.insert_one(
        BSON::ExtJSON.parse(File.read('spec/support/crypt/limits/limits-key.json'))
      )
    end

    let(:_2mib) { 2097152 }
    let(:_16mib) { 16777216 }

    context 'when a single, unencrypted document is larger than 2MiB' do
      it 'can perform insert_one using the encrypted client' do
        document = {
          _id: "over_2mib_under_16mib",
          unencrypted: 'a' * _2mib
        }

        result = client_encrypted['coll'].insert_one(document)

        expect(result).to be_ok
      end
    end

    context 'when a single encrypted document is larger than 2MiB' do
      it 'can perform insert_one using the encrypted client' do
        result = client_encrypted['coll'].insert_one(
          limits_doc.merge(
            _id: "encryption_exceeds_2mi",
            unencrypted: 'a' * (_2mib - 2000)
          )
        )

        expect(result).to be_ok
      end
    end

    context 'when bulk inserting two unencrypted documents under 2MiB' do
      it 'can perform bulk insert using the encrypted client' do
        bulk_write = Mongo::BulkWrite.new(
          client_encrypted['coll'],
          [
            { insert_one: { _id: 'over_2mib_1', unencrypted: 'a' * _2mib } },
            { insert_one: { _id: 'over_2mib_2', unencrypted: 'a' * _2mib } },
          ]
        )

        result = bulk_write.execute
        expect(result.inserted_count).to eq(2)

        command_succeeded_events = subscriber.succeeded_events.select do |event|
          event.command_name == 'insert'
        end

        expect(command_succeeded_events.length).to eq(2)
      end
    end

    context 'when bulk deletes two unencrypted documents under 2MiB' do
      it 'can perform bulk delete using the encrypted client' do
        # Insert documents that we can match and delete later
        bulk_write = Mongo::BulkWrite.new(
          client_encrypted['coll'],
          [
            { insert_one: { _id: 'over_2mib_1', unencrypted: 'a' * _2mib } },
            { insert_one: { _id: 'over_2mib_2', unencrypted: 'a' * _2mib } },
          ]
        )

        result = bulk_write.execute
        expect(result.inserted_count).to eq(2)

        command_succeeded_events = subscriber.succeeded_events.select do |event|
          event.command_name == 'insert'
        end

        expect(command_succeeded_events.length).to eq(2)
      end
    end

    context 'when bulk inserting two encrypted documents under 2MiB' do
      it 'can perform bulk_insert using the encrypted client' do
        bulk_write = Mongo::BulkWrite.new(
          client_encrypted['coll'],
          [
            {
              insert_one: limits_doc.merge(
                _id: "encryption_exceeds_2mib_1",
                unencrypted: 'a' * (_2mib - 2000)
              )
            },
            {
              insert_one: limits_doc.merge(
                _id: 'encryption_exceeds_2mib_2',
                unencrypted: 'a' * (_2mib - 2000)
              )
            },
          ]
        )

        result = bulk_write.execute
        expect(result.inserted_count).to eq(2)

        command_succeeded_events = subscriber.succeeded_events.select do |event|
          event.command_name == 'insert'
        end

        expect(command_succeeded_events.length).to eq(2)
      end
    end

    context 'when a single document is just smaller than 16MiB' do
      it 'can perform insert_one using the encrypted client' do
        result = client_encrypted['coll'].insert_one(
          _id: "under_16mib",
          unencrypted: "a" * (_16mib - 2000)
        )

        expect(result).to be_ok
      end
    end

    context 'when an encrypted document is greater than the 16MiB limit' do
      it 'raises an exception when attempting to insert the document' do
        expect do
          client_encrypted['coll'].insert_one(
            limits_doc.merge(
              _id: "encryption_exceeds_16mib",
              unencrypted: "a" * (16*1024*1024 + 500*1024),
            )
          )
        end.to raise_error(Mongo::Error::MaxBSONSize, /The document exceeds maximum allowed BSON object size after serialization/)
      end
    end
  end
end