summaryrefslogtreecommitdiffstats
path: root/src/test/librbd/crypto/test_mock_FormatRequest.cc
blob: 2008deb68c2c74addd87745b501c70b415977405 (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#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/Utils.h"

namespace librbd {
namespace util {

inline ImageCtx *get_image_ctx(MockImageCtx *image_ctx) {
  return image_ctx->image_ctx;
}

} // namespace util
} // namespace librbd

#include "librbd/crypto/FormatRequest.cc"

namespace librbd {
namespace crypto {

namespace util {

template <> void set_crypto(
        MockImageCtx *image_ctx, ceph::ref_t<CryptoInterface> crypto) {
  image_ctx->crypto = crypto.get();
}

} // namespace util

using ::testing::_;
using ::testing::Invoke;
using ::testing::Return;
using ::testing::WithArg;

template <>
struct ShutDownCryptoRequest<MockImageCtx> {
  Context *on_finish = nullptr;
  static ShutDownCryptoRequest *s_instance;
  static ShutDownCryptoRequest *create(
          MockImageCtx* image_ctx, Context *on_finish) {
    ceph_assert(s_instance != nullptr);
    s_instance->on_finish = on_finish;
    return s_instance;
  }

  MOCK_METHOD0(send, void());

  ShutDownCryptoRequest() {
    s_instance = this;
  }
};

ShutDownCryptoRequest<MockImageCtx> *ShutDownCryptoRequest<
        MockImageCtx>::s_instance = nullptr;

struct TestMockCryptoFormatRequest : public TestMockFixture {
  typedef FormatRequest<librbd::MockImageCtx> MockFormatRequest;
  typedef ShutDownCryptoRequest<MockImageCtx> MockShutDownCryptoRequest;

  MockImageCtx* mock_image_ctx;
  C_SaferCond finished_cond;
  Context *on_finish = &finished_cond;
  MockEncryptionFormat* mock_encryption_format;
  Context* format_context;
  MockCryptoInterface* crypto;
  MockCryptoInterface* old_crypto;
  MockFormatRequest* mock_format_request;
  std::string key = std::string(64, '0');

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

    librbd::ImageCtx *ictx;
    ASSERT_EQ(0, open_image(m_image_name, &ictx));
    mock_image_ctx = new MockImageCtx(*ictx);
    mock_encryption_format = new MockEncryptionFormat();
    crypto = new MockCryptoInterface();
    old_crypto = new MockCryptoInterface();
    mock_image_ctx->crypto = old_crypto;
    mock_format_request = MockFormatRequest::create(
          mock_image_ctx,
          std::unique_ptr<MockEncryptionFormat>(mock_encryption_format),
          on_finish);
  }

  void TearDown() override {
    crypto->put();
    old_crypto->put();
    delete mock_image_ctx;
    TestMockFixture::TearDown();
  }

  void expect_test_journal_feature(bool has_journal=false) {
    EXPECT_CALL(*mock_image_ctx, test_features(
            RBD_FEATURE_JOURNALING)).WillOnce(Return(has_journal));
  }

  void expect_encryption_format() {
    EXPECT_CALL(*mock_encryption_format, format(
            mock_image_ctx, _)).WillOnce(
                    WithArg<1>(Invoke([this](Context* ctx) {
                      format_context = ctx;
    })));
  }

  void expect_image_flush(int r) {
    EXPECT_CALL(*mock_image_ctx->io_image_dispatcher, send(_)).WillOnce(
            Invoke([r](io::ImageDispatchSpec* spec) {
              ASSERT_TRUE(boost::get<io::ImageDispatchSpec::Flush>(
                      &spec->request) != nullptr);
              spec->dispatch_result = io::DISPATCH_RESULT_COMPLETE;
              spec->aio_comp->set_request_count(1);
              spec->aio_comp->add_request();
              spec->aio_comp->complete_request(r);
            }));
  }
};

TEST_F(TestMockCryptoFormatRequest, JournalEnabled) {
  expect_test_journal_feature(true);
  mock_format_request->send();
  ASSERT_EQ(-ENOTSUP, finished_cond.wait());
  ASSERT_EQ(old_crypto, mock_image_ctx->crypto);
}

TEST_F(TestMockCryptoFormatRequest, FailShutDownCrypto) {
  expect_test_journal_feature(false);
  MockShutDownCryptoRequest mock_shutdown_crypto_request;
  EXPECT_CALL(mock_shutdown_crypto_request, send());
  mock_format_request->send();
  ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
  mock_shutdown_crypto_request.on_finish->complete(-EIO);
  ASSERT_EQ(-EIO, finished_cond.wait());
  ASSERT_EQ(old_crypto, mock_image_ctx->crypto);
}

TEST_F(TestMockCryptoFormatRequest, FormatFail) {
  mock_image_ctx->crypto = nullptr;
  expect_test_journal_feature(false);
  expect_encryption_format();
  mock_format_request->send();
  ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
  format_context->complete(-EIO);
  ASSERT_EQ(-EIO, finished_cond.wait());
  ASSERT_EQ(nullptr, mock_image_ctx->crypto);
}

TEST_F(TestMockCryptoFormatRequest, Success) {
  mock_image_ctx->crypto = nullptr;
  expect_test_journal_feature(false);
  expect_encryption_format();
  mock_format_request->send();
  ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
  expect_image_flush(0);
  EXPECT_CALL(*mock_encryption_format, get_crypto()).WillOnce(Return(crypto));
  format_context->complete(0);
  ASSERT_EQ(0, finished_cond.wait());
  ASSERT_EQ(crypto, mock_image_ctx->crypto);
}

TEST_F(TestMockCryptoFormatRequest, FailFlush) {
  mock_image_ctx->crypto = nullptr;
  expect_test_journal_feature(false);
  expect_encryption_format();
  mock_format_request->send();
  ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
  expect_image_flush(-EIO);
  format_context->complete(0);
  ASSERT_EQ(-EIO, finished_cond.wait());
  ASSERT_EQ(nullptr, mock_image_ctx->crypto);
}

TEST_F(TestMockCryptoFormatRequest, CryptoAlreadyLoaded) {
  expect_test_journal_feature(false);
  MockShutDownCryptoRequest mock_shutdown_crypto_request;
  EXPECT_CALL(mock_shutdown_crypto_request, send());
  mock_format_request->send();
  ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
  expect_encryption_format();
  mock_shutdown_crypto_request.on_finish->complete(0);
  ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
  expect_image_flush(0);
  EXPECT_CALL(*mock_encryption_format, get_crypto()).WillOnce(Return(crypto));
  format_context->complete(0);
  ASSERT_EQ(0, finished_cond.wait());
  ASSERT_EQ(crypto, mock_image_ctx->crypto);
}

} // namespace crypto
} // namespace librbd