• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / mender / 1047543048

24 Oct 2023 12:15PM UTC coverage: 80.215% (-0.01%) from 80.226%
1047543048

push

gitlab-ci

vpodzime
fix(https): Enable server host name verification in TLS handshake

It's not done automatically and we need to use a callback for
this. However, it's a common thing so Boost has the callback
(functor object) ready for us.

Ticket: MEN-6788
Changelog: none
Signed-off-by: Vratislav Podzimek <v.podzimek@mykolab.com>

6 of 6 new or added lines in 1 file covered. (100.0%)

6872 of 8567 relevant lines covered (80.21%)

9389.98 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.0
/common/key_value_database/platform/lmdb/lmdb.cpp
1
// Copyright 2023 Northern.tech AS
2
//
3
//    Licensed under the Apache License, Version 2.0 (the "License");
4
//    you may not use this file except in compliance with the License.
5
//    You may obtain a copy of the License at
6
//
7
//        http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//    Unless required by applicable law or agreed to in writing, software
10
//    distributed under the License is distributed on an "AS IS" BASIS,
11
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//    See the License for the specific language governing permissions and
13
//    limitations under the License.
14

15
#include <common/common.hpp>
16
#include <common/key_value_database_lmdb.hpp>
17

18
#include <lmdb++.h>
19

20
namespace mender {
21
namespace common {
22
namespace key_value_database {
23

24
class LmdbTransaction : public Transaction {
2,155✔
25
public:
26
        LmdbTransaction(lmdb::txn &txn, lmdb::dbi &dbi);
27

28
        expected::ExpectedBytes Read(const string &key) override;
29
        error::Error Write(const string &key, const vector<uint8_t> &value) override;
30
        error::Error Remove(const string &key) override;
31

32
private:
33
        lmdb::txn &txn_;
34
        lmdb::dbi &dbi_;
35
};
36

37
LmdbTransaction::LmdbTransaction(lmdb::txn &txn, lmdb::dbi &dbi) :
2,155✔
38
        txn_ {txn},
39
        dbi_ {dbi} {
2,155✔
40
}
×
41

42
expected::ExpectedBytes LmdbTransaction::Read(const string &key) {
1,349✔
43
        try {
44
                std::string_view value;
1,349✔
45
                bool exists = dbi_.get(txn_, key, value);
1,349✔
46
                if (!exists) {
1,349✔
47
                        return expected::unexpected(
810✔
48
                                MakeError(KeyError, "Key " + key + " not found in database"));
2,430✔
49
                }
50

51
                return common::ByteVectorFromString(value);
1,078✔
52
        } catch (std::runtime_error &e) {
×
53
                return expected::unexpected(MakeError(LmdbError, e.what()));
×
54
        }
55
}
56

57
error::Error LmdbTransaction::Write(const string &key, const vector<uint8_t> &value) {
1,017✔
58
        try {
59
                string_view data(reinterpret_cast<const char *>(value.data()), value.size());
60
                bool exists = dbi_.put(txn_, key, data);
1,017✔
61
                if (!exists) {
1,017✔
62
                        return MakeError(AlreadyExistsError, "Key " + key + " already exists");
×
63
                }
64

65
                return error::NoError;
1,017✔
66
        } catch (std::runtime_error &e) {
×
67
                return MakeError(LmdbError, e.what());
×
68
        }
69
}
70

71
error::Error LmdbTransaction::Remove(const string &key) {
1,583✔
72
        try {
73
                // We don't treat !exists as an error, just ignore return code.
74
                dbi_.del(txn_, key);
1,583✔
75

76
                return error::NoError;
1,583✔
77
        } catch (std::runtime_error &e) {
×
78
                return MakeError(LmdbError, e.what());
×
79
        }
80
}
81

82
KeyValueDatabaseLmdb::KeyValueDatabaseLmdb() :
386✔
83
        env_ {make_unique<lmdb::env>(lmdb::env::create())} {
772✔
84
}
386✔
85

86
KeyValueDatabaseLmdb::~KeyValueDatabaseLmdb() {
772✔
87
        Close();
386✔
88
}
386✔
89

90
error::Error KeyValueDatabaseLmdb::Open(const string &path) {
359✔
91
        Close();
359✔
92

93
        try {
94
                env_->open(path.c_str(), MDB_NOSUBDIR, 0600);
359✔
95
        } catch (std::runtime_error &e) {
4✔
96
                return MakeError(LmdbError, e.what());
4✔
97
        }
98

99
        successfully_opened_ = true;
357✔
100
        return error::NoError;
357✔
101
}
102

103
void KeyValueDatabaseLmdb::Close() {
746✔
104
        if (!successfully_opened_) {
746✔
105
                return;
106
        }
107

108
        env_->close();
357✔
109
        successfully_opened_ = false;
357✔
110
}
111

112
expected::ExpectedBytes KeyValueDatabaseLmdb::Read(const string &key) {
168✔
113
        vector<uint8_t> ret;
114
        auto err = ReadTransaction([&key, &ret](Transaction &txn) -> error::Error {
216✔
115
                auto result = txn.Read(key);
168✔
116
                if (result) {
168✔
117
                        ret = std::move(result.value());
48✔
118
                        return error::NoError;
48✔
119
                } else {
120
                        return result.error();
120✔
121
                }
122
        });
168✔
123
        if (mender::common::error::NoError != err) {
168✔
124
                return expected::unexpected(err);
240✔
125
        } else {
126
                return ret;
48✔
127
        }
128
}
129

130
error::Error KeyValueDatabaseLmdb::Write(const string &key, const vector<uint8_t> &value) {
210✔
131
        return WriteTransaction(
132
                [&key, &value](Transaction &txn) -> error::Error { return txn.Write(key, value); });
630✔
133
}
134

135
error::Error KeyValueDatabaseLmdb::Remove(const string &key) {
658✔
136
        return WriteTransaction([&key](Transaction &txn) -> error::Error { return txn.Remove(key); });
1,974✔
137
}
138

139
error::Error KeyValueDatabaseLmdb::WriteTransaction(function<error::Error(Transaction &)> txnFunc) {
1,703✔
140
        AssertOrReturnError(successfully_opened_);
1,703✔
141

142
        try {
143
                lmdb::txn lmdb_txn = lmdb::txn::begin(*env_, nullptr, 0);
3,406✔
144
                lmdb::dbi lmdb_dbi = lmdb::dbi::open(lmdb_txn, nullptr, 0);
1,703✔
145
                LmdbTransaction txn(lmdb_txn, lmdb_dbi);
146
                auto error = txnFunc(txn);
1,703✔
147
                if (error::NoError != error) {
1,703✔
148
                        lmdb_txn.abort();
56✔
149
                } else {
150
                        lmdb_txn.commit();
1,647✔
151
                }
152
                return error;
1,703✔
153
        } catch (std::runtime_error &e) {
×
154
                return MakeError(LmdbError, e.what());
×
155
        }
156
}
157

158
error::Error KeyValueDatabaseLmdb::ReadTransaction(function<error::Error(Transaction &)> txnFunc) {
453✔
159
        AssertOrReturnError(successfully_opened_);
454✔
160

161
        try {
162
                lmdb::txn lmdb_txn = lmdb::txn::begin(*env_, nullptr, MDB_RDONLY);
904✔
163
                lmdb::dbi lmdb_dbi = lmdb::dbi::open(lmdb_txn, nullptr, 0);
452✔
164
                LmdbTransaction txn(lmdb_txn, lmdb_dbi);
165
                return txnFunc(txn);
452✔
166
        } catch (std::runtime_error &e) {
×
167
                return MakeError(LmdbError, e.what());
×
168
        }
169
}
170

171
} // namespace key_value_database
172
} // namespace common
173
} // namespace mender
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc