aboutsummaryrefslogtreecommitdiffstats
path: root/test/test_bytestream01.cpp
blob: 31b833ae135fab2b11898792d055681fa861cd90 (plain)
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
/*
 * Author: Sven Gothel <sgothel@jausoft.com>
 * Copyright (c) 2022 Gothel Software e.K.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include <iostream>
#include <cassert>
#include <cinttypes>
#include <cstring>

#include <fstream>
#include <iostream>

#include <jau/test/catch2_ext.hpp>

#include <jau/debug.hpp>
#include <jau/file_util.hpp>
#include <jau/byte_stream.hpp>
#include <jau/io_util.hpp>

extern "C" {
    #include <unistd.h>
}

using namespace jau::fractions_i64_literals;

class TestByteStream01 {
    private:
        const size_t IDX_11kiB = 0;
        const size_t IDX_65MiB = 1;
        static std::vector<std::string> fname_payload_lst;
        static std::vector<std::string> fname_payload_copy_lst;
        static std::vector<uint64_t> fname_payload_size_lst;
        const std::string url_input_root = "http://localhost:8080/";

        class data {
            private:
                static void add_test_file(const std::string name, const size_t size_limit) {
                    jau::fs::remove(name);
                    jau::fs::remove(name+".enc");
                    jau::fs::remove(name+".enc.dec");
                    jau::fs::remove(name+".copy");
                    size_t size;
                    {
                        static const std::string one_line = "Hello World, this is a test and I like it. Exactly 100 characters long. 0123456780 abcdefghjklmnop..";
                        std::ofstream ofs(name, std::ios::out | std::ios::binary);

                        REQUIRE( ofs.good() == true );
                        REQUIRE( ofs.is_open() == true );

                        for(size=0; size < size_limit; size+=one_line.size()) {
                            ofs.write(reinterpret_cast<const char*>(one_line.data()), one_line.size());
                        }
                        ofs.write("X", 1); // make it odd
                        size += 1;
                    }
                    fname_payload_lst.push_back(name);
                    fname_payload_copy_lst.push_back(name+".copy");
                    fname_payload_size_lst.push_back( size );
                }
                data() {
                    add_test_file("testfile_blob_01_11kiB.bin", 1024*11);
                    add_test_file("testfile_blob_02_65MiB.bin", 1024*1024*65);
                }
            public:
                static const data& get() {
                    static data instance;
                    return instance;
                }
        };

    public:
        TestByteStream01() {
            // produce fresh demo data once per whole test class
            const data& d = data::get();
            (void)d;
        }

        ~TestByteStream01() {
#ifdef USE_LIBCURL
            int res = std::system("killall mini_httpd");
            (void)res;
#endif // USE_LIBCURL
        }

        static void httpd_start() {
#ifdef USE_LIBCURL
            int res = std::system("killall mini_httpd");
            const std::string cwd = jau::fs::get_cwd();
            const std::string cmd = "/usr/sbin/mini_httpd -p 8080 -l "+cwd+"/mini_httpd.log";
            jau::PLAIN_PRINT(true, "%s", cmd.c_str());
            res = std::system(cmd.c_str());
            (void)res;
#endif // USE_LIBCURL
        }

        static bool transfer(jau::io::ByteInStream& input, const std::string& output_fname, const size_t buffer_size=4096) {
            const jau::fraction_timespec _t0 = jau::getMonotonicTime();
            jau::PLAIN_PRINT(true, "Transfer Start: %s", input.to_string().c_str());
            {
                const jau::fs::file_stats output_stats(output_fname);
                if( output_stats.exists() ) {
                    if( output_stats.is_file() ) {
                        if( !jau::fs::remove(output_fname) ) {
                            ERR_PRINT2("ByteStream copy failed: Failed deletion of existing output file %s", output_fname.c_str());
                            return false;
                        }
                    } else {
                        ERR_PRINT2("ByteStream copy failed: Not overwriting existing output file %s", output_fname.c_str());
                        return false;
                    }
                }
            }
            std::ofstream outfile(output_fname, std::ios::out | std::ios::binary);
            if ( !outfile.good() || !outfile.is_open() ) {
                ERR_PRINT2("ByteStream copy failed: Couldn't open output file %s", output_fname.c_str());
                return false;
            }

            uint64_t out_bytes_payload = 0;
            jau::io::StreamConsumerFunc consume_data = [&](jau::io::secure_vector<uint8_t>& data, bool is_final) -> bool {
                if( !is_final && ( !input.has_content_size() || out_bytes_payload + data.size() < input.content_size() ) ) {
                    outfile.write(reinterpret_cast<char*>(data.data()), data.size());
                    out_bytes_payload += data.size();
                    return true; // continue ..
                } else {
                    outfile.write(reinterpret_cast<char*>(data.data()), data.size());
                    out_bytes_payload += data.size();
                    return false; // EOS
                }
            };
            jau::io::secure_vector<uint8_t> io_buffer;
            io_buffer.reserve(buffer_size);
            const uint64_t in_bytes_total = jau::io::read_stream(input, io_buffer, consume_data);
            input.close();

            if ( 0==in_bytes_total || outfile.fail() ) {
                IRQ_PRINT("ByteStream copy failed: Output file write failed %s", output_fname.c_str());
                return false;
            }

            const jau::fraction_i64 _td = ( jau::getMonotonicTime() - _t0 ).to_fraction_i64();
            jau::io::print_stats("Transfer "+output_fname, out_bytes_payload, _td);
            jau::PLAIN_PRINT(true, "Transfer End: %s", input.to_string().c_str());

            return true;
        }

        void test00a_protocols_error() {
            jau::fprintf_td(stderr, "\n"); jau::fprintf_td(stderr, "%s\n", __func__);
            const bool http_support_expected = jau::io::uri_tk::protocol_supported("http:");
            const bool file_support_expected = jau::io::uri_tk::protocol_supported("file:");
            httpd_start();
            {
                std::vector<std::string_view> protos = jau::io::uri_tk::supported_protocols();
                jau::PLAIN_PRINT(true, "test00_protocols: Supported protocols: %zu: %s", protos.size(), jau::to_string(protos, ",").c_str());
                if( http_support_expected ) { // assume no http -> no curl
                    REQUIRE( 0 < protos.size() );
                } else {
                    REQUIRE( 0 == protos.size() );
                }
            }
            const size_t file_idx = IDX_11kiB;
            {
                const std::string url = "not_exiting_file.txt";
                REQUIRE( false == jau::io::uri_tk::is_local_file_protocol(url) );
                REQUIRE( false == jau::io::uri_tk::protocol_supported(url) );

                std::unique_ptr<jau::io::ByteInStream> in = jau::io::to_ByteInStream(url);
                if( nullptr != in ) {
                    jau::PLAIN_PRINT(true, "test00_protocols: not_exiting_file: %s", in->to_string().c_str());
                }
                REQUIRE( nullptr == in );
            }
            {
                const std::string url = "file://not_exiting_file_uri.txt";
                REQUIRE( true == jau::io::uri_tk::is_local_file_protocol(url) );
                REQUIRE( file_support_expected == jau::io::uri_tk::protocol_supported(url) );

                std::unique_ptr<jau::io::ByteInStream> in = jau::io::to_ByteInStream(url);
                if( nullptr != in ) {
                    jau::PLAIN_PRINT(true, "test00_protocols: not_exiting_file_uri: %s", in->to_string().c_str());
                }
                REQUIRE( nullptr == in );
            }
            {
                const std::string url = "lala://localhost:8080/" + fname_payload_lst[file_idx];
                REQUIRE( false == jau::io::uri_tk::is_local_file_protocol(url) );
                REQUIRE( false == jau::io::uri_tk::protocol_supported(url) );

                std::unique_ptr<jau::io::ByteInStream> in = jau::io::to_ByteInStream(url);
                if( nullptr != in ) {
                    jau::PLAIN_PRINT(true, "test00_protocols: not_exiting_protocol_uri: %s", in->to_string().c_str());
                }
                REQUIRE( nullptr == in );
            }
            {
                const std::string url = url_input_root + "not_exiting_http_uri.txt";
                REQUIRE( false == jau::io::uri_tk::is_local_file_protocol(url) );
                REQUIRE( http_support_expected == jau::io::uri_tk::protocol_supported(url) );

                std::unique_ptr<jau::io::ByteInStream> in = jau::io::to_ByteInStream(url);
                if( http_support_expected ) {
                    REQUIRE( nullptr != in );
                    jau::sleep_for( 100_ms ); // time to read 404 response
                    jau::PLAIN_PRINT(true, "test00_protocols: not_exiting_http_uri: %s", in->to_string().c_str());
                    REQUIRE( true == in->end_of_data() );
                    REQUIRE( true == in->error() );
                    REQUIRE( 0 == in->content_size() );
                } else {
                    REQUIRE( nullptr == in );
                }
            }
        }

        void test00b_protocols_ok() {
            jau::fprintf_td(stderr, "\n"); jau::fprintf_td(stderr, "%s\n", __func__);
            const bool http_support_expected = jau::io::uri_tk::protocol_supported("http:");
            const bool file_support_expected = jau::io::uri_tk::protocol_supported("file:");
            httpd_start();
            const size_t file_idx = IDX_11kiB;
            {
                const std::string url = fname_payload_lst[file_idx];
                REQUIRE( false == jau::io::uri_tk::is_local_file_protocol(url) );
                REQUIRE( false == jau::io::uri_tk::protocol_supported(url) );

                std::unique_ptr<jau::io::ByteInStream> in = jau::io::to_ByteInStream(url);
                if( nullptr != in ) {
                    jau::PLAIN_PRINT(true, "test00_protocols: local-file-0: %s", in->to_string().c_str());
                }
                REQUIRE( nullptr != in );
                REQUIRE( false == in->error() );

                bool res = transfer(*in, fname_payload_copy_lst[file_idx]);
                REQUIRE( true == res );

                jau::fs::file_stats out_stats(fname_payload_copy_lst[file_idx]);
                REQUIRE( true == out_stats.exists() );
                REQUIRE( true == out_stats.is_file() );
                REQUIRE( in->content_size() == out_stats.size() );
                REQUIRE( fname_payload_size_lst[file_idx] == out_stats.size() );
                REQUIRE( true == jau::fs::compare(in->id(), out_stats.path(), true /* verbose */) );
            }
            {
                const std::string url = "file://" + fname_payload_lst[file_idx];
                REQUIRE( true == jau::io::uri_tk::is_local_file_protocol(url) );
                REQUIRE( file_support_expected == jau::io::uri_tk::protocol_supported(url) );

                std::unique_ptr<jau::io::ByteInStream> in = jau::io::to_ByteInStream(url);
                if( nullptr != in ) {
                    jau::PLAIN_PRINT(true, "test00_protocols: local-file-1: %s", in->to_string().c_str());
                }
                REQUIRE( nullptr != in );
                REQUIRE( false == in->error() );

                bool res = transfer(*in, fname_payload_copy_lst[file_idx]);
                REQUIRE( true == res );

                jau::fs::file_stats out_stats(fname_payload_copy_lst[file_idx]);
                REQUIRE( true == out_stats.exists() );
                REQUIRE( true == out_stats.is_file() );
                REQUIRE( in->content_size() == out_stats.size() );
                REQUIRE( fname_payload_size_lst[file_idx] == out_stats.size() );
                REQUIRE( true == jau::fs::compare(fname_payload_lst[file_idx], out_stats.path(), true /* verbose */) );
            }
            {
                const std::string url = url_input_root + fname_payload_lst[file_idx];
                REQUIRE( false == jau::io::uri_tk::is_local_file_protocol(url) );
                REQUIRE( http_support_expected == jau::io::uri_tk::protocol_supported(url) );

                std::unique_ptr<jau::io::ByteInStream> in = jau::io::to_ByteInStream(url);
                if( nullptr != in ) {
                    jau::PLAIN_PRINT(true, "test00_protocols: http: %s", in->to_string().c_str());
                }
                if( http_support_expected ) {
                    REQUIRE( nullptr != in );
                    REQUIRE( false == in->error() );

                    bool res = transfer(*in, fname_payload_copy_lst[file_idx]);
                    REQUIRE( true == res );

                    jau::fs::file_stats out_stats(fname_payload_copy_lst[file_idx]);
                    REQUIRE( true == out_stats.exists() );
                    REQUIRE( true == out_stats.is_file() );
                    REQUIRE( in->content_size() == out_stats.size() );
                    REQUIRE( fname_payload_size_lst[file_idx] == out_stats.size() );
                    REQUIRE( true == jau::fs::compare(fname_payload_lst[file_idx], out_stats.path(), true /* verbose */) );
                } else {
                    REQUIRE( nullptr == in );
                }
            }
        }

        void test01_copy_file_ok_11kiB_buff4k() {
            jau::fprintf_td(stderr, "\n"); jau::fprintf_td(stderr, "%s\n", __func__);
            const size_t file_idx = IDX_11kiB;
            jau::io::ByteInStream_File data_stream(fname_payload_lst[file_idx], true /* use_binary */);

            bool res = transfer(data_stream, fname_payload_copy_lst[file_idx], 4096);
            REQUIRE( true == res );

            jau::fs::file_stats out_stats(fname_payload_copy_lst[file_idx]);
            REQUIRE( true == out_stats.exists() );
            REQUIRE( true == out_stats.is_file() );
            REQUIRE( data_stream.content_size() == out_stats.size() );
            REQUIRE( fname_payload_size_lst[file_idx] == out_stats.size() );
            REQUIRE( true == jau::fs::compare(data_stream.id(), out_stats.path(), true /* verbose */) );
        }

        void test02_copy_file_ok_65MiB_buff4k() {
            jau::fprintf_td(stderr, "\n"); jau::fprintf_td(stderr, "%s\n", __func__);
            const size_t file_idx = IDX_65MiB;
            jau::io::ByteInStream_File data_stream(fname_payload_lst[file_idx], true /* use_binary */);

            bool res = transfer(data_stream, fname_payload_copy_lst[file_idx], 4096);
            REQUIRE( true == res );

            jau::fs::file_stats out_stats(fname_payload_copy_lst[file_idx]);
            REQUIRE( true == out_stats.exists() );
            REQUIRE( true == out_stats.is_file() );
            REQUIRE( data_stream.content_size() == out_stats.size() );
            REQUIRE( fname_payload_size_lst[file_idx] == out_stats.size() );
            REQUIRE( true == jau::fs::compare(data_stream.id(), out_stats.path(), true /* verbose */) );
        }

        void test04_copy_file_ok_65MiB_buff32k() {
            jau::fprintf_td(stderr, "\n"); jau::fprintf_td(stderr, "%s\n", __func__);
            const size_t file_idx = IDX_65MiB;
            jau::io::ByteInStream_File data_stream(fname_payload_lst[file_idx], true /* use_binary */);

            bool res = transfer(data_stream, fname_payload_copy_lst[file_idx], 32768);
            REQUIRE( true == res );

            jau::fs::file_stats out_stats(fname_payload_copy_lst[file_idx]);
            REQUIRE( true == out_stats.exists() );
            REQUIRE( true == out_stats.is_file() );
            REQUIRE( data_stream.content_size() == out_stats.size() );
            REQUIRE( fname_payload_size_lst[file_idx] == out_stats.size() );
            REQUIRE( true == jau::fs::compare(data_stream.id(), out_stats.path(), true /* verbose */) );
        }

        void test11_copy_http_ok_buff32k() {
            jau::fprintf_td(stderr, "\n"); jau::fprintf_td(stderr, "%s\n", __func__);
            if( !jau::io::uri_tk::protocol_supported("http:") ) {
                jau::PLAIN_PRINT(true, "http not supported, abort\n");
                return;
            }
            httpd_start();
            {
                const size_t file_idx = IDX_11kiB;

                const std::string uri_original = url_input_root + fname_payload_lst[file_idx];

                jau::io::ByteInStream_URL data_stream(uri_original, 500_ms);

                bool res = transfer(data_stream, fname_payload_copy_lst[file_idx], 32768);
                REQUIRE( true == res );

                jau::fs::file_stats out_stats(fname_payload_copy_lst[file_idx]);
                REQUIRE( true == out_stats.exists() );
                REQUIRE( true == out_stats.is_file() );
                REQUIRE( data_stream.content_size() == out_stats.size() );
                REQUIRE( fname_payload_size_lst[file_idx] == out_stats.size() );
                REQUIRE( true == jau::fs::compare(fname_payload_lst[file_idx], out_stats.path(), true /* verbose */) );
            }
            {
                const size_t file_idx = IDX_65MiB;

                const std::string uri_original = url_input_root + fname_payload_lst[file_idx];

                jau::io::ByteInStream_URL data_stream(uri_original, 500_ms);

                bool res = transfer(data_stream, fname_payload_copy_lst[file_idx], 32768);
                REQUIRE( true == res );

                jau::fs::file_stats out_stats(fname_payload_copy_lst[file_idx]);
                REQUIRE( true == out_stats.exists() );
                REQUIRE( true == out_stats.is_file() );
                REQUIRE( data_stream.content_size() == out_stats.size() );
                REQUIRE( fname_payload_size_lst[file_idx] == out_stats.size() );
                REQUIRE( true == jau::fs::compare(fname_payload_lst[file_idx], out_stats.path(), true /* verbose */) );
            }
        }

        void test12_copy_http_404() {
            jau::fprintf_td(stderr, "\n"); jau::fprintf_td(stderr, "%s\n", __func__);
            if( !jau::io::uri_tk::protocol_supported("http:") ) {
                jau::PLAIN_PRINT(true, "http not supported, abort\n");
                return;
            }
            httpd_start();
            {
                const size_t file_idx = IDX_11kiB;

                const std::string uri_original = url_input_root + "doesnt_exists.txt";

                jau::io::ByteInStream_URL data_stream(uri_original, 500_ms);

                bool res = transfer(data_stream, fname_payload_copy_lst[file_idx]);
                REQUIRE( false == res );

                jau::fs::file_stats out_stats(fname_payload_copy_lst[file_idx]);
                REQUIRE( true == out_stats.exists() );
                REQUIRE( true == out_stats.is_file() );
                REQUIRE( data_stream.error() == true );
                REQUIRE( data_stream.has_content_size() == false );
                REQUIRE( data_stream.content_size() == 0 );
                REQUIRE( 0 == out_stats.size() );
            }
        }

        // throttled, no content size, interruptReader() via set_eof() will avoid timeout
        static void feed_source_00(jau::io::ByteInStream_Feed * data_feed, const size_t feed_size=1024) {
            uint64_t xfer_total = 0;
            jau::io::ByteInStream_File data_stream(data_feed->id(), true /* use_binary */);
            uint8_t buffer[feed_size];
            while( !data_stream.end_of_data() ) {
                size_t count = data_stream.read(buffer, sizeof(buffer));
                if( 0 < count ) {
                    xfer_total += count;
                    data_feed->write(buffer, count);
                    jau::sleep_for( 16_ms );
                }
            }
            // probably set after transfering due to above sleep, which also ends when total size has been reached.
            data_feed->set_eof( jau::io::async_io_result_t::SUCCESS );
            (void)xfer_total; // not used yet ..
        }

        // throttled, with content size
        static void feed_source_01(jau::io::ByteInStream_Feed * data_feed, const size_t feed_size=1024) {
            uint64_t xfer_total = 0;
            jau::io::ByteInStream_File data_stream(data_feed->id(), true /* use_binary */);
            const uint64_t file_size = data_stream.content_size();
            data_feed->set_content_size( file_size );
            uint8_t buffer[feed_size];
            while( !data_stream.end_of_data() && xfer_total < file_size ) {
                size_t count = data_stream.read(buffer, sizeof(buffer));
                if( 0 < count ) {
                    xfer_total += count;
                    data_feed->write(buffer, count);
                    jau::sleep_for( 16_ms );
                }
            }
            // probably set after transfering due to above sleep, which also ends when total size has been reached.
            data_feed->set_eof( xfer_total == file_size ? jau::io::async_io_result_t::SUCCESS : jau::io::async_io_result_t::FAILED );
        }

        // full speed, with content size
        static void feed_source_10(jau::io::ByteInStream_Feed * data_feed, const size_t feed_size=1024) {
            uint64_t xfer_total = 0;
            jau::io::ByteInStream_File data_stream(data_feed->id(), true /* use_binary */);
            const uint64_t file_size = data_stream.content_size();
            data_feed->set_content_size( data_stream.content_size() );
            uint8_t buffer[feed_size];
            while( !data_stream.end_of_data() && xfer_total < file_size ) {
                size_t count = data_stream.read(buffer, sizeof(buffer));
                if( 0 < count ) {
                    xfer_total += count;
                    data_feed->write(buffer, count);
                }
            }
            data_feed->set_eof( xfer_total == file_size ? jau::io::async_io_result_t::SUCCESS : jau::io::async_io_result_t::FAILED );
        }

        // full speed, no content size, interrupting @ 1024 bytes within our header
        static void feed_source_20(jau::io::ByteInStream_Feed * data_feed, const size_t feed_size=1024) {
            uint64_t xfer_total = 0;
            jau::io::ByteInStream_File data_stream(data_feed->id(), true /* use_binary */);
            uint8_t buffer[feed_size];
            while( !data_stream.end_of_data() ) {
                size_t count = data_stream.read(buffer, sizeof(buffer));
                if( 0 < count ) {
                    xfer_total += count;
                    data_feed->write(buffer, count);
                    if( xfer_total >= 1024 ) {
                        data_feed->set_eof( jau::io::async_io_result_t::FAILED ); // calls data_feed->interruptReader();
                        return;
                    }
                }
            }
            // probably set after transfering due to above sleep, which also ends when total size has been reached.
            data_feed->set_eof( jau::io::async_io_result_t::SUCCESS );
        }

        // full speed, with content size, interrupting 1/4 way
        static void feed_source_21(jau::io::ByteInStream_Feed * data_feed, const size_t feed_size=1024) {
            uint64_t xfer_total = 0;
            jau::io::ByteInStream_File data_stream(data_feed->id(), true /* use_binary */);
            const uint64_t file_size = data_stream.content_size();
            data_feed->set_content_size( data_stream.content_size() );
            uint8_t buffer[feed_size];
            while( !data_stream.end_of_data() ) {
                size_t count = data_stream.read(buffer, sizeof(buffer));
                if( 0 < count ) {
                    xfer_total += count;
                    data_feed->write(buffer, count);
                    if( xfer_total >= file_size/4 ) {
                        data_feed->set_eof( jau::io::async_io_result_t::FAILED ); // calls data_feed->interruptReader();
                        return;
                    }
                }
            }
            // probably set after transfering due to above sleep, which also ends when total size has been reached.
            data_feed->set_eof( jau::io::async_io_result_t::SUCCESS );
        }

        void test20_copy_fed_ok_buff4k_feed1k() {
            jau::fprintf_td(stderr, "\n"); jau::fprintf_td(stderr, "%s\n", __func__);
            const size_t buffer_size = 4096;
            const size_t feed_size = 1024;
            {
                const size_t file_idx = IDX_11kiB;
                {
                    // full speed, with content size
                    jau::io::ByteInStream_Feed data_feed(fname_payload_lst[file_idx], 500_ms);
                    std::thread feeder_thread= std::thread(&feed_source_10, &data_feed, feed_size);

                    bool res = transfer(data_feed, fname_payload_copy_lst[file_idx], buffer_size);
                    if( feeder_thread.joinable() ) {
                        feeder_thread.join();
                    }
                    REQUIRE( true == res );

                    jau::fs::file_stats out_stats(fname_payload_copy_lst[file_idx]);
                    REQUIRE( true == out_stats.exists() );
                    REQUIRE( true == out_stats.is_file() );
                    REQUIRE( data_feed.content_size() == out_stats.size() );
                    REQUIRE( fname_payload_size_lst[file_idx] == out_stats.size() );
                    REQUIRE( true == jau::fs::compare(data_feed.id(), out_stats.path(), true /* verbose */) );
                }
                {
                    // throttled, with content size
                    jau::io::ByteInStream_Feed data_feed(fname_payload_lst[file_idx], 500_ms);
                    std::thread feeder_thread= std::thread(&feed_source_01, &data_feed, feed_size);

                    bool res = transfer(data_feed, fname_payload_copy_lst[file_idx], buffer_size);
                    if( feeder_thread.joinable() ) {
                        feeder_thread.join();
                    }
                    REQUIRE( true == res );

                    jau::fs::file_stats out_stats(fname_payload_copy_lst[file_idx]);
                    REQUIRE( true == out_stats.exists() );
                    REQUIRE( true == out_stats.is_file() );
                    REQUIRE( data_feed.content_size() == out_stats.size() );
                    REQUIRE( fname_payload_size_lst[file_idx] == out_stats.size() );
                    REQUIRE( true == jau::fs::compare(data_feed.id(), out_stats.path(), true /* verbose */) );
                }
                {
                    // throttled, no content size, interruptReader() via set_eof() will avoid timeout
                    jau::io::ByteInStream_Feed data_feed(fname_payload_lst[file_idx], 500_ms);
                    std::thread feeder_thread= std::thread(&feed_source_00, &data_feed, feed_size);

                    bool res = transfer(data_feed, fname_payload_copy_lst[file_idx], buffer_size);
                    if( feeder_thread.joinable() ) {
                        feeder_thread.join();
                    }
                    REQUIRE( true == res );

                    jau::fs::file_stats out_stats(fname_payload_copy_lst[file_idx]);
                    REQUIRE( true == out_stats.exists() );
                    REQUIRE( true == out_stats.is_file() );
                    REQUIRE( data_feed.content_size() == 0 );
                    REQUIRE( fname_payload_size_lst[file_idx] == out_stats.size() );
                    REQUIRE( true == jau::fs::compare(data_feed.id(), out_stats.path(), true /* verbose */) );
                }
            }
            {
                const size_t file_idx = IDX_65MiB;
                {
                    // full speed, with content size
                    jau::io::ByteInStream_Feed data_feed(fname_payload_lst[file_idx], 500_ms);
                    std::thread feeder_thread= std::thread(&feed_source_10, &data_feed, feed_size);

                    bool res = transfer(data_feed, fname_payload_copy_lst[file_idx], buffer_size);
                    if( feeder_thread.joinable() ) {
                        feeder_thread.join();
                    }
                    REQUIRE( true == res );

                    jau::fs::file_stats out_stats(fname_payload_copy_lst[file_idx]);
                    REQUIRE( true == out_stats.exists() );
                    REQUIRE( true == out_stats.is_file() );
                    REQUIRE( data_feed.content_size() == out_stats.size() );
                    REQUIRE( fname_payload_size_lst[file_idx] == out_stats.size() );
                    REQUIRE( true == jau::fs::compare(data_feed.id(), out_stats.path(), true /* verbose */) );
                }
            }
        }

        void test21_copy_fed_ok_buff32k() {
            jau::fprintf_td(stderr, "\n"); jau::fprintf_td(stderr, "%s\n", __func__);
            const size_t buffer_size = 32768;
            const size_t feed_size = 32768;
            {
                const size_t file_idx = IDX_11kiB;
                {
                    // full speed, with content size
                    jau::io::ByteInStream_Feed data_feed(fname_payload_lst[file_idx], 500_ms);
                    std::thread feeder_thread= std::thread(&feed_source_10, &data_feed, feed_size);

                    bool res = transfer(data_feed, fname_payload_copy_lst[file_idx], buffer_size);
                    if( feeder_thread.joinable() ) {
                        feeder_thread.join();
                    }
                    REQUIRE( true == res );

                    jau::fs::file_stats out_stats(fname_payload_copy_lst[file_idx]);
                    REQUIRE( true == out_stats.exists() );
                    REQUIRE( true == out_stats.is_file() );
                    REQUIRE( data_feed.content_size() == out_stats.size() );
                    REQUIRE( fname_payload_size_lst[file_idx] == out_stats.size() );
                    REQUIRE( true == jau::fs::compare(data_feed.id(), out_stats.path(), true /* verbose */) );
                }
                {
                    // throttled, with content size
                    jau::io::ByteInStream_Feed data_feed(fname_payload_lst[file_idx], 500_ms);
                    std::thread feeder_thread= std::thread(&feed_source_01, &data_feed, feed_size);

                    bool res = transfer(data_feed, fname_payload_copy_lst[file_idx], buffer_size);
                    if( feeder_thread.joinable() ) {
                        feeder_thread.join();
                    }
                    REQUIRE( true == res );

                    jau::fs::file_stats out_stats(fname_payload_copy_lst[file_idx]);
                    REQUIRE( true == out_stats.exists() );
                    REQUIRE( true == out_stats.is_file() );
                    REQUIRE( data_feed.content_size() == out_stats.size() );
                    REQUIRE( fname_payload_size_lst[file_idx] == out_stats.size() );
                    REQUIRE( true == jau::fs::compare(data_feed.id(), out_stats.path(), true /* verbose */) );
                }
                {
                    // throttled, no content size, interruptReader() via set_eof() will avoid timeout
                    jau::io::ByteInStream_Feed data_feed(fname_payload_lst[file_idx], 500_ms);
                    std::thread feeder_thread= std::thread(&feed_source_00, &data_feed, feed_size);

                    bool res = transfer(data_feed, fname_payload_copy_lst[file_idx], buffer_size);
                    if( feeder_thread.joinable() ) {
                        feeder_thread.join();
                    }
                    REQUIRE( true == res );

                    jau::fs::file_stats out_stats(fname_payload_copy_lst[file_idx]);
                    REQUIRE( true == out_stats.exists() );
                    REQUIRE( true == out_stats.is_file() );
                    REQUIRE( data_feed.content_size() == 0 );
                    REQUIRE( fname_payload_size_lst[file_idx] == out_stats.size() );
                    REQUIRE( true == jau::fs::compare(data_feed.id(), out_stats.path(), true /* verbose */) );
                }
            }
        }

        void test22_copy_fed_ok_buff32k() {
            jau::fprintf_td(stderr, "\n"); jau::fprintf_td(stderr, "%s\n", __func__);
            const size_t buffer_size = 32768;
            const size_t feed_size = 32768;
            {
                const size_t file_idx = IDX_65MiB;
                {
                    // full speed, with content size
                    jau::io::ByteInStream_Feed data_feed(fname_payload_lst[file_idx], 500_ms);
                    std::thread feeder_thread= std::thread(&feed_source_10, &data_feed, feed_size);

                    bool res = transfer(data_feed, fname_payload_copy_lst[file_idx], buffer_size);
                    if( feeder_thread.joinable() ) {
                        feeder_thread.join();
                    }
                    REQUIRE( true == res );

                    jau::fs::file_stats out_stats(fname_payload_copy_lst[file_idx]);
                    REQUIRE( true == out_stats.exists() );
                    REQUIRE( true == out_stats.is_file() );
                    REQUIRE( data_feed.content_size() == out_stats.size() );
                    REQUIRE( fname_payload_size_lst[file_idx] == out_stats.size() );
                    REQUIRE( true == jau::fs::compare(data_feed.id(), out_stats.path(), true /* verbose */) );
                }
            }
        }

        void test23_copy_fed_irq() {
            jau::fprintf_td(stderr, "\n"); jau::fprintf_td(stderr, "%s\n", __func__);
            const size_t buffer_size = 4096;
            const size_t feed_size = 1024;
            {
                const size_t file_idx = IDX_65MiB;
                {
                    // full speed, no content size, interrupting @ 1024 bytes within our header
                    jau::io::ByteInStream_Feed data_feed(fname_payload_lst[file_idx], 500_ms);
                    std::thread feeder_thread= std::thread(&feed_source_20, &data_feed, feed_size);

                    bool res = transfer(data_feed, fname_payload_copy_lst[file_idx], buffer_size);
                    if( feeder_thread.joinable() ) {
                        feeder_thread.join();
                    }
                    REQUIRE( true == res );

                    jau::fs::file_stats out_stats(fname_payload_copy_lst[file_idx]);
                    REQUIRE( true == out_stats.exists() );
                    REQUIRE( true == out_stats.is_file() );
                    REQUIRE( false == data_feed.has_content_size() );
                    REQUIRE( 0 == data_feed.content_size() );
                    REQUIRE( fname_payload_size_lst[file_idx] > out_stats.size() ); // interrupted...
                    REQUIRE( false == jau::fs::compare(data_feed.id(), out_stats.path(), true /* verbose */) );
                }
                {
                    // full speed, with content size, interrupting 1/4 way
                    jau::io::ByteInStream_Feed data_feed(fname_payload_lst[file_idx], 500_ms);
                    std::thread feeder_thread= std::thread(&feed_source_21, &data_feed, feed_size);

                    bool res = transfer(data_feed, fname_payload_copy_lst[file_idx], buffer_size);
                    if( feeder_thread.joinable() ) {
                        feeder_thread.join();
                    }
                    REQUIRE( true == res );

                    jau::fs::file_stats out_stats(fname_payload_copy_lst[file_idx]);
                    REQUIRE( true == out_stats.exists() );
                    REQUIRE( true == out_stats.is_file() );
                    REQUIRE( true == data_feed.has_content_size() );
                    REQUIRE( fname_payload_size_lst[file_idx] == data_feed.content_size() );
                    REQUIRE( data_feed.content_size() > out_stats.size() ); // interrupted...
                    REQUIRE( false == jau::fs::compare(data_feed.id(), out_stats.path(), true /* verbose */) );
                }
            }
        }

};

std::vector<std::string> TestByteStream01::fname_payload_lst;
std::vector<std::string> TestByteStream01::fname_payload_copy_lst;
std::vector<uint64_t> TestByteStream01::fname_payload_size_lst;

METHOD_AS_TEST_CASE( TestByteStream01::test00a_protocols_error,           "TestByteStream01 test00a_protocols_error");
METHOD_AS_TEST_CASE( TestByteStream01::test00b_protocols_ok,              "TestByteStream01 test00b_protocols_ok");

METHOD_AS_TEST_CASE( TestByteStream01::test01_copy_file_ok_11kiB_buff4k,  "TestByteStream01 test01_copy_file_ok_11kiB_buff4k");
METHOD_AS_TEST_CASE( TestByteStream01::test02_copy_file_ok_65MiB_buff4k,  "TestByteStream01 test02_copy_file_ok_65MiB_buff4k");
METHOD_AS_TEST_CASE( TestByteStream01::test04_copy_file_ok_65MiB_buff32k, "TestByteStream01 test04_copy_file_ok_65MiB_buff32k");

METHOD_AS_TEST_CASE( TestByteStream01::test11_copy_http_ok_buff32k,       "TestByteStream01 test11_copy_http_ok");
METHOD_AS_TEST_CASE( TestByteStream01::test12_copy_http_404,              "TestByteStream01 test12_copy_http_404");

METHOD_AS_TEST_CASE( TestByteStream01::test20_copy_fed_ok_buff4k_feed1k,  "TestByteStream01 test20_copy_fed_ok_buff4k_feed1k");
METHOD_AS_TEST_CASE( TestByteStream01::test21_copy_fed_ok_buff32k,        "TestByteStream01 test21_copy_fed_ok_buff32k");
METHOD_AS_TEST_CASE( TestByteStream01::test22_copy_fed_ok_buff32k,        "TestByteStream01 test22_copy_fed_ok_buff32k");
METHOD_AS_TEST_CASE( TestByteStream01::test23_copy_fed_irq,               "TestByteStream01 test23_copy_fed_irq");