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

randombit / botan / 20579846577

29 Dec 2025 06:24PM UTC coverage: 90.415% (+0.2%) from 90.243%
20579846577

push

github

web-flow
Merge pull request #5167 from randombit/jack/src-size-reductions

Changes to reduce unnecessary inclusions

101523 of 112285 relevant lines covered (90.42%)

12817276.56 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_eq("Encrypted value", i->second, value);
12✔
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
}  // namespace
72

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

78
         results.push_back(test_psk_db());
2✔
79

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

84
         return results;
1✔
85
      }
×
86

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

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

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

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

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

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

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

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

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

121
         result.test_eq("Expected number of names", all_names.size(), 3);
1✔
122
         result.test_eq("Have expected name", all_names.count("name"), 1);
3✔
123
         result.test_eq("Have expected name", all_names.count("name2"), 1);
3✔
124
         result.test_eq("Have expected name", all_names.count("leroy jeeeeeeeenkins"), 1);
3✔
125

126
         db.remove("name2");
1✔
127

128
         all_names = db.list_names();
2✔
129

130
         result.test_eq("Expected number of names", all_names.size(), 2);
1✔
131
         result.test_eq("Have expected name", all_names.count("name"), 1);
3✔
132
         result.test_eq("Have expected name", all_names.count("leroy jeeeeeeeenkins"), 1);
3✔
133

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

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

140
         return result;
1✔
141
      }
3✔
142

143
   #if defined(BOTAN_HAS_SQLITE3)
144

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

152
         const bool got_it = stmt->step();
6✔
153
         result.confirm("Had expected name", got_it);
12✔
154

155
         if(got_it) {
6✔
156
            result.test_eq("Had expected value", stmt->get_str(0), expected_value);
12✔
157
         }
158
      }
6✔
159

160
      Test::Result test_psk_sql_db() {
1✔
161
         Test::Result result("PSK_DB SQL");
1✔
162

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

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

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

172
         test_entry(result, *sqldb, table_name, "CUCJjJgWSa079ubutJQwlw==", "clYJSAf9CThuL96CP+rAfA==");
2✔
173
         result.test_eq("DB read", db.get_str("name"), "value");
2✔
174

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

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

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

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

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

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

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

212
         result.test_eq("Expected number of names", all_names.size(), 3);
1✔
213
         result.test_eq("Have expected name", all_names.count("name"), 1);
3✔
214
         result.test_eq("Have expected name", all_names.count("name2"), 1);
3✔
215
         result.test_eq("Have expected name", all_names.count("leroy jeeeeeeeenkins"), 1);
3✔
216

217
         db.remove("name2");
1✔
218

219
         all_names = db.list_names();
2✔
220

221
         result.test_eq("Expected number of names", all_names.size(), 2);
1✔
222
         result.test_eq("Have expected name", all_names.count("name"), 1);
3✔
223
         result.test_eq("Have expected name", all_names.count("leroy jeeeeeeeenkins"), 1);
3✔
224

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

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

231
         return result;
1✔
232
      }
4✔
233
   #endif
234
};
235

236
BOTAN_REGISTER_TEST("misc", "psk_db", PSK_DB_Tests);
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