Debian Codegraph @

crt/aws-c-io/tests/stream_test.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/testing/aws_test_harness.h>
7
8 #include <aws/common/file.h>
9 #include <aws/common/string.h>
10 #include <aws/io/stream.h>
11
12 #include <sys/stat.h>
13
14 #ifdef _WIN32
15 # include <io.h>
16 #endif
17
18 AWS_STATIC_STRING_FROM_LITERAL(s_simple_test, "SimpleTest");
19
20 /* 0x1A represents the Windows end-of-file character. Having this in the test data set allows us to verify that file
21 * stream reads on binary files do not terminate early on Windows.*/
22 const uint8_t s_simple_binary_test[] = {'a', 'b', 'c', 'd', 'e', 'f', 0x1A, 'g', 'h', 'i', 'j', 'k'};
23
24 static struct aws_input_stream *s_create_memory_stream(struct aws_allocator *allocator) {
25 struct aws_byte_cursor test_cursor = aws_byte_cursor_from_string(s_simple_test);
26 return aws_input_stream_new_from_cursor(allocator, &test_cursor);
27 }
28
29 static void s_destroy_memory_stream(struct aws_input_stream *stream) {
30 aws_input_stream_destroy(stream);
31 }
32
33 static struct aws_input_stream *s_create_file_stream(struct aws_allocator *allocator, const char *file_path) {
34 remove(file_path);
35
36 FILE *file = aws_fopen(file_path, "w");
37 fprintf(file, "%s", (char *)s_simple_test->bytes);
38 fclose(file);
39
40 return aws_input_stream_new_from_file(allocator, file_path);
41 }
42
43 static struct aws_input_stream *s_create_binary_file_stream(struct aws_allocator *allocator, const char *file_path) {
44 remove(file_path);
45
46 FILE *file = aws_fopen(file_path, "wb");
47 fwrite(s_simple_binary_test, sizeof(uint8_t), sizeof(s_simple_binary_test), file);
48 fclose(file);
49
50 return aws_input_stream_new_from_file(allocator, file_path);
51 }
52
53 static struct aws_input_stream *s_create_read_only_file_stream(struct aws_allocator *allocator, const char *file_path) {
54 remove(file_path);
55
56 FILE *file = aws_fopen(file_path, "w");
57 fprintf(file, "%s", (char *)s_simple_test->bytes);
58 fclose(file);
59 #ifdef _WIN32
60 if (_chmod(file_path, _S_IREAD)) {
61 return NULL;
62 }
63 #else
64 if (chmod(file_path, S_IRUSR | S_IRGRP | S_IROTH)) {
65 return NULL;
66 }
67 #endif
68 return aws_input_stream_new_from_file(allocator, file_path);
69 }
70
71 static void s_destroy_file_stream(struct aws_input_stream *stream, const char *file_path) {
72 aws_input_stream_destroy(stream);
73
74 remove(file_path);
75 }
76
77 static int s_do_simple_input_stream_test(
78 struct aws_input_stream *stream,
79 struct aws_allocator *allocator,
80 size_t read_buf_size,
81 struct aws_byte_cursor *expected_contents) {
82 struct aws_byte_buf read_buf;
83 aws_byte_buf_init(&read_buf, allocator, read_buf_size);
84
85 struct aws_byte_buf result_buf;
86 aws_byte_buf_init(&result_buf, allocator, 1024);
87
88 struct aws_stream_status status;
89 AWS_ZERO_STRUCT(status);
90
91 ASSERT_TRUE(aws_input_stream_get_status(stream, &status) == 0);
92 ASSERT_TRUE(status.is_end_of_stream == false);
93
94 while (!status.is_end_of_stream) {
95 const size_t starting_len = read_buf.len;
96 ASSERT_SUCCESS(aws_input_stream_read(stream, &read_buf));
97
98 if (starting_len - read_buf.len > 0) {
99 struct aws_byte_cursor dest_cursor = aws_byte_cursor_from_buf(&read_buf);
100 aws_byte_buf_append_dynamic(&result_buf, &dest_cursor);
101 }
102
103 read_buf.len = 0;
104
105 ASSERT_TRUE(aws_input_stream_get_status(stream, &status) == 0);
106 }
107
108 struct aws_byte_cursor result_cursor = aws_byte_cursor_from_buf(&result_buf);
109 ASSERT_TRUE(aws_byte_cursor_eq(expected_contents, &result_cursor));
110
111 aws_byte_buf_clean_up(&read_buf);
112 aws_byte_buf_clean_up(&result_buf);
113
114 return AWS_OP_SUCCESS;
115 }
116
117 static int s_test_input_stream_memory_simple(struct aws_allocator *allocator, void *ctx) {
118 (void)ctx;
119
120 struct aws_input_stream *stream = s_create_memory_stream(allocator);
121
122 struct aws_byte_cursor test_cursor = aws_byte_cursor_from_string(s_simple_test);
123 ASSERT_TRUE(s_do_simple_input_stream_test(stream, allocator, 100, &test_cursor) == AWS_OP_SUCCESS);
124
125 s_destroy_memory_stream(stream);
126
127 return AWS_OP_SUCCESS;
128 }
129
130 AWS_TEST_CASE(test_input_stream_memory_simple, s_test_input_stream_memory_simple);
131
132 static int s_test_input_stream_memory_iterate(struct aws_allocator *allocator, void *ctx) {
133 (void)ctx;
134
135 struct aws_input_stream *stream = s_create_memory_stream(allocator);
136
137 struct aws_byte_cursor test_cursor = aws_byte_cursor_from_string(s_simple_test);
138 ASSERT_TRUE(s_do_simple_input_stream_test(stream, allocator, 2, &test_cursor) == AWS_OP_SUCCESS);
139
140 s_destroy_memory_stream(stream);
141
142 return AWS_OP_SUCCESS;
143 }
144
145 AWS_TEST_CASE(test_input_stream_memory_iterate, s_test_input_stream_memory_iterate);
146
147 static int s_test_input_stream_file_simple(struct aws_allocator *allocator, void *ctx) {
148 (void)ctx;
149
150 const char *file_path = "test_input_stream_file_simple.txt"; /* unique name */
151 struct aws_input_stream *stream = s_create_file_stream(allocator, file_path);
152
153 struct aws_byte_cursor test_cursor = aws_byte_cursor_from_string(s_simple_test);
154 ASSERT_TRUE(s_do_simple_input_stream_test(stream, allocator, 100, &test_cursor) == AWS_OP_SUCCESS);
155
156 s_destroy_file_stream(stream, file_path);
157
158 return AWS_OP_SUCCESS;
159 }
160
161 AWS_TEST_CASE(test_input_stream_file_simple, s_test_input_stream_file_simple);
162
163 static int s_test_input_stream_file_iterate(struct aws_allocator *allocator, void *ctx) {
164 (void)ctx;
165
166 const char *file_path = "test_input_stream_file_iterate.txt"; /* unique name */
167 struct aws_input_stream *stream = s_create_file_stream(allocator, file_path);
168
169 struct aws_byte_cursor test_cursor = aws_byte_cursor_from_string(s_simple_test);
170 ASSERT_TRUE(s_do_simple_input_stream_test(stream, allocator, 2, &test_cursor) == AWS_OP_SUCCESS);
171
172 s_destroy_file_stream(stream, file_path);
173
174 return AWS_OP_SUCCESS;
175 }
176
177 AWS_TEST_CASE(test_input_stream_file_iterate, s_test_input_stream_file_iterate);
178
179 static int s_do_input_stream_seek_test(
180 struct aws_input_stream *stream,
181 struct aws_allocator *allocator,
182 int64_t offset,
183 enum aws_stream_seek_basis basis,
184 struct aws_byte_cursor *expected_contents) {
185 struct aws_byte_buf read_buf;
186 aws_byte_buf_init(&read_buf, allocator, 1024);
187
188 ASSERT_SUCCESS(aws_input_stream_seek(stream, offset, basis));
189
190 ASSERT_SUCCESS(aws_input_stream_read(stream, &read_buf));
191
192 struct aws_byte_cursor read_buf_cursor = aws_byte_cursor_from_buf(&read_buf);
193 ASSERT_TRUE(aws_byte_cursor_eq(expected_contents, &read_buf_cursor));
194
195 aws_byte_buf_clean_up(&read_buf);
196
197 return AWS_OP_SUCCESS;
198 }
199
200 #define SEEK_BEGINNING_OFFSET 5
201
202 static int s_test_input_stream_memory_seek_beginning(struct aws_allocator *allocator, void *ctx) {
203 (void)ctx;
204
205 struct aws_input_stream *stream = s_create_memory_stream(allocator);
206
207 struct aws_byte_cursor test_cursor = aws_byte_cursor_from_string(s_simple_test);
208 aws_byte_cursor_advance(&test_cursor, SEEK_BEGINNING_OFFSET);
209 ASSERT_TRUE(
210 s_do_input_stream_seek_test(stream, allocator, SEEK_BEGINNING_OFFSET, AWS_SSB_BEGIN, &test_cursor) ==
211 AWS_OP_SUCCESS);
212
213 s_destroy_memory_stream(stream);
214
215 return AWS_OP_SUCCESS;
216 }
217
218 AWS_TEST_CASE(test_input_stream_memory_seek_beginning, s_test_input_stream_memory_seek_beginning);
219
220 static int s_test_input_stream_file_seek_beginning(struct aws_allocator *allocator, void *ctx) {
221 (void)ctx;
222
223 const char *file_path = "test_input_stream_file_seek_beginning.txt"; /* unique name */
224 struct aws_input_stream *stream = s_create_file_stream(allocator, file_path);
225
226 struct aws_byte_cursor test_cursor = aws_byte_cursor_from_string(s_simple_test);
227 aws_byte_cursor_advance(&test_cursor, SEEK_BEGINNING_OFFSET);
228 ASSERT_TRUE(
229 s_do_input_stream_seek_test(stream, allocator, SEEK_BEGINNING_OFFSET, AWS_SSB_BEGIN, &test_cursor) ==
230 AWS_OP_SUCCESS);
231
232 s_destroy_file_stream(stream, file_path);
233
234 return AWS_OP_SUCCESS;
235 }
236
237 AWS_TEST_CASE(test_input_stream_file_seek_beginning, s_test_input_stream_file_seek_beginning);
238
239 #define SEEK_END_OFFSET (-3)
240
241 static int s_test_input_stream_memory_seek_end(struct aws_allocator *allocator, void *ctx) {
242 (void)ctx;
243
244 struct aws_input_stream *stream = s_create_memory_stream(allocator);
245
246 struct aws_byte_cursor test_cursor = aws_byte_cursor_from_string(s_simple_test);
247 aws_byte_cursor_advance(&test_cursor, (size_t)((int64_t)s_simple_test->len + SEEK_END_OFFSET));
248 ASSERT_TRUE(
249 s_do_input_stream_seek_test(stream, allocator, SEEK_END_OFFSET, AWS_SSB_END, &test_cursor) == AWS_OP_SUCCESS);
250
251 s_destroy_memory_stream(stream);
252
253 return AWS_OP_SUCCESS;
254 }
255
256 AWS_TEST_CASE(test_input_stream_memory_seek_end, s_test_input_stream_memory_seek_end);
257
258 static int s_test_input_stream_memory_seek_multiple_times(struct aws_allocator *allocator, void *ctx) {
259 (void)ctx;
260
261 const char *src_cstr = "0123456789";
262 const struct aws_byte_cursor src_cursor = aws_byte_cursor_from_c_str(src_cstr);
263 struct aws_input_stream *stream = aws_input_stream_new_from_cursor(allocator, &src_cursor);
264
265 char read_byte = 0;
266 struct aws_byte_buf read_buf = aws_byte_buf_from_empty_array(&read_byte, 1);
267
268 /* Seek to BEGIN + 2. Read "2" */
269 ASSERT_SUCCESS(aws_input_stream_seek(stream, 2, AWS_SSB_BEGIN));
270 read_buf.len = 0;
271 ASSERT_SUCCESS(aws_input_stream_read(stream, &read_buf));
272 ASSERT_INT_EQUALS('2', read_byte);
273
274 /* Seek to BEGIN + 4. Read "4" */
275 ASSERT_SUCCESS(aws_input_stream_seek(stream, 4, AWS_SSB_BEGIN));
276 read_buf.len = 0;
277 ASSERT_SUCCESS(aws_input_stream_read(stream, &read_buf));
278 ASSERT_INT_EQUALS('4', read_byte);
279
280 /* Seek to END - 1. Read "9" */
281 ASSERT_SUCCESS(aws_input_stream_seek(stream, -1, AWS_SSB_END));
282 read_buf.len = 0;
283 ASSERT_SUCCESS(aws_input_stream_read(stream, &read_buf));
284 ASSERT_INT_EQUALS('9', read_byte);
285
286 /* Seek to END - 5. Read "5" */
287 ASSERT_SUCCESS(aws_input_stream_seek(stream, -1, AWS_SSB_END));
288 read_buf.len = 0;
289 ASSERT_SUCCESS(aws_input_stream_read(stream, &read_buf));
290 ASSERT_INT_EQUALS('9', read_byte);
291
292 /* Seek to BEGIN + 0. Read "0" */
293 ASSERT_SUCCESS(aws_input_stream_seek(stream, 4, AWS_SSB_BEGIN));
294 read_buf.len = 0;
295 ASSERT_SUCCESS(aws_input_stream_read(stream, &read_buf));
296 ASSERT_INT_EQUALS('4', read_byte);
297
298 aws_input_stream_destroy(stream);
299 return AWS_OP_SUCCESS;
300 }
301
302 AWS_TEST_CASE(test_input_stream_memory_seek_multiple_times, s_test_input_stream_memory_seek_multiple_times);
303
304 static int s_test_input_stream_file_seek_end(struct aws_allocator *allocator, void *ctx) {
305 (void)ctx;
306
307 const char *file_path = "test_input_stream_file_seek_end.txt"; /* unique name */
308 struct aws_input_stream *stream = s_create_file_stream(allocator, file_path);
309
310 struct aws_byte_cursor test_cursor = aws_byte_cursor_from_string(s_simple_test);
311 aws_byte_cursor_advance(&test_cursor, (size_t)((int64_t)s_simple_test->len + SEEK_END_OFFSET));
312 ASSERT_TRUE(
313 s_do_input_stream_seek_test(stream, allocator, SEEK_END_OFFSET, AWS_SSB_END, &test_cursor) == AWS_OP_SUCCESS);
314
315 s_destroy_file_stream(stream, file_path);
316
317 return AWS_OP_SUCCESS;
318 }
319
320 AWS_TEST_CASE(test_input_stream_file_seek_end, s_test_input_stream_file_seek_end);
321
322 static int s_test_input_stream_memory_seek_past_end(struct aws_allocator *allocator, void *ctx) {
323 (void)ctx;
324
325 struct aws_input_stream *stream = s_create_memory_stream(allocator);
326
327 ASSERT_TRUE(aws_input_stream_seek(stream, 13, AWS_SSB_BEGIN) == AWS_OP_ERR);
328 ASSERT_TRUE(aws_last_error() == AWS_IO_STREAM_INVALID_SEEK_POSITION);
329
330 aws_reset_error();
331
332 ASSERT_TRUE(aws_input_stream_seek(stream, 1, AWS_SSB_END) == AWS_OP_ERR);
333 ASSERT_TRUE(aws_last_error() == AWS_IO_STREAM_INVALID_SEEK_POSITION);
334
335 s_destroy_memory_stream(stream);
336
337 return AWS_OP_SUCCESS;
338 }
339
340 AWS_TEST_CASE(test_input_stream_memory_seek_past_end, s_test_input_stream_memory_seek_past_end);
341
342 static int s_test_input_stream_memory_seek_before_start(struct aws_allocator *allocator, void *ctx) {
343 (void)ctx;
344
345 struct aws_input_stream *stream = s_create_memory_stream(allocator);
346
347 ASSERT_TRUE(aws_input_stream_seek(stream, -13, AWS_SSB_END) == AWS_OP_ERR);
348 ASSERT_TRUE(aws_last_error() == AWS_IO_STREAM_INVALID_SEEK_POSITION);
349
350 aws_reset_error();
351
352 ASSERT_TRUE(aws_input_stream_seek(stream, -1, AWS_SSB_BEGIN) == AWS_OP_ERR);
353 ASSERT_TRUE(aws_last_error() == AWS_IO_STREAM_INVALID_SEEK_POSITION);
354
355 s_destroy_memory_stream(stream);
356
357 return AWS_OP_SUCCESS;
358 }
359
360 AWS_TEST_CASE(test_input_stream_memory_seek_before_start, s_test_input_stream_memory_seek_before_start);
361
362 #define LENGTH_SEEK_OFFSET 3
363
364 static int s_test_input_stream_memory_length(struct aws_allocator *allocator, void *ctx) {
365 (void)ctx;
366
367 struct aws_input_stream *stream = s_create_memory_stream(allocator);
368
369 int64_t length = 0;
370 ASSERT_TRUE(aws_input_stream_get_length(stream, &length) == AWS_OP_SUCCESS);
371 ASSERT_TRUE(length == (int64_t)s_simple_test->len);
372
373 /* invariant under seeking */
374 aws_input_stream_seek(stream, LENGTH_SEEK_OFFSET, AWS_SSB_BEGIN);
375
376 ASSERT_TRUE(aws_input_stream_get_length(stream, &length) == AWS_OP_SUCCESS);
377 ASSERT_TRUE(length == (int64_t)s_simple_test->len);
378
379 s_destroy_memory_stream(stream);
380
381 return AWS_OP_SUCCESS;
382 }
383
384 AWS_TEST_CASE(test_input_stream_memory_length, s_test_input_stream_memory_length);
385
386 static int s_test_input_stream_file_length(struct aws_allocator *allocator, void *ctx) {
387 (void)ctx;
388
389 const char *file_path = "test_input_stream_file_length.txt"; /* unique name */
390 struct aws_input_stream *stream = s_create_file_stream(allocator, file_path);
391 int64_t length = 0;
392 ASSERT_TRUE(aws_input_stream_get_length(stream, &length) == AWS_OP_SUCCESS);
393 ASSERT_TRUE(length == (int64_t)s_simple_test->len);
394
395 /* invariant under seeking */
396 aws_input_stream_seek(stream, LENGTH_SEEK_OFFSET, AWS_SSB_BEGIN);
397
398 ASSERT_TRUE(aws_input_stream_get_length(stream, &length) == AWS_OP_SUCCESS);
399 ASSERT_TRUE(length == (int64_t)s_simple_test->len);
400
401 s_destroy_file_stream(stream, file_path);
402
403 return AWS_OP_SUCCESS;
404 }
405
406 AWS_TEST_CASE(test_input_stream_file_length, s_test_input_stream_file_length);
407
408 static int s_test_input_stream_binary(struct aws_allocator *allocator, void *ctx) {
409 (void)ctx;
410
411 const char *file_path = "test_input_stream_binary.dat"; /* unique name */
412 struct aws_input_stream *stream = s_create_binary_file_stream(allocator, file_path);
413
414 struct aws_byte_cursor test_cursor = {
415 .ptr = (uint8_t *)s_simple_binary_test,
416 .len = sizeof(s_simple_binary_test),
417 };
418
419 ASSERT_TRUE(s_do_simple_input_stream_test(stream, allocator, 100, &test_cursor) == AWS_OP_SUCCESS);
420
421 s_destroy_file_stream(stream, file_path);
422
423 return AWS_OP_SUCCESS;
424 }
425
426 AWS_TEST_CASE(test_input_stream_binary, s_test_input_stream_binary);
427
428 static int s_test_input_stream_read_only(struct aws_allocator *allocator, void *ctx) {
429 (void)ctx;
430
431 const char *file_path = "test_input_stream_read_only.txt"; /* unique name */
432 struct aws_input_stream *stream = s_create_read_only_file_stream(allocator, file_path);
433 ASSERT_NOT_NULL(stream);
434
435 struct aws_byte_cursor test_cursor = aws_byte_cursor_from_string(s_simple_test);
436 ASSERT_TRUE(s_do_simple_input_stream_test(stream, allocator, 100, &test_cursor) == AWS_OP_SUCCESS);
437
438 s_destroy_file_stream(stream, file_path);
439
440 return AWS_OP_SUCCESS;
441 }
442
443 AWS_TEST_CASE(test_input_stream_read_only, s_test_input_stream_read_only);