• 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

85.14
/src/tests/test_tss.cpp
1
/*
2
* (C) 2009 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_THRESHOLD_SECRET_SHARING)
10
   #include "test_rng.h"
11
   #include <botan/exceptn.h>
12
   #include <botan/tss.h>
13
#endif
14

15
namespace Botan_Tests {
16

17
namespace {
18

19
#if defined(BOTAN_HAS_THRESHOLD_SECRET_SHARING)
20

21
class TSS_Recovery_Tests final : public Text_Based_Test {
×
22
   public:
23
      TSS_Recovery_Tests() : Text_Based_Test("tss/recovery.vec", "N,M,Shares,Recovered") {}
2✔
24

25
      Test::Result run_one_test(const std::string& header, const VarMap& vars) override {
4✔
26
         Test::Result result("TSS");
4✔
27

28
         const std::vector<uint8_t> input = vars.get_req_bin("Recovered");
4✔
29
         const size_t N = vars.get_req_sz("N");
4✔
30
         const size_t M = vars.get_req_sz("M");
4✔
31
         const std::vector<std::vector<uint8_t>> expected_shares = vars.get_req_bin_list("Shares");
4✔
32

33
         try {
4✔
34
            std::vector<Botan::RTSS_Share> shares;
4✔
35
            shares.reserve(expected_shares.size());
4✔
36

37
            for(auto&& v : expected_shares) {
17✔
38
               shares.push_back(Botan::RTSS_Share(v.data(), v.size()));
26✔
39
            }
40

41
            auto reconstructed_secret_all = Botan::RTSS_Share::reconstruct(shares);
4✔
42
            result.test_bin_eq("Reconstructed secret correctly from all shares", reconstructed_secret_all, input);
3✔
43

44
            if(header == "Invalid") {
3✔
45
               result.test_failure("Invalid shares should not result in recovery");
×
46
            }
47

48
            if(N != M) {
3✔
49
               while(shares.size() > M) {
6✔
50
                  const size_t to_remove = this->rng().next_byte() % shares.size();
4✔
51
                  shares.erase(shares.begin() + to_remove);
4✔
52
                  try {
4✔
53
                     auto reconstructed_secret = Botan::RTSS_Share::reconstruct(shares);
4✔
54
                     result.test_bin_eq(
4✔
55
                        "Reconstructed secret correctly from reduced shares", reconstructed_secret, input);
56
                  } catch(Botan::Decoding_Error&) {
4✔
57
                     result.test_failure("Reconstruction failed with share count " + std::to_string(shares.size()));
×
58
                  }
×
59
               }
60
            }
61

62
         } catch(std::exception& e) {
5✔
63
            if(header == "Valid") {
1✔
64
               result.test_failure("Valid TSS failed to recover", e.what());
×
65
            } else {
66
               result.test_success("Invalid TSS rejected as expected");
1✔
67
            }
68
         }
1✔
69

70
         return result;
8✔
71
      }
8✔
72
};
73

74
BOTAN_REGISTER_TEST("utils", "tss_recovery", TSS_Recovery_Tests);
75

76
class TSS_Generation_Tests final : public Text_Based_Test {
×
77
   public:
78
      TSS_Generation_Tests() : Text_Based_Test("tss/generation.vec", "Input,RNG,Hash,Id,N,M,Shares") {}
2✔
79

80
      static size_t tss_hash_len(const std::string& hash) {
6✔
81
         if(hash == "None") {
6✔
82
            return 0;
83
         } else if(hash == "SHA-1") {
2✔
84
            return 20;
85
         } else if(hash == "SHA-256") {
1✔
86
            return 32;
87
         } else {
88
            throw Test_Error("Unknown TSS hash algorithm " + hash);
×
89
         }
90
      }
91

92
      Test::Result run_one_test(const std::string& /*header*/, const VarMap& vars) override {
6✔
93
         Test::Result result("TSS");
6✔
94

95
         const std::vector<uint8_t> input = vars.get_req_bin("Input");
6✔
96
         const std::vector<uint8_t> id = vars.get_req_bin("Id");
6✔
97
         const std::vector<uint8_t> rng_data = vars.get_req_bin("RNG");
6✔
98
         const uint8_t N = vars.get_req_u8("N");
6✔
99
         const uint8_t M = vars.get_req_u8("M");
6✔
100
         const std::string hash = vars.get_req_str("Hash");
6✔
101
         const std::vector<std::vector<uint8_t>> expected_shares = vars.get_req_bin_list("Shares");
6✔
102

103
         if(expected_shares.size() != N) {
6✔
104
            throw Test_Error("Invalid test data for TSS share count != N");
×
105
         }
106

107
         if(rng_data.size() != (input.size() + tss_hash_len(hash)) * (M - 1)) {
6✔
108
            throw Test_Error("Invalid test data for TSS share bad RNG input size");
×
109
         }
110

111
         Fixed_Output_RNG fixed_rng(rng_data);
6✔
112

113
         std::vector<Botan::RTSS_Share> shares =
6✔
114
            Botan::RTSS_Share::split(M, N, input.data(), static_cast<uint16_t>(input.size()), id, hash, fixed_rng);
6✔
115

116
         result.test_sz_eq("Expected number of shares", shares.size(), N);
6✔
117

118
         for(size_t i = 0; i != N; ++i) {
40✔
119
            result.test_bin_eq("Expected share", shares[i].data(), expected_shares[i]);
34✔
120
         }
121

122
         auto reconstructed_secret_all = Botan::RTSS_Share::reconstruct(shares);
6✔
123
         result.test_bin_eq("Reconstructed secret correctly from all shares", reconstructed_secret_all, input);
6✔
124

125
         if(N != M) {
6✔
126
            while(shares.size() > M) {
21✔
127
               const size_t to_remove = this->rng().next_byte() % shares.size();
17✔
128
               shares.erase(shares.begin() + to_remove);
17✔
129

130
               try {
17✔
131
                  auto reconstructed_secret = Botan::RTSS_Share::reconstruct(shares);
17✔
132
                  result.test_bin_eq("Reconstructed secret correctly from reduced shares", reconstructed_secret, input);
17✔
133
               } catch(Botan::Decoding_Error&) {
17✔
134
                  result.test_failure("Reconstruction failed with share count " + std::to_string(shares.size()));
×
135
               }
×
136
            }
137
         }
138

139
         return result;
6✔
140
      }
24✔
141
};
142

143
BOTAN_REGISTER_TEST("utils", "tss_generation", TSS_Generation_Tests);
144

145
#endif  // BOTAN_HAS_THRESHOLD_SECRET_SHARING
146

147
}  // namespace
148

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