-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtest-fhandle.cpp
217 lines (167 loc) · 5.48 KB
/
test-fhandle.cpp
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
#include "unittestframework.h"
#include <signal.h>
#include <cpputils/fhandle.h>
#include <cpputils/fhandle.h>
#include <vector>
// TODO: this test just crashes directly on windows.
TEST_CASE("filehandle") {
#ifndef _WIN32
signal(SIGPIPE, SIG_IGN);
#endif
SECTION("construct") {
// check invalid fh
CHECK_THROWS( filehandle{-1} );
// will throw on close
#ifndef _WIN32
// not checking on windows, there an invalid filehandle is fatal.
CHECK_THROWS( filehandle{99}.close() );
#endif
// check default constructor
CHECK( filehandle{}.empty() );
CHECK_THROWS( filehandle{}.fh() );
// copy constructor
CHECK( filehandle{filehandle{}}.empty() );
#ifndef _WIN32
// not checking on windows, there an invalid filehandle is fatal.
CHECK_THROWS( filehandle{filehandle{99}}.close() );
filehandle f;
filehandle g;
// check assignment
CHECK_NOTHROW( g = f );
CHECK_THROWS( f = -1 );
CHECK_NOTHROW( f = 99 );
CHECK( f.fh() == 99 );
CHECK( 99 == f.fh() );
CHECK_NOTHROW( g = f );
CHECK( 99 == g.fh() );
CHECK_THROWS( f.close() ); // because 99 is not a valid fd.
CHECK_NOTHROW( g.close() ); // now g is also already closed
#endif
}
#ifndef _WIN32
// TODO - create tests which work on windows as well.
SECTION("pair") {
filehandle f;
filehandle g;
int fpair[2];
::pipe(fpair);
f = fpair[0];
g = fpair[1];
CHECK( g.write("abc", 3) == 3 );
CHECK( f.read(3) == std::vector<uint8_t>{ 'a', 'b', 'c' } );
g.close();
CHECK( f.read(3).empty() );
}
SECTION("closereader") {
int fpair[2];
::pipe(fpair);
filehandle f = fpair[0];
if (1) {
filehandle g = fpair[1];
// data can be written and read
CHECK( g.write("abc", 3) == 3 );
CHECK( f.read(3) == std::vector<uint8_t>{ 'a', 'b', 'c' } );
// pipe is unidirectional
#ifndef __FreeBSD__
// todo - why does this not throw on freebsd?
CHECK_THROWS( f.write("abc", 3) );
CHECK_THROWS( g.read(3) );
#endif
// leaving scope closes 'g'
}
if (1) {
// 'g' is now already closed
filehandle g = fpair[1];
// data can no longer be written and read
CHECK_THROWS( g.write("abc", 3) ); // writing fails, because g is closed.
// reading returns EOF / empty
CHECK( f.read(3).empty() );
// will throw since fh is already closed.
// need to explicitly close, to avoid an exception in
// the destructor when leaving scope.
CHECK_THROWS( g.close() );
}
}
SECTION("closewriter") {
int fpair[2];
::pipe(fpair);
filehandle g = fpair[1];
if (1) {
filehandle f = fpair[0];
// data can be written and read
CHECK( g.write("abc", 3) == 3 );
CHECK( f.read(3) == std::vector<uint8_t>{ 'a', 'b', 'c' } );
// pipe is unidirectional
#ifndef __FreeBSD__
// todo - why does this not throw on freebsd?
CHECK_THROWS( f.write("abc", 3) );
CHECK_THROWS( g.read(3) );
#endif
// leaving scope closes 'f'
}
if (1) {
// 'f' is now already closed
filehandle f = fpair[0];
// data can no longer be written and read
CHECK_THROWS( g.write("abc", 3) ); // writing will sigpipe
CHECK_THROWS( f.read(3) ); // should fail because f is closed.
// will throw since fh is already closed.
// need to explicitly close, to avoid an exception in
// the destructor when leaving scope.
CHECK_THROWS( f.close() );
}
}
SECTION("copyfh") {
int fpair[2];
::pipe(fpair);
filehandle f = fpair[0];
filehandle h = fpair[1];
if (1) {
// 'g' is a copy of 'h'
filehandle g = h;
// data can be written and read
CHECK( g.write("abc", 3) == 3 );
CHECK( f.read(3) == std::vector<uint8_t>{ 'a', 'b', 'c' } );
// leaving scope does not close 'g', since it is a copy of 'h'.
}
if (1) {
// 'g' is still open
filehandle g = h;
// data can still be written
CHECK( g.write("abc", 3) == 3 );
CHECK( f.read(3) == std::vector<uint8_t>{ 'a', 'b', 'c' } );
}
}
SECTION("overwritewriter") {
int fpair[2];
::pipe(fpair);
filehandle f = fpair[0];
filehandle g = fpair[1];
int fpair2[2];
::pipe(fpair2);
// overwrite 'g', closing the read end of the pipe.
g = fpair2[1];
// reading returns EOF
CHECK( f.read(3).empty() );
}
SECTION("overwritereader") {
int fpair[2];
::pipe(fpair);
filehandle f = fpair[0];
filehandle g = fpair[1];
int fpair2[2];
::pipe(fpair2);
// overwrite 'f', closing the read end of the pipe.
f = fpair2[0];
// reading returns EOF
CHECK_THROWS( g.write("abc", 3) ); // writing fails, because f is closed.
}
#endif
// TODO
// - read(first,last)
// - read(first,size)
// - read(vector)
// - read(span)
// - read(range)
//
}