5 #include "bytesource.h"
8 struct custom_read_userdata {
13 ssize_t custom_read(void* userdata, uint8_t* buf, size_t count)
15 struct custom_read_userdata* ud = (struct custom_read_userdata*)userdata;
16 size_t num_to_read = (ud->bytes_left > count ? count : ud->bytes_left);
17 memcpy(buf, ud->bytes, num_to_read);
18 ud->bytes += num_to_read;
19 ud->bytes_left -= num_to_read;
23 ssize_t custom_read_slow(void* userdata, uint8_t* buf, size_t count)
25 struct custom_read_userdata* ud = (struct custom_read_userdata*)userdata;
26 size_t num_to_read = ((count > 0 && ud->bytes_left > 0) ? 1 : 0);
27 memcpy(buf, ud->bytes, num_to_read);
28 ud->bytes += num_to_read;
29 ud->bytes_left -= num_to_read;
33 // Two streams, separated by a marker.
34 uint8_t stream_bytes[] = {
35 0x01, 0x02, 0x03, 0xff, 0x00, 0x04, // some bytes
36 0xff, 0xf7, // a marker
37 0x05, 0x06, 0x07, 0x08, 0xff, 0x00 // more bytes
41 uint8_t first_stream[] = {
42 0x01, 0x02, 0x03, 0xff, 0x04
44 uint8_t second_stream[] = {
45 0x05, 0x06, 0x07, 0x08, 0xff
48 // Reading with a regular source.
49 void test_basic_reading()
51 struct custom_read_userdata ud;
52 ud.bytes = stream_bytes;
53 ud.bytes_left = sizeof(stream_bytes);
55 struct byte_source source;
56 init_byte_source(&source, custom_read, &ud);
58 // Read the first stream.
61 ret = byte_source_input_func(&source, buf, 4096);
62 assert(ret == sizeof(first_stream));
63 assert(memcmp(buf, first_stream, sizeof(first_stream)) == 0);
65 // Now we should get EOF.
66 ret = byte_source_input_func(&source, buf, 4096);
70 uint8_t marker = byte_source_read_marker(&source);
71 assert(marker == 0xf7);
73 // Read the second stream.
74 ret = byte_source_input_func(&source, buf, 4096);
75 assert(ret == sizeof(second_stream));
76 assert(memcmp(buf, second_stream, sizeof(second_stream)) == 0);
79 ret = byte_source_input_func(&source, buf, 4096);
83 // Reading with a slow source.
84 void test_slow_source()
86 struct custom_read_userdata ud;
87 ud.bytes = stream_bytes;
88 ud.bytes_left = sizeof(stream_bytes);
90 struct byte_source source;
91 init_byte_source(&source, custom_read_slow, &ud);
96 // Read the first stream. Since the source is slow, we'll get one by
97 // one byte, even though we asked for 4096.
98 for (unsigned i = 0; i < sizeof(first_stream); ++i) {
99 ret = byte_source_input_func(&source, buf, 4096);
101 assert(buf[0] == first_stream[i]);
104 // Now we should get EOF.
105 ret = byte_source_input_func(&source, buf, 4096);
109 uint8_t marker = byte_source_read_marker(&source);
110 assert(marker == 0xf7);
112 // Read the second stream.
113 for (unsigned i = 0; i < sizeof(second_stream); ++i) {
116 ret = byte_source_input_func(&source, buf, 4096);
118 assert(buf[0] == second_stream[i]);
122 ret = byte_source_input_func(&source, buf, 4096);
126 // Data with a truncated marker should first give the first bytes, then failure
127 // on next read since the stream EOFs.
128 void test_broken_marker()
130 uint8_t bytes[] = { 0x01, 0x02, 0x03, 0x04, 0xff };
131 uint8_t expected_bytes[] = { 0x01, 0x02, 0x03, 0x04 };
133 struct custom_read_userdata ud;
135 ud.bytes_left = sizeof(bytes);
137 struct byte_source source;
138 init_byte_source(&source, custom_read, &ud);
143 ret = byte_source_input_func(&source, buf, 4096);
144 assert(ret == sizeof(expected_bytes));
145 assert(memcmp(buf, expected_bytes, sizeof(expected_bytes)) == 0);
147 ret = byte_source_input_func(&source, buf, 4096);
151 // Testing small reads -- even with a fast source, we shouldn't get more data
152 // back than we asked for.
153 void test_small_reads()
155 uint8_t bytes[] = { 0xff, 0x80, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
157 struct custom_read_userdata ud;
159 ud.bytes_left = sizeof(bytes);
161 struct byte_source source;
162 init_byte_source(&source, custom_read, &ud);
167 ret = byte_source_input_func(&source, buf, 4096);
170 uint8_t marker = byte_source_read_marker(&source);
171 assert(marker == 0x80);
173 for (unsigned i = 0; i < 8; ++i) {
174 ret = byte_source_input_func(&source, buf, 1);
180 ret = byte_source_input_func(&source, buf, 4096);
188 printf("test_basic_reading()\n");
189 test_basic_reading();
191 printf("test_slow_source()\n");
194 printf("test_broken_marker()\n");
195 test_broken_marker();
197 printf("test_small_reads()\n");
200 printf("All tests pass.\n");