summaryrefslogtreecommitdiffstats
path: root/netwerk/test/TestNamedPipeService.cpp
blob: 7d3f2b58cb5a41929a3e72260555edf56d3a5c74 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/* -*- 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"
#include "TestHarness.h"

#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;

class nsNamedPipeDataObserver : public nsINamedPipeDataObserver
{
public:
  NS_DECL_THREADSAFE_ISUPPORTS
  NS_DECL_NSINAMEDPIPEDATAOBSERVER

  explicit nsNamedPipeDataObserver(HANDLE aPipe);

  int Read(void* aBuffer, uint32_t aSize);
  int Write(const void* aBuffer, uint32_t aSize);

  uint32_t Transferred() const { return mBytesTransferred; }

private:
  ~nsNamedPipeDataObserver() = default;

  HANDLE mPipe;
  OVERLAPPED mOverlapped;
  Atomic<uint32_t> mBytesTransferred;
  Monitor mMonitor;
};

NS_IMPL_ISUPPORTS(nsNamedPipeDataObserver, nsINamedPipeDataObserver)

nsNamedPipeDataObserver::nsNamedPipeDataObserver(HANDLE aPipe)
  : mPipe(aPipe)
  , mOverlapped()
  , mBytesTransferred(0)
  , mMonitor("named-pipe")
{
  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:
        {
          MonitorAutoLock lock(mMonitor);
          mMonitor.Wait();
        }
        if (!GetOverlappedResult(mPipe, &mOverlapped, &bytesRead, FALSE)) {
          fail("GetOverlappedResult failed");
          return -1;
        }
        if (mBytesTransferred != bytesRead) {
          fail("GetOverlappedResult mismatch");
          return -1;
        }

        break;
      default:
        fail("ReadFile error %d", GetLastError());
        return -1;
    }
  } else {
    MonitorAutoLock lock(mMonitor);
    mMonitor.Wait();

    if (mBytesTransferred != bytesRead) {
      fail("GetOverlappedResult mismatch");
      return -1;
    }
  }

  mBytesTransferred = 0;
  passed("[read] match");
  return bytesRead;
}

int
nsNamedPipeDataObserver::Write(const void* aBuffer, uint32_t aSize)
{
  DWORD bytesWritten = 0;
  if (!WriteFile(mPipe, aBuffer, aSize, &bytesWritten, &mOverlapped)) {
    switch(GetLastError()) {
      case ERROR_IO_PENDING:
        {
          MonitorAutoLock lock(mMonitor);
          mMonitor.Wait();
        }
        if (!GetOverlappedResult(mPipe, &mOverlapped, &bytesWritten, FALSE)) {
          fail("GetOverlappedResult failed");
          return -1;
        }
        if (mBytesTransferred != bytesWritten) {
          fail("GetOverlappedResult mismatch");
          return -1;
        }

        break;
      default:
        fail("WriteFile error %d", GetLastError());
        return -1;
    }
  } else {
    MonitorAutoLock lock(mMonitor);
    mMonitor.Wait();

    if (mBytesTransferred != bytesWritten) {
      fail("GetOverlappedResult mismatch");
      return -1;
    }
  }

  mBytesTransferred = 0;
  passed("[write] match");
  return bytesWritten;
}

NS_IMETHODIMP
nsNamedPipeDataObserver::OnDataAvailable(uint32_t aBytesTransferred,
                                         void *aOverlapped)
{
  if (aOverlapped != &mOverlapped) {
    fail("invalid overlapped object");
    return NS_ERROR_FAILURE;
  }

  DWORD bytesTransferred = 0;
  BOOL ret = GetOverlappedResult(mPipe,
                                 reinterpret_cast<LPOVERLAPPED>(aOverlapped),
                                 &bytesTransferred,
                                 FALSE);

  if (!ret) {
    fail("GetOverlappedResult failed");
    return NS_ERROR_FAILURE;
  }

  if (bytesTransferred != aBytesTransferred) {
    fail("GetOverlappedResult mismatch");
    return NS_ERROR_FAILURE;
  }

  mBytesTransferred += aBytesTransferred;
  MonitorAutoLock lock(mMonitor);
  mMonitor.Notify();

  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)
{
  if (!aPipe) {
    fail("Parameter aPipe is NULL\n");
    return FALSE;
  }

  // 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) {
    fail("CreateNamedPipe failed [%d]\n", GetLastError());
    return FALSE;
  }

  return ConnectToNewClient(*aPipe, aOverlapped);
}

BOOL ConnectToNewClient(HANDLE aPipe, LPOVERLAPPED aOverlapped)
{
  if (ConnectNamedPipe(aPipe, aOverlapped)) {
    fail("Unexpected, overlapped ConnectNamedPipe() always returns 0.\n");
    return FALSE;
  }

  switch (GetLastError())
  {
  case ERROR_IO_PENDING:
    return TRUE;

  case ERROR_PIPE_CONNECTED:
    if (SetEvent(aOverlapped->hEvent))
      break;

  default: // error
    fail("ConnectNamedPipe failed [%d]\n", GetLastError());
    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) {
    fail("pipe server should be pending");
    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) {
    fail("Unable to create pipe client");
    CloseHandle(*aServer);
    return NS_ERROR_FAILURE;
  }

  DWORD pipeMode = PIPE_READMODE_MESSAGE;
  if (!SetNamedPipeHandleState(*aClient, &pipeMode, nullptr, nullptr)) {
    fail("SetNamedPipeHandleState error (%d)", GetLastError());
    CloseHandle(*aServer);
    CloseHandle(*aClient);
    return NS_ERROR_FAILURE;
  }

  WaitForSingleObjectEx(overlapped.hEvent, INFINITE, TRUE);

  return NS_OK;
}

int
main(int32_t argc, char* argv[])
{
  ScopedXPCOM xpcom("NamedPipeService");
  if (xpcom.failed()) {
    fail("Unable to initalize XPCOM.");
    return -1;
  }

  nsresult rv;
  nsCOMPtr<nsINamedPipeService> svc =
    do_GetService(NS_NAMEDPIPESERVICE_CONTRACTID, &rv);
  if (NS_FAILED(rv)) {
    fail("Unable to create named pipe service");
    return -1;
  }

  HANDLE readPipe, writePipe;
  if (NS_FAILED(rv = CreateNamedPipe(&readPipe, &writePipe))) {
    fail("Unable to create pipes %d", GetLastError());
    return -1;
  }

  RefPtr<nsNamedPipeDataObserver> readObserver =
    new nsNamedPipeDataObserver(readPipe);
  RefPtr<nsNamedPipeDataObserver> writeObserver =
    new nsNamedPipeDataObserver(writePipe);

  if (NS_WARN_IF(NS_FAILED(svc->AddDataObserver(readPipe, readObserver)))) {
    fail("Unable to add read data observer");
    return -1;
  }

  if (NS_WARN_IF(NS_FAILED(svc->AddDataObserver(writePipe, writeObserver)))) {
    fail("Unable to add read data observer");
    return -1;
  }

  if (writeObserver->Write(TEST_STR, sizeof(TEST_STR)) != sizeof(TEST_STR)) {
    fail("write error");
    return -1;
  }

  char buffer[sizeof(TEST_STR)];
  if (readObserver->Read(buffer, sizeof(buffer)) != sizeof(TEST_STR)) {
    fail("read error");
    return -1;
  }

  if (strcmp(buffer, TEST_STR) != 0) {
    fail("I/O mismatch");
    return -1;
  }

  if (NS_WARN_IF(NS_FAILED(svc->RemoveDataObserver(readPipe, readObserver)))) {
    fail("Unable to remove read data observer");
    return -1;
  }

  if (NS_WARN_IF(NS_FAILED(svc->RemoveDataObserver(writePipe, writeObserver)))) {
    fail("Unable to remove read data observer");
    return -1;
  }

  passed("Finish");
  return 0;
}