forked from emscripten-core/emscripten
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipe.c
More file actions
142 lines (116 loc) · 3.58 KB
/
pipe.c
File metadata and controls
142 lines (116 loc) · 3.58 KB
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
/*
* Copyright 2017 The Emscripten Authors. All rights reserved.
* Emscripten is available under two separate licenses, the MIT license and the
* University of Illinois/NCSA Open Source License. Both these licenses can be
* found in the LICENSE file.
*/
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <poll.h>
#include <errno.h>
#include <assert.h>
#include <string.h>
#include <stdio.h>
unsigned char buf[1 << 16];
#define FALSE 0
#define TRUE 1
// This test program relies on the simplest read/write behavior when
// all the data can be read/written in one call.
void test_write(int fd1, unsigned char *ch, int size)
{
memset(buf, 0, sizeof buf);
for(int i = 0; i < size; ++i)
buf[i] = (*ch)++;
assert(write(fd1, buf, size) == size);
}
void test_read(int fd0, unsigned char *ch, int size)
{
memset(buf, 0, sizeof buf);
assert(read(fd0, buf, size) == size);
for(int i = 0; i < sizeof buf; ++i)
{
unsigned char correct_ch = (i < size) ? (*ch)++ : 0;
assert(buf[i] == correct_ch);
}
}
void test_poll(int *fd, int data_available)
{
struct pollfd pfds[2];
memset(pfds, 0, sizeof pfds);
pfds[0].fd = fd[0];
pfds[0].events = POLLIN | POLLOUT;
pfds[1].fd = fd[1];
pfds[1].events = POLLIN | POLLOUT;
int ret = poll(pfds, 2, 0);
if(data_available)
{
assert(ret == 2);
assert(pfds[0].revents == POLLIN);
}
else
{
assert(ret == 1);
assert(pfds[0].revents == 0);
}
assert(pfds[1].revents == POLLOUT);
}
int main()
{
int fd[2];
unsigned char wchar = 0;
unsigned char rchar = 0;
assert(pipe(fd) == 0);
// Test that pipe is not seekable
memset(buf, 0, sizeof buf);
assert(write(fd[1], buf, 128) == 128);
assert(lseek(fd[0], 0, SEEK_CUR) == -1);
assert(errno == ESPIPE);
assert(lseek(fd[1], 0, SEEK_CUR) == -1);
assert(errno == ESPIPE);
assert(read(fd[0], buf, sizeof buf) == 128);
// Now pipe is empty
// Test interleaved writing and reading of different buffer sizes
for(int i = 1; i < 200; ++i) // write about 40 Kb of data
{
test_write(fd[1], &wchar, i + 2);
test_poll(fd, TRUE);
test_read (fd[0], &rchar, i);
test_poll(fd, TRUE);
test_write(fd[1], &wchar, i + 1);
test_poll(fd, TRUE);
test_read (fd[0], &rchar, i + 3);
test_poll(fd, FALSE);
}
// Test reading when there is less data available than the read buffer size
assert(write(fd[1], buf, 10) == 10);
assert(read(fd[0], buf, sizeof buf) == 10);
// Write total of 1 Mb of data in small chunks
// The pipe should not overflow
int bytes_to_write = 1 << 20;
while(bytes_to_write > 0)
{
test_write(fd[1], &wchar, sizeof buf);
test_read (fd[0], &rchar, sizeof buf);
bytes_to_write -= sizeof buf;
}
// Write large chunks of data (supposed to be larger than one internal buffer)
test_write(fd[1], &wchar, 123);
test_write(fd[1], &wchar, (1 << 15) + 321);
test_write(fd[1], &wchar, 456);
test_read(fd[0], &rchar, 456);
test_read(fd[0], &rchar, (1 << 15) + 123);
test_read(fd[0], &rchar, 321);
// Test non-blocking read from empty pipe
assert(fcntl(fd[0], F_SETFL, O_NONBLOCK) == 0);
assert(read(fd[0], buf, sizeof buf) == -1);
assert(errno == EAGAIN);
// Normal operations still work in non-blocking mode
test_poll(fd, FALSE);
test_write(fd[1], &wchar, 10);
test_poll(fd, TRUE);
test_read (fd[0], &rchar, 10);
test_poll(fd, FALSE);
puts("success");
return 0;
}