summaryrefslogtreecommitdiff
path: root/include/ossl_test_memory.hpp
blob: 5b3f330db7d2c5b5610bdf1f677bbe5d65840c1e (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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright OpenBMC Authors

#pragma once

#include <gtest/gtest.h>

extern "C"
{
#include <openssl/crypto.h>
}

static void* osslMalloc(size_t num, const char* /*file*/, int /*line*/)
{
    // NOLINTNEXTLINE(cppcoreguidelines-no-malloc)
    return ::malloc(num);
}
static void* osslRealloc(void* ptr, size_t num, const char* /*file*/,
                         int /*line*/)
{
    // NOLINTNEXTLINE(cppcoreguidelines-no-malloc)
    return ::realloc(ptr, num);
}
static void osslFree(void* ptr, const char* /*file*/, int /*line*/)
{
    // NOLINTNEXTLINE(cppcoreguidelines-no-malloc)
    ::free(ptr);
}

class OpenSSLTestMemory
{
  public:
    OpenSSLTestMemory() noexcept
    {
        if (CRYPTO_set_mem_functions(osslMalloc, osslRealloc, osslFree) != 1)
        {
            BMCWEB_LOG_ERROR("Failed to set OpenSSL memory functions");
        }
    }
};