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"
18 typedef testing::Test URLResolverTest
;
20 TEST_F(URLResolverTest
, MojoURLsFallThrough
) {
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
) {
35 resolver
.AddURLMapping(GURL("https://domokit.org/test.mojo"),
36 GURL("file:///mojo/src/out/Debug/test.mojo"));
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
) {
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
) {
54 resolver
.AddOriginMapping(GURL("https://domokit.org"),
55 GURL("file:///mojo/src/out/Debug"));
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
) {
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
) {
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
) {
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());
88 #define ARG_LITERAL(x) x
90 #define ARG_LITERAL(x) L ## x
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
);
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
);
110 args
.push_back(ARG_LITERAL("--map-origin"));
111 mappings
= URLResolver::GetOriginMappings(args
);
112 EXPECT_EQ(0U, mappings
.size());
115 args
.push_back(ARG_LITERAL("--map-origin="));
116 mappings
= URLResolver::GetOriginMappings(args
);
117 EXPECT_EQ(0U, mappings
.size());
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
, TestQueryForBaseFileURL
) {
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/foo.mojo?a=b", mapped_url
.spec());
150 TEST_F(URLResolverTest
, TestQueryForBaseHttpURL
) {
151 URLResolver resolver
;
152 resolver
.SetMojoBaseURL(GURL("http://127.0.0.1:1234"));
153 GURL mapped_url
= resolver
.ResolveMojoURL(GURL("mojo:foo?a=b"));
154 EXPECT_EQ("http://127.0.0.1:1234/foo/foo.mojo?a=b", mapped_url
.spec());
159 } // namespace runner