aboutsummaryrefslogtreecommitdiff
path: root/components/service/crypto/client/test/test_crypto_client.cpp
blob: 3217c61ac3153dda6d21c33cc203337af473e504 (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
/*
 * Copyright (c) 2020-2021, Arm Limited and Contributors. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <cassert>
#include "test_crypto_client.h"

test_crypto_client::factory *test_crypto_client::m_default_factory = NULL;

test_crypto_client::test_crypto_client() :
    protobuf_crypto_client(),
    m_is_initialized(false),
    m_injected_faults()
{

}

test_crypto_client::~test_crypto_client()
{
    deinit();
}

bool test_crypto_client::init()
{
    bool success = !m_is_initialized;
    m_is_initialized = true;
    return success;
}

bool test_crypto_client::deinit()
{
    bool success = m_is_initialized;
    m_is_initialized = false;
    return success;
}

test_crypto_client *test_crypto_client::create_default()
{
    /*
     * It's mandatory to include a concrete test_crypto_client
     * that registers its own factory in a deployment if you
     * want to use this factory method.
     */
    assert(m_default_factory);
    return m_default_factory->create();
}

void test_crypto_client::register_factory(factory *factory)
{
    /*
     * Don't allow overriding of an existing default.  This
     * will happen if two test_crypto_client components have
     * been included in a deployment.
     */
    assert(!m_default_factory);
    m_default_factory = factory;
}

void test_crypto_client::deregister_factory(factory *factory)
{
    if (m_default_factory == factory) m_default_factory = NULL;
}

bool test_crypto_client::inject_fault(enum fault_code code)
{
    assert(!m_is_initialized);

    bool is_supported = is_fault_supported(code);
    if (is_supported) m_injected_faults.push_back(code);
    return is_supported;
}

bool test_crypto_client::is_fault_supported(enum fault_code code) const
{
    /* Derived classes may override this if fault simualtion is supported */
    (void)code;
    return false;
}

bool test_crypto_client::is_fault_injected(enum fault_code code) const
{
    bool is_injected = false;

    for (size_t i = 0; !is_injected && i < m_injected_faults.size(); ++i) {
        is_injected = (m_injected_faults[i] == code);
    }

    return is_injected;
}

/*
 * Test methods by default are not supported.  Calling a non-supported
 * method will trigger an assert.  A class derived from this one may
 * pick and choose which test methods it supports.
 */
bool test_crypto_client::keystore_reset_is_supported() const { return false; }
void test_crypto_client::keystore_reset() { assert(false); }

bool test_crypto_client::keystore_key_exists_is_supported() const { return false; }
bool test_crypto_client::keystore_key_exists(uint32_t id) const { (void)id; assert(false); return false; }

bool test_crypto_client::keystore_keys_held_is_supported() const { return false; }
size_t test_crypto_client::keystore_keys_held() const { assert(false); return 0; }