summaryrefslogtreecommitdiff
path: root/tests/ce-createpipe.c
blob: b44784cde7a5d8b2f6639dc58c3b95c7743b2a92 (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
/* ce-createpipe.c - Test the W32CE CreatePipe implementation.
   Copyright (C) 2010 Free Software Foundation, Inc.

   This file is part of Assuan.

   Assuan is free software; you can redistribute it and/or modify it
   under the terms of the GNU Lesser General Public License as
   published by the Free Software Foundation; either version 3 of
   the License, or (at your option) any later version.

   Assuan is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <errno.h>

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#include "common.h"

#include "../src/assuan.h" /* We need the CreatePipe prototype.  */


static DWORD
reader_thread (void *arg)
{
  HANDLE hd = arg;
  DWORD nread;
  char buffer[16];

  for (;;)
    {
      if (!ReadFile (hd, buffer, sizeof buffer, &nread, FALSE))
	{
	  log_error ("reader: ReadFile failed: rc=%d\n", (int)GetLastError ());
	  break;
	}
      log_info ("reader: read %d bytes\n", (int)nread);
      log_printhex ("got: ", buffer, nread);
    }

  log_info ("reader: finished\n");
  CloseHandle (hd);
  return 0;
}


static DWORD
writer_thread (void *arg)
{
  HANDLE hd = arg;
  DWORD nwritten;
  int i = 0;
  int j;
  char buffer[20];
  int count;

  for (count=0; count < 30; count++)
    {
      for (j=0; j < sizeof buffer; j++)
        buffer[j] = i++;

      if (!WriteFile (hd, buffer, sizeof buffer, &nwritten, NULL))
	{
	  log_error ("writer: WriteFile failed: rc=%d\n", (int)GetLastError ());
	  break;
	}
      if (nwritten != sizeof buffer)
        log_info ("writer: wrote only %d bytes\n", (int)nwritten);
    }

  log_info ("writer: finished\n");
  CloseHandle (hd);
  return 0;
}



static void
run_test (void)
{
  HANDLE hd[2];
  HANDLE threads[2];

  if (!CreatePipe (&hd[0], &hd[1], NULL, 0))
    {
      log_error ("CreatePipe failed: rc=%d\n", (int)GetLastError ());
      return;
    }
  log_info ("pipe created read=%p write=%p\n", hd[0], hd[1]);
  
  threads[0] = CreateThread (NULL, 0, reader_thread, hd[0], 0, NULL);
  if (!threads[0])
    log_fatal ("error creating reader thread: rc=%d\n", (int)GetLastError ());
  else
    log_info ("reader thread created\n");
  threads[1] = CreateThread (NULL, 0, writer_thread, hd[1], 0, NULL);
  if (!threads[0])
    log_fatal ("error creating writer thread: rc=%d\n", (int)GetLastError ());
  else
    log_info ("writer thread created\n");

  switch (WaitForMultipleObjects (2, threads, FALSE, INFINITE))
    {
    case WAIT_OBJECT_0:
      log_info ("reader thread finished first\n");
      break;
    case WAIT_OBJECT_0 + 1:
      log_info ("writer thread finished first\n");
      break;
    default:
      log_error ("WFMO failed: rc=%d\n", (int)GetLastError ());
      break;
    }

  

  CloseHandle (threads[0]);
  CloseHandle (threads[1]);
}



/* 
     M A I N
 */
int 
main (int argc, char **argv)
{
  int last_argc = -1;

  if (argc)
    {
      log_set_prefix (*argv);
      argc--; argv++;
    }
  while (argc && last_argc != argc )
    {
      last_argc = argc;
      if (!strcmp (*argv, "--help"))
        {
          printf ("usage: %s [options]\n"
                  "\n"
                  "Options:\n"
                  "  --verbose      Show what is going on\n",
                  log_get_prefix ());
          exit (0);
        }
      if (!strcmp (*argv, "--verbose"))
        {
          verbose = 1;
          argc--; argv++;
        }
      else if (!strcmp (*argv, "--debug"))
        {
          verbose = debug = 1;
          argc--; argv++;
        }
    }

  run_test ();

  return errorcount ? 1 : 0;
}