blob: bfafe0c2538344b9f70efac9f3d9b2549637ba82 [file]
// Copyright 2020 The Fuchsia 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 "src/graphics/drivers/misc/goldfish/instance.h"
#include <fidl/fuchsia.hardware.goldfish/cpp/wire.h>
#include <fidl/fuchsia.sysmem/cpp/wire.h>
#include <fuchsia/hardware/goldfish/pipe/c/banjo.h>
#include <fuchsia/hardware/goldfish/pipe/cpp/banjo-mock.h>
#include <lib/fake-bti/bti.h>
#include <lib/fidl-async/cpp/bind.h>
#include <lib/fidl/llcpp/wire_messaging.h>
#include <tuple>
#include <zxtest/zxtest.h>
#include "src/devices/testing/mock-ddk/mock-device.h"
namespace goldfish {
class FakeInstance : public Instance {
public:
FakeInstance(zx_device_t* parent) : Instance(parent) {}
zx_status_t Connect(async_dispatcher_t* dispatcher,
fidl::ServerEnd<fuchsia_hardware_goldfish::PipeDevice> server) {
return fidl::BindSingleInFlightOnly(dispatcher, std::move(server), this);
}
};
// Test suite creating fake Instance on a mock Pipe device.
class InstanceDeviceTest : public zxtest::Test {
public:
InstanceDeviceTest()
: zxtest::Test(),
fake_root_(MockDevice::FakeRootParent()),
loop_(&kAsyncLoopConfigAttachToCurrentThread) {}
// |zxtest::Test|
void SetUp() override {
zx::bti out_bti;
ASSERT_OK(fake_bti_create(out_bti.reset_and_get_address()));
ASSERT_OK(out_bti.duplicate(ZX_RIGHT_SAME_RIGHTS, &acpi_bti_));
zx::vmo out_vmo;
ASSERT_OK(zx::vmo::create(16 * 1024, 0u, &out_vmo));
mock_pipe_.ExpectGetBti(ZX_OK, std::move(out_bti));
mock_pipe_.mock_open().ExpectCallWithMatcher([](int32_t) {});
mock_pipe_.mock_exec().ExpectCallWithMatcher([](int32_t) {});
mock_pipe_.mock_destroy().ExpectCallWithMatcher([](int32_t) {});
mock_pipe_.mock_connect_sysmem().ExpectCallWithMatcher(
[](const zx::channel&) { return std::make_tuple(ZX_OK); });
mock_pipe_.mock_register_sysmem_heap().ExpectCallWithMatcher(
[](uint64_t, const zx::channel&) { return std::make_tuple(ZX_OK); });
mock_pipe_.mock_set_event().ExpectCallWithMatcher(
[](int32_t, const zx::event&) { return std::make_tuple(ZX_OK); });
mock_pipe_.mock_create().ExpectCallWithMatcher([this]() {
zx::vmo out_vmo;
zx::vmo::create(16 * 1024, 0u, &out_vmo);
return std::make_tuple(ZX_OK, ++id_, std::move(out_vmo));
});
fake_root_->AddProtocol(ZX_PROTOCOL_GOLDFISH_PIPE, mock_pipe_.GetProto()->ops,
mock_pipe_.GetProto()->ctx);
auto dut = std::make_unique<FakeInstance>(fake_root_.get());
ASSERT_OK(dut->Bind());
dut_ = dut.release();
zx::status endpoints = fidl::CreateEndpoints<fuchsia_hardware_goldfish::PipeDevice>();
ASSERT_OK(endpoints.status_value());
ASSERT_OK(dut_->Connect(loop_.dispatcher(), std::move(endpoints->server)));
fidl_client_.Bind(std::move(endpoints->client));
}
// |zxtest::Test|
void TearDown() override {
loop_.Quit();
loop_.JoinThreads();
}
protected:
ddk::MockGoldfishPipe mock_pipe_;
FakeInstance* dut_;
std::shared_ptr<MockDevice> fake_root_;
fidl::WireSyncClient<fuchsia_hardware_goldfish::PipeDevice> fidl_client_;
async::Loop loop_;
private:
zx::bti acpi_bti_;
int32_t id_ = 0;
};
TEST_F(InstanceDeviceTest, OpenPipe) {
auto endpoints = fidl::CreateEndpoints<fuchsia_hardware_goldfish::Pipe>();
ASSERT_TRUE(endpoints.is_ok());
ASSERT_TRUE(fidl_client_->OpenPipe(std::move(endpoints->server)).ok());
loop_.RunUntilIdle();
}
TEST_F(InstanceDeviceTest, OpenPipeCloseDutFirst) {
auto endpoints = fidl::CreateEndpoints<fuchsia_hardware_goldfish::Pipe>();
ASSERT_TRUE(endpoints.is_ok());
ASSERT_TRUE(fidl_client_->OpenPipe(std::move(endpoints->server)).ok());
loop_.RunUntilIdle();
endpoints->client.reset();
}
} // namespace goldfish