xref: /aosp_15_r20/external/cronet/base/nix/mime_util_xdg_fuzzer.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2023 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 <stdlib.h>
6 #include <string>
7 
8 #include "base/containers/span.h"
9 #include "base/files/file_path.h"
10 #include "base/files/file_util.h"
11 #include "base/files/scoped_temp_dir.h"
12 #include "base/logging.h"
13 #include "base/nix/mime_util_xdg.h"
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   base::ScopedTempDir temp_dir;
18   if (!temp_dir.CreateUniqueTempDir()) {
19     // Not a fuzzer error, so we return 0.
20     LOG(ERROR) << "Failed to create temp dir";
21     return 0;
22   }
23 
24   // The parser reads file $XDG_DATA_DIRS/mime/mime.cache.
25   setenv("XDG_DATA_DIRS", temp_dir.GetPath().value().c_str(), 1);
26   base::FilePath mime_dir = temp_dir.GetPath().Append("mime");
27   base::FilePath mime_cache = mime_dir.Append("mime.cache");
28   if (!base::CreateDirectory(mime_dir) ||
29       !base::WriteFile(mime_cache, base::make_span(data, size))) {
30     LOG(ERROR) << "Failed to create " << mime_cache;
31     // Not a fuzzer error, so we return 0.
32     return 0;
33   }
34 
35   base::FilePath dummy_path("foo.txt");
36   std::string type = base::nix::GetFileMimeType(dummy_path);
37   return 0;
38 }
39