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

randombit / botan / 5356326050

23 Jun 2023 01:05PM UTC coverage: 91.728% (-0.008%) from 91.736%
5356326050

Pull #3595

github

web-flow
Merge a5b917599 into 92171c524
Pull Request #3595: Improve clang-tidy coverage

78163 of 85212 relevant lines covered (91.73%)

12690161.35 hits per line

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

94.38
/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/internal/fmt.h>
13
#endif
14

15
namespace Botan_Tests {
16

17
namespace {
18

19
#if defined(BOTAN_HAS_MAC)
20

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

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

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

35
         Test::Result result(algo);
1,174✔
36

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

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

44
         for(const auto& provider_ask : providers) {
1,174✔
45
            auto mac = Botan::MessageAuthenticationCode::create(algo, provider_ask);
587✔
46

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

52
            const std::string provider(mac->provider());
587✔
53

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

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

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

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

77
            if(!mac->fresh_key_required_per_message()) {
587✔
78
               for(size_t i = 0; i != 3; ++i) {
2,036✔
79
                  mac->start(iv);
1,527✔
80
                  mac->update(input);
1,527✔
81
                  result.test_eq(provider, "correct mac (same key)", mac->final(), expected);
4,581✔
82
               }
83
            }
84

85
            // Test to make sure clear() resets what we need it to
86
            mac->set_key(key);
587✔
87
            mac->start(iv);
587✔
88
            mac->update("some discarded input");
587✔
89
            mac->clear();
587✔
90
            result.test_eq("key not set", mac->has_keying_material(), false);
587✔
91

92
            // do the same to test verify_mac()
93
            mac->set_key(key);
587✔
94
            mac->start(iv);
587✔
95
            mac->update(input);
587✔
96

97
            // Test that clone works and does not affect parent object
98
            auto clone = mac->new_object();
587✔
99
            result.confirm("Clone has different pointer", mac.get() != clone.get());
1,174✔
100
            result.test_eq("Clone has same name", mac->name(), clone->name());
1,214✔
101
            clone->set_key(key);
587✔
102
            clone->start(iv);
587✔
103
            clone->update(Test::rng().random_vec(32));
587✔
104

105
            result.test_eq(provider + " verify mac", mac->verify_mac(expected.data(), expected.size()), true);
587✔
106

107
            if(input.size() > 2) {
587✔
108
               mac->set_key(key);  // Poly1305 requires the re-key
551✔
109
               mac->start(iv);
551✔
110

111
               mac->update(input[0]);
551✔
112
               mac->update(&input[1], input.size() - 2);
551✔
113
               mac->update(input[input.size() - 1]);
551✔
114

115
               result.test_eq(provider, "split mac", mac->final(), expected);
1,102✔
116

117
               // do the same to test verify_mac()
118
               mac->set_key(key);
551✔
119
               mac->start(iv);
551✔
120

121
               mac->update(input[0]);
551✔
122
               mac->update(&input[1], input.size() - 2);
551✔
123
               mac->update(input[input.size() - 1]);
551✔
124

125
               result.test_eq(provider + " split mac", mac->verify_mac(expected.data(), expected.size()), true);
1,102✔
126
            }
127

128
            mac->clear();
587✔
129

130
            try {
587✔
131
               std::vector<uint8_t> buf(128);
587✔
132
               mac->update(buf.data(), buf.size());
587✔
133
               result.test_failure("Was able to MAC without a key being set");
587✔
134
            } catch(Botan::Invalid_State&) {
587✔
135
               result.test_success("Trying to MAC with no key set (after clear) fails");
587✔
136
            }
587✔
137

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

147
         return result;
148
      }
2,408✔
149
};
150

151
BOTAN_REGISTER_SERIALIZED_SMOKE_TEST("mac", "mac_algos", Message_Auth_Tests);
152

153
#endif
154

155
}  // namespace
156

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