-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathStreamTest.cpp
212 lines (151 loc) · 7.12 KB
/
StreamTest.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
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#include <aws/crt/Api.h>
#include <aws/crt/io/Stream.h>
#include <aws/common/byte_buf.h>
#include <aws/io/stream.h>
#include <aws/testing/aws_test_harness.h>
#include <sstream>
static int s_StreamTestCreateDestroyWrapper(struct aws_allocator *allocator, void *ctx)
{
(void)ctx;
{
Aws::Crt::ApiHandle apiHandle(allocator);
auto stringStream = Aws::Crt::MakeShared<std::stringstream>(allocator, "SomethingInteresting");
Aws::Crt::Io::StdIOStreamInputStream inputStream(stringStream, allocator);
ASSERT_TRUE(static_cast<bool>(inputStream));
ASSERT_NOT_NULL(inputStream.GetUnderlyingStream());
}
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(StreamTestCreateDestroyWrapper, s_StreamTestCreateDestroyWrapper)
static const char *STREAM_CONTENTS = "SomeContents";
static int s_StreamTestLength(struct aws_allocator *allocator, void *ctx)
{
(void)ctx;
{
Aws::Crt::ApiHandle apiHandle(allocator);
auto stringStream = Aws::Crt::MakeShared<std::stringstream>(allocator, STREAM_CONTENTS);
Aws::Crt::Io::StdIOStreamInputStream wrappedStream(stringStream, allocator);
int64_t length = 0;
ASSERT_SUCCESS(aws_input_stream_get_length(wrappedStream.GetUnderlyingStream(), &length));
ASSERT_TRUE(static_cast<uint64_t>(length) == strlen(STREAM_CONTENTS));
}
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(StreamTestLength, s_StreamTestLength)
static int s_StreamTestRead(struct aws_allocator *allocator, void *ctx)
{
(void)ctx;
{
Aws::Crt::ApiHandle apiHandle(allocator);
auto stringStream = Aws::Crt::MakeShared<Aws::Crt::StringStream>(allocator, STREAM_CONTENTS);
Aws::Crt::Io::StdIOStreamInputStream wrappedStream(stringStream, allocator);
aws_byte_buf buffer;
AWS_ZERO_STRUCT(buffer);
aws_byte_buf_init(&buffer, allocator, 256);
ASSERT_SUCCESS(aws_input_stream_read(wrappedStream.GetUnderlyingStream(), &buffer));
ASSERT_TRUE(buffer.len == strlen(STREAM_CONTENTS));
ASSERT_BIN_ARRAYS_EQUALS(STREAM_CONTENTS, buffer.len, buffer.buffer, buffer.len);
aws_byte_buf_clean_up(&buffer);
}
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(StreamTestRead, s_StreamTestRead)
static int s_StreamTestReadEmpty(struct aws_allocator *allocator, void *ctx)
{
(void)ctx;
{
Aws::Crt::ApiHandle apiHandle(allocator);
auto stringStream = Aws::Crt::MakeShared<Aws::Crt::StringStream>(allocator, "");
Aws::Crt::Io::StdIOStreamInputStream wrappedStream(stringStream, allocator);
aws_byte_buf buffer;
AWS_ZERO_STRUCT(buffer);
aws_byte_buf_init(&buffer, allocator, 256);
ASSERT_SUCCESS(aws_input_stream_read(wrappedStream.GetUnderlyingStream(), &buffer));
ASSERT_TRUE(buffer.len == 0);
aws_byte_buf_clean_up(&buffer);
}
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(StreamTestReadEmpty, s_StreamTestReadEmpty)
static const int64_t BEGIN_SEEK_OFFSET = 4;
static int s_StreamTestSeekBegin(struct aws_allocator *allocator, void *ctx)
{
(void)ctx;
{
Aws::Crt::ApiHandle apiHandle(allocator);
auto stringStream = Aws::Crt::MakeShared<Aws::Crt::StringStream>(allocator, STREAM_CONTENTS);
Aws::Crt::Io::StdIOStreamInputStream wrappedStream(stringStream, allocator);
ASSERT_SUCCESS(aws_input_stream_seek(wrappedStream.GetUnderlyingStream(), BEGIN_SEEK_OFFSET, AWS_SSB_BEGIN));
aws_byte_buf buffer;
AWS_ZERO_STRUCT(buffer);
aws_byte_buf_init(&buffer, allocator, 256);
ASSERT_SUCCESS(aws_input_stream_read(wrappedStream.GetUnderlyingStream(), &buffer));
ASSERT_TRUE(buffer.len == strlen(STREAM_CONTENTS) - BEGIN_SEEK_OFFSET);
ASSERT_BIN_ARRAYS_EQUALS(STREAM_CONTENTS + BEGIN_SEEK_OFFSET, buffer.len, buffer.buffer, buffer.len);
aws_byte_buf_clean_up(&buffer);
}
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(StreamTestSeekBegin, s_StreamTestSeekBegin)
static const int64_t END_SEEK_OFFSET = -4;
static int s_StreamTestSeekEnd(struct aws_allocator *allocator, void *ctx)
{
(void)ctx;
{
Aws::Crt::ApiHandle apiHandle(allocator);
auto stringStream = Aws::Crt::MakeShared<Aws::Crt::StringStream>(allocator, STREAM_CONTENTS);
Aws::Crt::Io::StdIOStreamInputStream wrappedStream(stringStream, allocator);
ASSERT_SUCCESS(aws_input_stream_seek(wrappedStream.GetUnderlyingStream(), END_SEEK_OFFSET, AWS_SSB_END));
aws_byte_buf buffer;
AWS_ZERO_STRUCT(buffer);
aws_byte_buf_init(&buffer, allocator, 256);
ASSERT_SUCCESS(aws_input_stream_read(wrappedStream.GetUnderlyingStream(), &buffer));
ASSERT_TRUE(buffer.len == -END_SEEK_OFFSET);
ASSERT_BIN_ARRAYS_EQUALS(
STREAM_CONTENTS + strlen(STREAM_CONTENTS) + END_SEEK_OFFSET, buffer.len, buffer.buffer, buffer.len);
aws_byte_buf_clean_up(&buffer);
}
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(StreamTestSeekEnd, s_StreamTestSeekEnd)
/* Test that C/C++ has the refcount on the stream will keep the object alive */
static int s_StreamTestRefcount(struct aws_allocator *allocator, void *ctx)
{
(void)ctx;
{
Aws::Crt::ApiHandle apiHandle(allocator);
aws_input_stream *c_stream = NULL;
{
auto stringStream = Aws::Crt::MakeShared<Aws::Crt::StringStream>(allocator, STREAM_CONTENTS);
/* Make a shared pointer for stream as the C side will ONLY interact with the shared pointer initialed
* stream */
std::shared_ptr<Aws::Crt::Io::StdIOStreamInputStream> wrappedStream =
Aws::Crt::MakeShared<Aws::Crt::Io::StdIOStreamInputStream>(allocator, stringStream, allocator);
/* C side keep a reference on it. */
aws_input_stream_acquire(wrappedStream->GetUnderlyingStream());
/* C side release a reference on it. So that it drops to zero from the C point of view, but as C++ still
* holding it, it's still valid to be used */
aws_input_stream_release(wrappedStream->GetUnderlyingStream());
/* Test that you can still use it correctly */
int64_t length = 0;
ASSERT_SUCCESS(aws_input_stream_get_length(wrappedStream->GetUnderlyingStream(), &length));
ASSERT_TRUE(static_cast<uint64_t>(length) == strlen(STREAM_CONTENTS));
/* C side keep a reference on it. */
aws_input_stream_acquire(wrappedStream->GetUnderlyingStream());
c_stream = wrappedStream->GetUnderlyingStream();
}
/* C++ object is now out of scope, but as C side still holding the reference to it, it still avaliable to be
* invoked from C */
int64_t length = 0;
ASSERT_SUCCESS(aws_input_stream_get_length(c_stream, &length));
ASSERT_TRUE(static_cast<uint64_t>(length) == strlen(STREAM_CONTENTS));
/* Release the refcount from C to clean up resource without leak */
aws_input_stream_release(c_stream);
}
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(StreamTestRefcount, s_StreamTestRefcount)