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

randombit / botan / 23225340130

18 Mar 2026 01:53AM UTC coverage: 89.677% (-0.001%) from 89.678%
23225340130

push

github

web-flow
Merge pull request #5456 from randombit/jack/clang-tidy-22

Fix various warnings from clang-tidy 22

104438 of 116460 relevant lines covered (89.68%)

11819947.55 hits per line

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

97.6
/src/tests/test_psk_db.cpp
1
/*
2
* (C) 2017 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6

7
#include "tests.h"
8

9
#if defined(BOTAN_HAS_PSK_DB)
10

11
   #include <botan/psk_db.h>
12
   #include <botan/rng.h>
13
   #include <map>
14

15
   #if defined(BOTAN_HAS_SQLITE3)
16
      #include <botan/sqlite3.h>
17
   #endif
18

19
namespace Botan_Tests {
20

21
namespace {
22

23
class Test_Map_PSK_Db : public Botan::Encrypted_PSK_Database {
1✔
24
   public:
25
      explicit Test_Map_PSK_Db(const Botan::secure_vector<uint8_t>& master_key) :
1✔
26
            Botan::Encrypted_PSK_Database(master_key) {}
1✔
27

28
      void test_entry(Test::Result& result, const std::string& index, const std::string& value) {
6✔
29
         auto i = m_vals.find(index);
6✔
30

31
         if(i == m_vals.end()) {
6✔
32
            result.test_failure("Expected to find encrypted name " + index);
×
33
         } else {
34
            result.test_str_eq("Encrypted value", i->second, value);
6✔
35
         }
36
      }
6✔
37

38
      void kv_set(std::string_view index, std::string_view value) override {
6✔
39
         m_vals.insert_or_assign(std::string(index), std::string(value));
18✔
40
      }
6✔
41

42
      std::string kv_get(std::string_view index) const override {
7✔
43
         auto i = m_vals.find(index);
7✔
44
         if(i == m_vals.end()) {
7✔
45
            return "";
1✔
46
         }
47
         return i->second;
6✔
48
      }
49

50
      void kv_del(std::string_view index) override {
2✔
51
         auto i = m_vals.find(index);
2✔
52
         if(i != m_vals.end()) {
2✔
53
            m_vals.erase(i);
1✔
54
         }
55
      }
2✔
56

57
      std::set<std::string> kv_get_all() const override {
2✔
58
         std::set<std::string> names;
2✔
59

60
         for(const auto& kv : m_vals) {
7✔
61
            names.insert(kv.first);
5✔
62
         }
63

64
         return names;
2✔
65
      }
×
66

67
   private:
68
      std::map<std::string, std::string, std::less<>> m_vals;
69
};
70

71
class PSK_DB_Tests final : public Test {
1✔
72
   public:
73
      std::vector<Test::Result> run() override {
1✔
74
         std::vector<Test::Result> results;
1✔
75

76
         results.push_back(test_psk_db());
2✔
77

78
   #if defined(BOTAN_HAS_SQLITE3)
79
         results.push_back(test_psk_sql_db());
2✔
80
   #endif
81

82
         return results;
1✔
83
      }
×
84

85
   private:
86
      static Test::Result test_psk_db() {
1✔
87
         Test::Result result("PSK_DB");
1✔
88

89
         const Botan::secure_vector<uint8_t> zeros(32);
1✔
90
         Test_Map_PSK_Db db(zeros);
1✔
91

92
         db.set_str("name", "value");
1✔
93
         db.test_entry(result, "CUCJjJgWSa079ubutJQwlw==", "clYJSAf9CThuL96CP+rAfA==");
2✔
94
         result.test_str_eq("DB read", db.get_str("name"), "value");
1✔
95

96
         db.set_str("name", "value1");
1✔
97
         db.test_entry(result, "CUCJjJgWSa079ubutJQwlw==", "7R8am3x/gLawOzMp5WwIJg==");
2✔
98
         result.test_str_eq("DB read", db.get_str("name"), "value1");
1✔
99

100
         db.set_str("name", "value");
1✔
101
         db.test_entry(result, "CUCJjJgWSa079ubutJQwlw==", "clYJSAf9CThuL96CP+rAfA==");
2✔
102
         result.test_str_eq("DB read", db.get_str("name"), "value");
1✔
103

104
         db.set_str("name2", "value");
1✔
105
         db.test_entry(result, "7CvsM7HDCZsV6VsFwWylNg==", "BqVQo4rdwOmf+ItCzEmjAg==");
2✔
106
         result.test_str_eq("DB read", db.get_str("name2"), "value");
1✔
107

108
         db.set_vec("name2", zeros);
1✔
109
         db.test_entry(result, "7CvsM7HDCZsV6VsFwWylNg==", "x+I1bUF/fJYPOTvKwOihEPWGR1XGzVuyRdsw4n5gpBRzNR7LjH7vjw==");
2✔
110
         result.test_bin_eq("DB read", db.get("name2"), zeros);
1✔
111

112
         // Test longer names
113
         db.set_str("leroy jeeeeeeeenkins", "chicken");
1✔
114
         db.test_entry(result, "KyYo272vlSjClM2F0OZBMlRYjr33ZXv2jN1oY8OfCEs=", "tCl1qShSTsXi9tA5Kpo9vg==");
2✔
115
         result.test_str_eq("DB read", db.get_str("leroy jeeeeeeeenkins"), "chicken");
1✔
116

117
         std::set<std::string> all_names = db.list_names();
1✔
118

119
         result.test_sz_eq("Expected number of names", all_names.size(), 3);
1✔
120
         result.test_sz_eq("Have expected name", all_names.count("name"), 1);
2✔
121
         result.test_sz_eq("Have expected name", all_names.count("name2"), 1);
2✔
122
         result.test_sz_eq("Have expected name", all_names.count("leroy jeeeeeeeenkins"), 1);
2✔
123

124
         db.remove("name2");
1✔
125

126
         all_names = db.list_names();
2✔
127

128
         result.test_sz_eq("Expected number of names", all_names.size(), 2);
1✔
129
         result.test_sz_eq("Have expected name", all_names.count("name"), 1);
2✔
130
         result.test_sz_eq("Have expected name", all_names.count("leroy jeeeeeeeenkins"), 1);
2✔
131

132
         result.test_throws(
1✔
133
            "exception if get called on non-existent PSK", "Named PSK not located", [&]() { db.get("name2"); });
2✔
134

135
         // test that redundant remove calls accepted
136
         db.remove("name2");
1✔
137

138
         return result;
1✔
139
      }
3✔
140

141
   #if defined(BOTAN_HAS_SQLITE3)
142

143
      void test_entry(Test::Result& result,
6✔
144
                      Botan::SQL_Database& db,
145
                      const std::string& table,
146
                      const std::string& expected_name,
147
                      const std::string& expected_value) {
148
         auto stmt = db.new_statement("select psk_value from " + table + " where psk_name='" + expected_name + "'");
30✔
149

150
         const bool got_it = stmt->step();
6✔
151
         result.test_is_true("Had expected name", got_it);
6✔
152

153
         if(got_it) {
6✔
154
            result.test_str_eq("Had expected value", stmt->get_str(0), expected_value);
6✔
155
         }
156
      }
6✔
157

158
      Test::Result test_psk_sql_db() {
1✔
159
         Test::Result result("PSK_DB SQL");
1✔
160

161
         const Botan::secure_vector<uint8_t> zeros(32);
1✔
162
         const Botan::secure_vector<uint8_t> not_zeros = this->rng().random_vec(32);
1✔
163

164
         const std::string table_name = "bobby";
1✔
165
         const std::shared_ptr<Botan::SQL_Database> sqldb = std::make_shared<Botan::Sqlite3_Database>(":memory:");
1✔
166

167
         Botan::Encrypted_PSK_Database_SQL db(zeros, sqldb, table_name);
1✔
168
         db.set_str("name", "value");
1✔
169

170
         test_entry(result, *sqldb, table_name, "CUCJjJgWSa079ubutJQwlw==", "clYJSAf9CThuL96CP+rAfA==");
2✔
171
         result.test_str_eq("DB read", db.get_str("name"), "value");
1✔
172

173
         db.set_str("name", "value1");
1✔
174
         test_entry(result, *sqldb, table_name, "CUCJjJgWSa079ubutJQwlw==", "7R8am3x/gLawOzMp5WwIJg==");
2✔
175
         result.test_str_eq("DB read", db.get_str("name"), "value1");
1✔
176

177
         db.set_str("name", "value");
1✔
178
         test_entry(result, *sqldb, table_name, "CUCJjJgWSa079ubutJQwlw==", "clYJSAf9CThuL96CP+rAfA==");
2✔
179
         result.test_str_eq("DB read", db.get_str("name"), "value");
1✔
180

181
         db.set_str("name2", "value");
1✔
182
         test_entry(result, *sqldb, table_name, "7CvsM7HDCZsV6VsFwWylNg==", "BqVQo4rdwOmf+ItCzEmjAg==");
2✔
183
         result.test_str_eq("DB read", db.get_str("name2"), "value");
1✔
184

185
         db.set_vec("name2", zeros);
1✔
186
         test_entry(result,
2✔
187
                    *sqldb,
1✔
188
                    table_name,
189
                    "7CvsM7HDCZsV6VsFwWylNg==",
190
                    "x+I1bUF/fJYPOTvKwOihEPWGR1XGzVuyRdsw4n5gpBRzNR7LjH7vjw==");
191
         result.test_bin_eq("DB read", db.get("name2"), zeros);
1✔
192

193
         // Test longer names
194
         db.set_str("leroy jeeeeeeeenkins", "chicken");
1✔
195
         test_entry(
2✔
196
            result, *sqldb, table_name, "KyYo272vlSjClM2F0OZBMlRYjr33ZXv2jN1oY8OfCEs=", "tCl1qShSTsXi9tA5Kpo9vg==");
1✔
197
         result.test_str_eq("DB read", db.get_str("leroy jeeeeeeeenkins"), "chicken");
1✔
198

199
         /*
200
         * Test that we can have another database in the same table with distinct key
201
         * without any problems.
202
         */
203
         Botan::Encrypted_PSK_Database_SQL db2(not_zeros, sqldb, table_name);
1✔
204
         db2.set_str("name", "price&value");
1✔
205
         result.test_str_eq("DB read", db2.get_str("name"), "price&value");
1✔
206
         result.test_sz_eq("DB2 size", db2.list_names().size(), 1);
1✔
207

208
         std::set<std::string> all_names = db.list_names();
1✔
209

210
         result.test_sz_eq("Expected number of names", all_names.size(), 3);
1✔
211
         result.test_sz_eq("Have expected name", all_names.count("name"), 1);
2✔
212
         result.test_sz_eq("Have expected name", all_names.count("name2"), 1);
2✔
213
         result.test_sz_eq("Have expected name", all_names.count("leroy jeeeeeeeenkins"), 1);
2✔
214

215
         db.remove("name2");
1✔
216

217
         all_names = db.list_names();
2✔
218

219
         result.test_sz_eq("Expected number of names", all_names.size(), 2);
1✔
220
         result.test_sz_eq("Have expected name", all_names.count("name"), 1);
2✔
221
         result.test_sz_eq("Have expected name", all_names.count("leroy jeeeeeeeenkins"), 1);
2✔
222

223
         result.test_throws(
1✔
224
            "exception if get called on non-existent PSK", "Named PSK not located", [&]() { db.get("name2"); });
2✔
225

226
         // test that redundant remove calls accepted
227
         db.remove("name2");
1✔
228

229
         return result;
1✔
230
      }
4✔
231
   #endif
232
};
233

234
BOTAN_REGISTER_TEST("misc", "psk_db", PSK_DB_Tests);
235

236
}  // namespace
237

238
}  // namespace Botan_Tests
239

240
#endif
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

© 2026 Coveralls, Inc