1 // Copyright (c) 2011 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 "base/files/file_path.h" 6 #include "chrome/browser/policy/policy_path_parser.h" 7 8 #include "testing/gtest/include/gtest/gtest.h" 9 10 namespace policy { 11 12 class PolicyPathParserTests : public testing::Test { 13 protected: 14 void CheckForSubstitution(base::FilePath::StringType test_string, 15 base::FilePath::StringType var_name) { 16 base::FilePath::StringType var(test_string); 17 base::FilePath::StringType var_result = 18 path_parser::ExpandPathVariables(var); 19 ASSERT_EQ(var_result.find(var_name), base::FilePath::StringType::npos); 20 } 21 }; 22 23 TEST_F(PolicyPathParserTests, AllPlatformVariables) { 24 // No vars whatsoever no substitution should occur. 25 base::FilePath::StringType no_vars(FILE_PATH_LITERAL("//$C/shares")); 26 base::FilePath::StringType no_vars_result = 27 path_parser::ExpandPathVariables(no_vars); 28 ASSERT_EQ(no_vars_result, no_vars); 29 30 // This is unknown variable and shouldn't be substituted. 31 base::FilePath::StringType unknown_vars(FILE_PATH_LITERAL("//$C/${buggy}")); 32 base::FilePath::StringType unknown_vars_result = 33 path_parser::ExpandPathVariables(unknown_vars); 34 ASSERT_EQ(unknown_vars_result, unknown_vars); 35 36 // Trim quotes around, but not inside paths. Test against bug 80211. 37 base::FilePath::StringType no_quotes(FILE_PATH_LITERAL("//$C/\"a\"/$path")); 38 base::FilePath::StringType single_quotes( 39 FILE_PATH_LITERAL("'//$C/\"a\"/$path'")); 40 base::FilePath::StringType double_quotes( 41 FILE_PATH_LITERAL("\"//$C/\"a\"/$path\"")); 42 base::FilePath::StringType quotes_result = 43 path_parser::ExpandPathVariables(single_quotes); 44 ASSERT_EQ(quotes_result, no_quotes); 45 quotes_result = path_parser::ExpandPathVariables(double_quotes); 46 ASSERT_EQ(quotes_result, no_quotes); 47 48 // Both should have been substituted. 49 base::FilePath::StringType vars( 50 FILE_PATH_LITERAL("${user_name}${machine_name}")); 51 base::FilePath::StringType vars_result = 52 path_parser::ExpandPathVariables(vars); 53 ASSERT_EQ(vars_result.find(FILE_PATH_LITERAL("${user_name}")), 54 base::FilePath::StringType::npos); 55 ASSERT_EQ(vars_result.find(FILE_PATH_LITERAL("${machine_name}")), 56 base::FilePath::StringType::npos); 57 58 // Should substitute only one instance. 59 vars = FILE_PATH_LITERAL("${machine_name}${machine_name}"); 60 vars_result = path_parser::ExpandPathVariables(vars); 61 size_t pos = vars_result.find(FILE_PATH_LITERAL("${machine_name}")); 62 ASSERT_NE(pos, base::FilePath::StringType::npos); 63 ASSERT_EQ(vars_result.find(FILE_PATH_LITERAL("${machine_name}"), pos+1), 64 base::FilePath::StringType::npos); 65 66 vars =FILE_PATH_LITERAL("${user_name}${machine_name}"); 67 vars_result = path_parser::ExpandPathVariables(vars); 68 ASSERT_EQ(vars_result.find(FILE_PATH_LITERAL("${user_name}")), 69 base::FilePath::StringType::npos); 70 ASSERT_EQ(vars_result.find(FILE_PATH_LITERAL("${machine_name}")), 71 base::FilePath::StringType::npos); 72 73 CheckForSubstitution(FILE_PATH_LITERAL("//$C/${user_name}"), 74 FILE_PATH_LITERAL("${user_name}")); 75 CheckForSubstitution(FILE_PATH_LITERAL("//$C/${machine_name}"), 76 FILE_PATH_LITERAL("${machine_name}")); 77 } 78 79 #if defined(OS_MACOSX) 80 81 TEST_F(PolicyPathParserTests, MacVariables) { 82 CheckForSubstitution(FILE_PATH_LITERAL("//$C/${users}"), 83 FILE_PATH_LITERAL("${users}")); 84 CheckForSubstitution(FILE_PATH_LITERAL("//$C/${documents}"), 85 FILE_PATH_LITERAL("${documents}")); 86 } 87 88 #elif defined(OS_WIN) 89 90 TEST_F(PolicyPathParserTests, WinVariables) { 91 CheckForSubstitution(FILE_PATH_LITERAL("//$C/${documents}"), 92 FILE_PATH_LITERAL("${documents}")); 93 CheckForSubstitution(FILE_PATH_LITERAL("//$C/${local_app_data}"), 94 FILE_PATH_LITERAL("${local_app_data}")); 95 CheckForSubstitution(FILE_PATH_LITERAL("//$C/${roaming_app_data}"), 96 FILE_PATH_LITERAL("${roaming_app_data}")); 97 CheckForSubstitution(FILE_PATH_LITERAL("//$C/${profile}"), 98 FILE_PATH_LITERAL("${profile}")); 99 CheckForSubstitution(FILE_PATH_LITERAL("//$C/${global_app_data}"), 100 FILE_PATH_LITERAL("${global_app_data}")); 101 CheckForSubstitution(FILE_PATH_LITERAL("//$C/${program_files}"), 102 FILE_PATH_LITERAL("${program_files}")); 103 CheckForSubstitution(FILE_PATH_LITERAL("//$C/${windows}"), 104 FILE_PATH_LITERAL("${windows}")); 105 CheckForSubstitution(FILE_PATH_LITERAL("//$C/${client_name}"), 106 FILE_PATH_LITERAL("${client_name}")); 107 } 108 109 #endif // OS_WIN 110 111 } // namespace policy 112