• 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

94.62
/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/mac.h>
12
   #include <botan/rng.h>
13
   #include <botan/internal/fmt.h>
14
#endif
15

16
namespace Botan_Tests {
17

18
namespace {
19

20
#if defined(BOTAN_HAS_MAC)
21

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

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

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

36
         Test::Result result(algo);
1,202✔
37

38
         const std::vector<std::string> providers = possible_providers(algo);
601✔
39

40
         if(providers.empty()) {
601✔
41
            result.note_missing("MAC " + algo);
×
42
            return result;
×
43
         }
44

45
         for(const auto& provider_ask : providers) {
1,202✔
46
            auto mac = Botan::MessageAuthenticationCode::create(algo, provider_ask);
601✔
47

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

53
            const std::string provider(mac->provider());
601✔
54

55
            result.test_is_nonempty("provider", provider);
601✔
56
            result.test_eq(provider, mac->name(), algo);
601✔
57

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

66
            result.test_eq("key not set", mac->has_keying_material(), false);
601✔
67
            mac->set_key(key);
601✔
68
            result.test_eq("key set", mac->has_keying_material(), true);
601✔
69
            mac->start(iv);
601✔
70
            mac->update(input);
601✔
71
            result.test_eq(provider, "correct mac", mac->final(), expected);
1,202✔
72

73
            mac->set_key(key);
601✔
74
            mac->start(iv);
601✔
75
            mac->update(input);
601✔
76
            result.test_eq(provider, "correct mac (try 2)", mac->final(), expected);
1,202✔
77

78
            if(iv.empty()) {
601✔
79
               mac->set_key(key);
538✔
80
               mac->update(input);
538✔
81
               result.test_eq(provider, "correct mac (no start call)", mac->final(), expected);
1,614✔
82
            }
83

84
            if(!mac->fresh_key_required_per_message()) {
601✔
85
               for(size_t i = 0; i != 3; ++i) {
2,060✔
86
                  mac->start(iv);
1,545✔
87
                  mac->update(input);
1,545✔
88
                  result.test_eq(provider, "correct mac (same key)", mac->final(), expected);
4,635✔
89
               }
90
            }
91

92
            // Test to make sure clear() resets what we need it to
93
            mac->set_key(key);
601✔
94
            mac->start(iv);
601✔
95
            mac->update("some discarded input");
601✔
96
            mac->clear();
601✔
97
            result.test_eq("key not set", mac->has_keying_material(), false);
601✔
98

99
            // do the same to test verify_mac()
100
            mac->set_key(key);
601✔
101
            mac->start(iv);
601✔
102
            mac->update(input);
601✔
103

104
            // Test that clone works and does not affect parent object
105
            auto clone = mac->new_object();
601✔
106
            result.confirm("Clone has different pointer", mac.get() != clone.get());
1,202✔
107
            result.test_eq("Clone has same name", mac->name(), clone->name());
1,202✔
108
            clone->set_key(key);
601✔
109
            clone->start(iv);
601✔
110
            clone->update(this->rng().random_vec(32));
601✔
111

112
            result.test_eq(provider + " verify mac", mac->verify_mac(expected.data(), expected.size()), true);
601✔
113

114
            if(input.size() > 2) {
601✔
115
               mac->set_key(key);  // Poly1305 requires the re-key
565✔
116
               mac->start(iv);
565✔
117

118
               mac->update(input[0]);
565✔
119
               mac->update(&input[1], input.size() - 2);
565✔
120
               mac->update(input[input.size() - 1]);
565✔
121

122
               result.test_eq(provider, "split mac", mac->final(), expected);
1,130✔
123

124
               // do the same to test verify_mac()
125
               mac->set_key(key);
565✔
126
               mac->start(iv);
565✔
127

128
               mac->update(input[0]);
565✔
129
               mac->update(&input[1], input.size() - 2);
565✔
130
               mac->update(input[input.size() - 1]);
565✔
131

132
               result.test_eq(provider + " split mac", mac->verify_mac(expected.data(), expected.size()), true);
1,130✔
133
            }
134

135
            mac->clear();
601✔
136

137
            try {
601✔
138
               std::vector<uint8_t> buf(128);
601✔
139
               mac->update(buf.data(), buf.size());
601✔
140
               result.test_failure("Was able to MAC without a key being set");
601✔
141
            } catch(Botan::Invalid_State&) {
601✔
142
               result.test_success("Trying to MAC with no key set (after clear) fails");
601✔
143
            }
601✔
144

145
            try {
601✔
146
               std::vector<uint8_t> buf(mac->output_length());
601✔
147
               mac->final(buf.data());
601✔
148
               result.test_failure("Was able to MAC without a key being set");
601✔
149
            } catch(Botan::Invalid_State&) {
601✔
150
               result.test_success("Trying to MAC with no key set (after clear) fails");
601✔
151
            }
601✔
152
         }
1,202✔
153

154
         return result;
155
      }
2,467✔
156
};
157

158
BOTAN_REGISTER_SERIALIZED_SMOKE_TEST("mac", "mac_algos", Message_Auth_Tests);
159

160
#endif
161

162
}  // namespace
163

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