• 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

91.3
/src/tests/test_passhash.cpp
1
/*
2
* (C) 2014,2015 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_BCRYPT)
10
   #include <botan/bcrypt.h>
11
#endif
12

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

17
#if defined(BOTAN_HAS_ARGON2_FMT)
18
   #include "test_rng.h"
19
   #include <botan/argon2fmt.h>
20
#endif
21

22
namespace Botan_Tests {
23

24
namespace {
25

26
#if defined(BOTAN_HAS_BCRYPT)
27
class Bcrypt_Tests final : public Text_Based_Test {
×
28
   public:
29
      Bcrypt_Tests() : Text_Based_Test("passhash/bcrypt.vec", "Password,Passhash") {}
3✔
30

31
      Test::Result run_one_test(const std::string& /*header*/, const VarMap& vars) override {
93✔
32
         // Encoded as binary so we can test binary inputs
33
         const std::vector<uint8_t> password_vec = vars.get_req_bin("Password");
93✔
34
         const std::string password(reinterpret_cast<const char*>(password_vec.data()), password_vec.size());
186✔
35

36
         const std::string passhash = vars.get_req_str("Passhash");
93✔
37

38
         Test::Result result("bcrypt");
93✔
39
         result.test_eq("correct hash accepted", Botan::check_bcrypt(password, passhash), true);
93✔
40

41
         // self-test low levels for each test password
42
         for(uint16_t level = 4; level <= 6; ++level) {
372✔
43
            const std::string gen_hash = Botan::generate_bcrypt(password, Test::rng(), level);
279✔
44
            result.test_eq("generated hash accepted", Botan::check_bcrypt(password, gen_hash), true);
558✔
45
         }
279✔
46

47
         return result;
93✔
48
      }
250✔
49

50
      std::vector<Test::Result> run_final_tests() override {
1✔
51
         Test::Result result("bcrypt");
1✔
52

53
         uint64_t start = Test::timestamp();
1✔
54

55
         const std::string password = "ag00d1_2BE5ur3";
1✔
56

57
         const uint16_t max_level = (Test::run_long_tests() ? 15 : 10);
1✔
58

59
         for(uint16_t level = 4; level <= max_level; ++level) {
13✔
60
            const std::string gen_hash = Botan::generate_bcrypt(password, Test::rng(), level);
12✔
61
            result.test_eq("generated hash accepted", Botan::check_bcrypt(password, gen_hash), true);
24✔
62
         }
12✔
63

64
         result.test_throws("Invalid bcrypt version rejected", "Unknown bcrypt version 'q'", []() {
3✔
65
            Botan::generate_bcrypt("pass", Test::rng(), 4, 'q');
1✔
66
         });
×
67

68
         result.set_ns_consumed(Test::timestamp() - start);
1✔
69

70
         return {result};
3✔
71
      }
1✔
72
};
73

74
BOTAN_REGISTER_TEST("pbkdf", "bcrypt", Bcrypt_Tests);
75

76
#endif
77

78
#if defined(BOTAN_HAS_ARGON2_FMT)
79
class Argon2_Tests final : public Text_Based_Test {
×
80
   public:
81
      Argon2_Tests() : Text_Based_Test("passhash/argon2.vec", "Password,Passhash", "Mode,M,T,P,Salt,OutLen") {}
4✔
82

83
      Test::Result run_one_test(const std::string& header, const VarMap& vars) override {
6✔
84
         const std::string password = vars.get_req_str("Password");
6✔
85
         const std::string passhash = vars.get_req_str("Passhash");
6✔
86

87
         Test::Result result("Argon2 password hash");
6✔
88

89
         if(header == "Verify") {
6✔
90
            const bool accepted = Botan::argon2_check_pwhash(password.data(), password.size(), passhash);
3✔
91
            result.test_eq("correct hash accepted", accepted, true);
6✔
92
         } else if(header == "Generate") {
3✔
93
            const std::vector<uint8_t> salt = vars.get_req_bin("Salt");
3✔
94
            const uint8_t y = vars.get_req_u8("Mode");
3✔
95
            const size_t M = vars.get_req_sz("M");
3✔
96
            const size_t t = vars.get_req_sz("T");
3✔
97
            const size_t p = vars.get_req_sz("P");
3✔
98
            const size_t out_len = vars.get_req_sz("OutLen");
3✔
99

100
            Fixed_Output_RNG rng(salt);
3✔
101

102
            const std::string generated =
3✔
103
               Botan::argon2_generate_pwhash(password.data(), password.size(), rng, p, M, t, y, salt.size(), out_len);
3✔
104

105
            result.test_eq("expected hash generated", generated, passhash);
3✔
106
            const bool accepted = Botan::argon2_check_pwhash(password.data(), password.size(), generated);
3✔
107
            result.test_eq("generated hash accepted", accepted, true);
6✔
108
         } else {
6✔
109
            throw Test_Error("Unexpected header in Argon2 password hash test file");
×
110
         }
111

112
         return result;
6✔
113
      }
6✔
114
};
115

116
BOTAN_REGISTER_TEST("pbkdf", "argon2_pass", Argon2_Tests);
117

118
#endif
119

120
#if defined(BOTAN_HAS_PASSHASH9)
121
class Passhash9_Tests final : public Text_Based_Test {
×
122
   public:
123
      Passhash9_Tests() : Text_Based_Test("passhash/passhash9.vec", "Password,Passhash,PRF") {}
3✔
124

125
      Test::Result run_one_test(const std::string& /*header*/, const VarMap& vars) override {
1✔
126
         // Encoded as binary so we can test binary inputs
127
         const std::vector<uint8_t> password_vec = vars.get_req_bin("Password");
1✔
128
         const std::string password(reinterpret_cast<const char*>(password_vec.data()), password_vec.size());
2✔
129

130
         const std::string passhash = vars.get_req_str("Passhash");
1✔
131
         const std::size_t prf = vars.get_req_sz("PRF");
1✔
132

133
         Test::Result result("passhash9");
1✔
134

135
         if(Botan::is_passhash9_alg_supported(uint8_t(prf))) {
1✔
136
            result.test_eq("correct hash accepted", Botan::check_passhash9(password, passhash), true);
2✔
137
         }
138

139
         for(uint8_t alg_id = 0; alg_id <= 4; ++alg_id) {
6✔
140
            if(Botan::is_passhash9_alg_supported(alg_id)) {
5✔
141
               const std::string gen_hash = Botan::generate_passhash9(password, Test::rng(), 2, alg_id);
5✔
142

143
               if(!result.test_eq("generated hash accepted", Botan::check_passhash9(password, gen_hash), true)) {
10✔
144
                  result.test_note("hash was " + gen_hash);
×
145
               }
146
            }
5✔
147
         }
148

149
         const uint16_t max_level = (Test::run_long_tests() ? 14 : 8);
1✔
150

151
         for(uint16_t level = 1; level <= max_level; ++level) {
15✔
152
            const uint8_t alg_id = 1;  // default used by generate_passhash9()
14✔
153
            if(Botan::is_passhash9_alg_supported(alg_id)) {
14✔
154
               const std::string gen_hash = Botan::generate_passhash9(password, Test::rng(), level, alg_id);
14✔
155
               if(!result.test_eq("generated hash accepted", Botan::check_passhash9(password, gen_hash), true)) {
28✔
156
                  result.test_note("hash was " + gen_hash);
×
157
               }
158
            }
14✔
159
         }
160

161
         return result;
1✔
162
      }
2✔
163

164
      std::vector<Test::Result> run_final_tests() override {
1✔
165
         Test::Result result("passhash9");
1✔
166

167
         result.confirm("Unknown algorithm is unknown", Botan::is_passhash9_alg_supported(255) == false);
2✔
168

169
         result.test_throws("Throws if algorithm not supported", "Passhash9: Algorithm id 255 is not defined", []() {
3✔
170
            Botan::generate_passhash9("pass", Test::rng(), 3, 255);
1✔
171
         });
×
172

173
         result.test_throws(
3✔
174
            "Throws if iterations is too high", "Requested passhash9 work factor 513 is too large", []() {
1✔
175
               Botan::check_passhash9("floof", "$9$AgIB3c5J3kvAuML84sZ5hWT9WzJtiYRPLCEARaujS7I6IKbNCwp0");
1✔
176
            });
177
         return {result};
3✔
178
      }
1✔
179
};
180

181
BOTAN_REGISTER_TEST("pbkdf", "passhash9", Passhash9_Tests);
182

183
#endif
184

185
}  // namespace
186

187
}  // namespace Botan_Tests
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