Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / mojo / shell / url_resolver_unittest.cc
blobdd8da75541195fa87190198f4501018ceb3844d4
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/shell/url_resolver.h"
7 #include "base/logging.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 namespace mojo {
11 namespace shell {
12 namespace test {
13 namespace {
15 typedef testing::Test URLResolverTest;
17 TEST_F(URLResolverTest, MojoURLsFallThrough) {
18 URLResolver resolver;
19 resolver.AddURLMapping(GURL("mojo:test"), GURL("mojo:foo"));
20 const GURL base_url("file:/base");
21 resolver.SetMojoBaseURL(base_url);
22 GURL mapped_url = resolver.ApplyMappings(GURL("mojo:test"));
23 std::string resolved(resolver.ResolveMojoURL(mapped_url).spec());
24 // Resolved must start with |base_url|.
25 EXPECT_EQ(base_url.spec(), resolved.substr(0, base_url.spec().size()));
26 // And must contain foo.
27 EXPECT_NE(std::string::npos, resolved.find("foo"));
30 TEST_F(URLResolverTest, MapURL) {
31 URLResolver resolver;
32 resolver.AddURLMapping(GURL("https://domokit.org/test.mojo"),
33 GURL("file:///mojo/src/out/Debug/test.mojo"));
34 GURL mapped_url =
35 resolver.ApplyMappings(GURL("https://domokit.org/test.mojo"));
36 EXPECT_EQ("file:///mojo/src/out/Debug/test.mojo", mapped_url.spec());
39 TEST_F(URLResolverTest, MultipleMapURL) {
40 URLResolver resolver;
41 resolver.AddURLMapping(GURL("https://a.org/foo"),
42 GURL("https://b.org/a/foo"));
43 resolver.AddURLMapping(GURL("https://b.org/a/foo"),
44 GURL("https://c.org/b/a/foo"));
45 GURL mapped_url = resolver.ApplyMappings(GURL("https://a.org/foo"));
46 EXPECT_EQ("https://c.org/b/a/foo", mapped_url.spec());
49 TEST_F(URLResolverTest, MapOrigin) {
50 URLResolver resolver;
51 resolver.AddOriginMapping(GURL("https://domokit.org"),
52 GURL("file:///mojo/src/out/Debug"));
53 GURL mapped_url =
54 resolver.ApplyMappings(GURL("https://domokit.org/test.mojo"));
55 EXPECT_EQ("file:///mojo/src/out/Debug/test.mojo", mapped_url.spec());
58 TEST_F(URLResolverTest, MultipleMapOrigin) {
59 URLResolver resolver;
60 resolver.AddOriginMapping(GURL("https://a.org"), GURL("https://b.org/a"));
61 resolver.AddOriginMapping(GURL("https://b.org"), GURL("https://c.org/b"));
62 GURL mapped_url = resolver.ApplyMappings(GURL("https://a.org/foo"));
63 EXPECT_EQ("https://c.org/b/a/foo", mapped_url.spec());
66 TEST_F(URLResolverTest, MapOriginThenURL) {
67 URLResolver resolver;
68 resolver.AddOriginMapping(GURL("https://a.org"), GURL("https://b.org/a"));
69 resolver.AddURLMapping(GURL("https://b.org/a/foo"),
70 GURL("https://c.org/b/a/foo"));
71 GURL mapped_url = resolver.ApplyMappings(GURL("https://a.org/foo"));
72 EXPECT_EQ("https://c.org/b/a/foo", mapped_url.spec());
75 TEST_F(URLResolverTest, MapURLThenOrigin) {
76 URLResolver resolver;
77 resolver.AddURLMapping(GURL("https://a.org/foo"),
78 GURL("https://b.org/a/foo"));
79 resolver.AddOriginMapping(GURL("https://b.org"), GURL("https://c.org/b"));
80 GURL mapped_url = resolver.ApplyMappings(GURL("https://a.org/foo"));
81 EXPECT_EQ("https://c.org/b/a/foo", mapped_url.spec());
84 #if defined(OS_POSIX)
85 #define ARG_LITERAL(x) x
86 #elif defined(OS_WIN)
87 #define ARG_LITERAL(x) L ## x
88 #endif
90 TEST_F(URLResolverTest, GetOriginMappings) {
91 base::CommandLine::StringVector args;
92 args.push_back(ARG_LITERAL("--map-origin=https://a.org=https://b.org/a"));
93 std::vector<URLResolver::OriginMapping> mappings =
94 URLResolver::GetOriginMappings(args);
95 ASSERT_EQ(1U, mappings.size());
96 EXPECT_EQ("https://a.org", mappings[0].origin);
97 EXPECT_EQ("https://b.org/a", mappings[0].base_url);
99 args.clear();
100 args.push_back(ARG_LITERAL("-map-origin=https://a.org=https://b.org/a"));
101 mappings = URLResolver::GetOriginMappings(args);
102 ASSERT_EQ(1U, mappings.size());
103 EXPECT_EQ("https://a.org", mappings[0].origin);
104 EXPECT_EQ("https://b.org/a", mappings[0].base_url);
106 args.clear();
107 args.push_back(ARG_LITERAL("--map-origin"));
108 mappings = URLResolver::GetOriginMappings(args);
109 EXPECT_EQ(0U, mappings.size());
111 args.clear();
112 args.push_back(ARG_LITERAL("--map-origin="));
113 mappings = URLResolver::GetOriginMappings(args);
114 EXPECT_EQ(0U, mappings.size());
116 args.clear();
117 args.push_back(ARG_LITERAL("mojo_shell"));
118 args.push_back(ARG_LITERAL("--map-origin=https://a.org=https://b.org/a"));
119 args.push_back(ARG_LITERAL("--map-origin=https://b.org=https://c.org/b"));
120 args.push_back(ARG_LITERAL("https://a.org/foo"));
121 mappings = URLResolver::GetOriginMappings(args);
122 ASSERT_EQ(2U, mappings.size());
123 EXPECT_EQ("https://a.org", mappings[0].origin);
124 EXPECT_EQ("https://b.org/a", mappings[0].base_url);
125 EXPECT_EQ("https://b.org", mappings[1].origin);
126 EXPECT_EQ("https://c.org/b", mappings[1].base_url);
129 TEST_F(URLResolverTest, TestQueryForURLMapping) {
130 URLResolver resolver;
131 resolver.SetMojoBaseURL(GURL("file:/base"));
132 resolver.AddURLMapping(GURL("https://a.org/foo"),
133 GURL("https://b.org/a/foo"));
134 resolver.AddURLMapping(GURL("https://b.org/a/foo"),
135 GURL("https://c.org/b/a/foo"));
136 GURL mapped_url = resolver.ApplyMappings(GURL("https://a.org/foo?a=b"));
137 EXPECT_EQ("https://c.org/b/a/foo?a=b", mapped_url.spec());
140 TEST_F(URLResolverTest, TestQueryForBaseURL) {
141 URLResolver resolver;
142 resolver.SetMojoBaseURL(GURL("file:///base"));
143 GURL mapped_url = resolver.ResolveMojoURL(GURL("mojo:foo?a=b"));
144 EXPECT_EQ("file:///base/foo.mojo?a=b", mapped_url.spec());
147 } // namespace
148 } // namespace test
149 } // namespace shell
150 } // namespace mojo