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

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 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
            shares.insert(std::make_pair(i, expected_share));
2,430✔
48
         }
49

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

106
}
107

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