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 175 176 177 178
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ash/guest_os/guest_os_external_protocol_handler.h"
#include <vector>
#include "base/base64.h"
#include "base/test/scoped_feature_list.h"
#include "base/time/time.h"
#include "chrome/browser/ash/borealis/borealis_util.h"
#include "chrome/browser/ash/borealis/testing/features.h"
#include "chrome/browser/ash/crostini/fake_crostini_features.h"
#include "chrome/browser/ash/guest_os/guest_os_registry_service.h"
#include "chrome/test/base/testing_profile.h"
#include "chromeos/ash/components/dbus/vm_applications/apps.pb.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
namespace guest_os {
class GuestOsExternalProtocolHandlerTest : public testing::Test {
protected:
void SetUp() override {
fake_crostini_features_.set_enabled(true);
app_list_.set_vm_type(vm_tools::apps::VmType::TERMINA);
app_list_.set_vm_name("vm_name");
app_list_.set_container_name("container_name");
}
TestingProfile* profile() { return &profile_; }
vm_tools::apps::ApplicationList& app_list() { return app_list_; }
void AddApp(const std::string& name,
const std::string& desktop_file_id,
const std::string& mime_type) {
vm_tools::apps::App& app = *app_list_.add_apps();
app.mutable_name()->add_values()->set_value(name);
app.set_desktop_file_id(desktop_file_id);
app.add_mime_types(mime_type);
}
private:
content::BrowserTaskEnvironment task_environment_;
TestingProfile profile_;
crostini::FakeCrostiniFeatures fake_crostini_features_;
vm_tools::apps::ApplicationList app_list_;
};
TEST_F(GuestOsExternalProtocolHandlerTest, TestNoRegisteredApps) {
AddApp("App", "id", "not-scheme");
GuestOsRegistryService(profile()).UpdateApplicationList(app_list());
EXPECT_FALSE(
GuestOsUrlHandler::GetForUrl(profile(), GURL("testscheme:12341234")));
}
TEST_F(GuestOsExternalProtocolHandlerTest, SingleRegisteredApp) {
AddApp("App", "id", "x-scheme-handler/testscheme");
GuestOsRegistryService(profile()).UpdateApplicationList(app_list());
EXPECT_TRUE(
GuestOsUrlHandler::GetForUrl(profile(), GURL("testscheme:12341234")));
}
TEST_F(GuestOsExternalProtocolHandlerTest, MostRecent) {
AddApp("App1", "id1", "x-scheme-handler/testscheme");
AddApp("App2", "id2", "x-scheme-handler/testscheme");
GuestOsRegistryService(profile()).UpdateApplicationList(app_list());
GuestOsRegistryService(profile()).AppLaunched(
GuestOsRegistryService::GenerateAppId("id1", "vm_name",
"container_name"));
auto registration =
GuestOsUrlHandler::GetForUrl(profile(), GURL("testscheme:12341234"));
ASSERT_TRUE(registration);
EXPECT_EQ("App1", registration->name());
}
TEST_F(GuestOsExternalProtocolHandlerTest, TransientUrlHandlerIsInvoked) {
GuestOsRegistryService service(profile());
int invocations = 0;
service.RegisterTransientUrlHandler(
/*handler=*/GuestOsUrlHandler(
"Handler1", base::BindRepeating(
[](int& invocations, Profile*, const GURL& url) {
invocations++;
EXPECT_EQ(url.spec(), "test://test");
},
std::ref(invocations))),
/*canHandleCallback=*/base::BindRepeating(
[](const GURL& url) { return url.SchemeIs("test"); }));
GURL url{"test://test"};
std::optional<GuestOsUrlHandler> handler = service.GetHandler(url);
ASSERT_TRUE(handler);
handler->Handle(profile(), url);
EXPECT_EQ(invocations, 1);
}
TEST_F(GuestOsExternalProtocolHandlerTest,
InapplicableTransientUrlHandlersIgnored) {
GuestOsRegistryService service(profile());
service.RegisterTransientUrlHandler(
/*handler=*/GuestOsUrlHandler(
"Handler1", base::BindRepeating([](Profile*, const GURL& url) {})),
/*canHandleCallback=*/base::BindRepeating(
[](const GURL& url) { return url.SchemeIs("test"); }));
std::optional<GuestOsUrlHandler> handler =
service.GetHandler(GURL("otherscheme://test"));
EXPECT_FALSE(handler);
}
TEST_F(GuestOsExternalProtocolHandlerTest, OffTheRecordProfile) {
auto* otr_profile = profile()->GetOffTheRecordProfile(
Profile::OTRProfileID::CreateUniqueForTesting(),
/*create_if_needed=*/true);
EXPECT_FALSE(
GuestOsUrlHandler::GetForUrl(otr_profile, GURL("testscheme:12341234")));
profile()->DestroyOffTheRecordProfile(otr_profile);
}
class GuestOsExternalProtocolHandlerBorealisTest
: public GuestOsExternalProtocolHandlerTest {
void SetUp() override {
GuestOsExternalProtocolHandlerTest::SetUp();
allow_borealis_ = std::make_unique<borealis::ScopedAllowBorealis>(
profile(), /*also_enable=*/true);
SetupBorealisApp();
}
protected:
void SetupBorealisApp() {
app_list().set_vm_type(vm_tools::apps::VmType::BOREALIS);
AddApp("App", "id",
std::string("x-scheme-handler/") + borealis::kAllowedScheme);
GuestOsRegistryService(profile()).UpdateApplicationList(app_list());
}
private:
std::unique_ptr<borealis::ScopedAllowBorealis> allow_borealis_;
};
TEST_F(GuestOsExternalProtocolHandlerBorealisTest, AllowedURL) {
EXPECT_TRUE(
GuestOsUrlHandler::GetForUrl(profile(), GURL("steam://store/9001")));
EXPECT_TRUE(GuestOsUrlHandler::GetForUrl(profile(), GURL("steam://run/400")));
}
TEST_F(GuestOsExternalProtocolHandlerBorealisTest, DisallowedURL) {
// Wrong scheme
EXPECT_FALSE(GuestOsUrlHandler::GetForUrl(
profile(), GURL("notborealisscheme://run/12345")));
// Action not in allow-list
EXPECT_FALSE(
GuestOsUrlHandler::GetForUrl(profile(), GURL("steam://uninstall/12345")));
// Invalid app id
EXPECT_FALSE(GuestOsUrlHandler::GetForUrl(profile(), GURL("steam://store/")));
EXPECT_FALSE(GuestOsUrlHandler::GetForUrl(
profile(),
GURL("steam://run/"
"1337133713371337133713371337133713371337133713371337133713371337133"
"7133713371337133713371337133713371337133713371337133713371337133713"
"3713371337133713371337133713371337133713371337133713371337133713371"
"3371337133713371337133713371337133713371337133713371337133713371337"
"1337133713371337133713371337133713371337133713371337133713371337133"
"7133713371337133713371337133713371337133713371337133713371337")));
}
} // namespace guest_os
|