summaryrefslogtreecommitdiffstats
path: root/src/test/librbd/crypto/test_mock_ShutDownCryptoRequest.cc
blob: 7585ba2925af495c03ec5551045f98cc8b0f3b95 (plain)
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
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#include "librbd/crypto/Utils.h"
#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 "librbd/crypto/ShutDownCryptoRequest.cc"

namespace librbd {

namespace {

struct MockTestImageCtx : public MockImageCtx {
  MockTestImageCtx(librbd::ImageCtx &image_ctx)
    : librbd::MockImageCtx(image_ctx) {
  }
};

} // anonymous namespace

namespace crypto {

using ::testing::_;
using ::testing::Invoke;
using ::testing::Return;
using ::testing::WithArgs;

struct TestMockShutDownCryptoRequest : public TestMockFixture {
  typedef ShutDownCryptoRequest<MockTestImageCtx> MockShutDownCryptoRequest;

  MockTestImageCtx* mock_image_ctx;
  C_SaferCond finished_cond;
  Context *on_finish = &finished_cond;
  MockShutDownCryptoRequest* mock_shutdown_crypto_request;
  MockCryptoInterface* crypto;
  Context* shutdown_object_dispatch_context;
  Context* shutdown_image_dispatch_context;

  void SetUp() override {
    TestMockFixture::SetUp();

    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_shutdown_crypto_request = MockShutDownCryptoRequest::create(
          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(
            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(
            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(
            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(
            io::IMAGE_DISPATCH_LAYER_CRYPTO, _)).WillOnce(
                    WithArgs<1>(Invoke([this](Context* ctx) {
                      shutdown_image_dispatch_context = ctx;
    })));
  }
};

TEST_F(TestMockShutDownCryptoRequest, NoCryptoObjectDispatch) {
  expect_crypto_object_layer_exists_check(false);
  mock_shutdown_crypto_request->send();
  ASSERT_EQ(0, finished_cond.wait());
  ASSERT_EQ(nullptr, mock_image_ctx->crypto);
}

TEST_F(TestMockShutDownCryptoRequest, FailShutdownObjectDispatch) {
  expect_crypto_object_layer_exists_check(true);
  expect_shutdown_crypto_object_dispatch();
  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);
}

TEST_F(TestMockShutDownCryptoRequest, NoCryptoImageDispatch) {
  expect_crypto_object_layer_exists_check(true);
  expect_shutdown_crypto_object_dispatch();
  mock_shutdown_crypto_request->send();
  ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
  expect_crypto_image_layer_exists_check(false);
  shutdown_object_dispatch_context->complete(0);
  ASSERT_EQ(0, finished_cond.wait());
  ASSERT_EQ(nullptr, mock_image_ctx->crypto);
}

TEST_F(TestMockShutDownCryptoRequest, FailShutdownImageDispatch) {
  expect_crypto_object_layer_exists_check(true);
  expect_shutdown_crypto_object_dispatch();
  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();
  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);
}

TEST_F(TestMockShutDownCryptoRequest, Success) {
  expect_crypto_object_layer_exists_check(true);
  expect_shutdown_crypto_object_dispatch();
  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();
  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->crypto);
}

} // namespace crypto
} // namespace librbd