]> git.proxmox.com Git - ceph.git/blobdiff - ceph/src/test/librbd/crypto/test_mock_ShutDownCryptoRequest.cc
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / test / librbd / crypto / test_mock_ShutDownCryptoRequest.cc
index 7585ba2925af495c03ec5551045f98cc8b0f3b95..7df99b78ac9efdd85ac1638107a9b5ea33eafc4f 100644 (file)
@@ -5,7 +5,7 @@
 #include "test/librbd/test_mock_fixture.h"
 #include "test/librbd/test_support.h"
 #include "test/librbd/mock/MockImageCtx.h"
-#include "test/librbd/mock/crypto/MockCryptoInterface.h"
+#include "test/librbd/mock/crypto/MockEncryptionFormat.h"
 
 #include "librbd/crypto/ShutDownCryptoRequest.cc"
 
@@ -17,6 +17,8 @@ struct MockTestImageCtx : public MockImageCtx {
   MockTestImageCtx(librbd::ImageCtx &image_ctx)
     : librbd::MockImageCtx(image_ctx) {
   }
+
+  MockTestImageCtx *parent = nullptr;
 };
 
 } // anonymous namespace
@@ -35,7 +37,7 @@ struct TestMockShutDownCryptoRequest : public TestMockFixture {
   C_SaferCond finished_cond;
   Context *on_finish = &finished_cond;
   MockShutDownCryptoRequest* mock_shutdown_crypto_request;
-  MockCryptoInterface* crypto;
+  MockEncryptionFormat* mock_encryption_format;
   Context* shutdown_object_dispatch_context;
   Context* shutdown_image_dispatch_context;
 
@@ -45,38 +47,39 @@ struct TestMockShutDownCryptoRequest : public TestMockFixture {
     librbd::ImageCtx *ictx;
     ASSERT_EQ(0, open_image(m_image_name, &ictx));
     mock_image_ctx = new MockTestImageCtx(*ictx);
-    crypto = new MockCryptoInterface();
-    mock_image_ctx->crypto = crypto;
+    mock_encryption_format = new MockEncryptionFormat();
+    mock_image_ctx->encryption_format.reset(mock_encryption_format);
     mock_shutdown_crypto_request = MockShutDownCryptoRequest::create(
-          mock_image_ctx, on_finish);
+        mock_image_ctx, on_finish);
   }
 
   void TearDown() override {
-    crypto->put();
     delete mock_image_ctx;
     TestMockFixture::TearDown();
   }
 
-  void expect_crypto_object_layer_exists_check(bool exists) {
-    EXPECT_CALL(*mock_image_ctx->io_object_dispatcher, exists(
+  void expect_crypto_object_layer_exists_check(
+          MockTestImageCtx* image_ctx, bool exists) {
+    EXPECT_CALL(*image_ctx->io_object_dispatcher, exists(
             io::OBJECT_DISPATCH_LAYER_CRYPTO)).WillOnce(Return(exists));
   }
 
-  void expect_crypto_image_layer_exists_check(bool exists) {
-    EXPECT_CALL(*mock_image_ctx->io_image_dispatcher, exists(
+  void expect_crypto_image_layer_exists_check(
+          MockTestImageCtx* image_ctx, bool exists) {
+    EXPECT_CALL(*image_ctx->io_image_dispatcher, exists(
             io::IMAGE_DISPATCH_LAYER_CRYPTO)).WillOnce(Return(exists));
   }
 
-  void expect_shutdown_crypto_object_dispatch() {
-    EXPECT_CALL(*mock_image_ctx->io_object_dispatcher, shut_down_dispatch(
+  void expect_shutdown_crypto_object_dispatch(MockTestImageCtx* image_ctx) {
+    EXPECT_CALL(*image_ctx->io_object_dispatcher, shut_down_dispatch(
             io::OBJECT_DISPATCH_LAYER_CRYPTO, _)).WillOnce(
                     WithArgs<1>(Invoke([this](Context* ctx) {
                       shutdown_object_dispatch_context = ctx;
     })));
   }
 
-  void expect_shutdown_crypto_image_dispatch() {
-    EXPECT_CALL(*mock_image_ctx->io_image_dispatcher, shut_down_dispatch(
+  void expect_shutdown_crypto_image_dispatch(MockTestImageCtx* image_ctx) {
+    EXPECT_CALL(*image_ctx->io_image_dispatcher, shut_down_dispatch(
             io::IMAGE_DISPATCH_LAYER_CRYPTO, _)).WillOnce(
                     WithArgs<1>(Invoke([this](Context* ctx) {
                       shutdown_image_dispatch_context = ctx;
@@ -85,59 +88,86 @@ struct TestMockShutDownCryptoRequest : public TestMockFixture {
 };
 
 TEST_F(TestMockShutDownCryptoRequest, NoCryptoObjectDispatch) {
-  expect_crypto_object_layer_exists_check(false);
+  expect_crypto_object_layer_exists_check(mock_image_ctx, false);
   mock_shutdown_crypto_request->send();
   ASSERT_EQ(0, finished_cond.wait());
-  ASSERT_EQ(nullptr, mock_image_ctx->crypto);
+  ASSERT_EQ(nullptr, mock_image_ctx->encryption_format.get());
 }
 
 TEST_F(TestMockShutDownCryptoRequest, FailShutdownObjectDispatch) {
-  expect_crypto_object_layer_exists_check(true);
-  expect_shutdown_crypto_object_dispatch();
+  expect_crypto_object_layer_exists_check(mock_image_ctx, true);
+  expect_shutdown_crypto_object_dispatch(mock_image_ctx);
   mock_shutdown_crypto_request->send();
   ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
   shutdown_object_dispatch_context->complete(-EIO);
   ASSERT_EQ(-EIO, finished_cond.wait());
-  ASSERT_NE(nullptr, mock_image_ctx->crypto);
+  ASSERT_EQ(mock_encryption_format, mock_image_ctx->encryption_format.get());
 }
 
 TEST_F(TestMockShutDownCryptoRequest, NoCryptoImageDispatch) {
-  expect_crypto_object_layer_exists_check(true);
-  expect_shutdown_crypto_object_dispatch();
+  expect_crypto_object_layer_exists_check(mock_image_ctx, true);
+  expect_shutdown_crypto_object_dispatch(mock_image_ctx);
   mock_shutdown_crypto_request->send();
   ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
-  expect_crypto_image_layer_exists_check(false);
+  expect_crypto_image_layer_exists_check(mock_image_ctx, false);
   shutdown_object_dispatch_context->complete(0);
   ASSERT_EQ(0, finished_cond.wait());
-  ASSERT_EQ(nullptr, mock_image_ctx->crypto);
+  ASSERT_EQ(nullptr, mock_image_ctx->encryption_format.get());
 }
 
 TEST_F(TestMockShutDownCryptoRequest, FailShutdownImageDispatch) {
-  expect_crypto_object_layer_exists_check(true);
-  expect_shutdown_crypto_object_dispatch();
+  expect_crypto_object_layer_exists_check(mock_image_ctx, true);
+  expect_shutdown_crypto_object_dispatch(mock_image_ctx);
   mock_shutdown_crypto_request->send();
   ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
-  expect_crypto_image_layer_exists_check(true);
-  expect_shutdown_crypto_image_dispatch();
+  expect_crypto_image_layer_exists_check(mock_image_ctx, true);
+  expect_shutdown_crypto_image_dispatch(mock_image_ctx);
   shutdown_object_dispatch_context->complete(0);
   ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
   shutdown_image_dispatch_context->complete(-EIO);
   ASSERT_EQ(-EIO, finished_cond.wait());
-  ASSERT_NE(nullptr, mock_image_ctx->crypto);
+  ASSERT_EQ(mock_encryption_format, mock_image_ctx->encryption_format.get());
 }
 
 TEST_F(TestMockShutDownCryptoRequest, Success) {
-  expect_crypto_object_layer_exists_check(true);
-  expect_shutdown_crypto_object_dispatch();
+  expect_crypto_object_layer_exists_check(mock_image_ctx, true);
+  expect_shutdown_crypto_object_dispatch(mock_image_ctx);
   mock_shutdown_crypto_request->send();
   ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
-  expect_crypto_image_layer_exists_check(true);
-  expect_shutdown_crypto_image_dispatch();
+  expect_crypto_image_layer_exists_check(mock_image_ctx, true);
+  expect_shutdown_crypto_image_dispatch(mock_image_ctx);
+  shutdown_object_dispatch_context->complete(0);
+  ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
+  shutdown_image_dispatch_context->complete(0);
+  ASSERT_EQ(0, finished_cond.wait());
+  ASSERT_EQ(nullptr, mock_image_ctx->encryption_format.get());
+}
+
+TEST_F(TestMockShutDownCryptoRequest, ShutdownParent) {
+  auto parent_image_ctx = new MockTestImageCtx(*mock_image_ctx->image_ctx);
+  mock_image_ctx->parent = parent_image_ctx;
+  expect_crypto_object_layer_exists_check(mock_image_ctx, true);
+  expect_shutdown_crypto_object_dispatch(mock_image_ctx);
+  mock_shutdown_crypto_request->send();
+  ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
+  expect_crypto_image_layer_exists_check(mock_image_ctx, true);
+  expect_shutdown_crypto_image_dispatch(mock_image_ctx);
+  shutdown_object_dispatch_context->complete(0);
+  ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
+  expect_crypto_object_layer_exists_check(parent_image_ctx, true);
+  expect_shutdown_crypto_object_dispatch(parent_image_ctx);
+  shutdown_image_dispatch_context->complete(0);
+  ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
+  expect_crypto_image_layer_exists_check(parent_image_ctx, true);
+  expect_shutdown_crypto_image_dispatch(parent_image_ctx);
   shutdown_object_dispatch_context->complete(0);
   ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
+  mock_image_ctx->parent = nullptr;
   shutdown_image_dispatch_context->complete(0);
   ASSERT_EQ(0, finished_cond.wait());
-  ASSERT_EQ(nullptr, mock_image_ctx->crypto);
+  ASSERT_EQ(nullptr, mock_image_ctx->encryption_format.get());
+  ASSERT_EQ(nullptr, parent_image_ctx->encryption_format.get());
+  delete parent_image_ctx;
 }
 
 } // namespace crypto