tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

TestUniquePtrIPC.cpp (2815B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 /*
      7 * Test UniquePtr IPC arguments.
      8 */
      9 
     10 #include "gtest/gtest.h"
     11 
     12 #include "mozilla/_ipdltest/IPDLUnitTest.h"
     13 #include "mozilla/_ipdltest/PTestUniquePtrIPCChild.h"
     14 #include "mozilla/_ipdltest/PTestUniquePtrIPCParent.h"
     15 
     16 using namespace mozilla::ipc;
     17 
     18 namespace mozilla::_ipdltest {
     19 
     20 class TestUniquePtrIPCParent : public PTestUniquePtrIPCParent {
     21  NS_INLINE_DECL_REFCOUNTING(TestUniquePtrIPCParent, override)
     22 private:
     23  ~TestUniquePtrIPCParent() = default;
     24 };
     25 
     26 class TestUniquePtrIPCChild : public PTestUniquePtrIPCChild {
     27  NS_INLINE_DECL_REFCOUNTING(TestUniquePtrIPCChild, override)
     28 private:
     29  IPCResult RecvTestMessage(const UniquePtr<std::string>& aA1,
     30                            const UniquePtr<DummyStruct>& aA2,
     31                            const DummyStruct& aA3,
     32                            const UniquePtr<std::string>& aA4,
     33                            const DummyUnion& aA5) final override {
     34    EXPECT_TRUE(aA1) << "TestMessage received NULL aA1";
     35    EXPECT_TRUE(aA2) << "TestMessage received NULL aA2";
     36    EXPECT_FALSE(aA4)
     37        << "TestMessage received non-NULL when expecting NULL aA4";
     38 
     39    EXPECT_EQ(*aA1, std::string("unique"));
     40    EXPECT_EQ(aA2->x(), std::string("uniqueStruct"));
     41    EXPECT_EQ(aA3.x(), std::string("struct"));
     42    EXPECT_EQ(*aA5.get_string(), std::string("union"));
     43 
     44    return IPC_OK();
     45  }
     46 
     47  IPCResult RecvTestSendReference(
     48      const UniquePtr<DummyStruct>& aA) final override {
     49    EXPECT_TRUE(aA) << "TestSendReference received NULL item in child";
     50    EXPECT_EQ(aA->x(), std::string("reference"));
     51 
     52    Close();
     53    return IPC_OK();
     54  }
     55 
     56  ~TestUniquePtrIPCChild() = default;
     57 };
     58 
     59 IPDL_TEST(TestUniquePtrIPC) {
     60  UniquePtr<std::string> a1 = MakeUnique<std::string>("unique");
     61  UniquePtr<DummyStruct> a2 = MakeUnique<DummyStruct>("uniqueStruct");
     62  DummyStruct a3("struct");
     63  UniquePtr<std::string> a4;
     64  DummyUnion a5(MakeUnique<std::string>("union"));
     65 
     66  EXPECT_TRUE(mActor->SendTestMessage(a1, a2, a3, a4, a5));
     67 
     68  EXPECT_TRUE(a1)
     69      << "IPC arguments are passed by const reference and shouldn't be moved";
     70  EXPECT_TRUE(a2)
     71      << "IPC arguments are passed by const reference and shouldn't be moved";
     72 
     73  EXPECT_FALSE(a4) << "somehow turned null ptr into non-null by sending it";
     74 
     75  // Pass UniquePtr by reference
     76  UniquePtr<DummyStruct> b = MakeUnique<DummyStruct>("reference");
     77 
     78  EXPECT_TRUE(mActor->SendTestSendReference(b));
     79  EXPECT_TRUE(b)
     80      << "IPC arguments are passed by const reference and shouldn't be moved";
     81 }
     82 
     83 }  // namespace mozilla::_ipdltest