Coverage Report

Created: 2026-02-14 07:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/kea-fuzzer/fuzz_encode.cc
Line
Count
Source
1
// Copyright (C) 2025 Ada Logics Ltd.
2
//
3
// This Source Code Form is subject to the terms of the Mozilla Public
4
// License, v. 2.0. If a copy of the MPL was not distributed with this
5
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
////////////////////////////////////////////////////////////////////////////////
7
#include <config.h>
8
#include <fuzzer/FuzzedDataProvider.h>
9
10
#include <exceptions/exceptions.h>
11
#include <util/encode/encode.h>
12
13
#include <string>
14
#include <vector>
15
#include <cstddef>
16
17
using namespace isc::util::encode;
18
19
1.33k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
20
1.33k
    if (size < 2) {
21
1
        return 0;
22
1
    }
23
24
1.33k
    FuzzedDataProvider fdp(data, size);
25
    
26
    // Choose which encoding/decoding path to test
27
1.33k
    uint8_t path = fdp.ConsumeIntegralInRange<uint8_t>(0, 11);
28
    
29
1.33k
    std::vector<uint8_t> binary_data;
30
1.33k
    std::string encoded_str;
31
1.33k
    std::vector<uint8_t> decoded_output;
32
    
33
1.33k
    switch (path) {
34
122
        case 0: {
35
            // Test Base64 encoding from binary data
36
122
            try {
37
122
                size_t bin_size = fdp.ConsumeIntegralInRange<size_t>(0, size);
38
122
                binary_data = fdp.ConsumeBytes<uint8_t>(bin_size);
39
122
                encoded_str = encodeBase64(binary_data);
40
                // Verify round-trip
41
122
                decodeBase64(encoded_str, decoded_output);
42
122
            } catch (const isc::Exception&) {
43
                // Expected for invalid input
44
0
            }
45
122
            break;
46
122
        }
47
        
48
101
        case 1: {
49
            // Test Base64 decoding from string
50
101
            try {
51
101
                encoded_str = fdp.ConsumeRemainingBytesAsString();
52
101
                decodeBase64(encoded_str, decoded_output);
53
101
            } catch (const isc::Exception&) {
54
                // Expected for invalid Base64 strings
55
99
            }
56
101
            break;
57
101
        }
58
        
59
93
        case 2: {
60
            // Test Base32Hex encoding from binary data
61
93
            try {
62
93
                size_t bin_size = fdp.ConsumeIntegralInRange<size_t>(0, size);
63
93
                binary_data = fdp.ConsumeBytes<uint8_t>(bin_size);
64
93
                encoded_str = encodeBase32Hex(binary_data);
65
                // Verify round-trip
66
93
                decodeBase32Hex(encoded_str, decoded_output);
67
93
            } catch (const isc::Exception&) {
68
                // Expected for invalid input
69
0
            }
70
93
            break;
71
93
        }
72
        
73
68
        case 3: {
74
            // Test Base32Hex decoding from string
75
68
            try {
76
68
                encoded_str = fdp.ConsumeRemainingBytesAsString();
77
68
                decodeBase32Hex(encoded_str, decoded_output);
78
68
            } catch (const isc::Exception&) {
79
                // Expected for invalid Base32Hex strings
80
67
            }
81
68
            break;
82
68
        }
83
        
84
107
        case 4: {
85
            // Test Base16 (hex) encoding from binary data
86
107
            try {
87
107
                size_t bin_size = fdp.ConsumeIntegralInRange<size_t>(0, size);
88
107
                binary_data = fdp.ConsumeBytes<uint8_t>(bin_size);
89
107
                encoded_str = encodeHex(binary_data);
90
                // Verify round-trip
91
107
                decodeHex(encoded_str, decoded_output);
92
107
            } catch (const isc::Exception&) {
93
                // Expected for invalid input
94
0
            }
95
107
            break;
96
107
        }
97
        
98
66
        case 5: {
99
            // Test Base16 (hex) decoding from string
100
66
            try {
101
66
                encoded_str = fdp.ConsumeRemainingBytesAsString();
102
66
                decodeHex(encoded_str, decoded_output);
103
66
            } catch (const isc::Exception&) {
104
                // Expected for invalid hex strings
105
65
            }
106
66
            break;
107
66
        }
108
        
109
139
        case 6: {
110
            // Test Base64 with various padding scenarios
111
139
            try {
112
139
                std::string test_str = fdp.ConsumeRandomLengthString();
113
                // Add various padding permutations
114
139
                test_str += fdp.ConsumeBool() ? "=" : "";
115
139
                test_str += fdp.ConsumeBool() ? "=" : "";
116
139
                decodeBase64(test_str, decoded_output);
117
139
            } catch (const isc::Exception&) {
118
                // Expected for invalid padding
119
95
            }
120
139
            break;
121
139
        }
122
        
123
140
        case 7: {
124
            // Test Base32Hex with various padding scenarios
125
140
            try {
126
140
                std::string test_str = fdp.ConsumeRandomLengthString();
127
                // Add various padding permutations
128
241
                for (int i = 0; i < fdp.ConsumeIntegralInRange(0, 6); i++) {
129
101
                    test_str += "=";
130
101
                }
131
140
                decodeBase32Hex(test_str, decoded_output);
132
140
            } catch (const isc::Exception&) {
133
                // Expected for invalid padding
134
118
            }
135
140
            break;
136
140
        }
137
        
138
65
        case 8: {
139
            // Test mixed case Base64 (should be case-sensitive)
140
65
            try {
141
65
                encoded_str = fdp.ConsumeRemainingBytesAsString();
142
                // Mix uppercase and lowercase
143
8.51M
                for (auto& c : encoded_str) {
144
8.51M
                    if (fdp.ConsumeBool() && isalpha(c)) {
145
0
                        c = (isupper(c)) ? tolower(c) : toupper(c);
146
0
                    }
147
8.51M
                }
148
65
                decodeBase64(encoded_str, decoded_output);
149
65
            } catch (const isc::Exception&) {
150
                // Expected for case errors
151
63
            }
152
65
            break;
153
65
        }
154
        
155
67
        case 9: {
156
            // Test mixed case Base32Hex (case-insensitive)
157
67
            try {
158
67
                encoded_str = fdp.ConsumeRemainingBytesAsString();
159
                // Mix uppercase and lowercase
160
6.69M
                for (auto& c : encoded_str) {
161
6.69M
                    if (fdp.ConsumeBool() && isalpha(c)) {
162
0
                        c = (isupper(c)) ? tolower(c) : toupper(c);
163
0
                    }
164
6.69M
                }
165
67
                decodeBase32Hex(encoded_str, decoded_output);
166
67
            } catch (const isc::Exception&) {
167
                // May succeed due to case-insensitivity
168
64
            }
169
67
            break;
170
67
        }
171
        
172
67
        case 10: {
173
            // Test Base16 with mixed case (case-insensitive)
174
67
            try {
175
67
                encoded_str = fdp.ConsumeRemainingBytesAsString();
176
                // Mix uppercase and lowercase
177
7.35M
                for (auto& c : encoded_str) {
178
7.35M
                    if (fdp.ConsumeBool() && isalpha(c)) {
179
0
                        c = (isupper(c)) ? tolower(c) : toupper(c);
180
0
                    }
181
7.35M
                }
182
67
                decodeHex(encoded_str, decoded_output);
183
67
            } catch (const isc::Exception&) {
184
                // May succeed due to case-insensitivity
185
66
            }
186
67
            break;
187
67
        }
188
        
189
298
        case 11: {
190
            // Test encoding/decoding with whitespace injection
191
298
            try {
192
298
                std::string test_str = fdp.ConsumeRandomLengthString();
193
                // Inject whitespace characters
194
298
                size_t insertions = fdp.ConsumeIntegralInRange<size_t>(0, 10);
195
1.26k
                for (size_t i = 0; i < insertions && test_str.size() > 0; i++) {
196
969
                    size_t pos = fdp.ConsumeIntegralInRange<size_t>(0, test_str.size());
197
969
                    char ws = fdp.PickValueInArray({' ', '\t', '\n', '\r'});
198
969
                    test_str.insert(pos, 1, ws);
199
969
                }
200
                
201
                // Try decoding with all encoders
202
298
                try { decodeBase64(test_str, decoded_output); } catch (...) {}
203
298
                try { decodeBase32Hex(test_str, decoded_output); } catch (...) {}
204
298
                try { decodeHex(test_str, decoded_output); } catch (...) {}
205
298
            } catch (const isc::Exception&) {
206
                // Expected for whitespace handling
207
0
            }
208
298
            break;
209
298
        }
210
1.33k
    }
211
    
212
1.33k
    return 0;
213
1.33k
}