OLD | NEW |
| (Empty) |
1 // Copyright 2015 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 "components/resource_provider/file_utils.h" | |
6 | |
7 #include <stddef.h> | |
8 | |
9 #include "base/files/file_path.h" | |
10 #include "base/macros.h" | |
11 #include "base/path_service.h" | |
12 #include "base/strings/string_util.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 | |
15 namespace resource_provider { | |
16 | |
17 // Assertions for invalid app paths. | |
18 TEST(FileUtilsTest, InvalidAppPath) { | |
19 struct TestCase { | |
20 std::string name; | |
21 }; | |
22 struct TestCase invalid_cases[]{ | |
23 {"http://foo"}, // Must start with 'mojo:'. | |
24 {"mojo:."}, // Don't allow '.'. | |
25 {"mojo:.."}, // Don't allow '..'. | |
26 {"mojo:foo/."}, // Don't allow '.'. | |
27 {"mojo:bar/.."}, // Don't allow '..'. | |
28 }; | |
29 | |
30 for (size_t i = 0; i < arraysize(invalid_cases); ++i) { | |
31 base::FilePath resulting_path(GetPathForApplicationName( | |
32 invalid_cases[i].name)); | |
33 EXPECT_TRUE(resulting_path.empty()) << "i=" << i | |
34 << " input=" << invalid_cases[i].name | |
35 << " result=" << resulting_path.value(); | |
36 } | |
37 } | |
38 | |
39 // Assertions for invalid app paths. | |
40 TEST(FileUtilsTest, InvalidResourcePath) { | |
41 struct TestCase { | |
42 std::string path; | |
43 }; | |
44 struct TestCase invalid_cases[]{ | |
45 {"."}, | |
46 {".."}, | |
47 {"foo/."}, | |
48 {"bar/.."}, | |
49 {"foo/./bar"}, | |
50 {"bar/../baz"}, | |
51 {"bar/baz/"}, | |
52 {"bar//baz/"}, | |
53 }; | |
54 | |
55 const base::FilePath app_path(GetPathForApplicationName("mojo:test")); | |
56 ASSERT_FALSE(app_path.empty()); | |
57 | |
58 for (size_t i = 0; i < arraysize(invalid_cases); ++i) { | |
59 base::FilePath resulting_path( | |
60 GetPathForResourceNamed(app_path, invalid_cases[i].path)); | |
61 EXPECT_TRUE(resulting_path.empty()) << i | |
62 << " input=" << invalid_cases[i].path | |
63 << " result=" << resulting_path.value(); | |
64 } | |
65 } | |
66 | |
67 TEST(FileUtilsTest, ValidPaths) { | |
68 const base::FilePath app_path(GetPathForApplicationName("mojo:test")); | |
69 ASSERT_FALSE(app_path.empty()); | |
70 | |
71 // Trivial single path element. | |
72 const base::FilePath trivial_path( | |
73 GetPathForResourceNamed(app_path, "single")); | |
74 EXPECT_EQ(app_path.AppendASCII("single").value(), trivial_path.value()); | |
75 | |
76 // Two path elements. | |
77 const base::FilePath two_paths(GetPathForResourceNamed(app_path, "a/b")); | |
78 EXPECT_EQ(app_path.AppendASCII("a").AppendASCII("b").value(), | |
79 two_paths.value()); | |
80 } | |
81 | |
82 } // namespace resource_provider | |
OLD | NEW |