Debian Codegraph @

unittest/gunit/components/keyring_common/backend_mem.h

raw symbols SCIP
1 /* Copyright (c) 2021, 2026, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24 #ifndef BACKEND_MEM_INCLUDED
25 #define BACKEND_MEM_INCLUDED
26
27 #include "components/keyrings/common/memstore/iterator.h"
28 #include "components/keyrings/common/operations/operations.h"
29 #include "components/keyrings/common/utils/utils.h"
30
31 namespace keyring_common_unit {
32 class Memory_backend final {
33 public:
34 Memory_backend() = default;
35
36 virtual ~Memory_backend() = default;
37
38 bool get(const keyring_common::meta::Metadata &metadata,
39 keyring_common::data::Data &data) const {
40 if (!metadata.valid()) return true;
41 return !cache_.get(metadata, data);
42 }
43
44 bool store(const keyring_common::meta::Metadata &metadata,
45 const keyring_common::data::Data &data) {
46 if (!metadata.valid() || !data.valid()) return true;
47 return !cache_.store(metadata, data);
48 }
49
50 bool erase(const keyring_common::meta::Metadata &metadata,
51 const keyring_common::data::Data &data) {
52 if (!metadata.valid()) return true;
53 (void)data;
54 return !cache_.erase(metadata);
55 }
56
57 bool generate(const keyring_common::meta::Metadata &metadata,
58 keyring_common::data::Data &data, size_t length) {
59 if (!metadata.valid()) return true;
60 if (cache_.get(metadata, data)) return true;
61
62 std::unique_ptr<unsigned char[]> key(new unsigned char[length]);
63 if (!key) return true;
64 if (!keyring_common::utils::get_random_data(key, length)) return true;
65
66 std::string key_str;
67 key_str.assign(reinterpret_cast<const char *>(key.get()), length);
68 data.set_data(key_str);
69 if (!cache_.store(metadata, data)) return true;
70
71 return false;
72 }
73
74 bool load_cache(
75 keyring_common::operations::Keyring_operations<Memory_backend> &) {
76 return false;
77 }
78
79 size_t maximum_data_length() const { return 16384; }
80
81 size_t size() const { return cache_.size(); }
82
83 private:
84 /** In memory cache for keyring data */
85 keyring_common::cache::Datacache<keyring_common::data::Data> cache_;
86 };
87 } // namespace keyring_common_unit
88
89 #endif // !BACKEND_MEM_INCLUDED