| 1 |
#include "s3_tester.h" |
| 2 |
#include <aws/io/stream.h> |
| 3 |
|
| 4 |
struct aws_s3_test_input_stream_impl { |
| 5 |
struct aws_input_stream base; |
| 6 |
size_t position; |
| 7 |
size_t length; |
| 8 |
struct aws_allocator *allocator; |
| 9 |
}; |
| 10 |
|
| 11 |
static int s_aws_s3_test_input_stream_seek( |
| 12 |
struct aws_input_stream *stream, |
| 13 |
int64_t offset, |
| 14 |
enum aws_stream_seek_basis basis) { |
| 15 |
(void)stream; |
| 16 |
(void)offset; |
| 17 |
(void)basis; |
| 18 |
|
| 19 |
|
| 20 |
aws_raise_error(AWS_ERROR_UNKNOWN); |
| 21 |
return AWS_OP_ERR; |
| 22 |
} |
| 23 |
|
| 24 |
static int s_aws_s3_test_input_stream_read( |
| 25 |
struct aws_input_stream *stream, |
| 26 |
struct aws_byte_buf *dest, |
| 27 |
struct aws_byte_cursor *test_string) { |
| 28 |
(void)stream; |
| 29 |
(void)dest; |
| 30 |
|
| 31 |
struct aws_s3_test_input_stream_impl *test_input_stream = |
| 32 |
AWS_CONTAINER_OF(stream, struct aws_s3_test_input_stream_impl, base); |
| 33 |
|
| 34 |
while (dest->len < dest->capacity && test_input_stream->position < test_input_stream->length) { |
| 35 |
size_t buffer_pos = test_input_stream->position % test_string->len; |
| 36 |
|
| 37 |
struct aws_byte_cursor source_byte_cursor = { |
| 38 |
.len = test_string->len - buffer_pos, |
| 39 |
.ptr = test_string->ptr + buffer_pos, |
| 40 |
}; |
| 41 |
|
| 42 |
size_t remaining_in_stream = test_input_stream->length - test_input_stream->position; |
| 43 |
if (remaining_in_stream < source_byte_cursor.len) { |
| 44 |
source_byte_cursor.len = remaining_in_stream; |
| 45 |
} |
| 46 |
|
| 47 |
size_t remaining_in_buffer = dest->capacity - dest->len; |
| 48 |
|
| 49 |
if (remaining_in_buffer < source_byte_cursor.len) { |
| 50 |
source_byte_cursor.len = remaining_in_buffer; |
| 51 |
} |
| 52 |
|
| 53 |
aws_byte_buf_append(dest, &source_byte_cursor); |
| 54 |
|
| 55 |
test_input_stream->position += source_byte_cursor.len; |
| 56 |
} |
| 57 |
|
| 58 |
return AWS_OP_SUCCESS; |
| 59 |
} |
| 60 |
|
| 61 |
static int s_aws_s3_test_input_stream_read_1(struct aws_input_stream *stream, struct aws_byte_buf *dest) { |
| 62 |
struct aws_byte_cursor test_string = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("This is an S3 test."); |
| 63 |
return s_aws_s3_test_input_stream_read(stream, dest, &test_string); |
| 64 |
} |
| 65 |
|
| 66 |
static int s_aws_s3_test_input_stream_read_2(struct aws_input_stream *stream, struct aws_byte_buf *dest) { |
| 67 |
struct aws_byte_cursor test_string = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Different S3 test value."); |
| 68 |
return s_aws_s3_test_input_stream_read(stream, dest, &test_string); |
| 69 |
} |
| 70 |
|
| 71 |
static int s_aws_s3_test_input_stream_get_status(struct aws_input_stream *stream, struct aws_stream_status *status) { |
| 72 |
(void)stream; |
| 73 |
(void)status; |
| 74 |
|
| 75 |
struct aws_s3_test_input_stream_impl *test_input_stream = |
| 76 |
AWS_CONTAINER_OF(stream, struct aws_s3_test_input_stream_impl, base); |
| 77 |
|
| 78 |
status->is_end_of_stream = test_input_stream->position == test_input_stream->length; |
| 79 |
status->is_valid = true; |
| 80 |
|
| 81 |
return AWS_OP_SUCCESS; |
| 82 |
} |
| 83 |
|
| 84 |
static int s_aws_s3_test_input_stream_get_length(struct aws_input_stream *stream, int64_t *out_length) { |
| 85 |
AWS_ASSERT(stream != NULL); |
| 86 |
struct aws_s3_test_input_stream_impl *test_input_stream = |
| 87 |
AWS_CONTAINER_OF(stream, struct aws_s3_test_input_stream_impl, base); |
| 88 |
*out_length = (int64_t)test_input_stream->length; |
| 89 |
return AWS_OP_SUCCESS; |
| 90 |
} |
| 91 |
|
| 92 |
static void s_aws_s3_test_input_stream_destroy(struct aws_s3_test_input_stream_impl *test_input_stream) { |
| 93 |
aws_mem_release(test_input_stream->allocator, test_input_stream); |
| 94 |
} |
| 95 |
|
| 96 |
static struct aws_input_stream_vtable s_aws_s3_test_input_stream_vtable_1 = { |
| 97 |
.seek = s_aws_s3_test_input_stream_seek, |
| 98 |
.read = s_aws_s3_test_input_stream_read_1, |
| 99 |
.get_status = s_aws_s3_test_input_stream_get_status, |
| 100 |
.get_length = s_aws_s3_test_input_stream_get_length, |
| 101 |
}; |
| 102 |
|
| 103 |
static struct aws_input_stream_vtable s_aws_s3_test_input_stream_vtable_2 = { |
| 104 |
.seek = s_aws_s3_test_input_stream_seek, |
| 105 |
.read = s_aws_s3_test_input_stream_read_2, |
| 106 |
.get_status = s_aws_s3_test_input_stream_get_status, |
| 107 |
.get_length = s_aws_s3_test_input_stream_get_length, |
| 108 |
}; |
| 109 |
|
| 110 |
struct aws_input_stream *aws_s3_test_input_stream_new_with_value_type( |
| 111 |
struct aws_allocator *allocator, |
| 112 |
size_t stream_length, |
| 113 |
enum aws_s3_test_stream_value stream_value) { |
| 114 |
|
| 115 |
struct aws_s3_test_input_stream_impl *test_input_stream = |
| 116 |
aws_mem_calloc(allocator, 1, sizeof(struct aws_s3_test_input_stream_impl)); |
| 117 |
|
| 118 |
test_input_stream->base.vtable = stream_value == TEST_STREAM_VALUE_1 ? &s_aws_s3_test_input_stream_vtable_1 |
| 119 |
: &s_aws_s3_test_input_stream_vtable_2; |
| 120 |
|
| 121 |
aws_ref_count_init( |
| 122 |
&test_input_stream->base.ref_count, |
| 123 |
test_input_stream, |
| 124 |
(aws_simple_completion_callback *)s_aws_s3_test_input_stream_destroy); |
| 125 |
|
| 126 |
struct aws_input_stream *input_stream = &test_input_stream->base; |
| 127 |
|
| 128 |
test_input_stream->position = 0; |
| 129 |
test_input_stream->length = stream_length; |
| 130 |
test_input_stream->allocator = allocator; |
| 131 |
|
| 132 |
return input_stream; |
| 133 |
} |
| 134 |
|
| 135 |
struct aws_input_stream *aws_s3_test_input_stream_new(struct aws_allocator *allocator, size_t stream_length) { |
| 136 |
return aws_s3_test_input_stream_new_with_value_type(allocator, stream_length, TEST_STREAM_VALUE_1); |
| 137 |
} |