summaryrefslogtreecommitdiff
path: root/chromium/mojo/runner/url_resolver_unittest.cc
blob: 9ecc821fb87bf630fcadaed4219870072a59331d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "mojo/runner/url_resolver.h"

#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/logging.h"
#include "mojo/util/filename_util.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace mojo {
namespace runner {
namespace test {
namespace {

typedef testing::Test URLResolverTest;

TEST_F(URLResolverTest, MojoURLsFallThrough) {
  URLResolver resolver;
  resolver.AddURLMapping(GURL("mojo:test"), GURL("mojo:foo"));
  const GURL base_url("file:/base");
  resolver.SetMojoBaseURL(base_url);
  GURL mapped_url = resolver.ApplyMappings(GURL("mojo:test"));
  std::string resolved(resolver.ResolveMojoURL(mapped_url).spec());
  // Resolved must start with |base_url|.
  EXPECT_EQ(base_url.spec(), resolved.substr(0, base_url.spec().size()));
  // And must contain foo.
  EXPECT_NE(std::string::npos, resolved.find("foo"));
}

TEST_F(URLResolverTest, MapURL) {
  URLResolver resolver;
  resolver.AddURLMapping(GURL("https://domokit.org/test.mojo"),
                         GURL("file:///mojo/src/out/Debug/test.mojo"));
  GURL mapped_url =
      resolver.ApplyMappings(GURL("https://domokit.org/test.mojo"));
  EXPECT_EQ("file:///mojo/src/out/Debug/test.mojo", mapped_url.spec());
}

TEST_F(URLResolverTest, MultipleMapURL) {
  URLResolver resolver;
  resolver.AddURLMapping(GURL("https://a.org/foo"),
                         GURL("https://b.org/a/foo"));
  resolver.AddURLMapping(GURL("https://b.org/a/foo"),
                         GURL("https://c.org/b/a/foo"));
  GURL mapped_url = resolver.ApplyMappings(GURL("https://a.org/foo"));
  EXPECT_EQ("https://c.org/b/a/foo", mapped_url.spec());
}

TEST_F(URLResolverTest, MapOrigin) {
  URLResolver resolver;
  resolver.AddOriginMapping(GURL("https://domokit.org"),
                            GURL("file:///mojo/src/out/Debug"));
  GURL mapped_url =
      resolver.ApplyMappings(GURL("https://domokit.org/test.mojo"));
  EXPECT_EQ("file:///mojo/src/out/Debug/test.mojo", mapped_url.spec());
}

TEST_F(URLResolverTest, MultipleMapOrigin) {
  URLResolver resolver;
  resolver.AddOriginMapping(GURL("https://a.org"), GURL("https://b.org/a"));
  resolver.AddOriginMapping(GURL("https://b.org"), GURL("https://c.org/b"));
  GURL mapped_url = resolver.ApplyMappings(GURL("https://a.org/foo"));
  EXPECT_EQ("https://c.org/b/a/foo", mapped_url.spec());
}

TEST_F(URLResolverTest, MapOriginThenURL) {
  URLResolver resolver;
  resolver.AddOriginMapping(GURL("https://a.org"), GURL("https://b.org/a"));
  resolver.AddURLMapping(GURL("https://b.org/a/foo"),
                         GURL("https://c.org/b/a/foo"));
  GURL mapped_url = resolver.ApplyMappings(GURL("https://a.org/foo"));
  EXPECT_EQ("https://c.org/b/a/foo", mapped_url.spec());
}

TEST_F(URLResolverTest, MapURLThenOrigin) {
  URLResolver resolver;
  resolver.AddURLMapping(GURL("https://a.org/foo"),
                         GURL("https://b.org/a/foo"));
  resolver.AddOriginMapping(GURL("https://b.org"), GURL("https://c.org/b"));
  GURL mapped_url = resolver.ApplyMappings(GURL("https://a.org/foo"));
  EXPECT_EQ("https://c.org/b/a/foo", mapped_url.spec());
}

#if defined(OS_POSIX)
#define ARG_LITERAL(x) x
#elif defined(OS_WIN)
#define ARG_LITERAL(x) L ## x
#endif

TEST_F(URLResolverTest, GetOriginMappings) {
  base::CommandLine::StringVector args;
  args.push_back(ARG_LITERAL("--map-origin=https://a.org=https://b.org/a"));
  std::vector<URLResolver::OriginMapping> mappings =
      URLResolver::GetOriginMappings(args);
  ASSERT_EQ(1U, mappings.size());
  EXPECT_EQ("https://a.org", mappings[0].origin);
  EXPECT_EQ("https://b.org/a", mappings[0].base_url);

  args.clear();
  args.push_back(ARG_LITERAL("-map-origin=https://a.org=https://b.org/a"));
  mappings = URLResolver::GetOriginMappings(args);
  ASSERT_EQ(1U, mappings.size());
  EXPECT_EQ("https://a.org", mappings[0].origin);
  EXPECT_EQ("https://b.org/a", mappings[0].base_url);

  args.clear();
  args.push_back(ARG_LITERAL("--map-origin"));
  mappings = URLResolver::GetOriginMappings(args);
  EXPECT_EQ(0U, mappings.size());

  args.clear();
  args.push_back(ARG_LITERAL("--map-origin="));
  mappings = URLResolver::GetOriginMappings(args);
  EXPECT_EQ(0U, mappings.size());

  args.clear();
  args.push_back(ARG_LITERAL("mojo_shell"));
  args.push_back(ARG_LITERAL("--map-origin=https://a.org=https://b.org/a"));
  args.push_back(ARG_LITERAL("--map-origin=https://b.org=https://c.org/b"));
  args.push_back(ARG_LITERAL("https://a.org/foo"));
  mappings = URLResolver::GetOriginMappings(args);
  ASSERT_EQ(2U, mappings.size());
  EXPECT_EQ("https://a.org", mappings[0].origin);
  EXPECT_EQ("https://b.org/a", mappings[0].base_url);
  EXPECT_EQ("https://b.org", mappings[1].origin);
  EXPECT_EQ("https://c.org/b", mappings[1].base_url);
}

TEST_F(URLResolverTest, TestQueryForURLMapping) {
  URLResolver resolver;
  resolver.SetMojoBaseURL(GURL("file:/base"));
  resolver.AddURLMapping(GURL("https://a.org/foo"),
                         GURL("https://b.org/a/foo"));
  resolver.AddURLMapping(GURL("https://b.org/a/foo"),
                         GURL("https://c.org/b/a/foo"));
  GURL mapped_url = resolver.ApplyMappings(GURL("https://a.org/foo?a=b"));
  EXPECT_EQ("https://c.org/b/a/foo?a=b", mapped_url.spec());
}

TEST_F(URLResolverTest, TestQueryForBaseURL) {
  URLResolver resolver;
  resolver.SetMojoBaseURL(GURL("file:///base"));
  GURL mapped_url = resolver.ResolveMojoURL(GURL("mojo:foo?a=b"));
  EXPECT_EQ("file:///base/foo.mojo?a=b", mapped_url.spec());
}

// Verifies that ResolveMojoURL prefers the directory with the name of the host
// over the raw file.
TEST_F(URLResolverTest, PreferDirectory) {
  base::ScopedTempDir tmp_dir;
  ASSERT_TRUE(tmp_dir.CreateUniqueTempDir());
  URLResolver resolver;
  // With no directory |mojo:foo| maps to path/foo.mojo.
  resolver.SetMojoBaseURL(util::FilePathToFileURL(tmp_dir.path()));
  const GURL mapped_url = resolver.ResolveMojoURL(GURL("mojo:foo"));
  EXPECT_EQ(util::FilePathToFileURL(tmp_dir.path()).spec() + "/foo.mojo",
            mapped_url.spec());

  // With a directory |mojo:foo| maps to path/foo/foo.mojo.
  const base::FilePath foo_file_path(
      tmp_dir.path().Append(FILE_PATH_LITERAL("foo")));
  ASSERT_TRUE(base::CreateDirectory(foo_file_path));
  const GURL mapped_url_with_dir = resolver.ResolveMojoURL(GURL("mojo:foo"));
  EXPECT_EQ(util::FilePathToFileURL(tmp_dir.path()).spec() + "/foo/foo.mojo",
            mapped_url_with_dir.spec());
}

}  // namespace
}  // namespace test
}  // namespace runner
}  // namespace mojo