summaryrefslogtreecommitdiffstats
path: root/ipc/ipdl/test/gtest/TestUniquePtrIPC.cpp
blob: b3c68b99b4c57ea868715bedb3441763063db8d7 (plain)
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
/* -*- 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/. */

/*
 * Test UniquePtr IPC arguments.
 */

#include "gtest/gtest.h"

#include "mozilla/_ipdltest/IPDLUnitTest.h"
#include "mozilla/_ipdltest/PTestUniquePtrIPCChild.h"
#include "mozilla/_ipdltest/PTestUniquePtrIPCParent.h"

using namespace mozilla::ipc;

namespace mozilla::_ipdltest {

class TestUniquePtrIPCParent : public PTestUniquePtrIPCParent {
  NS_INLINE_DECL_REFCOUNTING(TestUniquePtrIPCParent, override)
 private:
  ~TestUniquePtrIPCParent() = default;
};

class TestUniquePtrIPCChild : public PTestUniquePtrIPCChild {
  NS_INLINE_DECL_REFCOUNTING(TestUniquePtrIPCChild, override)
 private:
  IPCResult RecvTestMessage(const UniquePtr<std::string>& aA1,
                            const UniquePtr<DummyStruct>& aA2,
                            const DummyStruct& aA3,
                            const UniquePtr<std::string>& aA4,
                            const DummyUnion& aA5) final override {
    EXPECT_TRUE(aA1) << "TestMessage received NULL aA1";
    EXPECT_TRUE(aA2) << "TestMessage received NULL aA2";
    EXPECT_FALSE(aA4)
        << "TestMessage received non-NULL when expecting NULL aA4";

    EXPECT_EQ(*aA1, std::string("unique"));
    EXPECT_EQ(aA2->x(), std::string("uniqueStruct"));
    EXPECT_EQ(aA3.x(), std::string("struct"));
    EXPECT_EQ(*aA5.get_string(), std::string("union"));

    return IPC_OK();
  }

  IPCResult RecvTestSendReference(
      const UniquePtr<DummyStruct>& aA) final override {
    EXPECT_TRUE(aA) << "TestSendReference received NULL item in child";
    EXPECT_EQ(aA->x(), std::string("reference"));

    Close();
    return IPC_OK();
  }

  ~TestUniquePtrIPCChild() = default;
};

IPDL_TEST(TestUniquePtrIPC) {
  UniquePtr<std::string> a1 = MakeUnique<std::string>("unique");
  UniquePtr<DummyStruct> a2 = MakeUnique<DummyStruct>("uniqueStruct");
  DummyStruct a3("struct");
  UniquePtr<std::string> a4;
  DummyUnion a5(MakeUnique<std::string>("union"));

  EXPECT_TRUE(mActor->SendTestMessage(a1, a2, a3, a4, a5));

  EXPECT_TRUE(a1)
      << "IPC arguments are passed by const reference and shouldn't be moved";
  EXPECT_TRUE(a2)
      << "IPC arguments are passed by const reference and shouldn't be moved";

  EXPECT_FALSE(a4) << "somehow turned null ptr into non-null by sending it";

  // Pass UniquePtr by reference
  UniquePtr<DummyStruct> b = MakeUnique<DummyStruct>("reference");

  EXPECT_TRUE(mActor->SendTestSendReference(b));
  EXPECT_TRUE(b)
      << "IPC arguments are passed by const reference and shouldn't be moved";
}

}  // namespace mozilla::_ipdltest