xref: /aosp_15_r20/external/cronet/testing/libfuzzer/fuzzers/snappy_compress_fuzzer.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2018 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 "third_party/snappy/src/snappy.h"
6 
7 #include <cstdlib>
8 
9 #define FUZZING_ASSERT(condition)                                      \
10   if (!(condition)) {                                                  \
11     fprintf(stderr, "%s\n", "Fuzzing Assertion Failure: " #condition); \
12     abort();                                                           \
13   }
14 
15 // Entry point for LibFuzzer.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)16 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
17   const char* uncompressed = reinterpret_cast<const char*>(data);
18   std::string compressed;
19   snappy::Compress(uncompressed, size, &compressed);
20   FUZZING_ASSERT(
21       snappy::IsValidCompressedBuffer(compressed.data(), compressed.size()));
22 
23   std::string uncompressed_after_compress;
24   FUZZING_ASSERT(snappy::Uncompress(compressed.data(), compressed.size(),
25                                     &uncompressed_after_compress));
26   FUZZING_ASSERT(uncompressed_after_compress ==
27                  std::string(uncompressed, size));
28 
29   return 0;
30 }
31