Home | History | Annotate | Download | only in safe_browsing
      1 // Copyright 2014 The Chromium Authors. All rights reserved.
      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 "chrome/browser/safe_browsing/path_sanitizer.h"
      6 
      7 #include <vector>
      8 
      9 #include "base/logging.h"
     10 #include "base/path_service.h"
     11 #include "testing/gtest/include/gtest/gtest.h"
     12 
     13 namespace {
     14 
     15 // Returns the root directory with a trailing separator. Works on all platforms.
     16 base::FilePath GetRootDirectory() {
     17   base::FilePath dir_temp;
     18   if (!PathService::Get(base::DIR_TEMP, &dir_temp))
     19     NOTREACHED();
     20 
     21   std::vector<base::FilePath::StringType> components;
     22   dir_temp.GetComponents(&components);
     23 
     24   return base::FilePath(components[0]).AsEndingWithSeparator();
     25 }
     26 
     27 }  // namespace
     28 
     29 TEST(SafeBrowsingPathSanitizerTest, HomeDirectoryIsNotEmpty) {
     30   safe_browsing::PathSanitizer path_sanitizer;
     31 
     32   ASSERT_FALSE(path_sanitizer.GetHomeDirectory().empty());
     33 }
     34 
     35 TEST(SafeBrowsingPathSanitizerTest, DontStripHomeDirectoryTest) {
     36   // Test with path not in home directory.
     37   base::FilePath path =
     38       GetRootDirectory().Append(FILE_PATH_LITERAL("not_in_home_directory.ext"));
     39   base::FilePath path_expected = path;
     40 
     41   safe_browsing::PathSanitizer path_sanitizer;
     42   path_sanitizer.StripHomeDirectory(&path);
     43 
     44   ASSERT_EQ(path.value(), path_expected.value());
     45 }
     46 
     47 TEST(SafeBrowsingPathSanitizerTest, DoStripHomeDirectoryTest) {
     48   // Test with path in home directory.
     49   safe_browsing::PathSanitizer path_sanitizer;
     50 
     51   base::FilePath path = path_sanitizer.GetHomeDirectory().Append(
     52       FILE_PATH_LITERAL("in_home_directory.ext"));
     53   base::FilePath path_expected = base::FilePath(FILE_PATH_LITERAL("~")).Append(
     54       FILE_PATH_LITERAL("in_home_directory.ext"));
     55 
     56   path_sanitizer.StripHomeDirectory(&path);
     57 
     58   ASSERT_EQ(path.value(), path_expected.value());
     59 }
     60