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'
describe Mongo::Crypt::Binary do
require_libmongocrypt
let(:data) { 'I love Ruby' }
let(:binary) { described_class.from_data(data) }
describe '#initialize' do
context 'with nil data' do
let(:binary) { described_class.new }
it 'creates a new Mongo::Crypt::Binary object' do
expect do
binary
end.not_to raise_error
end
end
context 'with valid data' do
let(:binary) { described_class.new(data: data) }
it 'creates a new Mongo::Crypt::Binary object' do
expect do
binary
end.not_to raise_error
end
end
context 'with pointer' do
let(:pointer) { Mongo::Crypt::Binding.mongocrypt_binary_new }
let(:binary) { described_class.new(pointer: pointer) }
after do
Mongo::Crypt::Binding.mongocrypt_binary_destroy(pointer)
end
it 'creates a new Mongo::Crypt::Binary object from pointer' do
expect do
binary
end.not_to raise_error
expect(binary.ref).to eq(pointer)
end
end
end
describe '#self.from_data' do
let(:binary) { described_class.from_data(data) }
it 'creates a new Mongo::Crypt::Binary object' do
expect do
binary
end.not_to raise_error
end
end
describe '#self.from_pointer' do
let(:pointer) { Mongo::Crypt::Binding.mongocrypt_binary_new }
let(:binary) { described_class.from_pointer(pointer) }
after do
Mongo::Crypt::Binding.mongocrypt_binary_destroy(pointer)
end
it 'creates a new Mongo::Crypt::Binary object from pointer' do
expect do
binary
end.not_to raise_error
expect(binary.ref).to eq(pointer)
end
end
describe '#to_s' do
it 'returns the original string' do
expect(binary.to_s).to eq(data)
end
end
describe '#write' do
# Binary must have enough space pre-allocated
let(:binary) { described_class.from_data("\00" * data.length) }
it 'writes data to the binary object' do
expect(binary.write(data)).to be true
expect(binary.to_s).to eq(data)
end
context 'with no space allocated' do
let(:binary) { described_class.new }
it 'returns false' do
expect do
binary.write(data)
end.to raise_error(ArgumentError, /Cannot write #{data.length} bytes of data to a Binary object that was initialized with 0 bytes/)
end
end
context 'without enough space allocated' do
let(:binary) { described_class.from_data("\00" * (data.length - 1)) }
it 'returns false' do
expect do
binary.write(data)
end.to raise_error(ArgumentError, /Cannot write #{data.length} bytes of data to a Binary object that was initialized with #{data.length - 1} bytes/)
end
end
end
end
|