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