summaryrefslogtreecommitdiffstats
path: root/ipc/ipdl/test/gtest/TestManyHandles.cpp
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
commit26a029d407be480d791972afb5975cf62c9360a6 (patch)
treef435a8308119effd964b339f76abb83a57c29483 /ipc/ipdl/test/gtest/TestManyHandles.cpp
parentInitial commit. (diff)
downloadfirefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz
firefox-26a029d407be480d791972afb5975cf62c9360a6.zip
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'ipc/ipdl/test/gtest/TestManyHandles.cpp')
-rw-r--r--ipc/ipdl/test/gtest/TestManyHandles.cpp78
1 files changed, 78 insertions, 0 deletions
diff --git a/ipc/ipdl/test/gtest/TestManyHandles.cpp b/ipc/ipdl/test/gtest/TestManyHandles.cpp
new file mode 100644
index 0000000000..ead8af22b1
--- /dev/null
+++ b/ipc/ipdl/test/gtest/TestManyHandles.cpp
@@ -0,0 +1,78 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=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/. */
+
+#include "gtest/gtest.h"
+
+#include "mozilla/_ipdltest/IPDLUnitTest.h"
+#include "mozilla/_ipdltest/PTestManyHandlesChild.h"
+#include "mozilla/_ipdltest/PTestManyHandlesParent.h"
+
+using namespace mozilla::ipc;
+
+namespace mozilla::_ipdltest {
+
+class TestManyHandlesChild : public PTestManyHandlesChild {
+ NS_INLINE_DECL_THREADSAFE_REFCOUNTING(TestManyHandlesChild, override)
+
+ public:
+ IPCResult RecvManyHandles(nsTArray<FileDescriptor>&& aDescrs) override {
+ EXPECT_EQ(aDescrs.Length(), 500u);
+ for (int i = 0; i < static_cast<int>(aDescrs.Length()); ++i) {
+ UniqueFileHandle handle = aDescrs[i].TakePlatformHandle();
+ int value;
+ const int size = sizeof(value);
+#ifdef XP_WIN
+ DWORD numberOfBytesRead;
+ EXPECT_TRUE(
+ ::ReadFile(handle.get(), &value, size, &numberOfBytesRead, nullptr));
+ EXPECT_EQ(numberOfBytesRead, (DWORD)size);
+#else
+ EXPECT_EQ(read(handle.get(), &value, size), size);
+#endif
+ EXPECT_EQ(value, i);
+ }
+ Close();
+ return IPC_OK();
+ }
+
+ private:
+ ~TestManyHandlesChild() = default;
+};
+
+class TestManyHandlesParent : public PTestManyHandlesParent {
+ NS_INLINE_DECL_THREADSAFE_REFCOUNTING(TestManyHandlesParent, override)
+
+ private:
+ ~TestManyHandlesParent() = default;
+};
+
+IPDL_TEST(TestManyHandles) {
+ nsTArray<FileDescriptor> descrs;
+ for (int i = 0; i < 500; ++i) {
+ const int size = sizeof(i);
+ UniqueFileHandle readPipe;
+ UniqueFileHandle writePipe;
+#ifdef XP_WIN
+ ASSERT_TRUE(::CreatePipe(getter_Transfers(readPipe),
+ getter_Transfers(writePipe), nullptr, size));
+ DWORD numberOfBytesWritten;
+ ASSERT_TRUE(
+ ::WriteFile(writePipe.get(), &i, size, &numberOfBytesWritten, nullptr));
+ ASSERT_EQ(numberOfBytesWritten, (DWORD)size);
+#else
+ int fds[2];
+ ASSERT_EQ(pipe(fds), 0);
+ readPipe.reset(fds[0]);
+ writePipe.reset(fds[1]);
+ ASSERT_EQ(write(writePipe.get(), &i, size), size);
+#endif
+ descrs.AppendElement(FileDescriptor(std::move(readPipe)));
+ }
+ bool ok = mActor->SendManyHandles(descrs);
+ ASSERT_TRUE(ok);
+}
+
+} // namespace mozilla::_ipdltest