1 /* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #include <cstdint>
17 #include <cstdlib>
18 #include <iostream>
19 #include <regex>  // NOLINT
20 
21 #include "absl/strings/match.h"
22 #include "tensorflow/core/platform/path.h"
23 
24 // This is a fuzzer for tensorflow::io::CleanPath.
25 
26 namespace {
27 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)28 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
29   std::string input_path(reinterpret_cast<const char *>(data), size);
30   std::string clean_path = tensorflow::io::CleanPath(input_path);
31 
32   // Assert there are no '/./' no directory changes.
33   assert(!absl::StrContains(clean_path, "/./"));
34   // Assert there are no duplicate '/'.
35   assert(!absl::StrContains(clean_path, "//"));
36   // Assert there are no higher up directories after entering a directory.
37   std::regex higher_up_directory("[^.]{1}/[.]{2}");
38   assert(!std::regex_match(clean_path, higher_up_directory));
39 
40   return 0;
41 }
42 
43 }  // namespace
44