Aaron Boodman | b34bcbf | 2015-03-02 10:05:31 -0800 | [diff] [blame] | 1 | // 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 Robinson | c4d0fb2 | 2016-01-28 14:31:21 -0800 | [diff] [blame] | 13 | #include "mojo/converters/base/base_type_converters.h" |
James Robinson | 94ade6b | 2015-08-25 13:02:06 -0700 | [diff] [blame] | 14 | #include "mojo/data_pipe_utils/data_pipe_utils.h" |
Aaron Boodman | b34bcbf | 2015-03-02 10:05:31 -0800 | [diff] [blame] | 15 | #include "mojo/public/cpp/application/application_test_base.h" |
Viet-Trung Luu | 199850d | 2015-11-16 15:20:58 -0800 | [diff] [blame] | 16 | #include "mojo/public/cpp/application/connect.h" |
Viet-Trung Luu | d63ebab | 2016-05-17 15:49:50 -0700 | [diff] [blame] | 17 | #include "mojo/public/cpp/bindings/binding.h" |
Viet-Trung Luu | fe25adb | 2016-04-28 10:33:45 -0700 | [diff] [blame] | 18 | #include "mojo/public/cpp/bindings/synchronous_interface_ptr.h" |
Aaron Boodman | b34bcbf | 2015-03-02 10:05:31 -0800 | [diff] [blame] | 19 | #include "mojo/public/cpp/system/macros.h" |
Viet-Trung Luu | 199850d | 2015-11-16 15:20:58 -0800 | [diff] [blame] | 20 | #include "mojo/public/interfaces/application/application_connector.mojom.h" |
Viet-Trung Luu | 84765c4 | 2015-10-10 01:07:51 -0700 | [diff] [blame] | 21 | #include "mojo/services/http_server/cpp/http_server_util.h" |
Viet-Trung Luu | fe25adb | 2016-04-28 10:33:45 -0700 | [diff] [blame] | 22 | #include "mojo/services/http_server/interfaces/http_server.mojom-sync.h" |
Viet-Trung Luu | 84765c4 | 2015-10-10 01:07:51 -0700 | [diff] [blame] | 23 | #include "mojo/services/http_server/interfaces/http_server_factory.mojom.h" |
Viet-Trung Luu | 2e11a3f | 2015-10-13 13:20:30 -0700 | [diff] [blame] | 24 | #include "mojo/services/network/interfaces/net_address.mojom.h" |
Benjamin Lerman | dc3d193 | 2015-03-09 11:17:39 +0100 | [diff] [blame] | 25 | #include "shell/kPingable.h" |
Aaron Boodman | b34bcbf | 2015-03-02 10:05:31 -0800 | [diff] [blame] | 26 | #include "shell/test/pingable.mojom.h" |
Aaron Boodman | b34bcbf | 2015-03-02 10:05:31 -0800 | [diff] [blame] | 27 | |
Viet-Trung Luu | e377a9e | 2015-04-09 13:53:21 -0700 | [diff] [blame] | 28 | using mojo::String; |
| 29 | |
Aaron Boodman | b34bcbf | 2015-03-02 10:05:31 -0800 | [diff] [blame] | 30 | namespace { |
| 31 | |
| 32 | std::string GetURL(uint16_t port, const std::string& path) { |
Viet-Trung Luu | 33afb15 | 2015-03-11 16:58:49 -0700 | [diff] [blame] | 33 | return base::StringPrintf("http://127.0.0.1:%u/%s", |
| 34 | static_cast<unsigned>(port), path.c_str()); |
Aaron Boodman | b34bcbf | 2015-03-02 10:05:31 -0800 | [diff] [blame] | 35 | } |
| 36 | |
Aaron Boodman | b34bcbf | 2015-03-02 10:05:31 -0800 | [diff] [blame] | 37 | class GetHandler : public http_server::HttpHandler { |
| 38 | public: |
Viet-Trung Luu | bd07e3a | 2015-04-09 12:43:29 -0700 | [diff] [blame] | 39 | GetHandler(mojo::InterfaceRequest<http_server::HttpHandler> request, |
| 40 | uint16_t port) |
| 41 | : binding_(this, request.Pass()), port_(port) {} |
Aaron Boodman | b34bcbf | 2015-03-02 10:05:31 -0800 | [diff] [blame] | 42 | ~GetHandler() override {} |
| 43 | |
| 44 | private: |
| 45 | // http_server::HttpHandler: |
Viet-Trung Luu | bd07e3a | 2015-04-09 12:43:29 -0700 | [diff] [blame] | 46 | void HandleRequest(http_server::HttpRequestPtr request, |
| 47 | const mojo::Callback<void(http_server::HttpResponsePtr)>& |
| 48 | callback) override { |
Aaron Boodman | b34bcbf | 2015-03-02 10:05:31 -0800 | [diff] [blame] | 49 | http_server::HttpResponsePtr response; |
James Robinson | c4d0fb2 | 2016-01-28 14:31:21 -0800 | [diff] [blame] | 50 | if (base::StartsWith(request->relative_url.To<base::StringPiece>(), "/app", |
| 51 | base::CompareCase::SENSITIVE)) { |
Benjamin Lerman | dc3d193 | 2015-03-09 11:17:39 +0100 | [diff] [blame] | 52 | response = http_server::CreateHttpResponse( |
Viet-Trung Luu | bd07e3a | 2015-04-09 12:43:29 -0700 | [diff] [blame] | 53 | 200, std::string(shell::test::kPingable.data, |
| 54 | shell::test::kPingable.size)); |
Aaron Boodman | b34bcbf | 2015-03-02 10:05:31 -0800 | [diff] [blame] | 55 | 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 Luu | bd07e3a | 2015-04-09 12:43:29 -0700 | [diff] [blame] | 67 | mojo::Binding<http_server::HttpHandler> binding_; |
Aaron Boodman | b34bcbf | 2015-03-02 10:05:31 -0800 | [diff] [blame] | 68 | uint16_t port_; |
| 69 | |
| 70 | MOJO_DISALLOW_COPY_AND_ASSIGN(GetHandler); |
| 71 | }; |
| 72 | |
Viet-Trung Luu | bd07e3a | 2015-04-09 12:43:29 -0700 | [diff] [blame] | 73 | typedef mojo::test::ApplicationTestBase ShellAppTest; |
Aaron Boodman | b34bcbf | 2015-03-02 10:05:31 -0800 | [diff] [blame] | 74 | |
Viet-Trung Luu | bd07e3a | 2015-04-09 12:43:29 -0700 | [diff] [blame] | 75 | class ShellHTTPAppTest : public ShellAppTest { |
Aaron Boodman | b34bcbf | 2015-03-02 10:05:31 -0800 | [diff] [blame] | 76 | public: |
Viet-Trung Luu | bd07e3a | 2015-04-09 12:43:29 -0700 | [diff] [blame] | 77 | ShellHTTPAppTest() {} |
Aaron Boodman | b34bcbf | 2015-03-02 10:05:31 -0800 | [diff] [blame] | 78 | ~ShellHTTPAppTest() override {} |
| 79 | |
| 80 | protected: |
Aaron Boodman | b34bcbf | 2015-03-02 10:05:31 -0800 | [diff] [blame] | 81 | void SetUp() override { |
Viet-Trung Luu | bd07e3a | 2015-04-09 12:43:29 -0700 | [diff] [blame] | 82 | ShellAppTest::SetUp(); |
Aaron Boodman | b34bcbf | 2015-03-02 10:05:31 -0800 | [diff] [blame] | 83 | |
Viet-Trung Luu | d63ebab | 2016-05-17 15:49:50 -0700 | [diff] [blame] | 84 | mojo::ConnectToService(shell(), "mojo:http_server", |
Viet-Trung Luu | c1248b2 | 2016-04-26 13:41:55 -0700 | [diff] [blame] | 85 | GetProxy(&http_server_factory_)); |
Aaron Boodman | b34bcbf | 2015-03-02 10:05:31 -0800 | [diff] [blame] | 86 | |
Viet-Trung Luu | bd07e3a | 2015-04-09 12:43:29 -0700 | [diff] [blame] | 87 | mojo::NetAddressPtr local_address(mojo::NetAddress::New()); |
John Grossman | 87fe514 | 2015-10-02 10:11:43 -0700 | [diff] [blame] | 88 | local_address->family = mojo::NetAddressFamily::IPV4; |
Viet-Trung Luu | bd07e3a | 2015-04-09 12:43:29 -0700 | [diff] [blame] | 89 | local_address->ipv4 = mojo::NetAddressIPv4::New(); |
Aaron Boodman | b34bcbf | 2015-03-02 10:05:31 -0800 | [diff] [blame] | 90 | 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 Luu | fe25adb | 2016-04-28 10:33:45 -0700 | [diff] [blame] | 96 | http_server_factory_->CreateHttpServer(GetSynchronousProxy(&http_server_), |
Aaron Boodman | b34bcbf | 2015-03-02 10:05:31 -0800 | [diff] [blame] | 97 | local_address.Pass()); |
| 98 | |
Viet-Trung Luu | fe25adb | 2016-04-28 10:33:45 -0700 | [diff] [blame] | 99 | EXPECT_TRUE(http_server_->GetPort(&port_)); |
Aaron Boodman | b34bcbf | 2015-03-02 10:05:31 -0800 | [diff] [blame] | 100 | |
Viet-Trung Luu | bd07e3a | 2015-04-09 12:43:29 -0700 | [diff] [blame] | 101 | http_server::HttpHandlerPtr http_handler; |
Viet-Trung Luu | 71447c8 | 2015-11-16 16:59:09 -0800 | [diff] [blame] | 102 | handler_.reset(new GetHandler(GetProxy(&http_handler).Pass(), port_)); |
Viet-Trung Luu | fe25adb | 2016-04-28 10:33:45 -0700 | [diff] [blame] | 103 | bool result = false; |
| 104 | EXPECT_TRUE(http_server_->SetHandler(".*", http_handler.Pass(), &result)); |
| 105 | EXPECT_TRUE(result); |
Aaron Boodman | b34bcbf | 2015-03-02 10:05:31 -0800 | [diff] [blame] | 106 | } |
| 107 | |
Viet-Trung Luu | bd07e3a | 2015-04-09 12:43:29 -0700 | [diff] [blame] | 108 | std::string GetURL(const std::string& path) { return ::GetURL(port_, path); } |
Aaron Boodman | b34bcbf | 2015-03-02 10:05:31 -0800 | [diff] [blame] | 109 | |
| 110 | http_server::HttpServerFactoryPtr http_server_factory_; |
Viet-Trung Luu | fe25adb | 2016-04-28 10:33:45 -0700 | [diff] [blame] | 111 | mojo::SynchronousInterfacePtr<http_server::HttpServer> http_server_; |
Aaron Boodman | b34bcbf | 2015-03-02 10:05:31 -0800 | [diff] [blame] | 112 | scoped_ptr<GetHandler> handler_; |
| 113 | uint16_t port_; |
| 114 | |
| 115 | private: |
| 116 | MOJO_DISALLOW_COPY_AND_ASSIGN(ShellHTTPAppTest); |
| 117 | }; |
| 118 | |
Aaron Boodman | b34bcbf | 2015-03-02 10:05:31 -0800 | [diff] [blame] | 119 | // Test that we can load apps over http. |
| 120 | TEST_F(ShellHTTPAppTest, Http) { |
Viet-Trung Luu | bd07e3a | 2015-04-09 12:43:29 -0700 | [diff] [blame] | 121 | PingablePtr pingable; |
Viet-Trung Luu | d63ebab | 2016-05-17 15:49:50 -0700 | [diff] [blame] | 122 | mojo::ConnectToService(shell(), GetURL("app"), GetProxy(&pingable)); |
Viet-Trung Luu | e377a9e | 2015-04-09 13:53:21 -0700 | [diff] [blame] | 123 | 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 Boodman | b34bcbf | 2015-03-02 10:05:31 -0800 | [diff] [blame] | 131 | base::RunLoop().Run(); |
| 132 | } |
| 133 | |
| 134 | // Test that redirects work. |
| 135 | // TODO(aa): Test that apps receive the correct URL parameters. |
| 136 | TEST_F(ShellHTTPAppTest, Redirect) { |
Viet-Trung Luu | bd07e3a | 2015-04-09 12:43:29 -0700 | [diff] [blame] | 137 | PingablePtr pingable; |
Viet-Trung Luu | d63ebab | 2016-05-17 15:49:50 -0700 | [diff] [blame] | 138 | mojo::ConnectToService(shell(), GetURL("redirect"), GetProxy(&pingable)); |
Viet-Trung Luu | e377a9e | 2015-04-09 13:53:21 -0700 | [diff] [blame] | 139 | 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 Boodman | b34bcbf | 2015-03-02 10:05:31 -0800 | [diff] [blame] | 147 | base::RunLoop().Run(); |
| 148 | } |
| 149 | |
| 150 | // Test that querystring is not considered when resolving http applications. |
Michael Wasserman | 2d4bcfc | 2015-03-03 14:20:51 -0800 | [diff] [blame] | 151 | // 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 |
| 157 | TEST_F(ShellHTTPAppTest, MAYBE_QueryHandling) { |
Viet-Trung Luu | bd07e3a | 2015-04-09 12:43:29 -0700 | [diff] [blame] | 158 | PingablePtr pingable1; |
| 159 | PingablePtr pingable2; |
Viet-Trung Luu | d63ebab | 2016-05-17 15:49:50 -0700 | [diff] [blame] | 160 | mojo::ConnectToService(shell(), GetURL("app?foo"), GetProxy(&pingable1)); |
| 161 | mojo::ConnectToService(shell(), GetURL("app?bar"), GetProxy(&pingable2)); |
Aaron Boodman | b34bcbf | 2015-03-02 10:05:31 -0800 | [diff] [blame] | 162 | |
| 163 | int num_responses = 0; |
Benjamin Lerman | 1257b85 | 2015-09-24 09:52:12 +0200 | [diff] [blame] | 164 | 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 Boodman | b34bcbf | 2015-03-02 10:05:31 -0800 | [diff] [blame] | 181 | }; |
Benjamin Lerman | 1257b85 | 2015-09-24 09:52:12 +0200 | [diff] [blame] | 182 | pingable1->Ping("hello", callbacks_builder(1)); |
| 183 | pingable2->Ping("hello", callbacks_builder(2)); |
Aaron Boodman | b34bcbf | 2015-03-02 10:05:31 -0800 | [diff] [blame] | 184 | base::RunLoop().Run(); |
| 185 | } |
Aaron Boodman | b34bcbf | 2015-03-02 10:05:31 -0800 | [diff] [blame] | 186 | |
| 187 | // mojo: URLs can have querystrings too |
| 188 | TEST_F(ShellAppTest, MojoURLQueryHandling) { |
Viet-Trung Luu | bd07e3a | 2015-04-09 12:43:29 -0700 | [diff] [blame] | 189 | PingablePtr pingable; |
Viet-Trung Luu | d63ebab | 2016-05-17 15:49:50 -0700 | [diff] [blame] | 190 | mojo::ConnectToService(shell(), "mojo:pingable_app?foo", GetProxy(&pingable)); |
Viet-Trung Luu | 71447c8 | 2015-11-16 16:59:09 -0800 | [diff] [blame] | 191 | auto callback = [](const String& app_url, const String& connection_url, |
| 192 | const String& message) { |
James Robinson | c4d0fb2 | 2016-01-28 14:31:21 -0800 | [diff] [blame] | 193 | EXPECT_TRUE(base::EndsWith(app_url.To<base::StringPiece>(), |
| 194 | "/pingable_app.mojo", |
| 195 | base::CompareCase::SENSITIVE)); |
Viet-Trung Luu | e377a9e | 2015-04-09 13:53:21 -0700 | [diff] [blame] | 196 | EXPECT_EQ(app_url.To<std::string>() + "?foo", connection_url); |
| 197 | EXPECT_EQ("hello", message); |
| 198 | base::MessageLoop::current()->Quit(); |
| 199 | }; |
Aaron Boodman | b34bcbf | 2015-03-02 10:05:31 -0800 | [diff] [blame] | 200 | pingable->Ping("hello", callback); |
| 201 | base::RunLoop().Run(); |
| 202 | } |
| 203 | |
Viet-Trung Luu | 71447c8 | 2015-11-16 16:59:09 -0800 | [diff] [blame] | 204 | void TestApplicationConnector(mojo::ApplicationConnector* app_connector) { |
| 205 | PingablePtr pingable; |
Viet-Trung Luu | c5cfd31 | 2016-04-25 16:57:51 -0700 | [diff] [blame] | 206 | ConnectToService(app_connector, "mojo:pingable_app", GetProxy(&pingable)); |
Viet-Trung Luu | 71447c8 | 2015-11-16 16:59:09 -0800 | [diff] [blame] | 207 | auto callback = [](const String& app_url, const String& connection_url, |
| 208 | const String& message) { |
James Robinson | c4d0fb2 | 2016-01-28 14:31:21 -0800 | [diff] [blame] | 209 | EXPECT_TRUE(base::EndsWith(app_url.To<base::StringPiece>(), |
| 210 | "/pingable_app.mojo", |
| 211 | base::CompareCase::SENSITIVE)); |
Viet-Trung Luu | 71447c8 | 2015-11-16 16:59:09 -0800 | [diff] [blame] | 212 | 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 Luu | 199850d | 2015-11-16 15:20:58 -0800 | [diff] [blame] | 220 | TEST_F(ShellAppTest, ApplicationConnector) { |
| 221 | mojo::ApplicationConnectorPtr app_connector; |
Viet-Trung Luu | d63ebab | 2016-05-17 15:49:50 -0700 | [diff] [blame] | 222 | app_connector.Bind(mojo::CreateApplicationConnector(shell())); |
Viet-Trung Luu | 71447c8 | 2015-11-16 16:59:09 -0800 | [diff] [blame] | 223 | TestApplicationConnector(app_connector.get()); |
| 224 | } |
Viet-Trung Luu | 199850d | 2015-11-16 15:20:58 -0800 | [diff] [blame] | 225 | |
Viet-Trung Luu | 71447c8 | 2015-11-16 16:59:09 -0800 | [diff] [blame] | 226 | TEST_F(ShellAppTest, ApplicationConnectorDuplicate) { |
| 227 | mojo::ApplicationConnectorPtr app_connector1; |
Viet-Trung Luu | d63ebab | 2016-05-17 15:49:50 -0700 | [diff] [blame] | 228 | app_connector1.Bind(mojo::CreateApplicationConnector(shell())); |
Viet-Trung Luu | 71447c8 | 2015-11-16 16:59:09 -0800 | [diff] [blame] | 229 | { |
| 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 Luu | 199850d | 2015-11-16 15:20:58 -0800 | [diff] [blame] | 246 | } |
| 247 | |
Viet-Trung Luu | 33afb15 | 2015-03-11 16:58:49 -0700 | [diff] [blame] | 248 | } // namespace |