Fixes for Android GN build input/outputs
[chromium-blink-merge.git] / mojo / runner / url_resolver_unittest.cc
blob75d213f148ea6c41793fedaf717274d0da55db69
1 // Copyright 2014 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.
5 #include "mojo/runner/url_resolver.h"
7 #include "base/files/file_util.h"
8 #include "base/files/scoped_temp_dir.h"
9 #include "base/logging.h"
10 #include "mojo/util/filename_util.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace mojo {
14 namespace runner {
15 namespace test {
16 namespace {
18 typedef testing::Test URLResolverTest;
20 TEST_F(URLResolverTest, MojoURLsFallThrough) {
21 URLResolver resolver;
22 resolver.AddURLMapping(GURL("mojo:test"), GURL("mojo:foo"));
23 const GURL base_url("file:/base");
24 resolver.SetMojoBaseURL(base_url);
25 GURL mapped_url = resolver.ApplyMappings(GURL("mojo:test"));
26 std::string resolved(resolver.ResolveMojoURL(mapped_url).spec());
27 // Resolved must start with |base_url|.
28 EXPECT_EQ(base_url.spec(), resolved.substr(0, base_url.spec().size()));
29 // And must contain foo.
30 EXPECT_NE(std::string::npos, resolved.find("foo"));
33 TEST_F(URLResolverTest, MapURL) {
34 URLResolver resolver;
35 resolver.AddURLMapping(GURL("https://domokit.org/test.mojo"),
36 GURL("file:///mojo/src/out/Debug/test.mojo"));
37 GURL mapped_url =
38 resolver.ApplyMappings(GURL("https://domokit.org/test.mojo"));
39 EXPECT_EQ("file:///mojo/src/out/Debug/test.mojo", mapped_url.spec());
42 TEST_F(URLResolverTest, MultipleMapURL) {
43 URLResolver resolver;
44 resolver.AddURLMapping(GURL("https://a.org/foo"),
45 GURL("https://b.org/a/foo"));
46 resolver.AddURLMapping(GURL("https://b.org/a/foo"),
47 GURL("https://c.org/b/a/foo"));
48 GURL mapped_url = resolver.ApplyMappings(GURL("https://a.org/foo"));
49 EXPECT_EQ("https://c.org/b/a/foo", mapped_url.spec());
52 TEST_F(URLResolverTest, MapOrigin) {
53 URLResolver resolver;
54 resolver.AddOriginMapping(GURL("https://domokit.org"),
55 GURL("file:///mojo/src/out/Debug"));
56 GURL mapped_url =
57 resolver.ApplyMappings(GURL("https://domokit.org/test.mojo"));
58 EXPECT_EQ("file:///mojo/src/out/Debug/test.mojo", mapped_url.spec());
61 TEST_F(URLResolverTest, MultipleMapOrigin) {
62 URLResolver resolver;
63 resolver.AddOriginMapping(GURL("https://a.org"), GURL("https://b.org/a"));
64 resolver.AddOriginMapping(GURL("https://b.org"), GURL("https://c.org/b"));
65 GURL mapped_url = resolver.ApplyMappings(GURL("https://a.org/foo"));
66 EXPECT_EQ("https://c.org/b/a/foo", mapped_url.spec());
69 TEST_F(URLResolverTest, MapOriginThenURL) {
70 URLResolver resolver;
71 resolver.AddOriginMapping(GURL("https://a.org"), GURL("https://b.org/a"));
72 resolver.AddURLMapping(GURL("https://b.org/a/foo"),
73 GURL("https://c.org/b/a/foo"));
74 GURL mapped_url = resolver.ApplyMappings(GURL("https://a.org/foo"));
75 EXPECT_EQ("https://c.org/b/a/foo", mapped_url.spec());
78 TEST_F(URLResolverTest, MapURLThenOrigin) {
79 URLResolver resolver;
80 resolver.AddURLMapping(GURL("https://a.org/foo"),
81 GURL("https://b.org/a/foo"));
82 resolver.AddOriginMapping(GURL("https://b.org"), GURL("https://c.org/b"));
83 GURL mapped_url = resolver.ApplyMappings(GURL("https://a.org/foo"));
84 EXPECT_EQ("https://c.org/b/a/foo", mapped_url.spec());
87 #if defined(OS_POSIX)
88 #define ARG_LITERAL(x) x
89 #elif defined(OS_WIN)
90 #define ARG_LITERAL(x) L ## x
91 #endif
93 TEST_F(URLResolverTest, GetOriginMappings) {
94 base::CommandLine::StringVector args;
95 args.push_back(ARG_LITERAL("--map-origin=https://a.org=https://b.org/a"));
96 std::vector<URLResolver::OriginMapping> mappings =
97 URLResolver::GetOriginMappings(args);
98 ASSERT_EQ(1U, mappings.size());
99 EXPECT_EQ("https://a.org", mappings[0].origin);
100 EXPECT_EQ("https://b.org/a", mappings[0].base_url);
102 args.clear();
103 args.push_back(ARG_LITERAL("-map-origin=https://a.org=https://b.org/a"));
104 mappings = URLResolver::GetOriginMappings(args);
105 ASSERT_EQ(1U, mappings.size());
106 EXPECT_EQ("https://a.org", mappings[0].origin);
107 EXPECT_EQ("https://b.org/a", mappings[0].base_url);
109 args.clear();
110 args.push_back(ARG_LITERAL("--map-origin"));
111 mappings = URLResolver::GetOriginMappings(args);
112 EXPECT_EQ(0U, mappings.size());
114 args.clear();
115 args.push_back(ARG_LITERAL("--map-origin="));
116 mappings = URLResolver::GetOriginMappings(args);
117 EXPECT_EQ(0U, mappings.size());
119 args.clear();
120 args.push_back(ARG_LITERAL("mojo_shell"));
121 args.push_back(ARG_LITERAL("--map-origin=https://a.org=https://b.org/a"));
122 args.push_back(ARG_LITERAL("--map-origin=https://b.org=https://c.org/b"));
123 args.push_back(ARG_LITERAL("https://a.org/foo"));
124 mappings = URLResolver::GetOriginMappings(args);
125 ASSERT_EQ(2U, mappings.size());
126 EXPECT_EQ("https://a.org", mappings[0].origin);
127 EXPECT_EQ("https://b.org/a", mappings[0].base_url);
128 EXPECT_EQ("https://b.org", mappings[1].origin);
129 EXPECT_EQ("https://c.org/b", mappings[1].base_url);
132 TEST_F(URLResolverTest, TestQueryForURLMapping) {
133 URLResolver resolver;
134 resolver.SetMojoBaseURL(GURL("file:/base"));
135 resolver.AddURLMapping(GURL("https://a.org/foo"),
136 GURL("https://b.org/a/foo"));
137 resolver.AddURLMapping(GURL("https://b.org/a/foo"),
138 GURL("https://c.org/b/a/foo"));
139 GURL mapped_url = resolver.ApplyMappings(GURL("https://a.org/foo?a=b"));
140 EXPECT_EQ("https://c.org/b/a/foo?a=b", mapped_url.spec());
143 TEST_F(URLResolverTest, TestQueryForBaseURL) {
144 URLResolver resolver;
145 resolver.SetMojoBaseURL(GURL("file:///base"));
146 GURL mapped_url = resolver.ResolveMojoURL(GURL("mojo:foo?a=b"));
147 EXPECT_EQ("file:///base/foo.mojo?a=b", mapped_url.spec());
150 // Verifies that ResolveMojoURL prefers the directory with the name of the host
151 // over the raw file.
152 TEST_F(URLResolverTest, PreferDirectory) {
153 base::ScopedTempDir tmp_dir;
154 ASSERT_TRUE(tmp_dir.CreateUniqueTempDir());
155 URLResolver resolver;
156 // With no directory |mojo:foo| maps to path/foo.mojo.
157 resolver.SetMojoBaseURL(util::FilePathToFileURL(tmp_dir.path()));
158 const GURL mapped_url = resolver.ResolveMojoURL(GURL("mojo:foo"));
159 EXPECT_EQ(util::FilePathToFileURL(tmp_dir.path()).spec() + "/foo.mojo",
160 mapped_url.spec());
162 // With an empty directory |mojo:foo| maps to path/foo.mojo.
163 const base::FilePath foo_file_path(
164 tmp_dir.path().Append(FILE_PATH_LITERAL("foo")));
165 ASSERT_TRUE(base::CreateDirectory(foo_file_path));
166 const GURL mapped_url_with_dir = resolver.ResolveMojoURL(GURL("mojo:foo"));
167 EXPECT_EQ(util::FilePathToFileURL(tmp_dir.path()).spec() + "/foo.mojo",
168 mapped_url_with_dir.spec());
170 // When foo.mojo exists in the directory (path/foo/foo.mojo), then it should
171 // be picked up.
172 // With an empty directory |mojo:foo| maps to path/foo/foo.mojo.
173 ASSERT_EQ(1,
174 base::WriteFile(foo_file_path.Append(FILE_PATH_LITERAL("foo.mojo")),
175 "a", 1));
176 const GURL mapped_url_in_dir = resolver.ResolveMojoURL(GURL("mojo:foo"));
177 EXPECT_EQ(util::FilePathToFileURL(tmp_dir.path()).spec() + "/foo/foo.mojo",
178 mapped_url_in_dir.spec());
181 } // namespace
182 } // namespace test
183 } // namespace runner
184 } // namespace mojo