From 41d439e4e4a346b6a2ef3b0a18b1ee92160ee9f7 Mon Sep 17 00:00:00 2001 From: David Parks Date: Wed, 24 Oct 2018 21:04:44 +0000 Subject: [PATCH] Bug 1500219: Part 2 - Test UniquePtr integration with IPDL (r=jld) Tests that pass UniquePtrs in IPDL messages. Depends on D9143 Differential Revision: https://phabricator.services.mozilla.com/D9144 --HG-- extra : moz-landing-system : lando --- ipc/ipdl/test/cxx/PTestUniquePtrIPC.ipdl | 23 +++++++ ipc/ipdl/test/cxx/TestUniquePtrIPC.cpp | 88 ++++++++++++++++++++++++ ipc/ipdl/test/cxx/TestUniquePtrIPC.h | 71 +++++++++++++++++++ ipc/ipdl/test/cxx/moz.build | 2 + 4 files changed, 184 insertions(+) create mode 100644 ipc/ipdl/test/cxx/PTestUniquePtrIPC.ipdl create mode 100644 ipc/ipdl/test/cxx/TestUniquePtrIPC.cpp create mode 100644 ipc/ipdl/test/cxx/TestUniquePtrIPC.h diff --git a/ipc/ipdl/test/cxx/PTestUniquePtrIPC.ipdl b/ipc/ipdl/test/cxx/PTestUniquePtrIPC.ipdl new file mode 100644 index 000000000000..6c09c7174c76 --- /dev/null +++ b/ipc/ipdl/test/cxx/PTestUniquePtrIPC.ipdl @@ -0,0 +1,23 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +namespace mozilla { +namespace _ipdltest { + +struct DummyStruct { + int x; +}; + +protocol PTestUniquePtrIPC +{ +child: + async TestMessage(UniquePtr a1, UniquePtr a2, + DummyStruct a3, UniquePtr a4); + async TestSendReference(UniquePtr a); +}; + +} +} diff --git a/ipc/ipdl/test/cxx/TestUniquePtrIPC.cpp b/ipc/ipdl/test/cxx/TestUniquePtrIPC.cpp new file mode 100644 index 000000000000..72e4459c41ce --- /dev/null +++ b/ipc/ipdl/test/cxx/TestUniquePtrIPC.cpp @@ -0,0 +1,88 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public +* License, v. 2.0. If a copy of the MPL was not distributed with this +* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "TestUniquePtrIPC.h" + +namespace mozilla { +namespace _ipdltest { + +// --------------------------------------------------------------------------- +// PARENT PROCESS +// --------------------------------------------------------------------------- + +void +TestUniquePtrIPCParent::Main() +{ + UniquePtr a1 = MakeUnique(1); + UniquePtr a2 = MakeUnique(2); + DummyStruct a3(3); + UniquePtr a4; + + if (!SendTestMessage(std::move(a1), std::move(a2), a3, std::move(a4))) { + fail("failed sending UniquePtr items"); + } + + if (a1 || a2) { + fail("did not move TestMessage items in parent"); + } + + if (a4) { + fail("somehow turned null ptr into non-null by sending it"); + } + + // Pass UniquePtr by reference + UniquePtr b = MakeUnique(1); + + if (!SendTestSendReference(b)) { + fail("failed sending UniquePtr by reference"); + } + if (b) { + fail("did not move UniquePtr sent by reference"); + } +} + +// --------------------------------------------------------------------------- +// CHILD PROCESS +// --------------------------------------------------------------------------- + +mozilla::ipc::IPCResult +TestUniquePtrIPCChild::RecvTestMessage(UniquePtr&& aA1, + UniquePtr&& aA2, + const DummyStruct& aA3, + UniquePtr&& aA4) +{ + if ((!aA1) || (!aA2)) { + fail("TestMessage received NULL items in child"); + } + + if (aA4) { + fail("TestMessage received non-NULL when expecting NULL"); + } + + if ((*aA1 != 1) || (aA2->x() != 2) || (aA3.x() != 3)) { + fail("TestMessage received incorrect items in child"); + } + + return IPC_OK(); +} + +mozilla::ipc::IPCResult +TestUniquePtrIPCChild::RecvTestSendReference(UniquePtr&& aA) +{ + if (!aA) { + fail("TestSendReference received NULL item in child"); + } + + if (*aA != 1) { + fail("TestSendReference received incorrect item in child"); + } + + Close(); + return IPC_OK(); +} + + +} // namespace _ipdltest +} // namespace mozilla diff --git a/ipc/ipdl/test/cxx/TestUniquePtrIPC.h b/ipc/ipdl/test/cxx/TestUniquePtrIPC.h new file mode 100644 index 000000000000..ae306230650e --- /dev/null +++ b/ipc/ipdl/test/cxx/TestUniquePtrIPC.h @@ -0,0 +1,71 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public +* License, v. 2.0. If a copy of the MPL was not distributed with this +* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef mozilla_TestUniquePtrIPC_h +#define mozilla_TestUniquePtrIPC_h + +#include "mozilla/_ipdltest/IPDLUnitTests.h" + +#include "mozilla/_ipdltest/PTestUniquePtrIPCParent.h" +#include "mozilla/_ipdltest/PTestUniquePtrIPCChild.h" + +namespace mozilla { +namespace _ipdltest { + +class TestUniquePtrIPCParent : + public PTestUniquePtrIPCParent +{ +public: + TestUniquePtrIPCParent() { MOZ_COUNT_CTOR(TestUniquePtrIPCParent); } + virtual ~TestUniquePtrIPCParent() { MOZ_COUNT_DTOR(TestUniquePtrIPCParent); } + + static bool RunTestInProcesses() { return true; } + static bool RunTestInThreads() { return false; } + + void Main(); + + bool ShouldContinueFromReplyTimeout() override + { + return false; + } + + virtual void ActorDestroy(ActorDestroyReason why) override + { + if (NormalShutdown != why) { + fail("Abnormal shutdown of parent"); + } + passed("ok"); + QuitParent(); + } +}; + + +class TestUniquePtrIPCChild : + public PTestUniquePtrIPCChild +{ +public: + TestUniquePtrIPCChild() { MOZ_COUNT_CTOR(TestUniquePtrIPCChild); } + virtual ~TestUniquePtrIPCChild() { MOZ_COUNT_DTOR(TestUniquePtrIPCChild); } + + mozilla::ipc::IPCResult + RecvTestMessage(UniquePtr&& aA1, UniquePtr&& aA2, + const DummyStruct& aA3, UniquePtr&& aA4) override; + + mozilla::ipc::IPCResult + RecvTestSendReference(UniquePtr&& aA) override; + + virtual void ActorDestroy(ActorDestroyReason why) override + { + if (NormalShutdown != why) { + fail("Abnormal shutdown of child"); + } + QuitChild(); + } +}; + +} // namespace _ipdltest +} // namespace mozilla + +#endif // mozilla_TestUniquePtrIPC_h diff --git a/ipc/ipdl/test/cxx/moz.build b/ipc/ipdl/test/cxx/moz.build index 8e8845f5857d..ed3c2185a111 100644 --- a/ipc/ipdl/test/cxx/moz.build +++ b/ipc/ipdl/test/cxx/moz.build @@ -50,6 +50,7 @@ SOURCES += [ 'TestSyncError.cpp', 'TestSyncHang.cpp', 'TestSyncWakeup.cpp', + 'TestUniquePtrIPC.cpp', 'TestUrgency.cpp', 'TestUrgentHangs.cpp', ] @@ -123,6 +124,7 @@ IPDL_SOURCES += [ 'PTestSyncError.ipdl', 'PTestSyncHang.ipdl', 'PTestSyncWakeup.ipdl', + 'PTestUniquePtrIPC.ipdl', 'PTestUrgency.ipdl', 'PTestUrgentHangs.ipdl', ]