1 // Copyright 2022 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <fuzzer/FuzzedDataProvider.h>
6 #include <stdint.h>
7
8 #include <string>
9 #include <tuple>
10
11 #include "base/base64url.h"
12 #include "base/check.h"
13 #include "base/check_op.h"
14
15 namespace base {
16
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)17 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
18 FuzzedDataProvider provider(data, size);
19
20 // Test encoding of a random plaintext.
21 std::string plaintext = provider.ConsumeRandomLengthString();
22 Base64UrlEncodePolicy encode_policy =
23 provider.ConsumeBool() ? Base64UrlEncodePolicy::INCLUDE_PADDING
24 : Base64UrlEncodePolicy::OMIT_PADDING;
25 std::string encoded;
26 Base64UrlEncode(plaintext, encode_policy, &encoded);
27
28 // Check decoding of the above gives the original text.
29 std::string decoded;
30 CHECK(Base64UrlDecode(encoded,
31 encode_policy == Base64UrlEncodePolicy::INCLUDE_PADDING
32 ? Base64UrlDecodePolicy::REQUIRE_PADDING
33 : Base64UrlDecodePolicy::DISALLOW_PADDING,
34 &decoded));
35 CHECK_EQ(decoded, plaintext);
36 // Same result should be when ignoring padding.
37 decoded.clear();
38 CHECK(Base64UrlDecode(encoded, Base64UrlDecodePolicy::IGNORE_PADDING,
39 &decoded));
40 CHECK_EQ(decoded, plaintext);
41
42 // Additionally test decoding of a random input.
43 std::string decoding_input = provider.ConsumeRandomLengthString();
44 Base64UrlDecodePolicy decode_policy;
45 switch (provider.ConsumeIntegralInRange<int>(0, 2)) {
46 case 0:
47 decode_policy = Base64UrlDecodePolicy::REQUIRE_PADDING;
48 break;
49 case 1:
50 decode_policy = Base64UrlDecodePolicy::IGNORE_PADDING;
51 break;
52 case 2:
53 decode_policy = Base64UrlDecodePolicy::DISALLOW_PADDING;
54 break;
55 }
56 std::ignore = Base64UrlDecode(decoding_input, decode_policy, &decoded);
57
58 return 0;
59 }
60
61 } // namespace base
62