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