2016-09-30 19:51:01 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; 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 "TestCommon.h"
|
2016-11-29 21:53:48 +03:00
|
|
|
#include "gtest/gtest.h"
|
2016-09-30 19:51:01 +03:00
|
|
|
|
|
|
|
#include <Windows.h>
|
|
|
|
|
|
|
|
#include "mozilla/Atomics.h"
|
|
|
|
#include "mozilla/Monitor.h"
|
|
|
|
#include "nsINamedPipeService.h"
|
|
|
|
#include "nsNetCID.h"
|
|
|
|
|
|
|
|
#define PIPE_NAME "\\\\.\\pipe\\TestNPS"
|
|
|
|
#define TEST_STR "Hello World"
|
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
|
2016-12-08 22:05:39 +03:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unlike a monitor, an event allows a thread to wait on another thread
|
|
|
|
* completing an action without regard to ordering of the wait and the notify.
|
|
|
|
*/
|
|
|
|
class Event
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit Event(const char* aName)
|
|
|
|
: mMonitor(aName) { }
|
|
|
|
|
|
|
|
~Event() = default;
|
|
|
|
|
|
|
|
void Set() {
|
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
MOZ_ASSERT(!mSignaled);
|
|
|
|
mSignaled = true;
|
|
|
|
mMonitor.Notify();
|
|
|
|
}
|
|
|
|
void Wait() {
|
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
while (!mSignaled) {
|
|
|
|
lock.Wait();
|
|
|
|
}
|
|
|
|
mSignaled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Monitor mMonitor;
|
|
|
|
bool mSignaled = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2016-09-30 19:51:01 +03:00
|
|
|
class nsNamedPipeDataObserver : public nsINamedPipeDataObserver
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
NS_DECL_NSINAMEDPIPEDATAOBSERVER
|
|
|
|
|
|
|
|
explicit nsNamedPipeDataObserver(HANDLE aPipe);
|
|
|
|
|
|
|
|
int Read(void* aBuffer, uint32_t aSize);
|
2016-11-09 19:38:36 +03:00
|
|
|
int Write(const void* aBuffer, uint32_t aSize);
|
2016-09-30 19:51:01 +03:00
|
|
|
|
|
|
|
uint32_t Transferred() const { return mBytesTransferred; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
~nsNamedPipeDataObserver() = default;
|
|
|
|
|
|
|
|
HANDLE mPipe;
|
|
|
|
OVERLAPPED mOverlapped;
|
|
|
|
Atomic<uint32_t> mBytesTransferred;
|
2016-12-08 22:05:39 +03:00
|
|
|
Event mEvent;
|
2016-09-30 19:51:01 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(nsNamedPipeDataObserver, nsINamedPipeDataObserver)
|
|
|
|
|
|
|
|
nsNamedPipeDataObserver::nsNamedPipeDataObserver(HANDLE aPipe)
|
|
|
|
: mPipe(aPipe)
|
|
|
|
, mOverlapped()
|
|
|
|
, mBytesTransferred(0)
|
2016-12-08 22:05:39 +03:00
|
|
|
, mEvent("named-pipe")
|
2016-09-30 19:51:01 +03:00
|
|
|
{
|
|
|
|
mOverlapped.hEvent = CreateEventA(nullptr, TRUE, TRUE, "named-pipe");
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
nsNamedPipeDataObserver::Read(void* aBuffer, uint32_t aSize)
|
|
|
|
{
|
|
|
|
DWORD bytesRead = 0;
|
|
|
|
if (!ReadFile(mPipe, aBuffer, aSize, &bytesRead, &mOverlapped)) {
|
|
|
|
switch(GetLastError()) {
|
|
|
|
case ERROR_IO_PENDING:
|
|
|
|
{
|
2016-12-08 22:05:39 +03:00
|
|
|
mEvent.Wait();
|
2016-09-30 19:51:01 +03:00
|
|
|
}
|
|
|
|
if (!GetOverlappedResult(mPipe, &mOverlapped, &bytesRead, FALSE)) {
|
2016-11-29 21:53:48 +03:00
|
|
|
ADD_FAILURE() << "GetOverlappedResult failed";
|
2016-09-30 19:51:01 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (mBytesTransferred != bytesRead) {
|
2016-11-29 21:53:48 +03:00
|
|
|
ADD_FAILURE() << "GetOverlappedResult mismatch";
|
2016-09-30 19:51:01 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
default:
|
2016-11-29 21:53:48 +03:00
|
|
|
ADD_FAILURE() << "ReadFile error " << GetLastError();
|
2016-09-30 19:51:01 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
2016-12-08 22:05:39 +03:00
|
|
|
mEvent.Wait();
|
2016-09-30 19:51:01 +03:00
|
|
|
|
|
|
|
if (mBytesTransferred != bytesRead) {
|
2016-11-29 21:53:48 +03:00
|
|
|
ADD_FAILURE() << "GetOverlappedResult mismatch";
|
2016-09-30 19:51:01 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mBytesTransferred = 0;
|
|
|
|
return bytesRead;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2016-11-09 19:38:36 +03:00
|
|
|
nsNamedPipeDataObserver::Write(const void* aBuffer, uint32_t aSize)
|
2016-09-30 19:51:01 +03:00
|
|
|
{
|
|
|
|
DWORD bytesWritten = 0;
|
|
|
|
if (!WriteFile(mPipe, aBuffer, aSize, &bytesWritten, &mOverlapped)) {
|
|
|
|
switch(GetLastError()) {
|
|
|
|
case ERROR_IO_PENDING:
|
|
|
|
{
|
2016-12-08 22:05:39 +03:00
|
|
|
mEvent.Wait();
|
2016-09-30 19:51:01 +03:00
|
|
|
}
|
|
|
|
if (!GetOverlappedResult(mPipe, &mOverlapped, &bytesWritten, FALSE)) {
|
2016-11-29 21:53:48 +03:00
|
|
|
ADD_FAILURE() << "GetOverlappedResult failed";
|
2016-09-30 19:51:01 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (mBytesTransferred != bytesWritten) {
|
2016-11-29 21:53:48 +03:00
|
|
|
ADD_FAILURE() << "GetOverlappedResult mismatch";
|
2016-09-30 19:51:01 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
default:
|
2016-11-29 21:53:48 +03:00
|
|
|
ADD_FAILURE() << "WriteFile error " << GetLastError();
|
2016-09-30 19:51:01 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
2016-12-08 22:05:39 +03:00
|
|
|
mEvent.Wait();
|
2016-09-30 19:51:01 +03:00
|
|
|
|
|
|
|
if (mBytesTransferred != bytesWritten) {
|
2016-11-29 21:53:48 +03:00
|
|
|
ADD_FAILURE() << "GetOverlappedResult mismatch";
|
2016-09-30 19:51:01 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mBytesTransferred = 0;
|
|
|
|
return bytesWritten;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNamedPipeDataObserver::OnDataAvailable(uint32_t aBytesTransferred,
|
|
|
|
void *aOverlapped)
|
|
|
|
{
|
|
|
|
if (aOverlapped != &mOverlapped) {
|
2016-11-29 21:53:48 +03:00
|
|
|
ADD_FAILURE() << "invalid overlapped object";
|
2016-09-30 19:51:01 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD bytesTransferred = 0;
|
|
|
|
BOOL ret = GetOverlappedResult(mPipe,
|
|
|
|
reinterpret_cast<LPOVERLAPPED>(aOverlapped),
|
|
|
|
&bytesTransferred,
|
|
|
|
FALSE);
|
|
|
|
|
|
|
|
if (!ret) {
|
2016-11-29 21:53:48 +03:00
|
|
|
ADD_FAILURE() << "GetOverlappedResult failed";
|
2016-09-30 19:51:01 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bytesTransferred != aBytesTransferred) {
|
2016-11-29 21:53:48 +03:00
|
|
|
ADD_FAILURE() << "GetOverlappedResult mismatch";
|
2016-09-30 19:51:01 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mBytesTransferred += aBytesTransferred;
|
2016-12-08 22:05:39 +03:00
|
|
|
mEvent.Set();
|
2016-09-30 19:51:01 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNamedPipeDataObserver::OnError(uint32_t aError, void *aOverlapped)
|
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL CreateAndConnectInstance(LPOVERLAPPED aOverlapped, LPHANDLE aPipe);
|
|
|
|
BOOL ConnectToNewClient(HANDLE aPipe, LPOVERLAPPED aOverlapped);
|
|
|
|
|
|
|
|
BOOL CreateAndConnectInstance(LPOVERLAPPED aOverlapped, LPHANDLE aPipe)
|
|
|
|
{
|
|
|
|
// FIXME: adjust parameters
|
|
|
|
*aPipe = CreateNamedPipeA(
|
|
|
|
PIPE_NAME,
|
|
|
|
PIPE_ACCESS_DUPLEX |
|
|
|
|
FILE_FLAG_OVERLAPPED,
|
|
|
|
PIPE_TYPE_MESSAGE |
|
|
|
|
PIPE_READMODE_MESSAGE |
|
|
|
|
PIPE_WAIT,
|
|
|
|
1,
|
|
|
|
65536,
|
|
|
|
65536,
|
|
|
|
3000,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
if (*aPipe == INVALID_HANDLE_VALUE) {
|
2016-11-29 21:53:48 +03:00
|
|
|
ADD_FAILURE() << "CreateNamedPipe failed " << GetLastError();
|
2016-09-30 19:51:01 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ConnectToNewClient(*aPipe, aOverlapped);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL ConnectToNewClient(HANDLE aPipe, LPOVERLAPPED aOverlapped)
|
|
|
|
{
|
|
|
|
if (ConnectNamedPipe(aPipe, aOverlapped)) {
|
2016-11-29 21:53:48 +03:00
|
|
|
ADD_FAILURE() << "Unexpected, overlapped ConnectNamedPipe() always returns 0.";
|
2016-09-30 19:51:01 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (GetLastError())
|
|
|
|
{
|
|
|
|
case ERROR_IO_PENDING:
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
case ERROR_PIPE_CONNECTED:
|
|
|
|
if (SetEvent(aOverlapped->hEvent))
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: // error
|
2016-11-29 21:53:48 +03:00
|
|
|
ADD_FAILURE() << "ConnectNamedPipe failed " << GetLastError();
|
2016-09-30 19:51:01 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static nsresult
|
|
|
|
CreateNamedPipe(LPHANDLE aServer, LPHANDLE aClient)
|
|
|
|
{
|
|
|
|
OVERLAPPED overlapped;
|
|
|
|
overlapped.hEvent = CreateEvent(NULL, TRUE, TRUE, NULL);
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
ret = CreateAndConnectInstance(&overlapped, aServer);
|
|
|
|
if (!ret) {
|
2016-11-29 21:53:48 +03:00
|
|
|
ADD_FAILURE() << "pipe server should be pending";
|
2016-09-30 19:51:01 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aClient = CreateFileA(PIPE_NAME,
|
|
|
|
GENERIC_READ | GENERIC_WRITE,
|
|
|
|
FILE_SHARE_READ | FILE_SHARE_WRITE,
|
|
|
|
nullptr,
|
|
|
|
OPEN_EXISTING,
|
|
|
|
FILE_FLAG_OVERLAPPED,
|
|
|
|
nullptr);
|
|
|
|
|
|
|
|
if (*aClient == INVALID_HANDLE_VALUE) {
|
2016-11-29 21:53:48 +03:00
|
|
|
ADD_FAILURE() << "Unable to create pipe client";
|
2016-09-30 19:51:01 +03:00
|
|
|
CloseHandle(*aServer);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD pipeMode = PIPE_READMODE_MESSAGE;
|
|
|
|
if (!SetNamedPipeHandleState(*aClient, &pipeMode, nullptr, nullptr)) {
|
2016-11-29 21:53:48 +03:00
|
|
|
ADD_FAILURE() << "SetNamedPipeHandleState error " << GetLastError();
|
2016-09-30 19:51:01 +03:00
|
|
|
CloseHandle(*aServer);
|
|
|
|
CloseHandle(*aClient);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
WaitForSingleObjectEx(overlapped.hEvent, INFINITE, TRUE);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-11-29 21:53:48 +03:00
|
|
|
TEST(TestNamedPipeService,Test)
|
2016-09-30 19:51:01 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsINamedPipeService> svc =
|
|
|
|
do_GetService(NS_NAMEDPIPESERVICE_CONTRACTID, &rv);
|
2016-11-29 21:53:48 +03:00
|
|
|
ASSERT_TRUE(NS_SUCCEEDED(rv));
|
2016-09-30 19:51:01 +03:00
|
|
|
|
|
|
|
HANDLE readPipe, writePipe;
|
2016-11-29 21:53:48 +03:00
|
|
|
rv = CreateNamedPipe(&readPipe, &writePipe);
|
|
|
|
ASSERT_TRUE(NS_SUCCEEDED(rv));
|
2016-09-30 19:51:01 +03:00
|
|
|
|
|
|
|
RefPtr<nsNamedPipeDataObserver> readObserver =
|
|
|
|
new nsNamedPipeDataObserver(readPipe);
|
|
|
|
RefPtr<nsNamedPipeDataObserver> writeObserver =
|
|
|
|
new nsNamedPipeDataObserver(writePipe);
|
|
|
|
|
2016-11-29 21:53:48 +03:00
|
|
|
ASSERT_TRUE(NS_SUCCEEDED(svc->AddDataObserver(readPipe, readObserver)));
|
|
|
|
ASSERT_TRUE(NS_SUCCEEDED(svc->AddDataObserver(writePipe, writeObserver)));
|
|
|
|
ASSERT_EQ(writeObserver->Write(TEST_STR, sizeof(TEST_STR)), sizeof(TEST_STR));
|
2016-09-30 19:51:01 +03:00
|
|
|
|
|
|
|
char buffer[sizeof(TEST_STR)];
|
2016-11-29 21:53:48 +03:00
|
|
|
ASSERT_EQ(readObserver->Read(buffer, sizeof(buffer)), sizeof(TEST_STR));
|
|
|
|
ASSERT_STREQ(buffer, TEST_STR) << "I/O mismatch";
|
2016-09-30 19:51:01 +03:00
|
|
|
|
2016-11-29 21:53:48 +03:00
|
|
|
ASSERT_TRUE(NS_SUCCEEDED(svc->RemoveDataObserver(readPipe, readObserver)));
|
|
|
|
ASSERT_TRUE(NS_SUCCEEDED(svc->RemoveDataObserver(writePipe, writeObserver)));
|
|
|
|
}
|