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

randombit / botan / 5123321399

30 May 2023 04:06PM UTC coverage: 92.213% (+0.004%) from 92.209%
5123321399

Pull #3558

github

web-flow
Merge dd72f7389 into 057bcbc35
Pull Request #3558: Add braces around all if/else statements

75602 of 81986 relevant lines covered (92.21%)

11859779.3 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
         }
45
         return i->second;
6✔
46
      }
47

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

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

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

62
         return names;
2✔
63
      }
×
64

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

69
}  // namespace
70

71
class PSK_DB_Tests final : public Test {
×
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_eq("DB read", db.get_str("name"), "value");
2✔
95

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

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

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

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

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

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

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

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

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

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

132
         result.test_throws(
3✔
133
            "exception if get called on non-existent PSK", "Named PSK not located", [&]() { db.get("name2"); });
1✔
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
                      std::shared_ptr<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 + "'");
12✔
149

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

153
         if(got_it) {
6✔
154
            result.test_eq("Had expected value", stmt->get_str(0), expected_value);
18✔
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 = Test::rng().random_vec(32);
1✔
163

164
         const std::string table_name = "bobby";
1✔
165
         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==");
4✔
171
         result.test_eq("DB read", db.get_str("name"), "value");
2✔
172

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

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

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

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

193
         // Test longer names
194
         db.set_str("leroy jeeeeeeeenkins", "chicken");
1✔
195
         test_entry(
4✔
196
            result, sqldb, table_name, "KyYo272vlSjClM2F0OZBMlRYjr33ZXv2jN1oY8OfCEs=", "tCl1qShSTsXi9tA5Kpo9vg==");
197
         result.test_eq("DB read", db.get_str("leroy jeeeeeeeenkins"), "chicken");
2✔
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_eq("DB read", db2.get_str("name"), "price&value");
2✔
206
         result.test_eq("DB2 size", db2.list_names().size(), 1);
2✔
207

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

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

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

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

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

223
         result.test_throws(
3✔
224
            "exception if get called on non-existent PSK", "Named PSK not located", [&]() { db.get("name2"); });
1✔
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 Botan_Tests
237

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