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

randombit / botan / 20279912191

16 Dec 2025 07:18PM UTC coverage: 90.518% (+0.2%) from 90.36%
20279912191

Pull #5167

github

web-flow
Merge b45d77a93 into 3d96b675e
Pull Request #5167: Changes to reduce unnecessary inclusions

101149 of 111745 relevant lines covered (90.52%)

12673176.65 hits per line

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

89.8
/src/tests/test_zfec.cpp
1
/*
2
* (C) 2021 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_ZFEC)
10

11
   #include <botan/rng.h>
12
   #include <botan/zfec.h>
13

14
namespace Botan_Tests {
15

16
class ZFEC_KAT final : public Text_Based_Test {
×
17
   public:
18
      ZFEC_KAT() : Text_Based_Test("zfec.vec", "K,N,Data,Code") {}
2✔
19

20
      Test::Result run_one_test(const std::string& /*header*/, const VarMap& vars) override {
243✔
21
         const size_t K = vars.get_req_sz("K");
243✔
22
         const size_t N = vars.get_req_sz("N");
243✔
23

24
         const std::vector<uint8_t> input = vars.get_req_bin("Data");
243✔
25
         const std::vector<uint8_t> expected = vars.get_req_bin("Code");
243✔
26

27
         if(input.size() % K != 0) {
243✔
28
            throw Test_Error("ZFEC input is not a multiple of K bytes");
×
29
         }
30

31
         Test::Result result("ZFEC encoding/decoding");
243✔
32

33
         const size_t share_size = input.size() / K;
243✔
34

35
         if(expected.size() != share_size * (N - K)) {
243✔
36
            throw Test_Error("ZFEC output does not correspond with K/N");
×
37
         }
38

39
         std::map<size_t, const uint8_t*> shares;
243✔
40

41
         for(size_t i = 0; i != N; ++i) {
2,673✔
42
            const uint8_t* expected_share = nullptr;
2,430✔
43
            if(i < K) {
2,430✔
44
               expected_share = &input[share_size * i];
1,215✔
45
            } else {
46
               expected_share = &expected[share_size * (i - K)];
1,215✔
47
            }
48

49
            shares.insert(std::make_pair(i, expected_share));
2,430✔
50
         }
51

52
         const Botan::ZFEC zfec(K, N);
243✔
53

54
         const std::string zfec_impl = zfec.provider();
243✔
55

56
         std::set<size_t> shares_encoded;
243✔
57

58
         auto zfec_enc_fn = [&](size_t share, const uint8_t block[], size_t len) {
2,673✔
59
            if(shares_encoded.insert(share).second == false) {
2,430✔
60
               result.test_failure("Encoding returned the same share twice");
×
61
            }
62

63
            result.test_lt("ZFEC enc share in range", share, N);
2,430✔
64

65
            result.test_eq(zfec_impl.c_str(), "share " + std::to_string(share), block, len, shares[share], share_size);
7,290✔
66
         };
2,430✔
67

68
         zfec.encode(input.data(), input.size(), zfec_enc_fn);
243✔
69

70
         result.test_eq("Correct number of shares encoded", shares_encoded.size(), N);
243✔
71

72
         // First test full decoding:
73
         std::set<size_t> shares_decoded;
243✔
74

75
         auto zfec_dec_fn = [&](size_t share, const uint8_t block[], size_t len) {
2,673✔
76
            if(shares_decoded.insert(share).second == false) {
2,430✔
77
               result.test_failure("Decoding returned the same share twice");
×
78
            }
79

80
            result.test_lt("ZFEC dec share in range", share, K);
2,430✔
81

82
            result.test_eq(
4,860✔
83
               zfec_impl.c_str(), "share " + std::to_string(share), block, len, &input[share * share_size], share_size);
7,290✔
84
         };
2,430✔
85

86
         zfec.decode_shares(shares, share_size, zfec_dec_fn);
243✔
87

88
         result.test_eq("Correct number of shares decoded", shares_decoded.size(), K);
243✔
89

90
         // Now remove N-K shares:
91
         shares_decoded.clear();
243✔
92

93
         while(shares.size() != K) {
43,984✔
94
            const size_t idx = this->rng().next_byte();
43,741✔
95
            shares.erase(idx);
43,741✔
96
         }
97

98
         zfec.decode_shares(shares, share_size, zfec_dec_fn);
243✔
99

100
         result.test_eq("Correct number of shares decoded", shares_decoded.size(), K);
243✔
101

102
         return result;
243✔
103
      }
972✔
104
};
105

106
BOTAN_REGISTER_SERIALIZED_TEST("zfec", "zfec", ZFEC_KAT);
107

108
}  // namespace Botan_Tests
109

110
#endif
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