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

#include "gtest/gtest.h"
#include "librbd/crypto/CryptoContextPool.h"
#include "test/librbd/mock/crypto/MockDataCryptor.h"

#include "librbd/crypto/CryptoContextPool.cc"
template class librbd::crypto::CryptoContextPool<
        librbd::crypto::MockCryptoContext>;

using ::testing::Return;

namespace librbd {
namespace crypto {

struct TestMockCryptoCryptoContextPool : public ::testing::Test {
    MockDataCryptor cryptor;

    void expect_get_context(CipherMode mode) {
      EXPECT_CALL(cryptor, get_context(mode)).WillOnce(Return(
              new MockCryptoContext()));
    }

    void expect_return_context(MockCryptoContext* ctx, CipherMode mode) {
      delete ctx;
      EXPECT_CALL(cryptor, return_context(ctx, mode));
    }
};

TEST_F(TestMockCryptoCryptoContextPool, Test) {
  CryptoContextPool<MockCryptoContext> pool(&cryptor, 1);

  expect_get_context(CipherMode::CIPHER_MODE_ENC);
  auto enc_ctx = pool.get_context(CipherMode::CIPHER_MODE_ENC);

  expect_get_context(CipherMode::CIPHER_MODE_DEC);
  auto dec_ctx1 = pool.get_context(CipherMode::CIPHER_MODE_DEC);
  expect_get_context(CipherMode::CIPHER_MODE_DEC);
  auto dec_ctx2 = pool.get_context(CipherMode::CIPHER_MODE_DEC);
  pool.return_context(dec_ctx1, CipherMode::CIPHER_MODE_DEC);
  expect_return_context(dec_ctx2, CipherMode::CIPHER_MODE_DEC);
  pool.return_context(dec_ctx2, CipherMode::CIPHER_MODE_DEC);

  pool.return_context(enc_ctx, CipherMode::CIPHER_MODE_ENC);
  ASSERT_EQ(enc_ctx, pool.get_context(CipherMode::CIPHER_MODE_ENC));
  pool.return_context(enc_ctx, CipherMode::CIPHER_MODE_ENC);

  expect_return_context(enc_ctx, CipherMode::CIPHER_MODE_ENC);
  expect_return_context(dec_ctx1, CipherMode::CIPHER_MODE_DEC);
}

} // namespace crypto
} // namespace librbd