blob: b35db42e64a5ce1d05346c2bc7b84fc331c95257 [file] [log] [blame]
Aaron Boodmanb34bcbf2015-03-02 10:05:31 -08001// Copyright 2015 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.
4
5#include "base/base_paths.h"
6#include "base/bind.h"
7#include "base/files/file_path.h"
8#include "base/files/file_util.h"
9#include "base/path_service.h"
10#include "base/run_loop.h"
11#include "base/strings/string_util.h"
12#include "base/strings/stringprintf.h"
James Robinsonc4d0fb22016-01-28 14:31:21 -080013#include "mojo/converters/base/base_type_converters.h"
James Robinson94ade6b2015-08-25 13:02:06 -070014#include "mojo/data_pipe_utils/data_pipe_utils.h"
Aaron Boodmanb34bcbf2015-03-02 10:05:31 -080015#include "mojo/public/cpp/application/application_test_base.h"
Viet-Trung Luu199850d2015-11-16 15:20:58 -080016#include "mojo/public/cpp/application/connect.h"
Viet-Trung Luud63ebab2016-05-17 15:49:50 -070017#include "mojo/public/cpp/bindings/binding.h"
Viet-Trung Luufe25adb2016-04-28 10:33:45 -070018#include "mojo/public/cpp/bindings/synchronous_interface_ptr.h"
Aaron Boodmanb34bcbf2015-03-02 10:05:31 -080019#include "mojo/public/cpp/system/macros.h"
Viet-Trung Luu199850d2015-11-16 15:20:58 -080020#include "mojo/public/interfaces/application/application_connector.mojom.h"
Viet-Trung Luu84765c42015-10-10 01:07:51 -070021#include "mojo/services/http_server/cpp/http_server_util.h"
Viet-Trung Luufe25adb2016-04-28 10:33:45 -070022#include "mojo/services/http_server/interfaces/http_server.mojom-sync.h"
Viet-Trung Luu84765c42015-10-10 01:07:51 -070023#include "mojo/services/http_server/interfaces/http_server_factory.mojom.h"
Viet-Trung Luu2e11a3f2015-10-13 13:20:30 -070024#include "mojo/services/network/interfaces/net_address.mojom.h"
Benjamin Lermandc3d1932015-03-09 11:17:39 +010025#include "shell/kPingable.h"
Aaron Boodmanb34bcbf2015-03-02 10:05:31 -080026#include "shell/test/pingable.mojom.h"
Aaron Boodmanb34bcbf2015-03-02 10:05:31 -080027
Viet-Trung Luue377a9e2015-04-09 13:53:21 -070028using mojo::String;
29
Aaron Boodmanb34bcbf2015-03-02 10:05:31 -080030namespace {
31
32std::string GetURL(uint16_t port, const std::string& path) {
Viet-Trung Luu33afb152015-03-11 16:58:49 -070033 return base::StringPrintf("http://127.0.0.1:%u/%s",
34 static_cast<unsigned>(port), path.c_str());
Aaron Boodmanb34bcbf2015-03-02 10:05:31 -080035}
36
Aaron Boodmanb34bcbf2015-03-02 10:05:31 -080037class GetHandler : public http_server::HttpHandler {
38 public:
Viet-Trung Luubd07e3a2015-04-09 12:43:29 -070039 GetHandler(mojo::InterfaceRequest<http_server::HttpHandler> request,
40 uint16_t port)
41 : binding_(this, request.Pass()), port_(port) {}
Aaron Boodmanb34bcbf2015-03-02 10:05:31 -080042 ~GetHandler() override {}
43
44 private:
45 // http_server::HttpHandler:
Viet-Trung Luubd07e3a2015-04-09 12:43:29 -070046 void HandleRequest(http_server::HttpRequestPtr request,
47 const mojo::Callback<void(http_server::HttpResponsePtr)>&
48 callback) override {
Aaron Boodmanb34bcbf2015-03-02 10:05:31 -080049 http_server::HttpResponsePtr response;
James Robinsonc4d0fb22016-01-28 14:31:21 -080050 if (base::StartsWith(request->relative_url.To<base::StringPiece>(), "/app",
51 base::CompareCase::SENSITIVE)) {
Benjamin Lermandc3d1932015-03-09 11:17:39 +010052 response = http_server::CreateHttpResponse(
Viet-Trung Luubd07e3a2015-04-09 12:43:29 -070053 200, std::string(shell::test::kPingable.data,
54 shell::test::kPingable.size));
Aaron Boodmanb34bcbf2015-03-02 10:05:31 -080055 response->content_type = "application/octet-stream";
56 } else if (request->relative_url == "/redirect") {
57 response = http_server::HttpResponse::New();
58 response->status_code = 302;
59 response->custom_headers.insert("Location", GetURL(port_, "app"));
60 } else {
61 NOTREACHED();
62 }
63
64 callback.Run(response.Pass());
65 }
66
Viet-Trung Luubd07e3a2015-04-09 12:43:29 -070067 mojo::Binding<http_server::HttpHandler> binding_;
Aaron Boodmanb34bcbf2015-03-02 10:05:31 -080068 uint16_t port_;
69
70 MOJO_DISALLOW_COPY_AND_ASSIGN(GetHandler);
71};
72
Viet-Trung Luubd07e3a2015-04-09 12:43:29 -070073typedef mojo::test::ApplicationTestBase ShellAppTest;
Aaron Boodmanb34bcbf2015-03-02 10:05:31 -080074
Viet-Trung Luubd07e3a2015-04-09 12:43:29 -070075class ShellHTTPAppTest : public ShellAppTest {
Aaron Boodmanb34bcbf2015-03-02 10:05:31 -080076 public:
Viet-Trung Luubd07e3a2015-04-09 12:43:29 -070077 ShellHTTPAppTest() {}
Aaron Boodmanb34bcbf2015-03-02 10:05:31 -080078 ~ShellHTTPAppTest() override {}
79
80 protected:
Aaron Boodmanb34bcbf2015-03-02 10:05:31 -080081 void SetUp() override {
Viet-Trung Luubd07e3a2015-04-09 12:43:29 -070082 ShellAppTest::SetUp();
Aaron Boodmanb34bcbf2015-03-02 10:05:31 -080083
Viet-Trung Luud63ebab2016-05-17 15:49:50 -070084 mojo::ConnectToService(shell(), "mojo:http_server",
Viet-Trung Luuc1248b22016-04-26 13:41:55 -070085 GetProxy(&http_server_factory_));
Aaron Boodmanb34bcbf2015-03-02 10:05:31 -080086
Viet-Trung Luubd07e3a2015-04-09 12:43:29 -070087 mojo::NetAddressPtr local_address(mojo::NetAddress::New());
John Grossman87fe5142015-10-02 10:11:43 -070088 local_address->family = mojo::NetAddressFamily::IPV4;
Viet-Trung Luubd07e3a2015-04-09 12:43:29 -070089 local_address->ipv4 = mojo::NetAddressIPv4::New();
Aaron Boodmanb34bcbf2015-03-02 10:05:31 -080090 local_address->ipv4->addr.resize(4);
91 local_address->ipv4->addr[0] = 127;
92 local_address->ipv4->addr[1] = 0;
93 local_address->ipv4->addr[2] = 0;
94 local_address->ipv4->addr[3] = 1;
95 local_address->ipv4->port = 0;
Viet-Trung Luufe25adb2016-04-28 10:33:45 -070096 http_server_factory_->CreateHttpServer(GetSynchronousProxy(&http_server_),
Aaron Boodmanb34bcbf2015-03-02 10:05:31 -080097 local_address.Pass());
98
Viet-Trung Luufe25adb2016-04-28 10:33:45 -070099 EXPECT_TRUE(http_server_->GetPort(&port_));
Aaron Boodmanb34bcbf2015-03-02 10:05:31 -0800100
Viet-Trung Luubd07e3a2015-04-09 12:43:29 -0700101 http_server::HttpHandlerPtr http_handler;
Viet-Trung Luu71447c82015-11-16 16:59:09 -0800102 handler_.reset(new GetHandler(GetProxy(&http_handler).Pass(), port_));
Viet-Trung Luufe25adb2016-04-28 10:33:45 -0700103 bool result = false;
104 EXPECT_TRUE(http_server_->SetHandler(".*", http_handler.Pass(), &result));
105 EXPECT_TRUE(result);
Aaron Boodmanb34bcbf2015-03-02 10:05:31 -0800106 }
107
Viet-Trung Luubd07e3a2015-04-09 12:43:29 -0700108 std::string GetURL(const std::string& path) { return ::GetURL(port_, path); }
Aaron Boodmanb34bcbf2015-03-02 10:05:31 -0800109
110 http_server::HttpServerFactoryPtr http_server_factory_;
Viet-Trung Luufe25adb2016-04-28 10:33:45 -0700111 mojo::SynchronousInterfacePtr<http_server::HttpServer> http_server_;
Aaron Boodmanb34bcbf2015-03-02 10:05:31 -0800112 scoped_ptr<GetHandler> handler_;
113 uint16_t port_;
114
115 private:
116 MOJO_DISALLOW_COPY_AND_ASSIGN(ShellHTTPAppTest);
117};
118
Aaron Boodmanb34bcbf2015-03-02 10:05:31 -0800119// Test that we can load apps over http.
120TEST_F(ShellHTTPAppTest, Http) {
Viet-Trung Luubd07e3a2015-04-09 12:43:29 -0700121 PingablePtr pingable;
Viet-Trung Luud63ebab2016-05-17 15:49:50 -0700122 mojo::ConnectToService(shell(), GetURL("app"), GetProxy(&pingable));
Viet-Trung Luue377a9e2015-04-09 13:53:21 -0700123 pingable->Ping("hello",
124 [this](const String& app_url, const String& connection_url,
125 const String& message) {
126 EXPECT_EQ(GetURL("app"), app_url);
127 EXPECT_EQ(GetURL("app"), connection_url);
128 EXPECT_EQ("hello", message);
129 base::MessageLoop::current()->Quit();
130 });
Aaron Boodmanb34bcbf2015-03-02 10:05:31 -0800131 base::RunLoop().Run();
132}
133
134// Test that redirects work.
135// TODO(aa): Test that apps receive the correct URL parameters.
136TEST_F(ShellHTTPAppTest, Redirect) {
Viet-Trung Luubd07e3a2015-04-09 12:43:29 -0700137 PingablePtr pingable;
Viet-Trung Luud63ebab2016-05-17 15:49:50 -0700138 mojo::ConnectToService(shell(), GetURL("redirect"), GetProxy(&pingable));
Viet-Trung Luue377a9e2015-04-09 13:53:21 -0700139 pingable->Ping("hello",
140 [this](const String& app_url, const String& connection_url,
141 const String& message) {
142 EXPECT_EQ(GetURL("app"), app_url);
143 EXPECT_EQ(GetURL("app"), connection_url);
144 EXPECT_EQ("hello", message);
145 base::MessageLoop::current()->Quit();
146 });
Aaron Boodmanb34bcbf2015-03-02 10:05:31 -0800147 base::RunLoop().Run();
148}
149
150// Test that querystring is not considered when resolving http applications.
Michael Wasserman2d4bcfc2015-03-03 14:20:51 -0800151// TODO(aa|qsr): Fix this test on Linux ASAN http://crbug.com/463662
152#if defined(ADDRESS_SANITIZER)
153#define MAYBE_QueryHandling DISABLED_QueryHandling
154#else
155#define MAYBE_QueryHandling QueryHandling
156#endif // ADDRESS_SANITIZER
157TEST_F(ShellHTTPAppTest, MAYBE_QueryHandling) {
Viet-Trung Luubd07e3a2015-04-09 12:43:29 -0700158 PingablePtr pingable1;
159 PingablePtr pingable2;
Viet-Trung Luud63ebab2016-05-17 15:49:50 -0700160 mojo::ConnectToService(shell(), GetURL("app?foo"), GetProxy(&pingable1));
161 mojo::ConnectToService(shell(), GetURL("app?bar"), GetProxy(&pingable2));
Aaron Boodmanb34bcbf2015-03-02 10:05:31 -0800162
163 int num_responses = 0;
Benjamin Lerman1257b852015-09-24 09:52:12 +0200164 auto callbacks_builder = [this, &num_responses](int query_index) {
165 return [this, &num_responses, query_index](const String& app_url,
166 const String& connection_url,
167 const String& message) {
168 EXPECT_EQ(GetURL("app"), app_url);
169 EXPECT_EQ("hello", message);
170 if (query_index == 1) {
171 EXPECT_EQ(GetURL("app?foo"), connection_url);
172 } else if (query_index == 2) {
173 EXPECT_EQ(GetURL("app?bar"), connection_url);
174 } else {
175 CHECK(false);
176 }
177 ++num_responses;
178 if (num_responses == 2)
179 base::MessageLoop::current()->Quit();
180 };
Aaron Boodmanb34bcbf2015-03-02 10:05:31 -0800181 };
Benjamin Lerman1257b852015-09-24 09:52:12 +0200182 pingable1->Ping("hello", callbacks_builder(1));
183 pingable2->Ping("hello", callbacks_builder(2));
Aaron Boodmanb34bcbf2015-03-02 10:05:31 -0800184 base::RunLoop().Run();
185}
Aaron Boodmanb34bcbf2015-03-02 10:05:31 -0800186
187// mojo: URLs can have querystrings too
188TEST_F(ShellAppTest, MojoURLQueryHandling) {
Viet-Trung Luubd07e3a2015-04-09 12:43:29 -0700189 PingablePtr pingable;
Viet-Trung Luud63ebab2016-05-17 15:49:50 -0700190 mojo::ConnectToService(shell(), "mojo:pingable_app?foo", GetProxy(&pingable));
Viet-Trung Luu71447c82015-11-16 16:59:09 -0800191 auto callback = [](const String& app_url, const String& connection_url,
192 const String& message) {
James Robinsonc4d0fb22016-01-28 14:31:21 -0800193 EXPECT_TRUE(base::EndsWith(app_url.To<base::StringPiece>(),
194 "/pingable_app.mojo",
195 base::CompareCase::SENSITIVE));
Viet-Trung Luue377a9e2015-04-09 13:53:21 -0700196 EXPECT_EQ(app_url.To<std::string>() + "?foo", connection_url);
197 EXPECT_EQ("hello", message);
198 base::MessageLoop::current()->Quit();
199 };
Aaron Boodmanb34bcbf2015-03-02 10:05:31 -0800200 pingable->Ping("hello", callback);
201 base::RunLoop().Run();
202}
203
Viet-Trung Luu71447c82015-11-16 16:59:09 -0800204void TestApplicationConnector(mojo::ApplicationConnector* app_connector) {
205 PingablePtr pingable;
Viet-Trung Luuc5cfd312016-04-25 16:57:51 -0700206 ConnectToService(app_connector, "mojo:pingable_app", GetProxy(&pingable));
Viet-Trung Luu71447c82015-11-16 16:59:09 -0800207 auto callback = [](const String& app_url, const String& connection_url,
208 const String& message) {
James Robinsonc4d0fb22016-01-28 14:31:21 -0800209 EXPECT_TRUE(base::EndsWith(app_url.To<base::StringPiece>(),
210 "/pingable_app.mojo",
211 base::CompareCase::SENSITIVE));
Viet-Trung Luu71447c82015-11-16 16:59:09 -0800212 EXPECT_EQ(app_url, connection_url);
213 EXPECT_EQ("hello", message);
214 base::MessageLoop::current()->Quit();
215 };
216 pingable->Ping("hello", callback);
217 base::RunLoop().Run();
218}
219
Viet-Trung Luu199850d2015-11-16 15:20:58 -0800220TEST_F(ShellAppTest, ApplicationConnector) {
221 mojo::ApplicationConnectorPtr app_connector;
Viet-Trung Luud63ebab2016-05-17 15:49:50 -0700222 app_connector.Bind(mojo::CreateApplicationConnector(shell()));
Viet-Trung Luu71447c82015-11-16 16:59:09 -0800223 TestApplicationConnector(app_connector.get());
224}
Viet-Trung Luu199850d2015-11-16 15:20:58 -0800225
Viet-Trung Luu71447c82015-11-16 16:59:09 -0800226TEST_F(ShellAppTest, ApplicationConnectorDuplicate) {
227 mojo::ApplicationConnectorPtr app_connector1;
Viet-Trung Luud63ebab2016-05-17 15:49:50 -0700228 app_connector1.Bind(mojo::CreateApplicationConnector(shell()));
Viet-Trung Luu71447c82015-11-16 16:59:09 -0800229 {
230 SCOPED_TRACE("app_connector1");
231 TestApplicationConnector(app_connector1.get());
232 }
233
234 mojo::ApplicationConnectorPtr app_connector2;
235 app_connector1->Duplicate(GetProxy(&app_connector2));
236 {
237 SCOPED_TRACE("app_connector2");
238 TestApplicationConnector(app_connector2.get());
239 }
240
241 // The first one should still work.
242 {
243 SCOPED_TRACE("app_connector1 again");
244 TestApplicationConnector(app_connector1.get());
245 }
Viet-Trung Luu199850d2015-11-16 15:20:58 -0800246}
247
Viet-Trung Luu33afb152015-03-11 16:58:49 -0700248} // namespace