Debian Codegraph @

crt/aws-c-io/include/aws/testing/stream_tester.h

raw symbols SCIP
1 #ifndef AWS_TESTING_STREAM_TESTER_H
2 #define AWS_TESTING_STREAM_TESTER_H
3
4 /**
5 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
6 * SPDX-License-Identifier: Apache-2.0.
7 */
8 #include <aws/io/stream.h>
9
10 #ifndef AWS_UNSTABLE_TESTING_API
11 # error This code is designed for use by AWS owned libraries for the AWS C99 SDK. \
12 You are welcome to use it, but we make no promises on the stability of this API. \
13 To enable use of this code, set the AWS_UNSTABLE_TESTING_API compiler flag.
14 #endif
15
16 /**
17 * Use aws_input_stream tester to test edge cases in systems that take input streams.
18 * You can make it behave in specific weird ways (e.g. fail on 3rd read).
19 *
20 * There are a few ways to set what gets streamed.
21 * - source_bytes: if set, stream these bytes.
22 * - source_stream: if set, wrap this stream (but insert weird behavior like failing on 3rd read).
23 * - autogen_length: autogen streaming content N bytes in length.
24 */
25
26 enum aws_autogen_style {
27 AWS_AUTOGEN_LOREM_IPSUM,
28 AWS_AUTOGEN_ALPHABET,
29 AWS_AUTOGEN_NUMBERS,
30 };
31
32 struct aws_input_stream_tester_options {
33 /* bytes to be streamed.
34 * the stream copies these to its own internal buffer.
35 * or you can set the autogen_length */
36 struct aws_byte_cursor source_bytes;
37
38 /* wrap another stream */
39 struct aws_input_stream *source_stream;
40
41 /* if non-zero, autogen streaming content N bytes in length */
42 size_t autogen_length;
43
44 /* style of contents (if using autogen) */
45 enum aws_autogen_style autogen_style;
46
47 /* if non-zero, read at most N bytes per read() */
48 size_t max_bytes_per_read;
49
50 /* if non-zero, read 0 bytes the Nth time read() is called */
51 size_t read_zero_bytes_on_nth_read;
52
53 /* If false, EOF is reported by the read() which produces the last few bytes.
54 * If true, EOF isn't reported until there's one more read(), producing zero bytes.
55 * This emulates an underlying stream that reports EOF by reading 0 bytes */
56 bool eof_requires_extra_read;
57
58 /* if non-zero, fail the Nth time read() is called, raising `fail_with_error_code` */
59 size_t fail_on_nth_read;
60
61 /* error-code to raise if failing on purpose */
62 int fail_with_error_code;
63 };
64
65 struct aws_input_stream_tester {
66 struct aws_input_stream base;
67 struct aws_allocator *alloc;
68 struct aws_input_stream_tester_options options;
69 struct aws_byte_buf source_buf;
70 struct aws_input_stream *source_stream;
71 size_t read_count;
72 bool num_bytes_last_read; /* number of bytes read in the most recent successful read() */
73 uint64_t total_bytes_read;
74 };
75
76 static inline int s_input_stream_tester_seek(
77 struct aws_input_stream *stream,
78 int64_t offset,
79 enum aws_stream_seek_basis basis) {
80
81 struct aws_input_stream_tester *impl = (struct aws_input_stream_tester *)stream->impl;
82 return aws_input_stream_seek(impl->source_stream, offset, basis);
83 }
84
85 static inline int s_input_stream_tester_read(struct aws_input_stream *stream, struct aws_byte_buf *original_dest) {
86 struct aws_input_stream_tester *impl = (struct aws_input_stream_tester *)stream->impl;
87
88 impl->read_count++;
89
90 /* if we're configured to fail, then do it */
91 if (impl->read_count == impl->options.fail_on_nth_read) {
92 AWS_FATAL_ASSERT(impl->options.fail_with_error_code != 0);
93 return aws_raise_error(impl->options.fail_with_error_code);
94 }
95
96 /* cap how much is read, if that's how we're configured */
97 size_t bytes_to_read = original_dest->capacity - original_dest->len;
98 if (impl->options.max_bytes_per_read != 0) {
99 bytes_to_read = aws_min_size(bytes_to_read, impl->options.max_bytes_per_read);
100 }
101
102 if (impl->read_count == impl->options.read_zero_bytes_on_nth_read) {
103 bytes_to_read = 0;
104 }
105
106 /* pass artificially capped buffer to actual stream */
107 struct aws_byte_buf capped_buf =
108 aws_byte_buf_from_empty_array(original_dest->buffer + original_dest->len, bytes_to_read);
109
110 if (aws_input_stream_read(impl->source_stream, &capped_buf)) {
111 return AWS_OP_ERR;
112 }
113
114 size_t bytes_actually_read = capped_buf.len;
115 original_dest->len += bytes_actually_read;
116 impl->num_bytes_last_read = bytes_actually_read;
117 impl->total_bytes_read += bytes_actually_read;
118
119 return AWS_OP_SUCCESS;
120 }
121
122 static inline int s_input_stream_tester_get_status(struct aws_input_stream *stream, struct aws_stream_status *status) {
123 struct aws_input_stream_tester *impl = (struct aws_input_stream_tester *)stream->impl;
124 if (aws_input_stream_get_status(impl->source_stream, status)) {
125 return AWS_OP_ERR;
126 }
127
128 /* if we're emulating a stream that requires an additional 0 byte read to realize it's EOF */
129 if (impl->options.eof_requires_extra_read) {
130 if (impl->num_bytes_last_read > 0) {
131 status->is_end_of_stream = false;
132 }
133 }
134
135 return AWS_OP_SUCCESS;
136 }
137
138 static inline int s_input_stream_tester_get_length(struct aws_input_stream *stream, int64_t *out_length) {
139 struct aws_input_stream_tester *impl = (struct aws_input_stream_tester *)stream->impl;
140 return aws_input_stream_get_length(impl->source_stream, out_length);
141 }
142
143 static struct aws_input_stream_vtable s_input_stream_tester_vtable = {
144 .seek = s_input_stream_tester_seek,
145 .read = s_input_stream_tester_read,
146 .get_status = s_input_stream_tester_get_status,
147 .get_length = s_input_stream_tester_get_length,
148 };
149
150 /* init byte-buf and fill it autogenned content */
151 static inline void s_byte_buf_init_autogenned(
152 struct aws_byte_buf *buf,
153 struct aws_allocator *alloc,
154 size_t length,
155 enum aws_autogen_style style) {
156
157 aws_byte_buf_init(buf, alloc, length);
158 struct aws_byte_cursor pattern = {0};
159 switch (style) {
160 case AWS_AUTOGEN_LOREM_IPSUM:
161 pattern = aws_byte_cursor_from_c_str(
162 "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore "
163 "et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut "
164 "aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse "
165 "cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa "
166 "qui officia deserunt mollit anim id est laborum. ");
167 break;
168 case AWS_AUTOGEN_ALPHABET:
169 pattern = aws_byte_cursor_from_c_str("abcdefghijklmnopqrstuvwxyz");
170 break;
171 case AWS_AUTOGEN_NUMBERS:
172 pattern = aws_byte_cursor_from_c_str("1234567890");
173 break;
174 }
175
176 struct aws_byte_cursor pattern_cursor = {0};
177 while (buf->len < buf->capacity) {
178 if (pattern_cursor.len == 0) {
179 pattern_cursor = pattern;
180 }
181 aws_byte_buf_write_to_capacity(buf, &pattern_cursor);
182 }
183 }
184
185 static inline uint64_t aws_input_stream_tester_total_bytes_read(const struct aws_input_stream *stream) {
186 const struct aws_input_stream_tester *impl = (const struct aws_input_stream_tester *)stream->impl;
187 return impl->total_bytes_read;
188 }
189
190 static inline void s_input_stream_tester_destroy(void *user_data) {
191 struct aws_input_stream_tester *impl = (struct aws_input_stream_tester *)user_data;
192 aws_input_stream_release(impl->source_stream);
193 aws_byte_buf_clean_up(&impl->source_buf);
194 aws_mem_release(impl->alloc, impl);
195 }
196
197 static inline struct aws_input_stream *aws_input_stream_new_tester(
198 struct aws_allocator *alloc,
199 const struct aws_input_stream_tester_options *options) {
200
201 struct aws_input_stream_tester *impl =
202 (struct aws_input_stream_tester *)aws_mem_calloc(alloc, 1, sizeof(struct aws_input_stream_tester));
203 impl->base.impl = impl;
204 impl->base.vtable = &s_input_stream_tester_vtable;
205 aws_ref_count_init(&impl->base.ref_count, impl, s_input_stream_tester_destroy);
206 impl->alloc = alloc;
207 impl->options = *options;
208
209 if (options->source_stream != NULL) {
210 AWS_FATAL_ASSERT((options->autogen_length == 0) && (options->source_bytes.len == 0));
211 impl->source_stream = aws_input_stream_acquire(options->source_stream);
212 } else {
213 if (options->autogen_length > 0) {
214 AWS_FATAL_ASSERT(options->source_bytes.len == 0);
215 s_byte_buf_init_autogenned(&impl->source_buf, alloc, options->autogen_length, options->autogen_style);
216 } else {
217 aws_byte_buf_init_copy_from_cursor(&impl->source_buf, alloc, options->source_bytes);
218 }
219 struct aws_byte_cursor source_buf_cursor = aws_byte_cursor_from_buf(&impl->source_buf);
220 impl->source_stream = aws_input_stream_new_from_cursor(alloc, &source_buf_cursor);
221 AWS_FATAL_ASSERT(impl->source_stream);
222 }
223
224 return &impl->base;
225 }
226
227 #endif /* AWS_TESTING_STREAM_TESTER_H */