File: mongocrypt_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 (113 lines) | stat: -rw-r--r-- 3,268 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
# frozen_string_literal: true
# rubocop:todo all

require 'lite_spec_helper'
require_relative '../helpers/mongo_crypt_spec_helper'

describe 'Mongo::Crypt::Binding' do
  describe 'mongocrypt_t binding' do
    require_libmongocrypt

    after do
      Mongo::Crypt::Binding.mongocrypt_destroy(mongocrypt)
    end

    describe '#mongocrypt_new' do
      let(:mongocrypt) { Mongo::Crypt::Binding.mongocrypt_new }

      it 'returns a pointer' do
        expect(mongocrypt).to be_a_kind_of(FFI::Pointer)
      end
    end

    describe '#mongocrypt_init' do
      let(:key_bytes) { [114, 117, 98, 121] * 24 } # 96 bytes

      let(:kms_providers) do
        BSON::Document.new({
          local: {
            key: BSON::Binary.new(key_bytes.pack('C*'), :generic),
          }
        })
      end

      let(:binary) do
        data = kms_providers.to_bson.to_s
        Mongo::Crypt::Binding.mongocrypt_binary_new_from_data(
          FFI::MemoryPointer.from_string(data),
          data.bytesize,
        )
      end

      let(:mongocrypt) do
        Mongo::Crypt::Binding.mongocrypt_new.tap do |mongocrypt|
          Mongo::Crypt::Binding.mongocrypt_setopt_kms_providers(mongocrypt, binary)
        end
      end

      after do
        Mongo::Crypt::Binding.mongocrypt_binary_destroy(binary)
      end

      context 'with valid kms option' do
        before do
          MongoCryptSpecHelper.bind_crypto_hooks(mongocrypt)
        end

        it 'returns true' do
          expect(Mongo::Crypt::Binding.mongocrypt_init(mongocrypt)).to be true
        end
      end

      context 'with invalid kms option' do
        before do
          MongoCryptSpecHelper.bind_crypto_hooks(mongocrypt)
        end

        let(:key_bytes) { [114, 117, 98, 121] * 23 } # NOT 96 bytes

        it 'returns false' do
          expect(Mongo::Crypt::Binding.mongocrypt_init(mongocrypt)).to be false
        end
      end
    end

    describe '#mongocrypt_status' do
      let(:status) { Mongo::Crypt::Binding.mongocrypt_status_new }
      let(:mongocrypt) { mongocrypt = Mongo::Crypt::Binding.mongocrypt_new }

      after do
        Mongo::Crypt::Binding.mongocrypt_status_destroy(status)
      end

      context 'for a new mongocrypt_t object' do
        it 'returns an ok status' do
          Mongo::Crypt::Binding.mongocrypt_status(mongocrypt, status)
          expect(Mongo::Crypt::Binding.mongocrypt_status_type(status)).to eq(:ok)
        end
      end

      context 'for a mongocrypt_t object with invalid kms options' do
        let(:key_bytes) { [114, 117, 98, 121] * 23 } # NOT 96 bytes

        let(:binary) do
          p = FFI::MemoryPointer.new(key_bytes.size)
                .write_array_of_type(FFI::TYPE_UINT8, :put_uint8, key_bytes)

          Mongo::Crypt::Binding.mongocrypt_binary_new_from_data(p, key_bytes.length)
        end

        after do
          Mongo::Crypt::Binding.mongocrypt_binary_destroy(binary)
        end

        it 'returns a error_client status' do
          Mongo::Crypt::Binding.mongocrypt_setopt_kms_providers(mongocrypt, binary)

          Mongo::Crypt::Binding.mongocrypt_status(mongocrypt, status)
          expect(Mongo::Crypt::Binding.mongocrypt_status_type(status)).to eq(:error_client)
        end
      end
    end
  end
end