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

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 hits per line

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

96.75
/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

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

17
namespace Botan_Tests {
18

19
namespace {
20

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

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

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

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

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

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

54
      std::set<std::string> kv_get_all() const override {
2✔
55
         std::set<std::string> names;
2✔
56

57
         for(const auto& kv : m_vals) {
7✔
58
            names.insert(kv.first);
5✔
59
         }
60

61
         return names;
2✔
62
      }
×
63

64
   private:
65
      std::map<std::string, std::string, std::less<>> m_vals;
66
};
67

68
}
69

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

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

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

81
         return results;
1✔
82
      }
×
83

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

137
         return result;
1✔
138
      }
3✔
139

140
   #if defined(BOTAN_HAS_SQLITE3)
141

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

149
         bool got_it = stmt->step();
6✔
150
         result.confirm("Had expected name", got_it);
12✔
151

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

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

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

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

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

169
         test_entry(result, sqldb, table_name, "CUCJjJgWSa079ubutJQwlw==", "clYJSAf9CThuL96CP+rAfA==");
4✔
170
         result.test_eq("DB read", db.get_str("name"), "value");
2✔
171

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

235
}
236

237
#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

© 2025 Coveralls, Inc