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

randombit / botan / 22038388571

15 Feb 2026 03:39PM UTC coverage: 90.056% (-0.003%) from 90.059%
22038388571

Pull #5341

github

web-flow
Merge e5a537aa4 into 76dfe61e9
Pull Request #5341: Cleanup test predicates on binary strings

102343 of 113644 relevant lines covered (90.06%)

11670901.95 hits per line

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

90.0
/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
   #include <set>
14

15
namespace Botan_Tests {
16

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

66
            result.test_bin_eq(
4,860✔
67
               zfec_impl + " share " + std::to_string(share), {block, len}, {shares[share], share_size});
4,860✔
68
         };
2,430✔
69

70
         zfec.encode(input.data(), input.size(), zfec_enc_fn);
243✔
71

72
         result.test_sz_eq("Correct number of shares encoded", shares_encoded.size(), N);
243✔
73

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

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

82
            result.test_sz_lt("ZFEC dec share in range", share, K);
2,430✔
83

84
            result.test_bin_eq(
2,430✔
85
               zfec_impl + " share " + std::to_string(share), {block, len}, {&input[share * share_size], share_size});
4,860✔
86
         };
2,430✔
87

88
         zfec.decode_shares(shares, share_size, zfec_dec_fn);
243✔
89

90
         result.test_sz_eq("Correct number of shares decoded", shares_decoded.size(), K);
243✔
91

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

95
         while(shares.size() != K) {
47,020✔
96
            const size_t idx = this->rng().next_byte();
46,777✔
97
            shares.erase(idx);
46,777✔
98
         }
99

100
         zfec.decode_shares(shares, share_size, zfec_dec_fn);
243✔
101

102
         result.test_sz_eq("Correct number of shares decoded", shares_decoded.size(), K);
243✔
103

104
         return result;
243✔
105
      }
972✔
106
};
107

108
BOTAN_REGISTER_SERIALIZED_TEST("zfec", "zfec", ZFEC_KAT);
109

110
}  // namespace Botan_Tests
111

112
#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