]> git.sesse.net Git - fjl/blobdiff - bytesource_test.c
Fix a bug where we could return too much data from the byte source. Add test.
[fjl] / bytesource_test.c
index 79c242ad7d5c8df6d6b36996c2cdcb988c7e4e6b..36c6da2926a2863c049d7fc5f4b6b41436da888d 100644 (file)
@@ -31,7 +31,7 @@ ssize_t custom_read_slow(void* userdata, uint8_t* buf, size_t count)
 }
 
 // Two streams, separated by a marker.
-uint8_t bytes[] = {
+uint8_t stream_bytes[] = {
        0x01, 0x02, 0x03, 0xff, 0x00, 0x04,     // some bytes
        0xff, 0xf7,                             // a marker
        0x05, 0x06, 0x07, 0x08, 0xff, 0x00      // more bytes
@@ -49,8 +49,8 @@ uint8_t second_stream[] = {
 void test_basic_reading()
 {
        struct custom_read_userdata ud;
-       ud.bytes = bytes;
-       ud.bytes_left = sizeof(bytes);
+       ud.bytes = stream_bytes;
+       ud.bytes_left = sizeof(stream_bytes);
 
        struct byte_source source;
        init_byte_source(&source, custom_read, &ud);
@@ -84,8 +84,8 @@ void test_basic_reading()
 void test_slow_source()
 {
        struct custom_read_userdata ud;
-       ud.bytes = bytes;
-       ud.bytes_left = sizeof(bytes);
+       ud.bytes = stream_bytes;
+       ud.bytes_left = sizeof(stream_bytes);
 
        struct byte_source source;
        init_byte_source(&source, custom_read_slow, &ud);
@@ -123,6 +123,59 @@ void test_slow_source()
        assert(ret == 0);
 }
 
+// Data with a truncated marker should first give the first bytes, then failure
+// on next read since the stream EOFs.
+void test_broken_marker()
+{
+       uint8_t bytes[] = { 0x01, 0x02, 0x03, 0x04, 0xff };
+       uint8_t expected_bytes[] = { 0x01, 0x02, 0x03, 0x04 };
+
+       struct custom_read_userdata ud;
+       ud.bytes = bytes;
+       ud.bytes_left = sizeof(bytes);
+
+       struct byte_source source;
+       init_byte_source(&source, custom_read, &ud);
+       
+       uint8_t buf[4096];
+       ssize_t ret;
+
+       ret = byte_source_input_func(&source, buf, 4096);
+       assert(ret == sizeof(expected_bytes));
+       assert(memcmp(buf, expected_bytes, sizeof(expected_bytes)) == 0);
+
+       ret = byte_source_input_func(&source, buf, 4096);
+       assert(ret == -1);
+}
+
+// Testing small reads -- even with a fast source, we shouldn't get more data
+// back than we asked for.
+void test_small_reads()
+{
+       uint8_t bytes[] = { 0xff, 0x80, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
+
+       struct custom_read_userdata ud;
+       ud.bytes = bytes;
+       ud.bytes_left = sizeof(bytes);
+
+       struct byte_source source;
+       init_byte_source(&source, custom_read, &ud);
+
+       uint8_t marker = byte_source_read_marker(&source);
+       assert(marker == 0x80);
+
+       uint8_t buf[4096];
+       for (unsigned i = 0; i < 8; ++i) {
+               ssize_t ret = byte_source_input_func(&source, buf, 1);
+               assert(ret == 1);
+               assert(buf[0] == i);
+       }
+       
+       // Now EOF.
+       ssize_t ret = byte_source_input_func(&source, buf, 4096);
+       assert(ret == 0);
+}
+
 int main(void)
 {
        init_choices();
@@ -133,6 +186,12 @@ int main(void)
        printf("test_slow_source()\n");
        test_slow_source();
        
+       printf("test_broken_marker()\n");
+       test_broken_marker();
+       
+       printf("test_small_reads()\n");
+       test_small_reads();
+       
        printf("All tests pass.\n");
        return 0;
 }