tor-browser

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

TestManyHandles.cpp (2443B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
      5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include "gtest/gtest.h"
      8 
      9 #include "mozilla/_ipdltest/IPDLUnitTest.h"
     10 #include "mozilla/_ipdltest/PTestManyHandlesChild.h"
     11 #include "mozilla/_ipdltest/PTestManyHandlesParent.h"
     12 
     13 using namespace mozilla::ipc;
     14 
     15 namespace mozilla::_ipdltest {
     16 
     17 class TestManyHandlesChild : public PTestManyHandlesChild {
     18  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(TestManyHandlesChild, override)
     19 
     20 public:
     21  IPCResult RecvManyHandles(nsTArray<FileDescriptor>&& aDescrs) override {
     22    EXPECT_EQ(aDescrs.Length(), 500u);
     23    for (int i = 0; i < static_cast<int>(aDescrs.Length()); ++i) {
     24      UniqueFileHandle handle = aDescrs[i].TakePlatformHandle();
     25      int value;
     26      const int size = sizeof(value);
     27 #ifdef XP_WIN
     28      DWORD numberOfBytesRead;
     29      EXPECT_TRUE(
     30          ::ReadFile(handle.get(), &value, size, &numberOfBytesRead, nullptr));
     31      EXPECT_EQ(numberOfBytesRead, (DWORD)size);
     32 #else
     33      EXPECT_EQ(read(handle.get(), &value, size), size);
     34 #endif
     35      EXPECT_EQ(value, i);
     36    }
     37    Close();
     38    return IPC_OK();
     39  }
     40 
     41 private:
     42  ~TestManyHandlesChild() = default;
     43 };
     44 
     45 class TestManyHandlesParent : public PTestManyHandlesParent {
     46  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(TestManyHandlesParent, override)
     47 
     48 private:
     49  ~TestManyHandlesParent() = default;
     50 };
     51 
     52 IPDL_TEST(TestManyHandles) {
     53  nsTArray<FileDescriptor> descrs;
     54  for (int i = 0; i < 500; ++i) {
     55    const int size = sizeof(i);
     56    UniqueFileHandle readPipe;
     57    UniqueFileHandle writePipe;
     58 #ifdef XP_WIN
     59    ASSERT_TRUE(::CreatePipe(getter_Transfers(readPipe),
     60                             getter_Transfers(writePipe), nullptr, size));
     61    DWORD numberOfBytesWritten;
     62    ASSERT_TRUE(
     63        ::WriteFile(writePipe.get(), &i, size, &numberOfBytesWritten, nullptr));
     64    ASSERT_EQ(numberOfBytesWritten, (DWORD)size);
     65 #else
     66    int fds[2];
     67    ASSERT_EQ(pipe(fds), 0);
     68    readPipe.reset(fds[0]);
     69    writePipe.reset(fds[1]);
     70    ASSERT_EQ(write(writePipe.get(), &i, size), size);
     71 #endif
     72    descrs.AppendElement(FileDescriptor(std::move(readPipe)));
     73  }
     74  bool ok = mActor->SendManyHandles(descrs);
     75  ASSERT_TRUE(ok);
     76 }
     77 
     78 }  // namespace mozilla::_ipdltest