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

randombit / botan / 5123321399

30 May 2023 04:06PM UTC coverage: 92.213% (+0.004%) from 92.209%
5123321399

Pull #3558

github

web-flow
Merge dd72f7389 into 057bcbc35
Pull Request #3558: Add braces around all if/else statements

75602 of 81986 relevant lines covered (92.21%)

11859779.3 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/zfec.h>
12

13
namespace Botan_Tests {
14

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

92
         while(shares.size() != K) {
43,778✔
93
            const size_t idx = rng().next_byte();
43,535✔
94
            shares.erase(idx);
43,535✔
95
         }
96

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

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

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

105
BOTAN_REGISTER_TEST("zfec", "zfec", ZFEC_KAT);
106

107
}  // namespace Botan_Tests
108

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

© 2025 Coveralls, Inc