1// Copyright (c) 2010 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/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(FilePath::StringType test_string,
15                            FilePath::StringType var_name) {
16    FilePath::StringType var(test_string);
17    FilePath::StringType var_result =
18        path_parser::ExpandPathVariables(var);
19    ASSERT_EQ(var_result.find(var_name), FilePath::StringType::npos);
20  }
21};
22
23TEST_F(PolicyPathParserTests, AllPlatformVariables) {
24  // No vars whatsoever no substitution should occur.
25  FilePath::StringType no_vars(FILE_PATH_LITERAL("//$C/shares"));
26  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  FilePath::StringType unknown_vars(FILE_PATH_LITERAL("//$C/${buggy}"));
32  FilePath::StringType unknown_vars_result =
33      path_parser::ExpandPathVariables(unknown_vars);
34  ASSERT_EQ(unknown_vars_result, unknown_vars);
35
36  // Both should have been substituted.
37  FilePath::StringType vars(FILE_PATH_LITERAL("${user_name}${machine_name}"));
38  FilePath::StringType vars_result = path_parser::ExpandPathVariables(vars);
39  ASSERT_EQ(vars_result.find(FILE_PATH_LITERAL("${user_name}")),
40            FilePath::StringType::npos);
41  ASSERT_EQ(vars_result.find(FILE_PATH_LITERAL("${machine_name}")),
42            FilePath::StringType::npos);
43
44  // Should substitute only one instance.
45  vars = FILE_PATH_LITERAL("${machine_name}${machine_name}");
46  vars_result = path_parser::ExpandPathVariables(vars);
47  size_t pos = vars_result.find(FILE_PATH_LITERAL("${machine_name}"));
48  ASSERT_NE(pos, FilePath::StringType::npos);
49  ASSERT_EQ(vars_result.find(FILE_PATH_LITERAL("${machine_name}"), pos+1),
50            FilePath::StringType::npos);
51
52  vars =FILE_PATH_LITERAL("${user_name}${machine_name}");
53  vars_result = path_parser::ExpandPathVariables(vars);
54  ASSERT_EQ(vars_result.find(FILE_PATH_LITERAL("${user_name}")),
55            FilePath::StringType::npos);
56  ASSERT_EQ(vars_result.find(FILE_PATH_LITERAL("${machine_name}")),
57            FilePath::StringType::npos);
58
59  CheckForSubstitution(FILE_PATH_LITERAL("//$C/${user_name}"),
60                       FILE_PATH_LITERAL("${user_name}"));
61  CheckForSubstitution(FILE_PATH_LITERAL("//$C/${machine_name}"),
62                       FILE_PATH_LITERAL("${machine_name}"));
63}
64
65#if defined(OS_MACOSX)
66
67TEST_F(PolicyPathParserTests, MacVariables) {
68  CheckForSubstitution(FILE_PATH_LITERAL("//$C/${users}"),
69                       FILE_PATH_LITERAL("${users}"));
70  CheckForSubstitution(FILE_PATH_LITERAL("//$C/${documents}"),
71                       FILE_PATH_LITERAL("${documents}"));
72}
73
74#elif defined(OS_WIN)
75
76TEST_F(PolicyPathParserTests, WinVariables) {
77  CheckForSubstitution(FILE_PATH_LITERAL("//$C/${documents}"),
78                       FILE_PATH_LITERAL("${documents}"));
79  CheckForSubstitution(FILE_PATH_LITERAL("//$C/${local_app_data}"),
80                       FILE_PATH_LITERAL("${local_app_data}"));
81  CheckForSubstitution(FILE_PATH_LITERAL("//$C/${roaming_app_data}"),
82                       FILE_PATH_LITERAL("${roaming_app_data}"));
83  CheckForSubstitution(FILE_PATH_LITERAL("//$C/${profile}"),
84                       FILE_PATH_LITERAL("${profile}"));
85  CheckForSubstitution(FILE_PATH_LITERAL("//$C/${global_app_data}"),
86                       FILE_PATH_LITERAL("${global_app_data}"));
87  CheckForSubstitution(FILE_PATH_LITERAL("//$C/${program_files}"),
88                       FILE_PATH_LITERAL("${program_files}"));
89  CheckForSubstitution(FILE_PATH_LITERAL("//$C/${windows}"),
90                       FILE_PATH_LITERAL("${windows}"));
91}
92
93#endif  // OS_WIN
94
95}  // namespace policy
96