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

randombit / botan / 11844561993

14 Nov 2024 07:58PM UTC coverage: 91.178% (+0.1%) from 91.072%
11844561993

Pull #4435

github

web-flow
Merge 81dcb29da into e430f157a
Pull Request #4435: Test duration values ​​are now presented in seconds with six digits of precision. Tests without time measurements have been edited.

91856 of 100744 relevant lines covered (91.18%)

9311006.71 hits per line

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

77.61
/src/tests/test_pk_pad.cpp
1
/*
2
* (C) 2016 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_PK_PADDING)
10
   #include <botan/internal/eme.h>
11
   #include <botan/internal/emsa.h>
12
   #include <botan/internal/fmt.h>
13
#endif
14

15
namespace Botan_Tests {
16

17
#if defined(BOTAN_HAS_EME_PKCS1)
18
class EME_PKCS1v15_Decoding_Tests final : public Text_Based_Test {
×
19
   public:
20
      EME_PKCS1v15_Decoding_Tests() : Text_Based_Test("pk_pad_eme/pkcs1.vec", "RawCiphertext", "Plaintext") {}
2✔
21

22
      Test::Result run_one_test(const std::string& hdr, const VarMap& vars) override {
18✔
23
         const bool is_valid = (hdr == "valid");
18✔
24

25
         Test::Result result("PKCSv15 Decoding");
18✔
26

27
         auto pkcs = Botan::EME::create("PKCS1v15");
18✔
28
         if(!pkcs) {
18✔
29
            return result;
30
         }
31

32
         const std::vector<uint8_t> ciphertext = vars.get_req_bin("RawCiphertext");
18✔
33
         const std::vector<uint8_t> plaintext = vars.get_opt_bin("Plaintext");
18✔
34

35
         if(is_valid == false) {
18✔
36
            result.test_eq("Plaintext value should be empty for invalid EME inputs", plaintext.size(), 0);
22✔
37
         }
38

39
         std::vector<uint8_t> decoded(ciphertext.size());
18✔
40
         auto len = pkcs->unpad(decoded, ciphertext);
18✔
41

42
         result.test_eq("EME decoding valid/invalid matches", len.has_value().as_bool(), is_valid);
18✔
43

44
         if(len.has_value().as_bool()) {
18✔
45
            decoded.resize(len.value_or(0));
7✔
46
            result.test_eq("EME decoded plaintext correct", decoded, plaintext);
14✔
47
         } else {
48
            bool all_zeros = true;
49
            for(size_t i = 0; i != decoded.size(); ++i) {
137✔
50
               if(decoded[i] != 0) {
126✔
51
                  all_zeros = false;
×
52
               }
53
            }
54

55
            result.confirm("On invalid padding output is all zero", all_zeros);
22✔
56
         }
57

58
         // TODO: also test that encoding is accepted
59

60
         return result;
18✔
61
      }
61✔
62
};
63

64
BOTAN_REGISTER_TEST("pubkey", "eme_pkcs1v15", EME_PKCS1v15_Decoding_Tests);
65
#endif
66

67
#if defined(BOTAN_HAS_PK_PADDING)
68
class EMSA_unit_tests final : public Test {
×
69
   public:
70
      std::vector<Test::Result> run() override {
1✔
71
         Test::Result name_tests("EMSA_name_tests");
1✔
72
         name_tests.start_timer();
1✔
73

74
         std::vector<std::string> pads_need_hash = {
1✔
75
   #if BOTAN_HAS_EMSA_X931
76
            "EMSA2",
77
   #endif
78
   #if BOTAN_HAS_EMSA_PKCS1
79
            "EMSA3",
80
   #endif
81
   #if BOTAN_HAS_EMSA_PSSR
82
            "EMSA4",
83
            "PSSR_Raw",
84
   #endif
85
   #if BOTAN_HAS_ISO_9796
86
            "ISO_9796_DS2",
87
            "ISO_9796_DS3",
88
   #endif
89
         };
1✔
90

91
         std::vector<std::string> pads_no_hash = {
1✔
92
   #if BOTAN_HAS_EMSA_RAW
93
            "Raw",
94
   #endif
95
   #if BOTAN_HAS_EMSA_PKCS1
96
            "EMSA3(Raw)",
97
            "EMSA3(Raw,SHA-512)",
98
   #endif
99
         };
1✔
100

101
         for(const auto& pad : pads_need_hash) {
7✔
102
            try {
6✔
103
               const std::string hash_to_use = "SHA-256";
6✔
104
               auto emsa_1 = Botan::EMSA::create(Botan::fmt("{}({})", pad, hash_to_use));
6✔
105
               auto emsa_2 = Botan::EMSA::create(emsa_1->name());
6✔
106
               name_tests.test_eq("EMSA_name_test for " + pad, emsa_1->name(), emsa_2->name());
12✔
107
            } catch(Botan::Lookup_Error&) {
12✔
108
               name_tests.test_note("Skipping test due to missing hash");
×
109
            } catch(const std::exception& e) {
×
110
               name_tests.test_failure("EMSA_name_test for " + pad + ": " + e.what());
×
111
            }
×
112
         }
113

114
         for(const auto& pad : pads_need_hash) {
7✔
115
            std::string algo_name = pad + "(YYZ)";
6✔
116
            try {
6✔
117
               auto emsa = Botan::EMSA::create_or_throw(algo_name);
6✔
118
               name_tests.test_failure("EMSA_name_test for " + pad + ": " + "Could create EMSA with fantasy hash YYZ");
×
119
            } catch(Botan::Lookup_Error&) {
6✔
120
               name_tests.test_note("Skipping test due to missing hash");
6✔
121
            } catch(const std::exception& e) {
6✔
122
               name_tests.test_eq(
×
123
                  "EMSA_name_test for " + pad, e.what(), "Could not find any algorithm named \"" + algo_name + "\"");
×
124
            }
×
125
         }
6✔
126

127
         for(const auto& pad : pads_no_hash) {
4✔
128
            try {
3✔
129
               auto emsa_1 = Botan::EMSA::create(pad);
3✔
130
               auto emsa_2 = Botan::EMSA::create(emsa_1->name());
3✔
131
               name_tests.test_eq("EMSA_name_test for " + pad, emsa_1->name(), emsa_2->name());
6✔
132
            } catch(Botan::Lookup_Error&) {
6✔
133
               name_tests.test_note("Skipping test due to missing hash");
×
134
            } catch(const std::exception& e) {
×
135
               name_tests.test_failure("EMSA_name_test for " + pad + ": " + e.what());
×
136
            }
×
137
         }
138

139
         name_tests.end_timer();
1✔
140
         return {name_tests};
3✔
141
      }
2✔
142
};
143

144
BOTAN_REGISTER_TEST("pubkey", "pk_pad_emsa_unit", EMSA_unit_tests);
145

146
#endif
147

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

© 2025 Coveralls, Inc