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

randombit / botan / 27737522519

18 Jun 2026 02:59AM UTC coverage: 89.397% (-2.3%) from 91.708%
27737522519

push

github

randombit
Remove unused member variable from Encrypted_PSK_Database

111425 of 124641 relevant lines covered (89.4%)

11108238.09 hits per line

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

92.59
/src/tests/test_mac.cpp
1
/*
2
* (C) 2014,2015 Jack Lloyd
3
* (C) 2016 René Korthaus
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include "tests.h"
9

10
#if defined(BOTAN_HAS_MAC)
11
   #include <botan/exceptn.h>
12
   #include <botan/mac.h>
13
   #include <botan/rng.h>
14
   #include <botan/internal/fmt.h>
15
#endif
16

17
namespace Botan_Tests {
18

19
namespace {
20

21
#if defined(BOTAN_HAS_MAC)
22

23
class Message_Auth_Tests final : public Text_Based_Test {
×
24
   public:
25
      Message_Auth_Tests() : Text_Based_Test("mac", "Key,In,Out", "IV") {}
2✔
26

27
      std::vector<std::string> possible_providers(const std::string& algo) override {
1,282✔
28
         return provider_filter(Botan::MessageAuthenticationCode::providers(algo));
1,282✔
29
      }
30

31
      Test::Result run_one_test(const std::string& algo, const VarMap& vars) override {
1,282✔
32
         const std::vector<uint8_t> key = vars.get_req_bin("Key");
1,282✔
33
         const std::vector<uint8_t> input = vars.get_req_bin("In");
1,282✔
34
         const std::vector<uint8_t> expected = vars.get_req_bin("Out");
1,282✔
35
         const std::vector<uint8_t> iv = vars.get_opt_bin("IV");
1,282✔
36
         const std::vector<uint8_t> discarded_input = rng().random_vec<std::vector<uint8_t>>(rng().next_nonzero_byte());
1,282✔
37

38
         Test::Result result(algo);
1,282✔
39

40
         const std::vector<std::string> providers = possible_providers(algo);
1,282✔
41

42
         if(providers.empty()) {
1,282✔
43
            result.note_missing("MAC " + algo);
×
44
            return result;
×
45
         }
46

47
         for(const auto& provider_ask : providers) {
2,564✔
48
            auto mac = Botan::MessageAuthenticationCode::create(algo, provider_ask);
1,282✔
49

50
            if(!mac) {
1,282✔
51
               result.test_failure(Botan::fmt("MAC {} supported by {} but not found", algo, provider_ask));
×
52
               continue;
×
53
            }
54

55
            const std::string provider(mac->provider());
1,282✔
56

57
            result.test_str_not_empty("provider", provider);
1,282✔
58
            result.test_str_eq(provider, mac->name(), algo);
1,282✔
59

60
            try {
1,282✔
61
               std::vector<uint8_t> buf(128);
1,282✔
62
               mac->update(buf.data(), buf.size());
1,282✔
63
               result.test_failure("Was able to MAC without a key being set");
×
64
            } catch(Botan::Invalid_State&) {
1,282✔
65
               result.test_success("Trying to MAC with no key set fails");
1,282✔
66
            }
1,282✔
67

68
            result.test_is_false("key not set", mac->has_keying_material());
1,282✔
69
            mac->set_key(key);
1,282✔
70
            result.test_is_true("key set", mac->has_keying_material());
1,282✔
71
            mac->start(iv);
1,282✔
72
            mac->update(input);
1,282✔
73
            result.test_bin_eq(provider + " correct mac", mac->final(), expected);
3,846✔
74

75
            mac->set_key(key);
1,282✔
76
            mac->start(iv);
1,282✔
77
            mac->update(discarded_input);
1,282✔
78
            mac->start(iv);
1,282✔
79
            mac->update(input);
1,282✔
80
            result.test_bin_eq(provider + " start resets partial message", mac->final(), expected);
3,846✔
81

82
            mac->set_key(key);
1,282✔
83
            mac->start(iv);
1,282✔
84
            mac->update(discarded_input);
1,282✔
85
            mac->set_key(key);
1,282✔
86
            if(!iv.empty()) {
1,282✔
87
               mac->start(iv);
575✔
88
            }
89
            mac->update(input);
1,282✔
90
            result.test_bin_eq(provider + " set_key resets partial message", mac->final(), expected);
3,846✔
91

92
            mac->set_key(key);
1,282✔
93
            mac->start(iv);
1,282✔
94
            mac->update(input);
1,282✔
95
            result.test_bin_eq(provider + " correct mac (try 2)", mac->final(), expected);
3,846✔
96

97
            if(iv.empty()) {
1,282✔
98
               mac->set_key(key);
707✔
99
               mac->update(input);
707✔
100
               result.test_bin_eq(provider + " correct mac (no start call)", mac->final(), expected);
2,828✔
101
            }
102

103
            if(!mac->fresh_key_required_per_message()) {
1,282✔
104
               for(size_t i = 0; i != 3; ++i) {
4,144✔
105
                  mac->start(iv);
3,108✔
106
                  mac->update(input);
3,108✔
107
                  result.test_bin_eq(provider + " correct mac (same key)", mac->final(), expected);
12,432✔
108
               }
109
            }
110

111
            // Test to make sure clear() resets what we need it to
112
            mac->set_key(key);
1,282✔
113
            mac->start(iv);
1,282✔
114
            mac->update("some discarded input");
1,282✔
115
            mac->clear();
1,282✔
116
            result.test_is_false("key not set", mac->has_keying_material());
1,282✔
117

118
            // do the same to test verify_mac()
119
            mac->set_key(key);
1,282✔
120
            mac->start(iv);
1,282✔
121
            mac->update(input);
1,282✔
122

123
            // Test that clone works and does not affect parent object
124
            auto clone = mac->new_object();
1,282✔
125
            result.test_is_true("Clone has different pointer", mac.get() != clone.get());
1,282✔
126
            result.test_str_eq("Clone has same name", mac->name(), clone->name());
1,282✔
127
            clone->set_key(key);
1,282✔
128
            clone->start(iv);
1,282✔
129
            clone->update(this->rng().random_vec(32));
1,282✔
130

131
            result.test_is_true(provider + " verify mac", mac->verify_mac(expected.data(), expected.size()));
2,564✔
132

133
            if(input.size() > 2) {
1,282✔
134
               mac->set_key(key);  // Poly1305 requires the re-key
1,236✔
135
               mac->start(iv);
1,236✔
136

137
               mac->update(input[0]);
1,236✔
138
               mac->update(&input[1], input.size() - 2);
1,236✔
139
               mac->update(input[input.size() - 1]);
1,236✔
140

141
               result.test_bin_eq(provider + " split mac", mac->final(), expected);
3,708✔
142

143
               // do the same to test verify_mac()
144
               mac->set_key(key);
1,236✔
145
               mac->start(iv);
1,236✔
146

147
               mac->update(input[0]);
1,236✔
148
               mac->update(&input[1], input.size() - 2);
1,236✔
149
               mac->update(input[input.size() - 1]);
1,236✔
150

151
               result.test_is_true(provider + " split mac", mac->verify_mac(expected.data(), expected.size()));
3,708✔
152
            }
153

154
            mac->clear();
1,282✔
155

156
            try {
1,282✔
157
               std::vector<uint8_t> buf(128);
1,282✔
158
               mac->update(buf.data(), buf.size());
1,282✔
159
               result.test_failure("Was able to MAC without a key being set");
×
160
            } catch(Botan::Invalid_State&) {
1,282✔
161
               result.test_success("Trying to MAC with no key set (after clear) fails");
1,282✔
162
            }
1,282✔
163

164
            try {
1,282✔
165
               std::vector<uint8_t> buf(mac->output_length());
1,282✔
166
               mac->final(buf.data());
1,282✔
167
               result.test_failure("Was able to MAC without a key being set");
×
168
            } catch(Botan::Invalid_State&) {
1,282✔
169
               result.test_success("Trying to MAC with no key set (after clear) fails");
1,282✔
170
            }
1,282✔
171
         }
2,564✔
172

173
         return result;
174
      }
19,682✔
175
};
176

177
BOTAN_REGISTER_SERIALIZED_SMOKE_TEST("mac", "mac_algos", Message_Auth_Tests);
178

179
#endif
180

181
}  // namespace
182

183
}  // 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

© 2026 Coveralls, Inc