#include "librbd/crypto/BlockCrypto.h"
#include "include/byteorder.h"
#include "include/ceph_assert.h"
+#include "include/scope_guard.h"
#include <stdlib.h>
lderr(m_cct) << "unable to get crypt context" << dendl;
return -EIO;
}
+
+ auto sg = make_scope_guard([&] {
+ m_data_cryptor->return_context(ctx, mode); });
+
auto sector_number = image_offset / 512;
auto appender = data->get_contiguous_appender(src.length());
unsigned char* out_buf_ptr = nullptr;
}
}
- m_data_cryptor->return_context(ctx, mode);
-
return 0;
}
using ::testing::ExpectationSet;
using ::testing::internal::ExpectationBase;
+using ::testing::Invoke;
using ::testing::Return;
+using ::testing::WithArg;
using ::testing::_;
namespace librbd {
new MockCryptoContext())));
}
+ void expect_return_context(CipherMode mode) {
+ _set_last_expectation(
+ EXPECT_CALL(*cryptor, return_context(_, mode))
+ .After(*expectation_set).WillOnce(WithArg<0>(
+ Invoke([](MockCryptoContext* ctx) {
+ delete ctx;
+ }))));
+ }
+
void expect_init_context(const std::string& iv) {
_set_last_expectation(
EXPECT_CALL(*cryptor, init_context(_, CompareArrayToString(iv),
expect_update_context(std::string(2048, '1') + std::string(2048, '2'), 4096);
expect_init_context(std::string("\x38\x12\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16));
expect_update_context(std::string(2048, '2') + std::string(2048, '3'), 4096);
- EXPECT_CALL(*cryptor, return_context(_, CipherMode::CIPHER_MODE_ENC));
+ expect_return_context(CipherMode::CIPHER_MODE_ENC);
ASSERT_EQ(0, bc->encrypt(&data, image_offset));
data.append(std::string(4096, '1'));
expect_get_context(CipherMode::CIPHER_MODE_ENC);
EXPECT_CALL(*cryptor, init_context(_, _, _)).WillOnce(Return(-123));
+ expect_return_context(CipherMode::CIPHER_MODE_ENC);
ASSERT_EQ(-123, bc->encrypt(&data, 0));
}
expect_get_context(CipherMode::CIPHER_MODE_ENC);
EXPECT_CALL(*cryptor, init_context(_, _, _));
EXPECT_CALL(*cryptor, update_context(_, _, _, _)).WillOnce(Return(-123));
+ expect_return_context(CipherMode::CIPHER_MODE_ENC);
ASSERT_EQ(-123, bc->encrypt(&data, 0));
}