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