#include "base/notreached.h"
#include "base/strings/string_number_conversions.h"
#include "base/task/sequenced_task_runner.h"
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "content/browser/direct_sockets/direct_sockets_service_impl.h"
#include "content/browser/direct_sockets/direct_sockets_test_utils.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/shell/browser/shell.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "net/base/ip_address.h"
#include "net/base/ip_endpoint.h"
#include "net/dns/host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "services/network/public/mojom/network_context.mojom.h"
#include "services/network/public/mojom/udp_socket.mojom.h"
#include "services/network/test/test_network_context.h"
#include "services/network/test/test_udp_socket.h"
#include "services/network/test/udp_socket_test_util.h"
#include "testing/gmock/include/gmock/gmock-matchers.h"
#include "third_party/blink/public/common/features_generated.h"
#include "url/gurl.h"
#if BUILDFLAG(IS_CHROMEOS)
#include "chromeos/dbus/permission_broker/fake_permission_broker_client.h"
#endif
namespace content {
namespace {
constexpr char kLocalhostAddress[] = …;
}
class DirectSocketsUdpBrowserTest : public ContentBrowserTest { … };
IN_PROC_BROWSER_TEST_F(DirectSocketsUdpBrowserTest, CloseUdp) { … }
IN_PROC_BROWSER_TEST_F(DirectSocketsUdpBrowserTest, SendUdpAfterClose) { … }
IN_PROC_BROWSER_TEST_F(DirectSocketsUdpBrowserTest, ReadUdpAfterSocketClose) { … }
IN_PROC_BROWSER_TEST_F(DirectSocketsUdpBrowserTest, ReadUdpAfterStreamClose) { … }
IN_PROC_BROWSER_TEST_F(DirectSocketsUdpBrowserTest, CloseWithActiveReader) { … }
IN_PROC_BROWSER_TEST_F(DirectSocketsUdpBrowserTest,
CloseWithActiveReaderForce) { … }
IN_PROC_BROWSER_TEST_F(DirectSocketsUdpBrowserTest, ReadWriteUdpOnSendError) { … }
IN_PROC_BROWSER_TEST_F(DirectSocketsUdpBrowserTest, ReadWriteUdpOnSocketError) { … }
class DirectSocketsBoundUdpBrowserTest : public DirectSocketsUdpBrowserTest { … };
IN_PROC_BROWSER_TEST_F(DirectSocketsBoundUdpBrowserTest, ExchangeUdp) { … }
#if BUILDFLAG(IS_CHROMEOS)
IN_PROC_BROWSER_TEST_F(DirectSocketsBoundUdpBrowserTest, HasFirewallHole) {
class DelegateImpl : public chromeos::FakePermissionBrokerClient::Delegate {
public:
DelegateImpl(uint16_t port, base::OnceClosure quit_closure)
: port_(port), quit_closure_(std::move(quit_closure)) {}
void OnUdpPortReleased(uint16_t port,
const std::string& interface) override {
if (port == port_) {
ASSERT_EQ(interface, "");
ASSERT_TRUE(quit_closure_);
std::move(quit_closure_).Run();
}
}
private:
uint16_t port_;
base::OnceClosure quit_closure_;
};
auto* client = static_cast<chromeos::FakePermissionBrokerClient*>(
chromeos::PermissionBrokerClient::Get());
const std::string open_script = R"(
(async () => {
socket = new UDPSocket({ localAddress: '127.0.0.1' });
const { localPort } = await socket.opened;
return localPort;
})();
)";
const int32_t local_port = EvalJs(shell(), open_script).ExtractInt();
ASSERT_TRUE(client->HasUdpHole(local_port, "" ));
base::RunLoop run_loop;
auto delegate =
std::make_unique<DelegateImpl>(local_port, run_loop.QuitClosure());
client->AttachDelegate(delegate.get());
EXPECT_TRUE(EvalJs(shell(), content::test::WrapAsync("socket.close()"))
.error.empty());
run_loop.Run();
}
IN_PROC_BROWSER_TEST_F(DirectSocketsBoundUdpBrowserTest, FirewallHoleDenied) {
auto* client = chromeos::FakePermissionBrokerClient::Get();
client->SetUdpDenyAll();
const std::string open_script = R"(
(async () => {
socket = new UDPSocket({ localAddress: '127.0.0.1' });
return await socket.opened.catch(err => err.message);
})();
)";
EXPECT_THAT(EvalJs(shell(), open_script).ExtractString(),
testing::HasSubstr("Firewall"));
}
#endif
IN_PROC_BROWSER_TEST_F(DirectSocketsUdpBrowserTest, UdpMessageConfigurations) { … }
}