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

randombit / botan / 15166935530

21 May 2025 01:49PM UTC coverage: 91.087% (-0.01%) from 91.099%
15166935530

push

github

web-flow
Merge pull request #4873 from reneme/feature/span_in_mode_padding

Use std::span for block cipher padding schemes

96462 of 105901 relevant lines covered (91.09%)

12870063.96 hits per line

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

85.37
/src/tests/test_pad.cpp
1
/*
2
* (C) 2016 René Korthaus, Rohde & Schwarz Cybersecurity
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_CIPHER_MODE_PADDING)
10
   #include <botan/internal/mode_pad.h>
11
#endif
12

13
namespace Botan_Tests {
14

15
#if defined(BOTAN_HAS_CIPHER_MODE_PADDING)
16

17
class Cipher_Mode_Padding_Tests final : public Text_Based_Test {
×
18
   public:
19
      Cipher_Mode_Padding_Tests() : Text_Based_Test("pad.vec", "In,Blocksize", "Out") {}
2✔
20

21
      Test::Result run_one_test(const std::string& header, const VarMap& vars) override {
66✔
22
         const std::vector<uint8_t> input = vars.get_req_bin("In");
66✔
23
         const std::vector<uint8_t> expected = vars.get_opt_bin("Out");
66✔
24
         const size_t block_size = vars.get_req_sz("Blocksize");
66✔
25

26
         std::string algo = header;
66✔
27

28
         auto underscore = algo.find('_');
66✔
29
         if(underscore != std::string::npos) {
66✔
30
            if(algo.substr(underscore + 1, std::string::npos) != "Invalid") {
28✔
31
               throw Test_Error("Unexpected padding header " + header);
×
32
            }
33
            algo = algo.substr(0, underscore);
28✔
34
         }
35

36
         Test::Result result(algo);
132✔
37

38
         auto pad = Botan::BlockCipherModePaddingMethod::create(algo);
66✔
39

40
         if(!pad) {
66✔
41
            result.test_failure("Invalid padding method: " + algo);
×
42
            return result;
×
43
         }
44

45
         if(expected.empty()) {
66✔
46
            // This is an unpad an invalid input and ensure we reject
47
            if(pad->unpad(std::span{input}.last(block_size)) != block_size) {
28✔
48
               result.test_failure("Did not reject invalid padding", Botan::hex_encode(input));
×
49
            } else {
50
               result.test_success("Rejected invalid padding");
56✔
51
            }
52
         } else {
53
            // This is a pad plaintext and unpad valid padding round trip test
54
            Botan::secure_vector<uint8_t> buf(input.begin(), input.end());
38✔
55
            const size_t outlen = pad->output_length(buf.size(), block_size);
38✔
56
            if(!result.test_eq("output length", outlen, expected.size())) {
38✔
57
               return result;
×
58
            }
59
            buf.resize(outlen);
38✔
60
            pad->add_padding(buf, input.size() % block_size, block_size);
38✔
61
            result.test_eq("pad", buf, expected);
76✔
62

63
            buf.assign(expected.begin(), expected.end());
38✔
64

65
            const auto pad_bytes = [&] {
114✔
66
               if(algo == "NoPadding") {
38✔
67
                  const size_t maybe_partial = (buf.size() < block_size) ? buf.size() : block_size;
5✔
68
                  const auto no_padding = maybe_partial - pad->unpad(std::span{buf}.last(maybe_partial));
5✔
69
                  result.test_eq("no padding", no_padding, 0);
5✔
70
                  return no_padding;
5✔
71
               } else {
72
                  return block_size - pad->unpad(std::span{buf}.last(block_size));
33✔
73
               }
74
            }();
38✔
75

76
            buf.resize(buf.size() - pad_bytes);  // remove padding
38✔
77
            result.test_eq("unpad", buf, input);
76✔
78
         }
38✔
79

80
         return result;
81
      }
198✔
82
};
83

84
BOTAN_REGISTER_TEST("modes", "bc_pad", Cipher_Mode_Padding_Tests);
85

86
#endif
87

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