Commit b9bc53ed authored by Daniel Hosseinian's avatar Daniel Hosseinian Committed by Commit Bot

Add base32_fuzzer for base32::Base32Encode()

Change-Id: Ib2a9cf2b39eb758d120cff562c385d17a6d5c526
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1861139
Commit-Queue: Daniel Hosseinian <dhoss@chromium.org>
Reviewed-by: default avatarGabriel Charette <gab@chromium.org>
Cr-Commit-Position: refs/heads/master@{#708075}
parent 8c617f05
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
# Use of this source code is governed by a BSD-style license that can be # Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file. # found in the LICENSE file.
import("//testing/libfuzzer/fuzzer_test.gni")
static_library("base32") { static_library("base32") {
sources = [ sources = [
"base32.cc", "base32.cc",
...@@ -38,3 +40,15 @@ source_set("unit_tests") { ...@@ -38,3 +40,15 @@ source_set("unit_tests") {
"//testing/gtest", "//testing/gtest",
] ]
} }
fuzzer_test("base32_fuzzer") {
sources = [
"base32_fuzzer.cc",
]
deps = [
":base32",
":base32_test_util",
"//base",
]
}
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <stddef.h>
#include <stdint.h>
#include <limits>
#include "base/logging.h"
#include "base/stl_util.h"
#include "components/base32/base32.h"
#include "components/base32/base32_test_util.h"
base32::Base32EncodePolicy GetBase32EncodePolicyFromUint8(uint8_t value) {
// Dummy switch to detect changes to the enum definition.
switch (base32::Base32EncodePolicy()) {
case base32::Base32EncodePolicy::INCLUDE_PADDING:
case base32::Base32EncodePolicy::OMIT_PADDING:
break;
}
return (value % 2) == 0 ? base32::Base32EncodePolicy::INCLUDE_PADDING
: base32::Base32EncodePolicy::OMIT_PADDING;
}
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
if (size < 2 || size > std::numeric_limits<size_t>::max() / 5)
return 0;
const base32::Base32EncodePolicy encode_policy =
GetBase32EncodePolicyFromUint8(data[0]);
const base::StringPiece string_piece_input(
reinterpret_cast<const char*>(data + 1), size - 1);
std::string encoded_string =
base32::Base32Encode(string_piece_input, encode_policy);
std::string decoded_string = base32::Base32Decode(encoded_string);
CHECK_EQ(string_piece_input, decoded_string);
return 0;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment