Coverage Report

Created: 2025-12-31 07:33

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.30k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
20
1.30k
    if (size < 2) {
21
1
        return 0;
22
1
    }
23
24
1.30k
    FuzzedDataProvider fdp(data, size);
25
    
26
    // Choose which encoding/decoding path to test
27
1.30k
    uint8_t path = fdp.ConsumeIntegralInRange<uint8_t>(0, 11);
28
    
29
1.30k
    std::vector<uint8_t> binary_data;
30
1.30k
    std::string encoded_str;
31
1.30k
    std::vector<uint8_t> decoded_output;
32
    
33
1.30k
    switch (path) {
34
116
        case 0: {
35
            // Test Base64 encoding from binary data
36
116
            try {
37
116
                size_t bin_size = fdp.ConsumeIntegralInRange<size_t>(0, size);
38
116
                binary_data = fdp.ConsumeBytes<uint8_t>(bin_size);
39
116
                encoded_str = encodeBase64(binary_data);
40
                // Verify round-trip
41
116
                decodeBase64(encoded_str, decoded_output);
42
116
            } catch (const isc::Exception&) {
43
                // Expected for invalid input
44
0
            }
45
116
            break;
46
116
        }
47
        
48
92
        case 1: {
49
            // Test Base64 decoding from string
50
92
            try {
51
92
                encoded_str = fdp.ConsumeRemainingBytesAsString();
52
92
                decodeBase64(encoded_str, decoded_output);
53
92
            } catch (const isc::Exception&) {
54
                // Expected for invalid Base64 strings
55
89
            }
56
92
            break;
57
92
        }
58
        
59
96
        case 2: {
60
            // Test Base32Hex encoding from binary data
61
96
            try {
62
96
                size_t bin_size = fdp.ConsumeIntegralInRange<size_t>(0, size);
63
96
                binary_data = fdp.ConsumeBytes<uint8_t>(bin_size);
64
96
                encoded_str = encodeBase32Hex(binary_data);
65
                // Verify round-trip
66
96
                decodeBase32Hex(encoded_str, decoded_output);
67
96
            } catch (const isc::Exception&) {
68
                // Expected for invalid input
69
0
            }
70
96
            break;
71
96
        }
72
        
73
67
        case 3: {
74
            // Test Base32Hex decoding from string
75
67
            try {
76
67
                encoded_str = fdp.ConsumeRemainingBytesAsString();
77
67
                decodeBase32Hex(encoded_str, decoded_output);
78
67
            } catch (const isc::Exception&) {
79
                // Expected for invalid Base32Hex strings
80
65
            }
81
67
            break;
82
67
        }
83
        
84
111
        case 4: {
85
            // Test Base16 (hex) encoding from binary data
86
111
            try {
87
111
                size_t bin_size = fdp.ConsumeIntegralInRange<size_t>(0, size);
88
111
                binary_data = fdp.ConsumeBytes<uint8_t>(bin_size);
89
111
                encoded_str = encodeHex(binary_data);
90
                // Verify round-trip
91
111
                decodeHex(encoded_str, decoded_output);
92
111
            } catch (const isc::Exception&) {
93
                // Expected for invalid input
94
0
            }
95
111
            break;
96
111
        }
97
        
98
68
        case 5: {
99
            // Test Base16 (hex) decoding from string
100
68
            try {
101
68
                encoded_str = fdp.ConsumeRemainingBytesAsString();
102
68
                decodeHex(encoded_str, decoded_output);
103
68
            } catch (const isc::Exception&) {
104
                // Expected for invalid hex strings
105
65
            }
106
68
            break;
107
68
        }
108
        
109
142
        case 6: {
110
            // Test Base64 with various padding scenarios
111
142
            try {
112
142
                std::string test_str = fdp.ConsumeRandomLengthString();
113
                // Add various padding permutations
114
142
                test_str += fdp.ConsumeBool() ? "=" : "";
115
142
                test_str += fdp.ConsumeBool() ? "=" : "";
116
142
                decodeBase64(test_str, decoded_output);
117
142
            } catch (const isc::Exception&) {
118
                // Expected for invalid padding
119
97
            }
120
142
            break;
121
142
        }
122
        
123
132
        case 7: {
124
            // Test Base32Hex with various padding scenarios
125
132
            try {
126
132
                std::string test_str = fdp.ConsumeRandomLengthString();
127
                // Add various padding permutations
128
238
                for (int i = 0; i < fdp.ConsumeIntegralInRange(0, 6); i++) {
129
106
                    test_str += "=";
130
106
                }
131
132
                decodeBase32Hex(test_str, decoded_output);
132
132
            } catch (const isc::Exception&) {
133
                // Expected for invalid padding
134
108
            }
135
132
            break;
136
132
        }
137
        
138
64
        case 8: {
139
            // Test mixed case Base64 (should be case-sensitive)
140
64
            try {
141
64
                encoded_str = fdp.ConsumeRemainingBytesAsString();
142
                // Mix uppercase and lowercase
143
8.42M
                for (auto& c : encoded_str) {
144
8.42M
                    if (fdp.ConsumeBool() && isalpha(c)) {
145
0
                        c = (isupper(c)) ? tolower(c) : toupper(c);
146
0
                    }
147
8.42M
                }
148
64
                decodeBase64(encoded_str, decoded_output);
149
64
            } catch (const isc::Exception&) {
150
                // Expected for case errors
151
63
            }
152
64
            break;
153
64
        }
154
        
155
72
        case 9: {
156
            // Test mixed case Base32Hex (case-insensitive)
157
72
            try {
158
72
                encoded_str = fdp.ConsumeRemainingBytesAsString();
159
                // Mix uppercase and lowercase
160
7.87M
                for (auto& c : encoded_str) {
161
7.87M
                    if (fdp.ConsumeBool() && isalpha(c)) {
162
0
                        c = (isupper(c)) ? tolower(c) : toupper(c);
163
0
                    }
164
7.87M
                }
165
72
                decodeBase32Hex(encoded_str, decoded_output);
166
72
            } catch (const isc::Exception&) {
167
                // May succeed due to case-insensitivity
168
71
            }
169
72
            break;
170
72
        }
171
        
172
62
        case 10: {
173
            // Test Base16 with mixed case (case-insensitive)
174
62
            try {
175
62
                encoded_str = fdp.ConsumeRemainingBytesAsString();
176
                // Mix uppercase and lowercase
177
6.30M
                for (auto& c : encoded_str) {
178
6.30M
                    if (fdp.ConsumeBool() && isalpha(c)) {
179
0
                        c = (isupper(c)) ? tolower(c) : toupper(c);
180
0
                    }
181
6.30M
                }
182
62
                decodeHex(encoded_str, decoded_output);
183
62
            } catch (const isc::Exception&) {
184
                // May succeed due to case-insensitivity
185
60
            }
186
62
            break;
187
62
        }
188
        
189
283
        case 11: {
190
            // Test encoding/decoding with whitespace injection
191
283
            try {
192
283
                std::string test_str = fdp.ConsumeRandomLengthString();
193
                // Inject whitespace characters
194
283
                size_t insertions = fdp.ConsumeIntegralInRange<size_t>(0, 10);
195
1.08k
                for (size_t i = 0; i < insertions && test_str.size() > 0; i++) {
196
805
                    size_t pos = fdp.ConsumeIntegralInRange<size_t>(0, test_str.size());
197
805
                    char ws = fdp.PickValueInArray({' ', '\t', '\n', '\r'});
198
805
                    test_str.insert(pos, 1, ws);
199
805
                }
200
                
201
                // Try decoding with all encoders
202
283
                try { decodeBase64(test_str, decoded_output); } catch (...) {}
203
283
                try { decodeBase32Hex(test_str, decoded_output); } catch (...) {}
204
283
                try { decodeHex(test_str, decoded_output); } catch (...) {}
205
283
            } catch (const isc::Exception&) {
206
                // Expected for whitespace handling
207
0
            }
208
283
            break;
209
283
        }
210
1.30k
    }
211
    
212
1.30k
    return 0;
213
1.30k
}